ignore duplicate tracks being published
This commit is contained in:
@@ -251,6 +251,11 @@ func (p *ParticipantImpl) AddTrack(clientId, name string, trackType livekit.Trac
|
|||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
|
// if track is already published, reject
|
||||||
|
if p.pendingTracks[clientId] != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ti := &livekit.TrackInfo{
|
ti := &livekit.TrackInfo{
|
||||||
Type: trackType,
|
Type: trackType,
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|||||||
@@ -46,27 +46,55 @@ func TestIsReady(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrackPublishEvents(t *testing.T) {
|
func TestTrackPublishing(t *testing.T) {
|
||||||
p := newParticipantForTest("test")
|
t.Run("should send the correct events", func(t *testing.T) {
|
||||||
track := &typesfakes.FakePublishedTrack{}
|
p := newParticipantForTest("test")
|
||||||
track.IDReturns("id")
|
track := &typesfakes.FakePublishedTrack{}
|
||||||
published := false
|
track.IDReturns("id")
|
||||||
updated := false
|
published := false
|
||||||
p.OnTrackUpdated(func(p types.Participant, track types.PublishedTrack) {
|
updated := false
|
||||||
updated = true
|
p.OnTrackUpdated(func(p types.Participant, track types.PublishedTrack) {
|
||||||
})
|
updated = true
|
||||||
p.OnTrackPublished(func(p types.Participant, track types.PublishedTrack) {
|
})
|
||||||
published = true
|
p.OnTrackPublished(func(p types.Participant, track types.PublishedTrack) {
|
||||||
})
|
published = true
|
||||||
p.handleTrackPublished(track)
|
})
|
||||||
|
p.handleTrackPublished(track)
|
||||||
|
|
||||||
assert.True(t, published)
|
assert.True(t, published)
|
||||||
assert.False(t, updated)
|
assert.False(t, updated)
|
||||||
assert.Len(t, p.publishedTracks, 1)
|
assert.Len(t, p.publishedTracks, 1)
|
||||||
|
|
||||||
track.OnCloseArgsForCall(0)()
|
track.OnCloseArgsForCall(0)()
|
||||||
assert.Len(t, p.publishedTracks, 0)
|
assert.Len(t, p.publishedTracks, 0)
|
||||||
assert.True(t, updated)
|
assert.True(t, updated)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("sends back trackPublished event", func(t *testing.T) {
|
||||||
|
p := newParticipantForTest("test")
|
||||||
|
//track := &typesfakes.FakePublishedTrack{}
|
||||||
|
//track.IDReturns("id")
|
||||||
|
sink := p.responseSink.(*routingfakes.FakeMessageSink)
|
||||||
|
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
|
||||||
|
assert.Equal(t, 1, sink.WriteMessageCallCount())
|
||||||
|
res := sink.WriteMessageArgsForCall(0).(*livekit.SignalResponse)
|
||||||
|
assert.IsType(t, &livekit.SignalResponse_TrackPublished{}, res.Message)
|
||||||
|
published := res.Message.(*livekit.SignalResponse_TrackPublished).TrackPublished
|
||||||
|
assert.Equal(t, "cid", published.Cid)
|
||||||
|
assert.Equal(t, "webcam", published.Track.Name)
|
||||||
|
assert.Equal(t, livekit.TrackType_VIDEO, published.Track.Type)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should not allow adding of duplicate tracks", func(t *testing.T) {
|
||||||
|
p := newParticipantForTest("test")
|
||||||
|
//track := &typesfakes.FakePublishedTrack{}
|
||||||
|
//track.IDReturns("id")
|
||||||
|
sink := p.responseSink.(*routingfakes.FakeMessageSink)
|
||||||
|
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
|
||||||
|
p.AddTrack("cid", "duplicate", livekit.TrackType_AUDIO)
|
||||||
|
|
||||||
|
assert.Equal(t, 1, sink.WriteMessageCallCount())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// after disconnection, things should continue to function and not panic
|
// after disconnection, things should continue to function and not panic
|
||||||
|
|||||||
Reference in New Issue
Block a user