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 14, 2018
1 parent 0391a6e commit 04e763b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 5 additions & 2 deletions 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,13 +47,14 @@ 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

var BroadcastPrice = big.NewInt(1)
var BroadcastJobVideoProfiles = []ffmpeg.VideoProfile{ffmpeg.P240p30fps4x3, ffmpeg.P360p30fps16x9}
var MinDepositSegmentCount = int64(75) // 5 mins assuming 4s segments
var MinDepositSegmentCount = int64(75) // 5 mins assuming 4s segments
var LastHLSStreamID core.StreamID
var LastManifestID core.ManifestID

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 04e763b

Please sign in to comment.