diff options
author | Piotr Srebrny <piotr.srebrny@qt.io> | 2022-05-03 08:29:53 +0200 |
---|---|---|
committer | Piotr Srebrny <piotr.srebrny@qt.io> | 2022-05-12 12:53:28 +0200 |
commit | 2aeddf0c93274a1bce97e6186ce9465863bb87a8 (patch) | |
tree | 88b11540f85565d2fa67c5c96f99fb4d93f1502e /src/multimedia/windows | |
parent | 583b991120596a5fbfdfd726ec228c23e819107e (diff) |
Extract QWindowsVideoDevices from QWindowsMediaDevices
This patch implments the split of QPlatformMediaDevice to audio and
video devices for the Windows platform.
Change-Id: I5976b96ce0b789637cfe6fffc5f52c875b6bc73b
Reviewed-by: André de la Rocha <andre.rocha@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/multimedia/windows')
-rw-r--r-- | src/multimedia/windows/qwindowsmediadevices.cpp | 213 | ||||
-rw-r--r-- | src/multimedia/windows/qwindowsmediadevices_p.h | 10 |
2 files changed, 2 insertions, 221 deletions
diff --git a/src/multimedia/windows/qwindowsmediadevices.cpp b/src/multimedia/windows/qwindowsmediadevices.cpp index 464ae1d3d..ab223c0d7 100644 --- a/src/multimedia/windows/qwindowsmediadevices.cpp +++ b/src/multimedia/windows/qwindowsmediadevices.cpp @@ -39,27 +39,18 @@ #include "qwindowsmediadevices_p.h" #include "qmediadevices.h" -#include "private/qcameradevice_p.h" #include "qvarlengtharray.h" #include "qwindowsaudiosource_p.h" #include "qwindowsaudiosink_p.h" #include "qwindowsaudiodevice_p.h" -#include "qwindowsmultimediautils_p.h" - -#include <Dbt.h> -#include <ks.h> #include <mmsystem.h> #include <mmddk.h> -#include <mfapi.h> #include <mfobjects.h> #include <mfidl.h> -#include <mfreadwrite.h> #include <Mferror.h> #include <mmdeviceapi.h> -#include <Functiondiscoverykeys_devpkey.h> -#include <qwindowsaudioutils_p.h> #include <qwindowsmfdefs_p.h> #include <QtCore/qmap.h> @@ -190,54 +181,8 @@ public: } }; -LRESULT QT_WIN_CALLBACK deviceNotificationWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - if (message == WM_DEVICECHANGE) { - auto b = (PDEV_BROADCAST_HDR)lParam; - if (b && b->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { - auto wmd = reinterpret_cast<QWindowsMediaDevices *>(GetWindowLongPtr(hWnd, GWLP_USERDATA)); - - if (wmd) { - if (wParam == DBT_DEVICEARRIVAL) { - wmd->videoInputsChanged(); - } else if (wParam == DBT_DEVICEREMOVECOMPLETE) { - wmd->videoInputsChanged(); - } - } - } - } - - return 1; -} - -static const auto windowClassName = TEXT("QWindowsMediaDevicesMessageWindow"); - -HWND createMessageOnlyWindow() -{ - WNDCLASSEX wx = {}; - wx.cbSize = sizeof(WNDCLASSEX); - wx.lpfnWndProc = deviceNotificationWndProc; - wx.hInstance = GetModuleHandle(nullptr); - wx.lpszClassName = windowClassName; - - if (!RegisterClassEx(&wx)) - return nullptr; - - auto hwnd = CreateWindowEx(0, windowClassName, TEXT("Message"), - 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, nullptr, nullptr); - if (!hwnd) { - UnregisterClass(windowClassName, GetModuleHandle(nullptr)); - return nullptr; - } - - return hwnd; -} - QWindowsMediaDevices::QWindowsMediaDevices() - : QPlatformMediaDevices(), - m_videoDeviceMsgWindow(nullptr), - m_videoDeviceNotification(nullptr) - + : QPlatformMediaDevices() { CoInitialize(nullptr); @@ -275,29 +220,6 @@ QWindowsMediaDevices::QWindowsMediaDevices() } else { qWarning() << "Audio device change notification disabled"; } - - m_videoDeviceMsgWindow = createMessageOnlyWindow(); - if (m_videoDeviceMsgWindow) { - SetWindowLongPtr(m_videoDeviceMsgWindow, GWLP_USERDATA, (LONG_PTR)this); - - DEV_BROADCAST_DEVICEINTERFACE di = {}; - di.dbcc_size = sizeof(di); - di.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; - di.dbcc_classguid = QMM_KSCATEGORY_VIDEO_CAMERA; - - m_videoDeviceNotification = - RegisterDeviceNotification(m_videoDeviceMsgWindow, &di, DEVICE_NOTIFY_WINDOW_HANDLE); - if (!m_videoDeviceNotification) { - DestroyWindow(m_videoDeviceMsgWindow); - m_videoDeviceMsgWindow = nullptr; - - UnregisterClass(windowClassName, GetModuleHandle(nullptr)); - } - } - - if (!m_videoDeviceNotification) { - qWarning() << "Video device change notification disabled"; - } } QWindowsMediaDevices::~QWindowsMediaDevices() @@ -309,15 +231,6 @@ QWindowsMediaDevices::~QWindowsMediaDevices() m_deviceEnumerator.reset(); m_notificationClient.reset(); - if (m_videoDeviceNotification) { - UnregisterDeviceNotification(m_videoDeviceNotification); - } - - if (m_videoDeviceMsgWindow) { - DestroyWindow(m_videoDeviceMsgWindow); - UnregisterClass(windowClassName, GetModuleHandle(nullptr)); - } - CoUninitialize(); } @@ -393,130 +306,6 @@ QList<QAudioDevice> QWindowsMediaDevices::audioOutputs() const return availableDevices(QAudioDevice::Output); } -QList<QCameraDevice> QWindowsMediaDevices::videoInputs() const -{ - QList<QCameraDevice> cameras; - - IMFAttributes *pAttributes = NULL; - IMFActivate **ppDevices = NULL; - - // Create an attribute store to specify the enumeration parameters. - HRESULT hr = MFCreateAttributes(&pAttributes, 1); - if (SUCCEEDED(hr)) { - // Source type: video capture devices - hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, - MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); - - if (SUCCEEDED(hr)) { - // Enumerate devices. - UINT32 count; - hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count); - if (SUCCEEDED(hr)) { - // Iterate through devices. - for (int index = 0; index < int(count); index++) { - QCameraDevicePrivate *info = new QCameraDevicePrivate; - - IMFMediaSource *pSource = NULL; - IMFSourceReader *reader = NULL; - - WCHAR *deviceName = NULL; - UINT32 deviceNameLength = 0; - UINT32 deviceIdLength = 0; - WCHAR *deviceId = NULL; - - hr = ppDevices[index]->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, - &deviceName, &deviceNameLength); - if (SUCCEEDED(hr)) - info->description = QString::fromWCharArray(deviceName); - CoTaskMemFree(deviceName); - - hr = ppDevices[index]->GetAllocatedString( - MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &deviceId, - &deviceIdLength); - if (SUCCEEDED(hr)) - info->id = QString::fromWCharArray(deviceId).toUtf8(); - CoTaskMemFree(deviceId); - - // Create the media source object. - hr = ppDevices[index]->ActivateObject( - IID_PPV_ARGS(&pSource)); - // Create the media source reader. - hr = MFCreateSourceReaderFromMediaSource(pSource, NULL, &reader); - if (SUCCEEDED(hr)) { - QList<QSize> photoResolutions; - QList<QCameraFormat> videoFormats; - - DWORD dwMediaTypeIndex = 0; - IMFMediaType *mediaFormat = NULL; - GUID subtype = GUID_NULL; - HRESULT mediaFormatResult = S_OK; - - UINT32 frameRateMin = 0u; - UINT32 frameRateMax = 0u; - UINT32 denominator = 0u; - UINT32 width = 0u; - UINT32 height = 0u; - - while (SUCCEEDED(mediaFormatResult)) { - // Loop through the supported formats for the video device - mediaFormatResult = reader->GetNativeMediaType( - (DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, dwMediaTypeIndex, - &mediaFormat); - if (mediaFormatResult == MF_E_NO_MORE_TYPES) - break; - else if (SUCCEEDED(mediaFormatResult)) { - QVideoFrameFormat::PixelFormat pixelFormat = QVideoFrameFormat::Format_Invalid; - QSize resolution; - float minFr = .0; - float maxFr = .0; - - if (SUCCEEDED(mediaFormat->GetGUID(MF_MT_SUBTYPE, &subtype))) - pixelFormat = QWindowsMultimediaUtils::pixelFormatFromMediaSubtype(subtype); - - if (SUCCEEDED(MFGetAttributeSize(mediaFormat, MF_MT_FRAME_SIZE, &width, - &height))) { - resolution.rheight() = (int)height; - resolution.rwidth() = (int)width; - photoResolutions << resolution; - } - - if (SUCCEEDED(MFGetAttributeRatio(mediaFormat, MF_MT_FRAME_RATE_RANGE_MIN, - &frameRateMin, &denominator))) - minFr = qreal(frameRateMin) / denominator; - if (SUCCEEDED(MFGetAttributeRatio(mediaFormat, MF_MT_FRAME_RATE_RANGE_MAX, - &frameRateMax, &denominator))) - maxFr = qreal(frameRateMax) / denominator; - - auto *f = new QCameraFormatPrivate { QSharedData(), pixelFormat, - resolution, minFr, maxFr }; - videoFormats << f->create(); - } - ++dwMediaTypeIndex; - } - if (mediaFormat) - mediaFormat->Release(); - - info->videoFormats = videoFormats; - info->photoResolutions = photoResolutions; - } - if (reader) - reader->Release(); - cameras.append(info->create()); - } - } - for (DWORD i = 0; i < count; i++) { - if (ppDevices[i]) - ppDevices[i]->Release(); - } - CoTaskMemFree(ppDevices); - } - } - if (pAttributes) - pAttributes->Release(); - - return cameras; -} - QPlatformAudioSource *QWindowsMediaDevices::createAudioSource(const QAudioDevice &deviceInfo) { const auto *devInfo = static_cast<const QWindowsAudioDeviceInfo *>(deviceInfo.handle()); diff --git a/src/multimedia/windows/qwindowsmediadevices_p.h b/src/multimedia/windows/qwindowsmediadevices_p.h index a1a377309..da5aaf18c 100644 --- a/src/multimedia/windows/qwindowsmediadevices_p.h +++ b/src/multimedia/windows/qwindowsmediadevices_p.h @@ -53,10 +53,8 @@ #include <private/qplatformmediadevices_p.h> #include <private/qwindowsiupointer_p.h> -#include <qset.h> -#include <qaudio.h> + #include <qaudiodevice.h> -#include <QtCore/qt_windows.h> struct IMMDeviceEnumerator; @@ -65,8 +63,6 @@ QT_BEGIN_NAMESPACE class QWindowsEngine; class CMMNotificationClient; -LRESULT QT_WIN_CALLBACK deviceNotificationWndProc(HWND, UINT, WPARAM, LPARAM); - class QWindowsMediaDevices : public QPlatformMediaDevices { public: @@ -75,7 +71,6 @@ public: QList<QAudioDevice> audioInputs() const override; QList<QAudioDevice> audioOutputs() const override; - QList<QCameraDevice> videoInputs() const override; QPlatformAudioSource *createAudioSource(const QAudioDevice &deviceInfo) override; QPlatformAudioSink *createAudioSink(const QAudioDevice &deviceInfo) override; @@ -84,11 +79,8 @@ private: QWindowsIUPointer<IMMDeviceEnumerator> m_deviceEnumerator; QWindowsIUPointer<CMMNotificationClient> m_notificationClient; - HWND m_videoDeviceMsgWindow; - HDEVNOTIFY m_videoDeviceNotification; friend CMMNotificationClient; - friend LRESULT QT_WIN_CALLBACK deviceNotificationWndProc(HWND, UINT, WPARAM, LPARAM); }; QT_END_NAMESPACE |