diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 412f787b..6cb8ee12 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -830,7 +830,7 @@ func (r *Room) UpdateSubscriptionPermission(participant types.LocalParticipant, return nil } -func (r *Room) ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult { +func (r *Room) ResolveMediaTrackForSubscriber(sub types.LocalParticipant, trackID livekit.TrackID) types.MediaResolverResult { res := types.MediaResolverResult{} info := r.trackManager.GetTrackInfo(trackID) @@ -848,7 +848,7 @@ func (r *Room) ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIde pub := r.GetParticipantByID(info.PublisherID) // when publisher is not found, we will assume it doesn't have permission to access if pub != nil { - res.HasPermission = pub.HasPermission(trackID, subIdentity) + res.HasPermission = IsParticipantExemptFromTrackPermissionsRestrictions(sub) || pub.HasPermission(trackID, sub.Identity()) } return res @@ -1809,6 +1809,12 @@ func IsCloseNotifySkippable(closeReason types.ParticipantCloseReason) bool { return closeReason == types.ParticipantCloseReasonDuplicateIdentity } +func IsParticipantExemptFromTrackPermissionsRestrictions(p types.LocalParticipant) bool { + // egress/recorder participants bypass permissions as auto-egress does not + // have enough context to check permissions + return p.IsRecorder() +} + func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} { var fields []interface{} connectionType := types.ICEConnectionTypeUnknown diff --git a/pkg/rtc/subscriptionmanager.go b/pkg/rtc/subscriptionmanager.go index 927b14a1..be97b245 100644 --- a/pkg/rtc/subscriptionmanager.go +++ b/pkg/rtc/subscriptionmanager.go @@ -503,7 +503,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error { } trackID := s.trackID - res := m.params.TrackResolver(m.params.Participant.Identity(), trackID) + res := m.params.TrackResolver(m.params.Participant, trackID) s.logger.Debugw("resolved track", "result", res) if res.TrackChangedNotifier != nil && s.setChangedNotifier(res.TrackChangedNotifier) { @@ -517,7 +517,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error { if res.TrackRemovedNotifier != nil && s.setRemovedNotifier(res.TrackRemovedNotifier) { res.TrackRemovedNotifier.AddObserver(string(m.params.Participant.ID()), func() { // re-resolve the track in case the same track had been re-published - res := m.params.TrackResolver(m.params.Participant.Identity(), trackID) + res := m.params.TrackResolver(m.params.Participant, trackID) if res.Track != nil { // do not unsubscribe, track is still available return @@ -627,7 +627,7 @@ func (m *SubscriptionManager) subscribeSynchronous(trackID livekit.TrackID) erro return ErrNoSubscribePermission } - res := m.params.TrackResolver(m.params.Participant.Identity(), trackID) + res := m.params.TrackResolver(m.params.Participant, trackID) m.params.Logger.Debugw("resolved track", "trackID", trackID, " result", res) track := res.Track diff --git a/pkg/rtc/subscriptionmanager_test.go b/pkg/rtc/subscriptionmanager_test.go index f44b9915..a8dfe703 100644 --- a/pkg/rtc/subscriptionmanager_test.go +++ b/pkg/rtc/subscriptionmanager_test.go @@ -45,7 +45,7 @@ const ( func TestSubscribe(t *testing.T) { t.Run("happy path subscribe", func(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) resolver := newTestResolver(true, true, "pub", "pubID") sm.params.TrackResolver = resolver.Resolve @@ -118,7 +118,7 @@ func TestSubscribe(t *testing.T) { }) t.Run("no track permission", func(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) resolver := newTestResolver(false, true, "pub", "pubID") sm.params.TrackResolver = resolver.Resolve @@ -159,7 +159,7 @@ func TestSubscribe(t *testing.T) { }) t.Run("publisher left", func(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) resolver := newTestResolver(true, true, "pub", "pubID") sm.params.TrackResolver = resolver.Resolve @@ -188,7 +188,7 @@ func TestSubscribe(t *testing.T) { } func TestUnsubscribe(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) unsubCount := atomic.Int32{} sm.params.OnTrackUnsubscribed = func(subTrack types.SubscribedTrack) { @@ -208,7 +208,7 @@ func TestUnsubscribe(t *testing.T) { logger: logger.GetLogger(), } // a bunch of unfortunate manual wiring - res := resolver.Resolve("sub", s.trackID) + res := resolver.Resolve(nil, s.trackID) res.TrackChangedNotifier.AddObserver(string(sm.params.Participant.ID()), func() {}) s.changedNotifier = res.TrackChangedNotifier st, err := res.Track.AddSubscriber(sm.params.Participant) @@ -257,7 +257,7 @@ func TestUnsubscribe(t *testing.T) { } func TestSubscribeStatusChanged(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) resolver := newTestResolver(true, true, "pub", "pubID") sm.params.TrackResolver = resolver.Resolve @@ -320,7 +320,7 @@ func TestSubscribeStatusChanged(t *testing.T) { // clients may send update subscribed settings prior to subscription events coming through // settings should be persisted and used when the subscription does take place. func TestUpdateSettingsBeforeSubscription(t *testing.T) { - sm := newTestSubscriptionManager(t) + sm := newTestSubscriptionManager() defer sm.Close(false) resolver := newTestResolver(true, true, "pub", "pubID") sm.params.TrackResolver = resolver.Resolve @@ -351,7 +351,7 @@ func TestUpdateSettingsBeforeSubscription(t *testing.T) { } func TestSubscriptionLimits(t *testing.T) { - sm := newTestSubscriptionManagerWithParams(t, testSubscriptionParams{ + sm := newTestSubscriptionManagerWithParams(testSubscriptionParams{ SubscriptionLimitAudio: 1, SubscriptionLimitVideo: 1, }) @@ -452,11 +452,11 @@ type testSubscriptionParams struct { SubscriptionLimitVideo int32 } -func newTestSubscriptionManager(t *testing.T) *SubscriptionManager { - return newTestSubscriptionManagerWithParams(t, testSubscriptionParams{}) +func newTestSubscriptionManager() *SubscriptionManager { + return newTestSubscriptionManagerWithParams(testSubscriptionParams{}) } -func newTestSubscriptionManagerWithParams(t *testing.T, params testSubscriptionParams) *SubscriptionManager { +func newTestSubscriptionManagerWithParams(params testSubscriptionParams) *SubscriptionManager { p := &typesfakes.FakeLocalParticipant{} p.CanSubscribeReturns(true) p.IDReturns("subID") @@ -468,7 +468,7 @@ func newTestSubscriptionManagerWithParams(t *testing.T, params testSubscriptionP OnTrackSubscribed: func(subTrack types.SubscribedTrack) {}, OnTrackUnsubscribed: func(subTrack types.SubscribedTrack) {}, OnSubscriptionError: func(trackID livekit.TrackID, fatal bool, err error) {}, - TrackResolver: func(identity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult { + TrackResolver: func(sub types.LocalParticipant, trackID livekit.TrackID) types.MediaResolverResult { return types.MediaResolverResult{} }, Telemetry: &telemetryfakes.FakeTelemetryService{}, @@ -502,7 +502,7 @@ func (t *testResolver) SetPause(paused bool) { t.paused = paused } -func (t *testResolver) Resolve(identity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult { +func (t *testResolver) Resolve(_subscriber types.LocalParticipant, trackID livekit.TrackID) types.MediaResolverResult { t.lock.Lock() defer t.lock.Unlock() res := types.MediaResolverResult{ diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index d8cbeb0a..e5855698 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -476,7 +476,7 @@ type Room interface { UpdateSubscriptionPermission(participant LocalParticipant, permissions *livekit.SubscriptionPermission) error SyncState(participant LocalParticipant, state *livekit.SyncState) error SimulateScenario(participant LocalParticipant, scenario *livekit.SimulateScenario) error - ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIdentity, trackID livekit.TrackID) MediaResolverResult + ResolveMediaTrackForSubscriber(sub LocalParticipant, trackID livekit.TrackID) MediaResolverResult GetLocalParticipants() []LocalParticipant } @@ -593,7 +593,7 @@ type MediaResolverResult struct { } // MediaTrackResolver locates a specific media track for a subscriber -type MediaTrackResolver func(livekit.ParticipantIdentity, livekit.TrackID) MediaResolverResult +type MediaTrackResolver func(LocalParticipant, livekit.TrackID) MediaResolverResult // Supervisor/operation monitor related definitions type OperationMonitorEvent int diff --git a/pkg/rtc/types/typesfakes/fake_room.go b/pkg/rtc/types/typesfakes/fake_room.go index 576e8ddc..0b0a0d34 100644 --- a/pkg/rtc/types/typesfakes/fake_room.go +++ b/pkg/rtc/types/typesfakes/fake_room.go @@ -46,10 +46,10 @@ type FakeRoom struct { arg2 livekit.ParticipantID arg3 types.ParticipantCloseReason } - ResolveMediaTrackForSubscriberStub func(livekit.ParticipantIdentity, livekit.TrackID) types.MediaResolverResult + ResolveMediaTrackForSubscriberStub func(types.LocalParticipant, livekit.TrackID) types.MediaResolverResult resolveMediaTrackForSubscriberMutex sync.RWMutex resolveMediaTrackForSubscriberArgsForCall []struct { - arg1 livekit.ParticipantIdentity + arg1 types.LocalParticipant arg2 livekit.TrackID } resolveMediaTrackForSubscriberReturns struct { @@ -299,11 +299,11 @@ func (fake *FakeRoom) RemoveParticipantArgsForCall(i int) (livekit.ParticipantId return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 } -func (fake *FakeRoom) ResolveMediaTrackForSubscriber(arg1 livekit.ParticipantIdentity, arg2 livekit.TrackID) types.MediaResolverResult { +func (fake *FakeRoom) ResolveMediaTrackForSubscriber(arg1 types.LocalParticipant, arg2 livekit.TrackID) types.MediaResolverResult { fake.resolveMediaTrackForSubscriberMutex.Lock() ret, specificReturn := fake.resolveMediaTrackForSubscriberReturnsOnCall[len(fake.resolveMediaTrackForSubscriberArgsForCall)] fake.resolveMediaTrackForSubscriberArgsForCall = append(fake.resolveMediaTrackForSubscriberArgsForCall, struct { - arg1 livekit.ParticipantIdentity + arg1 types.LocalParticipant arg2 livekit.TrackID }{arg1, arg2}) stub := fake.ResolveMediaTrackForSubscriberStub @@ -325,13 +325,13 @@ func (fake *FakeRoom) ResolveMediaTrackForSubscriberCallCount() int { return len(fake.resolveMediaTrackForSubscriberArgsForCall) } -func (fake *FakeRoom) ResolveMediaTrackForSubscriberCalls(stub func(livekit.ParticipantIdentity, livekit.TrackID) types.MediaResolverResult) { +func (fake *FakeRoom) ResolveMediaTrackForSubscriberCalls(stub func(types.LocalParticipant, livekit.TrackID) types.MediaResolverResult) { fake.resolveMediaTrackForSubscriberMutex.Lock() defer fake.resolveMediaTrackForSubscriberMutex.Unlock() fake.ResolveMediaTrackForSubscriberStub = stub } -func (fake *FakeRoom) ResolveMediaTrackForSubscriberArgsForCall(i int) (livekit.ParticipantIdentity, livekit.TrackID) { +func (fake *FakeRoom) ResolveMediaTrackForSubscriberArgsForCall(i int) (types.LocalParticipant, livekit.TrackID) { fake.resolveMediaTrackForSubscriberMutex.RLock() defer fake.resolveMediaTrackForSubscriberMutex.RUnlock() argsForCall := fake.resolveMediaTrackForSubscriberArgsForCall[i]