< Summary

Information
Class: Livekit.Server.Sdk.Dotnet.RoomServiceClient
Assembly: LivekitApi
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitApi/RoomServiceClient.cs
Line coverage
100%
Covered lines: 133
Uncovered lines: 0
Coverable lines: 133
Total lines: 284
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CreateRoom()100%11100%
ListRooms()100%11100%
DeleteRoom()100%11100%
ListParticipants()100%11100%
GetParticipant()100%11100%
RemoveParticipant()100%11100%
MutePublishedTrack()100%11100%
UpdateParticipant()100%11100%
UpdateSubscriptions()100%11100%
SendData()100%11100%
UpdateRoomMetadata()100%11100%
ForwardParticipant()100%11100%
MoveParticipant()100%11100%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitApi/RoomServiceClient.cs

#LineLine coverage
 1using System.Net.Http;
 2using System.Threading.Tasks;
 3using Google.Protobuf;
 4
 5namespace Livekit.Server.Sdk.Dotnet
 6{
 7    /// <summary>
 8    /// Client of the LiveKit Room service: creates, lists and deletes rooms and manages their participants,
 9    /// tracks and data messages.
 10    /// </summary>
 11    /// <remarks>
 12    /// See <see href="https://docs.livekit.io/reference/other/roomservice-api/">Room service API</see>.
 13    /// </remarks>
 14    /// <example>
 15    /// <code language="csharp">
 16    /// var client = new RoomServiceClient("https://my.livekit.instance", "api-key", "api-secret");
 17    ///
 18    /// await client.CreateRoom(new CreateRoomRequest { Name = "my-room", EmptyTimeout = 300 });
 19    ///
 20    /// var response = await client.ListParticipants(new ListParticipantsRequest { Room = "my-room" });
 21    /// foreach (ParticipantInfo participant in response.Participants)
 22    ///     Console.WriteLine(participant.Identity);
 23    /// </code>
 24    /// </example>
 25    public class RoomServiceClient : BaseService
 26    {
 27        /// <summary>
 28        /// Creates the client.
 29        /// </summary>
 30        /// <param name="host">URL of the LiveKit server, <c>http(s)://</c> or <c>ws(s)://</c>; a subpath such as <c>htt
 31        /// <param name="apiKey">API key; the <c>LIVEKIT_API_KEY</c> environment variable when omitted.</param>
 32        /// <param name="apiSecret">API secret; the <c>LIVEKIT_API_SECRET</c> environment variable when omitted.</param>
 33        /// <param name="client">Optional <see cref="HttpClient"/> to send the requests with. The SDK never modifies it,
 34        public RoomServiceClient(
 35            string host,
 36            string apiKey = null,
 37            string apiSecret = null,
 38            HttpClient client = null
 39        )
 18940            : base(host, apiKey, apiSecret, client) { }
 41
 42        /// <summary>
 43        /// Creates a new room. Explicit room creation is not required, since rooms will
 44        /// be automatically created when the first participant joins. This method can be
 45        /// used to customize room settings.
 46        /// </summary>
 47        /// <param name="request">Room settings: name (required), empty timeout, maximum participants, metadata, egress.
 48        /// <returns>The created room, or the existing room with that name.</returns>
 49        public async Task<Room> CreateRoom(CreateRoomRequest request)
 2550        {
 2551            return await SendAsync(
 2552                    Twirp.Routes.RoomService.CreateRoom,
 2553                    request,
 2554                    Room.Parser,
 2555                    AuthHeader(new VideoGrants { RoomCreate = true })
 2556                )
 2557                .ConfigureAwait(false);
 2558        }
 59
 60        /// <summary>
 61        /// Lists active rooms.
 62        /// </summary>
 63        /// <param name="request">Optional room names to filter by.</param>
 64        /// <returns>The active rooms.</returns>
 65        public async Task<ListRoomsResponse> ListRooms(ListRoomsRequest request)
 6666        {
 6667            return await SendAsync(
 6668                    Twirp.Routes.RoomService.ListRooms,
 6669                    request,
 6670                    ListRoomsResponse.Parser,
 6671                    AuthHeader(new VideoGrants { RoomList = true })
 6672                )
 6673                .ConfigureAwait(false);
 6174        }
 75
 76        /// <summary>
 77        /// Deletes a room.
 78        /// </summary>
 79        /// <param name="request">Room name.</param>
 80        /// <returns>Empty response.</returns>
 81        public async Task<DeleteRoomResponse> DeleteRoom(DeleteRoomRequest request)
 2582        {
 2583            return await SendAsync(
 2584                    Twirp.Routes.RoomService.DeleteRoom,
 2585                    request,
 2586                    DeleteRoomResponse.Parser,
 2587                    AuthHeader(new VideoGrants { RoomCreate = true })
 2588                )
 2589                .ConfigureAwait(false);
 2590        }
 91
 92        /// <summary>
 93        /// Lists participants in a room.
 94        /// </summary>
 95        /// <param name="request">Room name.</param>
 96        /// <returns>The participants currently in the room.</returns>
 97        public async Task<ListParticipantsResponse> ListParticipants(
 98            ListParticipantsRequest request
 99        )
 3100        {
 3101            return await SendAsync(
 3102                    Twirp.Routes.RoomService.ListParticipants,
 3103                    request,
 3104                    ListParticipantsResponse.Parser,
 3105                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 3106                )
 3107                .ConfigureAwait(false);
 3108        }
 109
 110        /// <summary>
 111        /// Gets information on a specific participant, including the tracks that the participant has published.
 112        /// </summary>
 113        /// <param name="request">Room name and participant identity.</param>
 114        /// <returns>The participant.</returns>
 115        public async Task<ParticipantInfo> GetParticipant(RoomParticipantIdentity request)
 15116        {
 15117            return await SendAsync(
 15118                    Twirp.Routes.RoomService.GetParticipant,
 15119                    request,
 15120                    ParticipantInfo.Parser,
 15121                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 15122                )
 15123                .ConfigureAwait(false);
 15124        }
 125
 126        /// <summary>
 127        /// Removes a participant from a room. This will disconnect the participant
 128        /// and emit a Disconnected event for them. They can re-join the room later.
 129        /// </summary>
 130        /// <param name="request">Room name and participant identity.</param>
 131        /// <returns>Empty response.</returns>
 132        public async Task<RemoveParticipantResponse> RemoveParticipant(
 133            RoomParticipantIdentity request
 134        )
 1135        {
 1136            return await SendAsync(
 1137                    Twirp.Routes.RoomService.RemoveParticipant,
 1138                    request,
 1139                    RemoveParticipantResponse.Parser,
 1140                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 1141                )
 1142                .ConfigureAwait(false);
 1143        }
 144
 145        /// <summary>
 146        /// Mutes a track that a participant has published.
 147        /// </summary>
 148        /// <param name="request">Room name, participant identity, track SID and whether to mute or unmute.</param>
 149        /// <returns>The updated track.</returns>
 150        public async Task<MuteRoomTrackResponse> MutePublishedTrack(MuteRoomTrackRequest request)
 1151        {
 1152            return await SendAsync(
 1153                    Twirp.Routes.RoomService.MutePublishedTrack,
 1154                    request,
 1155                    MuteRoomTrackResponse.Parser,
 1156                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 1157                )
 1158                .ConfigureAwait(false);
 1159        }
 160
 161        /// <summary>
 162        /// Updates a participant's metadata or permissions.
 163        /// </summary>
 164        /// <param name="request">Room name, participant identity and the new metadata, permissions, name or attributes.
 165        /// <returns>The updated participant.</returns>
 166        public async Task<ParticipantInfo> UpdateParticipant(UpdateParticipantRequest request)
 1167        {
 1168            return await SendAsync(
 1169                    Twirp.Routes.RoomService.UpdateParticipant,
 1170                    request,
 1171                    ParticipantInfo.Parser,
 1172                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 1173                )
 1174                .ConfigureAwait(false);
 1175        }
 176
 177        /// <summary>
 178        /// Updates a participant's subscription to tracks.
 179        /// </summary>
 180        /// <param name="request">Room name, participant identity, track SIDs and whether to subscribe or unsubscribe.</
 181        /// <returns>Empty response.</returns>
 182        public async Task<UpdateSubscriptionsResponse> UpdateSubscriptions(
 183            UpdateSubscriptionsRequest request
 184        )
 2185        {
 2186            return await SendAsync(
 2187                    Twirp.Routes.RoomService.UpdateSubscriptions,
 2188                    request,
 2189                    UpdateSubscriptionsResponse.Parser,
 2190                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 2191                )
 2192                .ConfigureAwait(false);
 2193        }
 194
 195        /// <summary>
 196        /// Sends a data message to participants in the room.
 197        /// </summary>
 198        /// <param name="request">Room name, payload, delivery kind (reliable or lossy), destination identities and topi
 199        /// <returns>Empty response.</returns>
 200        public async Task<SendDataResponse> SendData(SendDataRequest request)
 1201        {
 202            // Add random nonce to request
 1203            request.Nonce = ByteString.CopyFrom(System.Guid.NewGuid().ToByteArray());
 1204            return await SendAsync(
 1205                    Twirp.Routes.RoomService.SendData,
 1206                    request,
 1207                    SendDataResponse.Parser,
 1208                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 1209                )
 1210                .ConfigureAwait(false);
 1211        }
 212
 213        /// <summary>
 214        /// Updates metadata of a room.
 215        /// </summary>
 216        /// <param name="request">Room name and the new metadata.</param>
 217        /// <returns>The updated room.</returns>
 218        public async Task<Room> UpdateRoomMetadata(UpdateRoomMetadataRequest request)
 1219        {
 1220            return await SendAsync(
 1221                    Twirp.Routes.RoomService.UpdateRoomMetadata,
 1222                    request,
 1223                    Room.Parser,
 1224                    AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room })
 1225                )
 1226                .ConfigureAwait(false);
 1227        }
 228
 229        /// <summary>
 230        /// Forwards a participant's track(s) to another room. Requires <c>RoomAdmin</c> and <c>DestinationRoom</c>.
 231        /// This will create a participant to join the destination room that has same information with the source partic
 232        /// except the kind to be <c>Forwarded</c>. All changes to the source participant will be reflected to the forwa
 233        /// participant. When the source participant disconnects or the <c>RemoveParticipant</c> method is called in the
 234        /// destination room, the forwarding will be stopped. A participant can be forwarded to multiple rooms. The dest
 235        /// room will be created if it does not exist.
 236        /// </summary>
 237        /// <param name="request">Room name, participant identity and the destination room.</param>
 238        /// <returns>Empty response.</returns>
 239        public async Task<ForwardParticipantResponse> ForwardParticipant(
 240            ForwardParticipantRequest request
 241        )
 1242        {
 1243            return await SendAsync(
 1244                    Twirp.Routes.RoomService.ForwardParticipant,
 1245                    request,
 1246                    ForwardParticipantResponse.Parser,
 1247                    AuthHeader(
 1248                        new VideoGrants
 1249                        {
 1250                            RoomAdmin = true,
 1251                            Room = request.Room,
 1252                            DestinationRoom = request.DestinationRoom,
 1253                        }
 1254                    )
 1255                )
 1256                .ConfigureAwait(false);
 1257        }
 258
 259        /// <summary>
 260        /// Move a connected participant to a different room. Requires <c>RoomAdmin</c> and <c>DestinationRoom</c>.
 261        /// The participant will be removed from the current room and added to the destination room.
 262        /// From other observers' perspective, the participant would've disconnected from the previous room and joined t
 263        /// </summary>
 264        /// <param name="request">Room name, participant identity and the destination room.</param>
 265        /// <returns>Empty response.</returns>
 266        public async Task<MoveParticipantResponse> MoveParticipant(MoveParticipantRequest request)
 1267        {
 1268            return await SendAsync(
 1269                    Twirp.Routes.RoomService.MoveParticipant,
 1270                    request,
 1271                    MoveParticipantResponse.Parser,
 1272                    AuthHeader(
 1273                        new VideoGrants
 1274                        {
 1275                            RoomAdmin = true,
 1276                            Room = request.Room,
 1277                            DestinationRoom = request.DestinationRoom,
 1278                        }
 1279                    )
 1280                )
 1281                .ConfigureAwait(false);
 1282        }
 283    }
 284}