| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using LiveKit.Proto; |
| | | 8 | | using LiveKit.Rtc.Internal; |
| | | 9 | | |
| | | 10 | | namespace LiveKit.Rtc |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Base class for all tracks. |
| | | 14 | | /// </summary> |
| | | 15 | | public abstract class Track : IDisposable |
| | | 16 | | { |
| | | 17 | | internal FfiHandle Handle { get; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Track session ID. |
| | | 21 | | /// </summary> |
| | | 22 | | public string Sid { get; protected set; } = string.Empty; |
| | | 23 | | |
| | | 24 | | internal void UpdateSid(string sid) |
| | | 25 | | { |
| | | 26 | | Sid = sid; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | internal void ApplyInfo(OwnedTrack? trackInfo) |
| | | 30 | | { |
| | | 31 | | if (trackInfo?.Info == null) |
| | | 32 | | return; |
| | | 33 | | Sid = trackInfo.Info.Sid ?? string.Empty; |
| | | 34 | | Name = trackInfo.Info.Name ?? string.Empty; |
| | | 35 | | IsMuted = trackInfo.Info.Muted; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Track name. |
| | | 40 | | /// </summary> |
| | | 41 | | public string Name { get; protected set; } = string.Empty; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Track kind. |
| | | 45 | | /// </summary> |
| | | 46 | | public Proto.TrackKind Kind { get; protected set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Track source. |
| | | 50 | | /// </summary> |
| | | 51 | | public Proto.TrackSource Source { get; protected set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Whether the track is muted. |
| | | 55 | | /// </summary> |
| | | 56 | | public bool IsMuted { get; protected set; } |
| | | 57 | | |
| | | 58 | | private bool _disposed; |
| | | 59 | | |
| | | 60 | | internal Track(FfiHandle handle) |
| | | 61 | | { |
| | | 62 | | Handle = handle; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the WebRTC statistics of the track (its RTP stream, codec, transport, candidate |
| | | 67 | | /// pair...), in the format of the W3C WebRTC statistics. The statistics of the whole session |
| | | 68 | | /// come from <see cref="Room.GetRtcStatsAsync"/>. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="cancellationToken">Cancels the wait for the statistics.</param> |
| | | 71 | | /// <returns>A task that completes with the statistics once the native SDK collected them.</returns> |
| | | 72 | | /// <exception cref="ObjectDisposedException">The track was disposed.</exception> |
| | | 73 | | /// <exception cref="InvalidOperationException">The native SDK could not collect them.</exception> |
| | | 74 | | public async Task<IReadOnlyList<Proto.RtcStats>> GetStatsAsync( |
| | | 75 | | CancellationToken cancellationToken = default |
| | | 76 | | ) |
| | | 77 | | { |
| | | 78 | | if (_disposed) |
| | | 79 | | throw new ObjectDisposedException(GetType().Name); |
| | | 80 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 81 | | |
| | | 82 | | var response = FfiClient.Instance.SendRequest( |
| | | 83 | | new FfiRequest { GetStats = new GetStatsRequest { TrackHandle = Handle.HandleId } } |
| | | 84 | | ); |
| | | 85 | | var asyncId = response.GetStats.AsyncId; |
| | | 86 | | var callbackEvent = await FfiClient |
| | | 87 | | .Instance.WaitForEventAsync( |
| | | 88 | | e => |
| | | 89 | | e.MessageCase == FfiEvent.MessageOneofCase.GetStats |
| | | 90 | | && e.GetStats.AsyncId == asyncId, |
| | | 91 | | TimeSpan.FromSeconds(10), |
| | | 92 | | cancellationToken |
| | | 93 | | ) |
| | | 94 | | .ConfigureAwait(false); |
| | | 95 | | |
| | | 96 | | var callback = callbackEvent.GetStats; |
| | | 97 | | if (callback.HasError && !string.IsNullOrEmpty(callback.Error)) |
| | | 98 | | throw new InvalidOperationException( |
| | | 99 | | $"Failed to get the track stats: {callback.Error}" |
| | | 100 | | ); |
| | | 101 | | return new List<Proto.RtcStats>(callback.Stats); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Disposes the track and releases resources. |
| | | 106 | | /// </summary> |
| | | 107 | | public virtual void Dispose() |
| | | 108 | | { |
| | | 109 | | if (_disposed) |
| | | 110 | | return; |
| | | 111 | | _disposed = true; |
| | | 112 | | Handle.Dispose(); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Base class for local tracks. |
| | | 118 | | /// </summary> |
| | | 119 | | public abstract class LocalTrack : Track |
| | | 120 | | { |
| | | 121 | | internal LocalTrack(FfiHandle handle) |
| | | 122 | | : base(handle) { } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Mutes the track. |
| | | 126 | | /// </summary> |
| | | 127 | | public virtual void Mute() |
| | | 128 | | { |
| | | 129 | | var request = new FfiRequest |
| | | 130 | | { |
| | | 131 | | LocalTrackMute = new LocalTrackMuteRequest |
| | | 132 | | { |
| | | 133 | | TrackHandle = Handle.HandleId, |
| | | 134 | | Mute = true, |
| | | 135 | | }, |
| | | 136 | | }; |
| | | 137 | | |
| | | 138 | | var response = FfiClient.Instance.SendRequest(request); |
| | | 139 | | |
| | | 140 | | if (response.LocalTrackMute != null) |
| | | 141 | | { |
| | | 142 | | IsMuted = response.LocalTrackMute.Muted; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Unmutes the track. |
| | | 148 | | /// </summary> |
| | | 149 | | public virtual void Unmute() |
| | | 150 | | { |
| | | 151 | | var request = new FfiRequest |
| | | 152 | | { |
| | | 153 | | LocalTrackMute = new LocalTrackMuteRequest |
| | | 154 | | { |
| | | 155 | | TrackHandle = Handle.HandleId, |
| | | 156 | | Mute = false, |
| | | 157 | | }, |
| | | 158 | | }; |
| | | 159 | | |
| | | 160 | | var response = FfiClient.Instance.SendRequest(request); |
| | | 161 | | |
| | | 162 | | if (response.LocalTrackMute != null) |
| | | 163 | | { |
| | | 164 | | IsMuted = response.LocalTrackMute.Muted; |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Base class for remote tracks. |
| | | 171 | | /// </summary> |
| | | 172 | | public abstract class RemoteTrack : Track |
| | | 173 | | { |
| | | 174 | | internal RemoteTrack(FfiHandle handle) |
| | | 175 | | : base(handle) { } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Local audio track. |
| | | 180 | | /// </summary> |
| | | 181 | | public class LocalAudioTrack : LocalTrack |
| | | 182 | | { |
| | | 183 | | internal LocalAudioTrack(FfiHandle handle) |
| | | 184 | | : base(handle) |
| | | 185 | | { |
| | | 186 | | Kind = Proto.TrackKind.KindAudio; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | internal LocalAudioTrack(FfiHandle handle, OwnedTrack trackInfo) |
| | | 190 | | : base(handle) |
| | | 191 | | { |
| | | 192 | | Kind = Proto.TrackKind.KindAudio; |
| | | 193 | | if (trackInfo?.Info != null) |
| | | 194 | | { |
| | | 195 | | Sid = trackInfo.Info.Sid ?? string.Empty; |
| | | 196 | | Name = trackInfo.Info.Name ?? string.Empty; |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Creates a new local audio track from an audio source. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="name">The track name.</param> |
| | | 204 | | /// <param name="source">The audio source to use.</param> |
| | | 205 | | /// <returns>A new local audio track.</returns> |
| | | 206 | | public static LocalAudioTrack Create(string name, AudioSource source) |
| | | 207 | | { |
| | | 208 | | var request = new FfiRequest |
| | | 209 | | { |
| | | 210 | | CreateAudioTrack = new CreateAudioTrackRequest |
| | | 211 | | { |
| | | 212 | | Name = name, |
| | | 213 | | SourceHandle = source.Handle.HandleId, |
| | | 214 | | }, |
| | | 215 | | }; |
| | | 216 | | |
| | | 217 | | var response = FfiClient.Instance.SendRequest(request); |
| | | 218 | | var trackInfo = response.CreateAudioTrack.Track; |
| | | 219 | | |
| | | 220 | | var handle = FfiHandle.FromId(trackInfo.Handle.Id); |
| | | 221 | | return new LocalAudioTrack(handle, trackInfo); |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Local video track. |
| | | 227 | | /// </summary> |
| | | 228 | | public class LocalVideoTrack : LocalTrack |
| | | 229 | | { |
| | | 230 | | internal LocalVideoTrack(FfiHandle handle) |
| | 0 | 231 | | : base(handle) |
| | 0 | 232 | | { |
| | 0 | 233 | | Kind = Proto.TrackKind.KindVideo; |
| | 0 | 234 | | } |
| | | 235 | | |
| | | 236 | | internal LocalVideoTrack(FfiHandle handle, OwnedTrack trackInfo) |
| | 490 | 237 | | : base(handle) |
| | 490 | 238 | | { |
| | 490 | 239 | | Kind = Proto.TrackKind.KindVideo; |
| | 490 | 240 | | if (trackInfo?.Info != null) |
| | 490 | 241 | | { |
| | 490 | 242 | | Sid = trackInfo.Info.Sid ?? string.Empty; |
| | 490 | 243 | | Name = trackInfo.Info.Name ?? string.Empty; |
| | 490 | 244 | | } |
| | 490 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Creates a new local video track from a video source. |
| | | 249 | | /// </summary> |
| | | 250 | | /// <param name="name">The track name.</param> |
| | | 251 | | /// <param name="source">The video source to use.</param> |
| | | 252 | | /// <returns>A new local video track.</returns> |
| | | 253 | | public static LocalVideoTrack Create(string name, VideoSource source) |
| | 490 | 254 | | { |
| | 490 | 255 | | return Create(name, source.Handle); |
| | 489 | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Creates a new local video track from a source of pre-encoded frames. Publish it with |
| | | 260 | | /// the codec of the frames, <c>VideoEncoder = EncoderBackendPreEncoded</c> and |
| | | 261 | | /// <c>Simulcast = false</c> in <see cref="TrackPublishOptions"/>. |
| | | 262 | | /// </summary> |
| | | 263 | | /// <param name="name">The track name.</param> |
| | | 264 | | /// <param name="source">The encoded video source to use.</param> |
| | | 265 | | /// <returns>A new local video track.</returns> |
| | | 266 | | public static LocalVideoTrack Create(string name, EncodedVideoSource source) |
| | 1 | 267 | | { |
| | 1 | 268 | | return Create(name, source.Handle); |
| | 1 | 269 | | } |
| | | 270 | | |
| | | 271 | | private static LocalVideoTrack Create(string name, FfiHandle sourceHandle) |
| | 491 | 272 | | { |
| | 491 | 273 | | var request = new FfiRequest |
| | 491 | 274 | | { |
| | 491 | 275 | | CreateVideoTrack = new CreateVideoTrackRequest |
| | 491 | 276 | | { |
| | 491 | 277 | | Name = name, |
| | 491 | 278 | | SourceHandle = sourceHandle.HandleId, |
| | 491 | 279 | | }, |
| | 491 | 280 | | }; |
| | | 281 | | |
| | 491 | 282 | | var response = FfiClient.Instance.SendRequest(request); |
| | 490 | 283 | | var trackInfo = response.CreateVideoTrack.Track; |
| | | 284 | | |
| | 490 | 285 | | var handle = FfiHandle.FromId(trackInfo.Handle.Id); |
| | 490 | 286 | | return new LocalVideoTrack(handle, trackInfo); |
| | 490 | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Remote audio track. |
| | | 292 | | /// </summary> |
| | | 293 | | public class RemoteAudioTrack : RemoteTrack |
| | | 294 | | { |
| | | 295 | | internal RemoteAudioTrack(FfiHandle handle) |
| | | 296 | | : base(handle) |
| | | 297 | | { |
| | | 298 | | Kind = Proto.TrackKind.KindAudio; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | internal RemoteAudioTrack(FfiHandle handle, OwnedTrack trackInfo) |
| | | 302 | | : base(handle) |
| | | 303 | | { |
| | | 304 | | Kind = Proto.TrackKind.KindAudio; |
| | | 305 | | ApplyInfo(trackInfo); |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Remote video track. |
| | | 311 | | /// </summary> |
| | | 312 | | public class RemoteVideoTrack : RemoteTrack |
| | | 313 | | { |
| | | 314 | | internal RemoteVideoTrack(FfiHandle handle) |
| | | 315 | | : base(handle) |
| | | 316 | | { |
| | | 317 | | Kind = Proto.TrackKind.KindVideo; |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | internal RemoteVideoTrack(FfiHandle handle, OwnedTrack trackInfo) |
| | | 321 | | : base(handle) |
| | | 322 | | { |
| | | 323 | | Kind = Proto.TrackKind.KindVideo; |
| | | 324 | | ApplyInfo(trackInfo); |
| | | 325 | | } |
| | | 326 | | } |
| | | 327 | | } |