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