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 uint32_t le_audio_demo_sink_received_samples; 99 static btstack_resample_t resample_instance; 100 static bool sink_receive_streaming; 101 102 static int16_t pcm_resample[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME * 2]; 103 104 105 static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration; 106 static hci_iso_type_t le_audio_demo_sink_type; 107 108 static uint32_t le_audio_demo_sink_sampling_frequency_hz; 109 static uint16_t le_audio_demo_sink_num_samples_per_frame; 110 static uint8_t le_audio_demo_sink_num_streams; 111 static uint8_t le_audio_demo_sink_num_channels_per_stream; 112 static uint8_t le_audio_demo_sink_num_channels; 113 static uint16_t le_audio_demo_sink_octets_per_frame; 114 static uint16_t le_audio_demo_sink_iso_interval_1250us; 115 static uint8_t le_audio_demo_sink_flush_timeout; 116 static uint8_t le_audio_demo_sink_pre_transmission_offset; 117 118 // playback 119 static uint16_t playback_start_threshold_bytes; 120 static bool playback_active; 121 static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 122 static btstack_ring_buffer_t playback_buffer; 123 124 // PLC 125 static bool stream_last_packet_received[MAX_CHANNELS]; 126 static uint16_t stream_last_packet_sequence[MAX_CHANNELS]; 127 static uint16_t group_last_packet_sequence; 128 static bool group_last_packet_received; 129 static uint16_t plc_timeout_initial_ms; 130 static uint16_t plc_timeout_subsequent_ms; 131 132 static uint32_t le_audio_demo_sink_lc3_frames; 133 static uint32_t le_audio_demo_sink_zero_frames; 134 static uint32_t samples_received; 135 static uint32_t samples_played; 136 static uint32_t samples_dropped; 137 138 static btstack_timer_source_t next_packet_timer; 139 140 // lc3 decoder 141 static bool le_audio_demo_lc3plus_decoder_requested = false; 142 static const btstack_lc3_decoder_t * lc3_decoder; 143 static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME]; 144 static bool have_pcm[MAX_CHANNELS]; 145 146 static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS]; 147 #ifdef HAVE_LC3PLUS 148 static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS]; 149 #endif 150 static void * decoder_contexts[MAX_CHANNELS]; 151 152 static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){ 153 // called from lower-layer but guaranteed to be on main thread 154 log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 155 156 samples_played += num_samples; 157 158 uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2; 159 if (playback_active == false){ 160 if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) { 161 log_info("Playback started"); 162 playback_active = true; 163 } 164 } else { 165 if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) { 166 log_info("Playback underrun"); 167 printf("Playback Underrun\n"); 168 // empty buffer 169 uint32_t bytes_read; 170 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 171 playback_active = false; 172 } 173 } 174 175 if (playback_active){ 176 uint32_t bytes_read; 177 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 178 btstack_assert(bytes_read == bytes_needed); 179 } else { 180 memset(buffer, 0, bytes_needed); 181 } 182 } 183 184 static void store_samples_in_ringbuffer(void){ 185 // check if we have all channels 186 uint8_t channel; 187 for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){ 188 if (have_pcm[channel] == false) return; 189 } 190 #ifdef HAVE_POSIX_FILE_IO 191 // write wav samples 192 wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm); 193 #endif 194 195 // count for samplerate compensation 196 le_audio_demo_sink_received_samples += le_audio_demo_sink_num_samples_per_frame; 197 198 // store samples in playback buffer 199 samples_received += le_audio_demo_sink_num_samples_per_frame; 200 uint32_t resampled_frames = btstack_resample_block(&resample_instance, pcm, le_audio_demo_sink_num_samples_per_frame, pcm_resample); 201 uint32_t bytes_to_store = resampled_frames * le_audio_demo_sink_num_channels * 2; 202 203 if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 204 btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm_resample, bytes_to_store); 205 log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 206 } else { 207 printf("Samples dropped\n"); 208 samples_dropped += le_audio_demo_sink_num_samples_per_frame; 209 } 210 memset(have_pcm, 0, sizeof(have_pcm)); 211 } 212 213 static void plc_do(uint8_t stream_index) { 214 // inject packet 215 uint8_t tmp_BEC_detect; 216 uint8_t BFI = 1; 217 uint8_t i; 218 for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){ 219 uint8_t effective_channel = stream_index + i; 220 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI, 221 &pcm[effective_channel], le_audio_demo_sink_num_channels, 222 &tmp_BEC_detect); 223 have_pcm[i] = true; 224 } 225 // and store in ringbuffer when PCM for all channels is available 226 store_samples_in_ringbuffer(); 227 } 228 229 // 230 // Perform PLC for packets missing in previous intervals 231 // 232 // assumptions: 233 // - packet sequence number is monotonic increasing 234 // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed 235 static void plc_check(uint16_t packet_sequence_number) { 236 while (group_last_packet_sequence != packet_sequence_number){ 237 uint8_t i; 238 for (i=0;i<le_audio_demo_sink_num_streams;i++){ 239 // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start 240 if (stream_last_packet_received[i] == false){ 241 printf_plc("- ISO #%u, very first packet missing\n", i); 242 have_pcm[i] = true; 243 store_samples_in_ringbuffer(); 244 245 stream_last_packet_received[i] = true; 246 stream_last_packet_sequence[i] = group_last_packet_sequence; 247 continue; 248 } 249 250 // missing packet if group sequence counter is higher than stream sequence counter 251 if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) { 252 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence); 253 #ifndef DEBUG_PLC 254 log_info("PLC for packet 0x%04x, stream #%u", group_last_packet_sequence, i); 255 #endif 256 plc_do(i); 257 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence); 258 stream_last_packet_sequence[i] = group_last_packet_sequence; 259 } 260 } 261 group_last_packet_sequence++; 262 } 263 } 264 265 static void plc_timeout(btstack_timer_source_t * timer) { 266 // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets 267 btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms); 268 btstack_run_loop_set_timer_handler(timer, plc_timeout); 269 btstack_run_loop_add_timer(timer); 270 271 switch (le_audio_demo_sink_type){ 272 case HCI_ISO_TYPE_CIS: 273 // assume no packet received in iso interval => FT packets missed 274 printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout); 275 plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout); 276 break; 277 case HCI_ISO_TYPE_BIS: 278 // assume PTO not used => 1 packet missed 279 plc_check(group_last_packet_sequence + 1); 280 break; 281 default: 282 btstack_unreachable(); 283 break; 284 } 285 } 286 287 void le_audio_demo_util_sink_init(const char * filename_wav){ 288 le_audio_demo_sink_filename_wav = filename_wav; 289 } 290 291 void le_audio_demo_util_sink_enable_lc3plus(bool enable){ 292 le_audio_demo_lc3plus_decoder_requested = enable; 293 } 294 295 static void setup_lc3_decoder(bool use_lc3plus_decoder){ 296 uint8_t channel; 297 for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){ 298 // pick decoder 299 void * decoder_context = NULL; 300 #ifdef HAVE_LC3PLUS 301 if (use_lc3plus_decoder){ 302 decoder_context = &fraunhofer_decoder_contexts[channel]; 303 lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context); 304 } 305 else 306 #endif 307 { 308 decoder_context = &google_decoder_contexts[channel]; 309 lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 310 } 311 decoder_contexts[channel] = decoder_context; 312 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); 313 } 314 btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 315 } 316 317 void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream, 318 uint32_t sampling_frequency_hz, 319 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 320 uint32_t iso_interval_1250us) { 321 le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz; 322 le_audio_demo_sink_frame_duration = frame_duration; 323 le_audio_demo_sink_octets_per_frame = octets_per_frame; 324 le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us; 325 le_audio_demo_sink_num_streams = num_streams; 326 le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream; 327 328 sink_receive_streaming = false; 329 330 le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream; 331 btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2)); 332 333 le_audio_demo_sink_lc3_frames = 0; 334 335 group_last_packet_received = false; 336 uint8_t i; 337 for (i=0;i<MAX_CHANNELS;i++){ 338 stream_last_packet_received[i] = false; 339 have_pcm[i] = false; 340 } 341 342 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); 343 344 // switch to lc3plus if requested and possible 345 bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US); 346 347 // init decoder 348 setup_lc3_decoder(use_lc3plus_decoder); 349 350 printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n", 351 num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder); 352 353 #ifdef HAVE_POSIX_FILE_IO 354 // create wav file 355 printf("WAV file: %s\n", le_audio_demo_sink_filename_wav); 356 wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz); 357 #endif 358 359 // init playback buffer 360 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 361 362 // calc start threshold in bytes for PLAYBACK_START_MS 363 playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2; 364 365 // sample rate compensation 366 le_audio_demo_sink_received_samples = 0; 367 368 // start playback 369 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 370 if (sink != NULL){ 371 btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() ); 372 btstack_resample_init(&resample_instance, le_audio_demo_sink_num_channels_per_stream); 373 sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback); 374 sink->start_stream(); 375 } 376 } 377 378 void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 379 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 380 uint32_t iso_interval_1250us, uint8_t flush_timeout){ 381 le_audio_demo_sink_type = HCI_ISO_TYPE_CIS; 382 le_audio_demo_sink_flush_timeout = flush_timeout; 383 384 // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval) 385 uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 8); 386 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 387 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 388 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 389 390 // set subsequent plc timeout: FT * ISO Interval 391 plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4; 392 393 // set initial plc timeout:FT * ISO Interval + 4 ms 394 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 395 396 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 397 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 398 399 le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, 400 frame_duration, octets_per_frame, iso_interval_1250us); 401 } 402 403 void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 404 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 405 uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) { 406 le_audio_demo_sink_type = HCI_ISO_TYPE_BIS; 407 le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset; 408 409 // set playback start: ISO Interval + 10 ms 410 uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10; 411 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 412 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 413 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 414 415 // set subsequent plc timeout: ISO Interval 416 plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4; 417 418 // set initial plc timeout: ISO Interval + 4 ms 419 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 420 421 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 422 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 423 424 le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, 425 octets_per_frame, iso_interval_1250us, pre_transmission_offset); 426 } 427 428 void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) { 429 uint16_t header = little_endian_read_16(packet, 0); 430 hci_con_handle_t con_handle = header & 0x0fff; 431 uint8_t pb_flag = (header >> 12) & 3; 432 uint8_t ts_flag = (header >> 14) & 1; 433 uint16_t iso_load_len = little_endian_read_16(packet, 2); 434 435 uint16_t offset = 4; 436 uint32_t time_stamp = 0; 437 if (ts_flag){ 438 time_stamp = little_endian_read_32(packet, offset); 439 offset += 4; 440 } 441 442 uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 443 444 uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 445 offset += 2; 446 447 uint16_t header_2 = little_endian_read_16(packet, offset); 448 uint16_t iso_sdu_length = header_2 & 0x3fff; 449 uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 450 offset += 2; 451 452 // avoid warning for (yet) unused fields 453 UNUSED(con_handle); 454 UNUSED(pb_flag); 455 UNUSED(iso_load_len); 456 UNUSED(packet_status_flag); 457 458 // start with first packet on first stream 459 if (group_last_packet_received == false){ 460 if (stream_index != 0){ 461 printf("Ignore first packet for second stream\n"); 462 return; 463 } 464 group_last_packet_received = true; 465 group_last_packet_sequence = packet_sequence_number; 466 } 467 468 if (stream_last_packet_received[stream_index]) { 469 printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number); 470 471 int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number, 472 stream_last_packet_sequence[stream_index]); 473 if (packet_sequence_delta < 1) { 474 // drop delayed packet that had already been generated by PLC 475 printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n", 476 packet_sequence_number, stream_last_packet_sequence[stream_index]); 477 return; 478 } 479 // simple check 480 if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) { 481 printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1); 482 } 483 } else { 484 printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number); 485 stream_last_packet_received[stream_index] = true; 486 } 487 488 if (sink_receive_streaming){ 489 plc_check(packet_sequence_number); 490 } 491 492 // either empty packets or num channels * num octets 493 if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) { 494 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); 495 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); 496 return; 497 } 498 499 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 500 if( (sink != NULL) && (iso_sdu_length>0)) { 501 if (!sink_receive_streaming && playback_active) { 502 btstack_sample_rate_compensation_init(&sample_rate_compensation, receive_time_ms, 503 le_audio_demo_sink_sampling_frequency_hz, FLOAT_TO_Q15(1.f)); 504 sink_receive_streaming = true; 505 } 506 } 507 508 if (iso_sdu_length == 0) { 509 if (sink_receive_streaming){ 510 // empty packet -> generate silence 511 memset(pcm, 0, sizeof(pcm)); 512 uint8_t i; 513 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) { 514 have_pcm[stream_index + i] = true; 515 } 516 le_audio_demo_sink_zero_frames++; 517 // pause detection (1000 ms for 10 ms, 750 ms for 7.5 ms frames) 518 if (le_audio_demo_sink_zero_frames > 100){ 519 printf("Pause detected, stopping audio\n"); 520 log_info("Pause detected, stopping audio"); 521 // pause/reset audio 522 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 523 sink_receive_streaming = false; 524 playback_active = false; 525 } 526 } 527 } else { 528 // regular packet -> decode codec frame 529 le_audio_demo_sink_zero_frames = 0; 530 uint8_t i; 531 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){ 532 uint8_t tmp_BEC_detect; 533 uint8_t BFI = 0; 534 uint8_t effective_channel = stream_index + i; 535 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI, 536 &pcm[effective_channel], le_audio_demo_sink_num_channels, 537 &tmp_BEC_detect); 538 offset += le_audio_demo_sink_octets_per_frame; 539 have_pcm[stream_index + i] = true; 540 } 541 } 542 543 store_samples_in_ringbuffer(); 544 545 if( (sink != NULL) && (iso_sdu_length>0)) { 546 if( sink_receive_streaming ) { 547 uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, receive_time_ms, 548 le_audio_demo_sink_received_samples, sink->get_samplerate() ); 549 btstack_resample_set_factor(&resample_instance, resampling_factor); 550 le_audio_demo_sink_received_samples = 0; 551 } 552 } 553 554 le_audio_demo_sink_lc3_frames++; 555 556 // PLC 557 btstack_run_loop_remove_timer(&next_packet_timer); 558 btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms); 559 btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout); 560 btstack_run_loop_add_timer(&next_packet_timer); 561 562 if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){ 563 printf("LC3 Frames: %4u - samples received %5u, played %5u, dropped %5u\n", le_audio_demo_sink_lc3_frames, samples_received, samples_played, samples_dropped); 564 samples_received = 0; 565 samples_dropped = 0; 566 samples_played = 0; 567 } 568 569 stream_last_packet_sequence[stream_index] = packet_sequence_number; 570 } 571 572 /** 573 * @brief Close sink: close wav file, stop playback 574 */ 575 void le_audio_demo_util_sink_close(void){ 576 #ifdef HAVE_POSIX_FILE_IO 577 printf("Close WAV file\n"); 578 wav_writer_close(); 579 #endif 580 // stop playback 581 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 582 if (sink != NULL){ 583 sink->stop_stream(); 584 } 585 sink_receive_streaming = false; 586 // stop timer 587 btstack_run_loop_remove_timer(&next_packet_timer); 588 } 589