Set publisher codec preferences after setting remote description (#3913)

* Set publisher codec preferences after setting remote description

Munging SDP prior to setting remote description was becoming problematic
in single peer connection mode. In that mode, it is possible that a
subscribe track m-section is added which sets the fmtp of H.265 to a
value that is different from when that client publishes. That gets
locked in as negotiated codecs when pion processes remote description.
Later when the client publishes H.265, the H.265 does only partial
match. So, if we munge offer and send it to SetRemoteDescription, the
H.265 does only a partial match due to different fmtp line and that gets
put at the end of the list. So, the answer does not enforce the
preferred codec. Changing pion to put partial match up front is more
risky given other projects. So, switch codec preferences to after remote
description is set and directly operate on transceiver which is a better
place to make these changes without munging SDP.

This fixes the case of
- firefox joins first
- Chrome preferring H.265 joining next. This causes a subscribe track
  m-section (for firefox's tracks) to be created first. So, the
  preferred codec munging was not working. Works after this change.

* clean up

* mage generate

* test

* clean up
This commit is contained in:
Raja Subramanian
2025-09-10 18:28:36 +05:30
committed by GitHub
parent fc995533e1
commit eee2001a31
9 changed files with 235 additions and 157 deletions
+19 -16
View File
@@ -1259,22 +1259,6 @@ func (p *ParticipantImpl) HandleOffer(sd *livekit.SessionDescription) error {
}
}
unmatchAudios, unmatchVideos := p.populateSdpCid(parsedOffer)
parsedOffer = p.setCodecPreferencesForPublisher(parsedOffer, unmatchAudios, unmatchVideos)
p.updateRidsFromSDP(parsedOffer, unmatchVideos)
// put together munged offer after setting codec preferences
bytes, err := parsedOffer.Marshal()
if err != nil {
lgr.Errorw("failed to marshal offer", err, "parsedOffer", parsedOffer)
return err
}
offer = webrtc.SessionDescription{
Type: offer.Type,
SDP: string(bytes),
}
err = p.TransportManager.HandleOffer(offer, offerId, p.MigrateState() == types.MigrateStateInit)
if err != nil {
lgr.Warnw("could not handle offer", err, "mungedOffer", offer)
@@ -1291,6 +1275,21 @@ func (p *ParticipantImpl) HandleOffer(sd *livekit.SessionDescription) error {
return nil
}
func (p *ParticipantImpl) onPublisherSetRemoteDescription() {
offer := p.TransportManager.LastPublisherOfferPending()
parsedOffer, err := offer.Unmarshal()
if err != nil {
p.pubLogger.Warnw("could not parse offer", err)
return
}
// set publish codec preferences after remote description is set
// and required transceivers are created
unmatchAudios, unmatchVideos := p.populateSdpCid(parsedOffer)
p.setCodecPreferencesForPublisher(parsedOffer, unmatchAudios, unmatchVideos)
p.updateRidsFromSDP(parsedOffer, unmatchVideos)
}
func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, answerId uint32) error {
if p.IsClosed() || p.IsDisconnected() {
return nil
@@ -1921,6 +1920,10 @@ type PublisherTransportHandler struct {
AnyTransportHandler
}
func (h PublisherTransportHandler) OnSetRemoteDescriptionOffer() {
h.p.onPublisherSetRemoteDescription()
}
func (h PublisherTransportHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32) error {
return h.p.onPublisherAnswer(sd, answerId)
}