c++winapimixer

How to get audio session GUID with Windows API


I'm trying to make a simple mixer app using Windows audio API. I've already figured out how to get IAudioSessionEnumerator and IAudioSessionControl which allows me to retrieve the DisplayName of each session. And now I want to get/set volumes of these sessions. From winApi docs I think I first need to use GetSimpleAudioVolume method but it requires session GUID as a parameter. So how do I get a GUID of an existing session? I couldn't find any answers for that in docs or google. Or maybe I missunderstood something?

//code prints DisplayNames of all sessions
void getSessions() {

    CoInitialize(NULL);

    IMMDeviceEnumerator *pDEnumerator = NULL;
    CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pDEnumerator);

    IMMDevice *pDevice = NULL;
    pDEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);

    IAudioSessionManager2 *pSManager2 = NULL;
    pDevice->Activate(IID_IAudioSessionManager2, CLSCTX_ALL, NULL, (void**)&pSManager2);

    IAudioSessionEnumerator *pSEnumerator = NULL;
    pSManager2->GetSessionEnumerator(&pSEnumerator);

    int audioSessionCount;
    pSEnumerator->GetCount(&audioSessionCount);

    std::cout << audioSessionCount << '\n';

    for (int i = 0; i < audioSessionCount; ++i) {
        IAudioSessionControl *controls;
        pSEnumerator->GetSession(i, &controls);
        LPWSTR name;
        controls->GetDisplayName(&name);
        while (*name != 0) {
            std::wcout << *name;
            ++name;
        }
        std::cout << '\n';
    }
}

Solution

  • Each audio session is uniquely identified with a GUID—session instance identifier.

    You can IAudioSessionControl2::GetSessionInstanceIdentifier and retrieve a string that contains the session identifier.

    Or you can query ISimpleAudioVolume interface on IAudioSessionControl interface like this:

    ISimpleAudioVolume *simpleAudioVol = NULL;
    controls->QueryInterface(IID_PPV_ARGS(&simpleAudioVol));