Skip to content

Commit

Permalink
mediaserver: Add stream keys for RTMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
j0sh committed Dec 18, 2018
1 parent ca2338a commit ebf0755
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion server/mediaserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package server

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -46,6 +47,7 @@ var ErrUnknownStream = errors.New("ErrUnknownStream")
const HLSWaitInterval = time.Second
const HLSBufferCap = uint(43200) //12 hrs assuming 1s segment
const HLSBufferWindow = uint(5)
const StreamKeyBytes = 6

const SegLen = 4 * time.Second
const BroadcastRetry = 15 * time.Second
Expand Down Expand Up @@ -167,7 +169,8 @@ func createRTMPStreamIDHandler(s *LivepeerServer) func(url *url.URL) (strmID str
}

// Generate RTMP part of StreamID
id, err := core.MakeStreamID(vid, "RTMP")
key := hex.EncodeToString(core.RandomIdGenerator(StreamKeyBytes))
id, err := core.MakeStreamID(vid, key)
if err != nil {
glog.Errorf("Error making stream ID")
return ""
Expand Down
11 changes: 9 additions & 2 deletions server/mediaserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package server

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"math/rand"
"net/url"
"sync"
"testing"
Expand Down Expand Up @@ -112,12 +114,16 @@ func TestCreateRTMPStreamHandler(t *testing.T) {
endHandler := endRTMPStreamHandler(s)

// Test hlsStreamID query param
expectedSid, _ := core.MakeStreamID(core.RandomVideoID(), "RTMP")
rand.Seed(123)
key := hex.EncodeToString(core.RandomIdGenerator(StreamKeyBytes))
expectedSid, _ := core.MakeStreamID(core.RandomVideoID(), key)
rand.Seed(123)
u, _ := url.Parse("rtmp://localhost?hlsStrmID=" + expectedSid.String())
if sid := createSid(u); sid != expectedSid.String() {
t.Error("Unexpected streamid")
}
// Test normal case
rand.Seed(123)
u, _ = url.Parse("rtmp://localhost")
st := stream.NewBasicRTMPVideoStream(createSid(u))
if st.GetStreamID() == "" {
Expand All @@ -128,14 +134,15 @@ func TestCreateRTMPStreamHandler(t *testing.T) {
t.Error("Handler failed ", err)
}
// Test collisions via stream reuse
u, _ = url.Parse("rtmp://localhost?hlsStrmID=" + st.GetStreamID())
rand.Seed(123)
if sid := createSid(u); sid != "" {
t.Error("Expected failure due to naming collision")
}
// Ensure the stream ID is reusable after the stream ends
if err := endHandler(u, st); err != nil {
t.Error("Could not clean up stream")
}
rand.Seed(123)
if sid := createSid(u); sid != st.GetStreamID() {
t.Error("Mismatched streamid during stream reuse")
}
Expand Down

0 comments on commit ebf0755

Please sign in to comment.