Set track level codec settings for all pending tracks (#1148)
This commit is contained in:
+30
-24
@@ -20,14 +20,15 @@ func (p *ParticipantImpl) setCodecPreferencesForPublisher(offer webrtc.SessionDe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
|
func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
|
||||||
parsed, lastAudio, err := p.TransportManager.GetLastUnmatchedMediaForOffer(offer, "audio")
|
parsed, unmatchAudios, err := p.TransportManager.GetUnmatchMediaForOffer(offer, "audio")
|
||||||
if err != nil || lastAudio == nil {
|
if err != nil || len(unmatchAudios) == 0 {
|
||||||
return offer
|
return offer
|
||||||
}
|
}
|
||||||
|
|
||||||
streamID, ok := lksdp.ExtractStreamID(lastAudio)
|
for _, unmatchAudio := range unmatchAudios {
|
||||||
|
streamID, ok := lksdp.ExtractStreamID(unmatchAudio)
|
||||||
if !ok {
|
if !ok {
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
p.pendingTracksLock.RLock()
|
p.pendingTracksLock.RLock()
|
||||||
@@ -35,13 +36,14 @@ func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.Se
|
|||||||
// if RED is disabled for this track, don't prefer RED codec in offer
|
// if RED is disabled for this track, don't prefer RED codec in offer
|
||||||
if info != nil && info.DisableRed {
|
if info != nil && info.DisableRed {
|
||||||
p.pendingTracksLock.RUnlock()
|
p.pendingTracksLock.RUnlock()
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
p.pendingTracksLock.RUnlock()
|
p.pendingTracksLock.RUnlock()
|
||||||
|
|
||||||
codecs, err := codecsFromMediaDescription(lastAudio)
|
codecs, err := codecsFromMediaDescription(unmatchAudio)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return offer
|
p.params.Logger.Errorw("extract codecs from media section failed", err, "media", unmatchAudio)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var opusPayload uint8
|
var opusPayload uint8
|
||||||
@@ -52,7 +54,7 @@ func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.Se
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if opusPayload == 0 {
|
if opusPayload == 0 {
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var preferredCodecs, leftCodecs []string
|
var preferredCodecs, leftCodecs []string
|
||||||
@@ -67,11 +69,12 @@ func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.Se
|
|||||||
|
|
||||||
// no opus/red found
|
// no opus/red found
|
||||||
if len(preferredCodecs) == 0 {
|
if len(preferredCodecs) == 0 {
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
lastAudio.MediaName.Formats = append(lastAudio.MediaName.Formats[:0], preferredCodecs...)
|
unmatchAudio.MediaName.Formats = append(unmatchAudio.MediaName.Formats[:0], preferredCodecs...)
|
||||||
lastAudio.MediaName.Formats = append(lastAudio.MediaName.Formats, leftCodecs...)
|
unmatchAudio.MediaName.Formats = append(unmatchAudio.MediaName.Formats, leftCodecs...)
|
||||||
|
}
|
||||||
|
|
||||||
bytes, err := parsed.Marshal()
|
bytes, err := parsed.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -86,21 +89,22 @@ func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.Se
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
|
func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
|
||||||
parsed, lastVideo, err := p.TransportManager.GetLastUnmatchedMediaForOffer(offer, "video")
|
parsed, unmatchVideos, err := p.TransportManager.GetUnmatchMediaForOffer(offer, "video")
|
||||||
if err != nil || lastVideo == nil {
|
if err != nil || len(unmatchVideos) == 0 {
|
||||||
return offer
|
return offer
|
||||||
}
|
}
|
||||||
// last video is pending for publish, set codec preference
|
// unmatched video is pending for publish, set codec preference
|
||||||
streamID, ok := lksdp.ExtractStreamID(lastVideo)
|
for _, unmatchVideo := range unmatchVideos {
|
||||||
|
streamID, ok := lksdp.ExtractStreamID(unmatchVideo)
|
||||||
if !ok {
|
if !ok {
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
p.pendingTracksLock.RLock()
|
p.pendingTracksLock.RLock()
|
||||||
_, info := p.getPendingTrack(streamID, livekit.TrackType_VIDEO)
|
_, info := p.getPendingTrack(streamID, livekit.TrackType_VIDEO)
|
||||||
if info == nil {
|
if info == nil {
|
||||||
p.pendingTracksLock.RUnlock()
|
p.pendingTracksLock.RUnlock()
|
||||||
return offer
|
continue
|
||||||
}
|
}
|
||||||
var mime string
|
var mime string
|
||||||
for _, c := range info.Codecs {
|
for _, c := range info.Codecs {
|
||||||
@@ -117,19 +121,20 @@ func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.Sess
|
|||||||
mime = strings.ToUpper(mime)
|
mime = strings.ToUpper(mime)
|
||||||
// remove dd extension if av1 not preferred
|
// remove dd extension if av1 not preferred
|
||||||
if !strings.Contains(mime, "AV1") {
|
if !strings.Contains(mime, "AV1") {
|
||||||
for i, attr := range lastVideo.Attributes {
|
for i, attr := range unmatchVideo.Attributes {
|
||||||
if strings.Contains(attr.Value, dd.ExtensionUrl) {
|
if strings.Contains(attr.Value, dd.ExtensionUrl) {
|
||||||
lastVideo.Attributes[i] = lastVideo.Attributes[len(lastVideo.Attributes)-1]
|
unmatchVideo.Attributes[i] = unmatchVideo.Attributes[len(unmatchVideo.Attributes)-1]
|
||||||
lastVideo.Attributes = lastVideo.Attributes[:len(lastVideo.Attributes)-1]
|
unmatchVideo.Attributes = unmatchVideo.Attributes[:len(unmatchVideo.Attributes)-1]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if mime != "" {
|
if mime != "" {
|
||||||
codecs, err := codecsFromMediaDescription(lastVideo)
|
codecs, err := codecsFromMediaDescription(unmatchVideo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return offer
|
p.params.Logger.Errorw("extract codecs from media section failed", err, "media", unmatchVideo)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var preferredCodecs, leftCodecs []string
|
var preferredCodecs, leftCodecs []string
|
||||||
@@ -141,8 +146,9 @@ func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.Sess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lastVideo.MediaName.Formats = append(lastVideo.MediaName.Formats[:0], preferredCodecs...)
|
unmatchVideo.MediaName.Formats = append(unmatchVideo.MediaName.Formats[:0], preferredCodecs...)
|
||||||
lastVideo.MediaName.Formats = append(lastVideo.MediaName.Formats, leftCodecs...)
|
unmatchVideo.MediaName.Formats = append(unmatchVideo.MediaName.Formats, leftCodecs...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := parsed.Marshal()
|
bytes, err := parsed.Marshal()
|
||||||
|
|||||||
+18
-19
@@ -336,7 +336,7 @@ func (t *TransportManager) createDataChannelsForSubscriber(pendingDataChannels [
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) GetLastUnmatchedMediaForOffer(offer webrtc.SessionDescription, mediaType string) (parsed *sdp.SessionDescription, unmatched *sdp.MediaDescription, err error) {
|
func (t *TransportManager) GetUnmatchMediaForOffer(offer webrtc.SessionDescription, mediaType string) (parsed *sdp.SessionDescription, unmatched []*sdp.MediaDescription, err error) {
|
||||||
// prefer codec from offer for clients that don't support setCodecPreferences
|
// prefer codec from offer for clients that don't support setCodecPreferences
|
||||||
parsed, err = offer.Unmarshal()
|
parsed, err = offer.Unmarshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -344,18 +344,7 @@ func (t *TransportManager) GetLastUnmatchedMediaForOffer(offer webrtc.SessionDes
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := len(parsed.MediaDescriptions) - 1; i >= 0; i-- {
|
var lastMatchedMid string
|
||||||
media := parsed.MediaDescriptions[i]
|
|
||||||
if media.MediaName.Media == mediaType {
|
|
||||||
unmatched = media
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if unmatched == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
lastAnswer := t.lastPublisherAnswer.Load()
|
lastAnswer := t.lastPublisherAnswer.Load()
|
||||||
if lastAnswer != nil {
|
if lastAnswer != nil {
|
||||||
answer := lastAnswer.(webrtc.SessionDescription)
|
answer := lastAnswer.(webrtc.SessionDescription)
|
||||||
@@ -366,16 +355,26 @@ func (t *TransportManager) GetLastUnmatchedMediaForOffer(offer webrtc.SessionDes
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range parsedAnswer.MediaDescriptions {
|
for i := len(parsedAnswer.MediaDescriptions) - 1; i >= 0; i-- {
|
||||||
mid, _ := m.Attribute(sdp.AttrKeyMID)
|
media := parsedAnswer.MediaDescriptions[i]
|
||||||
if lastMid, _ := unmatched.Attribute(sdp.AttrKeyMID); lastMid == mid {
|
if media.MediaName.Media == mediaType {
|
||||||
// mid matched, return
|
lastMatchedMid, _ = media.Attribute(sdp.AttrKeyMID)
|
||||||
unmatched = nil
|
break
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := len(parsed.MediaDescriptions) - 1; i >= 0; i-- {
|
||||||
|
media := parsed.MediaDescriptions[i]
|
||||||
|
if media.MediaName.Media == mediaType {
|
||||||
|
mid, _ := media.Attribute(sdp.AttrKeyMID)
|
||||||
|
if mid == lastMatchedMid {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
unmatched = append(unmatched, media)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user