Skip to content

Commit bdb55c8

Browse files
This CL is an attempt to remove a crash we can see when closing down VoiceEgine.
It can happen that the capture thread tries to access an invalid object after StopPlayout has been called. I have also extended the usage of the new ScopedCOMInitializer to all threads. See this step as code cleanup. Review URL: http://webrtc-codereview.appspot.com/239014 git-svn-id: http://webrtc.googlecode.com/svn/trunk@813 4adac7df-926f-26a2-2b94-8c16560cd09d
1 parent a6c2335 commit bdb55c8

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/modules/audio_device/main/source/win/audio_device_core_win.cc

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ bool AudioDeviceWindowsCore::CoreAudioIsSupported()
186186

187187
bool MMDeviceIsAvailable(false);
188188
bool coreAudioIsSupported(false);
189-
bool coUninitializeIsRequired(true);
190189

191190
HRESULT hr(S_OK);
192191
TCHAR buf[MAXERRORLENGTH];
@@ -199,8 +198,8 @@ bool AudioDeviceWindowsCore::CoreAudioIsSupported()
199198
// wrapper also ensures that each call to CoInitializeEx is balanced
200199
// by a corresponding call to CoUninitialize.
201200
//
202-
ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA);
203-
if (!com_init.succeeded()) {
201+
ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA);
202+
if (!comInit.succeeded()) {
204203
// Things will work even if an STA thread is calling this method but we
205204
// want to ensure that MTA is used and therefore return false here.
206205
return false;
@@ -3114,10 +3113,9 @@ WebRtc_Word32 AudioDeviceWindowsCore::StopPlayout()
31143113
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id,
31153114
"webrtc_core_audio_render_thread is now closed");
31163115

3117-
// Ensure that the thread has released these interfaces properly
3118-
assert(NULL == _ptrClientOut);
3119-
assert(NULL == _ptrRenderClient);
3120-
3116+
SAFE_RELEASE(_ptrClientOut);
3117+
SAFE_RELEASE(_ptrRenderClient);
3118+
31213119
_playIsInitialized = false;
31223120
_playing = false;
31233121

@@ -3427,12 +3425,12 @@ DWORD AudioDeviceWindowsCore::DoRenderThread()
34273425
LARGE_INTEGER t2;
34283426
WebRtc_Word32 time(0);
34293427

3430-
hr = CoInitializeEx(NULL, COM_THREADING_MODEL);
3431-
if (FAILED(hr))
3432-
{
3433-
_TraceCOMError(hr);
3434-
WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "unable to initialize COM in render thread");
3435-
return hr;
3428+
// Initialize COM as MTA in this thread.
3429+
ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA);
3430+
if (!comInit.succeeded()) {
3431+
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id,
3432+
"failed to initialize COM in render thread");
3433+
return -1;
34363434
}
34373435

34383436
_SetThreadName(-1, "webrtc_core_audio_render_thread");
@@ -3665,10 +3663,6 @@ DWORD AudioDeviceWindowsCore::DoRenderThread()
36653663
WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "_Rendering thread is now terminated properly");
36663664
}
36673665

3668-
SAFE_RELEASE(_ptrClientOut);
3669-
SAFE_RELEASE(_ptrRenderClient);
3670-
3671-
CoUninitialize();
36723666
return (DWORD)hr;
36733667
}
36743668

@@ -3677,15 +3671,6 @@ DWORD AudioDeviceWindowsCore::InitCaptureThreadPriority()
36773671
HRESULT hr = S_OK;
36783672
_hMmTask = NULL;
36793673

3680-
hr = CoInitializeEx(NULL, COM_THREADING_MODEL);
3681-
if (FAILED(hr))
3682-
{
3683-
_TraceCOMError(hr);
3684-
WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id,
3685-
"unable to initialize COM in capture thread");
3686-
return hr;
3687-
}
3688-
36893674
_SetThreadName(-1, "webrtc_core_audio_capture_thread");
36903675

36913676
// Use Multimedia Class Scheduler Service (MMCSS) to boost the thread
@@ -3735,6 +3720,14 @@ DWORD AudioDeviceWindowsCore::DoCaptureThreadPollDMO()
37353720
assert(_mediaBuffer != NULL);
37363721
bool keepRecording = true;
37373722

3723+
// Initialize COM as MTA in this thread.
3724+
ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA);
3725+
if (!comInit.succeeded()) {
3726+
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id,
3727+
"failed to initialize COM in polling DMO thread");
3728+
return -1;
3729+
}
3730+
37383731
HRESULT hr = InitCaptureThreadPriority();
37393732
if (FAILED(hr))
37403733
{
@@ -3861,7 +3854,6 @@ DWORD AudioDeviceWindowsCore::DoCaptureThreadPollDMO()
38613854
"Capturing thread is now terminated properly");
38623855
}
38633856

3864-
CoUninitialize();
38653857
return hr;
38663858
}
38673859

@@ -3891,6 +3883,14 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread()
38913883

38923884
_readSamples = 0;
38933885

3886+
// Initialize COM as MTA in this thread.
3887+
ScopedCOMInitializer comInit(ScopedCOMInitializer::kMTA);
3888+
if (!comInit.succeeded()) {
3889+
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id,
3890+
"failed to initialize COM in capture thread");
3891+
return -1;
3892+
}
3893+
38943894
hr = InitCaptureThreadPriority();
38953895
if (FAILED(hr))
38963896
{
@@ -4171,7 +4171,6 @@ DWORD AudioDeviceWindowsCore::DoCaptureThread()
41714171
delete [] syncBuffer;
41724172
}
41734173

4174-
CoUninitialize();
41754174
return (DWORD)hr;
41764175
}
41774176

0 commit comments

Comments
 (0)