mirror of
https://github.com/livekit/livekit.git
synced 2026-11-23 02:29:22 +00:00
* rtc: skip a closing track when a republish reuses its client id A MediaTrack whose receiver closed stays in the published list until the receiver close returns, and that close waits on the subscriber down track flush. A republish of the same client id in that window was queued behind the dying track and its media was matched to the dying track and dropped. The client id lookups now skip a track that is not open, and the track close handler re-sends TrackPublished only for a queued replacement. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * rtc: answer a queued replacement only once no open track holds its cid Two tracks can hold the same client id while the older one finishes its close. The older track's close callback answered a request queued behind the open track, and the media of that request landed on the open track. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * rtc: release a replacement queued behind any cid of the closing track AddTrack queues a request behind an open track that holds its cid by signaling or SDP cid, but the close handler released only the entry under the track's signaling cid. A request queued by SDP cid stayed queued. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
192 lines
5.5 KiB
Go
192 lines
5.5 KiB
Go
// Copyright 2026 LiveKit, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package rtc
|
|
|
|
import (
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pion/sdp/v3"
|
|
"github.com/pion/webrtc/v4"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
|
|
|
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
|
|
)
|
|
|
|
func newParticipantWithOpusOffer(t *testing.T, cid string, features ...livekit.AudioTrackFeature) *ParticipantImpl {
|
|
p := newParticipantForTest("test")
|
|
p.AddTrack(&livekit.AddTrackRequest{Cid: cid, Name: "mic", Type: livekit.TrackType_AUDIO, AudioFeatures: features})
|
|
offer := &sdp.SessionDescription{}
|
|
require.NoError(t, offer.UnmarshalString(strings.Join([]string{
|
|
"v=0",
|
|
"o=- 0 0 IN IP4 127.0.0.1",
|
|
"s=-",
|
|
"t=0 0",
|
|
"m=audio 9 UDP/TLS/RTP/SAVPF 111",
|
|
"c=IN IP4 0.0.0.0",
|
|
"a=mid:0",
|
|
"a=msid:- " + cid,
|
|
"a=sendonly",
|
|
"a=rtpmap:111 opus/48000/2",
|
|
"a=fmtp:111 minptime=10;stereo=1;useinbandfec=1",
|
|
"",
|
|
}, "\r\n")))
|
|
p.TransportManager.publisher.setRemoteOfferParsed(webrtc.SDPTypeOffer, offer)
|
|
return p
|
|
}
|
|
|
|
// the opus fmtp of the configured answer as pion parses it, payload type included, or "" when the line was dropped
|
|
func publisherAnswerFmtp(t *testing.T, p *ParticipantImpl, answerFmtp string) string {
|
|
answer := p.configurePublisherAnswer(webrtc.SessionDescription{
|
|
Type: webrtc.SDPTypeAnswer,
|
|
SDP: strings.Join([]string{
|
|
"v=0",
|
|
"o=- 0 0 IN IP4 127.0.0.1",
|
|
"s=-",
|
|
"t=0 0",
|
|
"m=audio 9 UDP/TLS/RTP/SAVPF 111",
|
|
"c=IN IP4 0.0.0.0",
|
|
"a=mid:0",
|
|
"a=recvonly",
|
|
"a=rtpmap:111 opus/48000/2",
|
|
"a=fmtp:111 " + answerFmtp,
|
|
"",
|
|
}, "\r\n"),
|
|
})
|
|
parsed, err := answer.Unmarshal()
|
|
require.NoError(t, err)
|
|
fmtp, _ := parsed.MediaDescriptions[0].Attribute("fmtp")
|
|
return fmtp
|
|
}
|
|
|
|
// opus at 11 prefixes RED's 111, and RED's fmtp must not go through the opus rewrite
|
|
func TestConfigurePublisherAnswerMatchesOpusPayloadTypeExactly(t *testing.T) {
|
|
p := newParticipantWithOpusOffer(t, "C")
|
|
answer := p.configurePublisherAnswer(webrtc.SessionDescription{
|
|
Type: webrtc.SDPTypeAnswer,
|
|
SDP: strings.Join([]string{
|
|
"v=0",
|
|
"o=- 0 0 IN IP4 127.0.0.1",
|
|
"s=-",
|
|
"t=0 0",
|
|
"m=audio 9 UDP/TLS/RTP/SAVPF 11 111",
|
|
"c=IN IP4 0.0.0.0",
|
|
"a=mid:0",
|
|
"a=recvonly",
|
|
"a=rtpmap:11 opus/48000/2",
|
|
"a=fmtp:11 useinbandfec=1",
|
|
"a=rtpmap:111 red/48000/2",
|
|
"a=fmtp:111 11/11",
|
|
"",
|
|
}, "\r\n"),
|
|
})
|
|
parsed, err := answer.Unmarshal()
|
|
require.NoError(t, err)
|
|
var attrs []string
|
|
for _, attr := range parsed.MediaDescriptions[0].Attributes {
|
|
attrs = append(attrs, attr.String())
|
|
}
|
|
require.Equal(t, []string{
|
|
"mid:0",
|
|
"recvonly",
|
|
"rtpmap:11 opus/48000/2",
|
|
"fmtp:11 useinbandfec=1;usedtx=1",
|
|
"rtpmap:111 red/48000/2",
|
|
"fmtp:111 11/11",
|
|
}, attrs)
|
|
}
|
|
|
|
func TestConfigurePublisherAnswerWhileTrackMovesToPublished(t *testing.T) {
|
|
const cid = "C"
|
|
p := newParticipantWithOpusOffer(t, cid)
|
|
|
|
done := make(chan struct{})
|
|
started := false
|
|
decoy := &typesfakes.FakeLocalMediaTrack{}
|
|
decoy.IsOpenReturns(true)
|
|
decoy.HasSdpCidCalls(func(string) bool {
|
|
if started {
|
|
return false
|
|
}
|
|
started = true
|
|
go func() {
|
|
defer close(done)
|
|
p.pendingTracksLock.RWMutex.Lock()
|
|
defer p.pendingTracksLock.RWMutex.Unlock()
|
|
signalCid, ti, _, _, _ := p.getPendingTrack(cid, livekit.TrackType_AUDIO, true)
|
|
p.addMediaTrack(signalCid, ti)
|
|
}()
|
|
// a failed TryRLock means the mover holds the lock or is queued, and a queued writer blocks later readers
|
|
for {
|
|
select {
|
|
case <-done:
|
|
return false
|
|
default:
|
|
}
|
|
if !p.pendingTracksLock.RWMutex.TryRLock() {
|
|
return false
|
|
}
|
|
p.pendingTracksLock.RWMutex.RUnlock()
|
|
runtime.Gosched()
|
|
}
|
|
})
|
|
p.UpTrackManager.AddPublishedTrack(decoy)
|
|
|
|
fmtp := publisherAnswerFmtp(t, p, "minptime=10;stereo=1;useinbandfec=1")
|
|
<-done
|
|
require.NotContains(t, fmtp, "stereo=1")
|
|
require.Contains(t, fmtp, "usedtx=1")
|
|
}
|
|
|
|
func TestConfigurePublisherAnswerRewritesOpusFmtpByKey(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
features []livekit.AudioTrackFeature
|
|
fmtp string
|
|
want string
|
|
}{
|
|
{
|
|
name: "leading stereo is dropped",
|
|
fmtp: "stereo=1;useinbandfec=1",
|
|
want: "111 useinbandfec=1;usedtx=1",
|
|
},
|
|
{
|
|
name: "params keep their order",
|
|
features: []livekit.AudioTrackFeature{livekit.AudioTrackFeature_TF_STEREO},
|
|
fmtp: "minptime=10;useinbandfec=1",
|
|
want: "111 minptime=10;useinbandfec=1;usedtx=1;stereo=1;maxaveragebitrate=510000",
|
|
},
|
|
{
|
|
name: "upper-case stereo and an existing maxaveragebitrate are replaced",
|
|
features: []livekit.AudioTrackFeature{livekit.AudioTrackFeature_TF_STEREO},
|
|
fmtp: "STEREO=1;maxaveragebitrate=64000;useinbandfec=1",
|
|
want: "111 useinbandfec=1;usedtx=1;stereo=1;maxaveragebitrate=510000",
|
|
},
|
|
{
|
|
name: "no params kept drops the fmtp line",
|
|
features: []livekit.AudioTrackFeature{livekit.AudioTrackFeature_TF_NO_DTX},
|
|
fmtp: "stereo=1",
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
p := newParticipantWithOpusOffer(t, "C", tc.features...)
|
|
require.Equal(t, tc.want, publisherAnswerFmtp(t, p, tc.fmtp))
|
|
})
|
|
}
|
|
}
|