Home | History | Annotate | Download | only in a2dp
      1 /*
      2  * Copyright 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "a2dp_aac_encoder"
     18 
     19 #include "a2dp_aac_encoder.h"
     20 
     21 #include <inttypes.h>
     22 #include <stdio.h>
     23 #include <string.h>
     24 
     25 #include <aacenc_lib.h>
     26 #include <base/logging.h>
     27 
     28 #include "a2dp_aac.h"
     29 #include "bt_common.h"
     30 #include "osi/include/log.h"
     31 #include "osi/include/osi.h"
     32 
     33 //
     34 // Encoder for AAC Source Codec
     35 //
     36 
     37 // A2DP AAC encoder interval in milliseconds
     38 #define A2DP_AAC_ENCODER_INTERVAL_MS 20
     39 
     40 /*
     41  * 2DH5 payload size of:
     42  * 679 bytes - (4 bytes L2CAP Header + 12 bytes AVDTP Header)
     43  */
     44 #define MAX_2MBPS_AVDTP_MTU 663
     45 
     46 // offset
     47 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
     48 #define A2DP_AAC_OFFSET (AVDT_MEDIA_OFFSET + 1)
     49 #else
     50 #define A2DP_AAC_OFFSET AVDT_MEDIA_OFFSET
     51 #endif
     52 
     53 typedef struct {
     54   uint32_t sample_rate;
     55   uint8_t channel_mode;
     56   uint8_t bits_per_sample;
     57   uint32_t frame_length;         // Samples per channel in a frame
     58   uint8_t input_channels_n;      // Number of channels
     59   int max_encoded_buffer_bytes;  // Max encoded bytes per frame
     60 } tA2DP_AAC_ENCODER_PARAMS;
     61 
     62 typedef struct {
     63   uint32_t counter;
     64   uint32_t bytes_per_tick; /* pcm bytes read each media task tick */
     65   uint64_t last_frame_us;
     66 } tA2DP_AAC_FEEDING_STATE;
     67 
     68 typedef struct {
     69   uint64_t session_start_us;
     70 
     71   size_t media_read_total_expected_packets;
     72   size_t media_read_total_expected_reads_count;
     73   size_t media_read_total_expected_read_bytes;
     74 
     75   size_t media_read_total_dropped_packets;
     76   size_t media_read_total_actual_reads_count;
     77   size_t media_read_total_actual_read_bytes;
     78 } a2dp_aac_encoder_stats_t;
     79 
     80 typedef struct {
     81   a2dp_source_read_callback_t read_callback;
     82   a2dp_source_enqueue_callback_t enqueue_callback;
     83   uint16_t TxAaMtuSize;
     84 
     85   bool use_SCMS_T;
     86   bool is_peer_edr;          // True if the peer device supports EDR
     87   bool peer_supports_3mbps;  // True if the peer device supports 3Mbps EDR
     88   uint16_t peer_mtu;         // MTU of the A2DP peer
     89   uint32_t timestamp;        // Timestamp for the A2DP frames
     90 
     91   HANDLE_AACENCODER aac_handle;
     92   bool has_aac_handle;  // True if aac_handle is valid
     93 
     94   tA2DP_FEEDING_PARAMS feeding_params;
     95   tA2DP_AAC_ENCODER_PARAMS aac_encoder_params;
     96   tA2DP_AAC_FEEDING_STATE aac_feeding_state;
     97 
     98   a2dp_aac_encoder_stats_t stats;
     99 } tA2DP_AAC_ENCODER_CB;
    100 
    101 static tA2DP_AAC_ENCODER_CB a2dp_aac_encoder_cb;
    102 
    103 static void a2dp_aac_encoder_update(uint16_t peer_mtu,
    104                                     A2dpCodecConfig* a2dp_codec_config,
    105                                     bool* p_restart_input,
    106                                     bool* p_restart_output,
    107                                     bool* p_config_updated);
    108 static void a2dp_aac_get_num_frame_iteration(uint8_t* num_of_iterations,
    109                                              uint8_t* num_of_frames,
    110                                              uint64_t timestamp_us);
    111 static void a2dp_aac_encode_frames(uint8_t nb_frame);
    112 static bool a2dp_aac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read);
    113 
    114 bool A2DP_LoadEncoderAac(void) {
    115   // Nothing to do - the library is statically linked
    116   return true;
    117 }
    118 
    119 void A2DP_UnloadEncoderAac(void) {
    120   // Nothing to do - the library is statically linked
    121   if (a2dp_aac_encoder_cb.has_aac_handle)
    122     aacEncClose(&a2dp_aac_encoder_cb.aac_handle);
    123   memset(&a2dp_aac_encoder_cb, 0, sizeof(a2dp_aac_encoder_cb));
    124 }
    125 
    126 void a2dp_aac_encoder_init(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
    127                            A2dpCodecConfig* a2dp_codec_config,
    128                            a2dp_source_read_callback_t read_callback,
    129                            a2dp_source_enqueue_callback_t enqueue_callback) {
    130   if (a2dp_aac_encoder_cb.has_aac_handle)
    131     aacEncClose(&a2dp_aac_encoder_cb.aac_handle);
    132   memset(&a2dp_aac_encoder_cb, 0, sizeof(a2dp_aac_encoder_cb));
    133 
    134   a2dp_aac_encoder_cb.stats.session_start_us = time_get_os_boottime_us();
    135 
    136   a2dp_aac_encoder_cb.read_callback = read_callback;
    137   a2dp_aac_encoder_cb.enqueue_callback = enqueue_callback;
    138   a2dp_aac_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
    139   a2dp_aac_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
    140   a2dp_aac_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
    141   a2dp_aac_encoder_cb.timestamp = 0;
    142 
    143   a2dp_aac_encoder_cb.use_SCMS_T = false;  // TODO: should be a parameter
    144 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
    145   a2dp_aac_encoder_cb.use_SCMS_T = true;
    146 #endif
    147 
    148   // NOTE: Ignore the restart_input / restart_output flags - this initization
    149   // happens when the connection is (re)started.
    150   bool restart_input = false;
    151   bool restart_output = false;
    152   bool config_updated = false;
    153   a2dp_aac_encoder_update(a2dp_aac_encoder_cb.peer_mtu, a2dp_codec_config,
    154                           &restart_input, &restart_output, &config_updated);
    155 }
    156 
    157 bool A2dpCodecConfigAacSource::updateEncoderUserConfig(
    158     const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, bool* p_restart_input,
    159     bool* p_restart_output, bool* p_config_updated) {
    160   a2dp_aac_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr;
    161   a2dp_aac_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps;
    162   a2dp_aac_encoder_cb.peer_mtu = p_peer_params->peer_mtu;
    163   a2dp_aac_encoder_cb.timestamp = 0;
    164 
    165   if (a2dp_aac_encoder_cb.peer_mtu == 0) {
    166     LOG_ERROR(LOG_TAG,
    167               "%s: Cannot update the codec encoder for %s: "
    168               "invalid peer MTU",
    169               __func__, name().c_str());
    170     return false;
    171   }
    172 
    173   a2dp_aac_encoder_update(a2dp_aac_encoder_cb.peer_mtu, this, p_restart_input,
    174                           p_restart_output, p_config_updated);
    175   return true;
    176 }
    177 
    178 // Update the A2DP AAC encoder.
    179 // |peer_mtu| is the peer MTU.
    180 // |a2dp_codec_config| is the A2DP codec to use for the update.
    181 static void a2dp_aac_encoder_update(uint16_t peer_mtu,
    182                                     A2dpCodecConfig* a2dp_codec_config,
    183                                     bool* p_restart_input,
    184                                     bool* p_restart_output,
    185                                     bool* p_config_updated) {
    186   tA2DP_AAC_ENCODER_PARAMS* p_encoder_params =
    187       &a2dp_aac_encoder_cb.aac_encoder_params;
    188   uint8_t codec_info[AVDT_CODEC_SIZE];
    189   AACENC_ERROR aac_error;
    190   int aac_param_value, aac_sampling_freq, aac_peak_bit_rate;
    191 
    192   *p_restart_input = false;
    193   *p_restart_output = false;
    194   *p_config_updated = false;
    195 
    196   if (!a2dp_aac_encoder_cb.has_aac_handle) {
    197     AACENC_ERROR aac_error = aacEncOpen(&a2dp_aac_encoder_cb.aac_handle, 0,
    198                                         2 /* max 2 channels: stereo */);
    199     if (aac_error != AACENC_OK) {
    200       LOG_ERROR(LOG_TAG, "%s: Cannot open AAC encoder handle: AAC error 0x%x",
    201                 __func__, aac_error);
    202       return;  // TODO: Return an error?
    203     }
    204     a2dp_aac_encoder_cb.has_aac_handle = true;
    205   }
    206 
    207   if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) {
    208     LOG_ERROR(LOG_TAG,
    209               "%s: Cannot update the codec encoder for %s: "
    210               "invalid codec config",
    211               __func__, a2dp_codec_config->name().c_str());
    212     return;
    213   }
    214   const uint8_t* p_codec_info = codec_info;
    215 
    216   // The feeding parameters
    217   tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_aac_encoder_cb.feeding_params;
    218   p_feeding_params->sample_rate = A2DP_GetTrackSampleRateAac(p_codec_info);
    219   p_feeding_params->bits_per_sample =
    220       a2dp_codec_config->getAudioBitsPerSample();
    221   p_feeding_params->channel_count = A2DP_GetTrackChannelCountAac(p_codec_info);
    222   LOG_DEBUG(LOG_TAG, "%s: sample_rate=%u bits_per_sample=%u channel_count=%u",
    223             __func__, p_feeding_params->sample_rate,
    224             p_feeding_params->bits_per_sample, p_feeding_params->channel_count);
    225   a2dp_aac_feeding_reset();
    226 
    227   // The codec parameters
    228   p_encoder_params->sample_rate =
    229       a2dp_aac_encoder_cb.feeding_params.sample_rate;
    230   p_encoder_params->channel_mode = A2DP_GetChannelModeCodeAac(p_codec_info);
    231 
    232   LOG_VERBOSE(LOG_TAG, "%s: original AVDTP MTU size: %d", __func__,
    233               a2dp_aac_encoder_cb.TxAaMtuSize);
    234   if (a2dp_aac_encoder_cb.is_peer_edr &&
    235       !a2dp_aac_encoder_cb.peer_supports_3mbps) {
    236     // This condition would be satisfied only if the remote device is
    237     // EDR and supports only 2 Mbps, but the effective AVDTP MTU size
    238     // exceeds the 2DH5 packet size.
    239     LOG_VERBOSE(LOG_TAG,
    240                 "%s: The remote device is EDR but does not support 3 Mbps",
    241                 __func__);
    242     if (peer_mtu > MAX_2MBPS_AVDTP_MTU) {
    243       LOG_WARN(LOG_TAG, "%s: Restricting AVDTP MTU size from %d to %d",
    244                __func__, peer_mtu, MAX_2MBPS_AVDTP_MTU);
    245       peer_mtu = MAX_2MBPS_AVDTP_MTU;
    246     }
    247   }
    248   uint16_t mtu_size = BT_DEFAULT_BUFFER_SIZE - A2DP_AAC_OFFSET - sizeof(BT_HDR);
    249   if (mtu_size < peer_mtu) {
    250     a2dp_aac_encoder_cb.TxAaMtuSize = mtu_size;
    251   } else {
    252     a2dp_aac_encoder_cb.TxAaMtuSize = peer_mtu;
    253   }
    254 
    255   LOG_DEBUG(LOG_TAG, "%s: MTU=%d, peer_mtu=%d", __func__,
    256             a2dp_aac_encoder_cb.TxAaMtuSize, peer_mtu);
    257   LOG_DEBUG(LOG_TAG, "%s: sample_rate: %d channel_mode: %d ", __func__,
    258             p_encoder_params->sample_rate, p_encoder_params->channel_mode);
    259 
    260   // Set the encoder's parameters: Audio Object Type - MANDATORY
    261   // A2DP_AAC_OBJECT_TYPE_MPEG2_LC -> AOT_AAC_LC
    262   // A2DP_AAC_OBJECT_TYPE_MPEG4_LC -> AOT_AAC_LC
    263   // A2DP_AAC_OBJECT_TYPE_MPEG4_LTP -> AOT_AAC_LTP
    264   // A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE -> AOT_AAC_SCAL
    265   aac_param_value = AOT_AAC_LC;
    266   int object_type = A2DP_GetObjectTypeCodeAac(p_codec_info);
    267   switch (object_type) {
    268     case A2DP_AAC_OBJECT_TYPE_MPEG2_LC:
    269       aac_param_value = AOT_AAC_LC;
    270       break;
    271     case A2DP_AAC_OBJECT_TYPE_MPEG4_LC:
    272       aac_param_value = AOT_AAC_LC;
    273       break;
    274     case A2DP_AAC_OBJECT_TYPE_MPEG4_LTP:
    275       aac_param_value = AOT_AAC_LTP;
    276       break;
    277     case A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE:
    278       aac_param_value = AOT_AAC_SCAL;
    279       break;
    280     default:
    281       LOG_ERROR(LOG_TAG,
    282                 "%s: Cannot set AAC parameter AACENC_AOT: "
    283                 "invalid object type %d",
    284                 __func__, object_type);
    285       return;  // TODO: Return an error?
    286   }
    287   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle, AACENC_AOT,
    288                                   aac_param_value);
    289   if (aac_error != AACENC_OK) {
    290     LOG_ERROR(LOG_TAG,
    291               "%s: Cannot set AAC parameter AACENC_AOT to %d: "
    292               "AAC error 0x%x",
    293               __func__, aac_param_value, aac_error);
    294     return;  // TODO: Return an error?
    295   }
    296 
    297   // Set the encoder's parameters: audioMuxVersion
    298   aac_param_value = 2;  // audioMuxVersion = "2"
    299   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    300                                   AACENC_AUDIOMUXVER, aac_param_value);
    301   if (aac_error != AACENC_OK) {
    302     LOG_ERROR(LOG_TAG,
    303               "%s: Cannot set AAC parameter AACENC_AUDIOMUXVER to %d: "
    304               "AAC error 0x%x",
    305               __func__, aac_param_value, aac_error);
    306     return;  // TODO: Return an error?
    307   }
    308 
    309   // Set the encoder's parameters: Signaling mode of the extension AOT
    310   aac_param_value = 1;  // Signaling mode of the extension AOT = 1
    311   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    312                                   AACENC_SIGNALING_MODE, aac_param_value);
    313   if (aac_error != AACENC_OK) {
    314     LOG_ERROR(LOG_TAG,
    315               "%s: Cannot set AAC parameter AACENC_SIGNALING_MODE to %d: "
    316               "AAC error 0x%x",
    317               __func__, aac_param_value, aac_error);
    318     return;  // TODO: Return an error?
    319   }
    320 
    321   // Set the encoder's parameters: Sample Rate - MANDATORY
    322   aac_param_value = A2DP_GetTrackSampleRateAac(p_codec_info);
    323   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    324                                   AACENC_SAMPLERATE, aac_param_value);
    325   if (aac_error != AACENC_OK) {
    326     LOG_ERROR(LOG_TAG,
    327               "%s: Cannot set AAC parameter AACENC_SAMPLERATE to %d: "
    328               "AAC error 0x%x",
    329               __func__, aac_param_value, aac_error);
    330     return;  // TODO: Return an error?
    331   }
    332   aac_sampling_freq = aac_param_value;  // Save for extra usage below
    333 
    334   // Set the encoder's parameters: Bit Rate - MANDATORY
    335   aac_param_value = A2DP_GetBitRateAac(p_codec_info);
    336   // Calculate the bit rate from MTU and sampling frequency
    337   aac_peak_bit_rate =
    338       A2DP_ComputeMaxBitRateAac(p_codec_info, a2dp_aac_encoder_cb.TxAaMtuSize);
    339   aac_param_value = std::min(aac_param_value, aac_peak_bit_rate);
    340   LOG_DEBUG(LOG_TAG, "%s: MTU = %d Sampling Frequency = %d Bit Rate = %d",
    341             __func__, a2dp_aac_encoder_cb.TxAaMtuSize, aac_sampling_freq,
    342             aac_param_value);
    343   if (aac_param_value == -1) {
    344     LOG_ERROR(LOG_TAG,
    345               "%s: Cannot set AAC parameter AACENC_BITRATE: "
    346               "invalid codec bit rate",
    347               __func__);
    348     return;  // TODO: Return an error?
    349   }
    350   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    351                                   AACENC_BITRATE, aac_param_value);
    352   if (aac_error != AACENC_OK) {
    353     LOG_ERROR(LOG_TAG,
    354               "%s: Cannot set AAC parameter AACENC_BITRATE to %d: "
    355               "AAC error 0x%x",
    356               __func__, aac_param_value, aac_error);
    357     return;  // TODO: Return an error?
    358   }
    359 
    360   // Set the encoder's parameters: PEAK Bit Rate
    361   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    362                                   AACENC_PEAK_BITRATE, aac_peak_bit_rate);
    363   if (aac_error != AACENC_OK) {
    364     LOG_ERROR(LOG_TAG,
    365               "%s: Cannot set AAC parameter AACENC_PEAK_BITRATE to %d: "
    366               "AAC error 0x%x",
    367               __func__, aac_peak_bit_rate, aac_error);
    368     return;  // TODO: Return an error?
    369   }
    370 
    371   // Set the encoder's parameters: Channel Mode - MANDATORY
    372   if (A2DP_GetTrackChannelCountAac(p_codec_info) == 1) {
    373     aac_param_value = MODE_1;  // Mono
    374   } else {
    375     aac_param_value = MODE_2;  // Stereo
    376   }
    377   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    378                                   AACENC_CHANNELMODE, aac_param_value);
    379   if (aac_error != AACENC_OK) {
    380     LOG_ERROR(LOG_TAG,
    381               "%s: Cannot set AAC parameter AACENC_CHANNELMODE to %d: "
    382               "AAC error 0x%x",
    383               __func__, aac_param_value, aac_error);
    384     return;  // TODO: Return an error?
    385   }
    386 
    387   // Set the encoder's parameters: Transport Type
    388   aac_param_value = TT_MP4_LATM_MCP1;  // muxConfigPresent = 1
    389   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    390                                   AACENC_TRANSMUX, aac_param_value);
    391   if (aac_error != AACENC_OK) {
    392     LOG_ERROR(LOG_TAG,
    393               "%s: Cannot set AAC parameter AACENC_TRANSMUX to %d: "
    394               "AAC error 0x%x",
    395               __func__, aac_param_value, aac_error);
    396     return;  // TODO: Return an error?
    397   }
    398 
    399   // Set the encoder's parameters: Header Period
    400   aac_param_value = 1;
    401   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    402                                   AACENC_HEADER_PERIOD, aac_param_value);
    403   if (aac_error != AACENC_OK) {
    404     LOG_ERROR(LOG_TAG,
    405               "%s: Cannot set AAC parameter AACENC_HEADER_PERIOD to %d: "
    406               "AAC error 0x%x",
    407               __func__, aac_param_value, aac_error);
    408     return;  // TODO: Return an error?
    409   }
    410 
    411   // Set the encoder's parameters: Variable Bit Rate Support
    412   aac_param_value = A2DP_GetVariableBitRateSupportAac(p_codec_info);
    413   if (aac_param_value == -1) {
    414     LOG_ERROR(LOG_TAG,
    415               "%s: Cannot set AAC parameter AACENC_BITRATEMODE: "
    416               "invalid codec bit rate mode",
    417               __func__);
    418     return;  // TODO: Return an error?
    419   }
    420   aac_error = aacEncoder_SetParam(a2dp_aac_encoder_cb.aac_handle,
    421                                   AACENC_BITRATEMODE, aac_param_value);
    422   if (aac_error != AACENC_OK) {
    423     LOG_ERROR(LOG_TAG,
    424               "%s: Cannot set AAC parameter AACENC_BITRATEMODE to %d: "
    425               "AAC error 0x%x",
    426               __func__, aac_param_value, aac_error);
    427     return;  // TODO: Return an error?
    428   }
    429 
    430   // Mark the end of setting the encoder's parameters
    431   aac_error =
    432       aacEncEncode(a2dp_aac_encoder_cb.aac_handle, NULL, NULL, NULL, NULL);
    433   if (aac_error != AACENC_OK) {
    434     LOG_ERROR(LOG_TAG,
    435               "%s: Cannot complete setting the AAC parameters: AAC error 0x%x",
    436               __func__, aac_error);
    437     return;  // TODO: Return an error?
    438   }
    439 
    440   // Retrieve the encoder info so we can save the frame length
    441   AACENC_InfoStruct aac_info;
    442   aac_error = aacEncInfo(a2dp_aac_encoder_cb.aac_handle, &aac_info);
    443   if (aac_error != AACENC_OK) {
    444     LOG_ERROR(LOG_TAG,
    445               "%s: Cannot retrieve the AAC encoder info: AAC error 0x%x",
    446               __func__, aac_error);
    447     return;  // TODO: Return an error?
    448   }
    449   p_encoder_params->frame_length = aac_info.frameLength;
    450   p_encoder_params->input_channels_n = aac_info.inputChannels;
    451   p_encoder_params->max_encoded_buffer_bytes = aac_info.maxOutBufBytes;
    452   LOG_DEBUG(LOG_TAG,
    453             "%s: AAC frame_length = %u input_channels_n = %u "
    454             "max_encoded_buffer_bytes = %d",
    455             __func__, p_encoder_params->frame_length,
    456             p_encoder_params->input_channels_n,
    457             p_encoder_params->max_encoded_buffer_bytes);
    458 }
    459 
    460 void a2dp_aac_encoder_cleanup(void) {
    461   if (a2dp_aac_encoder_cb.has_aac_handle)
    462     aacEncClose(&a2dp_aac_encoder_cb.aac_handle);
    463   memset(&a2dp_aac_encoder_cb, 0, sizeof(a2dp_aac_encoder_cb));
    464 }
    465 
    466 void a2dp_aac_feeding_reset(void) {
    467   /* By default, just clear the entire state */
    468   memset(&a2dp_aac_encoder_cb.aac_feeding_state, 0,
    469          sizeof(a2dp_aac_encoder_cb.aac_feeding_state));
    470 
    471   a2dp_aac_encoder_cb.aac_feeding_state.bytes_per_tick =
    472       (a2dp_aac_encoder_cb.feeding_params.sample_rate *
    473        a2dp_aac_encoder_cb.feeding_params.bits_per_sample / 8 *
    474        a2dp_aac_encoder_cb.feeding_params.channel_count *
    475        A2DP_AAC_ENCODER_INTERVAL_MS) /
    476       1000;
    477 
    478   LOG_DEBUG(LOG_TAG, "%s: PCM bytes per tick %u", __func__,
    479             a2dp_aac_encoder_cb.aac_feeding_state.bytes_per_tick);
    480 }
    481 
    482 void a2dp_aac_feeding_flush(void) {
    483   a2dp_aac_encoder_cb.aac_feeding_state.counter = 0;
    484 }
    485 
    486 period_ms_t a2dp_aac_get_encoder_interval_ms(void) {
    487   return A2DP_AAC_ENCODER_INTERVAL_MS;
    488 }
    489 
    490 void a2dp_aac_send_frames(uint64_t timestamp_us) {
    491   uint8_t nb_frame = 0;
    492   uint8_t nb_iterations = 0;
    493 
    494   a2dp_aac_get_num_frame_iteration(&nb_iterations, &nb_frame, timestamp_us);
    495   LOG_VERBOSE(LOG_TAG, "%s: Sending %d frames per iteration, %d iterations",
    496               __func__, nb_frame, nb_iterations);
    497   if (nb_frame == 0) return;
    498 
    499   for (uint8_t counter = 0; counter < nb_iterations; counter++) {
    500     // Transcode frame and enqueue
    501     a2dp_aac_encode_frames(nb_frame);
    502   }
    503 }
    504 
    505 // Obtains the number of frames to send and number of iterations
    506 // to be used. |num_of_iterations| and |num_of_frames| parameters
    507 // are used as output param for returning the respective values.
    508 static void a2dp_aac_get_num_frame_iteration(uint8_t* num_of_iterations,
    509                                              uint8_t* num_of_frames,
    510                                              uint64_t timestamp_us) {
    511   uint32_t result = 0;
    512   uint8_t nof = 0;
    513   uint8_t noi = 1;
    514 
    515   uint32_t pcm_bytes_per_frame =
    516       a2dp_aac_encoder_cb.aac_encoder_params.frame_length *
    517       a2dp_aac_encoder_cb.feeding_params.channel_count *
    518       a2dp_aac_encoder_cb.feeding_params.bits_per_sample / 8;
    519   LOG_VERBOSE(LOG_TAG, "%s: pcm_bytes_per_frame %u", __func__,
    520               pcm_bytes_per_frame);
    521 
    522   uint32_t us_this_tick = A2DP_AAC_ENCODER_INTERVAL_MS * 1000;
    523   uint64_t now_us = timestamp_us;
    524   if (a2dp_aac_encoder_cb.aac_feeding_state.last_frame_us != 0)
    525     us_this_tick =
    526         (now_us - a2dp_aac_encoder_cb.aac_feeding_state.last_frame_us);
    527   a2dp_aac_encoder_cb.aac_feeding_state.last_frame_us = now_us;
    528 
    529   a2dp_aac_encoder_cb.aac_feeding_state.counter +=
    530       a2dp_aac_encoder_cb.aac_feeding_state.bytes_per_tick * us_this_tick /
    531       (A2DP_AAC_ENCODER_INTERVAL_MS * 1000);
    532 
    533   result = a2dp_aac_encoder_cb.aac_feeding_state.counter / pcm_bytes_per_frame;
    534   a2dp_aac_encoder_cb.aac_feeding_state.counter -= result * pcm_bytes_per_frame;
    535   nof = result;
    536 
    537   LOG_VERBOSE(LOG_TAG, "%s: effective num of frames %u, iterations %u",
    538               __func__, nof, noi);
    539 
    540   *num_of_frames = nof;
    541   *num_of_iterations = noi;
    542 }
    543 
    544 static void a2dp_aac_encode_frames(uint8_t nb_frame) {
    545   tA2DP_AAC_ENCODER_PARAMS* p_encoder_params =
    546       &a2dp_aac_encoder_cb.aac_encoder_params;
    547   tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_aac_encoder_cb.feeding_params;
    548   uint8_t remain_nb_frame = nb_frame;
    549   uint8_t read_buffer[BT_DEFAULT_BUFFER_SIZE];
    550   int pcm_bytes_per_frame = p_encoder_params->frame_length *
    551                             p_feeding_params->channel_count *
    552                             p_feeding_params->bits_per_sample / 8;
    553   CHECK(pcm_bytes_per_frame <= static_cast<int>(sizeof(read_buffer)));
    554 
    555   // Setup the input buffer
    556   AACENC_BufDesc in_buf_desc;
    557   void* in_buf_vector[1] = {nullptr};
    558   int in_buf_identifiers[1] = {IN_AUDIO_DATA};
    559   int in_buf_sizes[1] = {pcm_bytes_per_frame};
    560   int in_buf_element_sizes[1] = {p_feeding_params->bits_per_sample / 8};
    561   in_buf_desc.numBufs = 1;
    562   in_buf_desc.bufs = in_buf_vector;
    563   in_buf_desc.bufferIdentifiers = in_buf_identifiers;
    564   in_buf_desc.bufSizes = in_buf_sizes;
    565   in_buf_desc.bufElSizes = in_buf_element_sizes;
    566 
    567   // Setup the output buffer (partially)
    568   AACENC_BufDesc out_buf_desc;
    569   void* out_buf_vector[1] = {nullptr};
    570   int out_buf_identifiers[1] = {OUT_BITSTREAM_DATA};
    571   int out_buf_sizes[1] = {p_encoder_params->max_encoded_buffer_bytes};
    572   // NOTE: out_buf_element_sizes below is probably unused by the encoder
    573   int out_buf_element_sizes[1] = {p_feeding_params->bits_per_sample / 8};
    574   out_buf_desc.numBufs = 1;
    575   out_buf_desc.bufs = out_buf_vector;
    576   out_buf_desc.bufferIdentifiers = out_buf_identifiers;
    577   out_buf_desc.bufSizes = out_buf_sizes;
    578   out_buf_desc.bufElSizes = out_buf_element_sizes;
    579   CHECK(p_encoder_params->max_encoded_buffer_bytes <=
    580         static_cast<int>(BT_DEFAULT_BUFFER_SIZE - sizeof(BT_HDR)));
    581 
    582   AACENC_InArgs aac_in_args;
    583   aac_in_args.numInSamples =
    584       p_encoder_params->frame_length * p_feeding_params->channel_count;
    585   aac_in_args.numAncBytes = 0;
    586 
    587   AACENC_OutArgs aac_out_args = {
    588       .numOutBytes = 0, .numInSamples = 0, .numAncBytes = 0};
    589 
    590   uint32_t count;
    591   uint32_t total_bytes_read = 0;
    592   int written = 0;
    593 
    594   while (nb_frame) {
    595     BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
    596     p_buf->offset = A2DP_AAC_OFFSET;
    597     p_buf->len = 0;
    598     p_buf->layer_specific = 0;
    599     a2dp_aac_encoder_cb.stats.media_read_total_expected_packets++;
    600 
    601     count = 0;
    602     do {
    603       //
    604       // Read the PCM data and encode it
    605       //
    606       uint32_t bytes_read = 0;
    607       if (a2dp_aac_read_feeding(read_buffer, &bytes_read)) {
    608         uint8_t* packet = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len;
    609         if (!a2dp_aac_encoder_cb.has_aac_handle) {
    610           LOG_ERROR(LOG_TAG, "%s: invalid AAC handle", __func__);
    611           a2dp_aac_encoder_cb.stats.media_read_total_dropped_packets++;
    612           osi_free(p_buf);
    613           return;
    614         }
    615         in_buf_vector[0] = read_buffer;
    616         out_buf_vector[0] = packet + count;
    617         AACENC_ERROR aac_error =
    618             aacEncEncode(a2dp_aac_encoder_cb.aac_handle, &in_buf_desc,
    619                          &out_buf_desc, &aac_in_args, &aac_out_args);
    620         if (aac_error != AACENC_OK) {
    621           LOG_ERROR(LOG_TAG, "%s: AAC encoding error: 0x%x", __func__,
    622                     aac_error);
    623           a2dp_aac_encoder_cb.stats.media_read_total_dropped_packets++;
    624           osi_free(p_buf);
    625           return;
    626         }
    627         written = aac_out_args.numOutBytes;
    628         count += written;
    629         p_buf->len += written;
    630         nb_frame--;
    631         p_buf->layer_specific++;  // added a frame to the buffer
    632       } else {
    633         LOG_WARN(LOG_TAG, "%s: underflow %d", __func__, nb_frame);
    634         a2dp_aac_encoder_cb.aac_feeding_state.counter +=
    635             nb_frame * p_encoder_params->frame_length *
    636             p_feeding_params->channel_count *
    637             p_feeding_params->bits_per_sample / 8;
    638 
    639         // no more pcm to read
    640         nb_frame = 0;
    641       }
    642       total_bytes_read += bytes_read;
    643     } while ((written == 0) && nb_frame);
    644 
    645     // NOTE: We don't check whether the packet will fit in the MTU,
    646     // because AAC doesn't give us control over the encoded frame size.
    647     // If the packet is larger than the MTU, it will be fragmented before
    648     // transmission.
    649     if (p_buf->len) {
    650       /*
    651        * Timestamp of the media packet header represent the TS of the
    652        * first frame, i.e the timestamp before including this frame.
    653        */
    654       *((uint32_t*)(p_buf + 1)) = a2dp_aac_encoder_cb.timestamp;
    655 
    656       a2dp_aac_encoder_cb.timestamp +=
    657           p_buf->layer_specific * p_encoder_params->frame_length;
    658 
    659       uint8_t done_nb_frame = remain_nb_frame - nb_frame;
    660       remain_nb_frame = nb_frame;
    661       if (!a2dp_aac_encoder_cb.enqueue_callback(p_buf, done_nb_frame,
    662                                                 total_bytes_read))
    663         return;
    664     } else {
    665       a2dp_aac_encoder_cb.stats.media_read_total_dropped_packets++;
    666       osi_free(p_buf);
    667     }
    668   }
    669 }
    670 
    671 static bool a2dp_aac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read) {
    672   uint32_t read_size = a2dp_aac_encoder_cb.aac_encoder_params.frame_length *
    673                        a2dp_aac_encoder_cb.feeding_params.channel_count *
    674                        a2dp_aac_encoder_cb.feeding_params.bits_per_sample / 8;
    675 
    676   a2dp_aac_encoder_cb.stats.media_read_total_expected_reads_count++;
    677   a2dp_aac_encoder_cb.stats.media_read_total_expected_read_bytes += read_size;
    678 
    679   /* Read Data from UIPC channel */
    680   uint32_t nb_byte_read =
    681       a2dp_aac_encoder_cb.read_callback(read_buffer, read_size);
    682   a2dp_aac_encoder_cb.stats.media_read_total_actual_read_bytes += nb_byte_read;
    683   *bytes_read = nb_byte_read;
    684 
    685   if (nb_byte_read < read_size) {
    686     if (nb_byte_read == 0) return false;
    687 
    688     /* Fill the unfilled part of the read buffer with silence (0) */
    689     memset(((uint8_t*)read_buffer) + nb_byte_read, 0, read_size - nb_byte_read);
    690     nb_byte_read = read_size;
    691   }
    692   a2dp_aac_encoder_cb.stats.media_read_total_actual_reads_count++;
    693 
    694   return true;
    695 }
    696 
    697 period_ms_t A2dpCodecConfigAacSource::encoderIntervalMs() const {
    698   return a2dp_aac_get_encoder_interval_ms();
    699 }
    700 
    701 int A2dpCodecConfigAacSource::getEffectiveMtu() const {
    702   return a2dp_aac_encoder_cb.TxAaMtuSize;
    703 }
    704 
    705 void A2dpCodecConfigAacSource::debug_codec_dump(int fd) {
    706   a2dp_aac_encoder_stats_t* stats = &a2dp_aac_encoder_cb.stats;
    707 
    708   A2dpCodecConfig::debug_codec_dump(fd);
    709 
    710   dprintf(fd,
    711           "  Packet counts (expected/dropped)                        : %zu / "
    712           "%zu\n",
    713           stats->media_read_total_expected_packets,
    714           stats->media_read_total_dropped_packets);
    715 
    716   dprintf(fd,
    717           "  PCM read counts (expected/actual)                       : %zu / "
    718           "%zu\n",
    719           stats->media_read_total_expected_reads_count,
    720           stats->media_read_total_actual_reads_count);
    721 
    722   dprintf(fd,
    723           "  PCM read bytes (expected/actual)                        : %zu / "
    724           "%zu\n",
    725           stats->media_read_total_expected_read_bytes,
    726           stats->media_read_total_actual_read_bytes);
    727 }
    728