Skip to content

Commit 1932439

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

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

peerconnection.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,31 @@ 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.mu.Lock()
873+
pc.removeRTPTransceiver(inactiveMidValue)
874+
pc.mu.Unlock()
851875
desc := SessionDescription{
852876
Type: SDPTypeAnswer,
853877
SDP: string(sdpBytes),
@@ -2264,6 +2288,31 @@ func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
22642288
pc.onNegotiationNeeded()
22652289
}
22662290

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

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

sdp.go

Lines changed: 10 additions & 2 deletions
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)