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

Commit 1caee667 authored by Cheney Ni's avatar Cheney Ni
Browse files

A2dpCodecs: apply the encoder settings via encoder_interface->encoder_init

Because the stack supports a single A2DP encoding instance which usually
needs to be initialized again after reconfig, the new codec config
should not apply to this encoder directly. It should go through
BTIF_A2DP_Source to set up the codec by one of those encoder_interface
encoder_init APIs, so the input and output format are consistent with
its configuration.

Bug: 195498246
Tag: #compatibility
Test: atest net_test_stack
      Switch A2DP codec manually
Change-Id: Ic9143b7cf91bf57df926c97fd7f3690da6227126
parent c474b971
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -1520,11 +1520,3 @@ bool A2dpCodecConfigAacSink::useRtpHeaderMarkerBit() const {
  // TODO: This method applies only to Source codecs
  return false;
}

bool A2dpCodecConfigAacSink::updateEncoderUserConfig(
    UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
    UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
    UNUSED_ATTR bool* p_config_updated) {
  // TODO: This method applies only to Source codecs
  return false;
}
+37 −60
Original line number Diff line number Diff line
@@ -78,9 +78,7 @@ typedef struct {
  uint16_t TxAaMtuSize;

  bool use_SCMS_T;
  bool is_peer_edr;          // True if the peer device supports EDR
  bool peer_supports_3mbps;  // True if the peer device supports 3Mbps EDR
  uint16_t peer_mtu;         // MTU of the A2DP peer
  tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
  uint32_t timestamp;        // Timestamp for the A2DP frames

  HANDLE_AACENCODER aac_handle;
@@ -93,12 +91,11 @@ typedef struct {
  a2dp_aac_encoder_stats_t stats;
} tA2DP_AAC_ENCODER_CB;

static uint32_t a2dp_aac_encoder_interval_ms = A2DP_AAC_ENCODER_INTERVAL_MS;

static tA2DP_AAC_ENCODER_CB a2dp_aac_encoder_cb;

static void a2dp_aac_encoder_update(uint16_t peer_mtu,
                                    A2dpCodecConfig* a2dp_codec_config,
static uint32_t a2dp_aac_encoder_interval_ms = A2DP_AAC_ENCODER_INTERVAL_MS;

static void a2dp_aac_encoder_update(A2dpCodecConfig* a2dp_codec_config,
                                    bool* p_restart_input,
                                    bool* p_restart_output,
                                    bool* p_config_updated);
@@ -107,6 +104,8 @@ static void a2dp_aac_get_num_frame_iteration(uint8_t* num_of_iterations,
                                             uint64_t timestamp_us);
static void a2dp_aac_encode_frames(uint8_t nb_frame);
static bool a2dp_aac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read);
static uint16_t adjust_effective_mtu(
    const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params);

bool A2DP_LoadEncoderAac(void) {
  // Nothing to do - the library is statically linked
@@ -133,9 +132,7 @@ void a2dp_aac_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,

  a2dp_aac_encoder_cb.read_callback = read_callback;
  a2dp_aac_encoder_cb.enqueue_callback = enqueue_callback;
  a2dp_aac_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
  a2dp_aac_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
  a2dp_aac_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
  a2dp_aac_encoder_cb.peer_params = *p_peer_params;
  a2dp_aac_encoder_cb.timestamp = 0;

  a2dp_aac_encoder_cb.use_SCMS_T = false;  // TODO: should be a parameter
@@ -144,40 +141,17 @@ void a2dp_aac_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
#endif

  // NOTE: Ignore the restart_input / restart_output flags - this initization
  // happens when the connection is (re)started.
  // happens when the audio session is (re)started.
  bool restart_input = false;
  bool restart_output = false;
  bool config_updated = false;
  a2dp_aac_encoder_update(a2dp_aac_encoder_cb.peer_mtu, a2dp_codec_config,
                          &restart_input, &restart_output, &config_updated);
}

bool A2dpCodecConfigAacSource::updateEncoderUserConfig(
    const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, bool* p_restart_input,
    bool* p_restart_output, bool* p_config_updated) {
  a2dp_aac_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
  a2dp_aac_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
  a2dp_aac_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
  a2dp_aac_encoder_cb.timestamp = 0;

  if (a2dp_aac_encoder_cb.peer_mtu == 0) {
    LOG_ERROR(
        "%s: Cannot update the codec encoder for %s: "
        "invalid peer MTU",
        __func__, name().c_str());
    return false;
  }

  a2dp_aac_encoder_update(a2dp_aac_encoder_cb.peer_mtu, this, p_restart_input,
                          p_restart_output, p_config_updated);
  return true;
  a2dp_aac_encoder_update(a2dp_codec_config, &restart_input, &restart_output,
                          &config_updated);
}

// Update the A2DP AAC encoder.
// |peer_mtu| is the peer MTU.
// |a2dp_codec_config| is the A2DP codec to use for the update.
static void a2dp_aac_encoder_update(uint16_t peer_mtu,
                                    A2dpCodecConfig* a2dp_codec_config,
static void a2dp_aac_encoder_update(A2dpCodecConfig* a2dp_codec_config,
                                    bool* p_restart_input,
                                    bool* p_restart_output,
                                    bool* p_config_updated) {
@@ -226,30 +200,11 @@ static void a2dp_aac_encoder_update(uint16_t peer_mtu,
      a2dp_aac_encoder_cb.feeding_params.sample_rate;
  p_encoder_params->channel_mode = A2DP_GetChannelModeCodeAac(p_codec_info);

  LOG_VERBOSE("%s: original AVDTP MTU size: %d", __func__,
              a2dp_aac_encoder_cb.TxAaMtuSize);
  if (a2dp_aac_encoder_cb.is_peer_edr &&
      !a2dp_aac_encoder_cb.peer_supports_3mbps) {
    // This condition would be satisfied only if the remote device is
    // EDR and supports only 2 Mbps, but the effective AVDTP MTU size
    // exceeds the 2DH5 packet size.
    LOG_VERBOSE("%s: The remote device is EDR but does not support 3 Mbps",
                __func__);
    if (peer_mtu > MAX_2MBPS_AVDTP_MTU) {
      LOG_WARN("%s: Restricting AVDTP MTU size from %d to %d", __func__,
               peer_mtu, MAX_2MBPS_AVDTP_MTU);
      peer_mtu = MAX_2MBPS_AVDTP_MTU;
    }
  }
  uint16_t mtu_size = BT_DEFAULT_BUFFER_SIZE - A2DP_AAC_OFFSET - sizeof(BT_HDR);
  if (mtu_size < peer_mtu) {
    a2dp_aac_encoder_cb.TxAaMtuSize = mtu_size;
  } else {
    a2dp_aac_encoder_cb.TxAaMtuSize = peer_mtu;
  }

  const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params =
      a2dp_aac_encoder_cb.peer_params;
  a2dp_aac_encoder_cb.TxAaMtuSize = adjust_effective_mtu(peer_params);
  LOG_INFO("%s: MTU=%d, peer_mtu=%d", __func__, a2dp_aac_encoder_cb.TxAaMtuSize,
           peer_mtu);
           peer_params.peer_mtu);
  LOG_INFO("%s: sample_rate: %d channel_mode: %d ", __func__,
           p_encoder_params->sample_rate, p_encoder_params->channel_mode);

@@ -732,6 +687,28 @@ static bool a2dp_aac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read) {
  return true;
}

static uint16_t adjust_effective_mtu(
    const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params) {
  uint16_t mtu_size = BT_DEFAULT_BUFFER_SIZE - A2DP_AAC_OFFSET - sizeof(BT_HDR);
  if (mtu_size > peer_params.peer_mtu) {
    mtu_size = peer_params.peer_mtu;
  }
  LOG_VERBOSE("%s: original AVDTP MTU size: %d", __func__, mtu_size);
  if (peer_params.is_peer_edr && !peer_params.peer_supports_3mbps) {
    // This condition would be satisfied only if the remote device is
    // EDR and supports only 2 Mbps, but the effective AVDTP MTU size
    // exceeds the 2DH5 packet size.
    LOG_VERBOSE("%s: The remote device is EDR but does not support 3 Mbps",
                __func__);
    if (mtu_size > MAX_2MBPS_AVDTP_MTU) {
      LOG_WARN("%s: Restricting AVDTP MTU size from %d to %d", __func__,
               mtu_size, MAX_2MBPS_AVDTP_MTU);
      mtu_size = MAX_2MBPS_AVDTP_MTU;
    }
  }
  return mtu_size;
}

void A2dpCodecConfigAacSource::debug_codec_dump(int fd) {
  a2dp_aac_encoder_stats_t* stats = &a2dp_aac_encoder_cb.stats;

+0 −13
Original line number Diff line number Diff line
@@ -389,19 +389,6 @@ bool A2dpCodecConfig::setCodecUserConfig(
    *p_restart_output = true;
  }

  bool encoder_restart_input = *p_restart_input;
  bool encoder_restart_output = *p_restart_output;
  bool encoder_config_updated = *p_config_updated;

  if (!a2dp_offload_status) {
    if (updateEncoderUserConfig(p_peer_params, &encoder_restart_input,
                                &encoder_restart_output,
                                &encoder_config_updated)) {
      if (encoder_restart_input) *p_restart_input = true;
      if (encoder_restart_output) *p_restart_output = true;
      if (encoder_config_updated) *p_config_updated = true;
    }
  }
  if (*p_restart_input || *p_restart_output) *p_config_updated = true;

  return true;
+0 −8
Original line number Diff line number Diff line
@@ -1562,11 +1562,3 @@ bool A2dpCodecConfigSbcSink::useRtpHeaderMarkerBit() const {
  // TODO: This method applies only to Source codecs
  return false;
}

bool A2dpCodecConfigSbcSink::updateEncoderUserConfig(
    UNUSED_ATTR const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
    UNUSED_ATTR bool* p_restart_input, UNUSED_ATTR bool* p_restart_output,
    UNUSED_ATTR bool* p_config_updated) {
  // TODO: This method applies only to Source codecs
  return false;
}
+42 −64
Original line number Diff line number Diff line
@@ -95,9 +95,7 @@ typedef struct {
  a2dp_source_enqueue_callback_t enqueue_callback;
  uint16_t TxAaMtuSize;
  uint8_t tx_sbc_frames;
  bool is_peer_edr;         /* True if the peer device supports EDR */
  bool peer_supports_3mbps; /* True if the peer device supports 3Mbps EDR */
  uint16_t peer_mtu;        /* MTU of the A2DP peer */
  tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
  uint32_t timestamp;       /* Timestamp for the A2DP frames */
  SBC_ENC_PARAMS sbc_encoder_params;
  tA2DP_FEEDING_PARAMS feeding_params;
@@ -109,8 +107,7 @@ typedef struct {

static tA2DP_SBC_ENCODER_CB a2dp_sbc_encoder_cb;

static void a2dp_sbc_encoder_update(uint16_t peer_mtu,
                                    A2dpCodecConfig* a2dp_codec_config,
static void a2dp_sbc_encoder_update(A2dpCodecConfig* a2dp_codec_config,
                                    bool* p_restart_input,
                                    bool* p_restart_output,
                                    bool* p_config_updated);
@@ -119,8 +116,10 @@ static void a2dp_sbc_encode_frames(uint8_t nb_frame);
static void a2dp_sbc_get_num_frame_iteration(uint8_t* num_of_iterations,
                                             uint8_t* num_of_frames,
                                             uint64_t timestamp_us);
static uint16_t adjust_effective_mtu(
    const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params);
static uint8_t calculate_max_frames_per_packet(void);
static uint16_t a2dp_sbc_source_rate();
static uint16_t a2dp_sbc_source_rate(bool is_peer_edr);
static uint32_t a2dp_sbc_frame_length(void);

bool A2DP_LoadEncoderSbc(void) {
@@ -143,46 +142,21 @@ void a2dp_sbc_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,

  a2dp_sbc_encoder_cb.read_callback = read_callback;
  a2dp_sbc_encoder_cb.enqueue_callback = enqueue_callback;
  a2dp_sbc_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
  a2dp_sbc_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
  a2dp_sbc_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
  a2dp_sbc_encoder_cb.peer_params = *p_peer_params;
  a2dp_sbc_encoder_cb.timestamp = 0;

  // NOTE: Ignore the restart_input / restart_output flags - this initization
  // happens when the connection is (re)started.
  // happens when the audio session is (re)started.
  bool restart_input = false;
  bool restart_output = false;
  bool config_updated = false;
  a2dp_sbc_encoder_update(a2dp_sbc_encoder_cb.peer_mtu, a2dp_codec_config,
                          &restart_input, &restart_output, &config_updated);
}

bool A2dpCodecConfigSbcSource::updateEncoderUserConfig(
    const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, bool* p_restart_input,
    bool* p_restart_output, bool* p_config_updated) {
  a2dp_sbc_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
  a2dp_sbc_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
  a2dp_sbc_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
  a2dp_sbc_encoder_cb.timestamp = 0;

  if (a2dp_sbc_encoder_cb.peer_mtu == 0) {
    LOG_ERROR(
        "%s: Cannot update the codec encoder for %s: "
        "invalid peer MTU",
        __func__, name().c_str());
    return false;
  }

  a2dp_sbc_encoder_update(a2dp_sbc_encoder_cb.peer_mtu, this, p_restart_input,
                          p_restart_output, p_config_updated);
  return true;
  a2dp_sbc_encoder_update(a2dp_codec_config, &restart_input, &restart_output,
                          &config_updated);
}

// Update the A2DP SBC encoder.
// |peer_mtu| is the peer MTU.
// |a2dp_codec_config| is the A2DP codec to use for the update.
static void a2dp_sbc_encoder_update(uint16_t peer_mtu,
                                    A2dpCodecConfig* a2dp_codec_config,
static void a2dp_sbc_encoder_update(A2dpCodecConfig* a2dp_codec_config,
                                    bool* p_restart_input,
                                    bool* p_restart_output,
                                    bool* p_config_updated) {
@@ -250,13 +224,6 @@ static void a2dp_sbc_encoder_update(uint16_t peer_mtu,
    p_encoder_params->s16NumOfChannels = SBC_MAX_NUM_OF_CHANNELS;
  }

  uint16_t mtu_size = A2DP_SBC_BUFFER_SIZE - A2DP_SBC_OFFSET - sizeof(BT_HDR);
  if (mtu_size < peer_mtu) {
    a2dp_sbc_encoder_cb.TxAaMtuSize = mtu_size;
  } else {
    a2dp_sbc_encoder_cb.TxAaMtuSize = peer_mtu;
  }

  if (p_encoder_params->s16SamplingFreq == SBC_sf16000)
    s16SamplingFreq = 16000;
  else if (p_encoder_params->s16SamplingFreq == SBC_sf32000)
@@ -267,10 +234,14 @@ static void a2dp_sbc_encoder_update(uint16_t peer_mtu,
    s16SamplingFreq = 48000;

  // Set the initial target bit rate
  p_encoder_params->u16BitRate = a2dp_sbc_source_rate();
  const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params =
      a2dp_sbc_encoder_cb.peer_params;
  p_encoder_params->u16BitRate = a2dp_sbc_source_rate(peer_params.is_peer_edr);

  a2dp_sbc_encoder_cb.TxAaMtuSize = adjust_effective_mtu(peer_params);
  LOG_INFO("%s: MTU=%d, peer_mtu=%d min_bitpool=%d max_bitpool=%d", __func__,
           a2dp_sbc_encoder_cb.TxAaMtuSize, peer_mtu, min_bitpool, max_bitpool);
           a2dp_sbc_encoder_cb.TxAaMtuSize, peer_params.peer_mtu, min_bitpool,
           max_bitpool);
  LOG_INFO(
      "%s: ChannelMode=%d, NumOfSubBands=%d, NumOfBlocks=%d, "
      "AllocationMethod=%d, BitRate=%d, SamplingFreq=%d BitPool=%d",
@@ -461,7 +432,7 @@ static void a2dp_sbc_get_num_frame_iteration(uint8_t* num_of_iterations,

  LOG_VERBOSE("%s: frames for available PCM data %u", __func__, projected_nof);

  if (a2dp_sbc_encoder_cb.is_peer_edr) {
  if (a2dp_sbc_encoder_cb.peer_params.is_peer_edr) {
    if (!a2dp_sbc_encoder_cb.tx_sbc_frames) {
      LOG_ERROR("%s: tx_sbc_frames not updated, update from here", __func__);
      a2dp_sbc_encoder_cb.tx_sbc_frames = calculate_max_frames_per_packet();
@@ -746,29 +717,36 @@ static bool a2dp_sbc_read_feeding(uint32_t* bytes_read) {
  return true;
}

static uint8_t calculate_max_frames_per_packet(void) {
  uint16_t effective_mtu_size = a2dp_sbc_encoder_cb.TxAaMtuSize;
  SBC_ENC_PARAMS* p_encoder_params = &a2dp_sbc_encoder_cb.sbc_encoder_params;
  uint16_t result = 0;
  uint32_t frame_len;

  LOG_VERBOSE("%s: original AVDTP MTU size: %d", __func__,
              a2dp_sbc_encoder_cb.TxAaMtuSize);
  if (a2dp_sbc_encoder_cb.is_peer_edr &&
      !a2dp_sbc_encoder_cb.peer_supports_3mbps) {
static uint16_t adjust_effective_mtu(
    const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params) {
  uint16_t mtu_size = A2DP_SBC_BUFFER_SIZE - A2DP_SBC_OFFSET - sizeof(BT_HDR);
  if (mtu_size > peer_params.peer_mtu) {
    mtu_size = peer_params.peer_mtu;
  }
  LOG_VERBOSE("%s: original AVDTP MTU size: %d", __func__, mtu_size);
  if (peer_params.is_peer_edr && !peer_params.peer_supports_3mbps) {
    // This condition would be satisfied only if the remote device is
    // EDR and supports only 2 Mbps, but the effective AVDTP MTU size
    // exceeds the 2DH5 packet size.
    LOG_VERBOSE("%s: The remote device is EDR but does not support 3 Mbps",
                __func__);

    if (effective_mtu_size > MAX_2MBPS_AVDTP_MTU) {
      LOG_WARN("%s: Restricting AVDTP MTU size to %d", __func__,
               MAX_2MBPS_AVDTP_MTU);
      effective_mtu_size = MAX_2MBPS_AVDTP_MTU;
      a2dp_sbc_encoder_cb.TxAaMtuSize = effective_mtu_size;
    if (mtu_size > MAX_2MBPS_AVDTP_MTU) {
      LOG_WARN("%s: Restricting AVDTP MTU size from %d to %d", __func__,
               mtu_size, MAX_2MBPS_AVDTP_MTU);
      mtu_size = MAX_2MBPS_AVDTP_MTU;
    }
  }
  return mtu_size;
}

static uint8_t calculate_max_frames_per_packet(void) {
  SBC_ENC_PARAMS* p_encoder_params = &a2dp_sbc_encoder_cb.sbc_encoder_params;
  uint16_t result = 0;
  uint32_t frame_len;

  a2dp_sbc_encoder_cb.TxAaMtuSize =
      adjust_effective_mtu(a2dp_sbc_encoder_cb.peer_params);
  const uint16_t& effective_mtu_size = a2dp_sbc_encoder_cb.TxAaMtuSize;

  if (!p_encoder_params->s16NumOfSubBands) {
    LOG_ERROR("%s: SubBands are set to 0, resetting to %d", __func__,
@@ -819,11 +797,11 @@ static uint8_t calculate_max_frames_per_packet(void) {
  return result;
}

static uint16_t a2dp_sbc_source_rate() {
static uint16_t a2dp_sbc_source_rate(bool is_peer_edr) {
  uint16_t rate = A2DP_SBC_DEFAULT_BITRATE;

  /* restrict bitrate if a2dp link is non-edr */
  if (!a2dp_sbc_encoder_cb.is_peer_edr) {
  if (!is_peer_edr) {
    rate = A2DP_SBC_NON_EDR_MAX_RATE;
    LOG_VERBOSE("%s: non-edr a2dp sink detected, restrict rate to %d", __func__,
                rate);
Loading