diff --git a/modules/audio_device/win/audio_device_core_win.cc b/modules/audio_device/win/audio_device_core_win.cc index fee76426cb..6e82f3d5df 100644 --- a/modules/audio_device/win/audio_device_core_win.cc +++ b/modules/audio_device/win/audio_device_core_win.cc @@ -790,6 +790,7 @@ int32_t AudioDeviceWindowsCore::SpeakerVolumeIsAvailable(bool& available) { HRESULT hr = S_OK; IAudioSessionManager* pManager = NULL; ISimpleAudioVolume* pVolume = NULL; + float volume = 0.0f; hr = _ptrDeviceOut->Activate(__uuidof(IAudioSessionManager), CLSCTX_ALL, NULL, (void**)&pManager); @@ -798,7 +799,6 @@ int32_t AudioDeviceWindowsCore::SpeakerVolumeIsAvailable(bool& available) { hr = pManager->GetSimpleAudioVolume(NULL, FALSE, &pVolume); EXIT_ON_ERROR(hr); - float volume(0.0f); hr = pVolume->GetMasterVolume(&volume); if (FAILED(hr)) { available = false; @@ -976,13 +976,13 @@ int32_t AudioDeviceWindowsCore::SetSpeakerMute(bool enable) { HRESULT hr = S_OK; IAudioEndpointVolume* pVolume = NULL; + const BOOL mute = enable; // Set the speaker system mute state. hr = _ptrDeviceOut->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, reinterpret_cast(&pVolume)); EXIT_ON_ERROR(hr); - const BOOL mute(enable); hr = pVolume->SetMute(mute, NULL); EXIT_ON_ERROR(hr); @@ -1083,13 +1083,13 @@ int32_t AudioDeviceWindowsCore::SetMicrophoneMute(bool enable) { HRESULT hr = S_OK; IAudioEndpointVolume* pVolume = NULL; + const BOOL mute = enable; // Set the microphone system mute state. hr = _ptrDeviceIn->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, reinterpret_cast(&pVolume)); EXIT_ON_ERROR(hr); - const BOOL mute(enable); hr = pVolume->SetMute(mute, NULL); EXIT_ON_ERROR(hr); @@ -1231,12 +1231,12 @@ int32_t AudioDeviceWindowsCore::MicrophoneVolumeIsAvailable(bool& available) { HRESULT hr = S_OK; IAudioEndpointVolume* pVolume = NULL; + float volume = 0.0f; hr = _ptrDeviceIn->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, reinterpret_cast(&pVolume)); EXIT_ON_ERROR(hr); - float volume(0.0f); hr = pVolume->GetMasterVolumeLevelScalar(&volume); if (FAILED(hr)) { available = false; @@ -1823,6 +1823,9 @@ int32_t AudioDeviceWindowsCore::InitPlayout() { WAVEFORMATEX* pWfxOut = NULL; WAVEFORMATEX Wfx = WAVEFORMATEX(); WAVEFORMATEX* pWfxClosestMatch = NULL; + UINT bufferFrameCount = 0; + constexpr int freqs[] = {48000, 44100, 16000, 96000, 32000, 8000}; + REFERENCE_TIME hnsBufferDuration = 0; // Create COM object with IAudioClient interface. SAFE_RELEASE(_ptrClientOut); @@ -1856,7 +1859,6 @@ int32_t AudioDeviceWindowsCore::InitPlayout() { Wfx.wBitsPerSample = 16; Wfx.cbSize = 0; - const int freqs[] = {48000, 44100, 16000, 96000, 32000, 8000}; hr = S_FALSE; // Iterate over frequencies and channels, in order of priority @@ -1941,8 +1943,6 @@ int32_t AudioDeviceWindowsCore::InitPlayout() { // buffer. // **************************************************************************** // - REFERENCE_TIME hnsBufferDuration = - 0; // ask for minimum buffer size (default) if (_devicePlaySampleRate == 44100) { // Ask for a larger buffer size (30ms) when using 44.1kHz as render rate. // There seems to be a larger risk of underruns for 44.1 compared @@ -1982,7 +1982,6 @@ int32_t AudioDeviceWindowsCore::InitPlayout() { // Get the actual size of the shared (endpoint buffer). // Typical value is 960 audio frames <=> 20ms @ 48kHz sample rate. - UINT bufferFrameCount(0); hr = _ptrClientOut->GetBufferSize(&bufferFrameCount); if (SUCCEEDED(hr)) { RTC_LOG(LS_VERBOSE) << "IAudioClient::GetBufferSize() => " @@ -2138,6 +2137,8 @@ int32_t AudioDeviceWindowsCore::InitRecording() { WAVEFORMATEX* pWfxIn = NULL; WAVEFORMATEXTENSIBLE Wfx = WAVEFORMATEXTENSIBLE(); WAVEFORMATEX* pWfxClosestMatch = NULL; + UINT bufferFrameCount = 0; + constexpr int freqs[6] = {48000, 44100, 16000, 96000, 32000, 8000}; // Create COM object with IAudioClient interface. SAFE_RELEASE(_ptrClientIn); @@ -2174,7 +2175,6 @@ int32_t AudioDeviceWindowsCore::InitRecording() { Wfx.Samples.wValidBitsPerSample = Wfx.Format.wBitsPerSample; Wfx.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; - const int freqs[6] = {48000, 44100, 16000, 96000, 32000, 8000}; hr = S_FALSE; // Iterate over frequencies and channels, in order of priority @@ -2271,7 +2271,6 @@ int32_t AudioDeviceWindowsCore::InitRecording() { // Get the actual size of the shared (endpoint buffer). // Typical value is 960 audio frames <=> 20ms @ 48kHz sample rate. - UINT bufferFrameCount(0); hr = _ptrClientIn->GetBufferSize(&bufferFrameCount); if (SUCCEEDED(hr)) { RTC_LOG(LS_VERBOSE) << "IAudioClient::GetBufferSize() => " @@ -2637,6 +2636,13 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() { HANDLE waitArray[2] = {_hShutdownRenderEvent, _hRenderSamplesReadyEvent}; HRESULT hr = S_OK; HANDLE hMmTask = NULL; + IAudioClock* clock = NULL; + REFERENCE_TIME latency = 0; + REFERENCE_TIME devPeriod = 0; + REFERENCE_TIME devPeriodMin = 0; + BYTE* pData = NULL; + int playout_delay = 0; + double endpointBufferSizeMS = 0.0; // Initialize COM as MTA in this thread. ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA); @@ -2669,7 +2675,6 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() { _Lock(); - IAudioClock* clock = NULL; // Get size of rendering buffer (length is expressed as the number of audio // frames the buffer can hold). This value is fixed during the rendering @@ -2683,7 +2688,6 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() { // Get maximum latency for the current stream (will not change for the // lifetime of the IAudioClient object). // - REFERENCE_TIME latency; _ptrClientOut->GetStreamLatency(&latency); RTC_LOG(LS_VERBOSE) << "[REND] max stream latency : " << (DWORD)latency << " (" << (double)(latency / 10000.0) << " ms)"; @@ -2698,8 +2702,6 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() { // an audio application can achieve. Typical value: 100000 <=> 0.01 sec = // 10ms. // - REFERENCE_TIME devPeriod = 0; - REFERENCE_TIME devPeriodMin = 0; _ptrClientOut->GetDevicePeriod(&devPeriod, &devPeriodMin); RTC_LOG(LS_VERBOSE) << "[REND] device period : " << (DWORD)devPeriod << " (" << (double)(devPeriod / 10000.0) << " ms)"; @@ -2707,20 +2709,19 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() { // Derive initial rendering delay. // Example: 10*(960/480) + 15 = 20 + 15 = 35ms // - int playout_delay = 10 * (bufferLength / _playBlockSize) + - (int)((latency + devPeriod) / 10000); + playout_delay = 10 * (bufferLength / _playBlockSize) + + (int)((latency + devPeriod) / 10000); _sndCardPlayDelay = playout_delay; _writtenSamples = 0; RTC_LOG(LS_VERBOSE) << "[REND] initial delay : " << playout_delay; - double endpointBufferSizeMS = + endpointBufferSizeMS = 10.0 * ((double)bufferLength / (double)_devicePlayBlockSize); RTC_LOG(LS_VERBOSE) << "[REND] endpointBufferSizeMS : " << endpointBufferSizeMS; // Before starting the stream, fill the rendering buffer with silence. // - BYTE* pData = NULL; hr = _ptrRenderClient->GetBuffer(bufferLength, &pData); EXIT_ON_ERROR(hr); @@ -3071,11 +3072,18 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread() { HRESULT hr = S_OK; LARGE_INTEGER t1; + UINT32 syncBufferSize = 0; + REFERENCE_TIME latency = 0; + REFERENCE_TIME devPeriod = 0; + REFERENCE_TIME devPeriodMin = 0; + double extraDelayMS = 0.0; + double endpointBufferSizeMS = 0.0; BYTE* syncBuffer = NULL; UINT32 syncBufIndex = 0; _readSamples = 0; + BYTE* pData = 0; // Initialize COM as MTA in this thread. ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA); @@ -3109,7 +3117,7 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread() { // It is used for compensation between native 44.1 and internal 44.0 and // for cases when the capture buffer is larger than 10ms. // - const UINT32 syncBufferSize = 2 * (bufferLength * _recAudioFrameSize); + syncBufferSize = 2 * (bufferLength * _recAudioFrameSize); syncBuffer = new BYTE[syncBufferSize]; if (syncBuffer == NULL) { return (DWORD)E_POINTER; @@ -3120,7 +3128,6 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread() { // Get maximum latency for the current stream (will not change for the // lifetime of the IAudioClient object). // - REFERENCE_TIME latency; _ptrClientIn->GetStreamLatency(&latency); RTC_LOG(LS_VERBOSE) << "[CAPT] max stream latency : " << (DWORD)latency << " (" << (double)(latency / 10000.0) << " ms)"; @@ -3128,17 +3135,14 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread() { // Get the length of the periodic interval separating successive processing // passes by the audio engine on the data in the endpoint buffer. // - REFERENCE_TIME devPeriod = 0; - REFERENCE_TIME devPeriodMin = 0; _ptrClientIn->GetDevicePeriod(&devPeriod, &devPeriodMin); RTC_LOG(LS_VERBOSE) << "[CAPT] device period : " << (DWORD)devPeriod << " (" << (double)(devPeriod / 10000.0) << " ms)"; - double extraDelayMS = (double)((latency + devPeriod) / 10000.0); + extraDelayMS = (double)((latency + devPeriod) / 10000.0); RTC_LOG(LS_VERBOSE) << "[CAPT] extraDelayMS : " << extraDelayMS; - double endpointBufferSizeMS = - 10.0 * ((double)bufferLength / (double)_recBlockSize); + endpointBufferSizeMS = 10.0 * ((double)bufferLength / (double)_recBlockSize); RTC_LOG(LS_VERBOSE) << "[CAPT] endpointBufferSizeMS : " << endpointBufferSizeMS; @@ -3174,7 +3178,7 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread() { } while (keepRecording) { - BYTE* pData = 0; + pData = 0; UINT32 framesAvailable = 0; DWORD flags = 0; UINT64 recTime = 0; @@ -3951,6 +3955,11 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( IPropertyStore* pProps = NULL; IAudioEndpointVolume* pEndpointVolume = NULL; LPWSTR pwszID = NULL; + PROPVARIANT varName; + DWORD dwState = 0; + DWORD dwHwSupportMask = 0; + UINT count = 0; + UINT nChannelCount = 0; // Generate a collection of audio endpoint devices in the system. // Get states for *all* endpoint devices. @@ -3964,7 +3973,6 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( // use the IMMDeviceCollection interface... - UINT count = 0; // Retrieve a count of the devices in the device collection. hr = pCollection->GetCount(&count); @@ -4004,7 +4012,6 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( // use the IPropertyStore interface... - PROPVARIANT varName; // Initialize container for property value. PropVariantInit(&varName); @@ -4015,7 +4022,6 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( RTC_LOG(LS_VERBOSE) << "friendly name: \"" << varName.pwszVal << "\""; // Get the endpoint's current device state - DWORD dwState; hr = pEndpoint->GetState(&dwState); CONTINUE_ON_ERROR(hr); if (dwState & DEVICE_STATE_ACTIVE) @@ -4028,7 +4034,6 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( RTC_LOG(LS_VERBOSE) << "state (0x" << ToHex(dwState) << ") : UNPLUGGED"; // Check the hardware volume capabilities. - DWORD dwHwSupportMask = 0; hr = pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&pEndpointVolume); CONTINUE_ON_ERROR(hr); @@ -4049,7 +4054,6 @@ int32_t AudioDeviceWindowsCore::_EnumerateEndpointDevicesAll( // Check the channel count (#channels in the audio stream that enters or // leaves the audio endpoint device) - UINT nChannelCount(0); hr = pEndpointVolume->GetChannelCount(&nChannelCount); CONTINUE_ON_ERROR(hr); RTC_LOG(LS_VERBOSE) << "#channels : " << nChannelCount;