RoomService.updateParticipantMetadata, participant permissions
This commit is contained in:
@@ -172,22 +172,22 @@ func (r *RoomManager) CloseIdleRooms() {
|
||||
}
|
||||
|
||||
// starts WebRTC session when a new participant is connected, takes place on RTC node
|
||||
func (r *RoomManager) StartSession(roomName, identity, metadata string, reconnect bool, requestSource routing.MessageSource, responseSink routing.MessageSink) {
|
||||
func (r *RoomManager) StartSession(roomName string, pi routing.ParticipantInit, requestSource routing.MessageSource, responseSink routing.MessageSink) {
|
||||
room, err := r.getOrCreateRoom(roomName)
|
||||
if err != nil {
|
||||
logger.Errorw("could not create room", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
participant := room.GetParticipant(identity)
|
||||
participant := room.GetParticipant(pi.Identity)
|
||||
if participant != nil {
|
||||
// When reconnecting, it means WS has interrupted by underlying peer connection is still ok
|
||||
// in this mode, we'll keep the participant SID, and just swap the sink for the underlying connection
|
||||
if reconnect {
|
||||
if pi.Reconnect {
|
||||
logger.Debugw("resuming RTC session",
|
||||
"room", roomName,
|
||||
"node", r.currentNode.Id,
|
||||
"participant", identity,
|
||||
"participant", pi.Identity,
|
||||
)
|
||||
// close previous sink, and link to new one
|
||||
prevSink := participant.GetResponseSink()
|
||||
@@ -206,22 +206,26 @@ func (r *RoomManager) StartSession(roomName, identity, metadata string, reconnec
|
||||
logger.Debugw("starting RTC session",
|
||||
"room", roomName,
|
||||
"node", r.currentNode.Id,
|
||||
"participant", identity,
|
||||
"participant", pi.Identity,
|
||||
"num_participants", len(room.GetParticipants()),
|
||||
)
|
||||
|
||||
participant, err = rtc.NewParticipant(identity, r.rtcConfig, responseSink, r.config.Audio)
|
||||
participant, err = rtc.NewParticipant(pi.Identity, r.rtcConfig, responseSink, r.config.Audio)
|
||||
if err != nil {
|
||||
logger.Errorw("could not create participant", "error", err)
|
||||
return
|
||||
}
|
||||
if metadata != "" {
|
||||
if pi.Metadata != "" {
|
||||
var md map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(metadata), &md); err == nil {
|
||||
if err := json.Unmarshal([]byte(pi.Metadata), &md); err == nil {
|
||||
participant.SetMetadata(md)
|
||||
}
|
||||
}
|
||||
|
||||
if pi.Permission != nil {
|
||||
participant.SetPermission(pi.Permission)
|
||||
}
|
||||
|
||||
// join room
|
||||
if err := room.Join(participant); err != nil {
|
||||
logger.Errorw("could not join room", "error", err)
|
||||
@@ -334,7 +338,13 @@ 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:
|
||||
room.UpdateSubscriptions(participant, msg.Subscription)
|
||||
if participant.CanSubscribe() {
|
||||
room.UpdateSubscriptions(participant, msg.Subscription)
|
||||
} else {
|
||||
logger.Warnw("rejected participant subscription",
|
||||
"participant", participant.Identity(),
|
||||
"tracks", msg.Subscription.TrackSids)
|
||||
}
|
||||
case *livekit.SignalRequest_TrackSetting:
|
||||
for _, subTrack := range participant.GetSubscribedTracks() {
|
||||
for _, sid := range msg.TrackSetting.TrackSids {
|
||||
@@ -373,6 +383,16 @@ func (r *RoomManager) handleRTCMessage(roomName, identity string, msg *livekit.R
|
||||
logger.Debugw("setting track muted", "room", roomName, "participant", identity,
|
||||
"track", rm.MuteTrack.TrackSid, "muted", rm.MuteTrack.Muted)
|
||||
participant.SetTrackMuted(rm.MuteTrack.TrackSid, rm.MuteTrack.Muted)
|
||||
case *livekit.RTCNodeMessage_UpdateMetadata:
|
||||
logger.Debugw("updating metadata", "room", roomName, "participant", identity)
|
||||
var md map[string]interface{}
|
||||
if rm.UpdateMetadata.Metadata != "" {
|
||||
if err := json.Unmarshal([]byte(rm.UpdateMetadata.Metadata), &md); err != nil {
|
||||
logger.Errorw("could not update metadata", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
participant.SetMetadata(md)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+52
-26
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/thoas/go-funk"
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
)
|
||||
|
||||
@@ -97,16 +98,8 @@ 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)
|
||||
|
||||
participant, err := s.roomManager.roomStore.GetParticipant(req.Room, req.Identity)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rtcSink, err := s.roomManager.router.CreateRTCSink(req.Room, participant.Identity)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -122,30 +115,16 @@ 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)
|
||||
}
|
||||
|
||||
participant, err := s.roomManager.roomStore.GetParticipant(req.Room, req.Identity)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rtcSink, err := s.roomManager.router.CreateRTCSink(req.Room, participant.Identity)
|
||||
rtcSink, err := s.createRTCSink(ctx, req.Room, req.Identity)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rtcSink.Close()
|
||||
err = rtcSink.WriteMessage(&livekit.RTCNodeMessage{
|
||||
Message: &livekit.RTCNodeMessage_MuteTrack{
|
||||
MuteTrack: req,
|
||||
},
|
||||
})
|
||||
|
||||
participant, err := s.roomManager.roomStore.GetParticipant(req.Room, req.Identity)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// find the track
|
||||
track := funk.Find(participant.Tracks, func(t *livekit.TrackInfo) bool {
|
||||
return t.Sid == req.TrackSid
|
||||
@@ -153,8 +132,55 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR
|
||||
if track == nil {
|
||||
return nil, twirp.NotFoundError(ErrTrackNotFound.Error())
|
||||
}
|
||||
|
||||
err = rtcSink.WriteMessage(&livekit.RTCNodeMessage{
|
||||
Message: &livekit.RTCNodeMessage_MuteTrack{
|
||||
MuteTrack: req,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res = &livekit.MuteRoomTrackResponse{
|
||||
Track: track.(*livekit.TrackInfo),
|
||||
}
|
||||
// mute might not have happened, reflect desired state
|
||||
res.Track.Muted = req.Muted
|
||||
return
|
||||
}
|
||||
|
||||
func (s *RoomService) UpdateParticipantMetadata(ctx context.Context, req *livekit.UpdateParticipantMetadataRequest) (*livekit.ParticipantInfo, error) {
|
||||
rtcSink, err := s.createRTCSink(ctx, req.Target.Room, req.Target.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rtcSink.Close()
|
||||
|
||||
participant, err := s.roomManager.roomStore.GetParticipant(req.Target.Room, req.Target.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = rtcSink.WriteMessage(&livekit.RTCNodeMessage{
|
||||
Message: &livekit.RTCNodeMessage_UpdateMetadata{
|
||||
UpdateMetadata: req,
|
||||
},
|
||||
})
|
||||
|
||||
participant.Metadata = req.Metadata
|
||||
return participant, 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)
|
||||
}
|
||||
|
||||
_, err := s.roomManager.roomStore.GetParticipant(room, identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.roomManager.router.CreateRTCSink(room, identity)
|
||||
}
|
||||
|
||||
+23
-11
@@ -56,14 +56,24 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
roomName := r.FormValue("room")
|
||||
reconnectParam := r.FormValue("reconnect")
|
||||
isReconnect := reconnectParam == "1" || reconnectParam == "true"
|
||||
|
||||
claims := GetGrants(r.Context())
|
||||
// require a claim
|
||||
if claims == nil || claims.Video == nil {
|
||||
handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||
return
|
||||
}
|
||||
identity := claims.Identity
|
||||
pi := routing.ParticipantInit{
|
||||
Reconnect: reconnectParam == "1" || reconnectParam == "true",
|
||||
Identity: claims.Identity,
|
||||
}
|
||||
// only use permissions if any of them are set, default permissive
|
||||
if claims.Video.CanPublish || claims.Video.CanSubscribe {
|
||||
pi.Permission = &livekit.ParticipantPermission{
|
||||
CanSubscribe: claims.Video.CanSubscribe,
|
||||
CanPublish: claims.Video.CanPublish,
|
||||
}
|
||||
}
|
||||
|
||||
onlyName, err := EnsureJoinPermission(r.Context())
|
||||
if err != nil {
|
||||
@@ -81,17 +91,16 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var metadata string
|
||||
if claims.Metadata != nil {
|
||||
if data, err := json.Marshal(claims.Metadata); err != nil {
|
||||
logger.Warnw("unable to encode metadata", "error", err)
|
||||
} else {
|
||||
metadata = string(data)
|
||||
pi.Metadata = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
// this needs to be started first *before* using router functions on this node
|
||||
connId, reqSink, resSource, err := s.router.StartParticipantSignal(roomName, identity, metadata, isReconnect)
|
||||
connId, reqSink, resSource, err := s.router.StartParticipantSignal(roomName, pi)
|
||||
if err != nil {
|
||||
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
|
||||
return
|
||||
@@ -100,7 +109,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
done := make(chan bool, 1)
|
||||
// function exits when websocket terminates, it'll close the event reading off of response sink as well
|
||||
defer func() {
|
||||
logger.Infow("WS connection closed", "participant", identity, "connectionId", connId)
|
||||
logger.Infow("WS connection closed", "participant", pi.Identity, "connectionId", connId)
|
||||
reqSink.Close()
|
||||
close(done)
|
||||
}()
|
||||
@@ -120,7 +129,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
"connectionId", connId,
|
||||
"room", rm.Sid,
|
||||
"roomName", rm.Name,
|
||||
"name", identity,
|
||||
"name", pi.Identity,
|
||||
)
|
||||
|
||||
// handle responses
|
||||
@@ -137,7 +146,8 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
case msg := <-resSource.ReadChan():
|
||||
if msg == nil {
|
||||
logger.Infow("source closed connection", "participant", identity,
|
||||
logger.Infow("source closed connection",
|
||||
"participant", pi.Identity,
|
||||
"connectionId", connId)
|
||||
return
|
||||
}
|
||||
@@ -145,7 +155,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if !ok {
|
||||
logger.Errorw("unexpected message type",
|
||||
"type", fmt.Sprintf("%T", msg),
|
||||
"participant", identity,
|
||||
"participant", pi.Identity,
|
||||
"connectionId", connId)
|
||||
continue
|
||||
}
|
||||
@@ -172,8 +182,10 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if err := reqSink.WriteMessage(req); err != nil {
|
||||
logger.Warnw("error writing to request sink", "error", err,
|
||||
"participant", identity, "connectionId", connId)
|
||||
logger.Warnw("error writing to request sink",
|
||||
"error", err,
|
||||
"participant", pi.Identity,
|
||||
"connectionId", connId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user