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

Commit 585961b1 authored by Jakub Rotkiewicz's avatar Jakub Rotkiewicz Committed by Gerrit Code Review
Browse files

Merge "jni/a2dp: Refactor logging" into main

parents 3765d138 a39da0f3
Loading
Loading
Loading
Loading
+21 −10
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@

#include <shared_mutex>

#include "btif/include/btif_util.h"
#include "com_android_bluetooth.h"
#include "hardware/bt_av.h"

@@ -53,7 +54,7 @@ static std::shared_timed_mutex callbacks_mutex;
static void bta2dp_connection_state_callback(const RawAddress& bd_addr,
                                             btav_connection_state_t state,
                                             const btav_error_t& /* error */) {
  log::info("");
  log::info("{}: state: {}", bd_addr, dump_av_conn_state(state));

  std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
  CallbackEnv sCallbackEnv(__func__);
@@ -75,7 +76,7 @@ static void bta2dp_connection_state_callback(const RawAddress& bd_addr,

static void bta2dp_audio_state_callback(const RawAddress& bd_addr,
                                        btav_audio_state_t state) {
  log::info("");
  log::info("{}: state: {}", bd_addr, dump_av_audio_state(state));

  std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
  CallbackEnv sCallbackEnv(__func__);
@@ -99,7 +100,11 @@ static void bta2dp_audio_config_callback(
    const RawAddress& bd_addr, btav_a2dp_codec_config_t codec_config,
    std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
    std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities) {
  log::info("");
  log::info(
      "{}: codec: {}, local codecs: {}, selectable codecs: {}", bd_addr,
      codec_config.CodecNameStr(),
      btav_a2dp_codec_config_t::PrintCodecs(codecs_local_capabilities),
      btav_a2dp_codec_config_t::PrintCodecs(codecs_selectable_capabilities));

  std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
  CallbackEnv sCallbackEnv(__func__);
@@ -165,7 +170,7 @@ static void bta2dp_audio_config_callback(

static bool bta2dp_mandatory_codec_preferred_callback(
    const RawAddress& bd_addr) {
  log::info("");
  log::info("{}", bd_addr);

  std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
  CallbackEnv sCallbackEnv(__func__);
@@ -370,7 +375,6 @@ static jobjectArray getSupportedCodecTypesNative(JNIEnv* env) {

static jboolean connectA2dpNative(JNIEnv* env, jobject /* object */,
                                  jbyteArray address) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sBluetoothA2dpInterface) {
    log::error("Failed to get the Bluetooth A2DP Interface");
@@ -385,6 +389,8 @@ static jboolean connectA2dpNative(JNIEnv* env, jobject /* object */,

  RawAddress bd_addr;
  bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  log::info("sBluetoothA2dpInterface: {}, {}",
            fmt::ptr(sBluetoothA2dpInterface), bd_addr);
  bt_status_t status = sBluetoothA2dpInterface->connect(bd_addr);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed A2DP connection, status: {}", bt_status_text(status));
@@ -395,7 +401,6 @@ static jboolean connectA2dpNative(JNIEnv* env, jobject /* object */,

static jboolean disconnectA2dpNative(JNIEnv* env, jobject /* object */,
                                     jbyteArray address) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sBluetoothA2dpInterface) {
    log::error("Failed to get the Bluetooth A2DP Interface");
@@ -410,6 +415,8 @@ static jboolean disconnectA2dpNative(JNIEnv* env, jobject /* object */,

  RawAddress bd_addr;
  bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  log::info("sBluetoothA2dpInterface: {}, {}",
            fmt::ptr(sBluetoothA2dpInterface), bd_addr);
  bt_status_t status = sBluetoothA2dpInterface->disconnect(bd_addr);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed A2DP disconnection, status: {}", bt_status_text(status));
@@ -420,7 +427,6 @@ static jboolean disconnectA2dpNative(JNIEnv* env, jobject /* object */,

static jboolean setSilenceDeviceNative(JNIEnv* env, jobject /* object */,
                                       jbyteArray address, jboolean silence) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sBluetoothA2dpInterface) {
    log::error("Failed to get the Bluetooth A2DP Interface");
@@ -436,6 +442,9 @@ static jboolean setSilenceDeviceNative(JNIEnv* env, jobject /* object */,
  if (bd_addr == RawAddress::kEmpty) {
    return JNI_FALSE;
  }
  log::info("sBluetoothA2dpInterface: {}, {}: silence: {}",
            fmt::ptr(sBluetoothA2dpInterface), bd_addr,
            static_cast<bool>(silence));
  bt_status_t status =
      sBluetoothA2dpInterface->set_silence_device(bd_addr, silence);
  if (status != BT_STATUS_SUCCESS) {
@@ -448,7 +457,6 @@ static jboolean setSilenceDeviceNative(JNIEnv* env, jobject /* object */,

static jboolean setActiveDeviceNative(JNIEnv* env, jobject /* object */,
                                      jbyteArray address) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sBluetoothA2dpInterface) {
    log::error("Failed to get the Bluetooth A2DP Interface");
@@ -461,6 +469,8 @@ static jboolean setActiveDeviceNative(JNIEnv* env, jobject /* object */,
  if (addr) {
    bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  }
  log::info("sBluetoothA2dpInterface: {}, {}",
            fmt::ptr(sBluetoothA2dpInterface), bd_addr);
  bt_status_t status = sBluetoothA2dpInterface->set_active_device(bd_addr);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed A2DP set_active_device, status: {}",
@@ -473,7 +483,6 @@ static jboolean setActiveDeviceNative(JNIEnv* env, jobject /* object */,
static jboolean setCodecConfigPreferenceNative(JNIEnv* env, jobject object,
                                               jbyteArray address,
                                               jobjectArray codecConfigArray) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  std::shared_lock<std::shared_timed_mutex> lock(interface_mutex);
  if (!sBluetoothA2dpInterface) {
    log::error("Failed to get the Bluetooth A2DP Interface");
@@ -490,7 +499,9 @@ static jboolean setCodecConfigPreferenceNative(JNIEnv* env, jobject object,
  bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  std::vector<btav_a2dp_codec_config_t> codec_preferences =
      prepareCodecPreferences(env, object, codecConfigArray);

  log::info("sBluetoothA2dpInterface: {}, {}: {}",
            fmt::ptr(sBluetoothA2dpInterface), bd_addr,
            btav_a2dp_codec_config_t::PrintCodecs(codec_preferences));
  bt_status_t status =
      sBluetoothA2dpInterface->config_codec(bd_addr, codec_preferences);
  if (status != BT_STATUS_SUCCESS) {
+30 −30
Original line number Diff line number Diff line
@@ -168,52 +168,40 @@ struct btav_a2dp_codec_config_t {
  int64_t codec_specific_3;  // Codec-specific value 3
  int64_t codec_specific_4;  // Codec-specific value 4

  std::string ToString() const {
    std::string codec_name_str;

  std::string CodecNameStr() const {
    switch (codec_type) {
      case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
        codec_name_str = "SBC";
        break;
        return "SBC";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
        codec_name_str = "AAC";
        break;
        return "AAC";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
        codec_name_str = "aptX";
        break;
        return "aptX";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
        codec_name_str = "aptX HD";
        break;
        return "aptX HD";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
        codec_name_str = "LDAC";
        break;
        return "LDAC";
      case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
        codec_name_str = "SBC (Sink)";
        break;
        return "SBC (Sink)";
      case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
        codec_name_str = "AAC (Sink)";
        break;
        return "AAC (Sink)";
      case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
        codec_name_str = "LDAC (Sink)";
        break;
        return "LDAC (Sink)";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_LC3:
        codec_name_str = "LC3";
        break;
        return "LC3";
      case BTAV_A2DP_CODEC_INDEX_SINK_OPUS:
        codec_name_str = "Opus (Sink)";
        break;
        return "Opus (Sink)";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS:
        codec_name_str = "Opus";
        break;
        return "Opus";
      case BTAV_A2DP_CODEC_INDEX_MAX:
        codec_name_str = "Unknown(CODEC_INDEX_MAX)";
        break;
        return "Unknown(CODEC_INDEX_MAX)";
      case BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN:
      case BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN:
        codec_name_str = "Unknown(CODEC_EXT)";
        break;
        return "Unknown(CODEC_EXT)";
    }
    return "Unknown";
  }

  std::string ToString() const {
    std::string sample_rate_str;
    AppendCapability(sample_rate_str,
                     (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE");
@@ -267,7 +255,7 @@ struct btav_a2dp_codec_config_t {
                     (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
                     "STEREO");

    return "codec: " + codec_name_str +
    return "codec: " + CodecNameStr() +
           " priority: " + std::to_string(codec_priority) +
           " sample_rate: " + sample_rate_str +
           " bits_per_sample: " + bits_per_sample_str +
@@ -278,6 +266,18 @@ struct btav_a2dp_codec_config_t {
           " codec_specific_4: " + std::to_string(codec_specific_4);
  }

  static std::string PrintCodecs(std::vector<btav_a2dp_codec_config_t> codecs) {
    std::ostringstream oss;
    for (size_t i = 0; i < codecs.size(); i++) {
      oss << codecs[i].CodecNameStr();
      if (i != (codecs.size() - 1)) {
        oss << ", ";
      }
    }

    return oss.str();
  }

 private:
  static std::string AppendCapability(std::string& result, bool append,
                                      const std::string& name) {