From d8b96ac2c8c0c1b0d96386a43f5798be89281833 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 23 Jun 2021 14:12:51 -0700 Subject: [PATCH] Room API to support selective subscriptions --- go.mod | 2 +- go.sum | 2 ++ pkg/rtc/errors.go | 1 + pkg/rtc/room.go | 10 +++++++--- pkg/service/roommanager.go | 22 ++++++++++++---------- pkg/service/roomservice.go | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index e32aacef..f6e6c918 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/wire v0.5.0 github.com/gorilla/websocket v1.4.2 github.com/jxskiss/base62 v0.0.0-20191017122030-4f11678b909b - github.com/livekit/protocol v0.5.3 + github.com/livekit/protocol v0.5.4 github.com/magefile/mage v1.11.0 github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 2c9ed399..1185ce5a 100644 --- a/go.sum +++ b/go.sum @@ -236,6 +236,8 @@ github.com/livekit/pion-webrtc/v3 v3.0.30 h1:2RomVfztLegWIePXcyPFJDiF5pbT64aIX4R github.com/livekit/pion-webrtc/v3 v3.0.30/go.mod h1:XFQeLYBf++bWWA0sJqh6zF1ouWluosxwTOMOoTZGaD0= github.com/livekit/protocol v0.5.3 h1:u4J6poLLX3zRFsJVGAz5X8KFv9+6dwffksMW/4EI+14= github.com/livekit/protocol v0.5.3/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0= +github.com/livekit/protocol v0.5.4 h1:OFOOahFXGOusgd8X1Ucnl8H/sxa3DzwSrnmKNIUOu/0= +github.com/livekit/protocol v0.5.4/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0= github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ= github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= diff --git a/pkg/rtc/errors.go b/pkg/rtc/errors.go index c6092a43..e064b407 100644 --- a/pkg/rtc/errors.go +++ b/pkg/rtc/errors.go @@ -9,4 +9,5 @@ var ( ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room") ErrUnexpectedOffer = errors.New("expected answer SDP, received offer") ErrDataChannelUnavailable = errors.New("data channel is not available") + ErrCannotSubscribe = errors.New("participant does not have permission to subscribe") ) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 7f585d44..6f12bf5d 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -248,12 +248,16 @@ func (r *Room) RemoveParticipant(identity string) { } } -func (r *Room) UpdateSubscriptions(participant types.Participant, subscription *livekit.UpdateSubscription) error { +func (r *Room) UpdateSubscriptions(participant types.Participant, trackIds []string, subscribe bool) error { + if !participant.CanSubscribe() { + return ErrCannotSubscribe + } + // find all matching tracks var tracks []types.PublishedTrack participants := r.GetParticipants() for _, p := range participants { - for _, sid := range subscription.TrackSids { + for _, sid := range trackIds { for _, track := range p.GetPublishedTracks() { if sid == track.ID() { tracks = append(tracks, track) @@ -264,7 +268,7 @@ func (r *Room) UpdateSubscriptions(participant types.Participant, subscription * // handle subscription changes for _, track := range tracks { - if subscription.Subscribe { + if subscribe { if err := track.AddSubscriber(participant); err != nil { return err } diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 4b34e397..d6e62003 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -393,17 +393,11 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici case *livekit.SignalRequest_Mute: participant.SetTrackMuted(msg.Mute.Sid, msg.Mute.Muted) case *livekit.SignalRequest_Subscription: - if participant.CanSubscribe() { - if err := room.UpdateSubscriptions(participant, msg.Subscription); err != nil { - logger.Warnw("could not update subscription", err, - "participant", participant.Identity(), - "tracks", msg.Subscription.TrackSids, - "subscribe", msg.Subscription.Subscribe) - } - } else { - logger.Infow("rejected participant subscription", + if err := room.UpdateSubscriptions(participant, msg.Subscription.TrackSids, msg.Subscription.Subscribe); err != nil { + logger.Warnw("could not update subscription", err, "participant", participant.Identity(), - "tracks", msg.Subscription.TrackSids) + "tracks", msg.Subscription.TrackSids, + "subscribe", msg.Subscription.Subscribe) } case *livekit.SignalRequest_TrackSetting: for _, subTrack := range participant.GetSubscribedTracks() { @@ -460,6 +454,14 @@ func (r *RoomManager) handleRTCMessage(roomName, identity string, msg *livekit.R _ = p.Close() } room.Close() + case *livekit.RTCNodeMessage_UpdateSubscriptions: + logger.Debugw("updating participant subscriptions", "room", roomName, "participant", identity) + if err := room.UpdateSubscriptions(participant, rm.UpdateSubscriptions.TrackSids, rm.UpdateSubscriptions.Subscribe); err != nil { + logger.Warnw("could not update subscription", err, + "participant", participant.Identity(), + "tracks", rm.UpdateSubscriptions.TrackSids, + "subscribe", rm.UpdateSubscriptions.Subscribe) + } } } diff --git a/pkg/service/roomservice.go b/pkg/service/roomservice.go index 9ee5019e..dd9aa87d 100644 --- a/pkg/service/roomservice.go +++ b/pkg/service/roomservice.go @@ -119,6 +119,10 @@ func (s *RoomService) GetParticipant(ctx context.Context, req *livekit.RoomParti } func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomParticipantIdentity) (res *livekit.RemoveParticipantResponse, err error) { + if err = EnsureAdminPermission(ctx, req.Room); err != nil { + return nil, twirpAuthError(err) + } + rtcSink, err := s.createRTCSink(ctx, req.Room, req.Identity) if err != nil { @@ -136,6 +140,10 @@ func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomPa } func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteRoomTrackRequest) (res *livekit.MuteRoomTrackResponse, err error) { + if err = EnsureAdminPermission(ctx, req.Room); err != nil { + return nil, twirpAuthError(err) + } + rtcSink, err := s.createRTCSink(ctx, req.Room, req.Identity) if err != nil { return @@ -172,6 +180,10 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR } func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.UpdateParticipantRequest) (*livekit.ParticipantInfo, error) { + if err := EnsureAdminPermission(ctx, req.Room); err != nil { + return nil, twirpAuthError(err) + } + rtcSink, err := s.createRTCSink(ctx, req.Room, req.Identity) if err != nil { return nil, err @@ -193,6 +205,26 @@ func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.Update return participant, nil } +func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.UpdateSubscriptionsRequest) (*livekit.UpdateSubscriptionsResponse, error) { + if err := EnsureAdminPermission(ctx, req.Room); err != nil { + return nil, twirpAuthError(err) + } + + rtcSink, err := s.createRTCSink(ctx, req.Room, req.Identity) + if err != nil { + return nil, err + } + defer rtcSink.Close() + + err = rtcSink.WriteMessage(&livekit.RTCNodeMessage{ + Message: &livekit.RTCNodeMessage_UpdateSubscriptions{ + UpdateSubscriptions: req, + }, + }) + + return &livekit.UpdateSubscriptionsResponse{}, nil +} + func (s *RoomService) createRTCSink(ctx context.Context, room, identity string) (routing.MessageSink, error) { if err := EnsureAdminPermission(ctx, room); err != nil { return nil, twirpAuthError(err)