Skip to content

Commit 343a3dd

Browse files
authored
Add an error return to makeStreamID to make issues explicit (#400)
* Add an error return to makeStreamID to make issues explicit * Reinstate nil check
1 parent 11a5584 commit 343a3dd

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

cmd/example/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ func main() {
7777

7878
lpms.HandleRTMPPublish(
7979
//makeStreamID (give the stream an ID)
80-
func(url *url.URL) stream.AppData {
80+
func(url *url.URL) (stream.AppData, error) {
8181
s := exampleStream(randString(10))
82-
return &s
82+
return &s, nil
8383
},
8484

8585
//gotStream

cmd/example/simple/simple.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func main() {
4040
lpms := core.New(&core.LPMSOpts{WorkDir: fmt.Sprintf("%v/.tmp", dir)})
4141

4242
lpms.HandleRTMPPublish(
43-
func(url *url.URL) stream.AppData {
43+
func(url *url.URL) (stream.AppData, error) {
4444
glog.Infof("Stream has been started!: %v", url)
45-
return exampleStream(randString(10))
45+
return exampleStream(randString(10)), nil
4646
},
4747

4848
func(url *url.URL, rs stream.RTMPVideoStream) (err error) {

core/lpms.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//The RTMP server. This will put up a RTMP endpoint when starting up Swarm.
2-
//To integrate with LPMS means your code will become the source / destination of the media server.
3-
//This RTMP endpoint is mainly used for video upload. The expected url is rtmp://localhost:port/livepeer/stream
1+
// The RTMP server. This will put up a RTMP endpoint when starting up Swarm.
2+
// To integrate with LPMS means your code will become the source / destination of the media server.
3+
// This RTMP endpoint is mainly used for video upload. The expected url is rtmp://localhost:port/livepeer/stream
44
package core
55

66
import (
@@ -67,7 +67,7 @@ func defaultLPMSOpts(opts *LPMSOpts) {
6767
}
6868
}
6969

70-
//New creates a new LPMS server object. It really just brokers everything to the components.
70+
// New creates a new LPMS server object. It really just brokers everything to the components.
7171
func New(opts *LPMSOpts) *LPMS {
7272
defaultLPMSOpts(opts)
7373
var rtmpServer *joy4rtmp.Server
@@ -83,7 +83,7 @@ func New(opts *LPMSOpts) *LPMS {
8383
return &LPMS{vidPlayer: player, vidListener: listener, workDir: opts.WorkDir, rtmpAddr: opts.RtmpAddr, httpAddr: httpAddr}
8484
}
8585

86-
//Start starts the rtmp and http servers, and initializes ffmpeg
86+
// Start starts the rtmp and http servers, and initializes ffmpeg
8787
func (l *LPMS) Start(ctx context.Context) error {
8888
ec := make(chan error, 1)
8989
ffmpeg.InitFFmpeg()
@@ -114,21 +114,21 @@ func (l *LPMS) Start(ctx context.Context) error {
114114
return nil
115115
}
116116

117-
//HandleRTMPPublish offload to the video listener. To understand how it works, look at videoListener.HandleRTMPPublish.
117+
// HandleRTMPPublish offload to the video listener. To understand how it works, look at videoListener.HandleRTMPPublish.
118118
func (l *LPMS) HandleRTMPPublish(
119-
makeStreamID func(url *url.URL) (strmID stream.AppData),
119+
makeStreamID func(url *url.URL) (strmID stream.AppData, err error),
120120
gotStream func(url *url.URL, rtmpStrm stream.RTMPVideoStream) (err error),
121121
endStream func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error) {
122122

123123
l.vidListener.HandleRTMPPublish(makeStreamID, gotStream, endStream)
124124
}
125125

126-
//HandleRTMPPlay offload to the video player
126+
// HandleRTMPPlay offload to the video player
127127
func (l *LPMS) HandleRTMPPlay(getStream func(url *url.URL) (stream.RTMPVideoStream, error)) error {
128128
return l.vidPlayer.HandleRTMPPlay(getStream)
129129
}
130130

131-
//HandleHLSPlay offload to the video player
131+
// HandleHLSPlay offload to the video player
132132
func (l *LPMS) HandleHLSPlay(
133133
getMasterPlaylist func(url *url.URL) (*m3u8.MasterPlaylist, error),
134134
getMediaPlaylist func(url *url.URL) (*m3u8.MediaPlaylist, error),
@@ -137,7 +137,7 @@ func (l *LPMS) HandleHLSPlay(
137137
l.vidPlayer.HandleHLSPlay(getMasterPlaylist, getMediaPlaylist, getSegment)
138138
}
139139

140-
//SegmentRTMPToHLS takes a rtmp stream and re-packages it into a HLS stream with the specified segmenter options
140+
// SegmentRTMPToHLS takes a rtmp stream and re-packages it into a HLS stream with the specified segmenter options
141141
func (l *LPMS) SegmentRTMPToHLS(ctx context.Context, rs stream.RTMPVideoStream, hs stream.HLSVideoStream, segOptions segmenter.SegmenterOptions) error {
142142
// set localhost if necessary. Check more problematic addrs? [::] ?
143143
rtmpAddr := l.rtmpAddr

vidlistener/listener.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ type VidListener struct {
2222
RtmpServer *joy4rtmp.Server
2323
}
2424

25-
//HandleRTMPPublish takes 3 parameters - makeStreamID, gotStream, and endStream.
26-
//makeStreamID is called when the stream starts. It should return a streamID from the requestURL.
27-
//gotStream is called when the stream starts. It gives you access to the stream.
28-
//endStream is called when the stream ends. It gives you access to the stream.
25+
// HandleRTMPPublish takes 3 parameters - makeStreamID, gotStream, and endStream.
26+
// makeStreamID is called when the stream starts. It should return a streamID from the requestURL.
27+
// gotStream is called when the stream starts. It gives you access to the stream.
28+
// endStream is called when the stream ends. It gives you access to the stream.
2929
func (self *VidListener) HandleRTMPPublish(
30-
makeStreamID func(url *url.URL) (strmID stream.AppData),
30+
makeStreamID func(url *url.URL) (strmID stream.AppData, err error),
3131
gotStream func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error,
3232
endStream func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error) {
3333

3434
if self.RtmpServer != nil {
3535
self.RtmpServer.HandlePublish = func(conn *joy4rtmp.Conn) {
3636
glog.V(2).Infof("RTMP server got upstream: %v", conn.URL)
3737

38-
strmID := makeStreamID(conn.URL)
39-
if strmID == nil || strmID.StreamID() == "" {
38+
strmID, err := makeStreamID(conn.URL)
39+
if err != nil || strmID == nil || strmID.StreamID() == "" {
4040
conn.Close()
4141
return
4242
}

vidlistener/listener_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestListener(t *testing.T) {
3131

3232
listener.HandleRTMPPublish(
3333
//makeStreamID
34-
func(url *url.URL) stream.AppData {
35-
return newTestStream()
34+
func(url *url.URL) (stream.AppData, error) {
35+
return newTestStream(), nil
3636
},
3737
//gotStream
3838
func(url *url.URL, rtmpStrm stream.RTMPVideoStream) (err error) {
@@ -82,8 +82,8 @@ func TestListenerError(t *testing.T) {
8282
failures := 0
8383
badListener.HandleRTMPPublish(
8484
//makeStreamID
85-
func(url *url.URL) stream.AppData {
86-
return newTestStream()
85+
func(url *url.URL) (stream.AppData, error) {
86+
return newTestStream(), nil
8787
},
8888
//gotStream
8989
func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error {
@@ -123,9 +123,9 @@ func TestListenerEmptyStreamID(t *testing.T) {
123123

124124
badListener.HandleRTMPPublish(
125125
//makeStreamID
126-
func(url *url.URL) stream.AppData {
126+
func(url *url.URL) (stream.AppData, error) {
127127
// On returning empty stream id connection should be closed
128-
return newTestStream()
128+
return newTestStream(), nil
129129
},
130130
//gotStream
131131
func(url *url.URL, rtmpStrm stream.RTMPVideoStream) error {

0 commit comments

Comments
 (0)