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

Commit 9b7cc669 authored by Grzegorz Kołodziejczyk's avatar Grzegorz Kołodziejczyk
Browse files

le_audio: Make audio tick thread real time type

Patch moves responsibility from bluetooth main thread to dedicated sink
worker thread.

Tag: #feature
Test: atest bluetooth_le_audio_client_test
Sponsor: jpawlowski@
Bug: 150670922
Change-Id: I4479bc909161e5bb7cf933b15154f8aceaaf68ee
parent 6cf2d3f4
Loading
Loading
Loading
Loading
+32 −2
Original line number Diff line number Diff line
@@ -29,12 +29,16 @@ using bluetooth::audio::le_audio::LeAudioClientInterface;

namespace {
LeAudioCodecConfiguration source_codec_config;
bluetooth::common::MessageLoopThread worker_thread(
    "bt_le_audio_sink_worker_thread");
bluetooth::common::RepeatingTimer audio_timer;
LeAudioClientInterface* leAudioClientInterface = nullptr;
LeAudioClientInterface::Sink* sinkClientInterface = nullptr;
LeAudioClientInterface::Source* sourceClientInterface = nullptr;
LeAudioClientAudioSinkReceiver* localAudioSinkReceiver = nullptr;
LeAudioClientAudioSourceReceiver* localAudioSourceReceiver = nullptr;
/* Guard audio sink receiver mutual access from stack with internal mutex */
std::mutex sinkInterfaceMutex;
LeAudioClientInterface::Sink* sinkClientInterface = nullptr;

enum {
  HAL_UNINITIALIZED,
@@ -89,6 +93,7 @@ void send_audio_data() {
        bluetooth::common::time_get_os_boottime_us();
  }

  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  if (localAudioSinkReceiver != nullptr) {
    localAudioSinkReceiver->OnAudioDataReady(data);
  }
@@ -97,7 +102,7 @@ void send_audio_data() {
void start_audio_ticks() {
  wakelock_acquire();
  audio_timer.SchedulePeriodic(
      get_main_thread()->GetWeakPtr(), FROM_HERE, base::Bind(&send_audio_data),
      worker_thread.GetWeakPtr(), FROM_HERE, base::Bind(&send_audio_data),
      base::TimeDelta::FromMicroseconds(source_codec_config.data_interval_us));
}

@@ -106,7 +111,25 @@ void stop_audio_ticks() {
  wakelock_release();
}

bool init_audio_sink_thread() {
  worker_thread.StartUp();
  if (!worker_thread.IsRunning()) {
    LOG(ERROR) << __func__ << ", unable to start up media thread";
    return false;
  }

  /* Schedule the rest of the operations */
  if (!worker_thread.EnableRealTimeScheduling()) {
#if defined(OS_ANDROID)
    LOG(FATAL) << __func__ << ", Failed to increase media thread priority";
#endif
  }

  return true;
}

bool le_audio_sink_on_resume_req(bool start_media_task) {
  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  if (localAudioSinkReceiver == nullptr) {
    LOG(ERROR) << __func__ << ": localAudioSinkReceiver is nullptr";
    return false;
@@ -143,6 +166,7 @@ bool le_audio_source_on_resume_req(bool start_media_task) {
}

bool le_audio_sink_on_suspend_req() {
  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  stop_audio_ticks();
  if (localAudioSinkReceiver != nullptr) {
    // Call OnAudioSuspend and block till it returns.
@@ -195,6 +219,7 @@ bool le_audio_source_on_suspend_req() {

bool le_audio_sink_on_metadata_update_req(
    const source_metadata_t& source_metadata) {
  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  if (localAudioSinkReceiver == nullptr) {
    LOG(ERROR) << __func__ << ", audio receiver not started";
    return false;
@@ -256,6 +281,7 @@ bool LeAudioClientAudioSource::Start(
  sinkClientInterface->SetPcmParameters(pcmParameters);
  sinkClientInterface->StartSession();

  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  localAudioSinkReceiver = audioReceiver;
  le_audio_sink_hal_state = HAL_STARTED;

@@ -278,6 +304,7 @@ void LeAudioClientAudioSource::Stop() {

  sinkClientInterface->StopSession();
  le_audio_sink_hal_state = HAL_STOPPED;
  std::lock_guard<std::mutex> guard(sinkInterfaceMutex);
  localAudioSinkReceiver = nullptr;

  stop_audio_ticks();
@@ -314,6 +341,8 @@ const void* LeAudioClientAudioSource::Acquire() {
    return nullptr;
  }

  if (!init_audio_sink_thread()) return nullptr;

  le_audio_sink_hal_state = HAL_STOPPED;
  return sinkClientInterface;
}
@@ -330,6 +359,7 @@ void LeAudioClientAudioSource::Release(const void* instance) {
    return;
  }

  worker_thread.ShutDown();
  sinkClientInterface->Cleanup();
  leAudioClientInterface->ReleaseSink(sinkClientInterface);
  le_audio_sink_hal_state = HAL_UNINITIALIZED;