Skip to content

Commit 0ea417e

Browse files
committed
feat: delete rejected transceiver.
1 parent 49b555b commit 0ea417e

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

peerconnection.go

+58-2
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,29 @@ func (pc *PeerConnection) CreateAnswer(*AnswerOptions) (SessionDescription, erro
847847
if err != nil {
848848
return SessionDescription{}, err
849849
}
850-
850+
// remove inactive media sections
851+
var inactiveMidValue []string
852+
for _, media := range d.MediaDescriptions {
853+
if media.MediaName.Media == mediaSectionApplication {
854+
continue
855+
}
856+
if media.MediaName.Port.Value == 0 {
857+
var midValue string
858+
var inactive bool
859+
for _, attr := range media.Attributes {
860+
if attr.Key == sdp.AttrKeyInactive {
861+
inactive = true
862+
}
863+
if attr.Key == sdp.AttrKeyMID {
864+
midValue = attr.Value
865+
}
866+
}
867+
if inactive {
868+
inactiveMidValue = append(inactiveMidValue, midValue)
869+
}
870+
}
871+
}
872+
pc.removeRTPTransceiver(inactiveMidValue)
851873
desc := SessionDescription{
852874
Type: SDPTypeAnswer,
853875
SDP: string(sdpBytes),
@@ -2264,6 +2286,31 @@ func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
22642286
pc.onNegotiationNeeded()
22652287
}
22662288

2289+
// removeRTPTransceiver remove inactive
2290+
// and fires onNegotiationNeeded;
2291+
// caller of this method should hold `pc.mu` lock
2292+
func (pc *PeerConnection) removeRTPTransceiver(mids []string) {
2293+
if len(mids) == 0 {
2294+
return
2295+
}
2296+
n := 0
2297+
for _, transceiver := range pc.rtpTransceivers {
2298+
needDelete := false
2299+
for _, mid := range mids {
2300+
if transceiver.Mid() == mid {
2301+
transceiver.Stop()
2302+
needDelete = true
2303+
}
2304+
}
2305+
if !needDelete {
2306+
pc.rtpTransceivers[n] = transceiver
2307+
n++
2308+
}
2309+
}
2310+
pc.rtpTransceivers = pc.rtpTransceivers[:n]
2311+
pc.onNegotiationNeeded()
2312+
}
2313+
22672314
// CurrentLocalDescription represents the local description that was
22682315
// successfully negotiated the last time the PeerConnection transitioned
22692316
// into the stable state plus any local candidates that have been generated
@@ -2628,7 +2675,16 @@ func (pc *PeerConnection) generateMatchedSDP(transceivers []*RTPTransceiver, use
26282675
mediaTransceivers := []*RTPTransceiver{t}
26292676

26302677
extensions, _ := rtpExtensionsFromMediaDescription(media)
2631-
mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, matchExtensions: extensions, rids: getRids(media)})
2678+
rejected := false
2679+
if media.MediaName.Port.Value == 0 {
2680+
for _, attr := range media.Attributes {
2681+
if attr.Key == sdp.AttrKeyInactive {
2682+
rejected = true
2683+
break
2684+
}
2685+
}
2686+
}
2687+
mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, matchExtensions: extensions, rids: getRids(media), rejected: rejected})
26322688
}
26332689
}
26342690

sdp.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,12 @@ func addTransceiverSDP(
467467
media.WithValueAttribute("rtcp-fb", fmt.Sprintf("%d %s %s", codec.PayloadType, feedback.Type, feedback.Parameter))
468468
}
469469
}
470-
if len(codecs) == 0 {
470+
if len(codecs) == 0 || mediaSection.rejected {
471471
// If we are sender and we have no codecs throw an error early
472472
if t.Sender() != nil {
473-
return false, ErrSenderWithNoCodecs
473+
if !mediaSection.rejected {
474+
return false, ErrSenderWithNoCodecs
475+
}
474476
}
475477

476478
// Explicitly reject track if we don't have the codec
@@ -492,8 +494,13 @@ func addTransceiverSDP(
492494
Address: "0.0.0.0",
493495
},
494496
},
497+
Attributes: []sdp.Attribute{
498+
{Key: RTPTransceiverDirectionInactive.String(), Value: ""},
499+
{Key: "mid", Value: midValue},
500+
},
495501
})
496502
return false, nil
503+
497504
}
498505

499506
directions := []RTPTransceiverDirection{}
@@ -564,6 +571,7 @@ type mediaSection struct {
564571
data bool
565572
matchExtensions map[string]int
566573
rids []*simulcastRid
574+
rejected bool
567575
}
568576

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

0 commit comments

Comments
 (0)