Skip to content

Commit

Permalink
core/vidplayer: add useful debug info to http response headers
Browse files Browse the repository at this point in the history
When viewing HLS manifests/segments, it is often useful to view
information on how those segments were generated. This commit adds
fields such as git-sha and node identifiers to the HTTP response
headers.
  • Loading branch information
emranemran committed Jun 13, 2022
1 parent f54db4f commit f0b0183
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion core/lpms.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type LPMSOpts struct {
// on the mux; LPMS won't initialize it.
// If set, HttpPort and HttpDisabled are ignored.
HttpMux *http.ServeMux

// Used to tag HTTP headers with useful debug info.
HttpDebug vidplayer.VidPlayerDebug
}

func defaultLPMSOpts(opts *LPMSOpts) {
Expand All @@ -78,7 +81,7 @@ func New(opts *LPMSOpts) *LPMS {
if !opts.HttpDisabled && opts.HttpMux == nil {
httpAddr = opts.HttpAddr
}
player := vidplayer.NewVidPlayer(rtmpServer, opts.VodPath, opts.HttpMux)
player := vidplayer.NewVidPlayer(rtmpServer, opts.VodPath, opts.HttpMux, opts.HttpDebug)
listener := &vidlistener.VidListener{RtmpServer: rtmpServer}
return &LPMS{vidPlayer: player, vidListener: listener, workDir: opts.WorkDir, rtmpAddr: opts.RtmpAddr, httpAddr: httpAddr}
}
Expand Down
4 changes: 2 additions & 2 deletions segmenter/video_segmenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestSegmenter(t *testing.T) {
opt := SegmenterOptions{SegLength: time.Second * 4}
vs := NewFFMpegVideoSegmenter(workDir, strm.GetStreamID(), strmUrl, opt)
server := &rtmp.Server{Addr: ":1939"}
player := vidplayer.NewVidPlayer(server, "", nil)
player := vidplayer.NewVidPlayer(server, "", nil, nil)

player.HandleRTMPPlay(
func(url *url.URL) (stream.RTMPVideoStream, error) {
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestSetStartSeq(t *testing.T) {
opt := SegmenterOptions{SegLength: time.Second * 4, StartSeq: startSeq}
vs := NewFFMpegVideoSegmenter(workDir, strm.GetStreamID(), strmUrl, opt)
server := &rtmp.Server{Addr: ":1936"}
player := vidplayer.NewVidPlayer(server, "", nil)
player := vidplayer.NewVidPlayer(server, "", nil, nil)

player.HandleRTMPPlay(
func(url *url.URL) (stream.RTMPVideoStream, error) {
Expand Down
25 changes: 24 additions & 1 deletion vidplayer/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ var ErrRTMP = errors.New("ErrRTMP")
var ErrHLS = errors.New("ErrHLS")
var PlaylistWaittime = 6 * time.Second

// vars to tag http response headers w/ debugging info
var (
lpVersion string
)

type VidPlayerDebug struct {
DebugEnabled bool
LivepeerVersion string
}

//VidPlayer is the module that handles playing video. For now we only support RTMP and HLS play.
type VidPlayer struct {
RtmpServer *joy4rtmp.Server
Expand All @@ -40,14 +50,15 @@ type VidPlayer struct {
func defaultRtmpPlayHandler(url *url.URL) (stream.RTMPVideoStream, error) { return nil, ErrRTMP }

//NewVidPlayer creates a new video player
func NewVidPlayer(rtmpS *joy4rtmp.Server, vodPath string, mux *http.ServeMux) *VidPlayer {
func NewVidPlayer(rtmpS *joy4rtmp.Server, vodPath string, mux *http.ServeMux, httpDebugHeaders VidPlayerDebug) *VidPlayer {
if mux == nil {
mux = http.DefaultServeMux
}
player := &VidPlayer{RtmpServer: rtmpS, VodPath: vodPath, rtmpPlayHandler: defaultRtmpPlayHandler, mux: mux}
if rtmpS != nil {
rtmpS.HandlePlay = player.rtmpServerHandlePlay()
}
player.setDebugHeaders(httpDebugHeaders)
return player
}

Expand Down Expand Up @@ -97,6 +108,15 @@ func (s *VidPlayer) HandleHLSPlay(
})
}

// Used to set header properties on HTTP responses generated by a B/O/T nodes.
// This is useful for debugging.
func (s *VidPlayer) setDebugHeaders(httpDebugHeaders VidPlayerDebug) {
if (!httpDebugHeaders.DebugEnabled) {
return
}
lpVersion = httpDebugHeaders.LivepeerVersion
}

func handleLive(w http.ResponseWriter, r *http.Request,
getMasterPlaylist func(url *url.URL) (*m3u8.MasterPlaylist, error),
getMediaPlaylist func(url *url.URL) (*m3u8.MediaPlaylist, error),
Expand All @@ -107,6 +127,7 @@ func handleLive(w http.ResponseWriter, r *http.Request,
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Expose-Headers", "Content-Length")
w.Header().Set("Cache-Control", "max-age=5")
w.Header().Set("Livepeer-Version", lpVersion)

ext := path.Ext(r.URL.Path)
if ".m3u8" == ext {
Expand Down Expand Up @@ -220,6 +241,8 @@ func handleVOD(url *url.URL, vodPath string, w http.ResponseWriter) error {
w.Write(dat)
}

w.Header().Set("Livepeer-Version", lpVersion)

http.Error(w, "Cannot find HTTP video resource: "+url.String(), 404)
return nil
}

0 comments on commit f0b0183

Please sign in to comment.