22static constexpr GUID KSCATEGORY_SENSOR_CAMERA = {
23 0x24e552d7, 0x6523, 0x47f7, { 0xa6, 0x47, 0xd3, 0x46, 0x5b, 0xf1, 0xf5, 0xca }
29LRESULT QT_WIN_CALLBACK deviceNotificationWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
31 if (message == WM_DEVICECHANGE) {
32 auto b = (PDEV_BROADCAST_HDR)lParam;
33 if (b && b->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
34 auto wmd =
reinterpret_cast<QWindowsVideoDevices *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
36 if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
37 wmd->onVideoInputsChanged();
51 wx.cbSize =
sizeof(WNDCLASSEX);
52 wx.lpfnWndProc = deviceNotificationWndProc;
53 wx.hInstance = GetModuleHandle(
nullptr);
54 wx.lpszClassName = windowClassName;
56 if (!RegisterClassEx(&wx))
59 auto hwnd = CreateWindowEx(0, windowClassName, TEXT(
"Message"),
60 0, 0, 0, 0, 0, HWND_MESSAGE,
nullptr,
nullptr,
nullptr);
62 UnregisterClass(windowClassName, GetModuleHandle(
nullptr));
70 : QPlatformVideoDevices(integration)
72 m_videoDeviceMsgWindow = createMessageOnlyWindow();
73 if (m_videoDeviceMsgWindow) {
74 SetWindowLongPtr(m_videoDeviceMsgWindow, GWLP_USERDATA, (LONG_PTR)
this);
76 DEV_BROADCAST_DEVICEINTERFACE di = {};
77 di.dbcc_size =
sizeof(di);
78 di.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
79 di.dbcc_classguid = KSCATEGORY_VIDEO_CAMERA;
81 m_videoDeviceNotification =
82 RegisterDeviceNotification(m_videoDeviceMsgWindow, &di, DEVICE_NOTIFY_WINDOW_HANDLE);
83 if (!m_videoDeviceNotification) {
84 DestroyWindow(m_videoDeviceMsgWindow);
85 m_videoDeviceMsgWindow =
nullptr;
87 UnregisterClass(windowClassName, GetModuleHandle(
nullptr));
91 if (!m_videoDeviceNotification) {
92 qWarning() <<
"Video device change notification disabled";
110 GUID subtype = GUID_NULL;
111 if (FAILED(mediaFormat->GetGUID(MF_MT_SUBTYPE, &subtype)))
114 auto pixelFormat = QWindowsMultimediaUtils::pixelFormatFromMediaSubtype(subtype);
115 if (pixelFormat == QVideoFrameFormat::Format_Invalid)
120 if (FAILED(MFGetAttributeSize(mediaFormat, MF_MT_FRAME_SIZE, &width, &height)))
122 QSize resolution{
int(width),
int(height) };
129 if (SUCCEEDED(MFGetAttributeRatio(mediaFormat, MF_MT_FRAME_RATE_RANGE_MIN, &num, &den)))
130 minFr =
float(num) /
float(den);
132 if (SUCCEEDED(MFGetAttributeRatio(mediaFormat, MF_MT_FRAME_RATE_RANGE_MAX, &num, &den)))
133 maxFr =
float(num) /
float(den);
135 auto *f =
new QCameraFormatPrivate{ QSharedData(), pixelFormat, resolution, minFr, maxFr };
154 auto info = std::make_unique<QCameraDevicePrivate>();
155 info->description = getString(device, MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME);
156 info->id = getString(device, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK).toUtf8();
158 IMFMediaSource *source = NULL;
159 HRESULT hr = device->ActivateObject(IID_PPV_ARGS(&source));
163 ComPtr<IMFSourceReader> reader;
164 hr = wmf.mfCreateSourceReaderFromMediaSource(source, NULL, reader.GetAddressOf());
168 QList<QSize> photoResolutions;
169 QList<QCameraFormat> videoFormats;
170 for (DWORD i = 0;; ++i) {
172 ComPtr<IMFMediaType> mediaFormat;
173 hr = reader->GetNativeMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, i,
174 mediaFormat.GetAddressOf());
178 auto maybeCamera = createCameraFormat(mediaFormat.Get());
180 videoFormats << *maybeCamera;
181 photoResolutions << maybeCamera->resolution();
185 info->videoFormats = videoFormats;
186 info->photoResolutions = photoResolutions;
187 return info.release()->create();
193 QList<QCameraDevice> cameras;
195 IMFActivate **devicesRaw =
nullptr;
196 HRESULT hr = wmf.mfEnumDeviceSources(attr, &devicesRaw, &count);
198 QComTaskResource<IMFActivate *[], QComDeleter> devices(devicesRaw, count);
200 for (UINT32 i = 0; i < count; i++) {
201 IMFActivate *device = devices[i];
203 auto maybeCamera = createCameraDevice(wmf, device);
205 cameras << *maybeCamera;
217 QList<QCameraDevice> cameras;
219 ComPtr<IMFAttributes> attr;
220 HRESULT hr = m_wmf->mfCreateAttributes(attr.GetAddressOf(), 2);
224 hr = attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
225 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
227 cameras << readCameraDevices(*m_wmf, attr.Get());
229 hr = attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_CATEGORY,
230 KSCATEGORY_SENSOR_CAMERA);
232 cameras << readCameraDevices(*m_wmf, attr.Get());