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

Commit e02d711e authored by Jeremy Wu's avatar Jeremy Wu Committed by Gerrit Code Review
Browse files

Merge changes Ide1006b0,I03eb0c6a into main

* changes:
  HfpClientInterface: allow uni-directional audio stream on SCO
  HfpClientInterface: r/w logs to verbose level
parents 43871ee6 e3a7961f
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include "hfp_client_interface_aidl.h"

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

#include <map>

@@ -73,11 +74,19 @@ std::unordered_map<tBTA_AG_UUID_CODEC, ::hfp::sco_config> HfpTransport::GetHfpSc
  return providerInfo->GetHfpScoConfig();
}

HfpTransport::HfpTransport() { hfp_pending_cmd_ = HFP_CTRL_CMD_NONE; }
bool HfpTransport::IsStreamActive() { return is_stream_active; }

void HfpTransport::SetStreamActive(bool active) { is_stream_active = active; }

HfpTransport::HfpTransport() {
  hfp_pending_cmd_ = HFP_CTRL_CMD_NONE;
  is_stream_active = false;
}

BluetoothAudioCtrlAck HfpTransport::StartRequest() {
  if (hfp_pending_cmd_ == HFP_CTRL_CMD_START) {
    log::info("HFP_CTRL_CMD_START in progress");
    is_stream_active = true;
    return BluetoothAudioCtrlAck::PENDING;
  } else if (hfp_pending_cmd_ != HFP_CTRL_CMD_NONE) {
    log::warn("busy in pending_cmd={}", hfp_pending_cmd_);
@@ -91,6 +100,7 @@ BluetoothAudioCtrlAck HfpTransport::StartRequest() {

  if (bta_ag_sco_is_open(cb)) {
    // Already started, ACK back immediately.
    is_stream_active = true;
    return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
  }

@@ -108,11 +118,15 @@ BluetoothAudioCtrlAck HfpTransport::StartRequest() {
  if (ctrl_ack->second != BluetoothAudioCtrlAck::SUCCESS_FINISHED) {
    return ctrl_ack->second;
  }
  is_stream_active = true;
  return BluetoothAudioCtrlAck::PENDING;
}

void HfpTransport::StopRequest() {
  log::info("handling");

  is_stream_active = false;

  RawAddress addr = bta_ag_get_active_device();
  if (addr.IsEmpty()) {
    log::error("No active device found");
@@ -183,6 +197,13 @@ BluetoothAudioCtrlAck HfpDecodingTransport::StartRequest(bool is_low_latency) {
}

BluetoothAudioCtrlAck HfpDecodingTransport::SuspendRequest() {
  transport_->SetStreamActive(false);

  if (HfpEncodingTransport::instance_ && HfpEncodingTransport::instance_->IsStreamActive()) {
    log::info("SCO will suspend when encoding transport suspends.");
    return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
  }

  return transport_->SuspendRequest();
}

@@ -215,6 +236,8 @@ uint8_t HfpDecodingTransport::GetPendingCmd() const { return transport_->GetPend

void HfpDecodingTransport::ResetPendingCmd() { transport_->ResetPendingCmd(); }

bool HfpDecodingTransport::IsStreamActive() { return transport_->IsStreamActive(); }

void HfpDecodingTransport::StopRequest() { transport_->StopRequest(); }

HfpEncodingTransport::HfpEncodingTransport(SessionType session_type)
@@ -229,6 +252,13 @@ BluetoothAudioCtrlAck HfpEncodingTransport::StartRequest(bool is_low_latency) {
}

BluetoothAudioCtrlAck HfpEncodingTransport::SuspendRequest() {
  transport_->SetStreamActive(false);

  if (HfpDecodingTransport::instance_ && HfpDecodingTransport::instance_->IsStreamActive()) {
    log::info("SCO will suspend when decoding transport suspends.");
    return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
  }

  return transport_->SuspendRequest();
}

@@ -263,6 +293,8 @@ uint8_t HfpEncodingTransport::GetPendingCmd() const { return transport_->GetPend

void HfpEncodingTransport::ResetPendingCmd() { transport_->ResetPendingCmd(); }

bool HfpEncodingTransport::IsStreamActive() { return transport_->IsStreamActive(); }

}  // namespace hfp
}  // namespace aidl
}  // namespace audio
+10 −0
Original line number Diff line number Diff line
@@ -74,8 +74,14 @@ public:
  static std::unordered_map<tBTA_AG_UUID_CODEC, ::hfp::sco_config> GetHfpScoConfig(
          SessionType sessionType);

  bool IsStreamActive();

  void SetStreamActive(bool active);

private:
  tHFP_CTRL_CMD hfp_pending_cmd_;

  bool is_stream_active;
};

// Source transport implementation
@@ -108,6 +114,8 @@ public:

  void ResetPendingCmd();

  bool IsStreamActive();

  static inline HfpDecodingTransport* instance_ = nullptr;
  static inline BluetoothAudioSourceClientInterface* software_hal_interface = nullptr;
  static inline BluetoothAudioSourceClientInterface* offloading_hal_interface = nullptr;
@@ -146,6 +154,8 @@ public:

  void ResetPendingCmd();

  bool IsStreamActive();

  static inline HfpEncodingTransport* instance_ = nullptr;
  static inline BluetoothAudioSinkClientInterface* software_hal_interface = nullptr;
  static inline BluetoothAudioSinkClientInterface* offloading_hal_interface = nullptr;
+18 −4
Original line number Diff line number Diff line
@@ -190,10 +190,16 @@ size_t HfpClientInterface::Decode::Write(const uint8_t* p_buf, uint32_t len) {
    log::warn("Unsupported HIDL or AIDL version");
    return 0;
  }
  log::info("decode");
  log::verbose("decode");

  auto instance = aidl::hfp::HfpDecodingTransport::instance_;
  if (instance->IsStreamActive()) {
    return get_decode_client_interface()->WriteAudioData(p_buf, len);
  }

  return len;
}

void HfpClientInterface::Decode::ConfirmStreamingRequest() {
  auto instance = aidl::hfp::HfpDecodingTransport::instance_;
  auto pending_cmd = instance->GetPendingCmd();
@@ -351,10 +357,18 @@ size_t HfpClientInterface::Encode::Read(uint8_t* p_buf, uint32_t len) {
    log::warn("Unsupported HIDL or AIDL version");
    return 0;
  }
  log::info("encode");
  log::verbose("encode");

  auto instance = aidl::hfp::HfpEncodingTransport::instance_;
  if (instance->IsStreamActive()) {
    return get_encode_client_interface()->ReadAudioData(p_buf, len);
  }

  memset(p_buf, 0x00, len);

  return len;
}

void HfpClientInterface::Encode::ConfirmStreamingRequest() {
  auto instance = aidl::hfp::HfpEncodingTransport::instance_;
  auto pending_cmd = instance->GetPendingCmd();
+40 −0
Original line number Diff line number Diff line
@@ -135,6 +135,9 @@ std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) { r

namespace hfp {

static bool encoding_transport_is_stream_active_ret;
static bool decoding_transport_is_stream_active_ret;

HfpTransport::HfpTransport() {}
BluetoothAudioCtrlAck HfpTransport::StartRequest() {
  return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
@@ -184,6 +187,7 @@ void HfpDecodingTransport::LogBytesWritten(size_t bytes_written) {}
uint8_t HfpDecodingTransport::GetPendingCmd() const { return HFP_CTRL_CMD_NONE; }
void HfpDecodingTransport::ResetPendingCmd() {}
void HfpDecodingTransport::StopRequest() {}
bool HfpDecodingTransport::IsStreamActive() { return decoding_transport_is_stream_active_ret; }

HfpEncodingTransport::HfpEncodingTransport(SessionType session_type)
    : IBluetoothSinkTransportInstance(session_type, (AudioConfiguration){}) {}
@@ -208,6 +212,7 @@ void HfpEncodingTransport::ResetPresentationPosition() {}
void HfpEncodingTransport::LogBytesRead(size_t bytes_written) {}
uint8_t HfpEncodingTransport::GetPendingCmd() const { return HFP_CTRL_CMD_NONE; }
void HfpEncodingTransport::ResetPendingCmd() {}
bool HfpEncodingTransport::IsStreamActive() { return encoding_transport_is_stream_active_ret; }

}  // namespace hfp
}  // namespace aidl
@@ -245,6 +250,8 @@ protected:
    init_message_loop_thread();
    sink_client_read_called = false;
    source_client_write_called = false;
    bluetooth::audio::aidl::hfp::encoding_transport_is_stream_active_ret = true;
    bluetooth::audio::aidl::hfp::decoding_transport_is_stream_active_ret = true;
  }

  virtual void TearDown() override { cleanup_message_loop_thread(); }
@@ -263,6 +270,24 @@ TEST_F(HfpClientInterfaceTest, InitEncodeInterfaceAndRead) {
  HfpClientInterface::Get()->ReleaseEncode(encode_);
}

TEST_F(HfpClientInterfaceTest, InitEncodeInterfaceAndReadWhenStreamInactive) {
  uint8_t data[48];
  data[0] = 0xab;

  HfpClientInterface::Encode* encode_ = nullptr;

  bluetooth::audio::aidl::hfp::encoding_transport_is_stream_active_ret = false;

  encode_ = HfpClientInterface::Get()->GetEncode(&message_loop_thread);
  ASSERT_NE(nullptr, encode_);

  encode_->Read(data, 48);
  ASSERT_EQ(0, sink_client_read_called);
  ASSERT_EQ(0x00, data[0]);

  HfpClientInterface::Get()->ReleaseEncode(encode_);
}

TEST_F(HfpClientInterfaceTest, InitDecodeInterfaceAndWrite) {
  uint8_t data[48];
  HfpClientInterface::Decode* decode_ = nullptr;
@@ -276,4 +301,19 @@ TEST_F(HfpClientInterfaceTest, InitDecodeInterfaceAndWrite) {
  HfpClientInterface::Get()->ReleaseDecode(decode_);
}

TEST_F(HfpClientInterfaceTest, InitDecodeInterfaceAndWriteWhenStreamInactive) {
  uint8_t data[48];

  HfpClientInterface::Decode* decode_ = nullptr;

  bluetooth::audio::aidl::hfp::decoding_transport_is_stream_active_ret = false;

  decode_ = HfpClientInterface::Get()->GetDecode(&message_loop_thread);
  ASSERT_NE(nullptr, decode_);

  decode_->Write(data, 48);
  ASSERT_EQ(0, source_client_write_called);

  HfpClientInterface::Get()->ReleaseDecode(decode_);
}
}  // namespace