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
202 changes: 188 additions & 14 deletions avstream/avscamera/DMFT/AvsCameraDMFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#ifdef MF_WPP
#include "AvsCameraDMFT.tmh" //--REF_ANALYZER_DONT_REMOVE--
#endif

// TODO: required to avoid bug OS bug 36971659 in extended property handling that introduces a 16 bytes cookie
typedef struct
{
//byte cookieBuffer[16];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented out code

KSCAMERA_EXTENDEDPROP_HEADER header;
} KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED, * PKSCAMERA_EXTENDEDPROP_HEADER_BUFFERED;

//
// This DeviceMFT is a stripped down implementation of the device MFT Sample present in the sample Repo
// The original DMFT is present at https://github.com/microsoft/Windows-driver-samples/tree/main/avstream/sampledevicemft
Expand All @@ -18,8 +26,11 @@ CMultipinMft::CMultipinMft()
m_lWorkQueuePriority ( 0 ),
m_spAttributes( nullptr ),
m_spSourceTransform( nullptr ),
m_SymbolicLink(nullptr)

m_SymbolicLink(nullptr),
m_hSelectedProfileKSEvent { nullptr },
m_hSelectedProfileKSEventSentToDriver { nullptr},
m_isProfileDDISupportedInBaseDriver{},
m_selectedProfileId { KSCAMERAPROFILE_Legacy, 0, 0 }
{
HRESULT hr = S_OK;
ComPtr<IMFAttributes> pAttributes = nullptr;
Expand All @@ -29,6 +40,7 @@ CMultipinMft::CMultipinMft()
DMFTCHECKHR_GOTO(pAttributes->SetUINT32( MF_SA_D3D_AWARE, TRUE ), done);
DMFTCHECKHR_GOTO(pAttributes->SetString( MFT_ENUM_HARDWARE_URL_Attribute, L"SampleMultiPinMft" ),done);
m_spAttributes = pAttributes;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: remove

done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
}
Expand Down Expand Up @@ -638,6 +650,10 @@ IFACEMETHODIMP CMultipinMft::ProcessInput(
goto done;
}

if (m_selectedProfileId.Type == KSCAMERAPROFILE_FaceAuth_Mode)
{
// DMFT might switch to different behavior when profile, KSCAMERAPROFILE_FaceAuth_Mode is selected.
}
DMFTCHECKHR_GOTO(spInPin->SendSample( pSample ), done );
done:
DMFTRACE( DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr );
Expand Down Expand Up @@ -990,13 +1006,23 @@ IFACEMETHODIMP CMultipinMft::KsProperty(
--*/
{
HRESULT hr = S_OK;

/// PDMFT only cares about ExtendedCameraControls, all others
/// are just blindly forwarded to the upstream DMFTs.
if (!IsEqualCLSID(pProperty->Set, KSPROPERTYSETID_ExtendedCameraControl))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsEqualCLSID

requires a lock

{
DMFTCHECKHR_GOTO(m_spIkscontrol->KsProperty(pProperty, ulPropertyLength, pvPropertyData,
ulDataLength, pulBytesReturned), done);
goto done;
}

DMFTCHECKHR_GOTO(m_spIkscontrol->KsProperty(pProperty,
ulPropertyLength,
pvPropertyData,
ulDataLength,
pulBytesReturned),done);
if (pProperty->Id == KSPROPERTY_CAMERACONTROL_EXTENDED_PROFILE)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (pProperty->Id == KSPROPERTY_CAMERACONTROL_EXTENDED_PROFILE)

This is incorrect. You're only checking against the Id. The combination should always be Set + Id to determine the control.

In this particular case, you're going to intercept any control whose Id is 34 regardless of whether that Id belongs to the KSPROPERTYSETID_ExtendedCameraControl or not. I think what you meant to do here is to intercept and call ProfilePropertyHandler only if the Set == KSPROPERTYSETID_ExtendedCameraControl && Id == KSPROPERTY_CAMERACONTROL_EXTENDED_PROFILE.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent poinnt!!

{
DMFTCHECKHR_GOTO(ProfilePropertyHandler(pProperty, ulPropertyLength, pvPropertyData, ulDataLength, pulBytesReturned), done);
goto done;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: only use goto for error case

}
done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}

Expand Down Expand Up @@ -1041,14 +1067,66 @@ IFACEMETHODIMP CMultipinMft::KsEvent(
{

HRESULT hr = S_OK;
// Handle the events here if you want, This sample passes the events to the driver
DMFTCHECKHR_GOTO(m_spIkscontrol->KsEvent(pEvent,
ulEventLength,
pEventData,
ulDataLength,
pBytesReturned), done);
// handle the event if it is to set profile
if (pEvent != nullptr
&& ulEventLength >= sizeof(KSEVENT)
&& pEvent->Set == KSEVENTSETID_ExtendedCameraControl
&& pEventData != nullptr
&& ulDataLength >= sizeof(KSEVENTDATA)
&& (pEvent->Id == KSPROPERTY_CAMERACONTROL_EXTENDED_PROFILE))
{

m_hSelectedProfileKSEvent = nullptr;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_hSelectedProfileKSEvent

use WIL handle object looks like this code leaks handles

if (DuplicateHandle(
GetCurrentProcess(),
((KSEVENTDATA*)(pEventData))->EventHandle.Event,
GetCurrentProcess(),
&m_hSelectedProfileKSEvent,
0,
FALSE,
DUPLICATE_SAME_ACCESS) == false)
{
return E_INVALIDARG;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return E_INVALIDARG;

match coding style of the rest of the code with macro checks

}
if (m_isProfileDDISupportedInBaseDriver.value_or(true))
{
m_hSelectedProfileKSEventSentToDriver = CreateEventExW(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_hSelectedProfileKSEventSentToDriver

use WIL event

nullptr,
nullptr,
0,
EVENT_ALL_ACCESS);

DMFTCHECKNULL_GOTO(m_hSelectedProfileKSEventSentToDriver, done, E_INVALIDARG);

KSEVENTDATA driverEventData = {};
driverEventData.NotificationType = KSEVENTF_EVENT_HANDLE;
driverEventData.EventHandle.Event = m_hSelectedProfileKSEventSentToDriver;
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "Handling profile set KsEvent, created profile KsEvent handle for driver: %p", m_hSelectedProfileKSEventSentToDriver);

// defer to source device
hr = m_spIkscontrol->KsEvent(pEvent, ulEventLength, (void*)(&driverEventData), ulDataLength, pBytesReturned);
if (FAILED(hr))
{
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "Failed to send profile KsEvent handle to driver: %p | hr=0x%08x", m_hSelectedProfileKSEventSentToDriver, hr);
m_hSelectedProfileKSEventSentToDriver = nullptr;
m_isProfileDDISupportedInBaseDriver = false;
}
}
}
else {
// Pass the events to the driver
hr = m_spIkscontrol->KsEvent(pEvent,
ulEventLength,
pEventData,
ulDataLength,
pBytesReturned);
if FAILED(hr)
{
return hr;
}
}
done:
return hr;
return S_OK;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S_OK

return hr

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If source device return failed on the following, hr won't hold S_OK here. I would assume if source device returned failed, but the DMFT itself supports the KsEvent, it should reply OK.

https://github.com/welljeng/Windows-driver-samples/blob/4a380c3ff93f2227e40865cb472638954b3c31a1/avstream/avscamera/DMFT/AvsCameraDMFT.cpp#L1107

}

//
Expand Down Expand Up @@ -1293,6 +1371,102 @@ IFACEMETHODIMP CMultipinMft::Shutdown(
return ShutdownEventGenerator();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to handle canceling event and outstanding controls in shutdown

}

/*++
Description:
Implements the KsProperty KSPROPERTY_CAMERACONTROL_EXTENDED_PROFILE handler.
--*/

HRESULT CMultipinMft::ProfilePropertyHandler(
_In_reads_bytes_(ulPropertyLength) PKSPROPERTY pProperty,
_In_ ULONG ulPropertyLength,
_In_ LPVOID pPropertyData,
_In_ ULONG ulDataLength,
_Inout_ PULONG pulBytesReturned)
{

UNREFERENCED_PARAMETER(ulPropertyLength);
HRESULT hr = S_OK;

if (pProperty->Flags & KSPROPERTY_TYPE_SET)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check size

{
DMFTCHECKNULL_GOTO(pulBytesReturned, done, E_POINTER);
*pulBytesReturned = sizeof(KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED) + sizeof(KSCAMERA_EXTENDEDPROP_PROFILE);
if (ulDataLength < *pulBytesReturned)
{
return HRESULT_FROM_WIN32(ERROR_MORE_DATA);
}
if (pPropertyData)
{

PBYTE pPayload = (PBYTE)pPropertyData;
PKSCAMERA_EXTENDEDPROP_HEADER pExtendedHeader = &((PKSCAMERA_EXTENDEDPROP_HEADER_BUFFERED)pPayload)->header;
KSCAMERA_EXTENDEDPROP_PROFILE* pProfile = (PKSCAMERA_EXTENDEDPROP_PROFILE)(pExtendedHeader + 1);

m_selectedProfileId.Type = pProfile->ProfileId;
m_selectedProfileId.Index = pProfile->Index;
m_selectedProfileId.Unused = pProfile->Reserved;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This information is never forwarded to the driver so the SentToDriver event will never signal.


if (m_selectedProfileId.Type == GUID_NULL)
{
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_WARNING, "The caller incorrectly sets GUID_NULL, default back to legacy.");
m_selectedProfileId = { KSCAMERAPROFILE_Legacy, 0, 0 };
}

// signal we are done
if (m_hSelectedProfileKSEvent != nullptr)
{
if (m_hSelectedProfileKSEventSentToDriver != nullptr)
{
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "Waiting for driver profile KsEvent handle: %p", m_hSelectedProfileKSEventSentToDriver);
if ( WaitForSingleObjectEx(m_hSelectedProfileKSEventSentToDriver, kMAX_WAIT_TIME_DRIVER_PROFILE_KSEVENT, FALSE) != WAIT_OBJECT_0)
{

DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_ERROR,
"Waiting for driver profile KsEvent handle: %p timed out after %i ms, failing",
m_hSelectedProfileKSEventSentToDriver,
kMAX_WAIT_TIME_DRIVER_PROFILE_KSEVENT);
SetEvent(m_hSelectedProfileKSEvent);
return HRESULT_FROM_WIN32(ERROR_TIMEOUT);
}
m_hSelectedProfileKSEventSentToDriver = nullptr;
}
SetEvent(m_hSelectedProfileKSEvent);
m_hSelectedProfileKSEvent = nullptr;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait should not block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using MFPutWaitingWorkItem to signal the m_hSelectedProfileKSEvent (using m_hSelectedProfileKSEventSentToDriver as the event handle to the API in question). This doesn't afford you the ability to use a timeout, but the spec requires drivers to signal the event both in the case of success or if the operation can't complete.

}
}
else if (pProperty->Flags & KSPROPERTY_TYPE_GET)
{
DMFTCHECKNULL_GOTO(pulBytesReturned, done, E_POINTER);
*pulBytesReturned = sizeof(KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED) + sizeof(KSCAMERA_EXTENDEDPROP_PROFILE);
if (ulDataLength < *pulBytesReturned)
{
return HRESULT_FROM_WIN32(ERROR_MORE_DATA);
}
if (pPropertyData)
{
if (!m_isProfileDDISupportedInBaseDriver.has_value())
{
hr = m_spIkscontrol->KsProperty(pProperty, ulPropertyLength, pPropertyData, ulDataLength, pulBytesReturned);
m_isProfileDDISupportedInBaseDriver = SUCCEEDED(hr);
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "Profile DDI GET support on base driver: %d, hr=0x%08x", m_isProfileDDISupportedInBaseDriver.value(), hr);
*pulBytesReturned = sizeof(KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED) + sizeof(KSCAMERA_EXTENDEDPROP_PROFILE);
}
}
}
// --GETPAYLOAD--
else if (pProperty->Flags & KSPROPERTY_TYPE_GETPAYLOADSIZE)
{
DMFTCHECKNULL_GOTO(pulBytesReturned, done, E_POINTER);
*pulBytesReturned = sizeof(KSCAMERA_EXTENDEDPROP_HEADER_BUFFERED) + sizeof(PKSCAMERA_EXTENDEDPROP_PROFILE);
}

done:
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! exiting %x = %!HRESULT!", hr, hr);
return hr;
}


//
// Static method to create an instance of the MFT.
//
Expand Down
18 changes: 18 additions & 0 deletions avstream/avscamera/DMFT/AvsCameraDMFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "common.h"
#include "mftpeventgenerator.h"
#include "basepin.h"
#include <optional>

//
// The Below GUID is needed to transfer photoconfirmation sample successfully in the pipeline
Expand All @@ -19,6 +20,8 @@ DEFINE_GUID(MFSourceReader_SampleAttribute_MediaType_priv,

interface IDirect3DDeviceManager9;

constexpr int kMAX_WAIT_TIME_DRIVER_PROFILE_KSEVENT = 3000;// ms, amount of time to wait for the profile DDI KsEvent sent to the driver

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kMAX_WAIT_TIME_DRIVER_PROFILE_KSEVENT

I think pipeline should handle max wait


//
// Forward declarations
//
Expand Down Expand Up @@ -275,9 +278,18 @@ class CMultipinMft :
_In_opt_ IMFMediaType *pMediaType,
_In_ DeviceStreamState newState
);

HRESULT BridgeInputPinOutputPin(
_In_ CInPin* pInPin,
_In_ COutPin* pOutPin);

HRESULT ProfilePropertyHandler(
_In_reads_bytes_(ulPropertyLength) PKSPROPERTY pProperty,
_In_ ULONG ulPropertyLength,
_Inout_updates_to_(ulDataLength, *pulBytesReturned) LPVOID pPropertyData,
_In_ ULONG ulDataLength,
_Inout_ PULONG pulBytesReturned);

//
//Inline functions
//
Expand Down Expand Up @@ -320,8 +332,14 @@ class CMultipinMft :
UINT32 m_punValue;
ComPtr<IKsControl> m_spIkscontrol;
ComPtr<IMFAttributes> m_spAttributes;

map<int, int> m_outputPinMap; // How output pins are connected to input pins i-><0..outpins>
PWCHAR m_SymbolicLink;
HANDLE m_hSelectedProfileKSEvent;
HANDLE m_hSelectedProfileKSEventSentToDriver;
std::optional<bool> m_isProfileDDISupportedInBaseDriver;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_isProfileDDISupportedInBaseDriver

this should be a static property of the DMFT since it ideally is developed with the camera/driver it is being used with

Copy link
Author

@welljeng welljeng Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hesitate of make it static since if the same DMFT is being applied on multiple camera DMFT chain, all the DMFT instances will share the same m_isProfileDDISupportedInBaseDriver value, which might not be the ideal case.
Correct me if I were wong.

image

SENSORPROFILEID m_selectedProfileId;

};


Expand Down
5 changes: 5 additions & 0 deletions avstream/avscamera/DMFT/AvsCameraDMFT.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<ClCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories);..\..\common;..\common</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdcpp17

I would recommend minimizing the $(Configuration)|$(Platform) unique configs move the common ones to a global setting

</ClCompile>
<Midl>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
Expand All @@ -178,6 +179,7 @@
<ClCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories);..\..\common;..\common</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Midl>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
Expand All @@ -194,6 +196,7 @@
<ClCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories);..\..\common;..\common</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Midl>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
Expand All @@ -210,6 +213,7 @@
<ClCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories);..\..\common;..\common</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Midl>
<PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;MF_WPP;SECURITY_WIN32;MFT_UNIQUE_METHOD_NAMES;MF_DEVICEMFT_ALLOW_MFT0_LOAD</PreprocessorDefinitions>
Expand Down Expand Up @@ -237,6 +241,7 @@
<ItemGroup>
<None Exclude="@(None)" Include="*.txt;*.htm;*.html" />
<None Exclude="@(None)" Include="*.ico;*.cur;*.bmp;*.dlg;*.rct;*.gif;*.jpg;*.jpeg;*.wav;*.jpe;*.tiff;*.tif;*.png;*.rc2" />
<None Include="packages.config" />
<None Include="Source.def" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions avstream/avscamera/DMFT/AvsCameraDMFT.vcxproj.Filters
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion avstream/avscamera/DMFT/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.210204.1" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.250325.1" targetFramework="native" />
</packages>
Loading