diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 91b2344e4..e084d0d70 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -535,7 +535,9 @@ func (p *ParticipantImpl) handleTrackPublished(track types.PublishedTrack) { p.lock.Lock() delete(p.publishedTracks, track.ID()) p.lock.Unlock() - p.onTrackUpdated(p, track) + if p.onTrackUpdated != nil { + p.onTrackUpdated(p, track) + } }) if p.onTrackPublished != nil { diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 2fdb5a60b..23d3fbd19 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -5,6 +5,8 @@ import ( "github.com/stretchr/testify/assert" + "github.com/livekit/livekit-server/pkg/rtc/types" + "github.com/livekit/livekit-server/pkg/rtc/types/typesfakes" "github.com/livekit/livekit-server/proto/livekit" ) @@ -40,3 +42,31 @@ func TestIsReady(t *testing.T) { }) } } + +func TestTrackPublishEvents(t *testing.T) { + p := newParticipantForTest("test") + track := &typesfakes.FakePublishedTrack{} + track.IDReturns("id") + published := false + updated := false + p.OnTrackUpdated(func(p types.Participant, track types.PublishedTrack) { + updated = true + }) + p.OnTrackPublished(func(p types.Participant, track types.PublishedTrack) { + published = true + }) + p.handleTrackPublished(track) + + assert.True(t, published) + assert.False(t, updated) + assert.Len(t, p.publishedTracks, 1) + + track.OnCloseArgsForCall(0)() + assert.Len(t, p.publishedTracks, 0) + assert.True(t, updated) +} + +func newParticipantForTest(name string) *ParticipantImpl { + p, _ := NewParticipant(&typesfakes.FakePeerConnection{}, &typesfakes.FakeSignalConnection{}, name, ReceiverConfig{}) + return p +}