Potential fix for crash after Mac sleep.

When a Mac goes to sleep, the OS pauses the IO threads. If a
subsequent StopSend/Playout happens, we time out waiting for the IO
threads, but didn't ensure they were shut down.

BUG=
TEST=voe_cmd_test, voe_auto_test

Review URL: http://webrtc-codereview.appspot.com/269013

git-svn-id: http://webrtc.googlecode.com/svn/trunk@949 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
andrew@webrtc.org 2011-11-15 16:23:41 +00:00
parent 85596d5bf4
commit 6a85b17a0a

View File

@ -1999,9 +1999,16 @@ WebRtc_Word32 AudioDeviceMac::StopRecording()
_critSect.Leave(); // Cannot be under lock, risk of deadlock _critSect.Leave(); // Cannot be under lock, risk of deadlock
if (kEventTimeout == _stopEventRec.Wait(2000)) if (kEventTimeout == _stopEventRec.Wait(2000))
{ {
WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, CriticalSectionScoped critScoped(_critSect);
_id, " Timed out stopping the capture IOProc. " WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id,
"We likely failed to detect a device removal."); " Timed out stopping the capture IOProc. "
"We may have failed to detect a device removal.");
WEBRTC_CA_LOG_WARN(AudioDeviceStop(_inputDeviceID,
_inDeviceIOProcID));
WEBRTC_CA_LOG_WARN(
AudioDeviceDestroyIOProcID(_inputDeviceID,
_inDeviceIOProcID));
} }
_critSect.Enter(); _critSect.Enter();
_doStopRec = false; _doStopRec = false;
@ -2025,9 +2032,17 @@ WebRtc_Word32 AudioDeviceMac::StopRecording()
_critSect.Leave(); // Cannot be under lock, risk of deadlock _critSect.Leave(); // Cannot be under lock, risk of deadlock
if (kEventTimeout == _stopEvent.Wait(2000)) if (kEventTimeout == _stopEvent.Wait(2000))
{ {
WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, CriticalSectionScoped critScoped(_critSect);
_id, " Timed out stopping the shared IOProc. " WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id,
"We likely failed to detect a device removal."); " Timed out stopping the shared IOProc. "
"We may have failed to detect a device removal.");
// We assume rendering on a shared device has stopped as well if
// the IOProc times out.
WEBRTC_CA_LOG_WARN(AudioDeviceStop(_outputDeviceID,
_deviceIOProcID));
WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_outputDeviceID,
_deviceIOProcID));
} }
_critSect.Enter(); _critSect.Enter();
_doStop = false; _doStop = false;
@ -2165,15 +2180,22 @@ WebRtc_Word32 AudioDeviceMac::StopPlayout()
_critSect.Leave(); // Cannot be under lock, risk of deadlock _critSect.Leave(); // Cannot be under lock, risk of deadlock
if (kEventTimeout == _stopEvent.Wait(2000)) if (kEventTimeout == _stopEvent.Wait(2000))
{ {
WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, CriticalSectionScoped critScoped(_critSect);
"Timed out stopping the render IOProc, " WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id,
"We likely failed to detect a device removal."); " Timed out stopping the render IOProc. "
"We may have failed to detect a device removal.");
// We assume capturing on a shared device has stopped as well if the
// IOProc times out.
WEBRTC_CA_LOG_WARN(AudioDeviceStop(_outputDeviceID,
_deviceIOProcID));
WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_outputDeviceID,
_deviceIOProcID));
} }
_critSect.Enter(); _critSect.Enter();
_doStop = false; _doStop = false;
WEBRTC_TRACE(kTraceDebug, kTraceAudioDevice, _id, WEBRTC_TRACE(kTraceDebug, kTraceAudioDevice, _id,
"Playout stopped"); "Playout stopped");
} }
// Setting this signal will allow the worker thread to be stopped. // Setting this signal will allow the worker thread to be stopped.
@ -2185,7 +2207,7 @@ WebRtc_Word32 AudioDeviceMac::StopPlayout()
{ {
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id,
" Timed out waiting for the render worker thread to " " Timed out waiting for the render worker thread to "
"stop."); "stop.");
} }
} }
_critSect.Enter(); _critSect.Enter();
@ -2917,7 +2939,7 @@ WebRtc_Word32 AudioDeviceMac::HandleProcessorOverload(
// overload. However, the Windows interpretations of these errors seem to // overload. However, the Windows interpretations of these errors seem to
// be more severe than what ProcessorOverload is thrown for. // be more severe than what ProcessorOverload is thrown for.
// //
// We don't log the notification, as it's sent from the HAL's IO thread. We // We don't log the notification, as it's sent from the HAL's IO thread. We
// don't want to slow it down even further. // don't want to slow it down even further.
if (propertyAddress.mScope == kAudioDevicePropertyScopeInput) if (propertyAddress.mScope == kAudioDevicePropertyScopeInput)
{ {
@ -3024,21 +3046,18 @@ OSStatus AudioDeviceMac::implDeviceIOProc(const AudioBufferList *inputData,
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
if (AudioDeviceCreateIOProcID != NULL) if (AudioDeviceCreateIOProcID != NULL)
{ {
WEBRTC_CA_LOG_ERR(AudioDeviceStop(_outputDeviceID, WEBRTC_CA_LOG_ERR(AudioDeviceStop(_outputDeviceID,
_deviceIOProcID)); _deviceIOProcID));
WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_outputDeviceID, WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_outputDeviceID,
_deviceIOProcID)); _deviceIOProcID));
} }
else else
{ {
#endif #endif
WEBRTC_CA_LOG_ERR(AudioDeviceStop(_outputDeviceID, WEBRTC_CA_LOG_ERR(AudioDeviceStop(_outputDeviceID,
deviceIOProc)); deviceIOProc));
WEBRTC_CA_LOG_WARN(AudioDeviceRemoveIOProc(_outputDeviceID, WEBRTC_CA_LOG_WARN(AudioDeviceRemoveIOProc(_outputDeviceID,
deviceIOProc)); deviceIOProc));
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
} }
#endif #endif
@ -3059,8 +3078,8 @@ OSStatus AudioDeviceMac::implDeviceIOProc(const AudioBufferList *inputData,
if (!_playing) if (!_playing)
{ {
// This can be the case when a shared device is capturing but not // This can be the case when a shared device is capturing but not
// rendering. We allow the checks above before returning to avoid a // rendering. We allow the checks above before returning to avoid a
// timeout when capturing is stopped. // timeout when capturing is stopped.
return 0; return 0;
} }
@ -3149,16 +3168,14 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList *inputData,
if (AudioDeviceCreateIOProcID != NULL) if (AudioDeviceCreateIOProcID != NULL)
{ {
WEBRTC_CA_LOG_ERR(AudioDeviceStop(_inputDeviceID, _inDeviceIOProcID)); WEBRTC_CA_LOG_ERR(AudioDeviceStop(_inputDeviceID, _inDeviceIOProcID));
WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_inputDeviceID, WEBRTC_CA_LOG_WARN(AudioDeviceDestroyIOProcID(_inputDeviceID,
_inDeviceIOProcID)); _inDeviceIOProcID));
} }
else else
{ {
#endif #endif
WEBRTC_CA_LOG_ERR(AudioDeviceStop(_inputDeviceID, inDeviceIOProc)); WEBRTC_CA_LOG_ERR(AudioDeviceStop(_inputDeviceID, inDeviceIOProc));
WEBRTC_CA_LOG_WARN(AudioDeviceRemoveIOProc(_inputDeviceID,
WEBRTC_CA_LOG_WARN(AudioDeviceRemoveIOProc(_inputDeviceID,
inDeviceIOProc)); inDeviceIOProc));
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
} }
@ -3176,7 +3193,7 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList *inputData,
} }
_critSect.Leave(); _critSect.Leave();
} }
if (!_recording) if (!_recording)
{ {
// Allow above checks to avoid a timeout on stopping capture. // Allow above checks to avoid a timeout on stopping capture.