Skip to content

Commit

Permalink
fix(examples): update sdk + fix simulcast (#339)
Browse files Browse the repository at this point in the history
* fix(examples): update sdk + fix simulcast

* bump sdk version

* Fix simulcast layer change

Co-authored-by: OrlandoCo <[email protected]>
  • Loading branch information
tarrencev and OrlandoCo authored Dec 14, 2020
1 parent b3f1f2f commit 96dca78
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 28 deletions.
2 changes: 2 additions & 0 deletions cmd/signal/json-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"os"

_ "net/http/pprof"

"github.com/gorilla/websocket"
"github.com/sourcegraph/jsonrpc2"
websocketjsonrpc2 "github.com/sourcegraph/jsonrpc2/websocket"
Expand Down
15 changes: 10 additions & 5 deletions examples/echotest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ <h3>Pion</h3>
onclick="controlRemoteVideo(this)"
value="high"
name="optremotevideos"
checked
/>
High</label
>
Expand Down Expand Up @@ -328,7 +329,8 @@ <h3>Pion</h3>
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/json-rpc.min.js"></script>
<script>
const localVideo = document.getElementById("local-video");
const remoteVideo = document.getElementById("remote-video");
Expand All @@ -350,15 +352,18 @@ <h3>Pion</h3>
/* eslint-env browser */
const joinBtns = document.getElementById("start-btns");

const signalLocal = new IonSDK.IonSFUJSONRPCSignal(
const signalLocal = new Signal.IonSFUJSONRPCSignal(
"ws://localhost:7000/ws"
);
const signalRemote = new IonSDK.IonSFUJSONRPCSignal(
const signalRemote = new Signal.IonSFUJSONRPCSignal(
"ws://localhost:7000/ws"
);

const clientLocal = new IonSDK.Client("test session", signalLocal);
const clientRemote = new IonSDK.Client("test session", signalRemote);
const clientLocal = new IonSDK.Client(signalLocal);
const clientRemote = new IonSDK.Client(signalRemote);

signalLocal.onopen = () => clientLocal.join("test session");
signalRemote.onopen = () => clientRemote.join("test session");

let localStream;
const start = (sc) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Race Test
# Gallery Test

Race test provides a demonstration large session negotiation.
Gallery test provides a demonstration large session negotiation.

## Instructions

Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions examples/racetest/index.html → examples/gallerytest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
</style>

<title>Pion ion-sfu | Racetest</title>
<title>Pion ion-sfu | Gallery Test</title>
</head>

<body>
Expand Down Expand Up @@ -73,7 +73,8 @@ <h3>Pion</h3>
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/json-rpc.min.js"></script>
<script>
const localVideo = document.getElementById("local-video");
const remotesDiv = document.getElementById("remotes");
Expand All @@ -84,15 +85,17 @@ <h3>Pion</h3>

class Peer {
constructor() {
this.signal = new IonSDK.IonSFUJSONRPCSignal(
this.signal = new Signal.IonSFUJSONRPCSignal(
"ws://localhost:7000/ws"
);

const videosDiv = document.createElement("div");
videosDiv.style.display = "flex";
remotesDiv.appendChild(videosDiv);

this.client = new IonSDK.Client("test session", this.signal);
this.client = new IonSDK.Client(this.signal);
this.signal.onopen = () => this.client.join("test session");

this.client.ontrack = (track, stream) => {
if (track.kind === "video") {
const remoteVideo = document.createElement("video");
Expand Down
8 changes: 5 additions & 3 deletions examples/pubsubtest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ <h3>Pion</h3>
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ion-sdk.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/json-rpc.min.js"></script>
<script>
const localVideo = document.getElementById("local-video");
const remotesDiv = document.getElementById("remotes");
Expand All @@ -150,11 +151,12 @@ <h3>Pion</h3>
],
};

const signalLocal = new IonSDK.IonSFUJSONRPCSignal(
const signalLocal = new Signal.IonSFUJSONRPCSignal(
"ws://localhost:7000/ws"
);

const clientLocal = new IonSDK.Client("test session", signalLocal);
const clientLocal = new IonSDK.Client(signalLocal);
signalLocal.onopen = () => clientLocal.join("test session");

let localStream;
const start = () => {
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZw
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo=
github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
Expand All @@ -45,7 +46,9 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -335,13 +338,15 @@ github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDf
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.8.0 h1:zvJNkoCFAnYFNC24FV8nW4JdRJ3GIFcLbg65lL/JDcw=
github.com/prometheus/client_golang v1.8.0/go.mod h1:O9VU6huf47PktckDQfMTX0Y8tY0/7TSWwj+ITvv0TnM=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
Expand All @@ -351,6 +356,7 @@ github.com/prometheus/common v0.4.1 h1:K0MGApIoQvMw27RTdJkPbr3JZ7DNbtxQNyi5STVM6
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA=
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.14.0 h1:RHRyE8UocrbjU+6UvRzwi6HjiDfxrrBU91TtbKzkGp4=
github.com/prometheus/common v0.14.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
Expand All @@ -360,6 +366,7 @@ github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNG
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.2.0 h1:wH4vA7pcjKuZzjF7lM8awk4fnuJO6idemZXoKnULUx4=
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down
9 changes: 6 additions & 3 deletions pkg/sfu/downtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (d *DownTrack) SwitchSpatialLayer(targetLayer int) {
if d.currentSpatialLayer != d.simulcast.targetSpatialLayer {
return
}
if err := d.receiver.SubDownTrack(d, targetLayer); err != nil {
if err := d.receiver.SubDownTrack(d, targetLayer); err == nil {
d.simulcast.targetSpatialLayer = targetLayer
}
}
Expand Down Expand Up @@ -240,6 +240,9 @@ func (d *DownTrack) writeSimulcastRTP(pkt rtp.Packet) error {
// Check if packet SSRC is different from before
// if true, the video source changed
if d.lastSSRC != pkt.SSRC {
if d.currentSpatialLayer == d.simulcast.targetSpatialLayer && d.lastSSRC != 0 {
return nil
}
relay := false
// Wait for a keyframe to sync new source
switch d.mime {
Expand Down Expand Up @@ -270,14 +273,14 @@ func (d *DownTrack) writeSimulcastRTP(pkt rtp.Packet) error {
// Packet is not a keyframe, discard it
if !relay {
d.receiver.SendRTCP([]rtcp.Packet{
&rtcp.PictureLossIndication{SenderSSRC: pkt.SSRC, MediaSSRC: pkt.SSRC},
&rtcp.PictureLossIndication{SenderSSRC: d.ssrc, MediaSSRC: pkt.SSRC},
})
return nil
}
// Switch is done remove sender from previous layer
// and update current layer
if d.currentSpatialLayer != d.simulcast.targetSpatialLayer {
d.receiver.DeleteDownTrack(d.currentSpatialLayer, d.peerID)
go d.receiver.DeleteDownTrack(d.currentSpatialLayer, d.peerID)
}
d.currentSpatialLayer = d.simulcast.targetSpatialLayer
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/sfu/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"github.com/pion/webrtc/v3"
)

const (
maxSize = 1024
)

// Receiver defines a interface for a track receivers
type Receiver interface {
TrackID() string
Expand All @@ -34,7 +30,7 @@ type Receiver interface {

// WebRTCReceiver receives a video track
type WebRTCReceiver struct {
sync.RWMutex
sync.Mutex
rtcpMu sync.RWMutex

peerID string
Expand All @@ -55,7 +51,7 @@ type WebRTCReceiver struct {

// NewWebRTCReceiver creates a new webrtc track receivers
func NewWebRTCReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote, pid string) Receiver {
w := &WebRTCReceiver{
return &WebRTCReceiver{
peerID: pid,
receiver: receiver,
trackID: track.ID(),
Expand All @@ -64,8 +60,6 @@ func NewWebRTCReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote,
kind: track.Kind(),
isSimulcast: len(track.RID()) > 0,
}

return w
}

func (w *WebRTCReceiver) StreamID() string {
Expand Down Expand Up @@ -128,6 +122,7 @@ func (w *WebRTCReceiver) AddDownTrack(track *DownTrack, bestQualityFirst bool) {
}
}
track.currentSpatialLayer = layer
track.simulcast.targetSpatialLayer = layer
track.trackType = SimulcastDownTrack
} else {
track.trackType = SimpleDownTrack
Expand All @@ -141,7 +136,7 @@ func (w *WebRTCReceiver) AddDownTrack(track *DownTrack, bestQualityFirst bool) {
func (w *WebRTCReceiver) SubDownTrack(track *DownTrack, layer int) error {
w.Lock()
if dts := w.downTracks[layer]; dts != nil {
dts = append(dts, track)
w.downTracks[layer] = append(dts, track)
} else {
w.Unlock()
return errNoReceiverFound
Expand Down Expand Up @@ -215,13 +210,13 @@ func (w *WebRTCReceiver) readRTP(track *webrtc.TrackRemote, layer int) {
continue
}

w.RLock()
w.Lock()
for _, dt := range w.downTracks[layer] {
if err := dt.WriteRTP(pkt); err == io.EOF {
go w.DeleteDownTrack(layer, dt.id)
}
}
w.RUnlock()
w.Unlock()
}
}

Expand Down

0 comments on commit 96dca78

Please sign in to comment.