Skip to content

Commit

Permalink
feat: delete rejected transceiver.
Browse files Browse the repository at this point in the history
  • Loading branch information
o-u-p committed Jan 16, 2025
1 parent 49b555b commit 1932439
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
62 changes: 60 additions & 2 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,31 @@ func (pc *PeerConnection) CreateAnswer(*AnswerOptions) (SessionDescription, erro
if err != nil {
return SessionDescription{}, err
}

// remove inactive media sections
var inactiveMidValue []string
for _, media := range d.MediaDescriptions {
if media.MediaName.Media == mediaSectionApplication {
continue
}
if media.MediaName.Port.Value == 0 {
var midValue string
var inactive bool
for _, attr := range media.Attributes {
if attr.Key == sdp.AttrKeyInactive {
inactive = true
}
if attr.Key == sdp.AttrKeyMID {
midValue = attr.Value
}
}
if inactive {
inactiveMidValue = append(inactiveMidValue, midValue)
}
}
}
pc.mu.Lock()
pc.removeRTPTransceiver(inactiveMidValue)
pc.mu.Unlock()
desc := SessionDescription{
Type: SDPTypeAnswer,
SDP: string(sdpBytes),
Expand Down Expand Up @@ -2264,6 +2288,31 @@ func (pc *PeerConnection) addRTPTransceiver(t *RTPTransceiver) {
pc.onNegotiationNeeded()
}

// removeRTPTransceiver remove inactive
// and fires onNegotiationNeeded;
// caller of this method should hold `pc.mu` lock
func (pc *PeerConnection) removeRTPTransceiver(mids []string) {
if len(mids) == 0 {
return
}
n := 0
for _, transceiver := range pc.rtpTransceivers {
needDelete := false
for _, mid := range mids {
if transceiver.Mid() == mid {
transceiver.Stop()
needDelete = true
}
}
if !needDelete {
pc.rtpTransceivers[n] = transceiver
n++
}
}
pc.rtpTransceivers = pc.rtpTransceivers[:n]
pc.onNegotiationNeeded()
}

// CurrentLocalDescription represents the local description that was
// successfully negotiated the last time the PeerConnection transitioned
// into the stable state plus any local candidates that have been generated
Expand Down Expand Up @@ -2628,7 +2677,16 @@ func (pc *PeerConnection) generateMatchedSDP(transceivers []*RTPTransceiver, use
mediaTransceivers := []*RTPTransceiver{t}

extensions, _ := rtpExtensionsFromMediaDescription(media)
mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, matchExtensions: extensions, rids: getRids(media)})
rejected := false
if media.MediaName.Port.Value == 0 {
for _, attr := range media.Attributes {
if attr.Key == sdp.AttrKeyInactive {
rejected = true
break
}
}
}
mediaSections = append(mediaSections, mediaSection{id: midValue, transceivers: mediaTransceivers, matchExtensions: extensions, rids: getRids(media), rejected: rejected})
}
}

Expand Down
12 changes: 10 additions & 2 deletions sdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,12 @@ func addTransceiverSDP(
media.WithValueAttribute("rtcp-fb", fmt.Sprintf("%d %s %s", codec.PayloadType, feedback.Type, feedback.Parameter))
}
}
if len(codecs) == 0 {
if len(codecs) == 0 || mediaSection.rejected {
// If we are sender and we have no codecs throw an error early
if t.Sender() != nil {
return false, ErrSenderWithNoCodecs
if !mediaSection.rejected {
return false, ErrSenderWithNoCodecs
}
}

// Explicitly reject track if we don't have the codec
Expand All @@ -492,8 +494,13 @@ func addTransceiverSDP(
Address: "0.0.0.0",
},
},
Attributes: []sdp.Attribute{
{Key: RTPTransceiverDirectionInactive.String(), Value: ""},
{Key: "mid", Value: midValue},
},
})
return false, nil

}

directions := []RTPTransceiverDirection{}
Expand Down Expand Up @@ -564,6 +571,7 @@ type mediaSection struct {
data bool
matchExtensions map[string]int
rids []*simulcastRid
rejected bool
}

func bundleMatchFromRemote(matchBundleGroup *string) func(mid string) bool {
Expand Down

0 comments on commit 1932439

Please sign in to comment.