Skip to content

Commit

Permalink
Thread checker fails when switching to/from bluetooth headset.
Browse files Browse the repository at this point in the history
Made some minor changes to resolve the issue. Only affects Debug builds.

NOTRY=TRUE

Bug: webrtc:9310
Change-Id: Ieeeb57d24b559282b2eefd4d8785f7cfe4f44e40
Reviewed-on: https://webrtc-review.googlesource.com/79624
Commit-Queue: Henrik Andreassson <[email protected]>
Reviewed-by: Peter Hanspers <[email protected]>
Cr-Commit-Position: refs/heads/master@{#23434}
  • Loading branch information
henrikand authored and Commit Bot committed May 29, 2018
1 parent 41757af commit 79445ea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
3 changes: 3 additions & 0 deletions modules/audio_device/ios/audio_device_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
// Closes and deletes the voice-processing I/O unit.
void ShutdownPlayOrRecord();

// Resets thread-checkers before a call is restarted.
void PrepareForNewStart();

// Ensures that methods are called from the same thread as this object is
// created on.
rtc::ThreadChecker thread_checker_;
Expand Down
26 changes: 17 additions & 9 deletions modules/audio_device/ios/audio_device_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,7 @@ static void LogDeviceInfo() {
if (!audio_unit_->Stop()) {
RTCLogError(@"Failed to stop the audio unit for interruption begin.");
} else {
// The audio unit has been stopped but will be restarted when the
// interruption ends in HandleInterruptionEnd(). It will result in audio
// callbacks from a new native I/O thread which means that we must detach
// thread checkers here to be prepared for an upcoming new audio stream.
io_thread_checker_.DetachFromThread();
// The audio device buffer must also be informed about the interrupted
// state so it can detach its thread checkers as well.
audio_device_buffer_->NativeAudioPlayoutInterrupted();
audio_device_buffer_->NativeAudioRecordingInterrupted();
PrepareForNewStart();
}
}
is_interrupted_ = true;
Expand Down Expand Up @@ -581,6 +573,7 @@ static void LogDeviceInfo() {
if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kStarted) {
audio_unit_->Stop();
restart_audio_unit = true;
PrepareForNewStart();
}
if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kInitialized) {
audio_unit_->Uninitialize();
Expand Down Expand Up @@ -901,4 +894,19 @@ static void LogDeviceInfo() {
[session unlockForConfiguration];
}

void AudioDeviceIOS::PrepareForNewStart() {
LOGI() << "PrepareForNewStart";
// The audio unit has been stopped and preparations are needed for an upcoming
// restart. It will result in audio callbacks from a new native I/O thread
// which means that we must detach thread checkers here to be prepared for an
// upcoming new audio stream.
io_thread_checker_.DetachFromThread();
// The audio device buffer must also be informed about the interrupted
// state so it can detach its thread checkers as well.
if (audio_device_buffer_) {
audio_device_buffer_->NativeAudioPlayoutInterrupted();
audio_device_buffer_->NativeAudioRecordingInterrupted();
}
}

} // namespace webrtc
3 changes: 3 additions & 0 deletions sdk/objc/Framework/Native/src/audio/audio_device_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ class AudioDeviceIOS : public AudioDeviceGeneric,
// Closes and deletes the voice-processing I/O unit.
void ShutdownPlayOrRecord();

// Resets thread-checkers before a call is restarted.
void PrepareForNewStart();

// Ensures that methods are called from the same thread as this object is
// created on.
rtc::ThreadChecker thread_checker_;
Expand Down
26 changes: 17 additions & 9 deletions sdk/objc/Framework/Native/src/audio/audio_device_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,7 @@ static void LogDeviceInfo() {
if (!audio_unit_->Stop()) {
RTCLogError(@"Failed to stop the audio unit for interruption begin.");
} else {
// The audio unit has been stopped but will be restarted when the
// interruption ends in HandleInterruptionEnd(). It will result in audio
// callbacks from a new native I/O thread which means that we must detach
// thread checkers here to be prepared for an upcoming new audio stream.
io_thread_checker_.DetachFromThread();
// The audio device buffer must also be informed about the interrupted
// state so it can detach its thread checkers as well.
audio_device_buffer_->NativeAudioPlayoutInterrupted();
audio_device_buffer_->NativeAudioRecordingInterrupted();
PrepareForNewStart();
}
}
is_interrupted_ = true;
Expand Down Expand Up @@ -587,6 +579,7 @@ static void LogDeviceInfo() {
if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kStarted) {
audio_unit_->Stop();
restart_audio_unit = true;
PrepareForNewStart();
}
if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kInitialized) {
audio_unit_->Uninitialize();
Expand Down Expand Up @@ -907,6 +900,21 @@ static void LogDeviceInfo() {
[session unlockForConfiguration];
}

void AudioDeviceIOS::PrepareForNewStart() {
LOGI() << "PrepareForNewStart";
// The audio unit has been stopped and preparations are needed for an upcoming
// restart. It will result in audio callbacks from a new native I/O thread
// which means that we must detach thread checkers here to be prepared for an
// upcoming new audio stream.
io_thread_checker_.DetachFromThread();
// The audio device buffer must also be informed about the interrupted
// state so it can detach its thread checkers as well.
if (audio_device_buffer_) {
audio_device_buffer_->NativeAudioPlayoutInterrupted();
audio_device_buffer_->NativeAudioRecordingInterrupted();
}
}

bool AudioDeviceIOS::IsInterrupted() {
return is_interrupted_;
}
Expand Down

0 comments on commit 79445ea

Please sign in to comment.