Handle migration better in single peer connection case. (#3886)

* Handle migration better in single peer connection case.

Have to avoid creating a sender for publish tracks.
Otherwise, the sender messes up the answer SDP because
it forced to inactive and that gets used by type match when setting
remote description offer.

Create sender only for m-lines which was used to send.

* remove unnecessary RemoveTrack
This commit is contained in:
Raja Subramanian
2025-08-29 18:18:32 +05:30
committed by GitHub
parent 5026de2bea
commit 38f7906ecb
2 changed files with 44 additions and 21 deletions
+43 -20
View File
@@ -1951,6 +1951,18 @@ func (t *PCTransport) initPCWithPreviousAnswer(previousAnswer webrtc.SessionDesc
continue continue
} }
if !t.params.IsOfferer {
// `sendrecv` or `sendonly` means this transceiver is used for sending
// Note that a transceiver previously used to send could be `inactive`.
// Let those transceivers be created when remote description is set.
_, ok1 := m.Attribute(webrtc.RTPTransceiverDirectionSendrecv.String())
_, ok2 := m.Attribute(webrtc.RTPTransceiverDirectionSendonly.String())
if !ok1 && !ok2 {
continue
}
}
tr, err := t.pc.AddTransceiverFromKind( tr, err := t.pc.AddTransceiverFromKind(
codecType, codecType,
webrtc.RTPTransceiverInit{ webrtc.RTPTransceiverInit{
@@ -1976,35 +1988,46 @@ func (t *PCTransport) initPCWithPreviousAnswer(previousAnswer webrtc.SessionDesc
return senders, nil return senders, nil
} }
func (t *PCTransport) SetPreviousSdp(offer, answer *webrtc.SessionDescription) { func (t *PCTransport) SetPreviousSdp(localDescription, remoteDescription *webrtc.SessionDescription) {
// when there is no answer, cannot migrate, force a full reconnect // when there is no answer, cannot migrate, force a full reconnect
if answer == nil { if (t.params.IsOfferer && remoteDescription == nil) || (!t.params.IsOfferer && localDescription == nil) {
t.onNegotiationFailed(true, "no previous answer") t.onNegotiationFailed(true, "no previous answer")
return return
} }
t.lock.Lock() t.lock.Lock()
if t.pc.RemoteDescription() == nil && t.previousAnswer == nil { var (
if t.params.IsOfferer { senders map[string]*webrtc.RTPSender
t.previousAnswer = answer err error
parseMids bool
)
if t.params.IsOfferer {
if t.pc.RemoteDescription() == nil && t.previousAnswer == nil {
t.previousAnswer = remoteDescription
senders, err = t.initPCWithPreviousAnswer(*remoteDescription)
parseMids = true
} }
senders, err := t.initPCWithPreviousAnswer(*answer) } else {
if err != nil { if t.pc.LocalDescription() == nil {
t.lock.Unlock() senders, err = t.initPCWithPreviousAnswer(*localDescription)
parseMids = true
t.onNegotiationFailed(true, fmt.Sprintf("initPCWithPreviousAnswer failed, error: %s", err))
return
} }
}
if err != nil {
t.lock.Unlock()
t.onNegotiationFailed(true, fmt.Sprintf("initPCWithPreviousAnswer failed, error: %s", err))
return
}
if offer != nil { if localDescription != nil && parseMids {
// in migration case, can't reuse transceiver before negotiated except track subscribed at previous node // in migration case, can't reuse transceiver before negotiating excepted tracks
t.canReuseTransceiver = false // that were subscribed at previous node
if err := t.parseTrackMid(*offer, senders); err != nil { t.canReuseTransceiver = false
t.params.Logger.Warnw( if err := t.parseTrackMid(*localDescription, senders); err != nil {
"parse previous offer failed", err, t.params.Logger.Warnw(
"offer", offer.SDP, "parse previous local description failed", err,
) "localDescription", localDescription.SDP,
} )
} }
} }
+1 -1
View File
@@ -816,7 +816,7 @@ func (t *TransportManager) SetMigrateInfo(
} }
if t.params.UseSinglePeerConnection { if t.params.UseSinglePeerConnection {
t.publisher.SetPreviousSdp(previousOffer, previousAnswer) t.publisher.SetPreviousSdp(previousAnswer, previousOffer)
} else { } else {
t.subscriber.SetPreviousSdp(previousOffer, previousAnswer) t.subscriber.SetPreviousSdp(previousOffer, previousAnswer)
} }