From d3d06d33bc8513ba5add788ea17eb4d609061a4e Mon Sep 17 00:00:00 2001 From: Fela Ameghino Date: Thu, 11 May 2017 15:33:39 +0200 Subject: [PATCH] Fixed the audio ActivationHandler to work properly from system VoIP background task --- os/windows/WindowsSandboxUtils.cpp | 69 +++++++++++------------------- os/windows/WindowsSandboxUtils.h | 33 +++++++------- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/os/windows/WindowsSandboxUtils.cpp b/os/windows/WindowsSandboxUtils.cpp index d16b82d..c4f94a5 100644 --- a/os/windows/WindowsSandboxUtils.cpp +++ b/os/windows/WindowsSandboxUtils.cpp @@ -12,57 +12,40 @@ using namespace tgvoip; using namespace Microsoft::WRL; -IAudioClient* WindowsSandboxUtils::ActivateAudioDevice(const wchar_t* devID, HRESULT* callRes, HRESULT* actRes){ +IAudioClient* WindowsSandboxUtils::ActivateAudioDevice(const wchar_t* devID, HRESULT* callRes, HRESULT* actRes) { // Did I say that I hate pointlessly asynchronous things? - HANDLE event = CreateEventEx(NULL, NULL, 0, 0); + HANDLE event = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS); ActivationHandler activationHandler(event); - Windows::ApplicationModel::Core::CoreApplication::GetCurrentView()->CoreWindow->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([devID, callRes, actRes, activationHandler](){ - ComPtr actHandler; - HRESULT cr=ActivateAudioInterfaceAsync(devID, __uuidof(IAudioClient), NULL, (IActivateAudioInterfaceCompletionHandler*)&activationHandler, &actHandler); - if(callRes) - *callRes=cr; - })); - WaitForSingleObjectEx(event, INFINITE, false); + IActivateAudioInterfaceAsyncOperation* actHandler; + HRESULT cr = ActivateAudioInterfaceAsync(devID, __uuidof(IAudioClient2), NULL, (IActivateAudioInterfaceCompletionHandler*)&activationHandler, &actHandler); + if (callRes) + *callRes = cr; + DWORD resulttt = WaitForSingleObjectEx(event, INFINITE, false); + DWORD last = GetLastError(); CloseHandle(event); - if(actRes) - *actRes=activationHandler.actResult; + if (actRes) + *actRes = activationHandler.actResult; return activationHandler.client; } -ActivationHandler::ActivationHandler(HANDLE _event) : event(_event), refCount(1){ +ActivationHandler::ActivationHandler(HANDLE _event) : event(_event) +{ } -STDMETHODIMP ActivationHandler::ActivateCompleted(IActivateAudioInterfaceAsyncOperation* op){ - HRESULT res = op->GetActivateResult(&actResult, (IUnknown**)&client); +STDMETHODIMP ActivationHandler::ActivateCompleted(IActivateAudioInterfaceAsyncOperation * operation) +{ + HRESULT hr = S_OK; + HRESULT hrActivateResult = S_OK; + IUnknown *punkAudioInterface = nullptr; + + hr = operation->GetActivateResult(&hrActivateResult, &punkAudioInterface); + if (SUCCEEDED(hr) && SUCCEEDED(hrActivateResult)) + { + punkAudioInterface->QueryInterface(IID_PPV_ARGS(&client)); + } + SetEvent(event); - return S_OK; -} - -HRESULT ActivationHandler::QueryInterface(REFIID iid, void** obj){ - if(!obj){ - return E_POINTER; - } - *obj=NULL; - - if(iid==IID_IUnknown){ - *obj=static_cast(this); - AddRef(); - }else if(iid==__uuidof(IActivateAudioInterfaceCompletionHandler)){ - *obj=static_cast(this); - AddRef(); - }else{ - return E_NOINTERFACE; - } - - return S_OK; -} - -ULONG ActivationHandler::AddRef(){ - return InterlockedIncrement(&refCount); -} - -ULONG ActivationHandler::Release(){ - return InterlockedDecrement(&refCount); -} + return hr; +} \ No newline at end of file diff --git a/os/windows/WindowsSandboxUtils.h b/os/windows/WindowsSandboxUtils.h index 85ac945..93926eb 100644 --- a/os/windows/WindowsSandboxUtils.h +++ b/os/windows/WindowsSandboxUtils.h @@ -13,25 +13,22 @@ using namespace Microsoft::WRL; -namespace tgvoip{ +namespace tgvoip { -class ActivationHandler : public IActivateAudioInterfaceCompletionHandler{ -public: - ActivationHandler(HANDLE _event); - STDMETHOD(ActivateCompleted)(IActivateAudioInterfaceAsyncOperation* op); - STDMETHOD(QueryInterface)(REFIID iid, void **pvObject); - STDMETHOD_(ULONG, AddRef)(); - STDMETHOD_(ULONG, Release)(); - HANDLE event; - IAudioClient* client; - HRESULT actResult; -private: - ULONG refCount; -}; + class ActivationHandler : + public RuntimeClass< RuntimeClassFlags< ClassicCom >, FtmBase, IActivateAudioInterfaceCompletionHandler > + { + public: + STDMETHOD(ActivateCompleted)(IActivateAudioInterfaceAsyncOperation *operation); + ActivationHandler(HANDLE _event); + HANDLE event; + IAudioClient* client; + HRESULT actResult; + }; -class WindowsSandboxUtils{ -public: - static IAudioClient* ActivateAudioDevice(const wchar_t* devID, HRESULT* callResult, HRESULT* actResult); -}; + class WindowsSandboxUtils { + public: + static IAudioClient* ActivateAudioDevice(const wchar_t* devID, HRESULT* callResult, HRESULT* actResult); + }; }