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