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_aptx_encoder" 18 19 #include "a2dp_vendor_aptx_encoder.h" 20 21 #include <dlfcn.h> 22 #include <inttypes.h> 23 #include <stdio.h> 24 #include <string.h> 25 26 #include "a2dp_vendor.h" 27 #include "a2dp_vendor_aptx.h" 28 #include "bt_common.h" 29 #include "osi/include/log.h" 30 #include "osi/include/osi.h" 31 32 // 33 // Encoder for aptX Source Codec 34 // 35 36 // 37 // The aptX encoder shared library, and the functions to use 38 // 39 static const char* APTX_ENCODER_LIB_NAME = "libaptX_encoder.so"; 40 static void* aptx_encoder_lib_handle = NULL; 41 42 static const char* APTX_ENCODER_INIT_NAME = "aptxbtenc_init"; 43 typedef int (*tAPTX_ENCODER_INIT)(void* state, short endian); 44 45 static const char* APTX_ENCODER_ENCODE_STEREO_NAME = "aptxbtenc_encodestereo"; 46 typedef int (*tAPTX_ENCODER_ENCODE_STEREO)(void* state, void* pcmL, void* pcmR, 47 void* buffer); 48 49 static const char* APTX_ENCODER_SIZEOF_PARAMS_NAME = "SizeofAptxbtenc"; 50 typedef int (*tAPTX_ENCODER_SIZEOF_PARAMS)(void); 51 52 static tAPTX_ENCODER_INIT aptx_encoder_init_func; 53 static tAPTX_ENCODER_ENCODE_STEREO aptx_encoder_encode_stereo_func; 54 static tAPTX_ENCODER_SIZEOF_PARAMS aptx_encoder_sizeof_params_func; 55 56 // offset 57 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 58 #define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET + 1) 59 #else 60 // no RTP header for aptX classic 61 #define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET - AVDT_MEDIA_HDR_SIZE) 62 #endif 63 64 #define A2DP_APTX_MAX_PCM_BYTES_PER_READ 4096 65 66 typedef struct { 67 uint64_t sleep_time_ns; 68 uint32_t pcm_reads; 69 uint32_t pcm_bytes_per_read; 70 uint32_t aptx_bytes; 71 uint32_t frame_size_counter; 72 } tAPTX_FRAMING_PARAMS; 73 74 typedef struct { 75 uint64_t session_start_us; 76 77 size_t media_read_total_expected_packets; 78 size_t media_read_total_expected_reads_count; 79 size_t media_read_total_expected_read_bytes; 80 81 size_t media_read_total_dropped_packets; 82 size_t media_read_total_actual_reads_count; 83 size_t media_read_total_actual_read_bytes; 84 } a2dp_aptx_encoder_stats_t; 85 86 typedef struct { 87 a2dp_source_read_callback_t read_callback; 88 a2dp_source_enqueue_callback_t enqueue_callback; 89 90 bool use_SCMS_T; 91 bool is_peer_edr; // True if the peer device supports EDR 92 bool peer_supports_3mbps; // True if the peer device supports 3Mbps EDR 93 uint16_t peer_mtu; // MTU of the A2DP peer 94 uint32_t timestamp; // Timestamp for the A2DP frames 95 96 tA2DP_FEEDING_PARAMS feeding_params; 97 tAPTX_FRAMING_PARAMS framing_params; 98 void* aptx_encoder_state; 99 a2dp_aptx_encoder_stats_t stats; 100 } tA2DP_APTX_ENCODER_CB; 101 102 static tA2DP_APTX_ENCODER_CB a2dp_aptx_encoder_cb; 103 104 static void a2dp_vendor_aptx_encoder_update(uint16_t peer_mtu, 105 A2dpCodecConfig* a2dp_codec_config, 106 bool* p_restart_input, 107 bool* p_restart_output, 108 bool* p_config_updated); 109 static void aptx_init_framing_params(tAPTX_FRAMING_PARAMS* framing_params); 110 static void aptx_update_framing_params(tAPTX_FRAMING_PARAMS* framing_params); 111 static size_t aptx_encode_16bit(tAPTX_FRAMING_PARAMS* framing_params, 112 size_t* data_out_index, uint16_t* data16_in, 113 uint8_t* data_out); 114 115 bool A2DP_VendorLoadEncoderAptx(void) { 116 if (aptx_encoder_lib_handle != NULL) return true; // Already loaded 117 118 // Open the encoder library 119 aptx_encoder_lib_handle = dlopen(APTX_ENCODER_LIB_NAME, RTLD_NOW); 120 if (aptx_encoder_lib_handle == NULL) { 121 LOG_ERROR(LOG_TAG, "%s: cannot open aptX encoder library %s: %s", __func__, 122 APTX_ENCODER_LIB_NAME, dlerror()); 123 return false; 124 } 125 126 aptx_encoder_init_func = (tAPTX_ENCODER_INIT)dlsym(aptx_encoder_lib_handle, 127 APTX_ENCODER_INIT_NAME); 128 if (aptx_encoder_init_func == NULL) { 129 LOG_ERROR(LOG_TAG, 130 "%s: cannot find function '%s' in the encoder library: %s", 131 __func__, APTX_ENCODER_INIT_NAME, dlerror()); 132 A2DP_VendorUnloadEncoderAptx(); 133 return false; 134 } 135 136 aptx_encoder_encode_stereo_func = (tAPTX_ENCODER_ENCODE_STEREO)dlsym( 137 aptx_encoder_lib_handle, APTX_ENCODER_ENCODE_STEREO_NAME); 138 if (aptx_encoder_encode_stereo_func == NULL) { 139 LOG_ERROR(LOG_TAG, 140 "%s: cannot find function '%s' in the encoder library: %s", 141 __func__, APTX_ENCODER_ENCODE_STEREO_NAME, dlerror()); 142 A2DP_VendorUnloadEncoderAptx(); 143 return false; 144 } 145 146 aptx_encoder_sizeof_params_func = (tAPTX_ENCODER_SIZEOF_PARAMS)dlsym( 147 aptx_encoder_lib_handle, APTX_ENCODER_SIZEOF_PARAMS_NAME); 148 if (aptx_encoder_sizeof_params_func == NULL) { 149 LOG_ERROR(LOG_TAG, 150 "%s: cannot find function '%s' in the encoder library: %s", 151 __func__, APTX_ENCODER_SIZEOF_PARAMS_NAME, dlerror()); 152 A2DP_VendorUnloadEncoderAptx(); 153 return false; 154 } 155 156 return true; 157 } 158 159 void A2DP_VendorUnloadEncoderAptx(void) { 160 aptx_encoder_init_func = NULL; 161 aptx_encoder_encode_stereo_func = NULL; 162 aptx_encoder_sizeof_params_func = NULL; 163 164 if (aptx_encoder_lib_handle != NULL) { 165 dlclose(aptx_encoder_lib_handle); 166 aptx_encoder_lib_handle = NULL; 167 } 168 } 169 170 void a2dp_vendor_aptx_encoder_init( 171 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 172 A2dpCodecConfig* a2dp_codec_config, 173 a2dp_source_read_callback_t read_callback, 174 a2dp_source_enqueue_callback_t enqueue_callback) { 175 memset(&a2dp_aptx_encoder_cb, 0, sizeof(a2dp_aptx_encoder_cb)); 176 177 a2dp_aptx_encoder_cb.stats.session_start_us = time_get_os_boottime_us(); 178 179 a2dp_aptx_encoder_cb.read_callback = read_callback; 180 a2dp_aptx_encoder_cb.enqueue_callback = enqueue_callback; 181 a2dp_aptx_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr; 182 a2dp_aptx_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps; 183 a2dp_aptx_encoder_cb.peer_mtu = p_peer_params->peer_mtu; 184 a2dp_aptx_encoder_cb.timestamp = 0; 185 186 /* aptX encoder config */ 187 a2dp_aptx_encoder_cb.use_SCMS_T = false; // TODO: should be a parameter 188 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 189 a2dp_aptx_encoder_cb.use_SCMS_T = true; 190 #endif 191 192 a2dp_aptx_encoder_cb.aptx_encoder_state = 193 osi_malloc(aptx_encoder_sizeof_params_func()); 194 if (a2dp_aptx_encoder_cb.aptx_encoder_state != NULL) { 195 aptx_encoder_init_func(a2dp_aptx_encoder_cb.aptx_encoder_state, 0); 196 } else { 197 LOG_ERROR(LOG_TAG, "%s: Cannot allocate aptX encoder state", __func__); 198 // TODO: Return an error? 199 } 200 201 // NOTE: Ignore the restart_input / restart_output flags - this initization 202 // happens when the connection is (re)started. 203 bool restart_input = false; 204 bool restart_output = false; 205 bool config_updated = false; 206 a2dp_vendor_aptx_encoder_update(a2dp_aptx_encoder_cb.peer_mtu, 207 a2dp_codec_config, &restart_input, 208 &restart_output, &config_updated); 209 } 210 211 bool A2dpCodecConfigAptx::updateEncoderUserConfig( 212 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, bool* p_restart_input, 213 bool* p_restart_output, bool* p_config_updated) { 214 a2dp_aptx_encoder_cb.is_peer_edr = p_peer_params->is_peer_edr; 215 a2dp_aptx_encoder_cb.peer_supports_3mbps = p_peer_params->peer_supports_3mbps; 216 a2dp_aptx_encoder_cb.peer_mtu = p_peer_params->peer_mtu; 217 a2dp_aptx_encoder_cb.timestamp = 0; 218 219 if (a2dp_aptx_encoder_cb.peer_mtu == 0) { 220 LOG_ERROR(LOG_TAG, 221 "%s: Cannot update the codec encoder for %s: " 222 "invalid peer MTU", 223 __func__, name().c_str()); 224 return false; 225 } 226 227 a2dp_vendor_aptx_encoder_update(a2dp_aptx_encoder_cb.peer_mtu, this, 228 p_restart_input, p_restart_output, 229 p_config_updated); 230 return true; 231 } 232 233 // Update the A2DP aptX encoder. 234 // |peer_mtu| is the peer MTU. 235 // |a2dp_codec_config| is the A2DP codec to use for the update. 236 static void a2dp_vendor_aptx_encoder_update(uint16_t peer_mtu, 237 A2dpCodecConfig* a2dp_codec_config, 238 bool* p_restart_input, 239 bool* p_restart_output, 240 bool* p_config_updated) { 241 uint8_t codec_info[AVDT_CODEC_SIZE]; 242 243 *p_restart_input = false; 244 *p_restart_output = false; 245 *p_config_updated = false; 246 if (!a2dp_codec_config->copyOutOtaCodecConfig(codec_info)) { 247 LOG_ERROR(LOG_TAG, 248 "%s: Cannot update the codec encoder for %s: " 249 "invalid codec config", 250 __func__, a2dp_codec_config->name().c_str()); 251 return; 252 } 253 const uint8_t* p_codec_info = codec_info; 254 255 // The feeding parameters 256 tA2DP_FEEDING_PARAMS* p_feeding_params = &a2dp_aptx_encoder_cb.feeding_params; 257 p_feeding_params->sample_rate = 258 A2DP_VendorGetTrackSampleRateAptx(p_codec_info); 259 p_feeding_params->bits_per_sample = 260 a2dp_codec_config->getAudioBitsPerSample(); 261 p_feeding_params->channel_count = 262 A2DP_VendorGetTrackChannelCountAptx(p_codec_info); 263 LOG_DEBUG(LOG_TAG, "%s: sample_rate=%u bits_per_sample=%u channel_count=%u", 264 __func__, p_feeding_params->sample_rate, 265 p_feeding_params->bits_per_sample, p_feeding_params->channel_count); 266 a2dp_vendor_aptx_feeding_reset(); 267 } 268 269 void a2dp_vendor_aptx_encoder_cleanup(void) { 270 osi_free(a2dp_aptx_encoder_cb.aptx_encoder_state); 271 memset(&a2dp_aptx_encoder_cb, 0, sizeof(a2dp_aptx_encoder_cb)); 272 } 273 274 // 275 // Initialize the framing parameters, and set those that don't change 276 // while streaming (e.g., 'sleep_time_ns'). 277 // 278 static void aptx_init_framing_params(tAPTX_FRAMING_PARAMS* framing_params) { 279 framing_params->sleep_time_ns = 0; 280 framing_params->pcm_reads = 0; 281 framing_params->pcm_bytes_per_read = 0; 282 framing_params->aptx_bytes = 0; 283 framing_params->frame_size_counter = 0; 284 285 if (a2dp_aptx_encoder_cb.feeding_params.sample_rate == 48000) { 286 if (a2dp_aptx_encoder_cb.use_SCMS_T) { 287 framing_params->sleep_time_ns = 13000000; 288 } else { 289 framing_params->sleep_time_ns = 14000000; 290 } 291 } else { 292 // Assume the sample rate is 44100 293 if (a2dp_aptx_encoder_cb.use_SCMS_T) { 294 framing_params->sleep_time_ns = 14000000; 295 } else { 296 framing_params->sleep_time_ns = 15000000; 297 } 298 } 299 300 LOG_DEBUG(LOG_TAG, "%s: sleep_time_ns = %" PRIu64, __func__, 301 framing_params->sleep_time_ns); 302 } 303 304 // 305 // Set frame size and transmission interval needed to stream the required 306 // sample rate using 2-DH5 packets for aptX and 2-DH3 packets for aptX-LL. 307 // With SCMS-T enabled we need to reserve room for extra headers added later. 308 // Packets are always sent at equals time intervals but to achieve the 309 // required sample rate, the frame size needs to change on occasion. 310 // 311 // Also need to specify how many of the required PCM samples are read at a 312 // time: 313 // aptx_bytes = pcm_reads * pcm_bytes_per_read / 4 314 // and 315 // number of aptX samples produced = pcm_bytes_per_read / 16 316 // 317 static void aptx_update_framing_params(tAPTX_FRAMING_PARAMS* framing_params) { 318 if (a2dp_aptx_encoder_cb.feeding_params.sample_rate == 48000) { 319 if (a2dp_aptx_encoder_cb.use_SCMS_T) { 320 framing_params->aptx_bytes = 624; 321 framing_params->pcm_bytes_per_read = 208; 322 framing_params->pcm_reads = 12; 323 } else { 324 framing_params->aptx_bytes = 672; 325 framing_params->pcm_bytes_per_read = 224; 326 framing_params->pcm_reads = 12; 327 } 328 } else { 329 // Assume the sample rate is 44100 330 if (a2dp_aptx_encoder_cb.use_SCMS_T) { 331 if (++framing_params->frame_size_counter < 20) { 332 framing_params->aptx_bytes = 616; 333 framing_params->pcm_bytes_per_read = 224; 334 framing_params->pcm_reads = 11; 335 } else { 336 framing_params->aptx_bytes = 644; 337 framing_params->pcm_bytes_per_read = 368; 338 framing_params->pcm_reads = 7; 339 framing_params->frame_size_counter = 0; 340 } 341 } else { 342 if (++framing_params->frame_size_counter < 8) { 343 framing_params->aptx_bytes = 660; 344 framing_params->pcm_bytes_per_read = 240; 345 framing_params->pcm_reads = 11; 346 } else { 347 framing_params->aptx_bytes = 672; 348 framing_params->pcm_bytes_per_read = 224; 349 framing_params->pcm_reads = 12; 350 framing_params->frame_size_counter = 0; 351 } 352 } 353 } 354 355 LOG_VERBOSE(LOG_TAG, 356 "%s: sleep_time_ns = %" PRIu64 357 " aptx_bytes = %u " 358 "pcm_bytes_per_read = %u pcm_reads = %u frame_size_counter = %u", 359 __func__, framing_params->sleep_time_ns, 360 framing_params->aptx_bytes, framing_params->pcm_bytes_per_read, 361 framing_params->pcm_reads, framing_params->frame_size_counter); 362 } 363 364 void a2dp_vendor_aptx_feeding_reset(void) { 365 aptx_init_framing_params(&a2dp_aptx_encoder_cb.framing_params); 366 } 367 368 void a2dp_vendor_aptx_feeding_flush(void) { 369 aptx_init_framing_params(&a2dp_aptx_encoder_cb.framing_params); 370 } 371 372 period_ms_t a2dp_vendor_aptx_get_encoder_interval_ms(void) { 373 return a2dp_aptx_encoder_cb.framing_params.sleep_time_ns / (1000 * 1000); 374 } 375 376 void a2dp_vendor_aptx_send_frames(uint64_t timestamp_us) { 377 tAPTX_FRAMING_PARAMS* framing_params = &a2dp_aptx_encoder_cb.framing_params; 378 379 // Prepare the packet to send 380 BT_HDR* p_buf = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE); 381 p_buf->offset = A2DP_APTX_OFFSET; 382 p_buf->len = 0; 383 p_buf->layer_specific = 0; 384 385 uint8_t* encoded_ptr = (uint8_t*)(p_buf + 1); 386 encoded_ptr += p_buf->offset; 387 388 aptx_update_framing_params(framing_params); 389 390 // 391 // Read the PCM data and encode it 392 // 393 uint16_t read_buffer16[A2DP_APTX_MAX_PCM_BYTES_PER_READ / sizeof(uint16_t)]; 394 uint32_t expected_read_bytes = 395 framing_params->pcm_reads * framing_params->pcm_bytes_per_read; 396 size_t encoded_ptr_index = 0; 397 size_t pcm_bytes_encoded = 0; 398 uint32_t bytes_read = 0; 399 400 a2dp_aptx_encoder_cb.stats.media_read_total_expected_packets++; 401 a2dp_aptx_encoder_cb.stats.media_read_total_expected_reads_count++; 402 a2dp_aptx_encoder_cb.stats.media_read_total_expected_read_bytes += 403 expected_read_bytes; 404 405 LOG_VERBOSE(LOG_TAG, "%s: PCM read of size %u", __func__, 406 expected_read_bytes); 407 bytes_read = a2dp_aptx_encoder_cb.read_callback((uint8_t*)read_buffer16, 408 expected_read_bytes); 409 a2dp_aptx_encoder_cb.stats.media_read_total_actual_read_bytes += bytes_read; 410 if (bytes_read < expected_read_bytes) { 411 LOG_WARN(LOG_TAG, 412 "%s: underflow at PCM reading: read %u bytes instead of %u", 413 __func__, bytes_read, expected_read_bytes); 414 a2dp_aptx_encoder_cb.stats.media_read_total_dropped_packets++; 415 osi_free(p_buf); 416 return; 417 } 418 a2dp_aptx_encoder_cb.stats.media_read_total_actual_reads_count++; 419 420 for (uint32_t reads = 0, offset = 0; reads < framing_params->pcm_reads; 421 reads++, offset += 422 (framing_params->pcm_bytes_per_read / sizeof(uint16_t))) { 423 pcm_bytes_encoded += aptx_encode_16bit(framing_params, &encoded_ptr_index, 424 read_buffer16 + offset, encoded_ptr); 425 } 426 427 // Compute the number of encoded bytes 428 const int COMPRESSION_RATIO = 4; 429 size_t encoded_bytes = pcm_bytes_encoded / COMPRESSION_RATIO; 430 p_buf->len += encoded_bytes; 431 LOG_VERBOSE(LOG_TAG, "%s: encoded %zu PCM bytes to %zu", __func__, 432 pcm_bytes_encoded, encoded_bytes); 433 434 // Update the RTP timestamp 435 *((uint32_t*)(p_buf + 1)) = a2dp_aptx_encoder_cb.timestamp; 436 const uint8_t BYTES_PER_FRAME = 2; 437 uint32_t rtp_timestamp = 438 (pcm_bytes_encoded / a2dp_aptx_encoder_cb.feeding_params.channel_count) / 439 BYTES_PER_FRAME; 440 a2dp_aptx_encoder_cb.timestamp += rtp_timestamp; 441 442 if (p_buf->len > 0) { 443 a2dp_aptx_encoder_cb.enqueue_callback(p_buf, 1, bytes_read); 444 } else { 445 a2dp_aptx_encoder_cb.stats.media_read_total_dropped_packets++; 446 osi_free(p_buf); 447 } 448 } 449 450 static size_t aptx_encode_16bit(tAPTX_FRAMING_PARAMS* framing_params, 451 size_t* data_out_index, uint16_t* data16_in, 452 uint8_t* data_out) { 453 size_t pcm_bytes_encoded = 0; 454 size_t frame = 0; 455 456 for (size_t aptx_samples = 0; 457 aptx_samples < framing_params->pcm_bytes_per_read / 16; aptx_samples++) { 458 uint32_t pcmL[4]; 459 uint32_t pcmR[4]; 460 uint16_t encoded_sample[2]; 461 462 for (size_t i = 0, j = frame; i < 4; i++, j++) { 463 pcmL[i] = (uint16_t) * (data16_in + (2 * j)); 464 pcmR[i] = (uint16_t) * (data16_in + ((2 * j) + 1)); 465 } 466 467 aptx_encoder_encode_stereo_func(a2dp_aptx_encoder_cb.aptx_encoder_state, 468 &pcmL, &pcmR, &encoded_sample); 469 470 data_out[*data_out_index + 0] = (uint8_t)((encoded_sample[0] >> 8) & 0xff); 471 data_out[*data_out_index + 1] = (uint8_t)((encoded_sample[0] >> 0) & 0xff); 472 data_out[*data_out_index + 2] = (uint8_t)((encoded_sample[1] >> 8) & 0xff); 473 data_out[*data_out_index + 3] = (uint8_t)((encoded_sample[1] >> 0) & 0xff); 474 475 frame += 4; 476 pcm_bytes_encoded += 16; 477 *data_out_index += 4; 478 } 479 480 return pcm_bytes_encoded; 481 } 482 483 period_ms_t A2dpCodecConfigAptx::encoderIntervalMs() const { 484 return a2dp_vendor_aptx_get_encoder_interval_ms(); 485 } 486 487 int A2dpCodecConfigAptx::getEffectiveMtu() const { 488 return a2dp_aptx_encoder_cb.peer_mtu; 489 } 490 491 void A2dpCodecConfigAptx::debug_codec_dump(int fd) { 492 a2dp_aptx_encoder_stats_t* stats = &a2dp_aptx_encoder_cb.stats; 493 494 A2dpCodecConfig::debug_codec_dump(fd); 495 496 dprintf(fd, 497 " Packet counts (expected/dropped) : %zu / " 498 "%zu\n", 499 stats->media_read_total_expected_packets, 500 stats->media_read_total_dropped_packets); 501 502 dprintf(fd, 503 " PCM read counts (expected/actual) : %zu / " 504 "%zu\n", 505 stats->media_read_total_expected_reads_count, 506 stats->media_read_total_actual_reads_count); 507 508 dprintf(fd, 509 " PCM read bytes (expected/actual) : %zu / " 510 "%zu\n", 511 stats->media_read_total_expected_read_bytes, 512 stats->media_read_total_actual_read_bytes); 513 } 514