From 6c94185aa65c50f0a1aabb7f6f3274d75ce8a457 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Thu, 7 Mar 2024 12:52:48 +0530 Subject: [PATCH] Bring back soft close. (#2553) Remote media track needs it. --- pkg/rtc/participant.go | 2 +- pkg/rtc/types/interfaces.go | 3 +- .../typesfakes/fake_local_media_track.go | 39 +++++++++++++++++++ .../typesfakes/fake_local_participant.go | 18 +++++---- pkg/rtc/types/typesfakes/fake_media_track.go | 39 +++++++++++++++++++ pkg/rtc/types/typesfakes/fake_participant.go | 18 +++++---- pkg/rtc/uptrackmanager.go | 8 +++- 7 files changed, 107 insertions(+), 20 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 21c4e1413..1146495de 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -1406,7 +1406,7 @@ func (p *ParticipantImpl) onSubscriberOffer(offer webrtc.SessionDescription) err } func (p *ParticipantImpl) removePublishedTrack(track types.MediaTrack) { - p.RemovePublishedTrack(track, false) + p.RemovePublishedTrack(track, false, true) if p.ProtocolVersion().SupportsUnpublish() { p.sendTrackUnpublished(track.ID()) } else { diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index b57a7e92a..b775c1c31 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -255,7 +255,7 @@ type Participant interface { IsPublisher() bool GetPublishedTrack(trackID livekit.TrackID) MediaTrack GetPublishedTracks() []MediaTrack - RemovePublishedTrack(track MediaTrack, willBeResumed bool) + RemovePublishedTrack(track MediaTrack, willBeResumed bool, shouldClose bool) GetAudioLevel() (smoothedLevel float64, active bool) @@ -485,6 +485,7 @@ type MediaTrack interface { GetTemporalLayerForSpatialFps(spatial int32, fps uint32, mime string) int32 Receivers() []sfu.TrackReceiver + ClearAllReceivers(willBeResumed bool) IsEncrypted() bool } diff --git a/pkg/rtc/types/typesfakes/fake_local_media_track.go b/pkg/rtc/types/typesfakes/fake_local_media_track.go index 0fbdcb6db..be86e74c4 100644 --- a/pkg/rtc/types/typesfakes/fake_local_media_track.go +++ b/pkg/rtc/types/typesfakes/fake_local_media_track.go @@ -28,6 +28,11 @@ type FakeLocalMediaTrack struct { result1 types.SubscribedTrack result2 error } + ClearAllReceiversStub func(bool) + clearAllReceiversMutex sync.RWMutex + clearAllReceiversArgsForCall []struct { + arg1 bool + } CloseStub func(bool) closeMutex sync.RWMutex closeArgsForCall []struct { @@ -437,6 +442,38 @@ func (fake *FakeLocalMediaTrack) AddSubscriberReturnsOnCall(i int, result1 types }{result1, result2} } +func (fake *FakeLocalMediaTrack) ClearAllReceivers(arg1 bool) { + fake.clearAllReceiversMutex.Lock() + fake.clearAllReceiversArgsForCall = append(fake.clearAllReceiversArgsForCall, struct { + arg1 bool + }{arg1}) + stub := fake.ClearAllReceiversStub + fake.recordInvocation("ClearAllReceivers", []interface{}{arg1}) + fake.clearAllReceiversMutex.Unlock() + if stub != nil { + fake.ClearAllReceiversStub(arg1) + } +} + +func (fake *FakeLocalMediaTrack) ClearAllReceiversCallCount() int { + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() + return len(fake.clearAllReceiversArgsForCall) +} + +func (fake *FakeLocalMediaTrack) ClearAllReceiversCalls(stub func(bool)) { + fake.clearAllReceiversMutex.Lock() + defer fake.clearAllReceiversMutex.Unlock() + fake.ClearAllReceiversStub = stub +} + +func (fake *FakeLocalMediaTrack) ClearAllReceiversArgsForCall(i int) bool { + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() + argsForCall := fake.clearAllReceiversArgsForCall[i] + return argsForCall.arg1 +} + func (fake *FakeLocalMediaTrack) Close(arg1 bool) { fake.closeMutex.Lock() fake.closeArgsForCall = append(fake.closeArgsForCall, struct { @@ -2116,6 +2153,8 @@ func (fake *FakeLocalMediaTrack) Invocations() map[string][][]interface{} { defer fake.addOnCloseMutex.RUnlock() fake.addSubscriberMutex.RLock() defer fake.addSubscriberMutex.RUnlock() + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() fake.closeMutex.RLock() defer fake.closeMutex.RUnlock() fake.getAllSubscribersMutex.RLock() diff --git a/pkg/rtc/types/typesfakes/fake_local_participant.go b/pkg/rtc/types/typesfakes/fake_local_participant.go index 6bbf2b5a6..6528fda61 100644 --- a/pkg/rtc/types/typesfakes/fake_local_participant.go +++ b/pkg/rtc/types/typesfakes/fake_local_participant.go @@ -631,11 +631,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 } RemoveTrackFromSubscriberStub func(*webrtc.RTPSender) error removeTrackFromSubscriberMutex sync.RWMutex @@ -4305,17 +4306,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) } } @@ -4325,17 +4327,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) RemoveTrackFromSubscriber(arg1 *webrtc.RTPSender) error { diff --git a/pkg/rtc/types/typesfakes/fake_media_track.go b/pkg/rtc/types/typesfakes/fake_media_track.go index 7f42687a2..d4bdfc17e 100644 --- a/pkg/rtc/types/typesfakes/fake_media_track.go +++ b/pkg/rtc/types/typesfakes/fake_media_track.go @@ -28,6 +28,11 @@ type FakeMediaTrack struct { result1 types.SubscribedTrack result2 error } + ClearAllReceiversStub func(bool) + clearAllReceiversMutex sync.RWMutex + clearAllReceiversArgsForCall []struct { + arg1 bool + } CloseStub func(bool) closeMutex sync.RWMutex closeArgsForCall []struct { @@ -373,6 +378,38 @@ func (fake *FakeMediaTrack) AddSubscriberReturnsOnCall(i int, result1 types.Subs }{result1, result2} } +func (fake *FakeMediaTrack) ClearAllReceivers(arg1 bool) { + fake.clearAllReceiversMutex.Lock() + fake.clearAllReceiversArgsForCall = append(fake.clearAllReceiversArgsForCall, struct { + arg1 bool + }{arg1}) + stub := fake.ClearAllReceiversStub + fake.recordInvocation("ClearAllReceivers", []interface{}{arg1}) + fake.clearAllReceiversMutex.Unlock() + if stub != nil { + fake.ClearAllReceiversStub(arg1) + } +} + +func (fake *FakeMediaTrack) ClearAllReceiversCallCount() int { + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() + return len(fake.clearAllReceiversArgsForCall) +} + +func (fake *FakeMediaTrack) ClearAllReceiversCalls(stub func(bool)) { + fake.clearAllReceiversMutex.Lock() + defer fake.clearAllReceiversMutex.Unlock() + fake.ClearAllReceiversStub = stub +} + +func (fake *FakeMediaTrack) ClearAllReceiversArgsForCall(i int) bool { + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() + argsForCall := fake.clearAllReceiversArgsForCall[i] + return argsForCall.arg1 +} + func (fake *FakeMediaTrack) Close(arg1 bool) { fake.closeMutex.Lock() fake.closeArgsForCall = append(fake.closeArgsForCall, struct { @@ -1702,6 +1739,8 @@ func (fake *FakeMediaTrack) Invocations() map[string][][]interface{} { defer fake.addOnCloseMutex.RUnlock() fake.addSubscriberMutex.RLock() defer fake.addSubscriberMutex.RUnlock() + fake.clearAllReceiversMutex.RLock() + defer fake.clearAllReceiversMutex.RUnlock() fake.closeMutex.RLock() defer fake.closeMutex.RUnlock() fake.getAllSubscribersMutex.RLock() diff --git a/pkg/rtc/types/typesfakes/fake_participant.go b/pkg/rtc/types/typesfakes/fake_participant.go index 14f5d4fcc..98fff9c80 100644 --- a/pkg/rtc/types/typesfakes/fake_participant.go +++ b/pkg/rtc/types/typesfakes/fake_participant.go @@ -158,11 +158,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 } SetMetadataStub func(string) setMetadataMutex sync.RWMutex @@ -1007,17 +1008,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) } } @@ -1027,17 +1029,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) SetMetadata(arg1 string) { diff --git a/pkg/rtc/uptrackmanager.go b/pkg/rtc/uptrackmanager.go index 9ec2f6089..dd600c6a7 100644 --- a/pkg/rtc/uptrackmanager.go +++ b/pkg/rtc/uptrackmanager.go @@ -268,8 +268,12 @@ func (u *UpTrackManager) AddPublishedTrack(track types.MediaTrack) { }) } -func (u *UpTrackManager) RemovePublishedTrack(track types.MediaTrack, willBeResumed bool) { - track.Close(willBeResumed) +func (u *UpTrackManager) RemovePublishedTrack(track types.MediaTrack, willBeResumed bool, shouldClose bool) { + if shouldClose { + track.Close(willBeResumed) + } else { + track.ClearAllReceivers(willBeResumed) + } u.lock.Lock() delete(u.publishedTracks, track.ID()) u.lock.Unlock()