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 <henrika@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38010}
This commit is contained in:
Danil Chapovalov
2022-09-05 11:15:39 +02:00
committed by WebRTC LUCI CQ
parent dd1eb2e1ec
commit fbfd81f61a
10 changed files with 33 additions and 105 deletions

View File

@ -13,6 +13,7 @@
#include <memory>
#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";

View File

@ -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

View File

@ -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";

View File

@ -16,10 +16,9 @@
#include <memory>
#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.