| | | 1 | | using System.Net.Http; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using Google.Protobuf; |
| | | 4 | | |
| | | 5 | | namespace 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 | | ) |
| | 189 | 40 | | : 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) |
| | 25 | 50 | | { |
| | 25 | 51 | | return await SendAsync( |
| | 25 | 52 | | Twirp.Routes.RoomService.CreateRoom, |
| | 25 | 53 | | request, |
| | 25 | 54 | | Room.Parser, |
| | 25 | 55 | | AuthHeader(new VideoGrants { RoomCreate = true }) |
| | 25 | 56 | | ) |
| | 25 | 57 | | .ConfigureAwait(false); |
| | 25 | 58 | | } |
| | | 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) |
| | 66 | 66 | | { |
| | 66 | 67 | | return await SendAsync( |
| | 66 | 68 | | Twirp.Routes.RoomService.ListRooms, |
| | 66 | 69 | | request, |
| | 66 | 70 | | ListRoomsResponse.Parser, |
| | 66 | 71 | | AuthHeader(new VideoGrants { RoomList = true }) |
| | 66 | 72 | | ) |
| | 66 | 73 | | .ConfigureAwait(false); |
| | 61 | 74 | | } |
| | | 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) |
| | 25 | 82 | | { |
| | 25 | 83 | | return await SendAsync( |
| | 25 | 84 | | Twirp.Routes.RoomService.DeleteRoom, |
| | 25 | 85 | | request, |
| | 25 | 86 | | DeleteRoomResponse.Parser, |
| | 25 | 87 | | AuthHeader(new VideoGrants { RoomCreate = true }) |
| | 25 | 88 | | ) |
| | 25 | 89 | | .ConfigureAwait(false); |
| | 25 | 90 | | } |
| | | 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 | | ) |
| | 3 | 100 | | { |
| | 3 | 101 | | return await SendAsync( |
| | 3 | 102 | | Twirp.Routes.RoomService.ListParticipants, |
| | 3 | 103 | | request, |
| | 3 | 104 | | ListParticipantsResponse.Parser, |
| | 3 | 105 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 3 | 106 | | ) |
| | 3 | 107 | | .ConfigureAwait(false); |
| | 3 | 108 | | } |
| | | 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) |
| | 15 | 116 | | { |
| | 15 | 117 | | return await SendAsync( |
| | 15 | 118 | | Twirp.Routes.RoomService.GetParticipant, |
| | 15 | 119 | | request, |
| | 15 | 120 | | ParticipantInfo.Parser, |
| | 15 | 121 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 15 | 122 | | ) |
| | 15 | 123 | | .ConfigureAwait(false); |
| | 15 | 124 | | } |
| | | 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 | | ) |
| | 1 | 135 | | { |
| | 1 | 136 | | return await SendAsync( |
| | 1 | 137 | | Twirp.Routes.RoomService.RemoveParticipant, |
| | 1 | 138 | | request, |
| | 1 | 139 | | RemoveParticipantResponse.Parser, |
| | 1 | 140 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 1 | 141 | | ) |
| | 1 | 142 | | .ConfigureAwait(false); |
| | 1 | 143 | | } |
| | | 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) |
| | 1 | 151 | | { |
| | 1 | 152 | | return await SendAsync( |
| | 1 | 153 | | Twirp.Routes.RoomService.MutePublishedTrack, |
| | 1 | 154 | | request, |
| | 1 | 155 | | MuteRoomTrackResponse.Parser, |
| | 1 | 156 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 1 | 157 | | ) |
| | 1 | 158 | | .ConfigureAwait(false); |
| | 1 | 159 | | } |
| | | 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) |
| | 1 | 167 | | { |
| | 1 | 168 | | return await SendAsync( |
| | 1 | 169 | | Twirp.Routes.RoomService.UpdateParticipant, |
| | 1 | 170 | | request, |
| | 1 | 171 | | ParticipantInfo.Parser, |
| | 1 | 172 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 1 | 173 | | ) |
| | 1 | 174 | | .ConfigureAwait(false); |
| | 1 | 175 | | } |
| | | 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 | | ) |
| | 2 | 185 | | { |
| | 2 | 186 | | return await SendAsync( |
| | 2 | 187 | | Twirp.Routes.RoomService.UpdateSubscriptions, |
| | 2 | 188 | | request, |
| | 2 | 189 | | UpdateSubscriptionsResponse.Parser, |
| | 2 | 190 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 2 | 191 | | ) |
| | 2 | 192 | | .ConfigureAwait(false); |
| | 2 | 193 | | } |
| | | 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) |
| | 1 | 201 | | { |
| | | 202 | | // Add random nonce to request |
| | 1 | 203 | | request.Nonce = ByteString.CopyFrom(System.Guid.NewGuid().ToByteArray()); |
| | 1 | 204 | | return await SendAsync( |
| | 1 | 205 | | Twirp.Routes.RoomService.SendData, |
| | 1 | 206 | | request, |
| | 1 | 207 | | SendDataResponse.Parser, |
| | 1 | 208 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 1 | 209 | | ) |
| | 1 | 210 | | .ConfigureAwait(false); |
| | 1 | 211 | | } |
| | | 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) |
| | 1 | 219 | | { |
| | 1 | 220 | | return await SendAsync( |
| | 1 | 221 | | Twirp.Routes.RoomService.UpdateRoomMetadata, |
| | 1 | 222 | | request, |
| | 1 | 223 | | Room.Parser, |
| | 1 | 224 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 1 | 225 | | ) |
| | 1 | 226 | | .ConfigureAwait(false); |
| | 1 | 227 | | } |
| | | 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 | | ) |
| | 1 | 242 | | { |
| | 1 | 243 | | return await SendAsync( |
| | 1 | 244 | | Twirp.Routes.RoomService.ForwardParticipant, |
| | 1 | 245 | | request, |
| | 1 | 246 | | ForwardParticipantResponse.Parser, |
| | 1 | 247 | | AuthHeader( |
| | 1 | 248 | | new VideoGrants |
| | 1 | 249 | | { |
| | 1 | 250 | | RoomAdmin = true, |
| | 1 | 251 | | Room = request.Room, |
| | 1 | 252 | | DestinationRoom = request.DestinationRoom, |
| | 1 | 253 | | } |
| | 1 | 254 | | ) |
| | 1 | 255 | | ) |
| | 1 | 256 | | .ConfigureAwait(false); |
| | 1 | 257 | | } |
| | | 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) |
| | 1 | 267 | | { |
| | 1 | 268 | | return await SendAsync( |
| | 1 | 269 | | Twirp.Routes.RoomService.MoveParticipant, |
| | 1 | 270 | | request, |
| | 1 | 271 | | MoveParticipantResponse.Parser, |
| | 1 | 272 | | AuthHeader( |
| | 1 | 273 | | new VideoGrants |
| | 1 | 274 | | { |
| | 1 | 275 | | RoomAdmin = true, |
| | 1 | 276 | | Room = request.Room, |
| | 1 | 277 | | DestinationRoom = request.DestinationRoom, |
| | 1 | 278 | | } |
| | 1 | 279 | | ) |
| | 1 | 280 | | ) |
| | 1 | 281 | | .ConfigureAwait(false); |
| | 1 | 282 | | } |
| | | 283 | | } |
| | | 284 | | } |