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 960f01c821SMatthias Ringwald static enum { 970f01c821SMatthias Ringwald LE_AUDIO_SINK_IDLE, 980f01c821SMatthias Ringwald LE_AUDIO_SINK_INIT, 990f01c821SMatthias Ringwald LE_AUDIO_SINK_CONFIGURED, 1000f01c821SMatthias Ringwald } le_audio_demo_util_sink_state = LE_AUDIO_SINK_IDLE; 1010f01c821SMatthias Ringwald 10224b69d49SMatthias Ringwald static const char * le_audio_demo_sink_filename_wav; 10378d87b81SDirk Helbig static btstack_sample_rate_compensation_t sample_rate_compensation; 1048dee1629SMatthias Ringwald static uint32_t le_audio_demo_sink_received_samples; 10578d87b81SDirk Helbig static btstack_resample_t resample_instance; 10678d87b81SDirk Helbig static bool sink_receive_streaming; 10778d87b81SDirk Helbig 10878d87b81SDirk Helbig static int16_t pcm_resample[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME * 2]; 10978d87b81SDirk Helbig 11024b69d49SMatthias Ringwald 11124b69d49SMatthias Ringwald static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration; 11224b69d49SMatthias Ringwald static hci_iso_type_t le_audio_demo_sink_type; 11324b69d49SMatthias Ringwald 11424b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_sampling_frequency_hz; 11524b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_num_samples_per_frame; 11624b69d49SMatthias Ringwald static uint8_t le_audio_demo_sink_num_streams; 11724b69d49SMatthias Ringwald static uint8_t le_audio_demo_sink_num_channels_per_stream; 11824b69d49SMatthias Ringwald static uint8_t le_audio_demo_sink_num_channels; 11924b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_octets_per_frame; 12024b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_iso_interval_1250us; 12124b69d49SMatthias Ringwald static uint8_t le_audio_demo_sink_flush_timeout; 12224b69d49SMatthias Ringwald static uint8_t le_audio_demo_sink_pre_transmission_offset; 12324b69d49SMatthias Ringwald 12424b69d49SMatthias Ringwald // playback 12524b69d49SMatthias Ringwald static uint16_t playback_start_threshold_bytes; 12624b69d49SMatthias Ringwald static bool playback_active; 12724b69d49SMatthias Ringwald static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 12824b69d49SMatthias Ringwald static btstack_ring_buffer_t playback_buffer; 12924b69d49SMatthias Ringwald 13024b69d49SMatthias Ringwald // PLC 13124b69d49SMatthias Ringwald static bool stream_last_packet_received[MAX_CHANNELS]; 13224b69d49SMatthias Ringwald static uint16_t stream_last_packet_sequence[MAX_CHANNELS]; 13324b69d49SMatthias Ringwald static uint16_t group_last_packet_sequence; 13424b69d49SMatthias Ringwald static bool group_last_packet_received; 13579d6b605SMatthias Ringwald static uint16_t plc_timeout_initial_ms; 13679d6b605SMatthias Ringwald static uint16_t plc_timeout_subsequent_ms; 13724b69d49SMatthias Ringwald 13824b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_lc3_frames; 13965cfaa39SMatthias Ringwald static uint32_t le_audio_demo_sink_zero_frames; 14024b69d49SMatthias Ringwald static uint32_t samples_received; 14124b69d49SMatthias Ringwald static uint32_t samples_played; 14224b69d49SMatthias Ringwald static uint32_t samples_dropped; 14324b69d49SMatthias Ringwald 14424b69d49SMatthias Ringwald static btstack_timer_source_t next_packet_timer; 14524b69d49SMatthias Ringwald 14624b69d49SMatthias Ringwald // lc3 decoder 14724b69d49SMatthias Ringwald static bool le_audio_demo_lc3plus_decoder_requested = false; 14824b69d49SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder; 14924b69d49SMatthias Ringwald static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME]; 15024b69d49SMatthias Ringwald static bool have_pcm[MAX_CHANNELS]; 15124b69d49SMatthias Ringwald 15224b69d49SMatthias Ringwald static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS]; 15324b69d49SMatthias Ringwald #ifdef HAVE_LC3PLUS 15424b69d49SMatthias Ringwald static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS]; 15524b69d49SMatthias Ringwald #endif 15624b69d49SMatthias Ringwald static void * decoder_contexts[MAX_CHANNELS]; 15724b69d49SMatthias Ringwald 15824b69d49SMatthias Ringwald static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){ 15924b69d49SMatthias Ringwald // called from lower-layer but guaranteed to be on main thread 16024b69d49SMatthias Ringwald log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 16124b69d49SMatthias Ringwald 16224b69d49SMatthias Ringwald samples_played += num_samples; 16324b69d49SMatthias Ringwald 16424b69d49SMatthias Ringwald uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2; 16524b69d49SMatthias Ringwald if (playback_active == false){ 16624b69d49SMatthias Ringwald if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) { 16724b69d49SMatthias Ringwald log_info("Playback started"); 16824b69d49SMatthias Ringwald playback_active = true; 16924b69d49SMatthias Ringwald } 17024b69d49SMatthias Ringwald } else { 17124b69d49SMatthias Ringwald if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) { 17224b69d49SMatthias Ringwald log_info("Playback underrun"); 17324b69d49SMatthias Ringwald printf("Playback Underrun\n"); 17424b69d49SMatthias Ringwald // empty buffer 17524b69d49SMatthias Ringwald uint32_t bytes_read; 17624b69d49SMatthias Ringwald btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 17724b69d49SMatthias Ringwald playback_active = false; 17824b69d49SMatthias Ringwald } 17924b69d49SMatthias Ringwald } 18024b69d49SMatthias Ringwald 18124b69d49SMatthias Ringwald if (playback_active){ 18224b69d49SMatthias Ringwald uint32_t bytes_read; 18324b69d49SMatthias Ringwald btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 18424b69d49SMatthias Ringwald btstack_assert(bytes_read == bytes_needed); 18524b69d49SMatthias Ringwald } else { 18624b69d49SMatthias Ringwald memset(buffer, 0, bytes_needed); 18724b69d49SMatthias Ringwald } 18824b69d49SMatthias Ringwald } 18924b69d49SMatthias Ringwald 19024b69d49SMatthias Ringwald static void store_samples_in_ringbuffer(void){ 19124b69d49SMatthias Ringwald // check if we have all channels 19224b69d49SMatthias Ringwald uint8_t channel; 19324b69d49SMatthias Ringwald for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){ 19424b69d49SMatthias Ringwald if (have_pcm[channel] == false) return; 19524b69d49SMatthias Ringwald } 19624b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 19724b69d49SMatthias Ringwald // write wav samples 19824b69d49SMatthias Ringwald wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm); 19924b69d49SMatthias Ringwald #endif 2008dee1629SMatthias Ringwald 2018dee1629SMatthias Ringwald // count for samplerate compensation 2028dee1629SMatthias Ringwald le_audio_demo_sink_received_samples += le_audio_demo_sink_num_samples_per_frame; 2038dee1629SMatthias Ringwald 20424b69d49SMatthias Ringwald // store samples in playback buffer 20524b69d49SMatthias Ringwald samples_received += le_audio_demo_sink_num_samples_per_frame; 20678d87b81SDirk Helbig uint32_t resampled_frames = btstack_resample_block(&resample_instance, pcm, le_audio_demo_sink_num_samples_per_frame, pcm_resample); 20778d87b81SDirk Helbig uint32_t bytes_to_store = resampled_frames * le_audio_demo_sink_num_channels * 2; 20878d87b81SDirk Helbig 20924b69d49SMatthias Ringwald if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 21078d87b81SDirk Helbig btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm_resample, bytes_to_store); 2110a065144SMatthias Ringwald log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2)); 21224b69d49SMatthias Ringwald } else { 21324b69d49SMatthias Ringwald printf("Samples dropped\n"); 21424b69d49SMatthias Ringwald samples_dropped += le_audio_demo_sink_num_samples_per_frame; 21524b69d49SMatthias Ringwald } 21624b69d49SMatthias Ringwald memset(have_pcm, 0, sizeof(have_pcm)); 21724b69d49SMatthias Ringwald } 21824b69d49SMatthias Ringwald 21924b69d49SMatthias Ringwald static void plc_do(uint8_t stream_index) { 22024b69d49SMatthias Ringwald // inject packet 22124b69d49SMatthias Ringwald uint8_t tmp_BEC_detect; 22224b69d49SMatthias Ringwald uint8_t BFI = 1; 22324b69d49SMatthias Ringwald uint8_t i; 22424b69d49SMatthias Ringwald for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){ 22524b69d49SMatthias Ringwald uint8_t effective_channel = stream_index + i; 22624b69d49SMatthias Ringwald (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI, 22724b69d49SMatthias Ringwald &pcm[effective_channel], le_audio_demo_sink_num_channels, 22824b69d49SMatthias Ringwald &tmp_BEC_detect); 229a77ae3beSMatthias Ringwald have_pcm[i] = true; 23024b69d49SMatthias Ringwald } 23124b69d49SMatthias Ringwald // and store in ringbuffer when PCM for all channels is available 23224b69d49SMatthias Ringwald store_samples_in_ringbuffer(); 23324b69d49SMatthias Ringwald } 23424b69d49SMatthias Ringwald 23524b69d49SMatthias Ringwald // 23624b69d49SMatthias Ringwald // Perform PLC for packets missing in previous intervals 23724b69d49SMatthias Ringwald // 23824b69d49SMatthias Ringwald // assumptions: 23924b69d49SMatthias Ringwald // - packet sequence number is monotonic increasing 24024b69d49SMatthias Ringwald // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed 24124b69d49SMatthias Ringwald static void plc_check(uint16_t packet_sequence_number) { 24224b69d49SMatthias Ringwald while (group_last_packet_sequence != packet_sequence_number){ 24324b69d49SMatthias Ringwald uint8_t i; 24424b69d49SMatthias Ringwald for (i=0;i<le_audio_demo_sink_num_streams;i++){ 24524b69d49SMatthias Ringwald // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start 24624b69d49SMatthias Ringwald if (stream_last_packet_received[i] == false){ 24724b69d49SMatthias Ringwald printf_plc("- ISO #%u, very first packet missing\n", i); 24824b69d49SMatthias Ringwald have_pcm[i] = true; 24924b69d49SMatthias Ringwald store_samples_in_ringbuffer(); 25024b69d49SMatthias Ringwald 25124b69d49SMatthias Ringwald stream_last_packet_received[i] = true; 25224b69d49SMatthias Ringwald stream_last_packet_sequence[i] = group_last_packet_sequence; 25324b69d49SMatthias Ringwald continue; 25424b69d49SMatthias Ringwald } 25524b69d49SMatthias Ringwald 256a77ae3beSMatthias Ringwald // missing packet if group sequence counter is higher than stream sequence counter 25724b69d49SMatthias Ringwald if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) { 25824b69d49SMatthias Ringwald printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence); 259a77ae3beSMatthias Ringwald #ifndef DEBUG_PLC 260a77ae3beSMatthias Ringwald log_info("PLC for packet 0x%04x, stream #%u", group_last_packet_sequence, i); 261a77ae3beSMatthias Ringwald #endif 26224b69d49SMatthias Ringwald plc_do(i); 26324b69d49SMatthias Ringwald btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence); 26424b69d49SMatthias Ringwald stream_last_packet_sequence[i] = group_last_packet_sequence; 26524b69d49SMatthias Ringwald } 26624b69d49SMatthias Ringwald } 26724b69d49SMatthias Ringwald group_last_packet_sequence++; 26824b69d49SMatthias Ringwald } 26924b69d49SMatthias Ringwald } 27024b69d49SMatthias Ringwald 27124b69d49SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) { 27224b69d49SMatthias Ringwald // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets 27379d6b605SMatthias Ringwald btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms); 27424b69d49SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, plc_timeout); 27524b69d49SMatthias Ringwald btstack_run_loop_add_timer(timer); 27624b69d49SMatthias Ringwald 27779d6b605SMatthias Ringwald switch (le_audio_demo_sink_type){ 27879d6b605SMatthias Ringwald case HCI_ISO_TYPE_CIS: 27979d6b605SMatthias Ringwald // assume no packet received in iso interval => FT packets missed 2804e13b5b4SMatthias Ringwald printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout); 28179d6b605SMatthias Ringwald plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout); 28279d6b605SMatthias Ringwald break; 28379d6b605SMatthias Ringwald case HCI_ISO_TYPE_BIS: 28479d6b605SMatthias Ringwald // assume PTO not used => 1 packet missed 28524b69d49SMatthias Ringwald plc_check(group_last_packet_sequence + 1); 28679d6b605SMatthias Ringwald break; 28779d6b605SMatthias Ringwald default: 28879d6b605SMatthias Ringwald btstack_unreachable(); 28979d6b605SMatthias Ringwald break; 29079d6b605SMatthias Ringwald } 29124b69d49SMatthias Ringwald } 29224b69d49SMatthias Ringwald 29324b69d49SMatthias Ringwald void le_audio_demo_util_sink_init(const char * filename_wav){ 29424b69d49SMatthias Ringwald le_audio_demo_sink_filename_wav = filename_wav; 2950f01c821SMatthias Ringwald le_audio_demo_util_sink_state = LE_AUDIO_SINK_INIT; 29624b69d49SMatthias Ringwald } 29724b69d49SMatthias Ringwald 29824b69d49SMatthias Ringwald void le_audio_demo_util_sink_enable_lc3plus(bool enable){ 29924b69d49SMatthias Ringwald le_audio_demo_lc3plus_decoder_requested = enable; 30024b69d49SMatthias Ringwald } 30124b69d49SMatthias Ringwald 302bc66a7deSMatthias Ringwald static void setup_lc3_decoder(bool use_lc3plus_decoder){ 303f4c19309SMatthias Ringwald uint8_t channel; 304f4c19309SMatthias Ringwald for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){ 305f4c19309SMatthias Ringwald // pick decoder 306f4c19309SMatthias Ringwald void * decoder_context = NULL; 307f4c19309SMatthias Ringwald #ifdef HAVE_LC3PLUS 308f4c19309SMatthias Ringwald if (use_lc3plus_decoder){ 309f4c19309SMatthias Ringwald decoder_context = &fraunhofer_decoder_contexts[channel]; 310f4c19309SMatthias Ringwald lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context); 311f4c19309SMatthias Ringwald } 312f4c19309SMatthias Ringwald else 313f4c19309SMatthias Ringwald #endif 314f4c19309SMatthias Ringwald { 315f4c19309SMatthias Ringwald decoder_context = &google_decoder_contexts[channel]; 316f4c19309SMatthias Ringwald lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 317f4c19309SMatthias Ringwald } 318f4c19309SMatthias Ringwald decoder_contexts[channel] = decoder_context; 319f4c19309SMatthias 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); 320f4c19309SMatthias Ringwald } 321f4c19309SMatthias Ringwald btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 322f4c19309SMatthias Ringwald } 323f4c19309SMatthias Ringwald 32424b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream, 32524b69d49SMatthias Ringwald uint32_t sampling_frequency_hz, 32624b69d49SMatthias Ringwald btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 32724b69d49SMatthias Ringwald uint32_t iso_interval_1250us) { 32824b69d49SMatthias Ringwald le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz; 32924b69d49SMatthias Ringwald le_audio_demo_sink_frame_duration = frame_duration; 33024b69d49SMatthias Ringwald le_audio_demo_sink_octets_per_frame = octets_per_frame; 33124b69d49SMatthias Ringwald le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us; 33224b69d49SMatthias Ringwald le_audio_demo_sink_num_streams = num_streams; 33324b69d49SMatthias Ringwald le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream; 33424b69d49SMatthias Ringwald 33578d87b81SDirk Helbig sink_receive_streaming = false; 3360f01c821SMatthias Ringwald le_audio_demo_util_sink_state = LE_AUDIO_SINK_CONFIGURED; 33778d87b81SDirk Helbig 33824b69d49SMatthias Ringwald le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream; 33924b69d49SMatthias Ringwald btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2)); 34024b69d49SMatthias Ringwald 34124b69d49SMatthias Ringwald le_audio_demo_sink_lc3_frames = 0; 34224b69d49SMatthias Ringwald 343f4c19309SMatthias Ringwald group_last_packet_received = false; 344f4c19309SMatthias Ringwald uint8_t i; 345f4c19309SMatthias Ringwald for (i=0;i<MAX_CHANNELS;i++){ 346f4c19309SMatthias Ringwald stream_last_packet_received[i] = false; 347f4c19309SMatthias Ringwald have_pcm[i] = false; 348f4c19309SMatthias Ringwald } 349f4c19309SMatthias Ringwald 35024b69d49SMatthias 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); 35124b69d49SMatthias Ringwald 35224b69d49SMatthias Ringwald // switch to lc3plus if requested and possible 35324b69d49SMatthias Ringwald bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US); 35424b69d49SMatthias Ringwald 35524b69d49SMatthias Ringwald // init decoder 356bc66a7deSMatthias Ringwald setup_lc3_decoder(use_lc3plus_decoder); 35724b69d49SMatthias Ringwald 35824b69d49SMatthias Ringwald printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n", 35924b69d49SMatthias Ringwald num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder); 36024b69d49SMatthias Ringwald 36124b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 36224b69d49SMatthias Ringwald // create wav file 36324b69d49SMatthias Ringwald printf("WAV file: %s\n", le_audio_demo_sink_filename_wav); 36424b69d49SMatthias Ringwald wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz); 36524b69d49SMatthias Ringwald #endif 36624b69d49SMatthias Ringwald 36724b69d49SMatthias Ringwald // init playback buffer 36824b69d49SMatthias Ringwald btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 36924b69d49SMatthias Ringwald 37024b69d49SMatthias Ringwald // calc start threshold in bytes for PLAYBACK_START_MS 37124b69d49SMatthias Ringwald playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2; 37224b69d49SMatthias Ringwald 3738dee1629SMatthias Ringwald // sample rate compensation 3748dee1629SMatthias Ringwald le_audio_demo_sink_received_samples = 0; 3758dee1629SMatthias Ringwald 37624b69d49SMatthias Ringwald // start playback 37724b69d49SMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 37824b69d49SMatthias Ringwald if (sink != NULL){ 37978d87b81SDirk Helbig btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() ); 38078d87b81SDirk Helbig btstack_resample_init(&resample_instance, le_audio_demo_sink_num_channels_per_stream); 38124b69d49SMatthias Ringwald sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback); 38224b69d49SMatthias Ringwald sink->start_stream(); 38324b69d49SMatthias Ringwald } 38424b69d49SMatthias Ringwald } 38524b69d49SMatthias Ringwald 38624b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 38724b69d49SMatthias Ringwald btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 38824b69d49SMatthias Ringwald uint32_t iso_interval_1250us, uint8_t flush_timeout){ 38924b69d49SMatthias Ringwald le_audio_demo_sink_type = HCI_ISO_TYPE_CIS; 39024b69d49SMatthias Ringwald le_audio_demo_sink_flush_timeout = flush_timeout; 39179d6b605SMatthias Ringwald 392f4c19309SMatthias Ringwald // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval) 3930388b643SMatthias Ringwald uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 8); 39479d6b605SMatthias Ringwald uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 39579d6b605SMatthias Ringwald playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 39679d6b605SMatthias Ringwald printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 39779d6b605SMatthias Ringwald 39879d6b605SMatthias Ringwald // set subsequent plc timeout: FT * ISO Interval 39979d6b605SMatthias Ringwald plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4; 40079d6b605SMatthias Ringwald 40179d6b605SMatthias Ringwald // set initial plc timeout:FT * ISO Interval + 4 ms 40279d6b605SMatthias Ringwald plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 40379d6b605SMatthias Ringwald 40479d6b605SMatthias Ringwald printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 40579d6b605SMatthias Ringwald printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 40679d6b605SMatthias Ringwald 40724b69d49SMatthias Ringwald le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, 40824b69d49SMatthias Ringwald frame_duration, octets_per_frame, iso_interval_1250us); 40924b69d49SMatthias Ringwald } 41024b69d49SMatthias Ringwald 41124b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz, 41224b69d49SMatthias Ringwald btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame, 41324b69d49SMatthias Ringwald uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) { 41424b69d49SMatthias Ringwald le_audio_demo_sink_type = HCI_ISO_TYPE_BIS; 41524b69d49SMatthias Ringwald le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset; 41679d6b605SMatthias Ringwald 41779d6b605SMatthias Ringwald // set playback start: ISO Interval + 10 ms 41879d6b605SMatthias Ringwald uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10; 41979d6b605SMatthias Ringwald uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms; 42079d6b605SMatthias Ringwald playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2; 42179d6b605SMatthias Ringwald printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes); 42279d6b605SMatthias Ringwald 42379d6b605SMatthias Ringwald // set subsequent plc timeout: ISO Interval 42479d6b605SMatthias Ringwald plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4; 42579d6b605SMatthias Ringwald 42679d6b605SMatthias Ringwald // set initial plc timeout: ISO Interval + 4 ms 42779d6b605SMatthias Ringwald plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4; 42879d6b605SMatthias Ringwald 42979d6b605SMatthias Ringwald printf("PLC: initial timeout %u ms\n", plc_timeout_initial_ms); 43079d6b605SMatthias Ringwald printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms); 43179d6b605SMatthias Ringwald 43224b69d49SMatthias Ringwald le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, 43324b69d49SMatthias Ringwald octets_per_frame, iso_interval_1250us, pre_transmission_offset); 43424b69d49SMatthias Ringwald } 43524b69d49SMatthias Ringwald 43624b69d49SMatthias Ringwald void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) { 4370f01c821SMatthias Ringwald 4380f01c821SMatthias Ringwald if (le_audio_demo_util_sink_state != LE_AUDIO_SINK_CONFIGURED) return; 4390f01c821SMatthias Ringwald 44024b69d49SMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 44124b69d49SMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 44224b69d49SMatthias Ringwald uint8_t pb_flag = (header >> 12) & 3; 44324b69d49SMatthias Ringwald uint8_t ts_flag = (header >> 14) & 1; 44424b69d49SMatthias Ringwald uint16_t iso_load_len = little_endian_read_16(packet, 2); 44524b69d49SMatthias Ringwald 44624b69d49SMatthias Ringwald uint16_t offset = 4; 44724b69d49SMatthias Ringwald uint32_t time_stamp = 0; 44824b69d49SMatthias Ringwald if (ts_flag){ 449ed07a8bdSMatthias Ringwald time_stamp = little_endian_read_32(packet, offset); 45024b69d49SMatthias Ringwald offset += 4; 45124b69d49SMatthias Ringwald } 45224b69d49SMatthias Ringwald 45324b69d49SMatthias Ringwald uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 45424b69d49SMatthias Ringwald 45524b69d49SMatthias Ringwald uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 45624b69d49SMatthias Ringwald offset += 2; 45724b69d49SMatthias Ringwald 45824b69d49SMatthias Ringwald uint16_t header_2 = little_endian_read_16(packet, offset); 45924b69d49SMatthias Ringwald uint16_t iso_sdu_length = header_2 & 0x3fff; 46024b69d49SMatthias Ringwald uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 46124b69d49SMatthias Ringwald offset += 2; 46224b69d49SMatthias Ringwald 463ed07a8bdSMatthias Ringwald // avoid warning for (yet) unused fields 464ed07a8bdSMatthias Ringwald UNUSED(con_handle); 465ed07a8bdSMatthias Ringwald UNUSED(pb_flag); 466ed07a8bdSMatthias Ringwald UNUSED(iso_load_len); 467ed07a8bdSMatthias Ringwald UNUSED(packet_status_flag); 468*f020be6aSMatthias Ringwald UNUSED(time_stamp); 469ed07a8bdSMatthias Ringwald 47024b69d49SMatthias Ringwald // start with first packet on first stream 47124b69d49SMatthias Ringwald if (group_last_packet_received == false){ 47224b69d49SMatthias Ringwald if (stream_index != 0){ 47324b69d49SMatthias Ringwald printf("Ignore first packet for second stream\n"); 47424b69d49SMatthias Ringwald return; 47524b69d49SMatthias Ringwald } 47624b69d49SMatthias Ringwald group_last_packet_received = true; 47724b69d49SMatthias Ringwald group_last_packet_sequence = packet_sequence_number; 47824b69d49SMatthias Ringwald } 47924b69d49SMatthias Ringwald 48024b69d49SMatthias Ringwald if (stream_last_packet_received[stream_index]) { 48124b69d49SMatthias Ringwald printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number); 48224b69d49SMatthias Ringwald 48324b69d49SMatthias Ringwald int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number, 48424b69d49SMatthias Ringwald stream_last_packet_sequence[stream_index]); 48524b69d49SMatthias Ringwald if (packet_sequence_delta < 1) { 48624b69d49SMatthias Ringwald // drop delayed packet that had already been generated by PLC 48724b69d49SMatthias Ringwald printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n", 48824b69d49SMatthias Ringwald packet_sequence_number, stream_last_packet_sequence[stream_index]); 48924b69d49SMatthias Ringwald return; 49024b69d49SMatthias Ringwald } 49124b69d49SMatthias Ringwald // simple check 49224b69d49SMatthias Ringwald if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) { 49324b69d49SMatthias Ringwald printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1); 49424b69d49SMatthias Ringwald } 49524b69d49SMatthias Ringwald } else { 49624b69d49SMatthias Ringwald printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number); 49724b69d49SMatthias Ringwald stream_last_packet_received[stream_index] = true; 49824b69d49SMatthias Ringwald } 49924b69d49SMatthias Ringwald 50065cfaa39SMatthias Ringwald if (sink_receive_streaming){ 50124b69d49SMatthias Ringwald plc_check(packet_sequence_number); 50265cfaa39SMatthias Ringwald } 50324b69d49SMatthias Ringwald 504f4c19309SMatthias Ringwald // either empty packets or num channels * num octets 505f4c19309SMatthias Ringwald if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) { 506f4c19309SMatthias 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); 507f4c19309SMatthias 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); 508f4c19309SMatthias Ringwald return; 509f4c19309SMatthias Ringwald } 510f4c19309SMatthias Ringwald 51178d87b81SDirk Helbig const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 51278d87b81SDirk Helbig if( (sink != NULL) && (iso_sdu_length>0)) { 51378d87b81SDirk Helbig if (!sink_receive_streaming && playback_active) { 51478d87b81SDirk Helbig btstack_sample_rate_compensation_init(&sample_rate_compensation, receive_time_ms, 51578d87b81SDirk Helbig le_audio_demo_sink_sampling_frequency_hz, FLOAT_TO_Q15(1.f)); 51678d87b81SDirk Helbig sink_receive_streaming = true; 51778d87b81SDirk Helbig } 51878d87b81SDirk Helbig } 51978d87b81SDirk Helbig 520f4c19309SMatthias Ringwald if (iso_sdu_length == 0) { 52165cfaa39SMatthias Ringwald if (sink_receive_streaming){ 522f4c19309SMatthias Ringwald // empty packet -> generate silence 523f4c19309SMatthias Ringwald memset(pcm, 0, sizeof(pcm)); 52424b69d49SMatthias Ringwald uint8_t i; 52524b69d49SMatthias Ringwald for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) { 526f4c19309SMatthias Ringwald have_pcm[stream_index + i] = true; 527f4c19309SMatthias Ringwald } 52865cfaa39SMatthias Ringwald le_audio_demo_sink_zero_frames++; 52965cfaa39SMatthias Ringwald // pause detection (1000 ms for 10 ms, 750 ms for 7.5 ms frames) 53065cfaa39SMatthias Ringwald if (le_audio_demo_sink_zero_frames > 100){ 53165cfaa39SMatthias Ringwald printf("Pause detected, stopping audio\n"); 53265cfaa39SMatthias Ringwald log_info("Pause detected, stopping audio"); 53365cfaa39SMatthias Ringwald // pause/reset audio 53465cfaa39SMatthias Ringwald btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 53565cfaa39SMatthias Ringwald sink_receive_streaming = false; 53665cfaa39SMatthias Ringwald playback_active = false; 53765cfaa39SMatthias Ringwald } 53865cfaa39SMatthias Ringwald } 539f4c19309SMatthias Ringwald } else { 540f4c19309SMatthias Ringwald // regular packet -> decode codec frame 54165cfaa39SMatthias Ringwald le_audio_demo_sink_zero_frames = 0; 542f4c19309SMatthias Ringwald uint8_t i; 543f4c19309SMatthias Ringwald for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){ 54424b69d49SMatthias Ringwald uint8_t tmp_BEC_detect; 54524b69d49SMatthias Ringwald uint8_t BFI = 0; 54624b69d49SMatthias Ringwald uint8_t effective_channel = stream_index + i; 54724b69d49SMatthias Ringwald (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI, 54824b69d49SMatthias Ringwald &pcm[effective_channel], le_audio_demo_sink_num_channels, 54924b69d49SMatthias Ringwald &tmp_BEC_detect); 55024b69d49SMatthias Ringwald offset += le_audio_demo_sink_octets_per_frame; 55124b69d49SMatthias Ringwald have_pcm[stream_index + i] = true; 55224b69d49SMatthias Ringwald } 553f4c19309SMatthias Ringwald } 55424b69d49SMatthias Ringwald 55524b69d49SMatthias Ringwald store_samples_in_ringbuffer(); 55624b69d49SMatthias Ringwald 5578dee1629SMatthias Ringwald if( (sink != NULL) && (iso_sdu_length>0)) { 5588dee1629SMatthias Ringwald if( sink_receive_streaming ) { 5598dee1629SMatthias Ringwald uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, receive_time_ms, 5608dee1629SMatthias Ringwald le_audio_demo_sink_received_samples, sink->get_samplerate() ); 5618dee1629SMatthias Ringwald btstack_resample_set_factor(&resample_instance, resampling_factor); 5628dee1629SMatthias Ringwald le_audio_demo_sink_received_samples = 0; 5638dee1629SMatthias Ringwald } 5648dee1629SMatthias Ringwald } 5658dee1629SMatthias Ringwald 56624b69d49SMatthias Ringwald le_audio_demo_sink_lc3_frames++; 56724b69d49SMatthias Ringwald 56824b69d49SMatthias Ringwald // PLC 56924b69d49SMatthias Ringwald btstack_run_loop_remove_timer(&next_packet_timer); 57079d6b605SMatthias Ringwald btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms); 57124b69d49SMatthias Ringwald btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout); 57224b69d49SMatthias Ringwald btstack_run_loop_add_timer(&next_packet_timer); 57324b69d49SMatthias Ringwald 57424b69d49SMatthias Ringwald if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){ 57524b69d49SMatthias 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); 57624b69d49SMatthias Ringwald samples_received = 0; 57724b69d49SMatthias Ringwald samples_dropped = 0; 57824b69d49SMatthias Ringwald samples_played = 0; 57924b69d49SMatthias Ringwald } 58024b69d49SMatthias Ringwald 58124b69d49SMatthias Ringwald stream_last_packet_sequence[stream_index] = packet_sequence_number; 58224b69d49SMatthias Ringwald } 58324b69d49SMatthias Ringwald 58424b69d49SMatthias Ringwald /** 58524b69d49SMatthias Ringwald * @brief Close sink: close wav file, stop playback 58624b69d49SMatthias Ringwald */ 58724b69d49SMatthias Ringwald void le_audio_demo_util_sink_close(void){ 58824b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 58924b69d49SMatthias Ringwald printf("Close WAV file\n"); 59024b69d49SMatthias Ringwald wav_writer_close(); 59124b69d49SMatthias Ringwald #endif 59224b69d49SMatthias Ringwald // stop playback 59324b69d49SMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 59424b69d49SMatthias Ringwald if (sink != NULL){ 59524b69d49SMatthias Ringwald sink->stop_stream(); 59624b69d49SMatthias Ringwald } 5970f01c821SMatthias Ringwald le_audio_demo_util_sink_state = LE_AUDIO_SINK_INIT; 59878d87b81SDirk Helbig sink_receive_streaming = false; 59924b69d49SMatthias Ringwald // stop timer 60024b69d49SMatthias Ringwald btstack_run_loop_remove_timer(&next_packet_timer); 60124b69d49SMatthias Ringwald } 602