[M87] Fix AAudio crash on device change
Currently, when using AAudio, we treat device changes like any other
error, which causes video playback to fail when plugging in headphones.
This CL changes this so we intercept device disconnection errors, and
report them as kDeviceChange errors to the OutputController, which
forces the graceful creation of a new stream.
(cherry picked from commit 42f255d543e50f19e8b8ed2e4d57bc0e050bafe0)
TBR: [email protected]
Bug: 1136559
Change-Id: I7969ad3edac2b7bbc9a3e2271f423cf7a029e7f3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2462340
Commit-Queue: Thomas Guilbert <[email protected]>
Commit-Queue: Dale Curtis <[email protected]>
Auto-Submit: Thomas Guilbert <[email protected]>
Reviewed-by: Dale Curtis <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#815438}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2466393
Reviewed-by: Thomas Guilbert <[email protected]>
Cr-Commit-Position: refs/branch-heads/4280@{#270}
Cr-Branched-From: ea420fb963f9658c9969b6513c56b8f47efa1a2a-refs/heads/master@{#812852}
diff --git a/media/audio/android/aaudio_output.cc b/media/audio/android/aaudio_output.cc
index f94be7c..e9bb8867 100644
--- a/media/audio/android/aaudio_output.cc
+++ b/media/audio/android/aaudio_output.cc
@@ -114,6 +114,13 @@
{
base::AutoLock al(lock_);
+
+ // The device might have been disconnected between Open() and Start().
+ if (device_changed_) {
+ callback->OnError(AudioSourceCallback::ErrorType::kDeviceChange);
+ return;
+ }
+
DCHECK(!callback_);
callback_ = callback;
}
@@ -217,9 +224,20 @@
void AAudioOutputStream::OnStreamError(aaudio_result_t error) {
base::AutoLock al(lock_);
+
+ if (error == AAUDIO_ERROR_DISCONNECTED)
+ device_changed_ = true;
+
+ if (!callback_)
+ return;
+
+ if (device_changed_) {
+ callback_->OnError(AudioSourceCallback::ErrorType::kDeviceChange);
+ return;
+ }
+
// TODO(dalecurtis): Consider sending a translated |error| code.
- if (callback_)
- callback_->OnError(AudioSourceCallback::ErrorType::kUnknown);
+ callback_->OnError(AudioSourceCallback::ErrorType::kUnknown);
}
void AAudioOutputStream::SetVolume(double volume) {
diff --git a/media/audio/android/aaudio_output.h b/media/audio/android/aaudio_output.h
index c4a8120..5c6b7b2 100644
--- a/media/audio/android/aaudio_output.h
+++ b/media/audio/android/aaudio_output.h
@@ -67,6 +67,7 @@
AudioSourceCallback* callback_ GUARDED_BY(lock_) = nullptr;
bool muted_ GUARDED_BY(lock_) = false;
double volume_ GUARDED_BY(lock_) = 1.0;
+ bool device_changed_ GUARDED_BY(lock_) = false;
DISALLOW_COPY_AND_ASSIGN(AAudioOutputStream);
};