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