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 "le_audio_demo_util_sink.h" 41 42 #include "btstack_bool.h" 43 #include "btstack_config.h" 44 #include <btstack_debug.h> 45 #include <printf.h> 46 47 #include "hci.h" 48 #include "btstack_audio.h" 49 #include "btstack_lc3_google.h" 50 #include "btstack_lc3plus_fraunhofer.h" 51 52 #include "hxcmod.h" 53 #include "mods/mod.h" 54 55 #ifdef HAVE_POSIX_FILE_IO 56 #include "wav_util.h" 57 #include "btstack_ring_buffer.h" 58 59 #endif 60 61 //#define DEBUG_PLC 62 #ifdef DEBUG_PLC 63 #define printf_plc(...) { \ 64 printf(__VA_ARGS__); \ 65 log_info(__VA_ARGS__);\ 66 } 67 #else 68 #define printf_plc(...) (void)(0); 69 #endif 70 71 #define MAX_CHANNELS 2 72 #define MAX_SAMPLES_PER_FRAME 480 73 #define MAX_LC3_FRAME_BYTES 155 74 75 // playback 76 #define MAX_NUM_LC3_FRAMES 15 77 #define MAX_BYTES_PER_SAMPLE 4 78 #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_CHANNELS * MAX_BYTES_PER_SAMPLE) 79 #define PLAYBACK_START_MS (MAX_NUM_LC3_FRAMES * 20 / 3) 80 81 #define ANSI_COLOR_RED "\x1b[31m" 82 #define ANSI_COLOR_GREEN "\x1b[32m" 83 #define ANSI_COLOR_YELLOW "\x1b[33m" 84 #define ANSI_COLOR_BLUE "\x1b[34m" 85 #define ANSI_COLOR_MAGENTA "\x1b[35m" 86 #define ANSI_COLOR_CYAN "\x1b[36m" 87 #define ANSI_COLOR_RESET "\x1b[0m" 88 89 // SINK 90 91 static const char * le_audio_demo_sink_filename_wav; 92 93 static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration; 94 static hci_iso_type_t le_audio_demo_sink_type; 95 96 static uint32_t le_audio_demo_sink_sampling_frequency_hz; 97 static uint16_t le_audio_demo_sink_num_samples_per_frame; 98 static uint8_t le_audio_demo_sink_num_streams; 99 static uint8_t le_audio_demo_sink_num_channels_per_stream; 100 static uint8_t le_audio_demo_sink_num_channels; 101 static uint16_t le_audio_demo_sink_octets_per_frame; 102 static uint16_t le_audio_demo_sink_iso_interval_1250us; 103 static uint8_t le_audio_demo_sink_flush_timeout; 104 static uint8_t le_audio_demo_sink_pre_transmission_offset; 105 106 // playback 107 static uint16_t playback_start_threshold_bytes; 108 static bool playback_active; 109 static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 110 static btstack_ring_buffer_t playback_buffer; 111 112 // PLC 113 static bool stream_last_packet_received[MAX_CHANNELS]; 114 static uint16_t stream_last_packet_sequence[MAX_CHANNELS]; 115 static uint16_t group_last_packet_sequence; 116 static bool group_last_packet_received; 117 static uint16_t plc_timeout_initial_ms; 118 static uint16_t plc_timeout_subsequent_ms; 119 120 static uint32_t le_audio_demo_sink_lc3_frames; 121 static uint32_t samples_received; 122 static uint32_t samples_played; 123 static uint32_t samples_dropped; 124 125 static btstack_timer_source_t next_packet_timer; 126 127 // lc3 decoder 128 static bool le_audio_demo_lc3plus_decoder_requested = false; 129 static const btstack_lc3_decoder_t * lc3_decoder; 130 static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME]; 131 static bool have_pcm[MAX_CHANNELS]; 132 133 static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS]; 134 #ifdef HAVE_LC3PLUS 135 static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS]; 136 #endif 137 static void * decoder_contexts[MAX_CHANNELS]; 138 139 static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){ 140 // called from lower-layer but guaranteed to be on main thread 141 log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 142 143 samples_played += num_samples; 144 145 uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2; 146 if (playback_active == false){ 147 if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) { 148 log_info("Playback started"); 149 playback_active = true; 150 } 151 } else { 152 if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) { 153 log_info("Playback underrun"); 154 printf("Playback Underrun\n"); 155 // empty buffer 156 uint32_t bytes_read; 157 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 158 playback_active = false; 159 } 160 } 161 162 if (playback_active){ 163 uint32_t bytes_read; 164 btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 165 btstack_assert(bytes_read == bytes_needed); 166 } else { 167 memset(buffer, 0, bytes_needed); 168 } 169 } 170 171 static void store_samples_in_ringbuffer(void){ 172 // check if we have all channels 173 uint8_t channel; 174 for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){ 175 if (have_pcm[channel] == false) return; 176 } 177 #ifdef HAVE_POSIX_FILE_IO 178 // write wav samples 179 wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm); 180 #endif 181 // store samples in playback buffer 182 uint32_t bytes_to_store = le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame * 2; 183 samples_received += le_audio_demo_sink_num_samples_per_frame; 184 if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 185 btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm, bytes_to_store); 186 } else { 187 printf("Samples dropped\n"); 188 samples_dropped += le_audio_demo_sink_num_samples_per_frame; 189 } 190 memset(have_pcm, 0, sizeof(have_pcm)); 191 } 192 193 static void plc_do(uint8_t stream_index) { 194 // inject packet 195 uint8_t tmp_BEC_detect; 196 uint8_t BFI = 1; 197 uint8_t i; 198 for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){ 199 uint8_t effective_channel = stream_index + i; 200 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI, 201 &pcm[effective_channel], le_audio_demo_sink_num_channels, 202 &tmp_BEC_detect); 203 } 204 // and store in ringbuffer when PCM for all channels is available 205 store_samples_in_ringbuffer(); 206 } 207 208 // 209 // Perform PLC for packets missing in previous intervals 210 // 211 // assumptions: 212 // - packet sequence number is monotonic increasing 213 // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed 214 static void plc_check(uint16_t packet_sequence_number) { 215 while (group_last_packet_sequence != packet_sequence_number){ 216 uint8_t i; 217 for (i=0;i<le_audio_demo_sink_num_streams;i++){ 218 // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start 219 if (stream_last_packet_received[i] == false){ 220 printf_plc("- ISO #%u, very first packet missing\n", i); 221 have_pcm[i] = true; 222 store_samples_in_ringbuffer(); 223 224 stream_last_packet_received[i] = true; 225 stream_last_packet_sequence[i] = group_last_packet_sequence; 226 continue; 227 } 228 229 // missing packet if big sequence counter is higher than bis sequence counter 230 if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) { 231 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence); 232 plc_do(i); 233 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence); 234 stream_last_packet_sequence[i] = group_last_packet_sequence; 235 } 236 } 237 group_last_packet_sequence++; 238 } 239 } 240 241 static void plc_timeout(btstack_timer_source_t * timer) { 242 // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets 243 btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms); 244 btstack_run_loop_set_timer_handler(timer, plc_timeout); 245 btstack_run_loop_add_timer(timer); 246 247 switch (le_audio_demo_sink_type){ 248 case HCI_ISO_TYPE_CIS: 249 // assume no packet received in iso interval => FT packets missed 250 printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout); 251 plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout); 252 break; 253 case HCI_ISO_TYPE_BIS: 254 // assume PTO not used => 1 packet missed 255 plc_check(group_last_packet_sequence + 1); 256 break; 257 default: 258 btstack_unreachable(); 259 break; 260 } 261 } 262 263 void le_audio_demo_util_sink_init(const char * filename_wav){ 264 le_audio_demo_sink_filename_wav = filename_wav; 265 } 266 267 void le_audio_demo_util_sink_enable_lc3plus(bool enable){ 268 le_audio_demo_lc3plus_decoder_requested = enable; 269 } 270 271 static void setup_lc3_decoder(bool use_lc3plus_decoder){ 272 uint8_t channel; 273 for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){ 274 // pick decoder 275 void * decoder_context = NULL; 276 #ifdef HAVE_LC3PLUS 277 if (use_lc3plus_decoder){ 278 decoder_context = &fraunhofer_decoder_contexts[channel]; 279 lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context); 280 } 281 else 282 #endif 283 { 284 decoder_context = &google_decoder_contexts[channel]; 285 lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 286 } 287 decoder_contexts[channel] = decoder_context; 288 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); 289 } 290 btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 291 } 292 293 void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream, 294 uint32_t sampling_frequency_hz, 295 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 296 uint32_t iso_interval_1250us) { 297 le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz; 298 le_audio_demo_sink_frame_duration = frame_duration; 299 le_audio_demo_sink_octets_per_frame = octets_per_frame; 300 le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us; 301 le_audio_demo_sink_num_streams = num_streams; 302 le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream; 303 304 le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream; 305 btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2)); 306 307 le_audio_demo_sink_lc3_frames = 0; 308 309 group_last_packet_received = false; 310 uint8_t i; 311 for (i=0;i<MAX_CHANNELS;i++){ 312 stream_last_packet_received[i] = false; 313 have_pcm[i] = false; 314 } 315 316 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); 317 318 // switch to lc3plus if requested and possible 319 bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US); 320 321 // init decoder 322 setup_lc3_decoder(use_lc3plus_decoder); 323 324 printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n", 325 num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder); 326 327 #ifdef HAVE_POSIX_FILE_IO 328 // create wav file 329 printf("WAV file: %s\n", le_audio_demo_sink_filename_wav); 330 wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz); 331 #endif 332 333 // init playback buffer 334 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 335 336 // calc start threshold in bytes for PLAYBACK_START_MS 337 playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2; 338 339 // start playback 340 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 341 if (sink != NULL){ 342 sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback); 343 sink->start_stream(); 344 } 345 } 346 347 void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 348 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 349 uint32_t iso_interval_1250us, uint8_t flush_timeout){ 350 le_audio_demo_sink_type = HCI_ISO_TYPE_CIS; 351 le_audio_demo_sink_flush_timeout = flush_timeout; 352 353 // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval) 354 uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 2); 355 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 356 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 357 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 358 359 // set subsequent plc timeout: FT * ISO Interval 360 plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4; 361 362 // set initial plc timeout:FT * ISO Interval + 4 ms 363 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 364 365 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 366 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 367 368 le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, 369 frame_duration, octets_per_frame, iso_interval_1250us); 370 } 371 372 void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 373 btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 374 uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) { 375 le_audio_demo_sink_type = HCI_ISO_TYPE_BIS; 376 le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset; 377 378 // set playback start: ISO Interval + 10 ms 379 uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10; 380 uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 381 playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 382 printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 383 384 // set subsequent plc timeout: ISO Interval 385 plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4; 386 387 // set initial plc timeout: ISO Interval + 4 ms 388 plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 389 390 printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 391 printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 392 393 le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, 394 octets_per_frame, iso_interval_1250us, pre_transmission_offset); 395 } 396 397 void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) { 398 uint16_t header = little_endian_read_16(packet, 0); 399 hci_con_handle_t con_handle = header & 0x0fff; 400 uint8_t pb_flag = (header >> 12) & 3; 401 uint8_t ts_flag = (header >> 14) & 1; 402 uint16_t iso_load_len = little_endian_read_16(packet, 2); 403 404 uint16_t offset = 4; 405 uint32_t time_stamp = 0; 406 if (ts_flag){ 407 time_stamp = little_endian_read_32(packet, offset); 408 offset += 4; 409 } 410 411 uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 412 413 uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 414 offset += 2; 415 416 uint16_t header_2 = little_endian_read_16(packet, offset); 417 uint16_t iso_sdu_length = header_2 & 0x3fff; 418 uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 419 offset += 2; 420 421 // avoid warning for (yet) unused fields 422 UNUSED(con_handle); 423 UNUSED(pb_flag); 424 UNUSED(iso_load_len); 425 UNUSED(packet_status_flag); 426 427 // start with first packet on first stream 428 if (group_last_packet_received == false){ 429 if (stream_index != 0){ 430 printf("Ignore first packet for second stream\n"); 431 return; 432 } 433 group_last_packet_received = true; 434 group_last_packet_sequence = packet_sequence_number; 435 } 436 437 if (stream_last_packet_received[stream_index]) { 438 printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number); 439 440 int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number, 441 stream_last_packet_sequence[stream_index]); 442 if (packet_sequence_delta < 1) { 443 // drop delayed packet that had already been generated by PLC 444 printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n", 445 packet_sequence_number, stream_last_packet_sequence[stream_index]); 446 return; 447 } 448 // simple check 449 if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) { 450 printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1); 451 } 452 } else { 453 printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number); 454 stream_last_packet_received[stream_index] = true; 455 } 456 457 plc_check(packet_sequence_number); 458 459 // either empty packets or num channels * num octets 460 if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) { 461 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); 462 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); 463 return; 464 } 465 466 if (iso_sdu_length == 0) { 467 // empty packet -> generate silence 468 memset(pcm, 0, sizeof(pcm)); 469 uint8_t i; 470 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) { 471 have_pcm[stream_index + i] = true; 472 } 473 } else { 474 // regular packet -> decode codec frame 475 uint8_t i; 476 for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){ 477 uint8_t tmp_BEC_detect; 478 uint8_t BFI = 0; 479 uint8_t effective_channel = stream_index + i; 480 (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI, 481 &pcm[effective_channel], le_audio_demo_sink_num_channels, 482 &tmp_BEC_detect); 483 offset += le_audio_demo_sink_octets_per_frame; 484 have_pcm[stream_index + i] = true; 485 } 486 } 487 488 store_samples_in_ringbuffer(); 489 490 log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 491 492 le_audio_demo_sink_lc3_frames++; 493 494 // PLC 495 btstack_run_loop_remove_timer(&next_packet_timer); 496 btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms); 497 btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout); 498 btstack_run_loop_add_timer(&next_packet_timer); 499 500 if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){ 501 printf("LC3 Frames: %4u - samples received %5u, played %5u, dropped %5u\n", le_audio_demo_sink_lc3_frames, samples_received, samples_played, samples_dropped); 502 samples_received = 0; 503 samples_dropped = 0; 504 samples_played = 0; 505 } 506 507 stream_last_packet_sequence[stream_index] = packet_sequence_number; 508 } 509 510 /** 511 * @brief Close sink: close wav file, stop playback 512 */ 513 void le_audio_demo_util_sink_close(void){ 514 #ifdef HAVE_POSIX_FILE_IO 515 printf("Close WAV file\n"); 516 wav_writer_close(); 517 #endif 518 // stop playback 519 const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 520 if (sink != NULL){ 521 sink->stop_stream(); 522 } 523 // stop timer 524 btstack_run_loop_remove_timer(&next_packet_timer); 525 }