Skip to content

Commit

Permalink
[ENH] MediaFoundation: Add VirtualCamera support (mfvirtualcamera.h)
Browse files Browse the repository at this point in the history
  • Loading branch information
amerkoleci committed Jul 30, 2024
1 parent cc2dda5 commit cfe0eb7
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Current Stable Release: 3.5.0 (March 2024)

-----------------------------------------------
Release: 3.6.X (August 2024)
- [ENH] MediaFoundation: Add VirtualCamera support (mfvirtualcamera.h)
- [ENH] General: Drop .net7 support
- [ENH] Vortice.Dxc: Handle correctly IDxcCompilerArgs bindings and improve interop
- [ENH] Vortice.WinUI interop improvements.
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<!-- Version -->
<PropertyGroup>
<VersionPrefix>3.5.10</VersionPrefix>
<VersionPrefix>3.5.11</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' == ''">beta</VersionSuffix>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<PackageVersion Include="Vortice.Dxc.Native" Version="1.0.1" />

<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.106" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.5.240607001" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.5.240627000" />

<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

</Project>
20 changes: 18 additions & 2 deletions src/Vortice.DirectX/Mappings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<include file="dxgitype.h" namespace="Vortice.DXGI" attach="true" />

<include file="dcommon.h" namespace="Vortice.DCommon" attach="true" />

<include file="Devpropdef.h" namespace="Vortice.Win32" attach="true" />

<!-- Multimedia -->

<include file="hidusage.h" namespace="Vortice.Multimedia" attach="true">
Expand Down Expand Up @@ -138,6 +139,9 @@
<create-cpp enum="HID_USAGE_PAGE" macro="HID_USAGE_PAGE_.*" />
<create-cpp enum="HID_USAGE_ID" macro="HID_USAGE_[^P].*" />
<!--<create-cpp enum="WAVE_FORMAT_ENCODING" macro="WAVE_FORMAT_(.*)" />-->

<!-- Devpropdef.h -->
<create-cpp enum="DEVPROP_TYPE" macro="DEVPROP_TYPE_.*" />
</extension>

<bindings>
Expand Down Expand Up @@ -288,6 +292,9 @@
<short name="AUTOCONVERTPCM">AutoConvertPCM</short>
<short name="EXPIREWHENUNOWNED">ExpireWhenUnowned</short>
<short name="HIDEWHENEXPIRED">HideWhenExpired</short>
<short name="DEVPROPKEY">DevPropertyKey</short>
<short name="DEVPROPTYPE">DevPropertyType</short>
<short name="NTSTATUS">NtStatus</short>
</naming>

<mapping>
Expand Down Expand Up @@ -437,6 +444,15 @@
<!-- dxcore -->
<map enum="DXCoreRuntimeFilterFlags" flags="true" none="false" />
<map enum="DXCoreHardwareTypeFilterFlags" flags="true" none="false" />


<!-- Devpropdef.h -->
<map enum="DEVPROP_TYPE" name="DevPropertyType" flags="false" namespace="Vortice.Win32"/>
<map enum="DEVPROPSTORE" name="DevPropertyStore" />
<map struct="DEVPROPCOMPKEY" name="DevPropertyCompoundKey" />
<map struct="DEVPROPERTY" name="DevProperty" />
<map field="DEVPROPERTY::Type" type="DEVPROP_TYPE" />
<map struct="DEVPROPKEY" name="DevPropertyKey" />
<map field="DEVPROPKEY::fmtid" name="FormatId" />
<map field="DEVPROPKEY::pid" name="PropertyId" />
</mapping>
</config>
6 changes: 3 additions & 3 deletions src/Vortice.DirectX/Multimedia/WaveFormatAdpcm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ internal unsafe void __MarshalFrom(ref __Native @ref)

private unsafe void __MarshalTo(ref __Native @ref)
{
if(Coefficients1 == null)
if (Coefficients1 == null)
throw new ArgumentNullException(nameof(Coefficients1));

if ( Coefficients1.Length > 7)
if (Coefficients1.Length > 7)
throw new InvalidOperationException("Unable to encode Adpcm format. Too may coefficients (max 7)");

extraSize = (short)(sizeof(int) + sizeof(int) * Coefficients1.Length);
Expand All @@ -149,6 +149,6 @@ private unsafe void __MarshalTo(ref __Native @ref)
pCoefs[i * 2 + 1] = Coefficients2[i];
}
}
}
}
#endregion
}
49 changes: 49 additions & 0 deletions src/Vortice.MediaFoundation/IMFVirtualCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using SharpGen.Runtime;
using Vortice.Mathematics;
using Vortice.Win32;

namespace Vortice.MediaFoundation;

public unsafe partial class IMFVirtualCamera
{
public Result AddProperty(DevPropertyKey key, DevPropertyType type, byte[] data)
{
fixed (byte* pbData = data)
return AddProperty(ref key, (uint)type, pbData, (uint)data.Length);
}

public Result AddProperty(DevPropertyKey key, DevPropertyType type, Span<byte> data)
{
fixed (byte* pbData = data)
return AddProperty(ref key, (uint)type, pbData, (uint)data.Length);
}

public Result AddProperty<T>(DevPropertyKey key, DevPropertyType type, Span<T> data)
where T : unmanaged
{
fixed (T* pbData = data)
return AddProperty(ref key, (uint)type, pbData, (uint)(sizeof(T) * data.Length));
}

public Result AddRegistryEntry(string entryName, string subkeyPath, int regType, byte[] data)
{
fixed (byte* pbData = data)
return AddRegistryEntry(entryName, subkeyPath, regType, pbData, (uint)data.Length);
}

public Result AddRegistryEntry(string entryName, string subkeyPath, int regType, Span<byte> data)
{
fixed (byte* pbData = data)
return AddRegistryEntry(entryName, subkeyPath, regType, pbData, (uint)data.Length);
}

public Result AddRegistryEntry<T>(string entryName, string subkeyPath, int regType, Span<T> data)
where T : unmanaged
{
fixed (T* pbData = data)
return AddRegistryEntry(entryName, subkeyPath, regType, pbData, (uint)(sizeof(T) * data.Length));
}
}
14 changes: 14 additions & 0 deletions src/Vortice.MediaFoundation/Mappings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</include>
<include file="mfmediaengine.h" attach="true"/>
<include file="mfcaptureengine.h" attach="true"/>
<include file="mfvirtualcamera.h" attach="true"/>

<extension>
<context id="mediafoundation-all"/>
Expand Down Expand Up @@ -974,6 +975,19 @@
<map function="MFCreateSourceReaderFromMediaSource" dll='"Mfreadwrite.dll"'/>
<map function="MFCreateSourceReaderFromURL" dll='"Mfreadwrite.dll"'/>

<!-- mfvirtualcamera.h -->
<map method="IMFCameraSyncObject::WaitOnSignal" hresult="true" check="false" />

<map method="IMFVirtualCamera::(.*)" hresult="true" check="false"/>
<map method="IMFVirtualCamera::AddProperty" property="false" visibility="private"/>
<map param="IMFVirtualCamera::AddProperty::pbData" type="void" keep-pointers="true"/>

<map method="IMFVirtualCamera::AddRegistryEntry" property="false" visibility="private"/>
<map param="IMFVirtualCamera::AddRegistryEntry::pbData" type="void" keep-pointers="true"/>

<map function="MFCreateVirtualCamera" dll='"mfsensorgroup.dll"' group="Vortice.MediaFoundation.MediaFactory" hresult="true" check="false" />
<map function="MFIsVirtualCameraTypeSupported" dll='"mfsensorgroup.dll"' group="Vortice.MediaFoundation.MediaFactory" hresult="true" check="false" />

<!-- WASAPI -->
<remove function="ActivateAudioInterfaceAsync"/>
</mapping>
Expand Down
56 changes: 56 additions & 0 deletions src/Vortice.MediaFoundation/MediaFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Amer Koleci and Contributors.
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.

using System.Runtime.InteropServices;
using SharpGen.Runtime;
using SharpGen.Runtime.Win32;
using Vortice.Multimedia;
Expand Down Expand Up @@ -240,4 +241,59 @@ public static Result MFSetAttributeSize(IMFAttributes attributes, Guid guidKey,
{
return MFSetAttribute2UInt32asUInt64(attributes, guidKey, width, height);
}

public static IMFVirtualCamera MFCreateVirtualCamera(
VirtualCameraType type,
VirtualCameraLifetime lifetime,
VirtualCameraAccess access,
string friendlyName,
string sourceId,
Guid[]? categories = default)
{
nint virtualCameraPtr = default;
Result result;

fixed (char* sourceId_ = sourceId)
fixed (char* friendlyName_ = friendlyName)
{
if (categories?.Length > 0)
{
fixed (Guid* categories_ = categories)
result = (Result)MFCreateVirtualCamera_(
(int)type,
(int)lifetime,
(int)access,
friendlyName_,
sourceId_,
categories_,
(uint)categories.Length,
&virtualCameraPtr
);
}
else
{
result = (Result)MFCreateVirtualCamera_(
(int)type,
(int)lifetime,
(int)access,
friendlyName_,
sourceId_,
null,
0,
&virtualCameraPtr);
}
}

result.CheckError();

return new IMFVirtualCamera(virtualCameraPtr);
}

public static bool MFIsVirtualCameraTypeSupported(VirtualCameraType type)
{
RawBool supported;
Result result = MFIsVirtualCameraTypeSupported_(unchecked((int)type), &supported);
result.CheckError();
return supported;
}
}

0 comments on commit cfe0eb7

Please sign in to comment.