mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
[Tools] Add DShow webcam report tool (#10502)
This commit is contained in:
26
tools/WebcamReportTool/DirectShowUtils.cpp
Normal file
26
tools/WebcamReportTool/DirectShowUtils.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "DirectShowUtils.h"
|
||||
|
||||
void FreeMediaTypeHelper(AM_MEDIA_TYPE& mt)
|
||||
{
|
||||
if (mt.cbFormat != 0)
|
||||
{
|
||||
CoTaskMemFree(mt.pbFormat);
|
||||
mt.cbFormat = 0;
|
||||
mt.pbFormat = nullptr;
|
||||
}
|
||||
if (mt.pUnk != nullptr)
|
||||
{
|
||||
mt.pUnk->Release();
|
||||
mt.pUnk = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void DeleteMediaTypeHelper(AM_MEDIA_TYPE* pmt)
|
||||
{
|
||||
if (!pmt)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FreeMediaTypeHelper(*pmt);
|
||||
CoTaskMemFree(const_cast<AM_MEDIA_TYPE*>(pmt));
|
||||
}
|
||||
22
tools/WebcamReportTool/DirectShowUtils.h
Normal file
22
tools/WebcamReportTool/DirectShowUtils.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <initguid.h>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <dshow.h>
|
||||
|
||||
#include <wil/com.h>
|
||||
#include <winrt/Windows.Foundation.h>
|
||||
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
std::ofstream& log();
|
||||
|
||||
#define TRACE log() << __FUNCTION__ << '\n';
|
||||
#define LOG(msg) log() << msg << '\n';
|
||||
|
||||
void DeleteMediaTypeHelper(AM_MEDIA_TYPE* pmt);
|
||||
|
||||
using unique_media_type_ptr =
|
||||
wistd::unique_ptr<AM_MEDIA_TYPE, wil::function_deleter<decltype(&DeleteMediaTypeHelper), DeleteMediaTypeHelper>>;
|
||||
25
tools/WebcamReportTool/WebcamReportTool.sln
Normal file
25
tools/WebcamReportTool/WebcamReportTool.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31005.135
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebcamReportTool", "WebcamReportTool.vcxproj", "{FC9599B4-68CD-E14B-71D7-456FDD8D0845}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{FC9599B4-68CD-E14B-71D7-456FDD8D0845}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{FC9599B4-68CD-E14B-71D7-456FDD8D0845}.Debug|x64.Build.0 = Debug|x64
|
||||
{FC9599B4-68CD-E14B-71D7-456FDD8D0845}.Release|x64.ActiveCfg = Release|x64
|
||||
{FC9599B4-68CD-E14B-71D7-456FDD8D0845}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {536C9E03-09C5-4CAA-AA5E-D62D34A5C09A}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
69
tools/WebcamReportTool/WebcamReportTool.vcxproj
Normal file
69
tools/WebcamReportTool/WebcamReportTool.vcxproj
Normal file
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" />
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{FC9599B4-68CD-E14B-71D7-456FDD8D0845}</ProjectGuid>
|
||||
<IgnoreWarnCompileDuplicatedFilename>true</IgnoreWarnCompileDuplicatedFilename>
|
||||
<RootNamespace>WebcamReportTool</RootNamespace>
|
||||
<ProjectName>WebcamReportTool</ProjectName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<IntDir>$(SolutionDir)..\..\$(Platform)\$(Configuration)\obj\$(ProjectName)\</IntDir>
|
||||
<OutDir>$(SolutionDir)..\..\$(Platform)\$(Configuration)\$(ProjectName)\</OutDir>
|
||||
</PropertyGroup>
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<TargetName>WebcamReportTool</TargetName>
|
||||
<TargetExt>.exe</TargetExt>
|
||||
<IncludePath>include;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<TargetName>WebcamReportTool</TargetName>
|
||||
<TargetExt>.exe</TargetExt>
|
||||
<IncludePath>include;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>Windowsapp.lib;Strmiids.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<TreatLinkerWarningAsErrors>true</TreatLinkerWarningAsErrors>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="DirectShowUtils.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DirectShowUtils.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" />
|
||||
<Import Project="packages\Microsoft.Windows.ImplementationLibrary.1.0.200902.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('packages\Microsoft.Windows.ImplementationLibrary.1.0.200902.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
|
||||
</ImportGroup>
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.props'))" />
|
||||
<Error Condition="!Exists('packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Windows.CppWinRT.2.0.200729.8\build\native\Microsoft.Windows.CppWinRT.targets'))" />
|
||||
<Error Condition="!Exists('packages\Microsoft.Windows.ImplementationLibrary.1.0.200902.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Windows.ImplementationLibrary.1.0.200902.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
219
tools/WebcamReportTool/main.cpp
Normal file
219
tools/WebcamReportTool/main.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
#include <initguid.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
#include <windows.h>
|
||||
#include <dshow.h>
|
||||
#include <cguid.h>
|
||||
|
||||
#include <wil/com.h>
|
||||
#include <wil/resource.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <Shlobj.h>
|
||||
#include <Shlobj_core.h>
|
||||
|
||||
#include "DirectShowUtils.h"
|
||||
|
||||
std::ofstream& log()
|
||||
{
|
||||
static std::ofstream report = []{
|
||||
char buffer[MAX_PATH]{};
|
||||
if (SHGetSpecialFolderPathA(HWND_DESKTOP, buffer, CSIDL_DESKTOP, false))
|
||||
{
|
||||
std::string path = buffer;
|
||||
path += "\\WebcamReport.txt";
|
||||
return std::ofstream{ path };
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::ofstream{ "WebcamReport.txt" };
|
||||
}
|
||||
}();
|
||||
return report;
|
||||
}
|
||||
|
||||
std::string GetMediaSubTypeString(const GUID& guid)
|
||||
{
|
||||
if (guid == MEDIASUBTYPE_RGB24)
|
||||
{
|
||||
return "MEDIASUBTYPE_RGB24";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_RGB32)
|
||||
{
|
||||
return "MEDIASUBTYPE_RGB32";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_YUY2)
|
||||
{
|
||||
return "MEDIASUBTYPE_YUY2";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_MJPG)
|
||||
{
|
||||
return "MEDIASUBTYPE_MJPG";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_NV12)
|
||||
{
|
||||
return "MEDIASUBTYPE_NV12";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_NV11)
|
||||
{
|
||||
return "MEDIASUBTYPE_NV11";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_YV12)
|
||||
{
|
||||
return "MEDIASUBTYPE_YV12";
|
||||
}
|
||||
else if (guid == MEDIASUBTYPE_YUYV)
|
||||
{
|
||||
return "MEDIASUBTYPE_YUYV";
|
||||
}
|
||||
else
|
||||
{
|
||||
OLECHAR* guidString = nullptr;
|
||||
StringFromCLSID(guid, &guidString);
|
||||
if (guidString)
|
||||
{
|
||||
std::wstring_view wideView{guidString};
|
||||
std::string result;
|
||||
for (const auto c :wideView)
|
||||
{
|
||||
result += static_cast<char>(c);
|
||||
}
|
||||
::CoTaskMemFree(guidString);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "MEDIASUBTYPE_UNKNOWN";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LogMediaTypes(wil::com_ptr_nothrow<IPin>& pin)
|
||||
{
|
||||
wil::com_ptr_nothrow<IEnumMediaTypes> mediaTypeEnum;
|
||||
if (pin->EnumMediaTypes(&mediaTypeEnum); !mediaTypeEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ULONG _ = 0;
|
||||
unique_media_type_ptr mt;
|
||||
log() << "Supported formats:\n";
|
||||
while (mediaTypeEnum->Next(1, wil::out_param(mt), &_) == S_OK)
|
||||
{
|
||||
if (mt->majortype != MEDIATYPE_Video)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto format = reinterpret_cast<VIDEOINFOHEADER*>(mt->pbFormat);
|
||||
if (!format->AvgTimePerFrame)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const auto formatAvgFPS = 10000000LL / format->AvgTimePerFrame;
|
||||
log() << GetMediaSubTypeString(mt->subtype) << '\t' << format->bmiHeader.biWidth << "x"
|
||||
<< format->bmiHeader.biHeight << " - " << formatAvgFPS << "fps\n";
|
||||
}
|
||||
log() << '\n';
|
||||
}
|
||||
|
||||
void ReportAllWebcams()
|
||||
{
|
||||
auto enumeratorFactory = wil::CoCreateInstanceNoThrow<ICreateDevEnum>(CLSID_SystemDeviceEnum);
|
||||
if (!enumeratorFactory)
|
||||
{
|
||||
LOG("Couldn't create devenum factory");
|
||||
return;
|
||||
}
|
||||
|
||||
wil::com_ptr_nothrow<IEnumMoniker> enumMoniker;
|
||||
enumeratorFactory->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &enumMoniker, CDEF_DEVMON_PNP_DEVICE);
|
||||
if (!enumMoniker)
|
||||
{
|
||||
LOG("Couldn't create class enumerator");
|
||||
return;
|
||||
}
|
||||
|
||||
ULONG _ = 0;
|
||||
wil::com_ptr_nothrow<IMoniker> moniker;
|
||||
while (enumMoniker->Next(1, &moniker, &_) == S_OK)
|
||||
{
|
||||
wil::com_ptr_nothrow<IPropertyBag> propertyData;
|
||||
moniker->BindToStorage(nullptr, nullptr, IID_IPropertyBag, reinterpret_cast<void**>(&propertyData));
|
||||
if (!propertyData)
|
||||
{
|
||||
LOG("BindToStorage failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
wil::unique_variant propVal;
|
||||
propVal.vt = VT_BSTR;
|
||||
|
||||
if (FAILED(propertyData->Read(L"FriendlyName", &propVal, nullptr)))
|
||||
{
|
||||
LOG("Couldn't obtain FriendlyName property");
|
||||
continue;
|
||||
}
|
||||
std::wstring wideFriendlyName = { propVal.bstrVal, SysStringLen(propVal.bstrVal) };
|
||||
std::string friendlyName;
|
||||
for (wchar_t c : wideFriendlyName)
|
||||
{
|
||||
friendlyName += (char)c;
|
||||
}
|
||||
log() << "Webcam " << friendlyName << '\n';
|
||||
|
||||
propVal.reset();
|
||||
propVal.vt = VT_BSTR;
|
||||
|
||||
if (FAILED(propertyData->Read(L"DevicePath", &propVal, nullptr)))
|
||||
{
|
||||
LOG("Couldn't obtain DevicePath property");
|
||||
continue;
|
||||
}
|
||||
wil::com_ptr_nothrow<IBaseFilter> filter;
|
||||
moniker->BindToObject(nullptr, nullptr, IID_IBaseFilter, reinterpret_cast<void**>(&filter));
|
||||
if (!filter)
|
||||
{
|
||||
LOG("Couldn't BindToObject");
|
||||
continue;
|
||||
}
|
||||
|
||||
wil::com_ptr_nothrow<IEnumPins> pinsEnum;
|
||||
if (FAILED(filter->EnumPins(&pinsEnum)))
|
||||
{
|
||||
LOG("BindToObject EnumPins");
|
||||
continue;
|
||||
}
|
||||
wil::com_ptr_nothrow<IPin> pin;
|
||||
|
||||
while (pinsEnum->Next(1, &pin, &_) == S_OK)
|
||||
{
|
||||
// Skip pins which do not belong to capture category
|
||||
GUID category{};
|
||||
DWORD __;
|
||||
if (auto props = pin.try_copy<IKsPropertySet>();
|
||||
!props ||
|
||||
FAILED(props->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, nullptr, 0, &category, sizeof(GUID), &__)) ||
|
||||
category != PIN_CATEGORY_CAPTURE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip non-output pins
|
||||
if (PIN_DIRECTION direction = {}; FAILED(pin->QueryDirection(&direction)) || direction != PINDIR_OUTPUT)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
LogMediaTypes(pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
auto comCtx = wil::CoInitializeEx();
|
||||
log() << "Report started\n";
|
||||
ReportAllWebcams();
|
||||
return 0;
|
||||
}
|
||||
5
tools/WebcamReportTool/packages.config
Normal file
5
tools/WebcamReportTool/packages.config
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.Windows.CppWinRT" version="2.0.200729.8" targetFramework="native" />
|
||||
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.200902.2" targetFramework="native" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user