Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/vidplayer: add useful debug info to http response headers #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 additions & 2 deletions segmenter/video_segmenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ 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)
debug := vidplayer.VidPlayerDebug{DebugEnabled: false, LivepeerVersion: "deadbeef"}
player := vidplayer.NewVidPlayer(server, "", nil, debug)

player.HandleRTMPPlay(
func(url *url.URL) (stream.RTMPVideoStream, error) {
Expand Down Expand Up @@ -264,7 +265,8 @@ 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)
debug := vidplayer.VidPlayerDebug{DebugEnabled: false, LivepeerVersion: "deadbeef"}
player := vidplayer.NewVidPlayer(server, "", nil, debug)

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
}