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

Commit e9f99405 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/stack/mmc: Migrate to libbluetooth_log

Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: I58ea52b64f743fe50070d737adabc9627f072913
parent a724e825
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -132,8 +132,12 @@ if (use.test) {
      "//bt/system/log:log_defaults",
      "//bt/system:external_gtest_main",
    ]
    libs = [
      "fmt",
    ]
    deps = [
      "//bt/system/stack/mmc/proto:mmc_config_proto",
      "//bt/system/log:libbluetooth_log",
      "//bt/system/common",
      "//bt/system/osi",
      "//bt/system/types",
@@ -157,8 +161,12 @@ if (use.test) {
      "//bt/system/log:log_defaults",
      "//bt/system:external_gtest_main",
    ]
    libs = [
      "fmt",
    ]
    deps = [
      "//bt/system/stack/mmc/proto:mmc_config_proto",
      "//bt/system/log:libbluetooth_log",
      "//bt/system/common",
      "//bt/system/osi",
      "//bt/system/types",
+5 −1
Original line number Diff line number Diff line
@@ -15,7 +15,11 @@
#

source_set("libcodec_client"){
  configs += [ "//bt/system/stack/mmc:target_defaults" ]
  configs += [
    "//bt/system/stack/mmc:target_defaults",
    "//bt/system/log:log_defaults",
  ]

  sources = [
    "codec_client.cc",
  ]
+22 −19
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <base/logging.h>
#include <base/timer/elapsed_timer.h>
#include <bluetooth/log.h>
#include <dbus/bus.h>
#include <dbus/message.h>
#include <dbus/object_proxy.h>
@@ -37,6 +38,8 @@
namespace mmc {
namespace {

using namespace bluetooth;

// Codec param field number in |ConfigParam|
const int kUnsupportedType = -1;
const int kHfpLc3EncoderId = 1;
@@ -53,7 +56,7 @@ int CodecId(const ConfigParam& config) {
  } else if (config.has_a2dp_aac_encoder_param()) {
    return kA2dpAacEncoderId;
  } else {
    LOG(WARNING) << "Unsupported codec type is used.";
    log::warn("Unsupported codec type is used.");
    return kUnsupportedType;
  }
}
@@ -70,7 +73,7 @@ CodecClient::CodecClient() {
  bus_ = new dbus::Bus(options);

  if (!bus_->Connect()) {
    LOG(ERROR) << "Failed to connect system bus";
    log::error("Failed to connect system bus");
    return;
  }

@@ -78,7 +81,7 @@ CodecClient::CodecClient() {
  codec_manager_ = bus_->GetObjectProxy(mmc::kMmcServiceName,
                                        dbus::ObjectPath(mmc::kMmcServicePath));
  if (!codec_manager_) {
    LOG(ERROR) << "Failed to get object proxy";
    log::error("Failed to get object proxy");
    return;
  }
}
@@ -101,7 +104,7 @@ int CodecClient::init(const ConfigParam config) {
  mmc::CodecInitRequest request;
  *request.mutable_config() = config;
  if (!writer.AppendProtoAsArrayOfBytes(request)) {
    LOG(ERROR) << "Failed to encode CodecInitRequest protobuf";
    log::error("Failed to encode CodecInitRequest protobuf");
    return -EINVAL;
  }

@@ -116,31 +119,31 @@ int CodecClient::init(const ConfigParam config) {
      ;

  if (!dbus_response) {
    LOG(ERROR) << "CodecInit failed";
    log::error("CodecInit failed");
    return -ECOMM;
  }

  dbus::MessageReader reader(dbus_response.get());
  mmc::CodecInitResponse response;
  if (!reader.PopArrayOfBytesAsProto(&response)) {
    LOG(ERROR) << "Failed to parse response protobuf";
    log::error("Failed to parse response protobuf");
    return -EINVAL;
  }

  if (response.socket_token().empty()) {
    LOG(ERROR) << "CodecInit returned empty socket token";
    log::error("CodecInit returned empty socket token");
    return -EBADMSG;
  }

  if (response.input_frame_size() < 0) {
    LOG(ERROR) << "CodecInit returned negative frame size";
    log::error("CodecInit returned negative frame size");
    return -EBADMSG;
  }

  // Create socket.
  skt_fd_ = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (skt_fd_ < 0) {
    LOG(ERROR) << "Failed to create socket: " << strerror(errno);
    log::error("Failed to create socket: {}", strerror(errno));
    return -errno;
  }

@@ -153,7 +156,7 @@ int CodecClient::init(const ConfigParam config) {
  int rc =
      connect(skt_fd_, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
  if (rc < 0) {
    LOG(ERROR) << "Failed to connect socket: " << strerror(errno);
    log::error("Failed to connect socket: {}", strerror(errno));
    return -errno;
  }
  unlink(addr.sun_path);
@@ -186,7 +189,7 @@ void CodecClient::cleanup() {
      ;

  if (!dbus_response) {
    LOG(WARNING) << "CodecCleanUp failed";
    log::warn("CodecCleanUp failed");
  }
  return;
}
@@ -198,12 +201,12 @@ int CodecClient::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  // i_buf and o_buf cannot be null.
  if (i_buf == nullptr || o_buf == nullptr) {
    LOG(ERROR) << "Buffer is null";
    log::error("Buffer is null");
    return -EINVAL;
  }

  if (i_len <= 0 || o_len <= 0) {
    LOG(ERROR) << "Non-positive buffer length";
    log::error("Non-positive buffer length");
    return -EINVAL;
  }

@@ -211,12 +214,12 @@ int CodecClient::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,
  int rc = send(skt_fd_, i_buf, i_len, MSG_NOSIGNAL);

  if (rc < 0) {
    LOG(ERROR) << "Failed to send data: " << strerror(errno);
    log::error("Failed to send data: {}", strerror(errno));
    return -errno;
  }
  // Full packet should be sent under SOCK_SEQPACKET setting.
  if (rc < i_len) {
    LOG(ERROR) << "Failed to send full packet";
    log::error("Failed to send full packet");
    return -EIO;
  }

@@ -226,24 +229,24 @@ int CodecClient::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  int pollret = poll(&pfd, 1, -1);
  if (pollret < 0) {
    LOG(ERROR) << "Failed to poll: " << strerror(errno);
    log::error("Failed to poll: {}", strerror(errno));
    return -errno;
  }

  if (pfd.revents & (POLLHUP | POLLNVAL)) {
    LOG(ERROR) << "Socket closed remotely.";
    log::error("Socket closed remotely.");
    return -EIO;
  }

  // POLLIN is returned..
  rc = recv(skt_fd_, o_buf, o_len, MSG_NOSIGNAL);
  if (rc < 0) {
    LOG(ERROR) << "Failed to recv data: " << strerror(errno);
    log::error("Failed to recv data: {}", strerror(errno));
    return -errno;
  }
  // Should be able to recv data when POLLIN is returned.
  if (rc == 0) {
    LOG(ERROR) << "Failed to recv data";
    log::error("Failed to recv data");
    return -EIO;
  }

+4 −1
Original line number Diff line number Diff line
@@ -39,7 +39,10 @@ source_set("libcodec_server_a2dp_aac") {
}

source_set("libcodec_server_hfp_lc3"){
  configs += [ "//bt/system/stack/mmc:target_defaults" ]
  configs += [
    "//bt/system/stack/mmc:target_defaults",
    "//bt/system/log:log_defaults",
  ]
  include_dirs = [
    "//bt/system",
    "//bt/system/include",
+17 −14
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ extern "C" {
}

#include <base/logging.h>
#include <bluetooth/log.h>

#include "a2dp_aac.h"
#include "mmc/proto/mmc_config.pb.h"
@@ -32,6 +33,8 @@ extern "C" {
namespace mmc {
namespace {

using namespace bluetooth;

const int A2DP_AAC_HEADER_LEN = 9;
const int A2DP_AAC_MAX_LEN_REPR = 4;
const int A2DP_AAC_MAX_PREFIX_SIZE =
@@ -51,20 +54,20 @@ A2dpAacEncoder::~A2dpAacEncoder() { cleanup(); }

int A2dpAacEncoder::init(ConfigParam config) {
  if (!config.has_a2dp_aac_encoder_param()) {
    LOG(ERROR) << "A2DP AAC Encoder params are not set";
    log::error("A2DP AAC Encoder params are not set");
    return -EINVAL;
  }

  const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
  if (!codec) {
    LOG(ERROR) << "Codec not found";
    log::error("Codec not found");
    return -ENOENT;
  }

  if (!avctx_) {
    avctx_ = avcodec_alloc_context3(codec);
    if (!avctx_) {
      LOG(ERROR) << "Cannot allocate context";
      log::error("Cannot allocate context");
      return -EINVAL;
    }
  }
@@ -81,12 +84,12 @@ int A2dpAacEncoder::init(ConfigParam config) {
    AVChannelLayout stereo = AV_CHANNEL_LAYOUT_STEREO;
    av_channel_layout_copy(&avctx_->ch_layout, &stereo);
  } else {
    LOG(ERROR) << "Invalid number of channels: " << channel_count;
    log::error("Invalid number of channels: {}", channel_count);
    return -EINVAL;
  }

  if (sample_rate != 44100 && sample_rate != 48000) {
    LOG(ERROR) << "Unsupported sample rate: " << sample_rate;
    log::error("Unsupported sample rate: {}", sample_rate);
    return -EINVAL;
  }

@@ -97,7 +100,7 @@ int A2dpAacEncoder::init(ConfigParam config) {

  int rc = avcodec_open2(avctx_, codec, NULL);
  if (rc < 0) {
    LOG(ERROR) << "Could not open context: " << rc;
    log::error("Could not open context: {}", rc);
    return -EINVAL;
  }

@@ -117,7 +120,7 @@ int A2dpAacEncoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  AVFrame* frame = av_frame_alloc();
  if (!frame) {
    LOG(ERROR) << "Could not alloc frame";
    log::error("Could not alloc frame");
    return -ENOMEM;
  }

@@ -127,21 +130,21 @@ int A2dpAacEncoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  rc = av_channel_layout_copy(&frame->ch_layout, &avctx_->ch_layout);
  if (rc < 0) {
    LOG(ERROR) << "Failed to copy channel layout: " << rc;
    log::error("Failed to copy channel layout: {}", rc);
    av_frame_free(&frame);
    return -EINVAL;
  }

  rc = av_frame_get_buffer(frame, 0);
  if (rc < 0) {
    LOG(ERROR) << "Failed to get buffer for frame: " << rc;
    log::error("Failed to get buffer for frame: {}", rc);
    av_frame_free(&frame);
    return -EIO;
  }

  rc = av_frame_make_writable(frame);
  if (rc < 0) {
    LOG(ERROR) << "Failed to make frame writable: " << rc;
    log::error("Failed to make frame writable: {}", rc);
    av_frame_free(&frame);
    return -EIO;
  }
@@ -182,13 +185,13 @@ int A2dpAacEncoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  AVPacket* pkt = av_packet_alloc();
  if (!pkt) {
    LOG(ERROR) << "Could not alloc packet";
    log::error("Could not alloc packet");
    return -ENOMEM;
  }

  rc = avcodec_send_frame(avctx_, frame);
  if (rc < 0) {
    LOG(ERROR) << "Failed to send frame: " << rc;
    log::error("Failed to send frame: {}", rc);
    av_frame_free(&frame);
    av_packet_free(&pkt);
    return -EIO;
@@ -196,7 +199,7 @@ int A2dpAacEncoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,

  rc = avcodec_receive_packet(avctx_, pkt);
  if (rc < 0 && rc != -EAGAIN) {
    LOG(ERROR) << "Failed to receive packet: " << rc;
    log::error("Failed to receive packet: {}", rc);
    av_frame_free(&frame);
    av_packet_free(&pkt);
    return -EIO;
@@ -215,7 +218,7 @@ int A2dpAacEncoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,
  int cap = param_.effective_frame_size();
  if (rc == -EAGAIN || cap < pkt->size + A2DP_AAC_MAX_PREFIX_SIZE) {
    if (rc != -EAGAIN) {
      LOG(WARNING) << "Dropped pkt: size=" << pkt->size << ", cap=" << cap;
      log::warn("Dropped pkt: size={}, cap={}", pkt->size, cap);
    }
    static uint8_t silent_frame[7] = {
        0x06, 0x21, 0x10, 0x04, 0x60, 0x8c, 0x1c,
Loading