xref: /btstack/example/le_audio_demo_util_sink.c (revision 8dee1629342465a32b1eda03b06fb703210278d1)
124b69d49SMatthias Ringwald /*
224b69d49SMatthias Ringwald  * Copyright (C) {copyright_year} BlueKitchen GmbH
324b69d49SMatthias Ringwald  *
424b69d49SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
524b69d49SMatthias Ringwald  * modification, are permitted provided that the following conditions
624b69d49SMatthias Ringwald  * are met:
724b69d49SMatthias Ringwald  *
824b69d49SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
924b69d49SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1024b69d49SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1124b69d49SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1224b69d49SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1324b69d49SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1424b69d49SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1524b69d49SMatthias Ringwald  *    from this software without specific prior written permission.
1624b69d49SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1724b69d49SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1824b69d49SMatthias Ringwald  *    monetary gain.
1924b69d49SMatthias Ringwald  *
2024b69d49SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2124b69d49SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2224b69d49SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2324b69d49SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
2424b69d49SMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2524b69d49SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2624b69d49SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2724b69d49SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2824b69d49SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2924b69d49SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3024b69d49SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3124b69d49SMatthias Ringwald  * SUCH DAMAGE.
3224b69d49SMatthias Ringwald  *
3324b69d49SMatthias Ringwald  * Please inquire about commercial licensing options at
3424b69d49SMatthias Ringwald  * [email protected]
3524b69d49SMatthias Ringwald  *
3624b69d49SMatthias Ringwald  */
3724b69d49SMatthias Ringwald 
3824b69d49SMatthias Ringwald #define BTSTACK_FILE__ "le_audio_demo_util_sink.c"
3924b69d49SMatthias Ringwald 
4078d87b81SDirk Helbig #include <stdio.h>
4178d87b81SDirk Helbig 
4224b69d49SMatthias Ringwald #include "le_audio_demo_util_sink.h"
4324b69d49SMatthias Ringwald 
4424b69d49SMatthias Ringwald #include "btstack_bool.h"
4524b69d49SMatthias Ringwald #include "btstack_config.h"
4624b69d49SMatthias Ringwald #include <btstack_debug.h>
4724b69d49SMatthias Ringwald #include <printf.h>
4824b69d49SMatthias Ringwald 
4924b69d49SMatthias Ringwald #include "hci.h"
5024b69d49SMatthias Ringwald #include "btstack_audio.h"
5124b69d49SMatthias Ringwald #include "btstack_lc3_google.h"
5224b69d49SMatthias Ringwald #include "btstack_lc3plus_fraunhofer.h"
5324b69d49SMatthias Ringwald 
5478d87b81SDirk Helbig #include "btstack_sample_rate_compensation.h"
5578d87b81SDirk Helbig #include "btstack_resample.h"
5678d87b81SDirk Helbig 
5724b69d49SMatthias Ringwald #include "hxcmod.h"
5824b69d49SMatthias Ringwald #include "mods/mod.h"
5924b69d49SMatthias Ringwald 
6024b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
6124b69d49SMatthias Ringwald #include "wav_util.h"
6224b69d49SMatthias Ringwald #include "btstack_ring_buffer.h"
6324b69d49SMatthias Ringwald 
6424b69d49SMatthias Ringwald #endif
6524b69d49SMatthias Ringwald 
6624b69d49SMatthias Ringwald //#define DEBUG_PLC
6724b69d49SMatthias Ringwald #ifdef DEBUG_PLC
684e13b5b4SMatthias Ringwald #define printf_plc(...) { \
694e13b5b4SMatthias Ringwald     printf(__VA_ARGS__);  \
704e13b5b4SMatthias Ringwald     log_info(__VA_ARGS__);\
714e13b5b4SMatthias Ringwald }
7224b69d49SMatthias Ringwald #else
7324b69d49SMatthias Ringwald #define printf_plc(...)  (void)(0);
7424b69d49SMatthias Ringwald #endif
7524b69d49SMatthias Ringwald 
7624b69d49SMatthias Ringwald #define MAX_CHANNELS 2
7724b69d49SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480
7824b69d49SMatthias Ringwald #define MAX_LC3_FRAME_BYTES   155
7924b69d49SMatthias Ringwald 
8024b69d49SMatthias Ringwald // playback
8124b69d49SMatthias Ringwald #define MAX_NUM_LC3_FRAMES   15
8224b69d49SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4
838d446e2bSDirk Helbig #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_CHANNELS * MAX_BYTES_PER_SAMPLE)
8424b69d49SMatthias Ringwald #define PLAYBACK_START_MS (MAX_NUM_LC3_FRAMES * 20 / 3)
8524b69d49SMatthias Ringwald 
8624b69d49SMatthias Ringwald #define ANSI_COLOR_RED     "\x1b[31m"
8724b69d49SMatthias Ringwald #define ANSI_COLOR_GREEN   "\x1b[32m"
8824b69d49SMatthias Ringwald #define ANSI_COLOR_YELLOW  "\x1b[33m"
8924b69d49SMatthias Ringwald #define ANSI_COLOR_BLUE    "\x1b[34m"
9024b69d49SMatthias Ringwald #define ANSI_COLOR_MAGENTA "\x1b[35m"
9124b69d49SMatthias Ringwald #define ANSI_COLOR_CYAN    "\x1b[36m"
9224b69d49SMatthias Ringwald #define ANSI_COLOR_RESET   "\x1b[0m"
9324b69d49SMatthias Ringwald 
9424b69d49SMatthias Ringwald // SINK
9524b69d49SMatthias Ringwald 
9624b69d49SMatthias Ringwald static const char * le_audio_demo_sink_filename_wav;
9778d87b81SDirk Helbig static btstack_sample_rate_compensation_t sample_rate_compensation;
98*8dee1629SMatthias Ringwald static uint32_t le_audio_demo_sink_received_samples;
9978d87b81SDirk Helbig static btstack_resample_t resample_instance;
10078d87b81SDirk Helbig static bool sink_receive_streaming;
10178d87b81SDirk Helbig 
10278d87b81SDirk Helbig static int16_t pcm_resample[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME * 2];
10378d87b81SDirk Helbig 
10424b69d49SMatthias Ringwald 
10524b69d49SMatthias Ringwald static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration;
10624b69d49SMatthias Ringwald static hci_iso_type_t               le_audio_demo_sink_type;
10724b69d49SMatthias Ringwald 
10824b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_sampling_frequency_hz;
10924b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_num_samples_per_frame;
11024b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_streams;
11124b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels_per_stream;
11224b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels;
11324b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_octets_per_frame;
11424b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_iso_interval_1250us;
11524b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_flush_timeout;
11624b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_pre_transmission_offset;
11724b69d49SMatthias Ringwald 
11824b69d49SMatthias Ringwald // playback
11924b69d49SMatthias Ringwald static uint16_t              playback_start_threshold_bytes;
12024b69d49SMatthias Ringwald static bool                  playback_active;
12124b69d49SMatthias Ringwald static uint8_t               playback_buffer_storage[PLAYBACK_BUFFER_SIZE];
12224b69d49SMatthias Ringwald static btstack_ring_buffer_t playback_buffer;
12324b69d49SMatthias Ringwald 
12424b69d49SMatthias Ringwald // PLC
12524b69d49SMatthias Ringwald static bool     stream_last_packet_received[MAX_CHANNELS];
12624b69d49SMatthias Ringwald static uint16_t stream_last_packet_sequence[MAX_CHANNELS];
12724b69d49SMatthias Ringwald static uint16_t group_last_packet_sequence;
12824b69d49SMatthias Ringwald static bool     group_last_packet_received;
12979d6b605SMatthias Ringwald static uint16_t plc_timeout_initial_ms;
13079d6b605SMatthias Ringwald static uint16_t plc_timeout_subsequent_ms;
13124b69d49SMatthias Ringwald 
13224b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_lc3_frames;
13365cfaa39SMatthias Ringwald static uint32_t le_audio_demo_sink_zero_frames;
13424b69d49SMatthias Ringwald static uint32_t samples_received;
13524b69d49SMatthias Ringwald static uint32_t samples_played;
13624b69d49SMatthias Ringwald static uint32_t samples_dropped;
13724b69d49SMatthias Ringwald 
13824b69d49SMatthias Ringwald static btstack_timer_source_t next_packet_timer;
13924b69d49SMatthias Ringwald 
14024b69d49SMatthias Ringwald // lc3 decoder
14124b69d49SMatthias Ringwald static bool le_audio_demo_lc3plus_decoder_requested = false;
14224b69d49SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder;
14324b69d49SMatthias Ringwald static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
14424b69d49SMatthias Ringwald static bool have_pcm[MAX_CHANNELS];
14524b69d49SMatthias Ringwald 
14624b69d49SMatthias Ringwald static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS];
14724b69d49SMatthias Ringwald #ifdef HAVE_LC3PLUS
14824b69d49SMatthias Ringwald static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS];
14924b69d49SMatthias Ringwald #endif
15024b69d49SMatthias Ringwald static void * decoder_contexts[MAX_CHANNELS];
15124b69d49SMatthias Ringwald 
15224b69d49SMatthias Ringwald static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){
15324b69d49SMatthias Ringwald     // called from lower-layer but guaranteed to be on main thread
15424b69d49SMatthias Ringwald     log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
15524b69d49SMatthias Ringwald 
15624b69d49SMatthias Ringwald     samples_played += num_samples;
15724b69d49SMatthias Ringwald 
15824b69d49SMatthias Ringwald     uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2;
15924b69d49SMatthias Ringwald     if (playback_active == false){
16024b69d49SMatthias Ringwald         if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) {
16124b69d49SMatthias Ringwald             log_info("Playback started");
16224b69d49SMatthias Ringwald             playback_active = true;
16324b69d49SMatthias Ringwald         }
16424b69d49SMatthias Ringwald     } else {
16524b69d49SMatthias Ringwald         if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) {
16624b69d49SMatthias Ringwald             log_info("Playback underrun");
16724b69d49SMatthias Ringwald             printf("Playback Underrun\n");
16824b69d49SMatthias Ringwald             // empty buffer
16924b69d49SMatthias Ringwald             uint32_t bytes_read;
17024b69d49SMatthias Ringwald             btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
17124b69d49SMatthias Ringwald             playback_active = false;
17224b69d49SMatthias Ringwald         }
17324b69d49SMatthias Ringwald     }
17424b69d49SMatthias Ringwald 
17524b69d49SMatthias Ringwald     if (playback_active){
17624b69d49SMatthias Ringwald         uint32_t bytes_read;
17724b69d49SMatthias Ringwald         btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
17824b69d49SMatthias Ringwald         btstack_assert(bytes_read == bytes_needed);
17924b69d49SMatthias Ringwald     } else {
18024b69d49SMatthias Ringwald         memset(buffer, 0, bytes_needed);
18124b69d49SMatthias Ringwald     }
18224b69d49SMatthias Ringwald }
18324b69d49SMatthias Ringwald 
18424b69d49SMatthias Ringwald static void store_samples_in_ringbuffer(void){
18524b69d49SMatthias Ringwald     // check if we have all channels
18624b69d49SMatthias Ringwald     uint8_t channel;
18724b69d49SMatthias Ringwald     for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){
18824b69d49SMatthias Ringwald         if (have_pcm[channel] == false) return;
18924b69d49SMatthias Ringwald     }
19024b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
19124b69d49SMatthias Ringwald     // write wav samples
19224b69d49SMatthias Ringwald     wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm);
19324b69d49SMatthias Ringwald #endif
194*8dee1629SMatthias Ringwald 
195*8dee1629SMatthias Ringwald     // count for samplerate compensation
196*8dee1629SMatthias Ringwald     le_audio_demo_sink_received_samples += le_audio_demo_sink_num_samples_per_frame;
197*8dee1629SMatthias Ringwald 
19824b69d49SMatthias Ringwald     // store samples in playback buffer
19924b69d49SMatthias Ringwald     samples_received += le_audio_demo_sink_num_samples_per_frame;
20078d87b81SDirk Helbig     uint32_t resampled_frames = btstack_resample_block(&resample_instance, pcm, le_audio_demo_sink_num_samples_per_frame, pcm_resample);
20178d87b81SDirk Helbig     uint32_t bytes_to_store = resampled_frames * le_audio_demo_sink_num_channels * 2;
20278d87b81SDirk Helbig 
20324b69d49SMatthias Ringwald     if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) {
20478d87b81SDirk Helbig         btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm_resample, bytes_to_store);
2050a065144SMatthias Ringwald         log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
20624b69d49SMatthias Ringwald     } else {
20724b69d49SMatthias Ringwald         printf("Samples dropped\n");
20824b69d49SMatthias Ringwald         samples_dropped += le_audio_demo_sink_num_samples_per_frame;
20924b69d49SMatthias Ringwald     }
21024b69d49SMatthias Ringwald     memset(have_pcm, 0, sizeof(have_pcm));
21124b69d49SMatthias Ringwald }
21224b69d49SMatthias Ringwald 
21324b69d49SMatthias Ringwald static void plc_do(uint8_t stream_index) {
21424b69d49SMatthias Ringwald     // inject packet
21524b69d49SMatthias Ringwald     uint8_t tmp_BEC_detect;
21624b69d49SMatthias Ringwald     uint8_t BFI = 1;
21724b69d49SMatthias Ringwald     uint8_t i;
21824b69d49SMatthias Ringwald     for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){
21924b69d49SMatthias Ringwald         uint8_t effective_channel = stream_index + i;
22024b69d49SMatthias Ringwald         (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI,
22124b69d49SMatthias Ringwald                                              &pcm[effective_channel], le_audio_demo_sink_num_channels,
22224b69d49SMatthias Ringwald                                              &tmp_BEC_detect);
223a77ae3beSMatthias Ringwald         have_pcm[i] = true;
22424b69d49SMatthias Ringwald     }
22524b69d49SMatthias Ringwald     // and store in ringbuffer when PCM for all channels is available
22624b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
22724b69d49SMatthias Ringwald }
22824b69d49SMatthias Ringwald 
22924b69d49SMatthias Ringwald //
23024b69d49SMatthias Ringwald // Perform PLC for packets missing in previous intervals
23124b69d49SMatthias Ringwald //
23224b69d49SMatthias Ringwald // assumptions:
23324b69d49SMatthias Ringwald // - packet sequence number is monotonic increasing
23424b69d49SMatthias Ringwald // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed
23524b69d49SMatthias Ringwald static void plc_check(uint16_t packet_sequence_number) {
23624b69d49SMatthias Ringwald     while (group_last_packet_sequence != packet_sequence_number){
23724b69d49SMatthias Ringwald         uint8_t i;
23824b69d49SMatthias Ringwald         for (i=0;i<le_audio_demo_sink_num_streams;i++){
23924b69d49SMatthias Ringwald             // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start
24024b69d49SMatthias Ringwald             if (stream_last_packet_received[i] == false){
24124b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, very first packet missing\n", i);
24224b69d49SMatthias Ringwald                 have_pcm[i] = true;
24324b69d49SMatthias Ringwald                 store_samples_in_ringbuffer();
24424b69d49SMatthias Ringwald 
24524b69d49SMatthias Ringwald                 stream_last_packet_received[i] = true;
24624b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
24724b69d49SMatthias Ringwald                 continue;
24824b69d49SMatthias Ringwald             }
24924b69d49SMatthias Ringwald 
250a77ae3beSMatthias Ringwald             // missing packet if group sequence counter is higher than stream sequence counter
25124b69d49SMatthias Ringwald             if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) {
25224b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence);
253a77ae3beSMatthias Ringwald #ifndef DEBUG_PLC
254a77ae3beSMatthias Ringwald                 log_info("PLC for packet 0x%04x, stream #%u", group_last_packet_sequence, i);
255a77ae3beSMatthias Ringwald #endif
25624b69d49SMatthias Ringwald                 plc_do(i);
25724b69d49SMatthias Ringwald                 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence);
25824b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
25924b69d49SMatthias Ringwald             }
26024b69d49SMatthias Ringwald         }
26124b69d49SMatthias Ringwald         group_last_packet_sequence++;
26224b69d49SMatthias Ringwald     }
26324b69d49SMatthias Ringwald }
26424b69d49SMatthias Ringwald 
26524b69d49SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) {
26624b69d49SMatthias Ringwald     // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets
26779d6b605SMatthias Ringwald     btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms);
26824b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(timer, plc_timeout);
26924b69d49SMatthias Ringwald     btstack_run_loop_add_timer(timer);
27024b69d49SMatthias Ringwald 
27179d6b605SMatthias Ringwald     switch (le_audio_demo_sink_type){
27279d6b605SMatthias Ringwald         case HCI_ISO_TYPE_CIS:
27379d6b605SMatthias Ringwald             // assume no packet received in iso interval => FT packets missed
2744e13b5b4SMatthias Ringwald             printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout);
27579d6b605SMatthias Ringwald             plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout);
27679d6b605SMatthias Ringwald             break;
27779d6b605SMatthias Ringwald         case HCI_ISO_TYPE_BIS:
27879d6b605SMatthias Ringwald             // assume PTO not used => 1 packet missed
27924b69d49SMatthias Ringwald             plc_check(group_last_packet_sequence + 1);
28079d6b605SMatthias Ringwald             break;
28179d6b605SMatthias Ringwald         default:
28279d6b605SMatthias Ringwald             btstack_unreachable();
28379d6b605SMatthias Ringwald             break;
28479d6b605SMatthias Ringwald     }
28524b69d49SMatthias Ringwald }
28624b69d49SMatthias Ringwald 
28724b69d49SMatthias Ringwald void le_audio_demo_util_sink_init(const char * filename_wav){
28824b69d49SMatthias Ringwald     le_audio_demo_sink_filename_wav = filename_wav;
28924b69d49SMatthias Ringwald }
29024b69d49SMatthias Ringwald 
29124b69d49SMatthias Ringwald void le_audio_demo_util_sink_enable_lc3plus(bool enable){
29224b69d49SMatthias Ringwald     le_audio_demo_lc3plus_decoder_requested = enable;
29324b69d49SMatthias Ringwald }
29424b69d49SMatthias Ringwald 
295bc66a7deSMatthias Ringwald static void setup_lc3_decoder(bool use_lc3plus_decoder){
296f4c19309SMatthias Ringwald     uint8_t channel;
297f4c19309SMatthias Ringwald     for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){
298f4c19309SMatthias Ringwald         // pick decoder
299f4c19309SMatthias Ringwald         void * decoder_context = NULL;
300f4c19309SMatthias Ringwald #ifdef HAVE_LC3PLUS
301f4c19309SMatthias Ringwald         if (use_lc3plus_decoder){
302f4c19309SMatthias Ringwald             decoder_context = &fraunhofer_decoder_contexts[channel];
303f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context);
304f4c19309SMatthias Ringwald         }
305f4c19309SMatthias Ringwald         else
306f4c19309SMatthias Ringwald #endif
307f4c19309SMatthias Ringwald         {
308f4c19309SMatthias Ringwald             decoder_context = &google_decoder_contexts[channel];
309f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context);
310f4c19309SMatthias Ringwald         }
311f4c19309SMatthias Ringwald         decoder_contexts[channel] = decoder_context;
312f4c19309SMatthias Ringwald         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);
313f4c19309SMatthias Ringwald     }
314f4c19309SMatthias Ringwald     btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
315f4c19309SMatthias Ringwald }
316f4c19309SMatthias Ringwald 
31724b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream,
31824b69d49SMatthias Ringwald                                                uint32_t sampling_frequency_hz,
31924b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
32024b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us) {
32124b69d49SMatthias Ringwald     le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz;
32224b69d49SMatthias Ringwald     le_audio_demo_sink_frame_duration = frame_duration;
32324b69d49SMatthias Ringwald     le_audio_demo_sink_octets_per_frame = octets_per_frame;
32424b69d49SMatthias Ringwald     le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us;
32524b69d49SMatthias Ringwald     le_audio_demo_sink_num_streams = num_streams;
32624b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream;
32724b69d49SMatthias Ringwald 
32878d87b81SDirk Helbig     sink_receive_streaming = false;
32978d87b81SDirk Helbig 
33024b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream;
33124b69d49SMatthias Ringwald     btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2));
33224b69d49SMatthias Ringwald 
33324b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames = 0;
33424b69d49SMatthias Ringwald 
335f4c19309SMatthias Ringwald     group_last_packet_received = false;
336f4c19309SMatthias Ringwald     uint8_t i;
337f4c19309SMatthias Ringwald     for (i=0;i<MAX_CHANNELS;i++){
338f4c19309SMatthias Ringwald         stream_last_packet_received[i] = false;
339f4c19309SMatthias Ringwald         have_pcm[i] = false;
340f4c19309SMatthias Ringwald     }
341f4c19309SMatthias Ringwald 
34224b69d49SMatthias Ringwald     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);
34324b69d49SMatthias Ringwald 
34424b69d49SMatthias Ringwald     // switch to lc3plus if requested and possible
34524b69d49SMatthias Ringwald     bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US);
34624b69d49SMatthias Ringwald 
34724b69d49SMatthias Ringwald     // init decoder
348bc66a7deSMatthias Ringwald     setup_lc3_decoder(use_lc3plus_decoder);
34924b69d49SMatthias Ringwald 
35024b69d49SMatthias Ringwald     printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n",
35124b69d49SMatthias Ringwald            num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder);
35224b69d49SMatthias Ringwald 
35324b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
35424b69d49SMatthias Ringwald     // create wav file
35524b69d49SMatthias Ringwald     printf("WAV file: %s\n", le_audio_demo_sink_filename_wav);
35624b69d49SMatthias Ringwald     wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz);
35724b69d49SMatthias Ringwald #endif
35824b69d49SMatthias Ringwald 
35924b69d49SMatthias Ringwald     // init playback buffer
36024b69d49SMatthias Ringwald     btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
36124b69d49SMatthias Ringwald 
36224b69d49SMatthias Ringwald     // calc start threshold in bytes for PLAYBACK_START_MS
36324b69d49SMatthias Ringwald     playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2;
36424b69d49SMatthias Ringwald 
365*8dee1629SMatthias Ringwald     // sample rate compensation
366*8dee1629SMatthias Ringwald     le_audio_demo_sink_received_samples = 0;
367*8dee1629SMatthias Ringwald 
36824b69d49SMatthias Ringwald     // start playback
36924b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
37024b69d49SMatthias Ringwald     if (sink != NULL){
37178d87b81SDirk Helbig         btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() );
37278d87b81SDirk Helbig         btstack_resample_init(&resample_instance, le_audio_demo_sink_num_channels_per_stream);
37324b69d49SMatthias Ringwald         sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback);
37424b69d49SMatthias Ringwald         sink->start_stream();
37524b69d49SMatthias Ringwald     }
37624b69d49SMatthias Ringwald }
37724b69d49SMatthias Ringwald 
37824b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
37924b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
38024b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t flush_timeout){
38124b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_CIS;
38224b69d49SMatthias Ringwald     le_audio_demo_sink_flush_timeout = flush_timeout;
38379d6b605SMatthias Ringwald 
384f4c19309SMatthias Ringwald     // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval)
3850388b643SMatthias Ringwald     uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 8);
38679d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
38779d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
38879d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
38979d6b605SMatthias Ringwald 
39079d6b605SMatthias Ringwald     // set subsequent plc timeout: FT * ISO Interval
39179d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4;
39279d6b605SMatthias Ringwald 
39379d6b605SMatthias Ringwald     // set initial plc timeout:FT * ISO Interval + 4 ms
39479d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
39579d6b605SMatthias Ringwald 
39679d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
39779d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
39879d6b605SMatthias Ringwald 
39924b69d49SMatthias Ringwald     le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz,
40024b69d49SMatthias Ringwald                                               frame_duration, octets_per_frame, iso_interval_1250us);
40124b69d49SMatthias Ringwald }
40224b69d49SMatthias Ringwald 
40324b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
40424b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
40524b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) {
40624b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_BIS;
40724b69d49SMatthias Ringwald     le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset;
40879d6b605SMatthias Ringwald 
40979d6b605SMatthias Ringwald     // set playback start: ISO Interval + 10 ms
41079d6b605SMatthias Ringwald     uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10;
41179d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
41279d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
41379d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
41479d6b605SMatthias Ringwald 
41579d6b605SMatthias Ringwald     // set subsequent plc timeout: ISO Interval
41679d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4;
41779d6b605SMatthias Ringwald 
41879d6b605SMatthias Ringwald     // set initial plc timeout: ISO Interval + 4 ms
41979d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
42079d6b605SMatthias Ringwald 
42179d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
42279d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
42379d6b605SMatthias Ringwald 
42424b69d49SMatthias Ringwald     le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration,
42524b69d49SMatthias Ringwald                                               octets_per_frame, iso_interval_1250us, pre_transmission_offset);
42624b69d49SMatthias Ringwald }
42724b69d49SMatthias Ringwald 
42824b69d49SMatthias Ringwald void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) {
42924b69d49SMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
43024b69d49SMatthias Ringwald     hci_con_handle_t con_handle = header & 0x0fff;
43124b69d49SMatthias Ringwald     uint8_t pb_flag = (header >> 12) & 3;
43224b69d49SMatthias Ringwald     uint8_t ts_flag = (header >> 14) & 1;
43324b69d49SMatthias Ringwald     uint16_t iso_load_len = little_endian_read_16(packet, 2);
43424b69d49SMatthias Ringwald 
43524b69d49SMatthias Ringwald     uint16_t offset = 4;
43624b69d49SMatthias Ringwald     uint32_t time_stamp = 0;
43724b69d49SMatthias Ringwald     if (ts_flag){
438ed07a8bdSMatthias Ringwald         time_stamp = little_endian_read_32(packet, offset);
43924b69d49SMatthias Ringwald         offset += 4;
44024b69d49SMatthias Ringwald     }
44124b69d49SMatthias Ringwald 
44224b69d49SMatthias Ringwald     uint32_t receive_time_ms = btstack_run_loop_get_time_ms();
44324b69d49SMatthias Ringwald 
44424b69d49SMatthias Ringwald     uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
44524b69d49SMatthias Ringwald     offset += 2;
44624b69d49SMatthias Ringwald 
44724b69d49SMatthias Ringwald     uint16_t header_2 = little_endian_read_16(packet, offset);
44824b69d49SMatthias Ringwald     uint16_t iso_sdu_length = header_2 & 0x3fff;
44924b69d49SMatthias Ringwald     uint8_t packet_status_flag = (uint8_t) (header_2 >> 14);
45024b69d49SMatthias Ringwald     offset += 2;
45124b69d49SMatthias Ringwald 
452ed07a8bdSMatthias Ringwald     // avoid warning for (yet) unused fields
453ed07a8bdSMatthias Ringwald     UNUSED(con_handle);
454ed07a8bdSMatthias Ringwald     UNUSED(pb_flag);
455ed07a8bdSMatthias Ringwald     UNUSED(iso_load_len);
456ed07a8bdSMatthias Ringwald     UNUSED(packet_status_flag);
457ed07a8bdSMatthias Ringwald 
45824b69d49SMatthias Ringwald     // start with first packet on first stream
45924b69d49SMatthias Ringwald     if (group_last_packet_received == false){
46024b69d49SMatthias Ringwald         if (stream_index != 0){
46124b69d49SMatthias Ringwald             printf("Ignore first packet for second stream\n");
46224b69d49SMatthias Ringwald             return;
46324b69d49SMatthias Ringwald         }
46424b69d49SMatthias Ringwald         group_last_packet_received = true;
46524b69d49SMatthias Ringwald         group_last_packet_sequence = packet_sequence_number;
46624b69d49SMatthias Ringwald     }
46724b69d49SMatthias Ringwald 
46824b69d49SMatthias Ringwald     if (stream_last_packet_received[stream_index]) {
46924b69d49SMatthias Ringwald         printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number);
47024b69d49SMatthias Ringwald 
47124b69d49SMatthias Ringwald         int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number,
47224b69d49SMatthias Ringwald                                                              stream_last_packet_sequence[stream_index]);
47324b69d49SMatthias Ringwald         if (packet_sequence_delta < 1) {
47424b69d49SMatthias Ringwald             // drop delayed packet that had already been generated by PLC
47524b69d49SMatthias Ringwald             printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n",
47624b69d49SMatthias Ringwald                        packet_sequence_number, stream_last_packet_sequence[stream_index]);
47724b69d49SMatthias Ringwald             return;
47824b69d49SMatthias Ringwald         }
47924b69d49SMatthias Ringwald         // simple check
48024b69d49SMatthias Ringwald         if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) {
48124b69d49SMatthias Ringwald             printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1);
48224b69d49SMatthias Ringwald         }
48324b69d49SMatthias Ringwald     } else {
48424b69d49SMatthias Ringwald         printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number);
48524b69d49SMatthias Ringwald         stream_last_packet_received[stream_index] = true;
48624b69d49SMatthias Ringwald     }
48724b69d49SMatthias Ringwald 
48865cfaa39SMatthias Ringwald     if (sink_receive_streaming){
48924b69d49SMatthias Ringwald         plc_check(packet_sequence_number);
49065cfaa39SMatthias Ringwald     }
49124b69d49SMatthias Ringwald 
492f4c19309SMatthias Ringwald     // either empty packets or num channels * num octets
493f4c19309SMatthias Ringwald     if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) {
494f4c19309SMatthias Ringwald         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);
495f4c19309SMatthias Ringwald         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);
496f4c19309SMatthias Ringwald         return;
497f4c19309SMatthias Ringwald     }
498f4c19309SMatthias Ringwald 
49978d87b81SDirk Helbig     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
50078d87b81SDirk Helbig     if( (sink != NULL) && (iso_sdu_length>0)) {
50178d87b81SDirk Helbig         if (!sink_receive_streaming && playback_active) {
50278d87b81SDirk Helbig             btstack_sample_rate_compensation_init(&sample_rate_compensation, receive_time_ms,
50378d87b81SDirk Helbig                                                   le_audio_demo_sink_sampling_frequency_hz, FLOAT_TO_Q15(1.f));
50478d87b81SDirk Helbig             sink_receive_streaming = true;
50578d87b81SDirk Helbig         }
50678d87b81SDirk Helbig     }
50778d87b81SDirk Helbig 
508f4c19309SMatthias Ringwald     if (iso_sdu_length == 0) {
50965cfaa39SMatthias Ringwald         if (sink_receive_streaming){
510f4c19309SMatthias Ringwald             // empty packet -> generate silence
511f4c19309SMatthias Ringwald             memset(pcm, 0, sizeof(pcm));
51224b69d49SMatthias Ringwald             uint8_t i;
51324b69d49SMatthias Ringwald             for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) {
514f4c19309SMatthias Ringwald                 have_pcm[stream_index + i] = true;
515f4c19309SMatthias Ringwald             }
51665cfaa39SMatthias Ringwald             le_audio_demo_sink_zero_frames++;
51765cfaa39SMatthias Ringwald             // pause detection (1000 ms for 10 ms, 750 ms for 7.5 ms frames)
51865cfaa39SMatthias Ringwald             if (le_audio_demo_sink_zero_frames > 100){
51965cfaa39SMatthias Ringwald                 printf("Pause detected, stopping audio\n");
52065cfaa39SMatthias Ringwald                 log_info("Pause detected, stopping audio");
52165cfaa39SMatthias Ringwald                 // pause/reset audio
52265cfaa39SMatthias Ringwald                 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
52365cfaa39SMatthias Ringwald                 sink_receive_streaming = false;
52465cfaa39SMatthias Ringwald                 playback_active = false;
52565cfaa39SMatthias Ringwald             }
52665cfaa39SMatthias Ringwald         }
527f4c19309SMatthias Ringwald     } else {
528f4c19309SMatthias Ringwald         // regular packet -> decode codec frame
52965cfaa39SMatthias Ringwald         le_audio_demo_sink_zero_frames = 0;
530f4c19309SMatthias Ringwald         uint8_t i;
531f4c19309SMatthias Ringwald         for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){
53224b69d49SMatthias Ringwald             uint8_t tmp_BEC_detect;
53324b69d49SMatthias Ringwald             uint8_t BFI = 0;
53424b69d49SMatthias Ringwald             uint8_t effective_channel = stream_index + i;
53524b69d49SMatthias Ringwald             (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI,
53624b69d49SMatthias Ringwald                                                  &pcm[effective_channel], le_audio_demo_sink_num_channels,
53724b69d49SMatthias Ringwald                                                  &tmp_BEC_detect);
53824b69d49SMatthias Ringwald             offset += le_audio_demo_sink_octets_per_frame;
53924b69d49SMatthias Ringwald             have_pcm[stream_index + i] = true;
54024b69d49SMatthias Ringwald         }
541f4c19309SMatthias Ringwald     }
54224b69d49SMatthias Ringwald 
54324b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
54424b69d49SMatthias Ringwald 
545*8dee1629SMatthias Ringwald     if( (sink != NULL) && (iso_sdu_length>0)) {
546*8dee1629SMatthias Ringwald         if( sink_receive_streaming ) {
547*8dee1629SMatthias Ringwald             uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, receive_time_ms,
548*8dee1629SMatthias Ringwald                                                                                   le_audio_demo_sink_received_samples, sink->get_samplerate() );
549*8dee1629SMatthias Ringwald             btstack_resample_set_factor(&resample_instance, resampling_factor);
550*8dee1629SMatthias Ringwald             le_audio_demo_sink_received_samples = 0;
551*8dee1629SMatthias Ringwald         }
552*8dee1629SMatthias Ringwald     }
553*8dee1629SMatthias Ringwald 
55424b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames++;
55524b69d49SMatthias Ringwald 
55624b69d49SMatthias Ringwald     // PLC
55724b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
55879d6b605SMatthias Ringwald     btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms);
55924b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout);
56024b69d49SMatthias Ringwald     btstack_run_loop_add_timer(&next_packet_timer);
56124b69d49SMatthias Ringwald 
56224b69d49SMatthias Ringwald     if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){
56324b69d49SMatthias Ringwald         printf("LC3 Frames: %4u - samples received %5u, played %5u, dropped %5u\n", le_audio_demo_sink_lc3_frames, samples_received, samples_played, samples_dropped);
56424b69d49SMatthias Ringwald         samples_received = 0;
56524b69d49SMatthias Ringwald         samples_dropped  =  0;
56624b69d49SMatthias Ringwald         samples_played = 0;
56724b69d49SMatthias Ringwald     }
56824b69d49SMatthias Ringwald 
56924b69d49SMatthias Ringwald     stream_last_packet_sequence[stream_index] = packet_sequence_number;
57024b69d49SMatthias Ringwald }
57124b69d49SMatthias Ringwald 
57224b69d49SMatthias Ringwald /**
57324b69d49SMatthias Ringwald  * @brief Close sink: close wav file, stop playback
57424b69d49SMatthias Ringwald  */
57524b69d49SMatthias Ringwald void le_audio_demo_util_sink_close(void){
57624b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
57724b69d49SMatthias Ringwald     printf("Close WAV file\n");
57824b69d49SMatthias Ringwald     wav_writer_close();
57924b69d49SMatthias Ringwald #endif
58024b69d49SMatthias Ringwald     // stop playback
58124b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
58224b69d49SMatthias Ringwald     if (sink != NULL){
58324b69d49SMatthias Ringwald         sink->stop_stream();
58424b69d49SMatthias Ringwald     }
58578d87b81SDirk Helbig     sink_receive_streaming = false;
58624b69d49SMatthias Ringwald     // stop timer
58724b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
58824b69d49SMatthias Ringwald }
589