Esempio n. 1
0
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
}
Esempio n. 2
0
func (p PreviewController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	urlBase := strings.Split(r.URL.Path, ".")[0]
	requestedProgram := urlBase

	if r.Method == "POST" {
		if newId := programPosted(w, r); newId == "" {
			return
		} else if requestedProgram == "new" || requestedProgram == "temp" {
			requestedProgram = newId
			fullUrl := fmt.Sprintf("/vod/%s.m3u8", requestedProgram)
			w.Header().Add("Location", fullUrl)
			w.Header().Add("X-Guide-Program", requestedProgram)
		}
	}

	program, err := datastore.GetProgram(requestedProgram)
	if err != nil {
		http.Error(w, "Program not found", 404)
		return
	}

	if urlBase != "temp" {
		program.Persist()
	}

	if filepath.Ext(r.URL.Path) == ".json" {
		w.Header().Add("Content-Type", "application/json")
		encoder := json.NewEncoder(w)
		if err := encoder.Encode(program); err != nil {
			http.Error(w, "Error serializing program", 500)
			return
		}
	} else {
		w.Header().Add("Content-Type", "application/x-mpegurl")
		playlist, _ := generator.Preview(program)
		fmt.Fprintln(w, playlist.Encode().String())
	}
}