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

Commit 0d2ddd15 authored by Henri Chataing's avatar Henri Chataing Committed by Gerrit Code Review
Browse files

Merge "btif_av: Make public the methods from btav_sink_interface_t" into main

parents 9afdc647 fe7b5a98
Loading
Loading
Loading
Loading
+21 −39
Original line number Diff line number Diff line
@@ -16,11 +16,13 @@

#define LOG_TAG "BluetoothA2dpSinkServiceJni"

#include <string.h>
#include <bluetooth/log.h>

#include <cstring>
#include <mutex>
#include <shared_mutex>

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

@@ -29,7 +31,6 @@ static jmethodID method_onConnectionStateChanged;
static jmethodID method_onAudioStateChanged;
static jmethodID method_onAudioConfigChanged;

static const btav_sink_interface_t* sBluetoothA2dpInterface = NULL;
static jobject mCallbacksObj = NULL;
static std::shared_timed_mutex callbacks_mutex;

@@ -119,11 +120,7 @@ static void initNative(JNIEnv* env, jobject object,
    return;
  }

  if (sBluetoothA2dpInterface != NULL) {
    log::warn("Cleaning up A2DP Interface before initializing...");
    sBluetoothA2dpInterface->cleanup();
    sBluetoothA2dpInterface = NULL;
  }
  btif_av_sink_cleanup();

  if (mCallbacksObj != NULL) {
    log::warn("Cleaning up A2DP callback object");
@@ -131,20 +128,11 @@ static void initNative(JNIEnv* env, jobject object,
    mCallbacksObj = NULL;
  }

  sBluetoothA2dpInterface =
      (btav_sink_interface_t*)btInf->get_profile_interface(
          BT_PROFILE_ADVANCED_AUDIO_SINK_ID);
  if (sBluetoothA2dpInterface == NULL) {
    log::error("Failed to get Bluetooth A2DP Sink Interface");
    return;
  }

  bt_status_t status = sBluetoothA2dpInterface->init(&sBluetoothA2dpCallbacks,
                                                     maxConnectedAudioDevices);
  bt_status_t status =
      btif_av_sink_init(&sBluetoothA2dpCallbacks, maxConnectedAudioDevices);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed to initialize Bluetooth A2DP Sink, status: {}",
               bt_status_text(status));
    sBluetoothA2dpInterface = NULL;
    return;
  }

@@ -160,10 +148,7 @@ static void cleanupNative(JNIEnv* env, jobject /* object */) {
    return;
  }

  if (sBluetoothA2dpInterface != NULL) {
    sBluetoothA2dpInterface->cleanup();
    sBluetoothA2dpInterface = NULL;
  }
  btif_av_sink_cleanup();

  if (mCallbacksObj != NULL) {
    env->DeleteGlobalRef(mCallbacksObj);
@@ -173,9 +158,6 @@ static void cleanupNative(JNIEnv* env, jobject /* object */) {

static jboolean connectA2dpNative(JNIEnv* env, jobject /* object */,
                                  jbyteArray address) {
  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));
  if (!sBluetoothA2dpInterface) return JNI_FALSE;

  jbyte* addr = env->GetByteArrayElements(address, NULL);
  if (!addr) {
    jniThrowIOException(env, EINVAL);
@@ -184,18 +166,19 @@ static jboolean connectA2dpNative(JNIEnv* env, jobject /* object */,

  RawAddress bd_addr;
  bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  bt_status_t status = sBluetoothA2dpInterface->connect(bd_addr);

  log::info("{}", bd_addr);
  bt_status_t status = btif_av_sink_connect(bd_addr);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed HF connection, status: {}", bt_status_text(status));
  }

  env->ReleaseByteArrayElements(address, addr, 0);
  return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

static jboolean disconnectA2dpNative(JNIEnv* env, jobject /* object */,
                                     jbyteArray address) {
  if (!sBluetoothA2dpInterface) return JNI_FALSE;

  jbyte* addr = env->GetByteArrayElements(address, NULL);
  if (!addr) {
    jniThrowIOException(env, EINVAL);
@@ -204,32 +187,29 @@ static jboolean disconnectA2dpNative(JNIEnv* env, jobject /* object */,

  RawAddress bd_addr;
  bd_addr.FromOctets(reinterpret_cast<const uint8_t*>(addr));
  bt_status_t status = sBluetoothA2dpInterface->disconnect(bd_addr);

  log::info("{}", bd_addr);
  bt_status_t status = btif_av_sink_disconnect(bd_addr);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed HF disconnection, status: {}", bt_status_text(status));
  }

  env->ReleaseByteArrayElements(address, addr, 0);
  return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

static void informAudioFocusStateNative(JNIEnv* /* env */, jobject /* object */,
                                        jint focus_state) {
  if (!sBluetoothA2dpInterface) return;
  sBluetoothA2dpInterface->set_audio_focus_state((uint8_t)focus_state);
  btif_av_sink_set_audio_focus_state((uint8_t)focus_state);
}

static void informAudioTrackGainNative(JNIEnv* /* env */, jobject /* object */,
                                       jfloat gain) {
  if (!sBluetoothA2dpInterface) return;
  sBluetoothA2dpInterface->set_audio_track_gain((float)gain);
  btif_av_sink_set_audio_track_gain((float)gain);
}

static jboolean setActiveDeviceNative(JNIEnv* env, jobject /* object */,
                                      jbyteArray address) {
  if (!sBluetoothA2dpInterface) return JNI_FALSE;

  log::info("sBluetoothA2dpInterface: {}", fmt::ptr(sBluetoothA2dpInterface));

  jbyte* addr = env->GetByteArrayElements(address, NULL);
  if (!addr) {
    jniThrowIOException(env, EINVAL);
@@ -238,13 +218,15 @@ static jboolean setActiveDeviceNative(JNIEnv* env, jobject /* object */,

  RawAddress rawAddress;
  rawAddress.FromOctets((uint8_t*)addr);
  bt_status_t status = sBluetoothA2dpInterface->set_active_device(rawAddress);

  log::info("{}", rawAddress);
  bt_status_t status = btif_av_sink_set_active_device(rawAddress);
  if (status != BT_STATUS_SUCCESS) {
    log::error("Failed sending passthru command, status: {}",
               bt_status_text(status));
  }
  env->ReleaseByteArrayElements(address, addr, 0);

  env->ReleaseByteArrayElements(address, addr, 0);
  return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
}

+11 −0
Original line number Diff line number Diff line
@@ -46,6 +46,17 @@ bt_status_t btif_av_source_set_codec_config_preference(
    std::vector<btav_a2dp_codec_config_t> codec_preferences);
void btif_av_source_cleanup();

/* Interface methods for the A2DP sink stack. */

bt_status_t btif_av_sink_init(btav_sink_callbacks_t* callbacks,
                              int max_connected_audio_devices);
bt_status_t btif_av_sink_connect(const RawAddress& peer_address);
bt_status_t btif_av_sink_disconnect(const RawAddress& peer_address);
void btif_av_sink_cleanup();
void btif_av_sink_set_audio_focus_state(int focus_state);
void btif_av_sink_set_audio_track_gain(float gain);
bt_status_t btif_av_sink_set_active_device(const RawAddress& peer_address);

/**
 * Enum to represent the type of local a2dp profile.
 */
+0 −5
Original line number Diff line number Diff line
@@ -141,8 +141,6 @@ bool is_local_device_atv = false;

/* handsfree profile - client */
extern const bthf_client_interface_t* btif_hf_client_get_interface();
/* advanced audio profile */
extern const btav_sink_interface_t* btif_av_get_sink_interface();
/*rfc l2cap*/
extern const btsock_interface_t* btif_sock_get_interface();
/* hid host profile */
@@ -888,9 +886,6 @@ static const void* get_profile_interface(const char* profile_id) {
  if (is_profile(profile_id, BT_PROFILE_PAN_ID))
    return btif_pan_get_interface();

  if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
    return btif_av_get_sink_interface();

  if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
    return btif_hh_get_interface();

+14 −33
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@
#include <frameworks/proto_logging/stats/enums/bluetooth/a2dp/enums.pb.h>
#include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>

#include <chrono>
#include <cstdint>
#include <future>
#include <mutex>
@@ -53,7 +52,6 @@
#include "device/include/device_iot_config.h"
#include "hardware/bt_av.h"
#include "include/hardware/bt_rc.h"
#include "internal_include/bt_trace.h"
#include "osi/include/alarm.h"
#include "osi/include/allocator.h"
#include "stack/include/avrc_api.h"
@@ -143,7 +141,6 @@ class BtifAvEvent {
};

class BtifAvPeer;
static bt_status_t sink_set_active_device(const RawAddress& peer_address);

static void btif_av_sink_delete_active_peer(void);
static void btif_av_source_delete_active_peer(void);
@@ -816,7 +813,6 @@ static BtifAvSink btif_av_sink;
    btif_rc_handler(e, d);         \
  } break;

static bt_status_t sink_disconnect_src(const RawAddress& peer_address);
static void btif_av_source_dispatch_sm_event(const RawAddress& peer_address,
                                             btif_av_sm_event_t event);
static void btif_av_sink_dispatch_sm_event(const RawAddress& peer_address,
@@ -1849,7 +1845,7 @@ bool BtifAvStateMachine::StateIdle::ProcessEvent(uint32_t event, void* p_data) {
        if (!can_connect) btif_av_source_disconnect(peer_.PeerAddress());
      } else if (peer_.IsSource()) {
        can_connect = btif_av_sink.AllowedToConnect(peer_.PeerAddress());
        if (!can_connect) sink_disconnect_src(peer_.PeerAddress());
        if (!can_connect) btif_av_sink_disconnect(peer_.PeerAddress());
      }
      if (!can_connect) {
        log::error("Cannot connect to peer {}: too many connected peers",
@@ -1910,7 +1906,7 @@ bool BtifAvStateMachine::StateIdle::ProcessEvent(uint32_t event, void* p_data) {
          if (btif_av_src_sink_coexist_enabled())
            BTA_AvCloseRc(((tBTA_AV*)p_data)->rc_open.rc_handle);
          else
            sink_disconnect_src(peer_.PeerAddress());
            btif_av_sink_disconnect(peer_.PeerAddress());
        }
      }
      if (!can_connect) {
@@ -2031,7 +2027,7 @@ bool BtifAvStateMachine::StateIdle::ProcessEvent(uint32_t event, void* p_data) {
          if (peer_.IsSink()) {
            btif_av_source_disconnect(peer_.PeerAddress());
          } else if (peer_.IsSource()) {
            sink_disconnect_src(peer_.PeerAddress());
            btif_av_sink_disconnect(peer_.PeerAddress());
          }

          btif_report_connection_state(
@@ -2235,7 +2231,7 @@ bool BtifAvStateMachine::StateOpening::ProcessEvent(uint32_t event,
            if (!can_connect) btif_av_source_disconnect(peer_.PeerAddress());
          } else if (peer_.IsSource()) {
            can_connect = btif_av_sink.AllowedToConnect(peer_.PeerAddress());
            if (!can_connect) sink_disconnect_src(peer_.PeerAddress());
            if (!can_connect) btif_av_sink_disconnect(peer_.PeerAddress());
          }
        }
        /** @} */
@@ -2567,7 +2563,7 @@ bool BtifAvStateMachine::StateOpened::ProcessEvent(uint32_t event,
        if (peer_.IsSink()) {
          btif_av_source_disconnect(peer_.PeerAddress());
        } else if (peer_.IsSource()) {
          sink_disconnect_src(peer_.PeerAddress());
          btif_av_sink_disconnect(peer_.PeerAddress());
        }
        break;
      }
@@ -2637,7 +2633,7 @@ void BtifAvStateMachine::StateStarted::OnEnter() {
  btif_a2dp_sink_set_rx_flush(false);

  // Report that we have entered the Streaming stage. Usually, this should
  // be followed by focus grant. See update_audio_focus_state()
  // be followed by focus grant. See set_audio_focus_state()
  btif_report_audio_state(
      peer_.PeerAddress(), BTAV_AUDIO_STATE_STARTED,
      peer_.IsSource() ? A2dpType::kSink : A2dpType::kSource);
@@ -3588,20 +3584,20 @@ bt_status_t btif_av_source_init(
}

// Initializes the AV interface for sink mode
static bt_status_t init_sink(btav_sink_callbacks_t* callbacks,
bt_status_t btif_av_sink_init(btav_sink_callbacks_t* callbacks,
                              int max_connected_audio_devices) {
  log::info("");
  return btif_av_sink.Init(callbacks, max_connected_audio_devices);
}

// Updates the final focus state reported by components calling this module
static void update_audio_focus_state(int state) {
void btif_av_sink_set_audio_focus_state(int state) {
  log::info("state={}", state);
  btif_a2dp_sink_set_focus_state_req((btif_a2dp_sink_focus_state_t)state);
}

// Updates the track gain (used for ducking).
static void update_audio_track_gain(float gain) {
void btif_av_sink_set_audio_track_gain(float gain) {
  log::info("gain={:f}", gain);
  btif_a2dp_sink_set_audio_track_gain(gain);
}
@@ -3698,7 +3694,7 @@ bt_status_t btif_av_source_connect(const RawAddress& peer_address) {
                            connect_int);
}

static bt_status_t sink_connect_src(const RawAddress& peer_address) {
bt_status_t btif_av_sink_connect(const RawAddress& peer_address) {
  log::info("peer={}", peer_address);

  if (!btif_av_sink.Enabled()) {
@@ -3728,7 +3724,7 @@ bt_status_t btif_av_source_disconnect(const RawAddress& peer_address) {
                     peer_address, kBtaHandleUnknown, btif_av_event));
}

static bt_status_t sink_disconnect_src(const RawAddress& peer_address) {
bt_status_t btif_av_sink_disconnect(const RawAddress& peer_address) {
  log::info("peer={}", peer_address);

  if (!btif_av_sink.Enabled()) {
@@ -3745,7 +3741,7 @@ static bt_status_t sink_disconnect_src(const RawAddress& peer_address) {
                     peer_address, kBtaHandleUnknown, btif_av_event));
}

static bt_status_t sink_set_active_device(const RawAddress& peer_address) {
bt_status_t btif_av_sink_set_active_device(const RawAddress& peer_address) {
  log::info("peer={}", peer_address);

  if (!btif_av_sink.Enabled()) {
@@ -3841,22 +3837,12 @@ void btif_av_source_cleanup(void) {
                                   base::Unretained(&btif_av_source)));
}

static void cleanup_sink(void) {
void btif_av_sink_cleanup(void) {
  log::info("");
  do_in_main_thread(FROM_HERE, base::BindOnce(&BtifAvSink::Cleanup,
                                              base::Unretained(&btif_av_sink)));
}

static const btav_sink_interface_t bt_av_sink_interface = {
    sizeof(btav_sink_interface_t),
    init_sink,
    sink_connect_src,
    sink_disconnect_src,
    cleanup_sink,
    update_audio_focus_state,
    update_audio_track_gain,
    sink_set_active_device};

RawAddress btif_av_source_active_peer(void) {
  return btif_av_source.ActivePeer();
}
@@ -4057,11 +4043,6 @@ bt_status_t btif_av_sink_execute_service(bool enable) {
  return BT_STATUS_SUCCESS;
}

// Get the AV callback interface for A2DP sink profile
const btav_sink_interface_t* btif_av_get_sink_interface(void) {
  return &bt_av_sink_interface;
}

bool btif_av_is_connected(const A2dpType local_a2dp_type) {
  BtifAvPeer* peer = btif_av_find_active_peer(local_a2dp_type);
  if (peer == nullptr) {
+6 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <memory>

#include "include/hardware/bluetooth.h"
#include "btif/include/btif_av.h"
#include "rust/cxx.h"
#include "src/profiles/a2dp.rs.h"
#include "types/raw_address.h"
@@ -69,26 +70,25 @@ std::unique_ptr<A2dpSinkIntf> GetA2dpSinkProfile(const unsigned char* btif) {

  const bt_interface_t* btif_ = reinterpret_cast<const bt_interface_t*>(btif);

  auto a2dp_sink = std::make_unique<A2dpSinkIntf>(
      reinterpret_cast<const btav_sink_interface_t*>(btif_->get_profile_interface("a2dp_sink")));
  auto a2dp_sink = std::make_unique<A2dpSinkIntf>();
  internal::g_a2dp_sink_if = a2dp_sink.get();
  return a2dp_sink;
}

int A2dpSinkIntf::init() const {
  return intf_->init(&internal::g_a2dp_sink_callbacks, 1);
  return btif_av_sink_init(&internal::g_a2dp_sink_callbacks, 1);
}

int A2dpSinkIntf::connect(RawAddress addr) const {
  return intf_->connect(addr);
  return btif_av_sink_connect(addr);
}

int A2dpSinkIntf::disconnect(RawAddress addr) const {
  return intf_->disconnect(addr);
  return btif_av_sink_disconnect(addr);
}

int A2dpSinkIntf::set_active_device(RawAddress addr) const {
  return intf_->set_active_device(addr);
  return btif_av_sink_set_active_device(addr);
}

void A2dpSinkIntf::cleanup() const {
Loading