Room API to support selective subscriptions

This commit is contained in:
David Zhao
2021-06-23 14:12:51 -07:00
parent 0c75924c73
commit d8b96ac2c8
6 changed files with 55 additions and 14 deletions
+1 -1
View File
@@ -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
+2
View File
@@ -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=
+1
View File
@@ -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")
)
+7 -3
View File
@@ -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
}
+12 -10
View File
@@ -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)
}
}
}
+32
View File
@@ -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)