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
9 changes: 2 additions & 7 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ jobs:

#-----------------------------------------------------------------------
# Setup environments
# Setup environments

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
2.2.x
3.1.x
5.0.x
6.0.x
7.0.x
8.0.x
9.0.x
10.0.x

- name: Setup NuGet package reference
run: |
Expand All @@ -47,7 +42,7 @@ jobs:

#-----------------------------------------------------------------------
# Build

- name: Build
run: dotnet build -p:Configuration=Release -p:Platform="Any CPU" -p:RestoreNoCache=True -p:BuildIdentifier=${GITHUB_RUN_NUMBER} FlashCap.sln

Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -30,7 +30,7 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/kekyo/FlashCap</PackageProjectUrl>
<PackageIcon>FlashCap.100.png</PackageIcon>
<PackageTags>image;camera;capture;independent;multi-platform;frame-grabber;direct-show;video-for-windows;v4l2;windows;linux</PackageTags>
<PackageTags>image;camera;capture;independent;multi-platform;frame-grabber;media-foundation;v4l2;avfoundation;windows;linux;macos</PackageTags>
<AllowedOutputExtensionsInPackageBuildOutputFolder>.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<NoWarn>$(NoWarn);NU1605;NU1701;NU1803;NU1902;NU1903</NoWarn>
</PropertyGroup>
Expand Down
7 changes: 4 additions & 3 deletions FSharp.FlashCap/FSharp.FlashCap.fsproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;netstandard2.0;netstandard2.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
<PackageReference Update="FSharp.Core" Version="5.0.0" />
<PackageReference Update="FSharp.Core" Version="10.0.100" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 0 additions & 4 deletions FlashCap.Core/CaptureDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ protected CaptureDevice(object identity, string name)
this.Name = name;
}

#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public void Dispose() =>
_ = this.DisposeAsync().ConfigureAwait(false);

Expand Down Expand Up @@ -101,9 +99,7 @@ internal async Task InternalStopAsync(CancellationToken ct)
await this.OnStopAsync(ct);
}

#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal void InternalOnCapture(
IntPtr pData, int size, long timestampMicroseconds, long frameIndex, PixelBuffer buffer) =>
this.OnCapture(pData, size, timestampMicroseconds, frameIndex, buffer);
Expand Down
20 changes: 12 additions & 8 deletions FlashCap.Core/CaptureDeviceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace FlashCap;
public enum DeviceTypes
{
VideoForWindows,
DirectShow,
V4L2,
AVFoundation,
MediaFoundation,
}

public enum TranscodeFormats
Expand Down Expand Up @@ -78,9 +78,7 @@ public override string ToString() =>

//////////////////////////////////////////////////////////////////////////

#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal Task<CaptureDevice> InternalOpenWithFrameProcessorAsync(
VideoCharacteristics characteristics,
TranscodeFormats transcodeFormat,
Expand Down Expand Up @@ -121,7 +119,8 @@ internal async Task<byte[]> InternalTakeOneShotAsync(
TranscodeFormats transcodeFormat,
CancellationToken ct)
{
var tcs = new TaskCompletionSource<byte[]>();
var tcs = new TaskCompletionSource<byte[]>(TaskCreationOptions.RunContinuationsAsynchronously);
using var registration = ct.Register(() => tcs.TrySetCanceled(ct));

using var device = await this.OnOpenWithFrameProcessorAsync(
characteristics, transcodeFormat,
Expand All @@ -138,9 +137,14 @@ internal async Task<byte[]> InternalTakeOneShotAsync(
ct);

await device.InternalStartAsync(ct);
var image = await tcs.Task;
await device.InternalStopAsync(ct);

return image;
try
{
return await tcs.Task.ConfigureAwait(false);
}
finally
{
await device.InternalStopAsync(default).
ConfigureAwait(false);
}
}
}
24 changes: 10 additions & 14 deletions FlashCap.Core/CaptureDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using FlashCap.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace FlashCap;
Expand All @@ -30,19 +29,16 @@ public CaptureDevices() :
public CaptureDevices(BufferPool defaultBufferPool) =>
this.DefaultBufferPool = defaultBufferPool;

protected virtual IEnumerable<CaptureDeviceDescriptor> OnEnumerateDescriptors() =>
NativeMethods.CurrentPlatform switch
{
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>(),
};
protected virtual IEnumerable<CaptureDeviceDescriptor> OnEnumerateDescriptors()
{
if (OperatingSystem.IsWindows())
return new MediaFoundationDevices(this.DefaultBufferPool).OnEnumerateDescriptors();
if (OperatingSystem.IsLinux())
return new V4L2Devices(this.DefaultBufferPool).OnEnumerateDescriptors();
if (OperatingSystem.IsMacOS())
return new AVFoundationDevices(this.DefaultBufferPool).OnEnumerateDescriptors();
return ArrayEx.Empty<CaptureDeviceDescriptor>();
}

internal IEnumerable<CaptureDeviceDescriptor> InternalEnumerateDescriptors() =>
this.OnEnumerateDescriptors();
Expand Down
14 changes: 12 additions & 2 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 Expand Up @@ -54,7 +56,11 @@ protected override async Task OnDisposeAsync()
this.deviceOutput?.Dispose();
this.queue?.Dispose();

Marshal.FreeHGlobal(this.bitmapHeader);
if (this.bitmapHeader != IntPtr.Zero)
{
NativeMethods.FreeMemory(this.bitmapHeader);
this.bitmapHeader = IntPtr.Zero;
}

if (frameProcessor is not null)
{
Expand Down Expand Up @@ -160,7 +166,11 @@ format.FormatDescription.Dimensions is var dimensions &&
}
catch
{
NativeMethods.FreeMemory(this.bitmapHeader);
if (this.bitmapHeader != IntPtr.Zero)
{
NativeMethods.FreeMemory(this.bitmapHeader);
this.bitmapHeader = IntPtr.Zero;
}
throw;
}
}
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
2 changes: 2 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 Down
Loading