Skip to content
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RootNamespace>FlashCap</RootNamespace>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1570;CS1591;CA1416;CS8981;NETSDK1215</NoWarn>
<NoWarn>$(NoWarn);CS1570;CS1591;CS8981;NETSDK1215</NoWarn>

<Product>FlashCap</Product>
<Trademark>FlashCap</Trademark>
Expand Down
1 change: 1 addition & 0 deletions FlashCap.Core/CaptureDeviceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum DeviceTypes
DirectShow,
V4L2,
AVFoundation,
MediaFoundation,
}

public enum TranscodeFormats
Expand Down
56 changes: 41 additions & 15 deletions FlashCap.Core/CaptureDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;

namespace FlashCap;

/// <summary>
/// By default, the following backends are considered for <see cref="OnEnumerateDescriptors"/>:
/// <list type= "bullet">
/// <item><description><see cref="DirectShowDevices"/> (windows)- Only if <c>RuntimeFeature.IsDynamicCodeSupported</c> is true</description></item>
/// <item><description><see cref="VideoForWindowsDevices"/> (windows)</description></item>
/// <item><description><c>MediaFoundationDevices</c> (Windows 7 or greater) - Supported on net48, netstandard2.0 or greater, .NET 5.0 or greater</description></item>
/// <item><description><see cref="V4L2Devices"/> (linux)</description></item>
/// <item><description><see cref="AVFoundationDevices"/> (macOs)</description></item>
/// </list>
/// </summary>
public class CaptureDevices
{
protected readonly BufferPool DefaultBufferPool;
Expand All @@ -26,23 +37,38 @@ public CaptureDevices() :
this(new DefaultBufferPool())
{
}

public CaptureDevices(BufferPool defaultBufferPool) =>
this.DefaultBufferPool = defaultBufferPool;

protected virtual IEnumerable<CaptureDeviceDescriptor> OnEnumerateDescriptors() =>
NativeMethods.CurrentPlatform switch
public CaptureDevices(BufferPool defaultBufferPool)
{
DefaultBufferPool = defaultBufferPool;
}

protected virtual IEnumerable<CaptureDeviceDescriptor> OnEnumerateDescriptors()
{
if (NativeMethods.IsWindows())
{
NativeMethods.Platforms.Windows =>
new DirectShowDevices(this.DefaultBufferPool).OnEnumerateDescriptors().
Concat(new VideoForWindowsDevices(this.DefaultBufferPool).OnEnumerateDescriptors()),
NativeMethods.Platforms.Linux =>
new V4L2Devices().OnEnumerateDescriptors(),
NativeMethods.Platforms.MacOS =>
new AVFoundationDevices().OnEnumerateDescriptors(),
_ =>
ArrayEx.Empty<CaptureDeviceDescriptor>(),
};
IEnumerable<CaptureDeviceDescriptor> descriptors = [];
#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER
if (RuntimeFeature.IsDynamicCodeSupported)
#endif
descriptors = new DirectShowDevices(this.DefaultBufferPool).OnEnumerateDescriptors();
descriptors = descriptors.Concat(new VideoForWindowsDevices(this.DefaultBufferPool).OnEnumerateDescriptors());
#if FLASHCAP_MEDIAFOUNDATION
if (NativeMethods.IsWindowsVersionAtLeast(6, 1))
descriptors = descriptors.Concat(new MediaFoundationDevices(this.DefaultBufferPool).OnEnumerateDescriptors());
#endif
return descriptors;
}
if (NativeMethods.IsLinux())
{
return new V4L2Devices().OnEnumerateDescriptors();
}
if (NativeMethods.IsMacOS())
{
return new AVFoundationDevices().OnEnumerateDescriptors();
}
return ArrayEx.Empty<CaptureDeviceDescriptor>();
}

internal IEnumerable<CaptureDeviceDescriptor> InternalEnumerateDescriptors() =>
this.OnEnumerateDescriptors();
Expand Down
2 changes: 2 additions & 0 deletions FlashCap.Core/Devices/AVFoundationDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using FlashCap.Internal;
Expand All @@ -23,6 +24,7 @@

namespace FlashCap.Devices;

[SupportedOSPlatform("macos")]
public sealed class AVFoundationDevice : CaptureDevice
{
private readonly string uniqueID;
Expand Down
2 changes: 2 additions & 0 deletions FlashCap.Core/Devices/AVFoundationDeviceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
//
////////////////////////////////////////////////////////////////////////////

using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

namespace FlashCap.Devices;

[SupportedOSPlatform("macos")]
public sealed class AVFoundationDeviceDescriptor : CaptureDeviceDescriptor
{
private readonly string uniqueId;
Expand Down
7 changes: 7 additions & 0 deletions FlashCap.Core/Devices/AVFoundationDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using FlashCap.Internal;
using FlashCap.Utilities;
using static FlashCap.Internal.AVFoundation.LibAVFoundation;

namespace FlashCap.Devices;

[SupportedOSPlatform("macos")]
public sealed class AVFoundationDevices : CaptureDevices
{
public AVFoundationDevices() :
Expand All @@ -32,6 +34,11 @@ public AVFoundationDevices(BufferPool defaultBufferPool) :

protected override IEnumerable<CaptureDeviceDescriptor> OnEnumerateDescriptors()
{
if (!NativeMethods.IsMacOS())
{
throw new PlatformNotSupportedException("AVFoundation capture requires macOS.");
}

if (AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video) != AVAuthorizationStatus.Authorized)
{
TaskCompletionSource<bool> tcs = new();
Expand Down
4 changes: 4 additions & 0 deletions FlashCap.Core/Devices/DirectShowDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
using FlashCap.Internal;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

namespace FlashCap.Devices;

[RequiresDynamicCode("Direct show depends on COM runtime generation and requires dynamic code")]
[SupportedOSPlatform("windows")]
public sealed class DirectShowDevice :
CaptureDevice
{
Expand Down
4 changes: 4 additions & 0 deletions FlashCap.Core/Devices/DirectShowDeviceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
//
////////////////////////////////////////////////////////////////////////////

using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

namespace FlashCap.Devices;

[RequiresDynamicCode("Direct show depends on COM runtime generation and requires dynamic code")]
[SupportedOSPlatform("windows")]
public sealed class DirectShowDeviceDescriptor : CaptureDeviceDescriptor
{
private readonly string devicePath;
Expand Down
4 changes: 4 additions & 0 deletions FlashCap.Core/Devices/DirectShowDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
using FlashCap.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Versioning;

namespace FlashCap.Devices;

[RequiresDynamicCode("Direct show depends on COM runtime generation and requires dynamic code")]
[SupportedOSPlatform("windows")]
public sealed class DirectShowDevices : CaptureDevices
{
public DirectShowDevices() :
Expand Down
Loading