Ensure subscribe data track handles are unique (#4162)
This commit is contained in:
@@ -16,7 +16,6 @@ package rtc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/rtc/datatrack"
|
"github.com/livekit/livekit-server/pkg/rtc/datatrack"
|
||||||
@@ -29,13 +28,13 @@ type DataDownTrackParams struct {
|
|||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
SubscriberID livekit.ParticipantID
|
SubscriberID livekit.ParticipantID
|
||||||
PublishDataTrack types.DataTrack
|
PublishDataTrack types.DataTrack
|
||||||
|
Handle uint16
|
||||||
Transport types.DataTrackTransport
|
Transport types.DataTrackTransport
|
||||||
}
|
}
|
||||||
|
|
||||||
type DataDownTrack struct {
|
type DataDownTrack struct {
|
||||||
params DataDownTrackParams
|
params DataDownTrackParams
|
||||||
dti *livekit.DataTrackInfo
|
dti *livekit.DataTrackInfo
|
||||||
handle uint16
|
|
||||||
createdAt int64
|
createdAt int64
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +42,6 @@ func NewDataDownTrack(params DataDownTrackParams, dti *livekit.DataTrackInfo) (*
|
|||||||
d := &DataDownTrack{
|
d := &DataDownTrack{
|
||||||
params: params,
|
params: params,
|
||||||
dti: dti,
|
dti: dti,
|
||||||
handle: uint16(rand.Intn(256)),
|
|
||||||
createdAt: time.Now().UnixNano(),
|
createdAt: time.Now().UnixNano(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +60,7 @@ func (d *DataDownTrack) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DataDownTrack) Handle() uint16 {
|
func (d *DataDownTrack) Handle() uint16 {
|
||||||
return d.handle
|
return d.params.Handle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DataDownTrack) PublishDataTrack() types.DataTrack {
|
func (d *DataDownTrack) PublishDataTrack() types.DataTrack {
|
||||||
@@ -88,14 +86,14 @@ func (d *DataDownTrack) SubscriberID() livekit.ParticipantID {
|
|||||||
|
|
||||||
func (d *DataDownTrack) WritePacket(data []byte, packet *datatrack.Packet) {
|
func (d *DataDownTrack) WritePacket(data []byte, packet *datatrack.Packet) {
|
||||||
forwardedPacket := *packet
|
forwardedPacket := *packet
|
||||||
forwardedPacket.Handle = d.handle
|
forwardedPacket.Handle = d.params.Handle
|
||||||
buf, err := forwardedPacket.Marshal()
|
buf, err := forwardedPacket.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.params.Logger.Warnw("could not marshal data track message", err)
|
d.params.Logger.Warnw("could not marshal data track message", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := d.params.Transport.SendDataTrackMessage(buf); err != nil {
|
if err := d.params.Transport.SendDataTrackMessage(buf); err != nil {
|
||||||
d.params.Logger.Warnw("could not send data track message", err, "handle", d.handle)
|
d.params.Logger.Warnw("could not send data track message", err, "handle", d.params.Handle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ func (d *DataTrack) AddSubscriber(sub types.LocalParticipant) (types.DataDownTra
|
|||||||
Logger: sub.GetLogger().WithValues("trackID", d.ID()),
|
Logger: sub.GetLogger().WithValues("trackID", d.ID()),
|
||||||
SubscriberID: sub.ID(),
|
SubscriberID: sub.ID(),
|
||||||
PublishDataTrack: d,
|
PublishDataTrack: d,
|
||||||
|
Handle: sub.GetNextSubscribedDataTrackHandle(),
|
||||||
Transport: sub.GetDataTrackTransport(),
|
Transport: sub.GetDataTrackTransport(),
|
||||||
},
|
},
|
||||||
d.dti,
|
d.dti,
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -271,6 +272,8 @@ type ParticipantImpl struct {
|
|||||||
*UpDataTrackManager
|
*UpDataTrackManager
|
||||||
*SubscriptionManager
|
*SubscriptionManager
|
||||||
|
|
||||||
|
nextSubscribedDataTrackHandle uint16
|
||||||
|
|
||||||
icQueue [2]atomic.Pointer[webrtc.ICECandidate]
|
icQueue [2]atomic.Pointer[webrtc.ICECandidate]
|
||||||
|
|
||||||
requireBroadcast bool
|
requireBroadcast bool
|
||||||
@@ -362,6 +365,7 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
|
|||||||
rpcPendingResponses: make(map[string]*utils.DataChannelRpcPendingResponseHandler),
|
rpcPendingResponses: make(map[string]*utils.DataChannelRpcPendingResponseHandler),
|
||||||
onClose: make(map[string]func(types.LocalParticipant)),
|
onClose: make(map[string]func(types.LocalParticipant)),
|
||||||
telemetryGuard: &telemetry.ReferenceGuard{},
|
telemetryGuard: &telemetry.ReferenceGuard{},
|
||||||
|
nextSubscribedDataTrackHandle: uint16(rand.Intn(256)),
|
||||||
}
|
}
|
||||||
p.setupSignalling()
|
p.setupSignalling()
|
||||||
|
|
||||||
|
|||||||
@@ -139,3 +139,15 @@ func (p *ParticipantImpl) onReceivedDataTrackMessage(data []byte) {
|
|||||||
|
|
||||||
p.listener().OnDataTrackMessage(p, data, &packet)
|
p.listener().OnDataTrackMessage(p, data, &packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ParticipantImpl) GetNextSubscribedDataTrackHandle() uint16 {
|
||||||
|
p.lock.Lock()
|
||||||
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
|
p.nextSubscribedDataTrackHandle++
|
||||||
|
if p.nextSubscribedDataTrackHandle == 0 {
|
||||||
|
p.nextSubscribedDataTrackHandle++
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.nextSubscribedDataTrackHandle
|
||||||
|
}
|
||||||
|
|||||||
@@ -543,6 +543,8 @@ type LocalParticipant interface {
|
|||||||
GetDataTrackTransport() DataTrackTransport
|
GetDataTrackTransport() DataTrackTransport
|
||||||
|
|
||||||
ClearParticipantListener()
|
ClearParticipantListener()
|
||||||
|
|
||||||
|
GetNextSubscribedDataTrackHandle() uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------
|
// ---------------------------------------------
|
||||||
|
|||||||
@@ -376,6 +376,16 @@ type FakeLocalParticipant struct {
|
|||||||
getLoggerResolverReturnsOnCall map[int]struct {
|
getLoggerResolverReturnsOnCall map[int]struct {
|
||||||
result1 logger.DeferredFieldResolver
|
result1 logger.DeferredFieldResolver
|
||||||
}
|
}
|
||||||
|
GetNextSubscribedDataTrackHandleStub func() uint16
|
||||||
|
getNextSubscribedDataTrackHandleMutex sync.RWMutex
|
||||||
|
getNextSubscribedDataTrackHandleArgsForCall []struct {
|
||||||
|
}
|
||||||
|
getNextSubscribedDataTrackHandleReturns struct {
|
||||||
|
result1 uint16
|
||||||
|
}
|
||||||
|
getNextSubscribedDataTrackHandleReturnsOnCall map[int]struct {
|
||||||
|
result1 uint16
|
||||||
|
}
|
||||||
GetPacerStub func() pacer.Pacer
|
GetPacerStub func() pacer.Pacer
|
||||||
getPacerMutex sync.RWMutex
|
getPacerMutex sync.RWMutex
|
||||||
getPacerArgsForCall []struct {
|
getPacerArgsForCall []struct {
|
||||||
@@ -3299,6 +3309,59 @@ func (fake *FakeLocalParticipant) GetLoggerResolverReturnsOnCall(i int, result1
|
|||||||
}{result1}
|
}{result1}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fake *FakeLocalParticipant) GetNextSubscribedDataTrackHandle() uint16 {
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.Lock()
|
||||||
|
ret, specificReturn := fake.getNextSubscribedDataTrackHandleReturnsOnCall[len(fake.getNextSubscribedDataTrackHandleArgsForCall)]
|
||||||
|
fake.getNextSubscribedDataTrackHandleArgsForCall = append(fake.getNextSubscribedDataTrackHandleArgsForCall, struct {
|
||||||
|
}{})
|
||||||
|
stub := fake.GetNextSubscribedDataTrackHandleStub
|
||||||
|
fakeReturns := fake.getNextSubscribedDataTrackHandleReturns
|
||||||
|
fake.recordInvocation("GetNextSubscribedDataTrackHandle", []interface{}{})
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.Unlock()
|
||||||
|
if stub != nil {
|
||||||
|
return stub()
|
||||||
|
}
|
||||||
|
if specificReturn {
|
||||||
|
return ret.result1
|
||||||
|
}
|
||||||
|
return fakeReturns.result1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeLocalParticipant) GetNextSubscribedDataTrackHandleCallCount() int {
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.RLock()
|
||||||
|
defer fake.getNextSubscribedDataTrackHandleMutex.RUnlock()
|
||||||
|
return len(fake.getNextSubscribedDataTrackHandleArgsForCall)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeLocalParticipant) GetNextSubscribedDataTrackHandleCalls(stub func() uint16) {
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.Lock()
|
||||||
|
defer fake.getNextSubscribedDataTrackHandleMutex.Unlock()
|
||||||
|
fake.GetNextSubscribedDataTrackHandleStub = stub
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeLocalParticipant) GetNextSubscribedDataTrackHandleReturns(result1 uint16) {
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.Lock()
|
||||||
|
defer fake.getNextSubscribedDataTrackHandleMutex.Unlock()
|
||||||
|
fake.GetNextSubscribedDataTrackHandleStub = nil
|
||||||
|
fake.getNextSubscribedDataTrackHandleReturns = struct {
|
||||||
|
result1 uint16
|
||||||
|
}{result1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeLocalParticipant) GetNextSubscribedDataTrackHandleReturnsOnCall(i int, result1 uint16) {
|
||||||
|
fake.getNextSubscribedDataTrackHandleMutex.Lock()
|
||||||
|
defer fake.getNextSubscribedDataTrackHandleMutex.Unlock()
|
||||||
|
fake.GetNextSubscribedDataTrackHandleStub = nil
|
||||||
|
if fake.getNextSubscribedDataTrackHandleReturnsOnCall == nil {
|
||||||
|
fake.getNextSubscribedDataTrackHandleReturnsOnCall = make(map[int]struct {
|
||||||
|
result1 uint16
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fake.getNextSubscribedDataTrackHandleReturnsOnCall[i] = struct {
|
||||||
|
result1 uint16
|
||||||
|
}{result1}
|
||||||
|
}
|
||||||
|
|
||||||
func (fake *FakeLocalParticipant) GetPacer() pacer.Pacer {
|
func (fake *FakeLocalParticipant) GetPacer() pacer.Pacer {
|
||||||
fake.getPacerMutex.Lock()
|
fake.getPacerMutex.Lock()
|
||||||
ret, specificReturn := fake.getPacerReturnsOnCall[len(fake.getPacerArgsForCall)]
|
ret, specificReturn := fake.getPacerReturnsOnCall[len(fake.getPacerArgsForCall)]
|
||||||
|
|||||||
+2
-4
@@ -348,13 +348,11 @@ func publishDataTracksForClients(t *testing.T, clients ...*testclient.RTCClient)
|
|||||||
var writers []testclient.TrackWriter
|
var writers []testclient.TrackWriter
|
||||||
for i := range clients {
|
for i := range clients {
|
||||||
c := clients[i]
|
c := clients[i]
|
||||||
|
for range 2 {
|
||||||
dtw, err := c.PublishDataTrack()
|
dtw, err := c.PublishDataTrack()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
writers = append(writers, dtw)
|
writers = append(writers, dtw)
|
||||||
|
}
|
||||||
dtw, err = c.PublishDataTrack()
|
|
||||||
require.NoError(t, err)
|
|
||||||
writers = append(writers, dtw)
|
|
||||||
}
|
}
|
||||||
return writers
|
return writers
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user