< Summary

Information
Class: LiveKit.Rtc.LocalTrack
Assembly: LivekitRtc
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Track.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 327
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Mute()50%22100%
Unmute()50%22100%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Track.cs

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using LiveKit.Proto;
 8using LiveKit.Rtc.Internal;
 9
 10namespace 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)
 4128122            : base(handle) { }
 123
 124        /// <summary>
 125        /// Mutes the track.
 126        /// </summary>
 127        public virtual void Mute()
 172128        {
 172129            var request = new FfiRequest
 172130            {
 172131                LocalTrackMute = new LocalTrackMuteRequest
 172132                {
 172133                    TrackHandle = Handle.HandleId,
 172134                    Mute = true,
 172135                },
 172136            };
 137
 172138            var response = FfiClient.Instance.SendRequest(request);
 139
 170140            if (response.LocalTrackMute != null)
 170141            {
 170142                IsMuted = response.LocalTrackMute.Muted;
 170143            }
 170144        }
 145
 146        /// <summary>
 147        /// Unmutes the track.
 148        /// </summary>
 149        public virtual void Unmute()
 3150        {
 3151            var request = new FfiRequest
 3152            {
 3153                LocalTrackMute = new LocalTrackMuteRequest
 3154                {
 3155                    TrackHandle = Handle.HandleId,
 3156                    Mute = false,
 3157                },
 3158            };
 159
 3160            var response = FfiClient.Instance.SendRequest(request);
 161
 3162            if (response.LocalTrackMute != null)
 3163            {
 3164                IsMuted = response.LocalTrackMute.Muted;
 3165            }
 3166        }
 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)
 231            : base(handle)
 232        {
 233            Kind = Proto.TrackKind.KindVideo;
 234        }
 235
 236        internal LocalVideoTrack(FfiHandle handle, OwnedTrack trackInfo)
 237            : base(handle)
 238        {
 239            Kind = Proto.TrackKind.KindVideo;
 240            if (trackInfo?.Info != null)
 241            {
 242                Sid = trackInfo.Info.Sid ?? string.Empty;
 243                Name = trackInfo.Info.Name ?? string.Empty;
 244            }
 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)
 254        {
 255            return Create(name, source.Handle);
 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)
 267        {
 268            return Create(name, source.Handle);
 269        }
 270
 271        private static LocalVideoTrack Create(string name, FfiHandle sourceHandle)
 272        {
 273            var request = new FfiRequest
 274            {
 275                CreateVideoTrack = new CreateVideoTrackRequest
 276                {
 277                    Name = name,
 278                    SourceHandle = sourceHandle.HandleId,
 279                },
 280            };
 281
 282            var response = FfiClient.Instance.SendRequest(request);
 283            var trackInfo = response.CreateVideoTrack.Track;
 284
 285            var handle = FfiHandle.FromId(trackInfo.Handle.Id);
 286            return new LocalVideoTrack(handle, trackInfo);
 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}