| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using LiveKit.Proto; |
| | | 7 | | using LiveKit.Rtc.Internal; |
| | | 8 | | |
| | | 9 | | namespace LiveKit.Rtc |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Base class for track publications. |
| | | 13 | | /// </summary> |
| | | 14 | | public abstract class TrackPublication |
| | | 15 | | { |
| | | 16 | | internal FfiHandle Handle { get; } |
| | | 17 | | internal TrackPublicationInfo Info { get; private set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Track publication SID. |
| | | 21 | | /// </summary> |
| | 1990 | 22 | | public string Sid => Info.Sid; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Track name. |
| | | 26 | | /// </summary> |
| | 0 | 27 | | public string Name => Info.Name; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Track kind. |
| | | 31 | | /// </summary> |
| | 10 | 32 | | public Proto.TrackKind Kind => Info.Kind; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Track source. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public Proto.TrackSource Source => Info.Source; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Whether the track is simulcasted. |
| | | 41 | | /// </summary> |
| | 5 | 42 | | public bool Simulcasted => Info.Simulcasted; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Video width (if applicable). |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public uint Width => Info.Width; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Video height (if applicable). |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public uint Height => Info.Height; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// MIME type. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public string MimeType => Info.MimeType; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Whether the track is muted. |
| | | 61 | | /// </summary> |
| | 4 | 62 | | public bool IsMuted => Info.Muted; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Encryption type for this track. |
| | | 66 | | /// </summary> |
| | 0 | 67 | | public Proto.EncryptionType EncryptionType => Info.EncryptionType; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// The associated track (may be null for remote publications until subscribed). |
| | | 71 | | /// </summary> |
| | | 72 | | public Track? Track { get; protected set; } |
| | | 73 | | |
| | 386 | 74 | | internal TrackPublication(FfiHandle handle, TrackPublicationInfo info) |
| | 386 | 75 | | { |
| | | 76 | | Handle = handle; |
| | 386 | 77 | | Info = info; |
| | 386 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal void UpdateInfo(TrackPublicationInfo info) |
| | 40 | 81 | | { |
| | 40 | 82 | | Info = info; |
| | 40 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Local track publication. |
| | | 88 | | /// </summary> |
| | | 89 | | public class LocalTrackPublication : TrackPublication |
| | | 90 | | { |
| | | 91 | | // RunContinuationsAsynchronously: ResolveFirstSubscription runs on the room's event chain, |
| | | 92 | | // and the code awaiting WaitForSubscriptionAsync must not run inline there. |
| | | 93 | | private readonly TaskCompletionSource<bool> _firstSubscription = |
| | | 94 | | new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// The local participant that owns this publication. |
| | | 98 | | /// </summary> |
| | | 99 | | public LocalParticipant Participant { get; } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// The local track. |
| | | 103 | | /// </summary> |
| | | 104 | | public new LocalTrack? Track |
| | | 105 | | { |
| | | 106 | | get => base.Track as LocalTrack; |
| | | 107 | | internal set => base.Track = value; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | internal LocalTrackPublication( |
| | | 111 | | FfiHandle handle, |
| | | 112 | | TrackPublicationInfo info, |
| | | 113 | | LocalParticipant participant |
| | | 114 | | ) |
| | | 115 | | : base(handle, info) |
| | | 116 | | { |
| | | 117 | | Participant = participant; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Waits until at least one remote participant has subscribed to this track. |
| | | 122 | | /// This is useful when you want to ensure someone is receiving your published track |
| | | 123 | | /// before proceeding (e.g., before starting to send audio/video frames). |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="cancellationToken">Cancellation token to abort the wait.</param> |
| | | 126 | | /// <returns>A task that completes when the first subscription occurs.</returns> |
| | | 127 | | public async Task WaitForSubscriptionAsync(CancellationToken cancellationToken = default) |
| | | 128 | | { |
| | | 129 | | if (cancellationToken == default) |
| | | 130 | | { |
| | | 131 | | await _firstSubscription.Task; |
| | | 132 | | } |
| | | 133 | | else |
| | | 134 | | { |
| | | 135 | | var tcs = new TaskCompletionSource<bool>( |
| | | 136 | | TaskCreationOptions.RunContinuationsAsynchronously |
| | | 137 | | ); |
| | | 138 | | using (cancellationToken.Register(() => tcs.TrySetCanceled())) |
| | | 139 | | { |
| | | 140 | | var completedTask = await Task.WhenAny(_firstSubscription.Task, tcs.Task); |
| | | 141 | | await completedTask; // Propagate cancellation if needed |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Internal method to signal that the first subscription has occurred. |
| | | 148 | | /// </summary> |
| | | 149 | | internal void ResolveFirstSubscription() |
| | | 150 | | { |
| | | 151 | | _firstSubscription.TrySetResult(true); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Cancels a pending <see cref="WaitForSubscriptionAsync"/>: the room disconnected, so no |
| | | 156 | | /// subscription can come anymore. |
| | | 157 | | /// </summary> |
| | | 158 | | internal void CancelFirstSubscription() |
| | | 159 | | { |
| | | 160 | | _firstSubscription.TrySetCanceled(); |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Remote track publication. |
| | | 166 | | /// </summary> |
| | | 167 | | public class RemoteTrackPublication : TrackPublication |
| | | 168 | | { |
| | | 169 | | /// <summary> |
| | | 170 | | /// The remote participant that owns this publication. |
| | | 171 | | /// </summary> |
| | | 172 | | public RemoteParticipant Participant { get; } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// The remote track (null until subscribed). |
| | | 176 | | /// </summary> |
| | | 177 | | public new RemoteTrack? Track |
| | | 178 | | { |
| | | 179 | | get => base.Track as RemoteTrack; |
| | | 180 | | internal set => base.Track = value; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Whether this publication is subscribed. |
| | | 185 | | /// </summary> |
| | | 186 | | public bool IsSubscribed => Track != null; |
| | | 187 | | |
| | | 188 | | internal RemoteTrackPublication( |
| | | 189 | | FfiHandle handle, |
| | | 190 | | TrackPublicationInfo info, |
| | | 191 | | RemoteParticipant participant |
| | | 192 | | ) |
| | | 193 | | : base(handle, info) |
| | | 194 | | { |
| | | 195 | | Participant = participant; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Sets whether the track should be subscribed. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <param name="subscribed">True to subscribe, false to unsubscribe.</param> |
| | | 202 | | public void SetSubscribed(bool subscribed) |
| | | 203 | | { |
| | | 204 | | var request = new FfiRequest |
| | | 205 | | { |
| | | 206 | | SetSubscribed = new SetSubscribedRequest |
| | | 207 | | { |
| | | 208 | | Subscribe = subscribed, |
| | | 209 | | PublicationHandle = Handle.HandleId, |
| | | 210 | | }, |
| | | 211 | | }; |
| | | 212 | | |
| | | 213 | | FfiClient.Instance.SendRequest(request); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// For simulcast video tracks, requests a specific layer from the server: |
| | | 218 | | /// <c>Low</c> (q), <c>Medium</c> (h) or <c>High</c> (f). |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="quality">The layer to receive.</param> |
| | | 221 | | /// <exception cref="InvalidOperationException"> |
| | | 222 | | /// The publication is not a video track, or it is not simulcasted (selecting a layer requires |
| | | 223 | | /// multiple layers). |
| | | 224 | | /// </exception> |
| | | 225 | | public void SetVideoQuality(Proto.VideoQuality quality) |
| | | 226 | | { |
| | | 227 | | if (Kind != Proto.TrackKind.KindVideo) |
| | | 228 | | throw new InvalidOperationException( |
| | | 229 | | $"SetVideoQuality is only supported on video publications (sid={Sid})" |
| | | 230 | | ); |
| | | 231 | | if (!Simulcasted) |
| | | 232 | | throw new InvalidOperationException( |
| | | 233 | | $"SetVideoQuality requires a simulcasted publication (sid={Sid})" |
| | | 234 | | ); |
| | | 235 | | |
| | | 236 | | FfiClient.Instance.SendRequest( |
| | | 237 | | new FfiRequest |
| | | 238 | | { |
| | | 239 | | SetRemoteTrackPublicationQuality = new SetRemoteTrackPublicationQualityRequest |
| | | 240 | | { |
| | | 241 | | TrackPublicationHandle = Handle.HandleId, |
| | | 242 | | Quality = quality, |
| | | 243 | | }, |
| | | 244 | | } |
| | | 245 | | ); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Enables or disables the subscribed track without unsubscribing. While disabled the server |
| | | 250 | | /// stops sending its media, which saves bandwidth when the track is not needed for a while |
| | | 251 | | /// (for example a video that is not being rendered). |
| | | 252 | | /// </summary> |
| | | 253 | | /// <param name="enabled">False to pause receiving, true to resume.</param> |
| | | 254 | | public void SetEnabled(bool enabled) |
| | | 255 | | { |
| | | 256 | | FfiClient.Instance.SendRequest( |
| | | 257 | | new FfiRequest |
| | | 258 | | { |
| | | 259 | | EnableRemoteTrackPublication = new EnableRemoteTrackPublicationRequest |
| | | 260 | | { |
| | | 261 | | TrackPublicationHandle = Handle.HandleId, |
| | | 262 | | Enabled = enabled, |
| | | 263 | | }, |
| | | 264 | | } |
| | | 265 | | ); |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Tells the server the dimensions at which this video track is consumed, so it can pick the |
| | | 270 | | /// most appropriate simulcast layer. |
| | | 271 | | /// </summary> |
| | | 272 | | /// <param name="width">Width in pixels.</param> |
| | | 273 | | /// <param name="height">Height in pixels.</param> |
| | | 274 | | public void SetVideoDimensions(uint width, uint height) |
| | | 275 | | { |
| | | 276 | | FfiClient.Instance.SendRequest( |
| | | 277 | | new FfiRequest |
| | | 278 | | { |
| | | 279 | | UpdateRemoteTrackPublicationDimension = |
| | | 280 | | new UpdateRemoteTrackPublicationDimensionRequest |
| | | 281 | | { |
| | | 282 | | TrackPublicationHandle = Handle.HandleId, |
| | | 283 | | Width = width, |
| | | 284 | | Height = height, |
| | | 285 | | }, |
| | | 286 | | } |
| | | 287 | | ); |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | } |