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

Commit 3a4ae412 authored by Cheney Ni's avatar Cheney Ni
Browse files

A2DP: Based on the HAL session type to control the encoder

Because we are now runtime switching the audio HAL between software and
hardare data path, it is not suitable any more to simply check whether
the offloading is enabled or not when handling the AV state change, and
here we check whether the current HAL is running under offloading.

TODO: BluetoothA2dp@1.0 is deprecated, and there would be UIPC and
BluetoothAudio@2.0 after related code removed.

Bug: 143922003
Test: Switching A2DP active devices and codec configurations
Change-Id: Iaa35013aa9dd4c928437b68bfa317696b8fad633
parent caa62c8b
Loading
Loading
Loading
Loading
+9 −4
Original line number Original line Diff line number Diff line
@@ -60,6 +60,7 @@ bool btif_a2dp_on_started(const RawAddress& peer_addr, tBTA_AV_START* p_av_start
    if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
    if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
      bluetooth::audio::a2dp::ack_stream_started(status);
      bluetooth::audio::a2dp::ack_stream_started(status);
    } else if (btif_av_is_a2dp_offload_enabled()) {
    } else if (btif_av_is_a2dp_offload_enabled()) {
      // TODO: BluetoothA2dp@1.0 is deprecated
      btif_a2dp_audio_on_started(status);
      btif_a2dp_audio_on_started(status);
    } else {
    } else {
      btif_a2dp_command_ack(status);
      btif_a2dp_command_ack(status);
@@ -75,7 +76,7 @@ bool btif_a2dp_on_started(const RawAddress& peer_addr, tBTA_AV_START* p_av_start
      LOG(WARNING) << __func__ << ": peer " << peer_addr << " A2DP is suspending and ignores the started event";
      LOG(WARNING) << __func__ << ": peer " << peer_addr << " A2DP is suspending and ignores the started event";
      return false;
      return false;
    }
    }
    if (btif_av_is_a2dp_offload_enabled()) {
    if (btif_av_is_a2dp_offload_running()) {
      btif_av_stream_start_offload();
      btif_av_stream_start_offload();
    } else if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
    } else if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
      if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
      if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
@@ -98,6 +99,7 @@ bool btif_a2dp_on_started(const RawAddress& peer_addr, tBTA_AV_START* p_av_start
    if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
    if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
      bluetooth::audio::a2dp::ack_stream_started(A2DP_CTRL_ACK_FAILURE);
      bluetooth::audio::a2dp::ack_stream_started(A2DP_CTRL_ACK_FAILURE);
    } else if (btif_av_is_a2dp_offload_enabled()) {
    } else if (btif_av_is_a2dp_offload_enabled()) {
      // TODO: BluetoothA2dp@1.0 is deprecated
      btif_a2dp_audio_on_started(p_av_start->status);
      btif_a2dp_audio_on_started(p_av_start->status);
    } else {
    } else {
      btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
      btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
@@ -116,9 +118,10 @@ void btif_a2dp_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) {
    return;
    return;
  }
  }
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
      !btif_av_is_a2dp_offload_enabled()) {
      !btif_av_is_a2dp_offload_running()) {
    btif_a2dp_source_on_stopped(p_av_suspend);
    btif_a2dp_source_on_stopped(p_av_suspend);
  } else if (p_av_suspend != NULL) {
  } else if (p_av_suspend != NULL) {
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_on_stopped(p_av_suspend->status);
    btif_a2dp_audio_on_stopped(p_av_suspend->status);
  }
  }
}
}
@@ -131,9 +134,10 @@ void btif_a2dp_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) {
    return;
    return;
  }
  }
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled() ||
      !btif_av_is_a2dp_offload_enabled()) {
      !btif_av_is_a2dp_offload_running()) {
    btif_a2dp_source_on_suspended(p_av_suspend);
    btif_a2dp_source_on_suspended(p_av_suspend);
  } else if (p_av_suspend != NULL) {
  } else if (p_av_suspend != NULL) {
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_on_suspended(p_av_suspend->status);
    btif_a2dp_audio_on_suspended(p_av_suspend->status);
  }
  }
}
}
@@ -159,7 +163,7 @@ void btif_a2dp_on_offload_started(const RawAddress& peer_addr,
      ack = A2DP_CTRL_ACK_FAILURE;
      ack = A2DP_CTRL_ACK_FAILURE;
      break;
      break;
  }
  }
  if (btif_av_is_a2dp_offload_enabled()) {
  if (btif_av_is_a2dp_offload_running()) {
    if (ack != BTA_AV_SUCCESS && btif_av_stream_started_ready()) {
    if (ack != BTA_AV_SUCCESS && btif_av_stream_started_ready()) {
      // Offload request will return with failure from btif_av sm if
      // Offload request will return with failure from btif_av sm if
      // suspend is triggered for remote start. Disconnect only if SoC
      // suspend is triggered for remote start. Disconnect only if SoC
@@ -173,6 +177,7 @@ void btif_a2dp_on_offload_started(const RawAddress& peer_addr,
    bluetooth::audio::a2dp::ack_stream_started(ack);
    bluetooth::audio::a2dp::ack_stream_started(ack);
  } else {
  } else {
    btif_a2dp_command_ack(ack);
    btif_a2dp_command_ack(ack);
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_on_started(status);
    btif_a2dp_audio_on_started(status);
  }
  }
}
}
+24 −17
Original line number Original line Diff line number Diff line
@@ -359,6 +359,7 @@ static void btif_a2dp_source_startup_delayed() {
  }
  }
  if (!bluetooth::audio::a2dp::init(&btif_a2dp_source_thread)) {
  if (!bluetooth::audio::a2dp::init(&btif_a2dp_source_thread)) {
    if (btif_av_is_a2dp_offload_enabled()) {
    if (btif_av_is_a2dp_offload_enabled()) {
      // TODO: BluetoothA2dp@1.0 is deprecated
      LOG(WARNING) << __func__ << ": Using BluetoothA2dp HAL";
      LOG(WARNING) << __func__ << ": Using BluetoothA2dp HAL";
    } else {
    } else {
      LOG(WARNING) << __func__ << ": Using legacy HAL";
      LOG(WARNING) << __func__ << ": Using legacy HAL";
@@ -401,6 +402,7 @@ static void btif_a2dp_source_start_session_delayed(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
        bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
        bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
  } else if (btif_av_is_a2dp_offload_enabled()) {
  } else if (btif_av_is_a2dp_offload_enabled()) {
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_interface_start_session();
    btif_a2dp_audio_interface_start_session();
  } else {
  } else {
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
@@ -467,6 +469,7 @@ static void btif_a2dp_source_end_session_delayed(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
        bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
        bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
  } else if (btif_av_is_a2dp_offload_enabled()) {
  } else if (btif_av_is_a2dp_offload_enabled()) {
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_interface_end_session();
    btif_a2dp_audio_interface_end_session();
  } else {
  } else {
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
    BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
@@ -501,6 +504,7 @@ static void btif_a2dp_source_shutdown_delayed(void) {
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
  if (bluetooth::audio::a2dp::is_hal_2_0_enabled()) {
    bluetooth::audio::a2dp::cleanup();
    bluetooth::audio::a2dp::cleanup();
  } else if (btif_av_is_a2dp_offload_enabled()) {
  } else if (btif_av_is_a2dp_offload_enabled()) {
    // TODO: BluetoothA2dp@1.0 is deprecated
    btif_a2dp_audio_interface_end_session();
    btif_a2dp_audio_interface_end_session();
  } else {
  } else {
    btif_a2dp_control_cleanup();
    btif_a2dp_control_cleanup();
@@ -718,7 +722,7 @@ void btif_a2dp_source_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) {
        btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
        btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
      }
      }
    }
    }
  } else if (btif_av_is_a2dp_offload_enabled()) {
  } else if (btif_av_is_a2dp_offload_running()) {
    bluetooth::audio::a2dp::ack_stream_suspended(A2DP_CTRL_ACK_SUCCESS);
    bluetooth::audio::a2dp::ack_stream_suspended(A2DP_CTRL_ACK_SUCCESS);
    return;
    return;
  }
  }
@@ -754,7 +758,7 @@ void btif_a2dp_source_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) {
        btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
        btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
      }
      }
    }
    }
  } else if (btif_av_is_a2dp_offload_enabled()) {
  } else if (btif_av_is_a2dp_offload_running()) {
    bluetooth::audio::a2dp::ack_stream_suspended(A2DP_CTRL_ACK_SUCCESS);
    bluetooth::audio::a2dp::ack_stream_suspended(A2DP_CTRL_ACK_SUCCESS);
    return;
    return;
  }
  }
@@ -776,13 +780,13 @@ void btif_a2dp_source_set_tx_flush(bool enable) {
}
}


static void btif_a2dp_source_audio_tx_start_event(void) {
static void btif_a2dp_source_audio_tx_start_event(void) {
  LOG_INFO(LOG_TAG, "%s: media_alarm is %srunning, streaming %s state=%s",
  LOG_INFO(
           __func__,
      LOG_TAG, "%s: media_alarm is %s, streaming %s state=%s", __func__,
           btif_a2dp_source_cb.media_alarm.IsScheduled() ? "" : "not ",
      btif_a2dp_source_cb.media_alarm.IsScheduled() ? "running" : "stopped",
      btif_a2dp_source_is_streaming() ? "true" : "false",
      btif_a2dp_source_is_streaming() ? "true" : "false",
      btif_a2dp_source_cb.StateStr().c_str());
      btif_a2dp_source_cb.StateStr().c_str());


  if (btif_av_is_a2dp_offload_enabled()) return;
  if (btif_av_is_a2dp_offload_running()) return;


  /* Reset the media feeding state */
  /* Reset the media feeding state */
  CHECK(btif_a2dp_source_cb.encoder_interface != nullptr);
  CHECK(btif_a2dp_source_cb.encoder_interface != nullptr);
@@ -792,6 +796,9 @@ static void btif_a2dp_source_audio_tx_start_event(void) {
      "%s: starting timer %" PRIu64 " ms", __func__,
      "%s: starting timer %" PRIu64 " ms", __func__,
      btif_a2dp_source_cb.encoder_interface->get_encoder_interval_ms());
      btif_a2dp_source_cb.encoder_interface->get_encoder_interval_ms());


  /* audio engine starting, reset tx suspended flag */
  btif_a2dp_source_cb.tx_flush = false;

  wakelock_acquire();
  wakelock_acquire();
  btif_a2dp_source_cb.media_alarm.SchedulePeriodic(
  btif_a2dp_source_cb.media_alarm.SchedulePeriodic(
      btif_a2dp_source_thread.GetWeakPtr(), FROM_HERE,
      btif_a2dp_source_thread.GetWeakPtr(), FROM_HERE,
@@ -816,13 +823,13 @@ static void btif_a2dp_source_audio_tx_start_event(void) {
}
}


static void btif_a2dp_source_audio_tx_stop_event(void) {
static void btif_a2dp_source_audio_tx_stop_event(void) {
  LOG_INFO(LOG_TAG, "%s: media_alarm is %srunning, streaming %s state=%s",
  LOG_INFO(
           __func__,
      LOG_TAG, "%s: media_alarm is %s, streaming %s state=%s", __func__,
           btif_a2dp_source_cb.media_alarm.IsScheduled() ? "" : "not ",
      btif_a2dp_source_cb.media_alarm.IsScheduled() ? "running" : "stopped",
      btif_a2dp_source_is_streaming() ? "true" : "false",
      btif_a2dp_source_is_streaming() ? "true" : "false",
      btif_a2dp_source_cb.StateStr().c_str());
      btif_a2dp_source_cb.StateStr().c_str());


  if (btif_av_is_a2dp_offload_enabled()) return;
  if (btif_av_is_a2dp_offload_running()) return;


  btif_a2dp_source_cb.stats.session_end_us =
  btif_a2dp_source_cb.stats.session_end_us =
      bluetooth::common::time_get_os_boottime_us();
      bluetooth::common::time_get_os_boottime_us();
@@ -876,7 +883,7 @@ static void btif_a2dp_source_audio_tx_stop_event(void) {
}
}


static void btif_a2dp_source_audio_handle_timer(void) {
static void btif_a2dp_source_audio_handle_timer(void) {
  if (btif_av_is_a2dp_offload_enabled()) return;
  if (btif_av_is_a2dp_offload_running()) return;


  uint64_t timestamp_us = bluetooth::common::time_get_os_boottime_us();
  uint64_t timestamp_us = bluetooth::common::time_get_os_boottime_us();
  log_tstamps_us("A2DP Source tx timer", timestamp_us);
  log_tstamps_us("A2DP Source tx timer", timestamp_us);
@@ -1029,7 +1036,7 @@ static void btif_a2dp_source_audio_tx_flush_event(void) {
  /* Flush all enqueued audio buffers (encoded) */
  /* Flush all enqueued audio buffers (encoded) */
  LOG_INFO(LOG_TAG, "%s: state=%s", __func__,
  LOG_INFO(LOG_TAG, "%s: state=%s", __func__,
           btif_a2dp_source_cb.StateStr().c_str());
           btif_a2dp_source_cb.StateStr().c_str());
  if (btif_av_is_a2dp_offload_enabled()) return;
  if (btif_av_is_a2dp_offload_running()) return;


  if (btif_a2dp_source_cb.encoder_interface != nullptr)
  if (btif_a2dp_source_cb.encoder_interface != nullptr)
    btif_a2dp_source_cb.encoder_interface->feeding_flush();
    btif_a2dp_source_cb.encoder_interface->feeding_flush();
@@ -1289,7 +1296,7 @@ static void btif_a2dp_source_update_metrics(void) {
  SchedulingStats enqueue_stats = stats.tx_queue_enqueue_stats;
  SchedulingStats enqueue_stats = stats.tx_queue_enqueue_stats;
  A2dpSessionMetrics metrics;
  A2dpSessionMetrics metrics;
  metrics.codec_index = stats.codec_index;
  metrics.codec_index = stats.codec_index;
  metrics.is_a2dp_offload = btif_av_is_a2dp_offload_enabled();
  metrics.is_a2dp_offload = btif_av_is_a2dp_offload_running();
  // session_start_us is 0 when btif_a2dp_source_start_audio_req() is not called
  // session_start_us is 0 when btif_a2dp_source_start_audio_req() is not called
  // mark the metric duration as invalid (-1) in this case
  // mark the metric duration as invalid (-1) in this case
  if (stats.session_start_us != 0) {
  if (stats.session_start_us != 0) {