Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
55 changes: 40 additions & 15 deletions FlashCap.Core/CaptureDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@
using FlashCap.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
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)</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,24 +38,37 @@ 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;
}

[RequiresUnreferencedCode("OnEnumerateDescriptors adds DirectShow which requires unreferenced code. Use platform-specific Devices directly.")]
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>(),
};
var 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>();
}

[RequiresUnreferencedCode("OnEnumerateDescriptors adds DirectShow which requires unreferenced code. Use platform-specific Devices directly.")]
internal IEnumerable<CaptureDeviceDescriptor> InternalEnumerateDescriptors() =>
this.OnEnumerateDescriptors();
}
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
9 changes: 9 additions & 0 deletions FlashCap.Core/Devices/AVFoundationDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
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 @@ -30,8 +33,14 @@ public AVFoundationDevices(BufferPool defaultBufferPool) :
{
}

[UnconditionalSuppressMessage("Trimming", "IL2046", Justification = "This backend does not require unreferenced code")]
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
7 changes: 6 additions & 1 deletion FlashCap.Core/Devices/DirectShowDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
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;

#if NET6_0_OR_GREATER // Remove this when dropping net5.0. Its required because RequiresUnreferencedCode was not allowed on class level in net5.0
[RequiresUnreferencedCode("Direct show depends on COM runtime generation and might require unreferenced code.")]
#endif
[SupportedOSPlatform("windows")]
public sealed class DirectShowDevice :
CaptureDevice
{
Expand Down
6 changes: 6 additions & 0 deletions FlashCap.Core/Devices/DirectShowDeviceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
//
////////////////////////////////////////////////////////////////////////////

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

namespace FlashCap.Devices;

#if NET6_0_OR_GREATER // Remove this when dropping net5.0. Its required because RequiresUnreferencedCode was not allowed on class level in net5.0
[RequiresUnreferencedCode("Direct show depends on COM runtime generation and might require unreferenced code.")]
#endif
[SupportedOSPlatform("windows")]
public sealed class DirectShowDeviceDescriptor : CaptureDeviceDescriptor
{
private readonly string devicePath;
Expand Down
6 changes: 6 additions & 0 deletions FlashCap.Core/Devices/DirectShowDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
using FlashCap.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Versioning;

namespace FlashCap.Devices;

#if NET6_0_OR_GREATER // Remove this when dropping net5.0. Its required because RequiresUnreferencedCode was not allowed on class level in net5.0
[RequiresUnreferencedCode("Direct show depends on COM runtime generation and might require unreferenced code.")]
#endif
[SupportedOSPlatform("windows")]
public sealed class DirectShowDevices : CaptureDevices
{
public DirectShowDevices() :
Expand Down
Loading