diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index c578948bf..9042f0c99 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -308,7 +308,7 @@ func (p *ParticipantImpl) SetPermission(permission *livekit.ParticipantPermissio // publish permission has been revoked then remove all published tracks if !canPublish { for _, track := range p.GetPublishedTracks() { - p.RemovePublishedTrack(track, false) + p.RemovePublishedTrack(track, false, false) if p.ProtocolVersion().SupportsUnpublish() { p.sendTrackUnpublished(track.ID()) } else { diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index e0900ba0f..5ff2613a0 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -178,7 +178,7 @@ type Participant interface { GetPublishedTrack(sid livekit.TrackID) MediaTrack GetPublishedTracks() []MediaTrack - RemovePublishedTrack(track MediaTrack, willBeResumed bool) + RemovePublishedTrack(track MediaTrack, willBeResumed bool, shouldClose bool) AddSubscriber(op LocalParticipant, params AddSubscriberParams) (int, error) RemoveSubscriber(op LocalParticipant, trackID livekit.TrackID, resume bool) @@ -363,6 +363,8 @@ type MediaTrack interface { UpdateVideoLayers(layers []*livekit.VideoLayer) IsSimulcast() bool + Close() + // callbacks AddOnClose(func()) diff --git a/pkg/rtc/types/typesfakes/fake_local_media_track.go b/pkg/rtc/types/typesfakes/fake_local_media_track.go index a2c416dd1..57dacd1bf 100644 --- a/pkg/rtc/types/typesfakes/fake_local_media_track.go +++ b/pkg/rtc/types/typesfakes/fake_local_media_track.go @@ -26,6 +26,10 @@ type FakeLocalMediaTrack struct { addSubscriberReturnsOnCall map[int]struct { result1 error } + CloseStub func() + closeMutex sync.RWMutex + closeArgsForCall []struct { + } GetAllSubscribersStub func() []livekit.ParticipantID getAllSubscribersMutex sync.RWMutex getAllSubscribersArgsForCall []struct { @@ -372,6 +376,30 @@ func (fake *FakeLocalMediaTrack) AddSubscriberReturnsOnCall(i int, result1 error }{result1} } +func (fake *FakeLocalMediaTrack) Close() { + fake.closeMutex.Lock() + fake.closeArgsForCall = append(fake.closeArgsForCall, struct { + }{}) + stub := fake.CloseStub + fake.recordInvocation("Close", []interface{}{}) + fake.closeMutex.Unlock() + if stub != nil { + fake.CloseStub() + } +} + +func (fake *FakeLocalMediaTrack) CloseCallCount() int { + fake.closeMutex.RLock() + defer fake.closeMutex.RUnlock() + return len(fake.closeArgsForCall) +} + +func (fake *FakeLocalMediaTrack) CloseCalls(stub func()) { + fake.closeMutex.Lock() + defer fake.closeMutex.Unlock() + fake.CloseStub = stub +} + func (fake *FakeLocalMediaTrack) GetAllSubscribers() []livekit.ParticipantID { fake.getAllSubscribersMutex.Lock() ret, specificReturn := fake.getAllSubscribersReturnsOnCall[len(fake.getAllSubscribersArgsForCall)] @@ -1741,6 +1769,8 @@ func (fake *FakeLocalMediaTrack) Invocations() map[string][][]interface{} { defer fake.addOnCloseMutex.RUnlock() fake.addSubscriberMutex.RLock() defer fake.addSubscriberMutex.RUnlock() + fake.closeMutex.RLock() + defer fake.closeMutex.RUnlock() fake.getAllSubscribersMutex.RLock() defer fake.getAllSubscribersMutex.RUnlock() fake.getAudioLevelMutex.RLock() diff --git a/pkg/rtc/types/typesfakes/fake_local_participant.go b/pkg/rtc/types/typesfakes/fake_local_participant.go index 9528ec7a4..d1b1e9eba 100644 --- a/pkg/rtc/types/typesfakes/fake_local_participant.go +++ b/pkg/rtc/types/typesfakes/fake_local_participant.go @@ -465,11 +465,12 @@ type FakeLocalParticipant struct { protocolVersionReturnsOnCall map[int]struct { result1 types.ProtocolVersion } - RemovePublishedTrackStub func(types.MediaTrack, bool) + RemovePublishedTrackStub func(types.MediaTrack, bool, bool) removePublishedTrackMutex sync.RWMutex removePublishedTrackArgsForCall []struct { arg1 types.MediaTrack arg2 bool + arg3 bool } RemoveSubscribedTrackStub func(types.SubscribedTrack) removeSubscribedTrackMutex sync.RWMutex @@ -3180,17 +3181,18 @@ func (fake *FakeLocalParticipant) ProtocolVersionReturnsOnCall(i int, result1 ty }{result1} } -func (fake *FakeLocalParticipant) RemovePublishedTrack(arg1 types.MediaTrack, arg2 bool) { +func (fake *FakeLocalParticipant) RemovePublishedTrack(arg1 types.MediaTrack, arg2 bool, arg3 bool) { fake.removePublishedTrackMutex.Lock() fake.removePublishedTrackArgsForCall = append(fake.removePublishedTrackArgsForCall, struct { arg1 types.MediaTrack arg2 bool - }{arg1, arg2}) + arg3 bool + }{arg1, arg2, arg3}) stub := fake.RemovePublishedTrackStub - fake.recordInvocation("RemovePublishedTrack", []interface{}{arg1, arg2}) + fake.recordInvocation("RemovePublishedTrack", []interface{}{arg1, arg2, arg3}) fake.removePublishedTrackMutex.Unlock() if stub != nil { - fake.RemovePublishedTrackStub(arg1, arg2) + fake.RemovePublishedTrackStub(arg1, arg2, arg3) } } @@ -3200,17 +3202,17 @@ func (fake *FakeLocalParticipant) RemovePublishedTrackCallCount() int { return len(fake.removePublishedTrackArgsForCall) } -func (fake *FakeLocalParticipant) RemovePublishedTrackCalls(stub func(types.MediaTrack, bool)) { +func (fake *FakeLocalParticipant) RemovePublishedTrackCalls(stub func(types.MediaTrack, bool, bool)) { fake.removePublishedTrackMutex.Lock() defer fake.removePublishedTrackMutex.Unlock() fake.RemovePublishedTrackStub = stub } -func (fake *FakeLocalParticipant) RemovePublishedTrackArgsForCall(i int) (types.MediaTrack, bool) { +func (fake *FakeLocalParticipant) RemovePublishedTrackArgsForCall(i int) (types.MediaTrack, bool, bool) { fake.removePublishedTrackMutex.RLock() defer fake.removePublishedTrackMutex.RUnlock() argsForCall := fake.removePublishedTrackArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2 + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 } func (fake *FakeLocalParticipant) RemoveSubscribedTrack(arg1 types.SubscribedTrack) { diff --git a/pkg/rtc/types/typesfakes/fake_media_track.go b/pkg/rtc/types/typesfakes/fake_media_track.go index 9eb7b525d..01d17dd03 100644 --- a/pkg/rtc/types/typesfakes/fake_media_track.go +++ b/pkg/rtc/types/typesfakes/fake_media_track.go @@ -26,6 +26,10 @@ type FakeMediaTrack struct { addSubscriberReturnsOnCall map[int]struct { result1 error } + CloseStub func() + closeMutex sync.RWMutex + closeArgsForCall []struct { + } GetAllSubscribersStub func() []livekit.ParticipantID getAllSubscribersMutex sync.RWMutex getAllSubscribersArgsForCall []struct { @@ -308,6 +312,30 @@ func (fake *FakeMediaTrack) AddSubscriberReturnsOnCall(i int, result1 error) { }{result1} } +func (fake *FakeMediaTrack) Close() { + fake.closeMutex.Lock() + fake.closeArgsForCall = append(fake.closeArgsForCall, struct { + }{}) + stub := fake.CloseStub + fake.recordInvocation("Close", []interface{}{}) + fake.closeMutex.Unlock() + if stub != nil { + fake.CloseStub() + } +} + +func (fake *FakeMediaTrack) CloseCallCount() int { + fake.closeMutex.RLock() + defer fake.closeMutex.RUnlock() + return len(fake.closeArgsForCall) +} + +func (fake *FakeMediaTrack) CloseCalls(stub func()) { + fake.closeMutex.Lock() + defer fake.closeMutex.Unlock() + fake.CloseStub = stub +} + func (fake *FakeMediaTrack) GetAllSubscribers() []livekit.ParticipantID { fake.getAllSubscribersMutex.Lock() ret, specificReturn := fake.getAllSubscribersReturnsOnCall[len(fake.getAllSubscribersArgsForCall)] @@ -1327,6 +1355,8 @@ func (fake *FakeMediaTrack) Invocations() map[string][][]interface{} { defer fake.addOnCloseMutex.RUnlock() fake.addSubscriberMutex.RLock() defer fake.addSubscriberMutex.RUnlock() + fake.closeMutex.RLock() + defer fake.closeMutex.RUnlock() fake.getAllSubscribersMutex.RLock() defer fake.getAllSubscribersMutex.RUnlock() fake.getNumSubscribersMutex.RLock() diff --git a/pkg/rtc/types/typesfakes/fake_participant.go b/pkg/rtc/types/typesfakes/fake_participant.go index 05c3ebd47..fc700d799 100644 --- a/pkg/rtc/types/typesfakes/fake_participant.go +++ b/pkg/rtc/types/typesfakes/fake_participant.go @@ -106,11 +106,12 @@ type FakeParticipant struct { isRecorderReturnsOnCall map[int]struct { result1 bool } - RemovePublishedTrackStub func(types.MediaTrack, bool) + RemovePublishedTrackStub func(types.MediaTrack, bool, bool) removePublishedTrackMutex sync.RWMutex removePublishedTrackArgsForCall []struct { arg1 types.MediaTrack arg2 bool + arg3 bool } RemoveSubscriberStub func(types.LocalParticipant, livekit.TrackID, bool) removeSubscriberMutex sync.RWMutex @@ -685,17 +686,18 @@ func (fake *FakeParticipant) IsRecorderReturnsOnCall(i int, result1 bool) { }{result1} } -func (fake *FakeParticipant) RemovePublishedTrack(arg1 types.MediaTrack, arg2 bool) { +func (fake *FakeParticipant) RemovePublishedTrack(arg1 types.MediaTrack, arg2 bool, arg3 bool) { fake.removePublishedTrackMutex.Lock() fake.removePublishedTrackArgsForCall = append(fake.removePublishedTrackArgsForCall, struct { arg1 types.MediaTrack arg2 bool - }{arg1, arg2}) + arg3 bool + }{arg1, arg2, arg3}) stub := fake.RemovePublishedTrackStub - fake.recordInvocation("RemovePublishedTrack", []interface{}{arg1, arg2}) + fake.recordInvocation("RemovePublishedTrack", []interface{}{arg1, arg2, arg3}) fake.removePublishedTrackMutex.Unlock() if stub != nil { - fake.RemovePublishedTrackStub(arg1, arg2) + fake.RemovePublishedTrackStub(arg1, arg2, arg3) } } @@ -705,17 +707,17 @@ func (fake *FakeParticipant) RemovePublishedTrackCallCount() int { return len(fake.removePublishedTrackArgsForCall) } -func (fake *FakeParticipant) RemovePublishedTrackCalls(stub func(types.MediaTrack, bool)) { +func (fake *FakeParticipant) RemovePublishedTrackCalls(stub func(types.MediaTrack, bool, bool)) { fake.removePublishedTrackMutex.Lock() defer fake.removePublishedTrackMutex.Unlock() fake.RemovePublishedTrackStub = stub } -func (fake *FakeParticipant) RemovePublishedTrackArgsForCall(i int) (types.MediaTrack, bool) { +func (fake *FakeParticipant) RemovePublishedTrackArgsForCall(i int) (types.MediaTrack, bool, bool) { fake.removePublishedTrackMutex.RLock() defer fake.removePublishedTrackMutex.RUnlock() argsForCall := fake.removePublishedTrackArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2 + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 } func (fake *FakeParticipant) RemoveSubscriber(arg1 types.LocalParticipant, arg2 livekit.TrackID, arg3 bool) { diff --git a/pkg/rtc/uptrackmanager.go b/pkg/rtc/uptrackmanager.go index f33715956..76e148873 100644 --- a/pkg/rtc/uptrackmanager.go +++ b/pkg/rtc/uptrackmanager.go @@ -327,8 +327,11 @@ func (u *UpTrackManager) AddPublishedTrack(track types.MediaTrack) { }) } -func (u *UpTrackManager) RemovePublishedTrack(track types.MediaTrack, willBeResumed bool) { +func (u *UpTrackManager) RemovePublishedTrack(track types.MediaTrack, willBeResumed bool, shouldClose bool) { track.InitiateClose(willBeResumed) + if shouldClose { + track.Close() + } u.lock.Lock() delete(u.publishedTracks, track.ID()) u.lock.Unlock()