示例#1
0
文件: list.go 项目: omarqazi/guide
func (l *List) PlayDescriptor(ld generator.VideoDescriptor) error {
	params := ld.Params
	listId := params["list_id"]
	startString := params["start_at"]
	start, _ := strconv.Atoi(startString)
	list := datastore.List{Id: listId}
	programIds, err := list.Get()
	if err != nil {
		return err
	}

	segmentsToHold := start
	for i := 0; i < len(programIds); i++ {
		if updatedIds, err := list.Get(); err == nil {
			programIds = updatedIds
		}

		if hasIndex := (len(programIds) > i); !hasIndex {
			return nil
		}
		program, err := datastore.GetProgram(programIds[i])
		if err != nil {
			continue
		}

		childPipeline := generator.RunProgram(program)
		mediaChannel := childPipeline.MediaChannel()
		childPipeline.Close()

		for moreComing := true; moreComing; {
			select {
			case descriptor, more := <-mediaChannel:
				if more {
					if segmentsToHold > 0 {
						segmentsToHold--
					} else {
						l.Output <- descriptor
					}
				} else {
					moreComing = false
				}
			case <-l.CancelChan:
				return errors.New("cancelled")
			}
		}

		if updatedIds, err := list.Get(); err == nil {
			programIds = updatedIds
		}
	}

	return nil
}
示例#2
0
func (lac ListAPIController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	l := datastore.List{Id: r.URL.Path}

	if r.Method == "POST" {
		bytes, err := ioutil.ReadAll(r.Body)
		if err != nil {
			http.Error(w, "Error reading request body", 500)
			return
		}
		var newList []string
		if err := json.Unmarshal(bytes, &newList); err != nil {
			http.Error(w, "Error parsing request body JSON", 400)
			log.Println(string(bytes))
			return
		}

		if err := l.Set(newList); err != nil {
			http.Error(w, "Error saving list", 500)
			return
		}
	}

	items, err := l.Get()
	if err != nil {
		http.Error(w, "Error getting list", 500)
		return
	}

	bytes, err := json.Marshal(items)
	if err != nil {
		http.Error(w, "Error rendering list", 500)
		return
	}

	w.Header().Add("Content-Type", "application/json")
	fmt.Fprintln(w, string(bytes))
}