Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0749f85e authored by Jeremy Wu's avatar Jeremy Wu
Browse files

Asrc: fix threading issue in using asrc from source HAL

When the asrc is instantiated from the worker thread in the source HAL,
its scheduler (|read_clock_timer_|) is owned by the worker thread and
invocations to |SchedulePeriodic| should be run in the same thread.

This CL modifies the signature so that it is aware of the thread in
which the task should be run from.

Bug: 331119124
Bug: 333657963
Test: m Bluetooth
Change-Id: Ia66c25c466af878bfd77a0085e64c20e09b9b5fe
parent 8d9a7145
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -29,8 +29,10 @@ cc_library_static {
    shared_libs: [
        "libchrome",
        "liblog",
        "server_configurable_flags",
    ],
    static_libs: [
        "bluetooth_flags_c_lib",
        "libbase",
        "libbluetooth_hci_pdl",
        "libbluetooth_log",
@@ -64,7 +66,11 @@ cc_library_host_shared {
    header_libs: [
        "libbluetooth_headers",
    ],
    shared_libs: [
        "server_configurable_flags",
    ],
    static_libs: [
        "bluetooth_flags_c_lib",
        "libbase",
        "libbluetooth_hci_pdl",
        "libbluetooth_log",
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ source_set("libbt-audio-asrc") {
  ]

  deps = [
    "//bt/flags:bluetooth_flags_c_lib",
    "//bt/system/gd:gd_default_deps"
  ]
}
+34 −18
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <base/strings/stringprintf.h>
#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>

#include <algorithm>
#include <cmath>
@@ -167,7 +168,22 @@ class SourceAudioHalAsrc::ClockRecovery
  }

 public:
  ClockRecovery() : state_{.id = StateId::RESET}, reference_timing_{0, 0, 0} {
  ClockRecovery(bluetooth::common::MessageLoopThread* thread)
      : state_{.id = StateId::RESET}, reference_timing_{0, 0, 0} {
    if (com::android::bluetooth::flags::run_clock_recovery_in_worker_thread()) {
      read_clock_timer_.SchedulePeriodic(
          thread->GetWeakPtr(), FROM_HERE,
          base::BindRepeating(
              [](void*) {
                bluetooth::shim::GetHciLayer()->EnqueueCommand(
                    bluetooth::hci::ReadClockBuilder::Create(
                        0, bluetooth::hci::WhichClock::LOCAL),
                    get_main_thread()->BindOnce(
                        [](bluetooth::hci::CommandCompleteView) {}));
              },
              nullptr),
          std::chrono::milliseconds(100));
    } else {
      read_clock_timer_.SchedulePeriodic(
          get_main_thread()->GetWeakPtr(), FROM_HERE,
          base::BindRepeating(
@@ -180,6 +196,7 @@ class SourceAudioHalAsrc::ClockRecovery
              },
              nullptr),
          std::chrono::milliseconds(100));
    }

    hal::LinkClocker::Register(this);
  }
@@ -411,10 +428,9 @@ inline int32_t SourceAudioHalAsrc::Resampler::Filter(const int32_t* in,

#endif

SourceAudioHalAsrc::SourceAudioHalAsrc(int channels, int sample_rate,
                                       int bit_depth, int interval_us,
                                       int num_burst_buffers,
                                       int burst_delay_ms)
SourceAudioHalAsrc::SourceAudioHalAsrc(
    bluetooth::common::MessageLoopThread* thread, int channels, int sample_rate,
    int bit_depth, int interval_us, int num_burst_buffers, int burst_delay_ms)
    : sample_rate_(sample_rate),
      bit_depth_(bit_depth),
      interval_us_(interval_us),
@@ -453,7 +469,7 @@ SourceAudioHalAsrc::SourceAudioHalAsrc(int channels, int sample_rate,
  // Setup modules, the 32 bits resampler is choosed over the 16 bits resampler
  // when the PCM bit_depth is higher than 16 bits.

  clock_recovery_ = std::make_unique<ClockRecovery>();
  clock_recovery_ = std::make_unique<ClockRecovery>(thread);
  resamplers_ = std::make_unique<std::vector<Resampler>>(channels, bit_depth_);

  // Deduct from the PCM stream characteristics, the size of the pool buffers
+5 −3
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@
#include <memory>
#include <vector>

#include "common/message_loop_thread.h"

namespace bluetooth::audio::asrc {

class SourceAudioHalAsrc {
@@ -36,9 +38,9 @@ class SourceAudioHalAsrc {
  // `burst_delay_ms` helps to ensure that the synchronization with the
  // transmission intervals is done.

  SourceAudioHalAsrc(int channels, int sample_rate, int bit_depth,
                     int interval_us, int num_burst_buffers = 2,
                     int burst_delay_ms = 500);
  SourceAudioHalAsrc(bluetooth::common::MessageLoopThread* thread, int channels,
                     int sample_rate, int bit_depth, int interval_us,
                     int num_burst_buffers = 2, int burst_delay_ms = 500);

  ~SourceAudioHalAsrc();

+2 −1
Original line number Diff line number Diff line
@@ -34,7 +34,8 @@ namespace bluetooth::audio::asrc {
class SourceAudioHalAsrcTest : public SourceAudioHalAsrc {
 public:
  SourceAudioHalAsrcTest(int channels, int bitdepth)
      : SourceAudioHalAsrc(channels, 48000, bitdepth, 10000) {}
      : SourceAudioHalAsrc(&message_loop_thread, channels, 48000, bitdepth,
                           10000) {}

  template <typename T>
  void Resample(double ratio, const T* in, size_t in_length, size_t* in_count,
Loading