Add UWP device finder
This commit is contained in:
parent
f0df9dc0fb
commit
e466cb6e30
@ -838,6 +838,7 @@ optional_source(WIN32
|
||||
HEADERS
|
||||
core/windows7thumbbar.h
|
||||
)
|
||||
optional_source(MSVC SOURCES engine/uwpdevicefinder.cpp)
|
||||
|
||||
optional_source(HAVE_SUBSONIC
|
||||
SOURCES
|
||||
@ -1198,6 +1199,9 @@ endif()
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(strawberry_lib PRIVATE dsound dwmapi)
|
||||
if(MSVC)
|
||||
target_link_libraries(strawberry_lib PRIVATE WindowsApp)
|
||||
endif()
|
||||
if(GETOPT_INCLUDE_DIRS)
|
||||
target_include_directories(strawberry_lib SYSTEM PRIVATE ${GETOPT_INCLUDE_DIRS})
|
||||
endif()
|
||||
|
85
src/engine/AsyncOperations.h
Normal file
85
src/engine/AsyncOperations.h
Normal file
@ -0,0 +1,85 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See LICENSE in the project root for license information.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
#include <wrl.h>
|
||||
#include <windows.foundation.h>
|
||||
|
||||
// eg. TOperation = IAsyncOperationWithProgress<UINT32, UINT32>
|
||||
// eg. THandler = IAsyncOperationWithProgressCompletedHandler<UINT, UINT>
|
||||
template<typename TOperation, typename THandler>
|
||||
class AsyncEventDelegate
|
||||
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::Delegate>, THandler, Microsoft::WRL::FtmBase> {
|
||||
public:
|
||||
AsyncEventDelegate()
|
||||
: _completedEvent(CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS)) {
|
||||
Microsoft::WRL::ComPtr<AsyncEventDelegate> spThis(this);
|
||||
auto lambda = ([this, spThis](_In_ HRESULT hr, _In_ TOperation *pOperation) {
|
||||
SetEvent(_completedEvent.Get());
|
||||
});
|
||||
_func = std::move(lambda);
|
||||
}
|
||||
|
||||
STDMETHOD(Invoke)
|
||||
(
|
||||
_In_ TOperation *pOperation,
|
||||
_In_ AsyncStatus status) {
|
||||
HRESULT hr = S_OK;
|
||||
|
||||
// if we completed successfully, then there is no need for getting hresult
|
||||
if (status != AsyncStatus::Completed) {
|
||||
Microsoft::WRL::ComPtr<TOperation> spOperation(pOperation);
|
||||
Microsoft::WRL::ComPtr<IAsyncInfo> spAsyncInfo;
|
||||
if (SUCCEEDED(spOperation.As(&spAsyncInfo))) {
|
||||
spAsyncInfo->get_ErrorCode(&hr);
|
||||
}
|
||||
}
|
||||
|
||||
_func(hr, pOperation);
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHOD(SyncWait)
|
||||
(_In_ TOperation *pOperation, _In_ DWORD dwMilliseconds) {
|
||||
HRESULT hr = pOperation->put_Completed(this);
|
||||
if (FAILED(hr)) {
|
||||
return hr;
|
||||
}
|
||||
|
||||
DWORD dwWait = WaitForSingleObjectEx(_completedEvent.Get(), dwMilliseconds, TRUE);
|
||||
if (WAIT_IO_COMPLETION == dwWait || WAIT_OBJECT_0 == dwWait)
|
||||
return S_OK;
|
||||
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<void(HRESULT, TOperation *)> _func;
|
||||
Microsoft::WRL::Wrappers::Event _completedEvent;
|
||||
};
|
||||
|
||||
template<typename TOperation, typename THandler>
|
||||
HRESULT SyncWait(_In_ TOperation *pOperation, _In_ DWORD dwMilliseconds) {
|
||||
auto spCallback = Microsoft::WRL::Make<AsyncEventDelegate<TOperation, THandler>>();
|
||||
|
||||
return spCallback->SyncWait(pOperation, dwMilliseconds);
|
||||
}
|
||||
|
||||
template<typename TResult>
|
||||
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncAction *pOperation, _In_ DWORD dwMilliseconds = INFINITE) {
|
||||
return SyncWait<ABI::Windows::Foundation::IAsyncAction, ABI::Windows::Foundation::IAsyncActionCompletedHandler>(pOperation, dwMilliseconds);
|
||||
}
|
||||
|
||||
template<typename TResult>
|
||||
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult> *pOperation, _In_ DWORD dwMilliseconds = INFINITE) {
|
||||
return SyncWait<ABI::Windows::Foundation::IAsyncOperation<TResult>, ABI::Windows::Foundation::IAsyncOperationCompletedHandler<TResult>>(pOperation, dwMilliseconds);
|
||||
}
|
||||
|
||||
template<typename TResult, typename TProgress>
|
||||
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress> *pOperation, _In_ DWORD dwMilliseconds = INFINITE) {
|
||||
return SyncWait<ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>, ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>>(pOperation, dwMilliseconds);
|
||||
}
|
@ -43,7 +43,10 @@
|
||||
#ifdef Q_OS_WIN32
|
||||
# include "directsounddevicefinder.h"
|
||||
# include "mmdevicefinder.h"
|
||||
#endif
|
||||
# ifdef _MSC_VER
|
||||
# include "uwpdevicefinder.h"
|
||||
# endif // _MSC_VER
|
||||
#endif // Q_OS_WIN32
|
||||
|
||||
DeviceFinders::DeviceFinders(QObject *parent) : QObject(parent) {}
|
||||
|
||||
@ -68,7 +71,10 @@ void DeviceFinders::Init() {
|
||||
#ifdef Q_OS_WIN32
|
||||
device_finders.append(new DirectSoundDeviceFinder);
|
||||
device_finders.append(new MMDeviceFinder);
|
||||
#endif
|
||||
# ifdef _MSC_VER
|
||||
device_finders.append(new UWPDeviceFinder);
|
||||
# endif // _MSC_VER
|
||||
#endif // Q_OS_WIN32
|
||||
|
||||
for (DeviceFinder *finder : device_finders) {
|
||||
if (!finder->Initialize()) {
|
||||
|
150
src/engine/uwpdevicefinder.cpp
Normal file
150
src/engine/uwpdevicefinder.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
#include <wrl.h>
|
||||
#include <windows.foundation.h>
|
||||
#include <windows.media.audio.h>
|
||||
|
||||
#include "AsyncOperations.h"
|
||||
|
||||
#include "uwpdevicefinder.h"
|
||||
#include "core/logging.h"
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
using namespace Microsoft::WRL::Wrappers;
|
||||
using namespace ABI::Windows::Foundation;
|
||||
using namespace ABI::Windows::Foundation::Collections;
|
||||
using namespace ABI::Windows::Devices::Enumeration;
|
||||
|
||||
UWPDeviceFinder::UWPDeviceFinder() : DeviceFinder("uwpdevice", { "wasapi2sink" }) {}
|
||||
|
||||
namespace {
|
||||
|
||||
static std::string wstring_to_stdstring(const std::wstring &wstr) {
|
||||
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
|
||||
|
||||
return converter.to_bytes(wstr.c_str());
|
||||
|
||||
}
|
||||
|
||||
static std::string hstring_to_stdstring(HString *hstr) {
|
||||
|
||||
if (!hstr) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
const wchar_t *raw_hstr = hstr->GetRawBuffer(nullptr);
|
||||
if (!raw_hstr) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
return wstring_to_stdstring(std::wstring(raw_hstr));
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QList<DeviceFinder::Device> UWPDeviceFinder::ListDevices() {
|
||||
|
||||
ComPtr<IDeviceInformationStatics> device_info_statics;
|
||||
HRESULT hr = ABI::Windows::Foundation::GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Enumeration_DeviceInformation).Get(), &device_info_statics);
|
||||
if (FAILED(hr)) {
|
||||
return QList<Device>();
|
||||
}
|
||||
|
||||
ComPtr<IAsyncOperation<DeviceInformationCollection*>> async_op;
|
||||
hr = device_info_statics->FindAllAsyncDeviceClass(DeviceClass::DeviceClass_AudioRender, &async_op);
|
||||
device_info_statics.Reset();
|
||||
if (FAILED(hr)) {
|
||||
return QList<Device>();
|
||||
}
|
||||
|
||||
hr = SyncWait<DeviceInformationCollection*>(async_op.Get());
|
||||
if (FAILED(hr)) {
|
||||
return QList<Device>();
|
||||
}
|
||||
|
||||
ComPtr<IVectorView<DeviceInformation*>> device_list;
|
||||
hr = async_op->GetResults(&device_list);
|
||||
async_op.Reset();
|
||||
if (FAILED(hr)) {
|
||||
return QList<Device>();
|
||||
}
|
||||
|
||||
unsigned int count = 0;
|
||||
hr = device_list->get_Size(&count);
|
||||
if (FAILED(hr)) {
|
||||
return QList<Device>();
|
||||
}
|
||||
|
||||
QList<Device> devices;
|
||||
|
||||
{
|
||||
Device default_device;
|
||||
default_device.description = "Default device";
|
||||
default_device.iconname = GuessIconName(default_device.description);
|
||||
devices.append(default_device);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
|
||||
ComPtr<IDeviceInformation> device_info;
|
||||
hr = device_list->GetAt(i, &device_info);
|
||||
if (FAILED(hr)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean enabled;
|
||||
hr = device_info->get_IsEnabled(&enabled);
|
||||
if (FAILED(hr) || !enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
HString id;
|
||||
hr = device_info->get_Id(id.GetAddressOf());
|
||||
if (FAILED(hr) || !id.IsValid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
HString name;
|
||||
hr = device_info->get_Name(name.GetAddressOf());
|
||||
if (FAILED(hr) || !name.IsValid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Device device;
|
||||
device.value = QString::fromStdString(hstring_to_stdstring(&id));
|
||||
device.description = QString::fromStdString(hstring_to_stdstring(&name));
|
||||
device.iconname = GuessIconName(device.description);
|
||||
devices.append(device);
|
||||
}
|
||||
|
||||
return devices;
|
||||
|
||||
}
|
35
src/engine/uwpdevicefinder.h
Normal file
35
src/engine/uwpdevicefinder.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UWPDEVICEFINDER_H
|
||||
#define UWPDEVICEFINDER_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "devicefinder.h"
|
||||
|
||||
class UWPDeviceFinder : public DeviceFinder {
|
||||
public:
|
||||
explicit UWPDeviceFinder();
|
||||
|
||||
virtual bool Initialize() { return true; }
|
||||
virtual QList<Device> ListDevices();
|
||||
};
|
||||
|
||||
#endif // UWPDEVICEFINDER_H
|
Loading…
x
Reference in New Issue
Block a user