rtc: parse each remote offer once per negotiation (#4904)
This commit is contained in:
+65
-31
@@ -300,6 +300,9 @@ type PCTransport struct {
|
||||
signalStateCheckTimer *time.Timer
|
||||
currentOfferIceCredential string // ice user:pwd, for publish side ice restart checking
|
||||
pendingRestartIceOffer *webrtc.SessionDescription
|
||||
// parsed forms kept so that a remote description is parsed once per negotiation
|
||||
pendingRestartIceOfferParsed *sdp.SessionDescription
|
||||
remoteOfferParsed *sdp.SessionDescription
|
||||
}
|
||||
|
||||
type TransportParams struct {
|
||||
@@ -1646,6 +1649,7 @@ func (t *PCTransport) clearConnTimer() {
|
||||
}
|
||||
|
||||
func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remoteId uint32) error {
|
||||
parsed, parseErr := sd.Unmarshal()
|
||||
if t.params.UseOneShotSignallingMode {
|
||||
if sd.Type == webrtc.SDPTypeOffer {
|
||||
remoteOfferId := t.remoteOfferId.Load()
|
||||
@@ -1666,8 +1670,7 @@ func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remo
|
||||
}
|
||||
|
||||
// add remote candidates to ICE connection details
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err == nil {
|
||||
if parseErr == nil {
|
||||
addRemoteICECandidates := func(attrs []sdp.Attribute) {
|
||||
for _, a := range attrs {
|
||||
if a.IsICECandidate() {
|
||||
@@ -1686,11 +1689,11 @@ func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remo
|
||||
}
|
||||
}
|
||||
|
||||
err = t.pc.SetRemoteDescription(sd)
|
||||
if err != nil {
|
||||
if err := t.pc.SetRemoteDescription(sd); err != nil {
|
||||
t.params.Logger.Errorw("could not set remote description on synchronous mode peer connection", err)
|
||||
return err
|
||||
}
|
||||
t.setRemoteOfferParsed(sd.Type, parsed)
|
||||
|
||||
rtxRepairs := nonSimulcastRTXRepairsFromSDP(parsed, t.params.Logger)
|
||||
if len(rtxRepairs) > 0 {
|
||||
@@ -1706,6 +1709,8 @@ func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remo
|
||||
signal: signalRemoteDescriptionReceived,
|
||||
data: remoteDescriptionData{
|
||||
sessionDescription: &sd,
|
||||
parsed: parsed,
|
||||
parseErr: parseErr,
|
||||
remoteId: remoteId,
|
||||
},
|
||||
})
|
||||
@@ -1907,6 +1912,7 @@ func (t *PCTransport) HandleICERestartSDPFragment(sdpFragment string) (string, e
|
||||
t.params.Logger.Warnw("could not set remote description", err)
|
||||
return "", err
|
||||
}
|
||||
t.setRemoteOfferParsed(sd.Type, parsedRemote)
|
||||
|
||||
// clear out connection details on ICE restart and re-populate
|
||||
t.connectionDetails.Clear()
|
||||
@@ -2289,6 +2295,23 @@ func (t *PCTransport) SetPreviousSdp(localDescription, remoteDescription *webrtc
|
||||
t.lock.Unlock()
|
||||
}
|
||||
|
||||
func (t *PCTransport) setRemoteOfferParsed(sdType webrtc.SDPType, parsed *sdp.SessionDescription) {
|
||||
if sdType != webrtc.SDPTypeOffer {
|
||||
return
|
||||
}
|
||||
t.lock.Lock()
|
||||
t.remoteOfferParsed = parsed
|
||||
t.lock.Unlock()
|
||||
}
|
||||
|
||||
// RemoteOfferParsed returns the parsed form of the last remote offer set on the peer connection,
|
||||
// candidates filtered the same way pion saw them
|
||||
func (t *PCTransport) RemoteOfferParsed() *sdp.SessionDescription {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
return t.remoteOfferParsed
|
||||
}
|
||||
|
||||
func (t *PCTransport) parseTrackMid(sd webrtc.SessionDescription, senders map[string]*webrtc.RTPSender) error {
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err != nil {
|
||||
@@ -2369,22 +2392,17 @@ func (t *PCTransport) handleICEGatheringCompleteAnswerer() error {
|
||||
}
|
||||
|
||||
offer := *t.pendingRestartIceOffer
|
||||
parsed := t.pendingRestartIceOfferParsed
|
||||
t.pendingRestartIceOffer = nil
|
||||
t.pendingRestartIceOfferParsed = nil
|
||||
|
||||
t.params.Logger.Debugw("accept remote restart ice offer after ICE gathering")
|
||||
|
||||
// Parse the offer payload types before SetRemoteDescription so this does not
|
||||
// race with pion's use of the same description.
|
||||
var offerAudioPT map[mime.MimeType]webrtc.PayloadType
|
||||
if parsed, err := offer.Unmarshal(); err == nil {
|
||||
offerAudioPT = offerAudioPayloadTypes(parsed)
|
||||
}
|
||||
|
||||
if err := t.setRemoteDescription(offer); err != nil {
|
||||
if err := t.setRemoteDescription(offer, parsed); err != nil {
|
||||
return err
|
||||
}
|
||||
t.params.Handler.OnSetRemoteDescriptionOffer()
|
||||
t.processSendersPendingConfig(offerAudioPT)
|
||||
t.processSendersPendingConfig(offerAudioPayloadTypes(parsed))
|
||||
|
||||
return t.createAndSendAnswer()
|
||||
}
|
||||
@@ -2498,10 +2516,23 @@ func (t *PCTransport) isCandidateFilterActive(preferTCP bool) bool {
|
||||
}
|
||||
|
||||
func (t *PCTransport) filterCandidates(sd webrtc.SessionDescription, preferTCP, isLocal bool) webrtc.SessionDescription {
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err != nil {
|
||||
t.params.Logger.Warnw("could not unmarshal SDP to filter candidates", err)
|
||||
return sd
|
||||
return t.filterCandidatesParsed(sd, nil, preferTCP, isLocal)
|
||||
}
|
||||
|
||||
// filterCandidatesParsed filters using an already parsed description when available,
|
||||
// the parsed description is modified in place to match the returned SDP
|
||||
func (t *PCTransport) filterCandidatesParsed(
|
||||
sd webrtc.SessionDescription,
|
||||
parsed *sdp.SessionDescription,
|
||||
preferTCP, isLocal bool,
|
||||
) webrtc.SessionDescription {
|
||||
if parsed == nil {
|
||||
var err error
|
||||
parsed, err = sd.Unmarshal()
|
||||
if err != nil {
|
||||
t.params.Logger.Warnw("could not unmarshal SDP to filter candidates", err)
|
||||
return sd
|
||||
}
|
||||
}
|
||||
|
||||
filterAttributes := func(attrs []sdp.Attribute) []sdp.Attribute {
|
||||
@@ -2727,15 +2758,20 @@ func (t *PCTransport) handleSendOffer(_ event) error {
|
||||
|
||||
type remoteDescriptionData struct {
|
||||
sessionDescription *webrtc.SessionDescription
|
||||
parsed *sdp.SessionDescription
|
||||
parseErr error // set when parsed is nil
|
||||
remoteId uint32
|
||||
}
|
||||
|
||||
func (t *PCTransport) handleRemoteDescriptionReceived(e event) error {
|
||||
rdd := e.data.(remoteDescriptionData)
|
||||
if rdd.parsed == nil {
|
||||
return rdd.parseErr
|
||||
}
|
||||
if rdd.sessionDescription.Type == webrtc.SDPTypeOffer {
|
||||
return t.handleRemoteOfferReceived(rdd.sessionDescription, rdd.remoteId)
|
||||
return t.handleRemoteOfferReceived(rdd.sessionDescription, rdd.parsed, rdd.remoteId)
|
||||
} else {
|
||||
return t.handleRemoteAnswerReceived(rdd.sessionDescription, rdd.remoteId)
|
||||
return t.handleRemoteAnswerReceived(rdd.sessionDescription, rdd.parsed, rdd.remoteId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2751,13 +2787,13 @@ func (t *PCTransport) isRemoteOfferRestartICE(parsed *sdp.SessionDescription) (s
|
||||
return credential, restartICE, nil
|
||||
}
|
||||
|
||||
func (t *PCTransport) setRemoteDescription(sd webrtc.SessionDescription) error {
|
||||
func (t *PCTransport) setRemoteDescription(sd webrtc.SessionDescription, parsed *sdp.SessionDescription) error {
|
||||
// filter before setting remote description so that pion does not see filtered remote candidates
|
||||
preferTCP := t.preferTCP.Load()
|
||||
if t.isCandidateFilterActive(preferTCP) {
|
||||
t.params.Logger.Debugw("remote description (unfiltered)", "type", sd.Type, "sdp", sd.SDP)
|
||||
}
|
||||
sd = t.filterCandidates(sd, preferTCP, false)
|
||||
sd = t.filterCandidatesParsed(sd, parsed, preferTCP, false)
|
||||
if t.isCandidateFilterActive(preferTCP) {
|
||||
t.params.Logger.Debugw("remote description (filtered)", "type", sd.Type, "sdp", sd.SDP)
|
||||
}
|
||||
@@ -2774,7 +2810,9 @@ func (t *PCTransport) setRemoteDescription(sd webrtc.SessionDescription) error {
|
||||
}
|
||||
prometheus.RecordServiceOperationError(sdpType, "remote_description")
|
||||
return errors.Wrap(err, "setting remote description failed")
|
||||
} else if sd.Type == webrtc.SDPTypeAnswer {
|
||||
}
|
||||
t.setRemoteOfferParsed(sd.Type, parsed)
|
||||
if sd.Type == webrtc.SDPTypeAnswer {
|
||||
t.lock.Lock()
|
||||
if !t.canReuseTransceiver {
|
||||
t.canReuseTransceiver = true
|
||||
@@ -2871,7 +2909,7 @@ func (t *PCTransport) createAndSendAnswer() error {
|
||||
return t.localDescriptionSent()
|
||||
}
|
||||
|
||||
func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, offerId uint32) error {
|
||||
func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, parsed *sdp.SessionDescription, offerId uint32) error {
|
||||
t.params.Logger.Debugw("processing offer", "offerId", offerId)
|
||||
remoteOfferId := t.remoteOfferId.Load()
|
||||
if remoteOfferId != 0 && remoteOfferId != t.localAnswerId.Load() {
|
||||
@@ -2884,11 +2922,6 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, o
|
||||
}
|
||||
t.remoteOfferId.Store(offerId)
|
||||
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.lock.Lock()
|
||||
if !t.firstOfferReceived {
|
||||
t.firstOfferReceived = true
|
||||
@@ -2915,6 +2948,7 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, o
|
||||
if offerRestartICE && t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {
|
||||
t.params.Logger.Debugw("remote offer restart ice while ice gathering")
|
||||
t.pendingRestartIceOffer = sd
|
||||
t.pendingRestartIceOfferParsed = parsed
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2928,7 +2962,7 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, o
|
||||
|
||||
isStartOfConnectionSequence := t.pc.RemoteDescription() == nil
|
||||
|
||||
if err := t.setRemoteDescription(*sd); err != nil {
|
||||
if err := t.setRemoteDescription(*sd, parsed); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2954,7 +2988,7 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription, o
|
||||
return t.createAndSendAnswer()
|
||||
}
|
||||
|
||||
func (t *PCTransport) handleRemoteAnswerReceived(sd *webrtc.SessionDescription, answerId uint32) error {
|
||||
func (t *PCTransport) handleRemoteAnswerReceived(sd *webrtc.SessionDescription, parsed *sdp.SessionDescription, answerId uint32) error {
|
||||
t.params.Logger.Debugw("processing answer", "answerId", answerId)
|
||||
if answerId != 0 && answerId != t.localOfferId.Load() {
|
||||
t.params.Logger.Warnw(
|
||||
@@ -2967,7 +3001,7 @@ func (t *PCTransport) handleRemoteAnswerReceived(sd *webrtc.SessionDescription,
|
||||
|
||||
t.clearSignalStateCheckTimer()
|
||||
|
||||
if err := t.setRemoteDescription(*sd); err != nil {
|
||||
if err := t.setRemoteDescription(*sd, parsed); err != nil {
|
||||
// Pion will call RTPSender.Send method for each new added Downtrack, and return error if the DownTrack.Bind
|
||||
// returns error. In case of Downtrack.Bind returns ErrUnsupportedCodec, the signal state will be stable as negotiation is already completed
|
||||
// before startRTPSenders, and the peerconnection state can be recovered by next negotiation which will be triggered
|
||||
|
||||
Reference in New Issue
Block a user