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