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
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)