Use answer with mid -> trackID mapping when in single peer connection (#4005)

* Use answer with mid -> trackID mapping when in single peer connection
mode.

* mage generate

* staticcheck

* handle mapped answer in tests
This commit is contained in:
Raja Subramanian
2025-10-15 19:36:22 +05:30
committed by GitHub
parent fe912acf37
commit 69ff25a055
12 changed files with 93 additions and 51 deletions
+8 -3
View File
@@ -1306,7 +1306,7 @@ func (p *ParticipantImpl) onPublisherSetRemoteDescription() {
p.updateRidsFromSDP(parsedOffer, unmatchVideos)
}
func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, answerId uint32) error {
func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error {
if p.IsClosed() || p.IsDisconnected() {
return nil
}
@@ -1318,6 +1318,11 @@ func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, an
"answer", answer,
"answerId", answerId,
)
if p.params.UseSinglePeerConnection {
return p.sendMappedSdpAnswer(answer, answerId, midToTrackID)
}
return p.sendSdpAnswer(answer, answerId)
}
@@ -1948,8 +1953,8 @@ func (h PublisherTransportHandler) OnSetRemoteDescriptionOffer() {
h.p.onPublisherSetRemoteDescription()
}
func (h PublisherTransportHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32) error {
return h.p.onPublisherAnswer(sd, answerId)
func (h PublisherTransportHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error {
return h.p.onPublisherAnswer(sd, answerId, midToTrackID)
}
func (h PublisherTransportHandler) OnTrack(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
+4
View File
@@ -299,6 +299,10 @@ func (p *ParticipantImpl) sendSdpAnswer(answer webrtc.SessionDescription, answer
return p.signaller.WriteMessage(p.signalling.SignalSdpAnswer(protosignalling.ToProtoSessionDescription(answer, answerId)))
}
func (p *ParticipantImpl) sendMappedSdpAnswer(answer webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error {
return p.signaller.WriteMessage(p.signalling.SignalMappedSdpAnswer(protosignalling.ToProtoMappedSessionDescription(answer, answerId, midToTrackID)))
}
func (p *ParticipantImpl) sendSdpOffer(offer webrtc.SessionDescription, offerId uint32) error {
return p.signaller.WriteMessage(p.signalling.SignalSdpOffer(protosignalling.ToProtoSessionDescription(offer, offerId)))
}
+1
View File
@@ -52,6 +52,7 @@ type ParticipantSignalling interface {
SignalTrackSubscribed(trackSubscribed *livekit.TrackSubscribed) proto.Message
SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Message
SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message
SignalMappedSdpAnswer(mappedAnswer *livekit.MappedSessionDescription) proto.Message
SignalSdpOffer(offer *livekit.SessionDescription) proto.Message
SignalStreamStateUpdate(streamStateUpdate *livekit.StreamStateUpdate) proto.Message
SignalSubscribedQualityUpdate(subscribedQualityUpdate *livekit.SubscribedQualityUpdate) proto.Message
+8
View File
@@ -173,6 +173,14 @@ func (s *signalling) SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Messa
}
}
func (s *signalling) SignalMappedSdpAnswer(mappedAnswer *livekit.MappedSessionDescription) proto.Message {
return &livekit.SignalResponse{
Message: &livekit.SignalResponse_MappedAnswer{
MappedAnswer: mappedAnswer,
},
}
}
func (s *signalling) SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message {
return &livekit.SignalResponse{
Message: &livekit.SignalResponse_Answer{
@@ -88,6 +88,10 @@ func (u *signallingUnimplemented) SignalSdpAnswer(answer *livekit.SessionDescrip
return nil
}
func (u *signallingUnimplemented) SignalMappedSdpAnswer(answer *livekit.MappedSessionDescription) proto.Message {
return nil
}
func (u *signallingUnimplemented) SignalSdpOffer(offer *livekit.SessionDescription) proto.Message {
return nil
}
+8 -1
View File
@@ -2640,6 +2640,13 @@ func (t *PCTransport) createAndSendAnswer() error {
return errors.Wrap(err, "setting local description failed")
}
midToTrackID := map[string]string{}
for _, tr := range t.pc.GetTransceivers() {
if tr.Sender() != nil && tr.Mid() != "" {
midToTrackID[tr.Mid()] = tr.Sender().Track().ID()
}
}
//
// Filter after setting local description as pion expects the answer
// to match between CreateAnswer and SetLocalDescription.
@@ -2661,7 +2668,7 @@ func (t *PCTransport) createAndSendAnswer() error {
}
answerId := t.remoteOfferId.Load()
if err := t.params.Handler.OnAnswer(answer, answerId); err != nil {
if err := t.params.Handler.OnAnswer(answer, answerId, midToTrackID); err != nil {
prometheus.RecordServiceOperationError("answer", "write_message")
return errors.Wrap(err, "could not send answer")
}
+2 -2
View File
@@ -44,7 +44,7 @@ type Handler interface {
OnDataSendError(err error)
OnOffer(sd webrtc.SessionDescription, offerId uint32) error
OnSetRemoteDescriptionOffer()
OnAnswer(sd webrtc.SessionDescription, answerId uint32) error
OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error
OnNegotiationStateChanged(state NegotiationState)
OnNegotiationFailed()
OnStreamStateChange(update *streamallocator.StreamStateUpdate) error
@@ -67,7 +67,7 @@ func (h UnimplementedHandler) OnOffer(sd webrtc.SessionDescription, offerId uint
return ErrNoOfferHandler
}
func (h UnimplementedHandler) OnSetRemoteDescriptionOffer() {}
func (h UnimplementedHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32) error {
func (h UnimplementedHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error {
return ErrNoAnswerHandler
}
func (h UnimplementedHandler) OnNegotiationStateChanged(state NegotiationState) {}
@@ -12,11 +12,12 @@ import (
)
type FakeHandler struct {
OnAnswerStub func(webrtc.SessionDescription, uint32) error
OnAnswerStub func(webrtc.SessionDescription, uint32, map[string]string) error
onAnswerMutex sync.RWMutex
onAnswerArgsForCall []struct {
arg1 webrtc.SessionDescription
arg2 uint32
arg3 map[string]string
}
onAnswerReturns struct {
result1 error
@@ -124,19 +125,20 @@ type FakeHandler struct {
invocationsMutex sync.RWMutex
}
func (fake *FakeHandler) OnAnswer(arg1 webrtc.SessionDescription, arg2 uint32) error {
func (fake *FakeHandler) OnAnswer(arg1 webrtc.SessionDescription, arg2 uint32, arg3 map[string]string) error {
fake.onAnswerMutex.Lock()
ret, specificReturn := fake.onAnswerReturnsOnCall[len(fake.onAnswerArgsForCall)]
fake.onAnswerArgsForCall = append(fake.onAnswerArgsForCall, struct {
arg1 webrtc.SessionDescription
arg2 uint32
}{arg1, arg2})
arg3 map[string]string
}{arg1, arg2, arg3})
stub := fake.OnAnswerStub
fakeReturns := fake.onAnswerReturns
fake.recordInvocation("OnAnswer", []interface{}{arg1, arg2})
fake.recordInvocation("OnAnswer", []interface{}{arg1, arg2, arg3})
fake.onAnswerMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
return stub(arg1, arg2, arg3)
}
if specificReturn {
return ret.result1
@@ -150,17 +152,17 @@ func (fake *FakeHandler) OnAnswerCallCount() int {
return len(fake.onAnswerArgsForCall)
}
func (fake *FakeHandler) OnAnswerCalls(stub func(webrtc.SessionDescription, uint32) error) {
func (fake *FakeHandler) OnAnswerCalls(stub func(webrtc.SessionDescription, uint32, map[string]string) error) {
fake.onAnswerMutex.Lock()
defer fake.onAnswerMutex.Unlock()
fake.OnAnswerStub = stub
}
func (fake *FakeHandler) OnAnswerArgsForCall(i int) (webrtc.SessionDescription, uint32) {
func (fake *FakeHandler) OnAnswerArgsForCall(i int) (webrtc.SessionDescription, uint32, map[string]string) {
fake.onAnswerMutex.RLock()
defer fake.onAnswerMutex.RUnlock()
argsForCall := fake.onAnswerArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
}
func (fake *FakeHandler) OnAnswerReturns(result1 error) {
+4 -4
View File
@@ -175,7 +175,7 @@ func TestNegotiationTiming(t *testing.T) {
return true
}, 10*time.Second, 10*time.Millisecond, "first offer not received yet")
handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error {
handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error {
transportA.HandleRemoteDescription(answer, answerId)
return nil
})
@@ -243,7 +243,7 @@ func TestFirstOfferMissedDuringICERestart(t *testing.T) {
// set offer/answer with restart ICE, will negotiate twice,
// first one is recover from missed offer
// second one is restartICE
handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error {
handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error {
transportA.HandleRemoteDescription(answer, answerId)
return nil
})
@@ -303,7 +303,7 @@ func TestFirstAnswerMissedDuringICERestart(t *testing.T) {
// first answer missed
var firstAnswerReceived atomic.Bool
handlerB.OnAnswerCalls(func(sd webrtc.SessionDescription, answerId uint32) error {
handlerB.OnAnswerCalls(func(sd webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error {
if firstAnswerReceived.Load() {
transportA.HandleRemoteDescription(sd, answerId)
} else {
@@ -538,7 +538,7 @@ func handleICEExchange(t *testing.T, a, b *PCTransport, ah, bh *transportfakes.F
func connectTransports(t *testing.T, offerer, answerer *PCTransport, offererHandler, answererHandler *transportfakes.FakeHandler, isICERestart bool, expectedOfferCount int32, expectedAnswerCount int32) {
var offerCount atomic.Int32
var answerCount atomic.Int32
answererHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error {
answererHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error {
answerCount.Inc()
offerer.HandleRemoteDescription(answer, answerId)
return nil