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_vendor_ldac_encoder"
18 #define ATRACE_TAG ATRACE_TAG_AUDIO
19
20 #include "a2dp_vendor_ldac_encoder.h"
21
22 #ifndef OS_GENERIC
23 #include <cutils/trace.h>
24 #endif
25 #include <dlfcn.h>
26 #include <inttypes.h>
27 #include <ldacBT_abr.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "a2dp_vendor.h"
32 #include "a2dp_vendor_ldac.h"
33 #include "common/time_util.h"
34 #include "osi/include/allocator.h"
35 #include "osi/include/log.h"
36 #include "osi/include/osi.h"
37 #include "stack/include/bt_hdr.h"
38
39 //
40 // Encoder for LDAC Source Codec
41 //
42
43 // Initial EQMID for ABR mode.
44 #define LDAC_ABR_MODE_EQMID LDACBT_EQMID_SQ
45
46 // A2DP LDAC encoder interval in milliseconds
47 #define A2DP_LDAC_ENCODER_INTERVAL_MS 20
48 #define A2DP_LDAC_MEDIA_BYTES_PER_FRAME 128
49
50 // offset
51 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
52 #define A2DP_LDAC_OFFSET (AVDT_MEDIA_OFFSET + A2DP_LDAC_MPL_HDR_LEN + 1)
53 #else
54 #define A2DP_LDAC_OFFSET (AVDT_MEDIA_OFFSET + A2DP_LDAC_MPL_HDR_LEN)
55 #endif
56
57 typedef struct {
58 uint32_t sample_rate;
59 uint8_t channel_mode;
60 uint8_t bits_per_sample;
61 int quality_mode_index;
62 int pcm_wlength;
63 LDACBT_SMPL_FMT_T pcm_fmt;
64 } tA2DP_LDAC_ENCODER_PARAMS;
65
66 typedef struct {
67 float counter;
68 uint32_t bytes_per_tick; /* pcm bytes read each media task tick */
69 uint64_t last_frame_us;
70 } tA2DP_LDAC_FEEDING_STATE;
71
72 typedef struct {
73 uint64_t session_start_us;
74
75 size_t media_read_total_expected_packets;
76 size_t media_read_total_expected_reads_count;
77 size_t media_read_total_expected_read_bytes;
78
79 size_t media_read_total_dropped_packets;
80 size_t media_read_total_actual_reads_count;
81 size_t media_read_total_actual_read_bytes;
82 } a2dp_ldac_encoder_stats_t;
83
84 typedef struct {
85 a2dp_source_read_callback_t read_callback;
86 a2dp_source_enqueue_callback_t enqueue_callback;
87 uint16_t TxAaMtuSize;
88 size_t TxQueueLength;
89
90 bool use_SCMS_T;
91 tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
92 uint32_t timestamp; // Timestamp for the A2DP frames
93
94 HANDLE_LDAC_BT ldac_handle;
95 bool has_ldac_handle; // True if ldac_handle is valid
96
97 HANDLE_LDAC_ABR ldac_abr_handle;
98 bool has_ldac_abr_handle;
99 int last_ldac_abr_eqmid;
100 size_t ldac_abr_adjustments;
101
102 tA2DP_FEEDING_PARAMS feeding_params;
103 tA2DP_LDAC_ENCODER_PARAMS ldac_encoder_params;
104 tA2DP_LDAC_FEEDING_STATE ldac_feeding_state;
105
106 a2dp_ldac_encoder_stats_t stats;
107 } tA2DP_LDAC_ENCODER_CB;
108
109 static bool ldac_abr_loaded = true; // the library is statically linked
110
111 static tA2DP_LDAC_ENCODER_CB a2dp_ldac_encoder_cb;
112
113 static void a2dp_vendor_ldac_encoder_update(A2dpCodecConfig* a2dp_codec_config,
114 bool* p_restart_input,
115 bool* p_restart_output,
116 bool* p_config_updated);
117 static void a2dp_ldac_get_num_frame_iteration(uint8_t* num_of_iterations,
118 uint8_t* num_of_frames,
119 uint64_t timestamp_us);
120 static void a2dp_ldac_encode_frames(uint8_t nb_frame);
121 static bool a2dp_ldac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read);
122 static uint16_t adjust_effective_mtu(
123 const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params);
124 static std::string quality_mode_index_to_name(int quality_mode_index);
125
A2DP_VendorLoadEncoderLdac(void)126 bool A2DP_VendorLoadEncoderLdac(void) {
127 // Nothing to do - the library is statically linked
128 return true;
129 }
130
A2DP_VendorUnloadEncoderLdac(void)131 void A2DP_VendorUnloadEncoderLdac(void) {
132 // Cleanup any LDAC-related state
133 a2dp_vendor_ldac_encoder_cleanup();
134 }
135
a2dp_vendor_ldac_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)136 void a2dp_vendor_ldac_encoder_init(
137 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
138 A2dpCodecConfig* a2dp_codec_config,
139 a2dp_source_read_callback_t read_callback,
140 a2dp_source_enqueue_callback_t enqueue_callback) {
141 a2dp_vendor_ldac_encoder_cleanup();
142
143 a2dp_ldac_encoder_cb.stats.session_start_us =
144 bluetooth::common::time_get_os_boottime_us();
145
146 a2dp_ldac_encoder_cb.read_callback = read_callback;
147 a2dp_ldac_encoder_cb.enqueue_callback = enqueue_callback;
148 a2dp_ldac_encoder_cb.peer_params = *p_peer_params;
149 a2dp_ldac_encoder_cb.timestamp = 0;
150 a2dp_ldac_encoder_cb.ldac_abr_handle = NULL;
151 a2dp_ldac_encoder_cb.has_ldac_abr_handle = false;
152 a2dp_ldac_encoder_cb.last_ldac_abr_eqmid = -1;
153 a2dp_ldac_encoder_cb.ldac_abr_adjustments = 0;
154
155 a2dp_ldac_encoder_cb.use_SCMS_T = false; // TODO: should be a parameter
156 #if (BTA_AV_CO_CP_SCMS_T == TRUE)
157 a2dp_ldac_encoder_cb.use_SCMS_T = true;
158 #endif
159
160 // NOTE: Ignore the restart_input / restart_output flags - this initization
161 // happens when the audio session is (re)started.
162 bool restart_input = false;
163 bool restart_output = false;
164 bool config_updated = false;
165 a2dp_vendor_ldac_encoder_update(a2dp_codec_config, &restart_input,
166 &restart_output, &config_updated);
167 }
168
169 // Update the A2DP LDAC encoder.
170 // |a2dp_codec_config| is the A2DP codec to use for the update.
a2dp_vendor_ldac_encoder_update(A2dpCodecConfig * a2dp_codec_config,bool * p_restart_input,bool * p_restart_output,bool * p_config_updated)171 static void a2dp_vendor_ldac_encoder_update(A2dpCodecConfig* a2dp_codec_config,
172 bool* p_restart_input,
173 bool* p_restart_output,
174 bool* p_config_updated) {
175 tA2DP_LDAC_ENCODER_PARAMS* p_encoder_params =
176 &a2dp_ldac_encoder_cb.ldac_encoder_params;
177 uint8_t codec_info[AVDT_CODEC_SIZE];
178
179 *p_restart_input = false;
180 *p_restart_output = false;
181 *p_config_updated = false;
182
183 if (!a2dp_ldac_encoder_cb.has_ldac_handle) {
184 a2dp_ldac_encoder_cb.ldac_handle = ldacBT_get_handle();
185 if (a2dp_ldac_encoder_cb.ldac_handle == NULL) {
186 LOG_ERROR("%s: Cannot get LDAC encoder handle", __func__);
187 return; // TODO: Return an error?
188 }
189 a2dp_ldac_encoder_cb.has_ldac_handle = true;
190 }
191 CHECK(a2dp_ldac_encoder_cb.ldac_handle != nullptr);
192
193 if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) {
194 LOG_ERROR(
195 "%s: Cannot update the codec encoder for %s: "
196 "invalid codec config",
197 __func__, a2dp_codec_config->name().c_str());
198 return;
199 }
200 const uint8_t* p_codec_info = codec_info;
201 btav_a2dp_codec_config_t codec_config = a2dp_codec_config->getCodecConfig();
202
203 // The feeding parameters
204 tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_ldac_encoder_cb.feeding_params;
205 p_feeding_params->sample_rate =
206 A2DP_VendorGetTrackSampleRateLdac(p_codec_info);
207 p_feeding_params->bits_per_sample =
208 a2dp_codec_config->getAudioBitsPerSample();
209 p_feeding_params->channel_count =
210 A2DP_VendorGetTrackChannelCountLdac(p_codec_info);
211 LOG_INFO("%s: sample_rate=%u bits_per_sample=%u channel_count=%u", __func__,
212 p_feeding_params->sample_rate, p_feeding_params->bits_per_sample,
213 p_feeding_params->channel_count);
214 a2dp_vendor_ldac_feeding_reset();
215
216 // The codec parameters
217 p_encoder_params->sample_rate =
218 a2dp_ldac_encoder_cb.feeding_params.sample_rate;
219 p_encoder_params->channel_mode =
220 A2DP_VendorGetChannelModeCodeLdac(p_codec_info);
221
222 // Set the quality mode index
223 int old_quality_mode_index = p_encoder_params->quality_mode_index;
224 if (codec_config.codec_specific_1 != 0) {
225 p_encoder_params->quality_mode_index = codec_config.codec_specific_1 % 10;
226 LOG_INFO("%s: setting quality mode to %s", __func__,
227 quality_mode_index_to_name(p_encoder_params->quality_mode_index)
228 .c_str());
229 } else {
230 p_encoder_params->quality_mode_index = A2DP_LDAC_QUALITY_ABR;
231 LOG_INFO("%s: setting quality mode to default %s", __func__,
232 quality_mode_index_to_name(p_encoder_params->quality_mode_index)
233 .c_str());
234 }
235
236 int ldac_eqmid = LDAC_ABR_MODE_EQMID;
237 if (p_encoder_params->quality_mode_index == A2DP_LDAC_QUALITY_ABR) {
238 if (!ldac_abr_loaded) {
239 p_encoder_params->quality_mode_index = A2DP_LDAC_QUALITY_MID;
240 LOG_WARN(
241
242 "%s: LDAC ABR library is not loaded, resetting quality mode to %s",
243 __func__,
244 quality_mode_index_to_name(p_encoder_params->quality_mode_index)
245 .c_str());
246 } else {
247 LOG_INFO("%s: changing mode from %s to %s", __func__,
248 quality_mode_index_to_name(old_quality_mode_index).c_str(),
249 quality_mode_index_to_name(p_encoder_params->quality_mode_index)
250 .c_str());
251 if (a2dp_ldac_encoder_cb.ldac_abr_handle != NULL) {
252 LOG_INFO("%s: already in LDAC ABR mode, do nothing.", __func__);
253 } else {
254 LOG_INFO("%s: get and init LDAC ABR handle.", __func__);
255 a2dp_ldac_encoder_cb.ldac_abr_handle = ldac_ABR_get_handle();
256 if (a2dp_ldac_encoder_cb.ldac_abr_handle != NULL) {
257 a2dp_ldac_encoder_cb.has_ldac_abr_handle = true;
258 a2dp_ldac_encoder_cb.last_ldac_abr_eqmid = -1;
259 a2dp_ldac_encoder_cb.ldac_abr_adjustments = 0;
260 ldac_ABR_Init(a2dp_ldac_encoder_cb.ldac_abr_handle,
261 A2DP_LDAC_ENCODER_INTERVAL_MS);
262 } else {
263 p_encoder_params->quality_mode_index = A2DP_LDAC_QUALITY_MID;
264 LOG_INFO(
265
266 "%s: get LDAC ABR handle failed, resetting quality mode to %s.",
267 __func__,
268 quality_mode_index_to_name(p_encoder_params->quality_mode_index)
269 .c_str());
270 }
271 }
272 }
273 } else {
274 ldac_eqmid = p_encoder_params->quality_mode_index;
275 LOG_INFO("%s: in %s mode, free LDAC ABR handle.", __func__,
276 quality_mode_index_to_name(ldac_eqmid).c_str());
277 if (a2dp_ldac_encoder_cb.has_ldac_abr_handle) {
278 ldac_ABR_free_handle(a2dp_ldac_encoder_cb.ldac_abr_handle);
279 a2dp_ldac_encoder_cb.ldac_abr_handle = NULL;
280 a2dp_ldac_encoder_cb.has_ldac_abr_handle = false;
281 a2dp_ldac_encoder_cb.last_ldac_abr_eqmid = -1;
282 a2dp_ldac_encoder_cb.ldac_abr_adjustments = 0;
283 }
284 }
285
286 if (p_encoder_params->quality_mode_index != old_quality_mode_index)
287 *p_config_updated = true;
288
289 p_encoder_params->pcm_wlength =
290 a2dp_ldac_encoder_cb.feeding_params.bits_per_sample >> 3;
291 // Set the Audio format from pcm_wlength
292 p_encoder_params->pcm_fmt = LDACBT_SMPL_FMT_S16;
293 if (p_encoder_params->pcm_wlength == 2)
294 p_encoder_params->pcm_fmt = LDACBT_SMPL_FMT_S16;
295 else if (p_encoder_params->pcm_wlength == 3)
296 p_encoder_params->pcm_fmt = LDACBT_SMPL_FMT_S24;
297 else if (p_encoder_params->pcm_wlength == 4)
298 p_encoder_params->pcm_fmt = LDACBT_SMPL_FMT_S32;
299
300 const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params =
301 a2dp_ldac_encoder_cb.peer_params;
302 a2dp_ldac_encoder_cb.TxAaMtuSize = adjust_effective_mtu(peer_params);
303 LOG_INFO("%s: MTU=%d, peer_mtu=%d", __func__,
304 a2dp_ldac_encoder_cb.TxAaMtuSize, peer_params.peer_mtu);
305 LOG_INFO(
306 "%s: sample_rate: %d channel_mode: %d "
307 "quality_mode_index: %d pcm_wlength: %d pcm_fmt: %d",
308 __func__, p_encoder_params->sample_rate, p_encoder_params->channel_mode,
309 p_encoder_params->quality_mode_index, p_encoder_params->pcm_wlength,
310 p_encoder_params->pcm_fmt);
311
312 // Initialize the encoder.
313 // NOTE: MTU in the initialization must include the AVDT media header size.
314 int result = ldacBT_init_handle_encode(
315 a2dp_ldac_encoder_cb.ldac_handle,
316 a2dp_ldac_encoder_cb.TxAaMtuSize + AVDT_MEDIA_HDR_SIZE, ldac_eqmid,
317 p_encoder_params->channel_mode, p_encoder_params->pcm_fmt,
318 p_encoder_params->sample_rate);
319 if (result != 0) {
320 int err_code = ldacBT_get_error_code(a2dp_ldac_encoder_cb.ldac_handle);
321 LOG_ERROR(
322 "%s: error initializing the LDAC encoder: %d api_error = %d "
323 "handle_error = %d block_error = %d error_code = 0x%x",
324 __func__, result, LDACBT_API_ERR(err_code), LDACBT_HANDLE_ERR(err_code),
325 LDACBT_BLOCK_ERR(err_code), err_code);
326 }
327 }
328
a2dp_vendor_ldac_encoder_cleanup(void)329 void a2dp_vendor_ldac_encoder_cleanup(void) {
330 if (a2dp_ldac_encoder_cb.has_ldac_abr_handle) {
331 ldac_ABR_free_handle(a2dp_ldac_encoder_cb.ldac_abr_handle);
332 }
333 if (a2dp_ldac_encoder_cb.has_ldac_handle) {
334 ldacBT_free_handle(a2dp_ldac_encoder_cb.ldac_handle);
335 }
336 memset(&a2dp_ldac_encoder_cb, 0, sizeof(a2dp_ldac_encoder_cb));
337 }
338
a2dp_vendor_ldac_feeding_reset(void)339 void a2dp_vendor_ldac_feeding_reset(void) {
340 /* By default, just clear the entire state */
341 memset(&a2dp_ldac_encoder_cb.ldac_feeding_state, 0,
342 sizeof(a2dp_ldac_encoder_cb.ldac_feeding_state));
343
344 a2dp_ldac_encoder_cb.ldac_feeding_state.bytes_per_tick =
345 (a2dp_ldac_encoder_cb.feeding_params.sample_rate *
346 a2dp_ldac_encoder_cb.feeding_params.bits_per_sample / 8 *
347 a2dp_ldac_encoder_cb.feeding_params.channel_count *
348 A2DP_LDAC_ENCODER_INTERVAL_MS) /
349 1000;
350
351 LOG_INFO("%s: PCM bytes per tick %u", __func__,
352 a2dp_ldac_encoder_cb.ldac_feeding_state.bytes_per_tick);
353 }
354
a2dp_vendor_ldac_feeding_flush(void)355 void a2dp_vendor_ldac_feeding_flush(void) {
356 a2dp_ldac_encoder_cb.ldac_feeding_state.counter = 0.0f;
357 }
358
a2dp_vendor_ldac_get_encoder_interval_ms(void)359 uint64_t a2dp_vendor_ldac_get_encoder_interval_ms(void) {
360 return A2DP_LDAC_ENCODER_INTERVAL_MS;
361 }
362
a2dp_vendor_ldac_get_effective_frame_size()363 int a2dp_vendor_ldac_get_effective_frame_size() {
364 return a2dp_ldac_encoder_cb.TxAaMtuSize;
365 }
366
a2dp_vendor_ldac_send_frames(uint64_t timestamp_us)367 void a2dp_vendor_ldac_send_frames(uint64_t timestamp_us) {
368 uint8_t nb_frame = 0;
369 uint8_t nb_iterations = 0;
370
371 a2dp_ldac_get_num_frame_iteration(&nb_iterations, &nb_frame, timestamp_us);
372 LOG_VERBOSE("%s: Sending %d frames per iteration, %d iterations", __func__,
373 nb_frame, nb_iterations);
374 if (nb_frame == 0) return;
375
376 for (uint8_t counter = 0; counter < nb_iterations; counter++) {
377 if (a2dp_ldac_encoder_cb.has_ldac_abr_handle) {
378 int flag_enable = 1;
379 int prev_eqmid = a2dp_ldac_encoder_cb.last_ldac_abr_eqmid;
380 a2dp_ldac_encoder_cb.last_ldac_abr_eqmid =
381 ldac_ABR_Proc(a2dp_ldac_encoder_cb.ldac_handle,
382 a2dp_ldac_encoder_cb.ldac_abr_handle,
383 a2dp_ldac_encoder_cb.TxQueueLength, flag_enable);
384 if (prev_eqmid != a2dp_ldac_encoder_cb.last_ldac_abr_eqmid)
385 a2dp_ldac_encoder_cb.ldac_abr_adjustments++;
386 #ifndef OS_GENERIC
387 ATRACE_INT("LDAC ABR level", a2dp_ldac_encoder_cb.last_ldac_abr_eqmid);
388 #endif
389 }
390 // Transcode frame and enqueue
391 a2dp_ldac_encode_frames(nb_frame);
392 }
393 }
394
395 // Obtains the number of frames to send and number of iterations
396 // to be used. |num_of_iterations| and |num_of_frames| parameters
397 // are used as output param for returning the respective values.
a2dp_ldac_get_num_frame_iteration(uint8_t * num_of_iterations,uint8_t * num_of_frames,uint64_t timestamp_us)398 static void a2dp_ldac_get_num_frame_iteration(uint8_t* num_of_iterations,
399 uint8_t* num_of_frames,
400 uint64_t timestamp_us) {
401 uint32_t result = 0;
402 uint8_t nof = 0;
403 uint8_t noi = 1;
404
405 uint32_t pcm_bytes_per_frame =
406 A2DP_LDAC_MEDIA_BYTES_PER_FRAME *
407 a2dp_ldac_encoder_cb.feeding_params.channel_count *
408 a2dp_ldac_encoder_cb.feeding_params.bits_per_sample / 8;
409 LOG_VERBOSE("%s: pcm_bytes_per_frame %u", __func__, pcm_bytes_per_frame);
410
411 uint32_t us_this_tick = A2DP_LDAC_ENCODER_INTERVAL_MS * 1000;
412 uint64_t now_us = timestamp_us;
413 if (a2dp_ldac_encoder_cb.ldac_feeding_state.last_frame_us != 0)
414 us_this_tick =
415 (now_us - a2dp_ldac_encoder_cb.ldac_feeding_state.last_frame_us);
416 a2dp_ldac_encoder_cb.ldac_feeding_state.last_frame_us = now_us;
417
418 a2dp_ldac_encoder_cb.ldac_feeding_state.counter +=
419 (float)a2dp_ldac_encoder_cb.ldac_feeding_state.bytes_per_tick * us_this_tick /
420 (A2DP_LDAC_ENCODER_INTERVAL_MS * 1000);
421
422 result =
423 a2dp_ldac_encoder_cb.ldac_feeding_state.counter / pcm_bytes_per_frame;
424 a2dp_ldac_encoder_cb.ldac_feeding_state.counter -=
425 result * pcm_bytes_per_frame;
426 nof = result;
427
428 LOG_VERBOSE("%s: effective num of frames %u, iterations %u", __func__, nof,
429 noi);
430
431 *num_of_frames = nof;
432 *num_of_iterations = noi;
433 }
434
a2dp_ldac_encode_frames(uint8_t nb_frame)435 static void a2dp_ldac_encode_frames(uint8_t nb_frame) {
436 tA2DP_LDAC_ENCODER_PARAMS* p_encoder_params =
437 &a2dp_ldac_encoder_cb.ldac_encoder_params;
438 uint8_t remain_nb_frame = nb_frame;
439 uint16_t ldac_frame_size;
440 uint8_t read_buffer[LDACBT_MAX_LSU * 4 /* byte/sample */ * 2 /* ch */];
441
442 switch (p_encoder_params->sample_rate) {
443 case 176400:
444 case 192000:
445 ldac_frame_size = 512; // sample/ch
446 break;
447 case 88200:
448 case 96000:
449 ldac_frame_size = 256; // sample/ch
450 break;
451 case 44100:
452 case 48000:
453 default:
454 ldac_frame_size = 128; // sample/ch
455 break;
456 }
457
458 uint32_t count;
459 int32_t encode_count = 0;
460 int32_t out_frames = 0;
461 int written = 0;
462
463 uint32_t bytes_read = 0;
464 while (nb_frame) {
465 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
466 p_buf->offset = A2DP_LDAC_OFFSET;
467 p_buf->len = 0;
468 p_buf->layer_specific = 0;
469 a2dp_ldac_encoder_cb.stats.media_read_total_expected_packets++;
470
471 count = 0;
472 do {
473 //
474 // Read the PCM data and encode it
475 //
476 uint32_t temp_bytes_read = 0;
477 if (a2dp_ldac_read_feeding(read_buffer, &temp_bytes_read)) {
478 bytes_read += temp_bytes_read;
479 uint8_t* packet = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len;
480 if (a2dp_ldac_encoder_cb.ldac_handle == NULL) {
481 LOG_ERROR("%s: invalid LDAC handle", __func__);
482 a2dp_ldac_encoder_cb.stats.media_read_total_dropped_packets++;
483 osi_free(p_buf);
484 return;
485 }
486 int result = ldacBT_encode(
487 a2dp_ldac_encoder_cb.ldac_handle, read_buffer, (int*)&encode_count,
488 packet + count, (int*)&written, (int*)&out_frames);
489 if (result != 0) {
490 int err_code =
491 ldacBT_get_error_code(a2dp_ldac_encoder_cb.ldac_handle);
492 LOG_ERROR(
493 "%s: LDAC encoding error: %d api_error = %d "
494 "handle_error = %d block_error = %d error_code = 0x%x",
495 __func__, result, LDACBT_API_ERR(err_code),
496 LDACBT_HANDLE_ERR(err_code), LDACBT_BLOCK_ERR(err_code),
497 err_code);
498 a2dp_ldac_encoder_cb.stats.media_read_total_dropped_packets++;
499 osi_free(p_buf);
500 return;
501 }
502 count += written;
503 p_buf->len += written;
504 nb_frame--;
505 p_buf->layer_specific += out_frames; // added a frame to the buffer
506 } else {
507 LOG_WARN("%s: underflow %d", __func__, nb_frame);
508 a2dp_ldac_encoder_cb.ldac_feeding_state.counter +=
509 nb_frame * LDACBT_ENC_LSU *
510 a2dp_ldac_encoder_cb.feeding_params.channel_count *
511 a2dp_ldac_encoder_cb.feeding_params.bits_per_sample / 8;
512
513 // no more pcm to read
514 nb_frame = 0;
515 }
516 } while ((written == 0) && nb_frame);
517
518 if (p_buf->len) {
519 /*
520 * Timestamp of the media packet header represent the TS of the
521 * first frame, i.e the timestamp before including this frame.
522 */
523 *((uint32_t*)(p_buf + 1)) = a2dp_ldac_encoder_cb.timestamp;
524
525 a2dp_ldac_encoder_cb.timestamp += p_buf->layer_specific * ldac_frame_size;
526
527 uint8_t done_nb_frame = remain_nb_frame - nb_frame;
528 remain_nb_frame = nb_frame;
529 if (!a2dp_ldac_encoder_cb.enqueue_callback(p_buf, done_nb_frame,
530 bytes_read))
531 return;
532 } else {
533 // NOTE: Unlike the execution path for other codecs, it is normal for
534 // LDAC to NOT write encoded data to the last buffer if there wasn't
535 // enough data to write to. That data is accumulated internally by
536 // the codec and included in the next iteration. Therefore, here we
537 // don't increment the "media_read_total_dropped_packets" counter.
538 osi_free(p_buf);
539 }
540 }
541 }
542
a2dp_ldac_read_feeding(uint8_t * read_buffer,uint32_t * bytes_read)543 static bool a2dp_ldac_read_feeding(uint8_t* read_buffer, uint32_t* bytes_read) {
544 uint32_t read_size = LDACBT_ENC_LSU *
545 a2dp_ldac_encoder_cb.feeding_params.channel_count *
546 a2dp_ldac_encoder_cb.feeding_params.bits_per_sample / 8;
547
548 a2dp_ldac_encoder_cb.stats.media_read_total_expected_reads_count++;
549 a2dp_ldac_encoder_cb.stats.media_read_total_expected_read_bytes += read_size;
550
551 /* Read Data from UIPC channel */
552 uint32_t nb_byte_read =
553 a2dp_ldac_encoder_cb.read_callback(read_buffer, read_size);
554 a2dp_ldac_encoder_cb.stats.media_read_total_actual_read_bytes += nb_byte_read;
555
556 if (nb_byte_read < read_size) {
557 if (nb_byte_read == 0) return false;
558
559 /* Fill the unfilled part of the read buffer with silence (0) */
560 memset(((uint8_t*)read_buffer) + nb_byte_read, 0, read_size - nb_byte_read);
561 nb_byte_read = read_size;
562 }
563 a2dp_ldac_encoder_cb.stats.media_read_total_actual_reads_count++;
564
565 *bytes_read = nb_byte_read;
566 return true;
567 }
568
adjust_effective_mtu(const tA2DP_ENCODER_INIT_PEER_PARAMS & peer_params)569 static uint16_t adjust_effective_mtu(
570 const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params) {
571 uint16_t mtu_size =
572 BT_DEFAULT_BUFFER_SIZE - A2DP_LDAC_OFFSET - sizeof(BT_HDR);
573 if (mtu_size > peer_params.peer_mtu) {
574 mtu_size = peer_params.peer_mtu;
575 }
576 LOG_VERBOSE("%s: original AVDTP MTU size: %d", __func__, mtu_size);
577 return mtu_size;
578 }
579
quality_mode_index_to_name(int quality_mode_index)580 static std::string quality_mode_index_to_name(int quality_mode_index) {
581 switch (quality_mode_index) {
582 case A2DP_LDAC_QUALITY_HIGH:
583 return "HIGH";
584 case A2DP_LDAC_QUALITY_MID:
585 return "MID";
586 case A2DP_LDAC_QUALITY_LOW:
587 return "LOW";
588 case A2DP_LDAC_QUALITY_ABR:
589 return "ABR";
590 default:
591 return "Unknown";
592 }
593 }
594
a2dp_vendor_ldac_set_transmit_queue_length(size_t transmit_queue_length)595 void a2dp_vendor_ldac_set_transmit_queue_length(size_t transmit_queue_length) {
596 a2dp_ldac_encoder_cb.TxQueueLength = transmit_queue_length;
597 }
598
debug_codec_dump(int fd)599 void A2dpCodecConfigLdacSource::debug_codec_dump(int fd) {
600 a2dp_ldac_encoder_stats_t* stats = &a2dp_ldac_encoder_cb.stats;
601 tA2DP_LDAC_ENCODER_PARAMS* p_encoder_params =
602 &a2dp_ldac_encoder_cb.ldac_encoder_params;
603
604 A2dpCodecConfig::debug_codec_dump(fd);
605
606 dprintf(
607 fd, " LDAC quality mode : %s\n",
608 quality_mode_index_to_name(p_encoder_params->quality_mode_index).c_str());
609
610 dprintf(fd,
611 " LDAC transmission bitrate (Kbps) : %d\n",
612 ldacBT_get_bitrate(a2dp_ldac_encoder_cb.ldac_handle));
613
614 dprintf(fd,
615 " LDAC saved transmit queue length : %zu\n",
616 a2dp_ldac_encoder_cb.TxQueueLength);
617 if (a2dp_ldac_encoder_cb.has_ldac_abr_handle) {
618 dprintf(fd,
619 " LDAC adaptive bit rate encode quality mode index : %d\n",
620 a2dp_ldac_encoder_cb.last_ldac_abr_eqmid);
621 dprintf(fd,
622 " LDAC adaptive bit rate adjustments : %zu\n",
623 a2dp_ldac_encoder_cb.ldac_abr_adjustments);
624 }
625 dprintf(fd, " Encoder interval (ms): %" PRIu64 "\n",
626 a2dp_vendor_ldac_get_encoder_interval_ms());
627 dprintf(fd, " Effective MTU: %d\n",
628 a2dp_vendor_ldac_get_effective_frame_size());
629 dprintf(fd,
630 " Packet counts (expected/dropped) : %zu / "
631 "%zu\n",
632 stats->media_read_total_expected_packets,
633 stats->media_read_total_dropped_packets);
634
635 dprintf(fd,
636 " PCM read counts (expected/actual) : %zu / "
637 "%zu\n",
638 stats->media_read_total_expected_reads_count,
639 stats->media_read_total_actual_reads_count);
640
641 dprintf(fd,
642 " PCM read bytes (expected/actual) : %zu / "
643 "%zu\n",
644 stats->media_read_total_expected_read_bytes,
645 stats->media_read_total_actual_read_bytes);
646 }
647