Ejemplo n.º 1
1
std::vector<ProcessVolume> GetProcessVolumes() {
  std::vector<ProcessVolume> volumes;

  CoInitialize(NULL);

  IAudioSessionEnumerator* enumerator = GetAudioSessionEnumerator();
  int sessionCount;
  enumerator->GetCount(&sessionCount);
  for (int index = 0; index < sessionCount; index++) {
    IAudioSessionControl* session = nullptr;
    IAudioSessionControl2* session2 = nullptr;
    enumerator->GetSession(index, &session);
    session->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&session2);

    DWORD id = 0;
    session2->GetProcessId(&id);

    std::string processName = GetProcessNameFromPid(id);
    float peakVolume = GetPeakVolumeFromAudioSessionControl(session);

    ProcessVolume data;
    data.processName = processName;
    data.processId = id;
    data.peakVolume = peakVolume;
    volumes.push_back(data);

    session2->Release();
    session->Release();
  }
  enumerator->Release();

  return std::move(volumes);
}
Ejemplo n.º 2
0
//
//  Utility function to retrieve the session control interface for the current audio session.
//
//  We assume that DirectShow uses the NULL session GUID and doesn't specify any session specific flags.
//
HRESULT CMediaPlayer::GetSessionControl2()
{
    HRESULT hr = S_OK;
    if (_SessionControl2 == NULL)
    {
        hr = GetSessionManager2();
        if (SUCCEEDED(hr))
        {
            IAudioSessionControl * sessionControl;
            hr = _SessionManager2->GetAudioSessionControl(NULL, 0, &sessionControl);
            if (SUCCEEDED(hr))
            {
                hr = sessionControl->QueryInterface(IID_PPV_ARGS(&_SessionControl2));
                if (FAILED(hr))
                {
                    MessageBox(_AppWindow, L"Unable to QI for SessionControl2", L"Get SessionControl Error", MB_OK);
                }

                sessionControl->Release();
                sessionControl = NULL;
            }
            else
            {
                MessageBox(_AppWindow, L"Unable to get Session Control", L"Get SessionControl Error", MB_OK);
            }
        }
    }
    return hr;
}
	static HRESULT EnableDuckingOptOut(IMMDevice* pEndpoint, bool bDuckingOptOutChecked)
	{
		HRESULT hr = S_OK;

		// Activate session manager.
		IAudioSessionManager2* pSessionManager2 = NULL;
		if (SUCCEEDED(hr))
		{
			hr = pEndpoint->Activate(__uuidof(IAudioSessionManager2),
				CLSCTX_INPROC_SERVER,
				NULL,
				reinterpret_cast<void **>(&pSessionManager2));
		}

		IAudioSessionControl* pSessionControl = NULL;
		if (SUCCEEDED(hr))
		{
			hr = pSessionManager2->GetAudioSessionControl(NULL, 0, &pSessionControl);

			pSessionManager2->Release();
			pSessionManager2 = NULL;
		}

		IAudioSessionControl2* pSessionControl2 = NULL;
		if (SUCCEEDED(hr))
		{
			hr = pSessionControl->QueryInterface(
				__uuidof(IAudioSessionControl2),
				(void**)&pSessionControl2);

			pSessionControl->Release();
			pSessionControl = NULL;
		}

		//  Sync the ducking state with the specified preference.
		if (SUCCEEDED(hr))
		{
			hr = pSessionControl2->SetDuckingPreference((::BOOL)bDuckingOptOutChecked);
			if (FAILED(hr))
			{
				UE_LOG(LogVoiceCapture, Display, TEXT("Failed to duck audio endpoint. Error: %0x08x"), hr);
			}

			pSessionControl2->Release();
			pSessionControl2 = NULL;
		}
		return hr;
	}
Ejemplo n.º 4
0
void changeFGWindowVolume()
{
	if (soundOption == dncItemID) return;

	char titleBuff[128];
	HWND activeHWND = GetForegroundWindow();
	GetWindowText(activeHWND, titleBuff, 128);

	UINT count = sizeof(mediaCommands) / sizeof(mediaCommands[0]);
	for (UINT i = 0; i < count; i++)
	{
		if (strstr(titleBuff, mediaCommands[i].title) > 0) return;
	}

	IMMDevice *mmDevice;
	IMMDeviceEnumerator *mmDeviceEnum;
	IAudioSessionManager2 *sessionManager;
	IAudioSessionEnumerator *sessionEnum;
	IAudioSessionControl *sessionControl;
	IAudioSessionControl2 *sessionControl2;
	ISimpleAudioVolume *audioVolume;

	CoCreateInstance(__uuidof(MMDeviceEnumerator), 0, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&mmDeviceEnum);
	mmDeviceEnum->GetDefaultAudioEndpoint(eRender, eMultimedia, &mmDevice);
	mmDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, 0, (void**)&sessionManager);
	sessionManager->GetSessionEnumerator(&sessionEnum);

	DWORD activePid;
	GetWindowThreadProcessId(activeHWND, &activePid);

	int sessionCount;
	sessionEnum->GetCount(&sessionCount);
	for (int i = 0; i < sessionCount; i++)
	{
		sessionEnum->GetSession(i, &sessionControl);
		sessionControl->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&sessionControl2);

		DWORD pid;
		sessionControl2->GetProcessId(&pid);
		if (activePid == pid)
		{
			sessionControl->QueryInterface(__uuidof(ISimpleAudioVolume), (void**)&audioVolume);

			BOOL muted;
			audioVolume->GetMute(&muted);

			float volumeLevel;
			audioVolume->GetMasterVolume(&volumeLevel);

			if (soundOption == muteItemID)
			{
				audioVolume->SetMute(!muted, 0);

				if (volumeLevel != 1.0f)
				{
					audioVolume->SetMasterVolume(1.0f, 0);
				}
			}
			else
			{
				float newVolumeLevel = (soundOption - sBaseItemID) / 100.0f;
				audioVolume->SetMasterVolume(volumeLevel == 1.0f ? newVolumeLevel : 1.0f, 0);

				if (muted)
				{
					audioVolume->SetMute(false, 0);
				}
			}

			audioVolume->Release();
		}

		sessionControl->Release();
		sessionControl2->Release();
	}

	sessionEnum->Release();
	sessionManager->Release();
	mmDevice->Release();
	mmDeviceEnum->Release();
}