| | | 1 | | // author: https://github.com/pabloFuente |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using LiveKit.Proto; |
| | | 7 | | using LiveKit.Rtc.Internal; |
| | | 8 | | |
| | | 9 | | namespace 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) |
| | 0 | 31 | | : 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> |
| | 794 | 42 | | public VideoSource(int width, int height, bool isScreencast = false) |
| | 794 | 43 | | { |
| | 794 | 44 | | _width = width; |
| | 794 | 45 | | _height = height; |
| | | 46 | | |
| | 794 | 47 | | var request = new FfiRequest |
| | 794 | 48 | | { |
| | 794 | 49 | | NewVideoSource = new NewVideoSourceRequest |
| | 794 | 50 | | { |
| | 794 | 51 | | Type = VideoSourceType.VideoSourceNative, |
| | 794 | 52 | | Resolution = new VideoSourceResolution |
| | 794 | 53 | | { |
| | 794 | 54 | | Width = (uint)width, |
| | 794 | 55 | | Height = (uint)height, |
| | 794 | 56 | | }, |
| | 794 | 57 | | IsScreencast = isScreencast, |
| | 794 | 58 | | }, |
| | 794 | 59 | | }; |
| | | 60 | | |
| | 794 | 61 | | var response = FfiClient.Instance.SendRequest(request); |
| | 794 | 62 | | var sourceInfo = response.NewVideoSource.Source; |
| | 794 | 63 | | _handle = FfiHandle.FromId(sourceInfo.Handle.Id); |
| | 794 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the width of the video source in pixels. |
| | | 68 | | /// </summary> |
| | 21 | 69 | | public int Width => _width; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the height of the video source in pixels. |
| | | 73 | | /// </summary> |
| | 21 | 74 | | public int Height => _height; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the internal FFI handle. |
| | | 78 | | /// </summary> |
| | 490 | 79 | | 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 |
| | 0 | 100 | | ) => 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 | | ) |
| | 2914 | 120 | | { |
| | 2914 | 121 | | if (_disposed) |
| | 1 | 122 | | 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. |
| | 2913 | 126 | | frame.WithPinnedBuffer(buffer => |
| | 2913 | 127 | | { |
| | 2913 | 128 | | var request = new CaptureVideoFrameRequest |
| | 2913 | 129 | | { |
| | 2913 | 130 | | SourceHandle = _handle.HandleId, |
| | 2913 | 131 | | Buffer = buffer, |
| | 2913 | 132 | | Rotation = rotation, |
| | 2913 | 133 | | TimestampUs = timestampUs, |
| | 2913 | 134 | | }; |
| | 2913 | 135 | | if (metadata != null) |
| | 21 | 136 | | request.Metadata = metadata; |
| | 2913 | 137 | | return FfiClient.Instance.SendRequest( |
| | 2913 | 138 | | new FfiRequest { CaptureVideoFrame = request } |
| | 2913 | 139 | | ); |
| | 5826 | 140 | | }); |
| | 2913 | 141 | | } |
| | | 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") |
| | 0 | 149 | | { |
| | 0 | 150 | | var request = new FfiRequest |
| | 0 | 151 | | { |
| | 0 | 152 | | CreateVideoTrack = new CreateVideoTrackRequest |
| | 0 | 153 | | { |
| | 0 | 154 | | Name = name, |
| | 0 | 155 | | SourceHandle = _handle.HandleId, |
| | 0 | 156 | | }, |
| | 0 | 157 | | }; |
| | | 158 | | |
| | 0 | 159 | | var response = FfiClient.Instance.SendRequest(request); |
| | 0 | 160 | | var trackInfo = response.CreateVideoTrack.Track; |
| | | 161 | | |
| | 0 | 162 | | return new LocalVideoTrack(FfiHandle.FromId(trackInfo.Handle.Id), trackInfo); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Releases the native video source. |
| | | 167 | | /// </summary> |
| | | 168 | | public void Dispose() |
| | 651 | 169 | | { |
| | 651 | 170 | | if (_disposed) |
| | 174 | 171 | | return; |
| | 477 | 172 | | _disposed = true; |
| | 477 | 173 | | _handle.Dispose(); |
| | 651 | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |