1 /* 2 * Copyright (C) {copyright_year} BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "le_audio_demo_util_sink.c" 39 40 #include <stdio.h> 41 42 #include "le_audio_demo_util_sink.h" 43 44 #include "btstack_bool.h" 45 #include "btstack_config.h" 46 #include <btstack_debug.h> 47 #include <printf.h> 48 49 #include "hci.h" 50 #include "btstack_audio.h" 51 #include "btstack_lc3_google.h" 52 #include "btstack_lc3plus_fraunhofer.h" 53 54 #include "btstack_sample_rate_compensation.h" 55 #include "btstack_resample.h" 56 57 #include "hxcmod.h" 58 #include "mods/mod.h" 59 60 #ifdef HAVE_POSIX_FILE_IO 61 #include "wav_util.h" 62 #include "btstack_ring_buffer.h" 63 64 #endif 65 66 //#define DEBUG_PLC 67 #ifdef DEBUG_PLC 68 #define printf_plc(...) { \ 69 printf(__VA_ARGS__); \ 70 log_info(__VA_ARGS__);\ 71 } 72 #else 73 #define printf_plc(...) (void)(0); 74 #endif 75 76 #define MAX_CHANNELS 2 77 #define MAX_SAMPLES_PER_FRAME 480 78 #define MAX_LC3_FRAME_BYTES 155 79 80 // playback 81 #define MAX_NUM_LC3_FRAMES 15 82 #define MAX_BYTES_PER_SAMPLE 4 83 #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_CHANNELS * MAX_BYTES_PER_SAMPLE) 84 #define PLAYBACK_START_MS (MAX_NUM_LC3_FRAMES * 20 / 3) 85 86 #define ANSI_COLOR_RED "\x1b[31m" 87 #define ANSI_COLOR_GREEN "\x1b[32m" 88 #define ANSI_COLOR_YELLOW "\x1b[33m" 89 #define ANSI_COLOR_BLUE "\x1b[34m" 90 #define ANSI_COLOR_MAGENTA "\x1b[35m" 91 #define ANSI_COLOR_CYAN "\x1b[36m" 92 #define ANSI_COLOR_RESET "\x1b[0m" 93 94 // SINK 95 96 static const char * le_audio_demo_sink_filename_wav; 97 static btstack_sample_rate_compensation_t sample_rate_compensation; 98 static btstack_resample_t resample_instance; 99 static bool sink_receive_streaming; 100 101 static int16_t pcm_resample[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME * 2]; 102 103 104 static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration; 105 static hci_iso_type_t le_audio_demo_sink_type; 106 107 static uint32_t le_audio_demo_sink_sampling_frequency_hz; 108 static uint16_t le_audio_demo_sink_num_samples_per_frame; 109 static uint8_t le_audio_demo_sink_num_streams; 110 static uint8_t le_audio_demo_sink_num_channels_per_stream; 111 static uint8_t le_audio_demo_sink_num_channels; 112 static uint16_t le_audio_demo_sink_octets_per_frame; 113 static uint16_t le_audio_demo_sink_iso_interval_1250us; 114 static uint8_t le_audio_demo_sink_flush_timeout; 115 static uint8_t le_audio_demo_sink_pre_transmission_offset; 116 117 // playback 118 static uint16_t playback_start_threshold_bytes; 119 static bool playback_active; 120 static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 121 static btstack_ring_buffer_t playback_buffer; 122 123 // PLC 124 static bool stream_last_packet_received[MAX_CHANNELS]; 125 static uint16_t stream_last_packet_sequence[MAX_CHANNELS]; 126 static uint16_t group_last_packet_sequence; 127 static bool group_last_packet_received; 128 static uint16_t plc_timeout_initial_ms; 129 static uint16_t plc_timeout_subsequent_ms; 130 131 static uint32_t le_audio_demo_sink_lc3_frames; 132 static uint32_t samples_received; 133 static uint32_t samples_played; 134 static uint32_t samples_dropped; 135 136 static btstack_timer_source_t next_packet_timer; 137 138 // lc3 decoder 139 static bool le_audio_demo_lc3plus_decoder_requested = false; 140 static const btstack_lc3_decoder_t * lc3_decoder; 141 static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME]; 142 static bool have_pcm[MAX_CHANNELS]; 143 144 static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS]; 145 #ifdef HAVE_LC3PLUS 146 static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS]; 147 #endif 148 static void * decoder_contexts[MAX_CHANNELS]; 149 150 static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){ 151 // called from lower-layer but guaranteed to be on main thread 152 log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 153 154 samples_played += num_samples; 155 156 uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2; 157 if (playback_active == false){ 158 if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) { 159 log_info("Playback started"); 160 playback_active = true; 161 } 162 } else { 163 if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) { 164 log_info("Playback underrun"); 165 printf("Playback Underrun\n"); 166 // empty buffer 167 uint32_t bytes_read; 168 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 169 playback_active = false; 170 } 171 } 172 173 if (playback_active){ 174 uint32_t bytes_read; 175 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 176 btstack_assert(bytes_read == bytes_needed); 177 } else { 178 memset(buffer, 0, bytes_needed); 179 } 180 } 181 182 static void store_samples_in_ringbuffer(void){ 183 // check if we have all channels 184 uint8_t channel; 185 for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){ 186 if (have_pcm[channel] == false) return; 187 } 188 #ifdef HAVE_POSIX_FILE_IO 189 // write wav samples 190 wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm); 191 #endif 192 // store samples in playback buffer 193 samples_received += le_audio_demo_sink_num_samples_per_frame; 194 uint32_t resampled_frames = btstack_resample_block(&resample_instance, pcm, le_audio_demo_sink_num_samples_per_frame, pcm_resample); 195 uint32_t bytes_to_store = resampled_frames * le_audio_demo_sink_num_channels * 2; 196 197 if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 198 btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm_resample, bytes_to_store); 199 } else { 200 printf("Samples dropped\n"); 201 samples_dropped += le_audio_demo_sink_num_samples_per_frame; 202 } 203 memset(have_pcm, 0, sizeof(have_pcm)); 204 } 205 206 static void plc_do(uint8_t stream_index) { 207 // inject packet 208 uint8_t tmp_BEC_detect; 209 uint8_t BFI = 1; 210 uint8_t i; 211 for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){ 212 uint8_t effective_channel = stream_index + i; 213 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI, 214 &pcm[effective_channel], le_audio_demo_sink_num_channels, 215 &tmp_BEC_detect); 216 } 217 // and store in ringbuffer when PCM for all channels is available 218 store_samples_in_ringbuffer(); 219 } 220 221 // 222 // Perform PLC for packets missing in previous intervals 223 // 224 // assumptions: 225 // - packet sequence number is monotonic increasing 226 // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed 227 static void plc_check(uint16_t packet_sequence_number) { 228 while (group_last_packet_sequence != packet_sequence_number){ 229 uint8_t i; 230 for (i=0;i<le_audio_demo_sink_num_streams;i++){ 231 // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start 232 if (stream_last_packet_received[i] == false){ 233 printf_plc("- ISO #%u, very first packet missing\n", i); 234 have_pcm[i] = true; 235 store_samples_in_ringbuffer(); 236 237 stream_last_packet_received[i] = true; 238 stream_last_packet_sequence[i] = group_last_packet_sequence; 239 continue; 240 } 241 242 // missing packet if big sequence counter is higher than bis sequence counter 243 if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) { 244 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence); 245 plc_do(i); 246 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence); 247 stream_last_packet_sequence[i] = group_last_packet_sequence; 248 } 249 } 250 group_last_packet_sequence++; 251 } 252 } 253 254 static void plc_timeout(btstack_timer_source_t * timer) { 255 // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets 256 btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms); 257 btstack_run_loop_set_timer_handler(timer, plc_timeout); 258 btstack_run_loop_add_timer(timer); 259 260 switch (le_audio_demo_sink_type){ 261 case HCI_ISO_TYPE_CIS: 262 // assume no packet received in iso interval => FT packets missed 263 printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout); 264 plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout); 265 break; 266 case HCI_ISO_TYPE_BIS: 267 // assume PTO not used => 1 packet missed 268 plc_check(group_last_packet_sequence + 1); 269 break; 270 default: 271 btstack_unreachable(); 272 break; 273 } 274 } 275 276 void le_audio_demo_util_sink_init(const char * filename_wav){ 277 le_audio_demo_sink_filename_wav = filename_wav; 278 } 279 280 void le_audio_demo_util_sink_enable_lc3plus(bool enable){ 281 le_audio_demo_lc3plus_decoder_requested = enable; 282 } 283 284 static void setup_lc3_decoder(bool use_lc3plus_decoder){ 285 uint8_t channel; 286 for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){ 287 // pick decoder 288 void * decoder_context = NULL; 289 #ifdef HAVE_LC3PLUS 290 if (use_lc3plus_decoder){ 291 decoder_context = &fraunhofer_decoder_contexts[channel]; 292 lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context); 293 } 294 else 295 #endif 296 { 297 decoder_context = &google_decoder_contexts[channel]; 298 lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 299 } 300 decoder_contexts[channel] = decoder_context; 301 lc3_decoder->configure(decoder_context, le_audio_demo_sink_sampling_frequency_hz, le_audio_demo_sink_frame_duration, le_audio_demo_sink_octets_per_frame); 302 } 303 btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 304 } 305 306 void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream, 307 uint32_t sampling_frequency_hz, 308 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 309 uint32_t iso_interval_1250us) { 310 le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz; 311 le_audio_demo_sink_frame_duration = frame_duration; 312 le_audio_demo_sink_octets_per_frame = octets_per_frame; 313 le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us; 314 le_audio_demo_sink_num_streams = num_streams; 315 le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream; 316 317 sink_receive_streaming = false; 318 319 le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream; 320 btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2)); 321 322 le_audio_demo_sink_lc3_frames = 0; 323 324 group_last_packet_received = false; 325 uint8_t i; 326 for (i=0;i<MAX_CHANNELS;i++){ 327 stream_last_packet_received[i] = false; 328 have_pcm[i] = false; 329 } 330 331 le_audio_demo_sink_num_samples_per_frame = btstack_lc3_samples_per_frame(le_audio_demo_sink_sampling_frequency_hz, le_audio_demo_sink_frame_duration); 332 333 // switch to lc3plus if requested and possible 334 bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US); 335 336 // init decoder 337 setup_lc3_decoder(use_lc3plus_decoder); 338 339 printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n", 340 num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder); 341 342 #ifdef HAVE_POSIX_FILE_IO 343 // create wav file 344 printf("WAV file: %s\n", le_audio_demo_sink_filename_wav); 345 wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz); 346 #endif 347 348 // init playback buffer 349 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 350 351 // calc start threshold in bytes for PLAYBACK_START_MS 352 playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2; 353 354 // start playback 355 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 356 if (sink != NULL){ 357 btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() ); 358 btstack_resample_init(&resample_instance, le_audio_demo_sink_num_channels_per_stream); 359 sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback); 360 sink->start_stream(); 361 } 362 } 363 364 void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 365 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 366 uint32_t iso_interval_1250us, uint8_t flush_timeout){ 367 le_audio_demo_sink_type = HCI_ISO_TYPE_CIS; 368 le_audio_demo_sink_flush_timeout = flush_timeout; 369 370 // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval) 371 uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 8); 372 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 373 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 374 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 375 376 // set subsequent plc timeout: FT * ISO Interval 377 plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4; 378 379 // set initial plc timeout:FT * ISO Interval + 4 ms 380 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 381 382 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 383 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 384 385 le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, 386 frame_duration, octets_per_frame, iso_interval_1250us); 387 } 388 389 void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 390 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 391 uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) { 392 le_audio_demo_sink_type = HCI_ISO_TYPE_BIS; 393 le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset; 394 395 // set playback start: ISO Interval + 10 ms 396 uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10; 397 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 398 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 399 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 400 401 // set subsequent plc timeout: ISO Interval 402 plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4; 403 404 // set initial plc timeout: ISO Interval + 4 ms 405 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 406 407 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 408 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 409 410 le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, 411 octets_per_frame, iso_interval_1250us, pre_transmission_offset); 412 } 413 414 void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) { 415 uint16_t header = little_endian_read_16(packet, 0); 416 hci_con_handle_t con_handle = header & 0x0fff; 417 uint8_t pb_flag = (header >> 12) & 3; 418 uint8_t ts_flag = (header >> 14) & 1; 419 uint16_t iso_load_len = little_endian_read_16(packet, 2); 420 421 uint16_t offset = 4; 422 uint32_t time_stamp = 0; 423 if (ts_flag){ 424 time_stamp = little_endian_read_32(packet, offset); 425 offset += 4; 426 } 427 428 uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 429 430 uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 431 offset += 2; 432 433 uint16_t header_2 = little_endian_read_16(packet, offset); 434 uint16_t iso_sdu_length = header_2 & 0x3fff; 435 uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 436 offset += 2; 437 438 // avoid warning for (yet) unused fields 439 UNUSED(con_handle); 440 UNUSED(pb_flag); 441 UNUSED(iso_load_len); 442 UNUSED(packet_status_flag); 443 444 // start with first packet on first stream 445 if (group_last_packet_received == false){ 446 if (stream_index != 0){ 447 printf("Ignore first packet for second stream\n"); 448 return; 449 } 450 group_last_packet_received = true; 451 group_last_packet_sequence = packet_sequence_number; 452 } 453 454 if (stream_last_packet_received[stream_index]) { 455 printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number); 456 457 int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number, 458 stream_last_packet_sequence[stream_index]); 459 if (packet_sequence_delta < 1) { 460 // drop delayed packet that had already been generated by PLC 461 printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n", 462 packet_sequence_number, stream_last_packet_sequence[stream_index]); 463 return; 464 } 465 // simple check 466 if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) { 467 printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1); 468 } 469 } else { 470 printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number); 471 stream_last_packet_received[stream_index] = true; 472 } 473 474 plc_check(packet_sequence_number); 475 476 // either empty packets or num channels * num octets 477 if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) { 478 printf("ISO Length %u != %u * %u\n", iso_sdu_length, le_audio_demo_sink_num_channels_per_stream, le_audio_demo_sink_octets_per_frame); 479 log_info("ISO Length %u != %u * %u", iso_sdu_length, le_audio_demo_sink_num_channels_per_stream, le_audio_demo_sink_octets_per_frame); 480 return; 481 } 482 483 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 484 if( (sink != NULL) && (iso_sdu_length>0)) { 485 if( !sink_receive_streaming && playback_active ) { 486 btstack_sample_rate_compensation_init( &sample_rate_compensation, receive_time_ms, 487 le_audio_demo_sink_sampling_frequency_hz, FLOAT_TO_Q15(1.f) ); 488 sink_receive_streaming = true; 489 } 490 if( sink_receive_streaming ) { 491 uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, receive_time_ms, 492 le_audio_demo_sink_num_samples_per_frame, sink->get_samplerate() ); 493 btstack_resample_set_factor(&resample_instance, resampling_factor); 494 } 495 } 496 497 498 if (iso_sdu_length == 0) { 499 // empty packet -> generate silence 500 memset(pcm, 0, sizeof(pcm)); 501 uint8_t i; 502 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) { 503 have_pcm[stream_index + i] = true; 504 } 505 } else { 506 // regular packet -> decode codec frame 507 uint8_t i; 508 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){ 509 uint8_t tmp_BEC_detect; 510 uint8_t BFI = 0; 511 uint8_t effective_channel = stream_index + i; 512 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI, 513 &pcm[effective_channel], le_audio_demo_sink_num_channels, 514 &tmp_BEC_detect); 515 offset += le_audio_demo_sink_octets_per_frame; 516 have_pcm[stream_index + i] = true; 517 } 518 } 519 520 store_samples_in_ringbuffer(); 521 522 log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 523 524 le_audio_demo_sink_lc3_frames++; 525 526 // PLC 527 btstack_run_loop_remove_timer(&next_packet_timer); 528 btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms); 529 btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout); 530 btstack_run_loop_add_timer(&next_packet_timer); 531 532 if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){ 533 printf("LC3 Frames: %4u - samples received %5u, played %5u, dropped %5u\n", le_audio_demo_sink_lc3_frames, samples_received, samples_played, samples_dropped); 534 samples_received = 0; 535 samples_dropped = 0; 536 samples_played = 0; 537 } 538 539 stream_last_packet_sequence[stream_index] = packet_sequence_number; 540 } 541 542 /** 543 * @brief Close sink: close wav file, stop playback 544 */ 545 void le_audio_demo_util_sink_close(void){ 546 #ifdef HAVE_POSIX_FILE_IO 547 printf("Close WAV file\n"); 548 wav_writer_close(); 549 #endif 550 // stop playback 551 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 552 if (sink != NULL){ 553 sink->stop_stream(); 554 } 555 sink_receive_streaming = false; 556 // stop timer 557 btstack_run_loop_remove_timer(&next_packet_timer); 558 } 559