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