简体   繁体   中英

windows core audio api IAudioSessionControl2::GetProcessId returns unreferenced error code

I'm trying to develop a sound mixer app with Windows core audio api. When running the test code I ran into errors when calling IAudioSessionControl2::GetProcessId

test code:

#include <iostream>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <endpointvolume.h>

#define CHECK_RES(res, retCode) if(res != S_OK){CoUninitialize();std::cerr << "error code: " << res << std::endl;return retCode;}
#define SAFE_RELEASE(ptr) if(ptr!=NULL){ptr->Release();ptr=NULL;}

#define CODE(code) std::cout << #code << ": " << code << std::endl;

int main()
{

    HRESULT res = CoInitialize(NULL);
    CHECK_RES(res, -1);

    IMMDeviceEnumerator* pDeviceEnumerator = NULL;
    res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID*)&pDeviceEnumerator);
    CHECK_RES(res, -2);

    EDataFlow dataFlow = EDataFlow::eRender;
    ERole role = ERole::eMultimedia;
    IMMDevice* pDevice = NULL;
    res = pDeviceEnumerator->GetDefaultAudioEndpoint(dataFlow, role, &pDevice);
    CHECK_RES(res, -3);

    IAudioSessionManager2* pSessionManager = NULL;
    res = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (LPVOID*)&pSessionManager);
    CHECK_RES(res, -4);

    IAudioSessionEnumerator* pAudioSessionsEnumerator = NULL;
    res = pSessionManager->GetSessionEnumerator(&pAudioSessionsEnumerator);
    CHECK_RES(res, -5);

    int size = 0;
    res = pAudioSessionsEnumerator->GetCount(&size);
    CHECK_RES(res, -6);
    IAudioSessionControl2* pSessionControl = NULL;
    for (int i = 0; i < size; i++) {
        LPWSTR name = 0;
        res = pAudioSessionsEnumerator->GetSession(i, (IAudioSessionControl**)&pSessionControl);
        CHECK_RES(res, -7);

        // here res is 0, but name is 0 too 
        res = pSessionControl->GetSessionInstanceIdentifier(&name);
        std::cout << "res for name operation is : " << res << ", name: " << name << std::endl;

        // if instead of pSessionControl->GetSessionInstanceIdentifier I run 
        // pSessionControl->GetSessionIdentifier, above res code is -2147467262
        // and below res code goes -2

        DWORD id = NULL;
        // here I get res = -1 when I run this method
        // res can be -2 if GetSessionIdentifier is ran
        res = pSessionControl->GetProcessId(&id);
        std::cout << "res for id operation is : " << res << ", id: " << id << std::endl;

        SAFE_RELEASE(pSessionControl);


        CoTaskMemFree(name);
    }

    // tried to compare to know error codes but none matched
    CODE(AUDCLNT_E_DEVICE_INVALIDATED);
    CODE(AUDCLNT_E_RESOURCES_INVALIDATED);

    SAFE_RELEASE(pDeviceEnumerator);
    SAFE_RELEASE(pDevice);
    SAFE_RELEASE(pAudioSessionsEnumerator);
    SAFE_RELEASE(pSessionManager);

    CoUninitialize();
    return 0;

}

However, if there was any other solution to retrieve the process linked to the IAudioSessionControl, I would be pleased to hear it.

I'm trying to develop a sound mixer app with Windows core audio api. When running the test code I ran into errors when calling IAudioSessionControl2::GetProcessId

test code:

#include <iostream>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <endpointvolume.h>

#define CHECK_RES(res, retCode) if(res != S_OK){CoUninitialize();std::cerr << "error code: " << res << std::endl;return retCode;}
#define SAFE_RELEASE(ptr) if(ptr!=NULL){ptr->Release();ptr=NULL;}

#define CODE(code) std::cout << #code << ": " << code << std::endl;

int main()
{

    HRESULT res = CoInitialize(NULL);
    CHECK_RES(res, -1);

    IMMDeviceEnumerator* pDeviceEnumerator = NULL;
    res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID*)&pDeviceEnumerator);
    CHECK_RES(res, -2);

    EDataFlow dataFlow = EDataFlow::eRender;
    ERole role = ERole::eMultimedia;
    IMMDevice* pDevice = NULL;
    res = pDeviceEnumerator->GetDefaultAudioEndpoint(dataFlow, role, &pDevice);
    CHECK_RES(res, -3);

    IAudioSessionManager2* pSessionManager = NULL;
    res = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (LPVOID*)&pSessionManager);
    CHECK_RES(res, -4);

    IAudioSessionEnumerator* pAudioSessionsEnumerator = NULL;
    res = pSessionManager->GetSessionEnumerator(&pAudioSessionsEnumerator);
    CHECK_RES(res, -5);

    int size = 0;
    res = pAudioSessionsEnumerator->GetCount(&size);
    CHECK_RES(res, -6);
    IAudioSessionControl2* pSessionControl = NULL;
    for (int i = 0; i < size; i++) {
        LPWSTR name = 0;
        res = pAudioSessionsEnumerator->GetSession(i, (IAudioSessionControl**)&pSessionControl);
        CHECK_RES(res, -7);

        // here res is 0, but name is 0 too 
        res = pSessionControl->GetSessionInstanceIdentifier(&name);
        std::cout << "res for name operation is : " << res << ", name: " << name << std::endl;

        // if instead of pSessionControl->GetSessionInstanceIdentifier I run 
        // pSessionControl->GetSessionIdentifier, above res code is -2147467262
        // and below res code goes -2

        DWORD id = NULL;
        // here I get res = -1 when I run this method
        // res can be -2 if GetSessionIdentifier is ran
        res = pSessionControl->GetProcessId(&id);
        std::cout << "res for id operation is : " << res << ", id: " << id << std::endl;

        SAFE_RELEASE(pSessionControl);


        CoTaskMemFree(name);
    }

    // tried to compare to know error codes but none matched
    CODE(AUDCLNT_E_DEVICE_INVALIDATED);
    CODE(AUDCLNT_E_RESOURCES_INVALIDATED);

    SAFE_RELEASE(pDeviceEnumerator);
    SAFE_RELEASE(pDevice);
    SAFE_RELEASE(pAudioSessionsEnumerator);
    SAFE_RELEASE(pSessionManager);

    CoUninitialize();
    return 0;

}

However, if there was any other solution to retrieve the process linked to the IAudioSessionControl, I would be pleased to hear it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM