< Summary

Information
Line coverage
76%
Covered lines: 49
Uncovered lines: 15
Coverable lines: 64
Total lines: 176
Line coverage: 76.5%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%11100%
get_Width()100%11100%
get_Height()100%11100%
get_Handle()100%11100%
CaptureFrame(...)100%210%
CaptureFrame(...)100%44100%
CreateTrack(...)100%210%
Dispose()100%22100%

File(s)

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

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.ComponentModel;
 5using System.Runtime.CompilerServices;
 6using LiveKit.Proto;
 7using LiveKit.Rtc.Internal;
 8
 9namespace LiveKit.Rtc
 10{
 11    /// <summary>
 12    /// Represents a video source for publishing video frames.
 13    /// </summary>
 14    public class VideoSource : IDisposable
 15    {
 16        private readonly int _width;
 17        private readonly int _height;
 18        private readonly FfiHandle _handle;
 19        private bool _disposed;
 20
 21        /// <summary>
 22        /// Initializes a new instance of the video source. Deprecated, kept for the applications
 23        /// compiled against an earlier version: use <see cref="VideoSource(int, int, bool)"/>.
 24        /// </summary>
 25        /// <param name="width">The width of the video source in pixels.</param>
 26        /// <param name="height">The height of the video source in pixels.</param>
 27        [Obsolete("Use VideoSource(int width, int height, bool isScreencast = false) instead.")]
 28        [EditorBrowsable(EditorBrowsableState.Never)]
 29        [OverloadResolutionPriority(-1)]
 30        public VideoSource(int width, int height)
 031            : this(width, height, false) { }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the video source.
 35        /// </summary>
 36        /// <param name="width">The width of the video source in pixels.</param>
 37        /// <param name="height">The height of the video source in pixels.</param>
 38        /// <param name="isScreencast">
 39        /// Whether the frames are screen content, for which WebRTC optimizes its pipeline (sharpness
 40        /// over frame rate). Defaults to false.
 41        /// </param>
 79442        public VideoSource(int width, int height, bool isScreencast = false)
 79443        {
 79444            _width = width;
 79445            _height = height;
 46
 79447            var request = new FfiRequest
 79448            {
 79449                NewVideoSource = new NewVideoSourceRequest
 79450                {
 79451                    Type = VideoSourceType.VideoSourceNative,
 79452                    Resolution = new VideoSourceResolution
 79453                    {
 79454                        Width = (uint)width,
 79455                        Height = (uint)height,
 79456                    },
 79457                    IsScreencast = isScreencast,
 79458                },
 79459            };
 60
 79461            var response = FfiClient.Instance.SendRequest(request);
 79462            var sourceInfo = response.NewVideoSource.Source;
 79463            _handle = FfiHandle.FromId(sourceInfo.Handle.Id);
 79464        }
 65
 66        /// <summary>
 67        /// Gets the width of the video source in pixels.
 68        /// </summary>
 2169        public int Width => _width;
 70
 71        /// <summary>
 72        /// Gets the height of the video source in pixels.
 73        /// </summary>
 2174        public int Height => _height;
 75
 76        /// <summary>
 77        /// Gets the internal FFI handle.
 78        /// </summary>
 49079        internal FfiHandle Handle => _handle;
 80
 81        /// <summary>
 82        /// Captures a video frame and sends it to the video source. Deprecated, kept for the
 83        /// applications compiled against an earlier version: use
 84        /// <see cref="CaptureFrame(VideoFrame, long, Proto.VideoRotation, Proto.FrameMetadata)"/>.
 85        /// </summary>
 86        /// <param name="frame">The video frame to capture.</param>
 87        /// <param name="timestampUs">Timestamp in microseconds. If 0, current time is used.</param>
 88        /// <param name="rotation">Rotation to apply to the frame.</param>
 89        // The signature of 0.1.7 without its default values: callers compiled against it keep
 90        // binding to it, and source code binds to the overload below
 91        [Obsolete(
 92            "Use CaptureFrame(VideoFrame frame, long timestampUs = 0, VideoRotation rotation = _0, FrameMetadata? metada
 93        )]
 94        [EditorBrowsable(EditorBrowsableState.Never)]
 95        [OverloadResolutionPriority(-1)]
 96        public void CaptureFrame(
 97            VideoFrame frame,
 98            long timestampUs,
 99            Proto.VideoRotation rotation
 0100        ) => CaptureFrame(frame, timestampUs, rotation, null);
 101
 102        /// <summary>
 103        /// Captures a video frame and sends it to the video source.
 104        /// </summary>
 105        /// <param name="frame">The video frame to capture.</param>
 106        /// <param name="timestampUs">Optional timestamp in microseconds. If 0, current time is used.</param>
 107        /// <param name="rotation">Optional rotation to apply to the frame.</param>
 108        /// <param name="metadata">
 109        /// Optional metadata delivered with the frame (user timestamp, frame ID, user data), which
 110        /// subscribers read from <see cref="VideoFrameEvent.Metadata"/>. Only the features enabled in
 111        /// <see cref="TrackPublishOptions.FrameMetadataFeatures"/> when the track was published reach
 112        /// them.
 113        /// </param>
 114        public void CaptureFrame(
 115            VideoFrame frame,
 116            long timestampUs = 0,
 117            Proto.VideoRotation rotation = Proto.VideoRotation._0,
 118            Proto.FrameMetadata? metadata = null
 119        )
 2914120        {
 2914121            if (_disposed)
 1122                throw new ObjectDisposedException(nameof(VideoSource));
 123
 124            // The FFI copies the pixels into its own buffer before SendRequest returns. Keep them
 125            // pinned through request serialization and the native call.
 2913126            frame.WithPinnedBuffer(buffer =>
 2913127            {
 2913128                var request = new CaptureVideoFrameRequest
 2913129                {
 2913130                    SourceHandle = _handle.HandleId,
 2913131                    Buffer = buffer,
 2913132                    Rotation = rotation,
 2913133                    TimestampUs = timestampUs,
 2913134                };
 2913135                if (metadata != null)
 21136                    request.Metadata = metadata;
 2913137                return FfiClient.Instance.SendRequest(
 2913138                    new FfiRequest { CaptureVideoFrame = request }
 2913139                );
 5826140            });
 2913141        }
 142
 143        /// <summary>
 144        /// Creates a local video track from this video source.
 145        /// </summary>
 146        /// <param name="name">The name of the track.</param>
 147        /// <returns>A new LocalVideoTrack.</returns>
 148        public LocalVideoTrack CreateTrack(string name = "video")
 0149        {
 0150            var request = new FfiRequest
 0151            {
 0152                CreateVideoTrack = new CreateVideoTrackRequest
 0153                {
 0154                    Name = name,
 0155                    SourceHandle = _handle.HandleId,
 0156                },
 0157            };
 158
 0159            var response = FfiClient.Instance.SendRequest(request);
 0160            var trackInfo = response.CreateVideoTrack.Track;
 161
 0162            return new LocalVideoTrack(FfiHandle.FromId(trackInfo.Handle.Id), trackInfo);
 0163        }
 164
 165        /// <summary>
 166        /// Releases the native video source.
 167        /// </summary>
 168        public void Dispose()
 651169        {
 651170            if (_disposed)
 174171                return;
 477172            _disposed = true;
 477173            _handle.Dispose();
 651174        }
 175    }
 176}