Skip to content

Commit 462a717

Browse files
committed
Answer to paused simucalst stream correctly
Answer to paused simucalst stream correctly
1 parent f369fda commit 462a717

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727

2828
sdpAttributeRid = "rid"
2929

30+
sdpAttributeSimulcast = "simulcast"
31+
3032
rtpOutboundMTU = 1200
3133

3234
rtpPayloadTypeBitmask = 0x7F

sdp.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,30 @@ func trackDetailsToRTPReceiveParameters(t *trackDetails) RTPReceiveParameters {
232232
return RTPReceiveParameters{Encodings: encodings}
233233
}
234234

235-
func getRids(media *sdp.MediaDescription) map[string]string {
236-
rids := map[string]string{}
235+
func getRids(media *sdp.MediaDescription) map[string]*simulcastRid {
236+
rids := map[string]*simulcastRid{}
237+
var simulcastAttr string
237238
for _, attr := range media.Attributes {
238239
if attr.Key == sdpAttributeRid {
239240
split := strings.Split(attr.Value, " ")
240-
rids[split[0]] = attr.Value
241+
rids[split[0]] = &simulcastRid{attrValue: attr.Value}
242+
} else if attr.Key == sdpAttributeSimulcast {
243+
simulcastAttr = attr.Value
244+
}
245+
}
246+
// process paused stream like "a=simulcast:send 1;~2;~3"
247+
if simulcastAttr != "" {
248+
if space := strings.Index(simulcastAttr, " "); space > 0 {
249+
simulcastAttr = simulcastAttr[space+1:]
250+
}
251+
ridStates := strings.Split(simulcastAttr, ";")
252+
for _, ridState := range ridStates {
253+
if ridState[:1] == "~" {
254+
rid := ridState[1:]
255+
if r, ok := rids[rid]; ok {
256+
r.paused = true
257+
}
258+
}
241259
}
242260
}
243261
return rids
@@ -379,7 +397,7 @@ func addSenderSDP(
379397
sendRids = append(sendRids, encoding.RID)
380398
}
381399
// Simulcast
382-
media.WithValueAttribute("simulcast", "send "+strings.Join(sendRids, ";"))
400+
media.WithValueAttribute(sdpAttributeSimulcast, "send "+strings.Join(sendRids, ";"))
383401
}
384402

385403
if !isPlanB {
@@ -475,10 +493,13 @@ func addTransceiverSDP(
475493

476494
for rid := range mediaSection.ridMap {
477495
media.WithValueAttribute(sdpAttributeRid, rid+" recv")
496+
if mediaSection.ridMap[rid].paused {
497+
rid = "~" + rid
498+
}
478499
recvRids = append(recvRids, rid)
479500
}
480501
// Simulcast
481-
media.WithValueAttribute("simulcast", "recv "+strings.Join(recvRids, ";"))
502+
media.WithValueAttribute(sdpAttributeSimulcast, "recv "+strings.Join(recvRids, ";"))
482503
}
483504

484505
addSenderSDP(mediaSection, isPlanB, media)
@@ -500,11 +521,16 @@ func addTransceiverSDP(
500521
return true, nil
501522
}
502523

524+
type simulcastRid struct {
525+
attrValue string
526+
paused bool
527+
}
528+
503529
type mediaSection struct {
504530
id string
505531
transceivers []*RTPTransceiver
506532
data bool
507-
ridMap map[string]string
533+
ridMap map[string]*simulcastRid
508534
}
509535

510536
func bundleMatchFromRemote(matchBundleGroup *string) func(mid string) bool {

sdp_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,14 @@ func TestPopulateSDP(t *testing.T) {
381381

382382
tr := &RTPTransceiver{kind: RTPCodecTypeVideo, api: api, codecs: me.videoCodecs}
383383
tr.setDirection(RTPTransceiverDirectionRecvonly)
384-
ridMap := map[string]string{
385-
"ridkey": "some",
384+
ridMap := map[string]*simulcastRid{
385+
"ridkey": {
386+
attrValue: "some",
387+
},
388+
"ridPaused": {
389+
attrValue: "some2",
390+
paused: true,
391+
},
386392
}
387393
mediaSections := []mediaSection{{id: "video", transceivers: []*RTPTransceiver{tr}, ridMap: ridMap}}
388394

@@ -392,21 +398,20 @@ func TestPopulateSDP(t *testing.T) {
392398
assert.Nil(t, err)
393399

394400
// Test contains rid map keys
395-
var found bool
401+
var ridFound int
396402
for _, desc := range offerSdp.MediaDescriptions {
397403
if desc.MediaName.Media != "video" {
398404
continue
399405
}
400-
for _, a := range desc.Attributes {
401-
if a.Key == sdpAttributeRid {
402-
if strings.Contains(a.Value, "ridkey") {
403-
found = true
404-
break
405-
}
406-
}
406+
ridInSDP := getRids(desc)
407+
if ridKey, ok := ridInSDP["ridkey"]; ok && !ridKey.paused {
408+
ridFound++
409+
}
410+
if ridPaused, ok := ridInSDP["ridPaused"]; ok && ridPaused.paused {
411+
ridFound++
407412
}
408413
}
409-
assert.Equal(t, true, found, "Rid key should be present")
414+
assert.Equal(t, 2, ridFound, "All rid keys should be present")
410415
})
411416
t.Run("SetCodecPreferences", func(t *testing.T) {
412417
se := SettingEngine{}

0 commit comments

Comments
 (0)