From fbfd81f61afb79f478a353d1d4e0ef052118413e Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Mon, 5 Sep 2022 11:15:39 +0200 Subject: [PATCH] In android aaudio wrappers use threads through TaskQueue interface Bug: webrtc:9702 Change-Id: I4686b8312a5e6705050ec89381938ea5da379d9b Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/274160 Reviewed-by: Henrik Andreassson Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#38010} --- modules/audio_device/BUILD.gn | 1 - modules/audio_device/android/aaudio_player.cc | 18 +++------------- modules/audio_device/android/aaudio_player.h | 14 ++++--------- .../audio_device/android/aaudio_recorder.cc | 21 +++---------------- .../audio_device/android/aaudio_recorder.h | 11 +++------- sdk/android/BUILD.gn | 7 ++++++- .../src/jni/audio_device/aaudio_player.cc | 18 +++------------- .../src/jni/audio_device/aaudio_player.h | 16 ++++++-------- .../src/jni/audio_device/aaudio_recorder.cc | 20 ++---------------- .../src/jni/audio_device/aaudio_recorder.h | 12 +++-------- 10 files changed, 33 insertions(+), 105 deletions(-) diff --git a/modules/audio_device/BUILD.gn b/modules/audio_device/BUILD.gn index 327c08f674..7919dcb1ac 100644 --- a/modules/audio_device/BUILD.gn +++ b/modules/audio_device/BUILD.gn @@ -192,7 +192,6 @@ rtc_library("audio_device_impl") { "../../api/task_queue", "../../common_audio", "../../common_audio:common_audio_c", - "../../rtc_base", "../../rtc_base:buffer", "../../rtc_base:checks", "../../rtc_base:logging", diff --git a/modules/audio_device/android/aaudio_player.cc b/modules/audio_device/android/aaudio_player.cc index 5257b2ba1b..81e5bf5427 100644 --- a/modules/audio_device/android/aaudio_player.cc +++ b/modules/audio_device/android/aaudio_player.cc @@ -13,6 +13,7 @@ #include #include "api/array_view.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/android/audio_manager.h" #include "modules/audio_device/fine_audio_buffer.h" #include "rtc_base/checks.h" @@ -20,12 +21,8 @@ namespace webrtc { -enum AudioDeviceMessageType : uint32_t { - kMessageOutputStreamDisconnected, -}; - AAudioPlayer::AAudioPlayer(AudioManager* audio_manager) - : main_thread_(rtc::Thread::Current()), + : main_thread_(TaskQueueBase::Current()), aaudio_(audio_manager, AAUDIO_DIRECTION_OUTPUT, this) { RTC_LOG(LS_INFO) << "ctor"; thread_checker_aaudio_.Detach(); @@ -147,7 +144,7 @@ void AAudioPlayer::OnErrorCallback(aaudio_result_t error) { // from the callback, use another thread instead". A message is therefore // sent to the main thread to do the restart operation. RTC_DCHECK(main_thread_); - main_thread_->Post(RTC_FROM_HERE, this, kMessageOutputStreamDisconnected); + main_thread_->PostTask([this] { HandleStreamDisconnected(); }); } } @@ -204,15 +201,6 @@ aaudio_data_callback_result_t AAudioPlayer::OnDataCallback(void* audio_data, return AAUDIO_CALLBACK_RESULT_CONTINUE; } -void AAudioPlayer::OnMessage(rtc::Message* msg) { - RTC_DCHECK_RUN_ON(&main_thread_checker_); - switch (msg->message_id) { - case kMessageOutputStreamDisconnected: - HandleStreamDisconnected(); - break; - } -} - void AAudioPlayer::HandleStreamDisconnected() { RTC_DCHECK_RUN_ON(&main_thread_checker_); RTC_DLOG(LS_INFO) << "HandleStreamDisconnected"; diff --git a/modules/audio_device/android/aaudio_player.h b/modules/audio_device/android/aaudio_player.h index 4bf3ee3bc0..ea5d578092 100644 --- a/modules/audio_device/android/aaudio_player.h +++ b/modules/audio_device/android/aaudio_player.h @@ -16,10 +16,9 @@ #include #include "api/sequence_checker.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/android/aaudio_wrapper.h" #include "modules/audio_device/include/audio_device_defines.h" -#include "rtc_base/message_handler.h" -#include "rtc_base/thread.h" #include "rtc_base/thread_annotations.h" namespace webrtc { @@ -48,8 +47,7 @@ class AudioManager; // where the internal AAudio buffer can be increased when needed. It will // reduce the risk of underruns (~glitches) at the expense of an increased // latency. -class AAudioPlayer final : public AAudioObserverInterface, - public rtc::MessageHandler { +class AAudioPlayer final : public AAudioObserverInterface { public: explicit AAudioPlayer(AudioManager* audio_manager); ~AAudioPlayer(); @@ -85,10 +83,6 @@ class AAudioPlayer final : public AAudioObserverInterface, // Called on a real-time thread owned by AAudio. void OnErrorCallback(aaudio_result_t error) override; - // rtc::MessageHandler used for restart messages from the error-callback - // thread to the main (creating) thread. - void OnMessage(rtc::Message* msg) override; - private: // Closes the existing stream and starts a new stream. void HandleStreamDisconnected(); @@ -102,8 +96,8 @@ class AAudioPlayer final : public AAudioObserverInterface, // object. SequenceChecker thread_checker_aaudio_; - // The thread on which this object is created on. - rtc::Thread* main_thread_; + // The task queue on which this object is created on. + TaskQueueBase* main_thread_; // Wraps all AAudio resources. Contains an output stream using the default // output audio device. Can be accessed on both the main thread and the diff --git a/modules/audio_device/android/aaudio_recorder.cc b/modules/audio_device/android/aaudio_recorder.cc index 4757cf8cf0..21e5dd8a74 100644 --- a/modules/audio_device/android/aaudio_recorder.cc +++ b/modules/audio_device/android/aaudio_recorder.cc @@ -13,6 +13,7 @@ #include #include "api/array_view.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/android/audio_manager.h" #include "modules/audio_device/fine_audio_buffer.h" #include "rtc_base/checks.h" @@ -21,12 +22,8 @@ namespace webrtc { -enum AudioDeviceMessageType : uint32_t { - kMessageInputStreamDisconnected, -}; - AAudioRecorder::AAudioRecorder(AudioManager* audio_manager) - : main_thread_(rtc::Thread::Current()), + : main_thread_(TaskQueueBase::Current()), aaudio_(audio_manager, AAUDIO_DIRECTION_INPUT, this) { RTC_LOG(LS_INFO) << "ctor"; thread_checker_aaudio_.Detach(); @@ -142,7 +139,7 @@ void AAudioRecorder::OnErrorCallback(aaudio_result_t error) { // from the callback, use another thread instead". A message is therefore // sent to the main thread to do the restart operation. RTC_DCHECK(main_thread_); - main_thread_->Post(RTC_FROM_HERE, this, kMessageInputStreamDisconnected); + main_thread_->PostTask([this] { HandleStreamDisconnected(); }); } } @@ -190,18 +187,6 @@ aaudio_data_callback_result_t AAudioRecorder::OnDataCallback( return AAUDIO_CALLBACK_RESULT_CONTINUE; } -void AAudioRecorder::OnMessage(rtc::Message* msg) { - RTC_DCHECK_RUN_ON(&thread_checker_); - switch (msg->message_id) { - case kMessageInputStreamDisconnected: - HandleStreamDisconnected(); - break; - default: - RTC_LOG(LS_ERROR) << "Invalid message id: " << msg->message_id; - break; - } -} - void AAudioRecorder::HandleStreamDisconnected() { RTC_DCHECK_RUN_ON(&thread_checker_); RTC_LOG(LS_INFO) << "HandleStreamDisconnected"; diff --git a/modules/audio_device/android/aaudio_recorder.h b/modules/audio_device/android/aaudio_recorder.h index d0ad6be43d..6df7eed076 100644 --- a/modules/audio_device/android/aaudio_recorder.h +++ b/modules/audio_device/android/aaudio_recorder.h @@ -16,10 +16,9 @@ #include #include "api/sequence_checker.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/android/aaudio_wrapper.h" #include "modules/audio_device/include/audio_device_defines.h" -#include "rtc_base/message_handler.h" -#include "rtc_base/thread.h" namespace webrtc { @@ -41,8 +40,7 @@ class AudioManager; // // TODO(henrika): add comments about device changes and adaptive buffer // management. -class AAudioRecorder : public AAudioObserverInterface, - public rtc::MessageHandler { +class AAudioRecorder : public AAudioObserverInterface { public: explicit AAudioRecorder(AudioManager* audio_manager); ~AAudioRecorder(); @@ -79,9 +77,6 @@ class AAudioRecorder : public AAudioObserverInterface, // Called on a real-time thread owned by AAudio. void OnErrorCallback(aaudio_result_t error) override; - // rtc::MessageHandler used for restart messages. - void OnMessage(rtc::Message* msg) override; - private: // Closes the existing stream and starts a new stream. void HandleStreamDisconnected(); @@ -96,7 +91,7 @@ class AAudioRecorder : public AAudioObserverInterface, SequenceChecker thread_checker_aaudio_; // The thread on which this object is created on. - rtc::Thread* main_thread_; + TaskQueueBase* main_thread_; // Wraps all AAudio resources. Contains an input stream using the default // input audio device. diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index f082bd353a..cf7966cf9e 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -1225,10 +1225,15 @@ if (current_os == "linux" || is_android) { ":audio_device_module_base", ":base_jni", "../../api:array_view", + "../../api:sequence_checker", + "../../api/task_queue", "../../modules/audio_device", "../../modules/audio_device:audio_device_buffer", - "../../rtc_base", "../../rtc_base:checks", + "../../rtc_base:logging", + "../../rtc_base:macromagic", + "../../rtc_base:stringutils", + "../../rtc_base:timeutils", "../../system_wrappers", ] absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ] diff --git a/sdk/android/src/jni/audio_device/aaudio_player.cc b/sdk/android/src/jni/audio_device/aaudio_player.cc index ae8fcb9613..2b745b3bd8 100644 --- a/sdk/android/src/jni/audio_device/aaudio_player.cc +++ b/sdk/android/src/jni/audio_device/aaudio_player.cc @@ -13,6 +13,7 @@ #include #include "api/array_view.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/fine_audio_buffer.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" @@ -21,12 +22,8 @@ namespace webrtc { namespace jni { -enum AudioDeviceMessageType : uint32_t { - kMessageOutputStreamDisconnected, -}; - AAudioPlayer::AAudioPlayer(const AudioParameters& audio_parameters) - : main_thread_(rtc::Thread::Current()), + : main_thread_(TaskQueueBase::Current()), aaudio_(audio_parameters, AAUDIO_DIRECTION_OUTPUT, this) { RTC_LOG(LS_INFO) << "ctor"; thread_checker_aaudio_.Detach(); @@ -163,7 +160,7 @@ void AAudioPlayer::OnErrorCallback(aaudio_result_t error) { // from the callback, use another thread instead". A message is therefore // sent to the main thread to do the restart operation. RTC_DCHECK(main_thread_); - main_thread_->Post(RTC_FROM_HERE, this, kMessageOutputStreamDisconnected); + main_thread_->PostTask([this] { HandleStreamDisconnected(); }); } } @@ -220,15 +217,6 @@ aaudio_data_callback_result_t AAudioPlayer::OnDataCallback(void* audio_data, return AAUDIO_CALLBACK_RESULT_CONTINUE; } -void AAudioPlayer::OnMessage(rtc::Message* msg) { - RTC_DCHECK_RUN_ON(&main_thread_checker_); - switch (msg->message_id) { - case kMessageOutputStreamDisconnected: - HandleStreamDisconnected(); - break; - } -} - void AAudioPlayer::HandleStreamDisconnected() { RTC_DCHECK_RUN_ON(&main_thread_checker_); RTC_DLOG(LS_INFO) << "HandleStreamDisconnected"; diff --git a/sdk/android/src/jni/audio_device/aaudio_player.h b/sdk/android/src/jni/audio_device/aaudio_player.h index 9e775ecfa3..7286d6e872 100644 --- a/sdk/android/src/jni/audio_device/aaudio_player.h +++ b/sdk/android/src/jni/audio_device/aaudio_player.h @@ -17,10 +17,9 @@ #include "absl/types/optional.h" #include "api/sequence_checker.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/audio_device_buffer.h" #include "modules/audio_device/include/audio_device_defines.h" -#include "rtc_base/message_handler.h" -#include "rtc_base/thread.h" #include "rtc_base/thread_annotations.h" #include "sdk/android/src/jni/audio_device/aaudio_wrapper.h" #include "sdk/android/src/jni/audio_device/audio_device_module.h" @@ -52,9 +51,7 @@ namespace jni { // where the internal AAudio buffer can be increased when needed. It will // reduce the risk of underruns (~glitches) at the expense of an increased // latency. -class AAudioPlayer final : public AudioOutput, - public AAudioObserverInterface, - public rtc::MessageHandler { +class AAudioPlayer final : public AudioOutput, public AAudioObserverInterface { public: explicit AAudioPlayer(const AudioParameters& audio_parameters); ~AAudioPlayer() override; @@ -90,11 +87,10 @@ class AAudioPlayer final : public AudioOutput, // Called on a real-time thread owned by AAudio. void OnErrorCallback(aaudio_result_t error) override; - // rtc::MessageHandler used for restart messages from the error-callback - // thread to the main (creating) thread. - void OnMessage(rtc::Message* msg) override; - private: + // TODO(henrika): Implement. + int GetPlayoutUnderrunCount() override { return 0; } + // Closes the existing stream and starts a new stream. void HandleStreamDisconnected(); @@ -108,7 +104,7 @@ class AAudioPlayer final : public AudioOutput, SequenceChecker thread_checker_aaudio_; // The thread on which this object is created on. - rtc::Thread* main_thread_; + TaskQueueBase* main_thread_; // Wraps all AAudio resources. Contains an output stream using the default // output audio device. Can be accessed on both the main thread and the diff --git a/sdk/android/src/jni/audio_device/aaudio_recorder.cc b/sdk/android/src/jni/audio_device/aaudio_recorder.cc index d66c1d0235..39130cd551 100644 --- a/sdk/android/src/jni/audio_device/aaudio_recorder.cc +++ b/sdk/android/src/jni/audio_device/aaudio_recorder.cc @@ -22,12 +22,8 @@ namespace webrtc { namespace jni { -enum AudioDeviceMessageType : uint32_t { - kMessageInputStreamDisconnected, -}; - AAudioRecorder::AAudioRecorder(const AudioParameters& audio_parameters) - : main_thread_(rtc::Thread::Current()), + : main_thread_(TaskQueueBase::Current()), aaudio_(audio_parameters, AAUDIO_DIRECTION_INPUT, this) { RTC_LOG(LS_INFO) << "ctor"; thread_checker_aaudio_.Detach(); @@ -153,7 +149,7 @@ void AAudioRecorder::OnErrorCallback(aaudio_result_t error) { // from the callback, use another thread instead". A message is therefore // sent to the main thread to do the restart operation. RTC_DCHECK(main_thread_); - main_thread_->Post(RTC_FROM_HERE, this, kMessageInputStreamDisconnected); + main_thread_->PostTask([this] { HandleStreamDisconnected(); }); } } @@ -201,18 +197,6 @@ aaudio_data_callback_result_t AAudioRecorder::OnDataCallback( return AAUDIO_CALLBACK_RESULT_CONTINUE; } -void AAudioRecorder::OnMessage(rtc::Message* msg) { - RTC_DCHECK_RUN_ON(&thread_checker_); - switch (msg->message_id) { - case kMessageInputStreamDisconnected: - HandleStreamDisconnected(); - break; - default: - RTC_LOG(LS_ERROR) << "Invalid message id: " << msg->message_id; - break; - } -} - void AAudioRecorder::HandleStreamDisconnected() { RTC_DCHECK_RUN_ON(&thread_checker_); RTC_LOG(LS_INFO) << "HandleStreamDisconnected"; diff --git a/sdk/android/src/jni/audio_device/aaudio_recorder.h b/sdk/android/src/jni/audio_device/aaudio_recorder.h index a911577bfe..016c9b00af 100644 --- a/sdk/android/src/jni/audio_device/aaudio_recorder.h +++ b/sdk/android/src/jni/audio_device/aaudio_recorder.h @@ -16,10 +16,9 @@ #include #include "api/sequence_checker.h" +#include "api/task_queue/task_queue_base.h" #include "modules/audio_device/audio_device_buffer.h" #include "modules/audio_device/include/audio_device_defines.h" -#include "rtc_base/message_handler.h" -#include "rtc_base/thread.h" #include "sdk/android/src/jni/audio_device/aaudio_wrapper.h" #include "sdk/android/src/jni/audio_device/audio_device_module.h" @@ -44,9 +43,7 @@ namespace jni { // // TODO(henrika): add comments about device changes and adaptive buffer // management. -class AAudioRecorder : public AudioInput, - public AAudioObserverInterface, - public rtc::MessageHandler { +class AAudioRecorder : public AudioInput, public AAudioObserverInterface { public: explicit AAudioRecorder(const AudioParameters& audio_parameters); ~AAudioRecorder() override; @@ -82,9 +79,6 @@ class AAudioRecorder : public AudioInput, // Called on a real-time thread owned by AAudio. void OnErrorCallback(aaudio_result_t error) override; - // rtc::MessageHandler used for restart messages. - void OnMessage(rtc::Message* msg) override; - private: // Closes the existing stream and starts a new stream. void HandleStreamDisconnected(); @@ -99,7 +93,7 @@ class AAudioRecorder : public AudioInput, SequenceChecker thread_checker_aaudio_; // The thread on which this object is created on. - rtc::Thread* main_thread_; + TaskQueueBase* main_thread_; // Wraps all AAudio resources. Contains an input stream using the default // input audio device.