|
| 1 | +//////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// FlashCap - Independent camera capture library. |
| 4 | +// Copyright (c) Yoh Deadfall (@YohDeadfall) |
| 5 | +// Copyright (c) Felipe Ferreira Quintella (@ffquintella) |
| 6 | +// |
| 7 | +// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0 |
| 8 | +// |
| 9 | +//////////////////////////////////////////////////////////////////////////// |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Diagnostics; |
| 13 | +using System.Linq; |
| 14 | +using System.Runtime.InteropServices; |
| 15 | +using System.Threading; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using FlashCap.Internal; |
| 18 | +using static FlashCap.Internal.AVFoundation.LibAVFoundation; |
| 19 | +using static FlashCap.Internal.NativeMethods; |
| 20 | +using static FlashCap.Internal.NativeMethods_AVFoundation.LibCoreFoundation; |
| 21 | +using static FlashCap.Internal.NativeMethods_AVFoundation.LibCoreMedia; |
| 22 | +using static FlashCap.Internal.NativeMethods_AVFoundation.LibCoreVideo; |
| 23 | + |
| 24 | +namespace FlashCap.Devices; |
| 25 | + |
| 26 | +public sealed class AVFoundationDevice : CaptureDevice |
| 27 | +{ |
| 28 | + private readonly string uniqueID; |
| 29 | + |
| 30 | + private DispatchQueue? queue; |
| 31 | + private AVCaptureDevice? device; |
| 32 | + private AVCaptureDeviceInput? deviceInput; |
| 33 | + private AVCaptureVideoDataOutput? deviceOutput; |
| 34 | + private AVCaptureSession? session; |
| 35 | + private FrameProcessor? frameProcessor; |
| 36 | + private IntPtr bitmapHeader; |
| 37 | + |
| 38 | + public AVFoundationDevice(string uniqueID, string modelID) : |
| 39 | + base(uniqueID, modelID) |
| 40 | + { |
| 41 | + this.uniqueID = uniqueID; |
| 42 | + } |
| 43 | + |
| 44 | + protected override async Task OnDisposeAsync() |
| 45 | + { |
| 46 | + if (this.session != null) |
| 47 | + { |
| 48 | + this.session.StopRunning(); |
| 49 | + this.session.Dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + this.device?.Dispose(); |
| 53 | + this.deviceInput?.Dispose(); |
| 54 | + this.deviceOutput?.Dispose(); |
| 55 | + this.queue?.Dispose(); |
| 56 | + |
| 57 | + Marshal.FreeHGlobal(this.bitmapHeader); |
| 58 | + |
| 59 | + if (frameProcessor is not null) |
| 60 | + { |
| 61 | + await frameProcessor.DisposeAsync().ConfigureAwait(false); |
| 62 | + } |
| 63 | + |
| 64 | + await base.OnDisposeAsync().ConfigureAwait(false); |
| 65 | + } |
| 66 | + |
| 67 | + protected override Task OnInitializeAsync(VideoCharacteristics characteristics, TranscodeFormats transcodeFormat, |
| 68 | + FrameProcessor frameProcessor, CancellationToken ct) |
| 69 | + { |
| 70 | + |
| 71 | + this.frameProcessor = frameProcessor; |
| 72 | + this.Characteristics = characteristics; |
| 73 | + |
| 74 | + if (!NativeMethods_AVFoundation.PixelFormatMap.TryGetValue(characteristics.PixelFormat, out var pixelFormatType) || |
| 75 | + !NativeMethods.GetCompressionAndBitCount(characteristics.PixelFormat, out var compression, out var bitCount)) |
| 76 | + { |
| 77 | + throw new ArgumentException( |
| 78 | + $"FlashCap: Couldn't set video format: UniqueID={this.uniqueID}"); |
| 79 | + } |
| 80 | + |
| 81 | + this.bitmapHeader = NativeMethods.AllocateMemory(new IntPtr(MarshalEx.SizeOf<BITMAPINFOHEADER>())); |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + unsafe |
| 86 | + { |
| 87 | + var pBih = (BITMAPINFOHEADER*)this.bitmapHeader.ToPointer(); |
| 88 | + |
| 89 | + pBih->biSize = MarshalEx.SizeOf<BITMAPINFOHEADER>(); |
| 90 | + pBih->biCompression = compression; |
| 91 | + pBih->biPlanes = 1; |
| 92 | + pBih->biBitCount = bitCount; |
| 93 | + pBih->biWidth = characteristics.Width; |
| 94 | + pBih->biHeight = characteristics.Height; |
| 95 | + pBih->biSizeImage = pBih->CalculateImageSize(); |
| 96 | + } |
| 97 | + |
| 98 | + this.queue = new DispatchQueue(nameof(FlashCap)); |
| 99 | + this.device = AVCaptureDevice.DeviceWithUniqueID(uniqueID) |
| 100 | + ?? throw new InvalidOperationException( |
| 101 | + $"FlashCap: Couldn't find device: UniqueID={this.uniqueID}"); |
| 102 | + |
| 103 | + this.device.LockForConfiguration(); |
| 104 | + try |
| 105 | + { |
| 106 | + var formatSelected = this.device.Formats |
| 107 | + .FirstOrDefault(format => |
| 108 | + format.FormatDescription.Dimensions is var dimensions && |
| 109 | + format.FormatDescription.MediaType == CMMediaType.Video && |
| 110 | + dimensions.Width == characteristics.Width && |
| 111 | + dimensions.Height == characteristics.Height) |
| 112 | + ?? throw new InvalidOperationException( |
| 113 | + $"FlashCap: Couldn't set video format: UniqueID={this.uniqueID}"); |
| 114 | + |
| 115 | + this.device.ActiveFormat = formatSelected; |
| 116 | + |
| 117 | + var frameDuration = CMTimeMake( |
| 118 | + characteristics.FramesPerSecond.Denominator, |
| 119 | + characteristics.FramesPerSecond.Numerator); |
| 120 | + |
| 121 | + device.ActiveVideoMinFrameDuration = frameDuration; |
| 122 | + device.ActiveVideoMaxFrameDuration = frameDuration; |
| 123 | + |
| 124 | + this.deviceInput = new AVCaptureDeviceInput(device); |
| 125 | + |
| 126 | + this.deviceOutput = new AVCaptureVideoDataOutput(); |
| 127 | + |
| 128 | + if (this.deviceOutput.AvailableVideoCVPixelFormatTypes?.Any() == true) |
| 129 | + { |
| 130 | + var validPixelFormat = this.deviceOutput.AvailableVideoCVPixelFormatTypes.FirstOrDefault(p => p == pixelFormatType); |
| 131 | + this.deviceOutput.SetPixelFormatType(validPixelFormat); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + // Fallback to the mapped pixel format if no available list is provided |
| 136 | + this.deviceOutput.SetPixelFormatType(pixelFormatType); |
| 137 | + } |
| 138 | + |
| 139 | + this.deviceOutput.SetSampleBufferDelegate(new VideoBufferHandler(this), this.queue); |
| 140 | + this.deviceOutput.AlwaysDiscardsLateVideoFrames = true; |
| 141 | + } |
| 142 | + finally |
| 143 | + { |
| 144 | + this.device.UnlockForConfiguration(); |
| 145 | + } |
| 146 | + |
| 147 | + this.session = new AVCaptureSession(); |
| 148 | + this.session.AddInput(this.deviceInput); |
| 149 | + |
| 150 | + if (this.session.CanAddOutput(deviceOutput)) |
| 151 | + { |
| 152 | + this.session.AddOutput(this.deviceOutput); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + throw new Exception("Can't add video output"); |
| 157 | + } |
| 158 | + |
| 159 | + return TaskCompat.CompletedTask; |
| 160 | + } |
| 161 | + catch |
| 162 | + { |
| 163 | + NativeMethods.FreeMemory(this.bitmapHeader); |
| 164 | + throw; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + protected override Task OnStartAsync(CancellationToken ct) |
| 169 | + { |
| 170 | + this.session?.StartRunning(); |
| 171 | + return TaskCompat.CompletedTask; |
| 172 | + } |
| 173 | + |
| 174 | + protected override Task OnStopAsync(CancellationToken ct) |
| 175 | + { |
| 176 | + this.session?.StopRunning(); |
| 177 | + return TaskCompat.CompletedTask; |
| 178 | + } |
| 179 | + |
| 180 | + protected override void OnCapture(IntPtr pData, int size, long timestampMicroseconds, long frameIndex, PixelBuffer buffer) |
| 181 | + { |
| 182 | + buffer.CopyIn(this.bitmapHeader, pData, size, timestampMicroseconds, frameIndex, TranscodeFormats.Auto); |
| 183 | + } |
| 184 | + |
| 185 | + internal sealed class VideoBufferHandler : AVCaptureVideoDataOutputSampleBuffer |
| 186 | + { |
| 187 | + private readonly AVFoundationDevice device; |
| 188 | + private int frameIndex; |
| 189 | + |
| 190 | + public VideoBufferHandler(AVFoundationDevice device) |
| 191 | + { |
| 192 | + this.device = device; |
| 193 | + } |
| 194 | + |
| 195 | + public override void DidDropSampleBuffer(IntPtr captureOutput, IntPtr sampleBuffer, IntPtr connection) |
| 196 | + { |
| 197 | + Debug.WriteLine("Dropped"); |
| 198 | + var valid = CMSampleBufferIsValid(sampleBuffer); |
| 199 | + } |
| 200 | + |
| 201 | + public void CaptureOutputCallback(IntPtr self, IntPtr _cmd, IntPtr output, IntPtr sampleBuffer, |
| 202 | + IntPtr connection) |
| 203 | + { |
| 204 | + |
| 205 | + var pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 206 | + |
| 207 | + if (pixelBuffer == IntPtr.Zero) |
| 208 | + { |
| 209 | + throw new Exception("Failed to get image buffer"); |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + // Lock the base address for reading, process the buffer, etc. |
| 214 | + CVPixelBufferLockBaseAddress(pixelBuffer, PixelBufferLockFlags.ReadOnly); |
| 215 | + |
| 216 | + try |
| 217 | + { |
| 218 | + // Process the pixel buffer as needed |
| 219 | + this.device.frameProcessor?.OnFrameArrived( |
| 220 | + this.device, |
| 221 | + CVPixelBufferGetBaseAddress(pixelBuffer), |
| 222 | + (int)CVPixelBufferGetDataSize(pixelBuffer), |
| 223 | + (long)(CMTimeGetSeconds(CMSampleBufferGetDecodeTimeStamp(sampleBuffer)) * 1000), |
| 224 | + frameIndex++); |
| 225 | + } |
| 226 | + finally |
| 227 | + { |
| 228 | + CVPixelBufferUnlockBaseAddress(pixelBuffer, PixelBufferLockFlags.ReadOnly); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments