xref: /btstack/example/le_audio_demo_util_sink.c (revision 0b541edf27f21e52e6a1f62ac158d5fabc12eeb5)
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>
41a18990d3SMatthias Ringwald #include <inttypes.h>
4278d87b81SDirk Helbig 
4324b69d49SMatthias Ringwald #include "le_audio_demo_util_sink.h"
4424b69d49SMatthias Ringwald 
4524b69d49SMatthias Ringwald #include "btstack_bool.h"
4624b69d49SMatthias Ringwald #include "btstack_config.h"
4724b69d49SMatthias Ringwald #include <btstack_debug.h>
48a18990d3SMatthias Ringwald #include <stdio.h>
4924b69d49SMatthias Ringwald 
5024b69d49SMatthias Ringwald #include "hci.h"
5124b69d49SMatthias Ringwald #include "btstack_audio.h"
5224b69d49SMatthias Ringwald #include "btstack_lc3_google.h"
5324b69d49SMatthias Ringwald #include "btstack_lc3plus_fraunhofer.h"
5424b69d49SMatthias Ringwald 
5578d87b81SDirk Helbig #include "btstack_sample_rate_compensation.h"
5678d87b81SDirk Helbig #include "btstack_resample.h"
5778d87b81SDirk Helbig 
5824b69d49SMatthias Ringwald #include "hxcmod.h"
5924b69d49SMatthias Ringwald #include "mods/mod.h"
6024b69d49SMatthias Ringwald 
61a18990d3SMatthias Ringwald #include "btstack_ring_buffer.h"
6224b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
6324b69d49SMatthias Ringwald #include "wav_util.h"
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 
86a18990d3SMatthias Ringwald // analysis
87a18990d3SMatthias Ringwald #define PACKET_PREFIX_LEN 10
88a18990d3SMatthias Ringwald 
8924b69d49SMatthias Ringwald #define ANSI_COLOR_RED     "\x1b[31m"
9024b69d49SMatthias Ringwald #define ANSI_COLOR_GREEN   "\x1b[32m"
9124b69d49SMatthias Ringwald #define ANSI_COLOR_YELLOW  "\x1b[33m"
9224b69d49SMatthias Ringwald #define ANSI_COLOR_BLUE    "\x1b[34m"
9324b69d49SMatthias Ringwald #define ANSI_COLOR_MAGENTA "\x1b[35m"
9424b69d49SMatthias Ringwald #define ANSI_COLOR_CYAN    "\x1b[36m"
9524b69d49SMatthias Ringwald #define ANSI_COLOR_RESET   "\x1b[0m"
9624b69d49SMatthias Ringwald 
97a18990d3SMatthias Ringwald // statistics
98a18990d3SMatthias Ringwald static uint16_t last_packet_sequence[MAX_CHANNELS];
99a18990d3SMatthias Ringwald static uint32_t last_packet_time_ms[MAX_CHANNELS];
100a18990d3SMatthias Ringwald static uint8_t  last_packet_prefix[MAX_CHANNELS * PACKET_PREFIX_LEN];
101a18990d3SMatthias Ringwald 
10224b69d49SMatthias Ringwald // SINK
10324b69d49SMatthias Ringwald 
1040f01c821SMatthias Ringwald static enum {
1050f01c821SMatthias Ringwald     LE_AUDIO_SINK_IDLE,
1060f01c821SMatthias Ringwald     LE_AUDIO_SINK_INIT,
1070f01c821SMatthias Ringwald     LE_AUDIO_SINK_CONFIGURED,
1080f01c821SMatthias Ringwald } le_audio_demo_util_sink_state = LE_AUDIO_SINK_IDLE;
1090f01c821SMatthias Ringwald 
11024b69d49SMatthias Ringwald static const char * le_audio_demo_sink_filename_wav;
11178d87b81SDirk Helbig static btstack_sample_rate_compensation_t sample_rate_compensation;
1128dee1629SMatthias Ringwald static uint32_t le_audio_demo_sink_received_samples;
11378d87b81SDirk Helbig static btstack_resample_t resample_instance;
11478d87b81SDirk Helbig static bool sink_receive_streaming;
11578d87b81SDirk Helbig 
11678d87b81SDirk Helbig static int16_t pcm_resample[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME * 2];
11778d87b81SDirk Helbig 
11824b69d49SMatthias Ringwald 
11924b69d49SMatthias Ringwald static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration;
12024b69d49SMatthias Ringwald static hci_iso_type_t               le_audio_demo_sink_type;
12124b69d49SMatthias Ringwald 
12224b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_sampling_frequency_hz;
12324b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_num_samples_per_frame;
12424b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_streams;
12524b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels_per_stream;
12624b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels;
12724b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_octets_per_frame;
12824b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_iso_interval_1250us;
12924b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_flush_timeout;
13024b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_pre_transmission_offset;
13124b69d49SMatthias Ringwald 
13224b69d49SMatthias Ringwald // playback
13324b69d49SMatthias Ringwald static uint16_t              playback_start_threshold_bytes;
13424b69d49SMatthias Ringwald static bool                  playback_active;
13524b69d49SMatthias Ringwald static uint8_t               playback_buffer_storage[PLAYBACK_BUFFER_SIZE];
13624b69d49SMatthias Ringwald static btstack_ring_buffer_t playback_buffer;
13724b69d49SMatthias Ringwald 
13824b69d49SMatthias Ringwald // PLC
13924b69d49SMatthias Ringwald static bool     stream_last_packet_received[MAX_CHANNELS];
14024b69d49SMatthias Ringwald static uint16_t stream_last_packet_sequence[MAX_CHANNELS];
14124b69d49SMatthias Ringwald static uint16_t group_last_packet_sequence;
14224b69d49SMatthias Ringwald static bool     group_last_packet_received;
14379d6b605SMatthias Ringwald static uint16_t plc_timeout_initial_ms;
14479d6b605SMatthias Ringwald static uint16_t plc_timeout_subsequent_ms;
14524b69d49SMatthias Ringwald 
14624b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_lc3_frames;
14765cfaa39SMatthias Ringwald static uint32_t le_audio_demo_sink_zero_frames;
14824b69d49SMatthias Ringwald static uint32_t samples_received;
14924b69d49SMatthias Ringwald static uint32_t samples_played;
15024b69d49SMatthias Ringwald static uint32_t samples_dropped;
15124b69d49SMatthias Ringwald 
15224b69d49SMatthias Ringwald static btstack_timer_source_t next_packet_timer;
15324b69d49SMatthias Ringwald 
15424b69d49SMatthias Ringwald // lc3 decoder
15524b69d49SMatthias Ringwald static bool le_audio_demo_lc3plus_decoder_requested = false;
15624b69d49SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder;
15724b69d49SMatthias Ringwald static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
15824b69d49SMatthias Ringwald static bool have_pcm[MAX_CHANNELS];
15924b69d49SMatthias Ringwald 
16024b69d49SMatthias Ringwald static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS];
16124b69d49SMatthias Ringwald #ifdef HAVE_LC3PLUS
16224b69d49SMatthias Ringwald static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS];
16324b69d49SMatthias Ringwald #endif
16424b69d49SMatthias Ringwald static void * decoder_contexts[MAX_CHANNELS];
16524b69d49SMatthias Ringwald 
16624b69d49SMatthias Ringwald static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){
16724b69d49SMatthias Ringwald     // called from lower-layer but guaranteed to be on main thread
16824b69d49SMatthias Ringwald     log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
16924b69d49SMatthias Ringwald 
17024b69d49SMatthias Ringwald     samples_played += num_samples;
17124b69d49SMatthias Ringwald 
17224b69d49SMatthias Ringwald     uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2;
17324b69d49SMatthias Ringwald     if (playback_active == false){
17424b69d49SMatthias Ringwald         if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) {
17524b69d49SMatthias Ringwald             log_info("Playback started");
17624b69d49SMatthias Ringwald             playback_active = true;
17724b69d49SMatthias Ringwald         }
17824b69d49SMatthias Ringwald     } else {
17924b69d49SMatthias Ringwald         if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) {
18024b69d49SMatthias Ringwald             log_info("Playback underrun");
18124b69d49SMatthias Ringwald             printf("Playback Underrun\n");
18224b69d49SMatthias Ringwald             // empty buffer
18324b69d49SMatthias Ringwald             uint32_t bytes_read;
18424b69d49SMatthias Ringwald             btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
18524b69d49SMatthias Ringwald             playback_active = false;
18624b69d49SMatthias Ringwald         }
18724b69d49SMatthias Ringwald     }
18824b69d49SMatthias Ringwald 
18924b69d49SMatthias Ringwald     if (playback_active){
19024b69d49SMatthias Ringwald         uint32_t bytes_read;
19124b69d49SMatthias Ringwald         btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
19224b69d49SMatthias Ringwald         btstack_assert(bytes_read == bytes_needed);
19324b69d49SMatthias Ringwald     } else {
19424b69d49SMatthias Ringwald         memset(buffer, 0, bytes_needed);
19524b69d49SMatthias Ringwald     }
19624b69d49SMatthias Ringwald }
19724b69d49SMatthias Ringwald 
19824b69d49SMatthias Ringwald static void store_samples_in_ringbuffer(void){
19924b69d49SMatthias Ringwald     // check if we have all channels
20024b69d49SMatthias Ringwald     uint8_t channel;
20124b69d49SMatthias Ringwald     for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){
20224b69d49SMatthias Ringwald         if (have_pcm[channel] == false) return;
20324b69d49SMatthias Ringwald     }
20424b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
20524b69d49SMatthias Ringwald     // write wav samples
20624b69d49SMatthias Ringwald     wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm);
20724b69d49SMatthias Ringwald #endif
2088dee1629SMatthias Ringwald 
2098dee1629SMatthias Ringwald     // count for samplerate compensation
2108dee1629SMatthias Ringwald     le_audio_demo_sink_received_samples += le_audio_demo_sink_num_samples_per_frame;
2118dee1629SMatthias Ringwald 
21224b69d49SMatthias Ringwald     // store samples in playback buffer
21324b69d49SMatthias Ringwald     samples_received += le_audio_demo_sink_num_samples_per_frame;
21478d87b81SDirk Helbig     uint32_t resampled_frames = btstack_resample_block(&resample_instance, pcm, le_audio_demo_sink_num_samples_per_frame, pcm_resample);
21578d87b81SDirk Helbig     uint32_t bytes_to_store = resampled_frames * le_audio_demo_sink_num_channels * 2;
21678d87b81SDirk Helbig 
21724b69d49SMatthias Ringwald     if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) {
21878d87b81SDirk Helbig         btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm_resample, bytes_to_store);
2190a065144SMatthias Ringwald         log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
22024b69d49SMatthias Ringwald     } else {
22124b69d49SMatthias Ringwald         printf("Samples dropped\n");
22224b69d49SMatthias Ringwald         samples_dropped += le_audio_demo_sink_num_samples_per_frame;
22324b69d49SMatthias Ringwald     }
22424b69d49SMatthias Ringwald     memset(have_pcm, 0, sizeof(have_pcm));
22524b69d49SMatthias Ringwald }
22624b69d49SMatthias Ringwald 
22724b69d49SMatthias Ringwald static void plc_do(uint8_t stream_index) {
22824b69d49SMatthias Ringwald     // inject packet
22924b69d49SMatthias Ringwald     uint8_t tmp_BEC_detect;
23024b69d49SMatthias Ringwald     uint8_t BFI = 1;
23124b69d49SMatthias Ringwald     uint8_t i;
23224b69d49SMatthias Ringwald     for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){
23324b69d49SMatthias Ringwald         uint8_t effective_channel = stream_index + i;
23424b69d49SMatthias Ringwald         (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI,
23524b69d49SMatthias Ringwald                                              &pcm[effective_channel], le_audio_demo_sink_num_channels,
23624b69d49SMatthias Ringwald                                              &tmp_BEC_detect);
237a77ae3beSMatthias Ringwald         have_pcm[i] = true;
23824b69d49SMatthias Ringwald     }
23924b69d49SMatthias Ringwald     // and store in ringbuffer when PCM for all channels is available
24024b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
24124b69d49SMatthias Ringwald }
24224b69d49SMatthias Ringwald 
24324b69d49SMatthias Ringwald //
24424b69d49SMatthias Ringwald // Perform PLC for packets missing in previous intervals
24524b69d49SMatthias Ringwald //
24624b69d49SMatthias Ringwald // assumptions:
24724b69d49SMatthias Ringwald // - packet sequence number is monotonic increasing
24824b69d49SMatthias Ringwald // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed
24924b69d49SMatthias Ringwald static void plc_check(uint16_t packet_sequence_number) {
25024b69d49SMatthias Ringwald     while (group_last_packet_sequence != packet_sequence_number){
25124b69d49SMatthias Ringwald         uint8_t i;
25224b69d49SMatthias Ringwald         for (i=0;i<le_audio_demo_sink_num_streams;i++){
25324b69d49SMatthias Ringwald             // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start
25424b69d49SMatthias Ringwald             if (stream_last_packet_received[i] == false){
25524b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, very first packet missing\n", i);
25624b69d49SMatthias Ringwald                 have_pcm[i] = true;
25724b69d49SMatthias Ringwald                 store_samples_in_ringbuffer();
25824b69d49SMatthias Ringwald 
25924b69d49SMatthias Ringwald                 stream_last_packet_received[i] = true;
26024b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
26124b69d49SMatthias Ringwald                 continue;
26224b69d49SMatthias Ringwald             }
26324b69d49SMatthias Ringwald 
264a77ae3beSMatthias Ringwald             // missing packet if group sequence counter is higher than stream sequence counter
26524b69d49SMatthias Ringwald             if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) {
26624b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence);
267a77ae3beSMatthias Ringwald #ifndef DEBUG_PLC
268a77ae3beSMatthias Ringwald                 log_info("PLC for packet 0x%04x, stream #%u", group_last_packet_sequence, i);
269a77ae3beSMatthias Ringwald #endif
27024b69d49SMatthias Ringwald                 plc_do(i);
27124b69d49SMatthias Ringwald                 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence);
27224b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
27324b69d49SMatthias Ringwald             }
27424b69d49SMatthias Ringwald         }
27524b69d49SMatthias Ringwald         group_last_packet_sequence++;
27624b69d49SMatthias Ringwald     }
27724b69d49SMatthias Ringwald }
27824b69d49SMatthias Ringwald 
27924b69d49SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) {
28024b69d49SMatthias Ringwald     // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets
28179d6b605SMatthias Ringwald     btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms);
28224b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(timer, plc_timeout);
28324b69d49SMatthias Ringwald     btstack_run_loop_add_timer(timer);
28424b69d49SMatthias Ringwald 
28579d6b605SMatthias Ringwald     switch (le_audio_demo_sink_type){
28679d6b605SMatthias Ringwald         case HCI_ISO_TYPE_CIS:
28779d6b605SMatthias Ringwald             // assume no packet received in iso interval => FT packets missed
2884e13b5b4SMatthias Ringwald             printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout);
28979d6b605SMatthias Ringwald             plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout);
29079d6b605SMatthias Ringwald             break;
29179d6b605SMatthias Ringwald         case HCI_ISO_TYPE_BIS:
29279d6b605SMatthias Ringwald             // assume PTO not used => 1 packet missed
29324b69d49SMatthias Ringwald             plc_check(group_last_packet_sequence + 1);
29479d6b605SMatthias Ringwald             break;
29579d6b605SMatthias Ringwald         default:
29679d6b605SMatthias Ringwald             btstack_unreachable();
29779d6b605SMatthias Ringwald             break;
29879d6b605SMatthias Ringwald     }
29924b69d49SMatthias Ringwald }
30024b69d49SMatthias Ringwald 
30124b69d49SMatthias Ringwald void le_audio_demo_util_sink_init(const char * filename_wav){
30224b69d49SMatthias Ringwald     le_audio_demo_sink_filename_wav = filename_wav;
3030f01c821SMatthias Ringwald     le_audio_demo_util_sink_state = LE_AUDIO_SINK_INIT;
30424b69d49SMatthias Ringwald }
30524b69d49SMatthias Ringwald 
30624b69d49SMatthias Ringwald void le_audio_demo_util_sink_enable_lc3plus(bool enable){
30724b69d49SMatthias Ringwald     le_audio_demo_lc3plus_decoder_requested = enable;
30824b69d49SMatthias Ringwald }
30924b69d49SMatthias Ringwald 
310bc66a7deSMatthias Ringwald static void setup_lc3_decoder(bool use_lc3plus_decoder){
311f4c19309SMatthias Ringwald     uint8_t channel;
312f4c19309SMatthias Ringwald     for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){
313f4c19309SMatthias Ringwald         // pick decoder
314f4c19309SMatthias Ringwald         void * decoder_context = NULL;
315f4c19309SMatthias Ringwald #ifdef HAVE_LC3PLUS
316f4c19309SMatthias Ringwald         if (use_lc3plus_decoder){
317f4c19309SMatthias Ringwald             decoder_context = &fraunhofer_decoder_contexts[channel];
318f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context);
319f4c19309SMatthias Ringwald         }
320f4c19309SMatthias Ringwald         else
321f4c19309SMatthias Ringwald #endif
322f4c19309SMatthias Ringwald         {
323f4c19309SMatthias Ringwald             decoder_context = &google_decoder_contexts[channel];
324f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context);
325f4c19309SMatthias Ringwald         }
326f4c19309SMatthias Ringwald         decoder_contexts[channel] = decoder_context;
327f4c19309SMatthias 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);
328f4c19309SMatthias Ringwald     }
329f4c19309SMatthias Ringwald     btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
330f4c19309SMatthias Ringwald }
331f4c19309SMatthias Ringwald 
33224b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream,
33324b69d49SMatthias Ringwald                                                uint32_t sampling_frequency_hz,
33424b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
33524b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us) {
33624b69d49SMatthias Ringwald     le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz;
33724b69d49SMatthias Ringwald     le_audio_demo_sink_frame_duration = frame_duration;
33824b69d49SMatthias Ringwald     le_audio_demo_sink_octets_per_frame = octets_per_frame;
33924b69d49SMatthias Ringwald     le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us;
34024b69d49SMatthias Ringwald     le_audio_demo_sink_num_streams = num_streams;
34124b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream;
34224b69d49SMatthias Ringwald 
34378d87b81SDirk Helbig     sink_receive_streaming = false;
3440f01c821SMatthias Ringwald     le_audio_demo_util_sink_state = LE_AUDIO_SINK_CONFIGURED;
34578d87b81SDirk Helbig 
34624b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream;
34724b69d49SMatthias Ringwald     btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2));
34824b69d49SMatthias Ringwald 
34924b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames = 0;
35024b69d49SMatthias Ringwald 
351f4c19309SMatthias Ringwald     group_last_packet_received = false;
352f4c19309SMatthias Ringwald     uint8_t i;
353f4c19309SMatthias Ringwald     for (i=0;i<MAX_CHANNELS;i++){
354f4c19309SMatthias Ringwald         stream_last_packet_received[i] = false;
355f4c19309SMatthias Ringwald         have_pcm[i] = false;
356f4c19309SMatthias Ringwald     }
357f4c19309SMatthias Ringwald 
35824b69d49SMatthias 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);
35924b69d49SMatthias Ringwald 
36024b69d49SMatthias Ringwald     // switch to lc3plus if requested and possible
36124b69d49SMatthias Ringwald     bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US);
36224b69d49SMatthias Ringwald 
36324b69d49SMatthias Ringwald     // init decoder
364bc66a7deSMatthias Ringwald     setup_lc3_decoder(use_lc3plus_decoder);
36524b69d49SMatthias Ringwald 
36624b69d49SMatthias Ringwald     printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n",
36724b69d49SMatthias Ringwald            num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder);
36824b69d49SMatthias Ringwald 
36924b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
37024b69d49SMatthias Ringwald     // create wav file
37124b69d49SMatthias Ringwald     printf("WAV file: %s\n", le_audio_demo_sink_filename_wav);
37224b69d49SMatthias Ringwald     wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz);
37324b69d49SMatthias Ringwald #endif
37424b69d49SMatthias Ringwald 
37524b69d49SMatthias Ringwald     // init playback buffer
37624b69d49SMatthias Ringwald     btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
37724b69d49SMatthias Ringwald 
37824b69d49SMatthias Ringwald     // calc start threshold in bytes for PLAYBACK_START_MS
37924b69d49SMatthias Ringwald     playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2;
38024b69d49SMatthias Ringwald 
3818dee1629SMatthias Ringwald     // sample rate compensation
3828dee1629SMatthias Ringwald     le_audio_demo_sink_received_samples = 0;
3838dee1629SMatthias Ringwald 
38424b69d49SMatthias Ringwald     // start playback
38524b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
38624b69d49SMatthias Ringwald     if (sink != NULL){
38778d87b81SDirk Helbig         btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() );
388*0b541edfSMatthias Ringwald         btstack_resample_init(&resample_instance, le_audio_demo_sink_num_channels);
38924b69d49SMatthias Ringwald         sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback);
39024b69d49SMatthias Ringwald         sink->start_stream();
39124b69d49SMatthias Ringwald     }
39224b69d49SMatthias Ringwald }
39324b69d49SMatthias Ringwald 
39424b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
39524b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
39624b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t flush_timeout){
39724b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_CIS;
39824b69d49SMatthias Ringwald     le_audio_demo_sink_flush_timeout = flush_timeout;
39979d6b605SMatthias Ringwald 
400f4c19309SMatthias Ringwald     // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval)
4010388b643SMatthias Ringwald     uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 8);
40279d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
40379d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
40479d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
40579d6b605SMatthias Ringwald 
40679d6b605SMatthias Ringwald     // set subsequent plc timeout: FT * ISO Interval
40779d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4;
40879d6b605SMatthias Ringwald 
40979d6b605SMatthias Ringwald     // set initial plc timeout:FT * ISO Interval + 4 ms
41079d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
41179d6b605SMatthias Ringwald 
41279d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
41379d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
41479d6b605SMatthias Ringwald 
41524b69d49SMatthias Ringwald     le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz,
41624b69d49SMatthias Ringwald                                               frame_duration, octets_per_frame, iso_interval_1250us);
41724b69d49SMatthias Ringwald }
41824b69d49SMatthias Ringwald 
41924b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
42024b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
42124b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) {
42224b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_BIS;
42324b69d49SMatthias Ringwald     le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset;
42479d6b605SMatthias Ringwald 
42579d6b605SMatthias Ringwald     // set playback start: ISO Interval + 10 ms
42679d6b605SMatthias Ringwald     uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10;
42779d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
42879d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
42979d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
43079d6b605SMatthias Ringwald 
43179d6b605SMatthias Ringwald     // set subsequent plc timeout: ISO Interval
43279d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4;
43379d6b605SMatthias Ringwald 
43479d6b605SMatthias Ringwald     // set initial plc timeout: ISO Interval + 4 ms
43579d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
43679d6b605SMatthias Ringwald 
43779d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
43879d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
43979d6b605SMatthias Ringwald 
440a8325db8SMatthias Ringwald     le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration, octets_per_frame, iso_interval_1250us);
44124b69d49SMatthias Ringwald }
44224b69d49SMatthias Ringwald 
443a18990d3SMatthias Ringwald void le_audio_demo_util_sink_count(uint8_t stream_index, uint8_t *packet, uint16_t size) {
444a18990d3SMatthias Ringwald     // check for missing packet
445a18990d3SMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
446a18990d3SMatthias Ringwald     uint8_t ts_flag = (header >> 14) & 1;
447a18990d3SMatthias Ringwald 
448a18990d3SMatthias Ringwald     uint16_t offset = 4;
449a18990d3SMatthias Ringwald     uint32_t time_stamp = 0;
450a18990d3SMatthias Ringwald     if (ts_flag){
451a18990d3SMatthias Ringwald         time_stamp = little_endian_read_32(packet, offset);
452a18990d3SMatthias Ringwald         offset += 4;
453a18990d3SMatthias Ringwald     }
454a18990d3SMatthias Ringwald 
455a18990d3SMatthias Ringwald     uint32_t receive_time_ms = btstack_run_loop_get_time_ms();
456a18990d3SMatthias Ringwald 
457a18990d3SMatthias Ringwald     uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
458a18990d3SMatthias Ringwald     offset += 4;
459a18990d3SMatthias Ringwald 
460a18990d3SMatthias Ringwald     uint16_t last_seq_no = last_packet_sequence[stream_index];
461a18990d3SMatthias Ringwald     bool packet_missed = (last_seq_no != 0) && ((last_seq_no + 1) != packet_sequence_number);
462a18990d3SMatthias Ringwald     if (packet_missed){
463a18990d3SMatthias Ringwald         // print last packet
464a18990d3SMatthias Ringwald         printf("\n");
465a18990d3SMatthias Ringwald         printf("%04x %10"PRIu32" %u ", last_seq_no, last_packet_time_ms[stream_index], stream_index);
466a18990d3SMatthias Ringwald         printf_hexdump(&last_packet_prefix[le_audio_demo_sink_num_streams*PACKET_PREFIX_LEN], PACKET_PREFIX_LEN);
467a18990d3SMatthias Ringwald         last_seq_no++;
468a18990d3SMatthias Ringwald 
469a18990d3SMatthias Ringwald         printf(ANSI_COLOR_RED);
470a18990d3SMatthias Ringwald         while (last_seq_no < packet_sequence_number){
471a18990d3SMatthias Ringwald             printf("%04x            %u MISSING\n", last_seq_no, stream_index);
472a18990d3SMatthias Ringwald             last_seq_no++;
473a18990d3SMatthias Ringwald         }
474a18990d3SMatthias Ringwald         printf(ANSI_COLOR_RESET);
475a18990d3SMatthias Ringwald 
476a18990d3SMatthias Ringwald         // print current packet
477a18990d3SMatthias Ringwald         printf("%04x %10"PRIu32" %u ", packet_sequence_number, receive_time_ms, stream_index);
478a18990d3SMatthias Ringwald         printf_hexdump(&packet[offset], PACKET_PREFIX_LEN);
479a18990d3SMatthias Ringwald     }
480a18990d3SMatthias Ringwald 
481a18990d3SMatthias Ringwald     // cache current packet
482a18990d3SMatthias Ringwald     memcpy(&last_packet_prefix[le_audio_demo_sink_num_streams*PACKET_PREFIX_LEN], &packet[offset], PACKET_PREFIX_LEN);
483a18990d3SMatthias Ringwald }
484a18990d3SMatthias Ringwald 
48524b69d49SMatthias Ringwald void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) {
4860f01c821SMatthias Ringwald 
4870f01c821SMatthias Ringwald     if (le_audio_demo_util_sink_state != LE_AUDIO_SINK_CONFIGURED) return;
4880f01c821SMatthias Ringwald 
48924b69d49SMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
49024b69d49SMatthias Ringwald     hci_con_handle_t con_handle = header & 0x0fff;
49124b69d49SMatthias Ringwald     uint8_t pb_flag = (header >> 12) & 3;
49224b69d49SMatthias Ringwald     uint8_t ts_flag = (header >> 14) & 1;
49324b69d49SMatthias Ringwald     uint16_t iso_load_len = little_endian_read_16(packet, 2);
49424b69d49SMatthias Ringwald 
49524b69d49SMatthias Ringwald     uint16_t offset = 4;
49624b69d49SMatthias Ringwald     uint32_t time_stamp = 0;
49724b69d49SMatthias Ringwald     if (ts_flag){
498ed07a8bdSMatthias Ringwald         time_stamp = little_endian_read_32(packet, offset);
49924b69d49SMatthias Ringwald         offset += 4;
50024b69d49SMatthias Ringwald     }
50124b69d49SMatthias Ringwald 
50224b69d49SMatthias Ringwald     uint32_t receive_time_ms = btstack_run_loop_get_time_ms();
50324b69d49SMatthias Ringwald 
50424b69d49SMatthias Ringwald     uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
50524b69d49SMatthias Ringwald     offset += 2;
50624b69d49SMatthias Ringwald 
50724b69d49SMatthias Ringwald     uint16_t header_2 = little_endian_read_16(packet, offset);
50824b69d49SMatthias Ringwald     uint16_t iso_sdu_length = header_2 & 0x3fff;
50924b69d49SMatthias Ringwald     uint8_t packet_status_flag = (uint8_t) (header_2 >> 14);
51024b69d49SMatthias Ringwald     offset += 2;
51124b69d49SMatthias Ringwald 
512ed07a8bdSMatthias Ringwald     // avoid warning for (yet) unused fields
513ed07a8bdSMatthias Ringwald     UNUSED(con_handle);
514ed07a8bdSMatthias Ringwald     UNUSED(pb_flag);
515ed07a8bdSMatthias Ringwald     UNUSED(iso_load_len);
516ed07a8bdSMatthias Ringwald     UNUSED(packet_status_flag);
517f020be6aSMatthias Ringwald     UNUSED(time_stamp);
518ed07a8bdSMatthias Ringwald 
51924b69d49SMatthias Ringwald     // start with first packet on first stream
52024b69d49SMatthias Ringwald     if (group_last_packet_received == false){
52124b69d49SMatthias Ringwald         if (stream_index != 0){
52224b69d49SMatthias Ringwald             printf("Ignore first packet for second stream\n");
52324b69d49SMatthias Ringwald             return;
52424b69d49SMatthias Ringwald         }
52524b69d49SMatthias Ringwald         group_last_packet_received = true;
52624b69d49SMatthias Ringwald         group_last_packet_sequence = packet_sequence_number;
52724b69d49SMatthias Ringwald     }
52824b69d49SMatthias Ringwald 
52924b69d49SMatthias Ringwald     if (stream_last_packet_received[stream_index]) {
53024b69d49SMatthias Ringwald         printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number);
53124b69d49SMatthias Ringwald 
53224b69d49SMatthias Ringwald         int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number,
53324b69d49SMatthias Ringwald                                                              stream_last_packet_sequence[stream_index]);
53424b69d49SMatthias Ringwald         if (packet_sequence_delta < 1) {
53524b69d49SMatthias Ringwald             // drop delayed packet that had already been generated by PLC
53624b69d49SMatthias Ringwald             printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n",
53724b69d49SMatthias Ringwald                        packet_sequence_number, stream_last_packet_sequence[stream_index]);
53824b69d49SMatthias Ringwald             return;
53924b69d49SMatthias Ringwald         }
54024b69d49SMatthias Ringwald         // simple check
54124b69d49SMatthias Ringwald         if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) {
54224b69d49SMatthias Ringwald             printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1);
54324b69d49SMatthias Ringwald         }
54424b69d49SMatthias Ringwald     } else {
54524b69d49SMatthias Ringwald         printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number);
54624b69d49SMatthias Ringwald         stream_last_packet_received[stream_index] = true;
54724b69d49SMatthias Ringwald     }
54824b69d49SMatthias Ringwald 
54965cfaa39SMatthias Ringwald     if (sink_receive_streaming){
55024b69d49SMatthias Ringwald         plc_check(packet_sequence_number);
55165cfaa39SMatthias Ringwald     }
55224b69d49SMatthias Ringwald 
553f4c19309SMatthias Ringwald     // either empty packets or num channels * num octets
554f4c19309SMatthias Ringwald     if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) {
555f4c19309SMatthias 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);
556f4c19309SMatthias 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);
557f4c19309SMatthias Ringwald         return;
558f4c19309SMatthias Ringwald     }
559f4c19309SMatthias Ringwald 
56078d87b81SDirk Helbig     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
56178d87b81SDirk Helbig     if( (sink != NULL) && (iso_sdu_length>0)) {
56278d87b81SDirk Helbig         if (!sink_receive_streaming && playback_active) {
56378d87b81SDirk Helbig             btstack_sample_rate_compensation_init(&sample_rate_compensation, receive_time_ms,
56478d87b81SDirk Helbig                                                   le_audio_demo_sink_sampling_frequency_hz, FLOAT_TO_Q15(1.f));
56578d87b81SDirk Helbig             sink_receive_streaming = true;
56678d87b81SDirk Helbig         }
56778d87b81SDirk Helbig     }
56878d87b81SDirk Helbig 
569f4c19309SMatthias Ringwald     if (iso_sdu_length == 0) {
57065cfaa39SMatthias Ringwald         if (sink_receive_streaming){
571f4c19309SMatthias Ringwald             // empty packet -> generate silence
572f4c19309SMatthias Ringwald             memset(pcm, 0, sizeof(pcm));
57324b69d49SMatthias Ringwald             uint8_t i;
57424b69d49SMatthias Ringwald             for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) {
575cad32b97SMatthias Ringwald                 uint8_t effective_channel = (stream_index * le_audio_demo_sink_num_channels_per_stream) + i;
576cad32b97SMatthias Ringwald                 have_pcm[effective_channel] = true;
577f4c19309SMatthias Ringwald             }
57865cfaa39SMatthias Ringwald             le_audio_demo_sink_zero_frames++;
57965cfaa39SMatthias Ringwald             // pause detection (1000 ms for 10 ms, 750 ms for 7.5 ms frames)
58065cfaa39SMatthias Ringwald             if (le_audio_demo_sink_zero_frames > 100){
58165cfaa39SMatthias Ringwald                 printf("Pause detected, stopping audio\n");
58265cfaa39SMatthias Ringwald                 log_info("Pause detected, stopping audio");
58365cfaa39SMatthias Ringwald                 // pause/reset audio
58465cfaa39SMatthias Ringwald                 btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
58565cfaa39SMatthias Ringwald                 sink_receive_streaming = false;
58665cfaa39SMatthias Ringwald                 playback_active = false;
58765cfaa39SMatthias Ringwald             }
58865cfaa39SMatthias Ringwald         }
589f4c19309SMatthias Ringwald     } else {
590f4c19309SMatthias Ringwald         // regular packet -> decode codec frame
59165cfaa39SMatthias Ringwald         le_audio_demo_sink_zero_frames = 0;
592f4c19309SMatthias Ringwald         uint8_t i;
593f4c19309SMatthias Ringwald         for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){
59424b69d49SMatthias Ringwald             uint8_t tmp_BEC_detect;
59524b69d49SMatthias Ringwald             uint8_t BFI = 0;
596cad32b97SMatthias Ringwald             uint8_t effective_channel = (stream_index * le_audio_demo_sink_num_channels_per_stream) + i;
59724b69d49SMatthias Ringwald             (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI,
59824b69d49SMatthias Ringwald                                                  &pcm[effective_channel], le_audio_demo_sink_num_channels,
59924b69d49SMatthias Ringwald                                                  &tmp_BEC_detect);
60024b69d49SMatthias Ringwald             offset += le_audio_demo_sink_octets_per_frame;
601cad32b97SMatthias Ringwald             have_pcm[effective_channel] = true;
60224b69d49SMatthias Ringwald         }
603f4c19309SMatthias Ringwald     }
60424b69d49SMatthias Ringwald 
60524b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
60624b69d49SMatthias Ringwald 
6078dee1629SMatthias Ringwald     if( (sink != NULL) && (iso_sdu_length>0)) {
6088dee1629SMatthias Ringwald         if( sink_receive_streaming ) {
6098dee1629SMatthias Ringwald             uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, receive_time_ms,
6108dee1629SMatthias Ringwald                                                                                   le_audio_demo_sink_received_samples, sink->get_samplerate() );
6118dee1629SMatthias Ringwald             btstack_resample_set_factor(&resample_instance, resampling_factor);
6128dee1629SMatthias Ringwald             le_audio_demo_sink_received_samples = 0;
6138dee1629SMatthias Ringwald         }
6148dee1629SMatthias Ringwald     }
6158dee1629SMatthias Ringwald 
61624b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames++;
61724b69d49SMatthias Ringwald 
61824b69d49SMatthias Ringwald     // PLC
61924b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
62079d6b605SMatthias Ringwald     btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms);
62124b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout);
62224b69d49SMatthias Ringwald     btstack_run_loop_add_timer(&next_packet_timer);
62324b69d49SMatthias Ringwald 
62424b69d49SMatthias Ringwald     if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){
62524b69d49SMatthias 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);
62624b69d49SMatthias Ringwald         samples_received = 0;
62724b69d49SMatthias Ringwald         samples_dropped  =  0;
62824b69d49SMatthias Ringwald         samples_played = 0;
62924b69d49SMatthias Ringwald     }
63024b69d49SMatthias Ringwald 
63124b69d49SMatthias Ringwald     stream_last_packet_sequence[stream_index] = packet_sequence_number;
63224b69d49SMatthias Ringwald }
63324b69d49SMatthias Ringwald 
63424b69d49SMatthias Ringwald /**
63524b69d49SMatthias Ringwald  * @brief Close sink: close wav file, stop playback
63624b69d49SMatthias Ringwald  */
63724b69d49SMatthias Ringwald void le_audio_demo_util_sink_close(void){
63824b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
63924b69d49SMatthias Ringwald     printf("Close WAV file\n");
64024b69d49SMatthias Ringwald     wav_writer_close();
64124b69d49SMatthias Ringwald #endif
64224b69d49SMatthias Ringwald     // stop playback
64324b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
64424b69d49SMatthias Ringwald     if (sink != NULL){
64524b69d49SMatthias Ringwald         sink->stop_stream();
64624b69d49SMatthias Ringwald     }
6470f01c821SMatthias Ringwald     le_audio_demo_util_sink_state = LE_AUDIO_SINK_INIT;
64878d87b81SDirk Helbig     sink_receive_streaming = false;
64924b69d49SMatthias Ringwald     // stop timer
65024b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
65124b69d49SMatthias Ringwald }
652