xref: /btstack/example/le_audio_demo_util_sink.c (revision 4e13b5b46d97c7d4cfddf7cd14f10ace0fb5ee92)
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 
4024b69d49SMatthias Ringwald #include "le_audio_demo_util_sink.h"
4124b69d49SMatthias Ringwald 
4224b69d49SMatthias Ringwald #include "btstack_bool.h"
4324b69d49SMatthias Ringwald #include "btstack_config.h"
4424b69d49SMatthias Ringwald #include <btstack_debug.h>
4524b69d49SMatthias Ringwald #include <printf.h>
4624b69d49SMatthias Ringwald 
4724b69d49SMatthias Ringwald #include "hci.h"
4824b69d49SMatthias Ringwald #include "btstack_audio.h"
4924b69d49SMatthias Ringwald #include "btstack_lc3_google.h"
5024b69d49SMatthias Ringwald #include "btstack_lc3plus_fraunhofer.h"
5124b69d49SMatthias Ringwald 
5224b69d49SMatthias Ringwald #include "hxcmod.h"
5324b69d49SMatthias Ringwald #include "mods/mod.h"
5424b69d49SMatthias Ringwald 
5524b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
5624b69d49SMatthias Ringwald #include "wav_util.h"
5724b69d49SMatthias Ringwald #include "btstack_ring_buffer.h"
5824b69d49SMatthias Ringwald 
5924b69d49SMatthias Ringwald #endif
6024b69d49SMatthias Ringwald 
6124b69d49SMatthias Ringwald //#define DEBUG_PLC
6224b69d49SMatthias Ringwald #ifdef DEBUG_PLC
63*4e13b5b4SMatthias Ringwald #define printf_plc(...) { \
64*4e13b5b4SMatthias Ringwald     printf(__VA_ARGS__);  \
65*4e13b5b4SMatthias Ringwald     log_info(__VA_ARGS__);\
66*4e13b5b4SMatthias Ringwald }
6724b69d49SMatthias Ringwald #else
6824b69d49SMatthias Ringwald #define printf_plc(...)  (void)(0);
6924b69d49SMatthias Ringwald #endif
7024b69d49SMatthias Ringwald 
7124b69d49SMatthias Ringwald #define MAX_CHANNELS 2
7224b69d49SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480
7324b69d49SMatthias Ringwald #define MAX_LC3_FRAME_BYTES   155
7424b69d49SMatthias Ringwald 
7524b69d49SMatthias Ringwald // playback
7624b69d49SMatthias Ringwald #define MAX_NUM_LC3_FRAMES   15
7724b69d49SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4
7824b69d49SMatthias Ringwald #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE)
7924b69d49SMatthias Ringwald #define PLAYBACK_START_MS (MAX_NUM_LC3_FRAMES * 20 / 3)
8024b69d49SMatthias Ringwald 
8124b69d49SMatthias Ringwald #define ANSI_COLOR_RED     "\x1b[31m"
8224b69d49SMatthias Ringwald #define ANSI_COLOR_GREEN   "\x1b[32m"
8324b69d49SMatthias Ringwald #define ANSI_COLOR_YELLOW  "\x1b[33m"
8424b69d49SMatthias Ringwald #define ANSI_COLOR_BLUE    "\x1b[34m"
8524b69d49SMatthias Ringwald #define ANSI_COLOR_MAGENTA "\x1b[35m"
8624b69d49SMatthias Ringwald #define ANSI_COLOR_CYAN    "\x1b[36m"
8724b69d49SMatthias Ringwald #define ANSI_COLOR_RESET   "\x1b[0m"
8824b69d49SMatthias Ringwald 
8924b69d49SMatthias Ringwald // SINK
9024b69d49SMatthias Ringwald 
9124b69d49SMatthias Ringwald static const char * le_audio_demo_sink_filename_wav;
9224b69d49SMatthias Ringwald 
9324b69d49SMatthias Ringwald static btstack_lc3_frame_duration_t le_audio_demo_sink_frame_duration;
9424b69d49SMatthias Ringwald static hci_iso_type_t               le_audio_demo_sink_type;
9524b69d49SMatthias Ringwald 
9624b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_sampling_frequency_hz;
9724b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_num_samples_per_frame;
9824b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_streams;
9924b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels_per_stream;
10024b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_num_channels;
10124b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_octets_per_frame;
10224b69d49SMatthias Ringwald static uint16_t le_audio_demo_sink_iso_interval_1250us;
10324b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_flush_timeout;
10424b69d49SMatthias Ringwald static uint8_t  le_audio_demo_sink_pre_transmission_offset;
10524b69d49SMatthias Ringwald 
10624b69d49SMatthias Ringwald // playback
10724b69d49SMatthias Ringwald static uint16_t              playback_start_threshold_bytes;
10824b69d49SMatthias Ringwald static bool                  playback_active;
10924b69d49SMatthias Ringwald static uint8_t               playback_buffer_storage[PLAYBACK_BUFFER_SIZE];
11024b69d49SMatthias Ringwald static btstack_ring_buffer_t playback_buffer;
11124b69d49SMatthias Ringwald 
11224b69d49SMatthias Ringwald // PLC
11324b69d49SMatthias Ringwald static bool     stream_last_packet_received[MAX_CHANNELS];
11424b69d49SMatthias Ringwald static uint16_t stream_last_packet_sequence[MAX_CHANNELS];
11524b69d49SMatthias Ringwald static uint16_t group_last_packet_sequence;
11624b69d49SMatthias Ringwald static bool     group_last_packet_received;
11779d6b605SMatthias Ringwald static uint16_t plc_timeout_initial_ms;
11879d6b605SMatthias Ringwald static uint16_t plc_timeout_subsequent_ms;
11924b69d49SMatthias Ringwald 
12024b69d49SMatthias Ringwald static uint32_t le_audio_demo_sink_lc3_frames;
12124b69d49SMatthias Ringwald static uint32_t samples_received;
12224b69d49SMatthias Ringwald static uint32_t samples_played;
12324b69d49SMatthias Ringwald static uint32_t samples_dropped;
12424b69d49SMatthias Ringwald 
12524b69d49SMatthias Ringwald static btstack_timer_source_t next_packet_timer;
12624b69d49SMatthias Ringwald 
12724b69d49SMatthias Ringwald // lc3 decoder
12824b69d49SMatthias Ringwald static bool le_audio_demo_lc3plus_decoder_requested = false;
12924b69d49SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder;
13024b69d49SMatthias Ringwald static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
13124b69d49SMatthias Ringwald static bool have_pcm[MAX_CHANNELS];
13224b69d49SMatthias Ringwald 
13324b69d49SMatthias Ringwald static btstack_lc3_decoder_google_t google_decoder_contexts[MAX_CHANNELS];
13424b69d49SMatthias Ringwald #ifdef HAVE_LC3PLUS
13524b69d49SMatthias Ringwald static btstack_lc3plus_fraunhofer_decoder_t fraunhofer_decoder_contexts[MAX_CHANNELS];
13624b69d49SMatthias Ringwald #endif
13724b69d49SMatthias Ringwald static void * decoder_contexts[MAX_CHANNELS];
13824b69d49SMatthias Ringwald 
13924b69d49SMatthias Ringwald static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){
14024b69d49SMatthias Ringwald     // called from lower-layer but guaranteed to be on main thread
14124b69d49SMatthias Ringwald     log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
14224b69d49SMatthias Ringwald 
14324b69d49SMatthias Ringwald     samples_played += num_samples;
14424b69d49SMatthias Ringwald 
14524b69d49SMatthias Ringwald     uint32_t bytes_needed = num_samples * le_audio_demo_sink_num_channels * 2;
14624b69d49SMatthias Ringwald     if (playback_active == false){
14724b69d49SMatthias Ringwald         if (btstack_ring_buffer_bytes_available(&playback_buffer) >= playback_start_threshold_bytes) {
14824b69d49SMatthias Ringwald             log_info("Playback started");
14924b69d49SMatthias Ringwald             playback_active = true;
15024b69d49SMatthias Ringwald         }
15124b69d49SMatthias Ringwald     } else {
15224b69d49SMatthias Ringwald         if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)) {
15324b69d49SMatthias Ringwald             log_info("Playback underrun");
15424b69d49SMatthias Ringwald             printf("Playback Underrun\n");
15524b69d49SMatthias Ringwald             // empty buffer
15624b69d49SMatthias Ringwald             uint32_t bytes_read;
15724b69d49SMatthias Ringwald             btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
15824b69d49SMatthias Ringwald             playback_active = false;
15924b69d49SMatthias Ringwald         }
16024b69d49SMatthias Ringwald     }
16124b69d49SMatthias Ringwald 
16224b69d49SMatthias Ringwald     if (playback_active){
16324b69d49SMatthias Ringwald         uint32_t bytes_read;
16424b69d49SMatthias Ringwald         btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
16524b69d49SMatthias Ringwald         btstack_assert(bytes_read == bytes_needed);
16624b69d49SMatthias Ringwald     } else {
16724b69d49SMatthias Ringwald         memset(buffer, 0, bytes_needed);
16824b69d49SMatthias Ringwald     }
16924b69d49SMatthias Ringwald }
17024b69d49SMatthias Ringwald 
17124b69d49SMatthias Ringwald static void store_samples_in_ringbuffer(void){
17224b69d49SMatthias Ringwald     // check if we have all channels
17324b69d49SMatthias Ringwald     uint8_t channel;
17424b69d49SMatthias Ringwald     for (channel = 0; channel < le_audio_demo_sink_num_channels; channel++){
17524b69d49SMatthias Ringwald         if (have_pcm[channel] == false) return;
17624b69d49SMatthias Ringwald     }
17724b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
17824b69d49SMatthias Ringwald     // write wav samples
17924b69d49SMatthias Ringwald     wav_writer_write_int16(le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame, pcm);
18024b69d49SMatthias Ringwald #endif
18124b69d49SMatthias Ringwald     // store samples in playback buffer
18224b69d49SMatthias Ringwald     uint32_t bytes_to_store = le_audio_demo_sink_num_channels * le_audio_demo_sink_num_samples_per_frame * 2;
18324b69d49SMatthias Ringwald     samples_received += le_audio_demo_sink_num_samples_per_frame;
18424b69d49SMatthias Ringwald     if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) {
18524b69d49SMatthias Ringwald         btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm, bytes_to_store);
18624b69d49SMatthias Ringwald     } else {
18724b69d49SMatthias Ringwald         printf("Samples dropped\n");
18824b69d49SMatthias Ringwald         samples_dropped += le_audio_demo_sink_num_samples_per_frame;
18924b69d49SMatthias Ringwald     }
19024b69d49SMatthias Ringwald     memset(have_pcm, 0, sizeof(have_pcm));
19124b69d49SMatthias Ringwald }
19224b69d49SMatthias Ringwald 
19324b69d49SMatthias Ringwald static void plc_do(uint8_t stream_index) {
19424b69d49SMatthias Ringwald     // inject packet
19524b69d49SMatthias Ringwald     uint8_t tmp_BEC_detect;
19624b69d49SMatthias Ringwald     uint8_t BFI = 1;
19724b69d49SMatthias Ringwald     uint8_t i;
19824b69d49SMatthias Ringwald     for (i = 0; i < le_audio_demo_sink_num_channels_per_stream; i++){
19924b69d49SMatthias Ringwald         uint8_t effective_channel = stream_index + i;
20024b69d49SMatthias Ringwald         (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], NULL, BFI,
20124b69d49SMatthias Ringwald                                              &pcm[effective_channel], le_audio_demo_sink_num_channels,
20224b69d49SMatthias Ringwald                                              &tmp_BEC_detect);
20324b69d49SMatthias Ringwald     }
20424b69d49SMatthias Ringwald     // and store in ringbuffer when PCM for all channels is available
20524b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
20624b69d49SMatthias Ringwald }
20724b69d49SMatthias Ringwald 
20824b69d49SMatthias Ringwald //
20924b69d49SMatthias Ringwald // Perform PLC for packets missing in previous intervals
21024b69d49SMatthias Ringwald //
21124b69d49SMatthias Ringwald // assumptions:
21224b69d49SMatthias Ringwald // - packet sequence number is monotonic increasing
21324b69d49SMatthias Ringwald // - if packet with seq nr x is received, all packets with smaller seq number are either received or missed
21424b69d49SMatthias Ringwald static void plc_check(uint16_t packet_sequence_number) {
21524b69d49SMatthias Ringwald     while (group_last_packet_sequence != packet_sequence_number){
21624b69d49SMatthias Ringwald         uint8_t i;
21724b69d49SMatthias Ringwald         for (i=0;i<le_audio_demo_sink_num_streams;i++){
21824b69d49SMatthias Ringwald             // deal with first packet missing. inject silent samples, pcm buffer is memset to zero at start
21924b69d49SMatthias Ringwald             if (stream_last_packet_received[i] == false){
22024b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, very first packet missing\n", i);
22124b69d49SMatthias Ringwald                 have_pcm[i] = true;
22224b69d49SMatthias Ringwald                 store_samples_in_ringbuffer();
22324b69d49SMatthias Ringwald 
22424b69d49SMatthias Ringwald                 stream_last_packet_received[i] = true;
22524b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
22624b69d49SMatthias Ringwald                 continue;
22724b69d49SMatthias Ringwald             }
22824b69d49SMatthias Ringwald 
22924b69d49SMatthias Ringwald             // missing packet if big sequence counter is higher than bis sequence counter
23024b69d49SMatthias Ringwald             if (btstack_time16_delta(group_last_packet_sequence, stream_last_packet_sequence[i]) > 0) {
23124b69d49SMatthias Ringwald                 printf_plc("- ISO #%u, PLC for %u\n", i, group_last_packet_sequence);
23224b69d49SMatthias Ringwald                 plc_do(i);
23324b69d49SMatthias Ringwald                 btstack_assert((stream_last_packet_sequence[i] + 1) == group_last_packet_sequence);
23424b69d49SMatthias Ringwald                 stream_last_packet_sequence[i] = group_last_packet_sequence;
23524b69d49SMatthias Ringwald             }
23624b69d49SMatthias Ringwald         }
23724b69d49SMatthias Ringwald         group_last_packet_sequence++;
23824b69d49SMatthias Ringwald     }
23924b69d49SMatthias Ringwald }
24024b69d49SMatthias Ringwald 
24124b69d49SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) {
24224b69d49SMatthias Ringwald     // Restart timer. This will loose sync with ISO interval, but if we stop caring if we loose that many packets
24379d6b605SMatthias Ringwald     btstack_run_loop_set_timer(timer, plc_timeout_subsequent_ms);
24424b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(timer, plc_timeout);
24524b69d49SMatthias Ringwald     btstack_run_loop_add_timer(timer);
24624b69d49SMatthias Ringwald 
24779d6b605SMatthias Ringwald     switch (le_audio_demo_sink_type){
24879d6b605SMatthias Ringwald         case HCI_ISO_TYPE_CIS:
24979d6b605SMatthias Ringwald             // assume no packet received in iso interval => FT packets missed
250*4e13b5b4SMatthias Ringwald             printf_plc("PLC: timeout cis, group %u, FT %u", group_last_packet_sequence, le_audio_demo_sink_flush_timeout);
25179d6b605SMatthias Ringwald             plc_check(group_last_packet_sequence + le_audio_demo_sink_flush_timeout);
25279d6b605SMatthias Ringwald             break;
25379d6b605SMatthias Ringwald         case HCI_ISO_TYPE_BIS:
25479d6b605SMatthias Ringwald             // assume PTO not used => 1 packet missed
25524b69d49SMatthias Ringwald             plc_check(group_last_packet_sequence + 1);
25679d6b605SMatthias Ringwald             break;
25779d6b605SMatthias Ringwald         default:
25879d6b605SMatthias Ringwald             btstack_unreachable();
25979d6b605SMatthias Ringwald             break;
26079d6b605SMatthias Ringwald     }
26124b69d49SMatthias Ringwald }
26224b69d49SMatthias Ringwald 
26324b69d49SMatthias Ringwald void le_audio_demo_util_sink_init(const char * filename_wav){
26424b69d49SMatthias Ringwald     le_audio_demo_sink_filename_wav = filename_wav;
26524b69d49SMatthias Ringwald }
26624b69d49SMatthias Ringwald 
26724b69d49SMatthias Ringwald void le_audio_demo_util_sink_enable_lc3plus(bool enable){
26824b69d49SMatthias Ringwald     le_audio_demo_lc3plus_decoder_requested = enable;
26924b69d49SMatthias Ringwald }
27024b69d49SMatthias Ringwald 
271bc66a7deSMatthias Ringwald static void setup_lc3_decoder(bool use_lc3plus_decoder){
272f4c19309SMatthias Ringwald     uint8_t channel;
273f4c19309SMatthias Ringwald     for (channel = 0 ; channel < le_audio_demo_sink_num_channels ; channel++){
274f4c19309SMatthias Ringwald         // pick decoder
275f4c19309SMatthias Ringwald         void * decoder_context = NULL;
276f4c19309SMatthias Ringwald #ifdef HAVE_LC3PLUS
277f4c19309SMatthias Ringwald         if (use_lc3plus_decoder){
278f4c19309SMatthias Ringwald             decoder_context = &fraunhofer_decoder_contexts[channel];
279f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3plus_fraunhofer_decoder_init_instance(decoder_context);
280f4c19309SMatthias Ringwald         }
281f4c19309SMatthias Ringwald         else
282f4c19309SMatthias Ringwald #endif
283f4c19309SMatthias Ringwald         {
284f4c19309SMatthias Ringwald             decoder_context = &google_decoder_contexts[channel];
285f4c19309SMatthias Ringwald             lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context);
286f4c19309SMatthias Ringwald         }
287f4c19309SMatthias Ringwald         decoder_contexts[channel] = decoder_context;
288f4c19309SMatthias 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);
289f4c19309SMatthias Ringwald     }
290f4c19309SMatthias Ringwald     btstack_assert(le_audio_demo_sink_num_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
291f4c19309SMatthias Ringwald }
292f4c19309SMatthias Ringwald 
29324b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_general(uint8_t num_streams, uint8_t num_channels_per_stream,
29424b69d49SMatthias Ringwald                                                uint32_t sampling_frequency_hz,
29524b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
29624b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us) {
29724b69d49SMatthias Ringwald     le_audio_demo_sink_sampling_frequency_hz = sampling_frequency_hz;
29824b69d49SMatthias Ringwald     le_audio_demo_sink_frame_duration = frame_duration;
29924b69d49SMatthias Ringwald     le_audio_demo_sink_octets_per_frame = octets_per_frame;
30024b69d49SMatthias Ringwald     le_audio_demo_sink_iso_interval_1250us = iso_interval_1250us;
30124b69d49SMatthias Ringwald     le_audio_demo_sink_num_streams = num_streams;
30224b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels_per_stream = num_channels_per_stream;
30324b69d49SMatthias Ringwald 
30424b69d49SMatthias Ringwald     le_audio_demo_sink_num_channels = le_audio_demo_sink_num_streams * le_audio_demo_sink_num_channels_per_stream;
30524b69d49SMatthias Ringwald     btstack_assert((le_audio_demo_sink_num_channels == 1) || (le_audio_demo_sink_num_channels == 2));
30624b69d49SMatthias Ringwald 
30724b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames = 0;
30824b69d49SMatthias Ringwald 
309f4c19309SMatthias Ringwald     group_last_packet_received = false;
310f4c19309SMatthias Ringwald     uint8_t i;
311f4c19309SMatthias Ringwald     for (i=0;i<MAX_CHANNELS;i++){
312f4c19309SMatthias Ringwald         stream_last_packet_received[i] = false;
313f4c19309SMatthias Ringwald         have_pcm[i] = false;
314f4c19309SMatthias Ringwald     }
315f4c19309SMatthias Ringwald 
31624b69d49SMatthias 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);
31724b69d49SMatthias Ringwald 
31824b69d49SMatthias Ringwald     // switch to lc3plus if requested and possible
31924b69d49SMatthias Ringwald     bool use_lc3plus_decoder = le_audio_demo_lc3plus_decoder_requested && (frame_duration == BTSTACK_LC3_FRAME_DURATION_10000US);
32024b69d49SMatthias Ringwald 
32124b69d49SMatthias Ringwald     // init decoder
322bc66a7deSMatthias Ringwald     setup_lc3_decoder(use_lc3plus_decoder);
32324b69d49SMatthias Ringwald 
32424b69d49SMatthias Ringwald     printf("Configure: %u streams, %u channels per stream, sampling rate %u, samples per frame %u, lc3plus %u\n",
32524b69d49SMatthias Ringwald            num_streams, num_channels_per_stream, sampling_frequency_hz, le_audio_demo_sink_num_samples_per_frame, use_lc3plus_decoder);
32624b69d49SMatthias Ringwald 
32724b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
32824b69d49SMatthias Ringwald     // create wav file
32924b69d49SMatthias Ringwald     printf("WAV file: %s\n", le_audio_demo_sink_filename_wav);
33024b69d49SMatthias Ringwald     wav_writer_open(le_audio_demo_sink_filename_wav, le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz);
33124b69d49SMatthias Ringwald #endif
33224b69d49SMatthias Ringwald 
33324b69d49SMatthias Ringwald     // init playback buffer
33424b69d49SMatthias Ringwald     btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
33524b69d49SMatthias Ringwald 
33624b69d49SMatthias Ringwald     // calc start threshold in bytes for PLAYBACK_START_MS
33724b69d49SMatthias Ringwald     playback_start_threshold_bytes = (sampling_frequency_hz / 1000 * PLAYBACK_START_MS) * le_audio_demo_sink_num_channels * 2;
33824b69d49SMatthias Ringwald 
33924b69d49SMatthias Ringwald     // start playback
34024b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
34124b69d49SMatthias Ringwald     if (sink != NULL){
34224b69d49SMatthias Ringwald         sink->init(le_audio_demo_sink_num_channels, le_audio_demo_sink_sampling_frequency_hz, le_audio_connection_sink_playback);
34324b69d49SMatthias Ringwald         sink->start_stream();
34424b69d49SMatthias Ringwald     }
34524b69d49SMatthias Ringwald }
34624b69d49SMatthias Ringwald 
34724b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_unicast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
34824b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
34924b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t flush_timeout){
35024b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_CIS;
35124b69d49SMatthias Ringwald     le_audio_demo_sink_flush_timeout = flush_timeout;
35279d6b605SMatthias Ringwald 
353f4c19309SMatthias Ringwald     // set playback start: FT * ISO Interval + max(10 ms, 1/2 ISO Interval)
354f4c19309SMatthias Ringwald     uint16_t playback_start_ms = flush_timeout * (iso_interval_1250us * 5 / 4) + btstack_max(10, iso_interval_1250us * 5 / 2);
35579d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
35679d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
35779d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
35879d6b605SMatthias Ringwald 
35979d6b605SMatthias Ringwald     // set subsequent plc timeout: FT * ISO Interval
36079d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = flush_timeout * iso_interval_1250us * 5 / 4;
36179d6b605SMatthias Ringwald 
36279d6b605SMatthias Ringwald     // set initial plc timeout:FT * ISO Interval + 4 ms
36379d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
36479d6b605SMatthias Ringwald 
36579d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
36679d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
36779d6b605SMatthias Ringwald 
36824b69d49SMatthias Ringwald     le_audio_demo_util_sink_configure_general(num_streams, num_channels_per_stream, sampling_frequency_hz,
36924b69d49SMatthias Ringwald                                               frame_duration, octets_per_frame, iso_interval_1250us);
37024b69d49SMatthias Ringwald }
37124b69d49SMatthias Ringwald 
37224b69d49SMatthias Ringwald void le_audio_demo_util_sink_configure_broadcast(uint8_t num_streams, uint8_t num_channels_per_stream, uint32_t sampling_frequency_hz,
37324b69d49SMatthias Ringwald                                                btstack_lc3_frame_duration_t frame_duration, uint16_t octets_per_frame,
37424b69d49SMatthias Ringwald                                                uint32_t iso_interval_1250us, uint8_t pre_transmission_offset) {
37524b69d49SMatthias Ringwald     le_audio_demo_sink_type = HCI_ISO_TYPE_BIS;
37624b69d49SMatthias Ringwald     le_audio_demo_sink_pre_transmission_offset = pre_transmission_offset;
37779d6b605SMatthias Ringwald 
37879d6b605SMatthias Ringwald     // set playback start: ISO Interval + 10 ms
37979d6b605SMatthias Ringwald     uint16_t playback_start_ms = (iso_interval_1250us * 5 / 4) + 10;
38079d6b605SMatthias Ringwald     uint16_t playback_start_samples = sampling_frequency_hz / 1000 * playback_start_ms;
38179d6b605SMatthias Ringwald     playback_start_threshold_bytes = playback_start_samples * num_streams * num_channels_per_stream * 2;
38279d6b605SMatthias Ringwald     printf("Playback: start %u ms (%u samples, %u bytes)\n", playback_start_ms, playback_start_samples, playback_start_threshold_bytes);
38379d6b605SMatthias Ringwald 
38479d6b605SMatthias Ringwald     // set subsequent plc timeout: ISO Interval
38579d6b605SMatthias Ringwald     plc_timeout_subsequent_ms = iso_interval_1250us * 5 / 4;
38679d6b605SMatthias Ringwald 
38779d6b605SMatthias Ringwald     // set initial plc timeout: ISO Interval + 4 ms
38879d6b605SMatthias Ringwald     plc_timeout_initial_ms = plc_timeout_subsequent_ms + 4;
38979d6b605SMatthias Ringwald 
39079d6b605SMatthias Ringwald     printf("PLC: initial timeout    %u ms\n", plc_timeout_initial_ms);
39179d6b605SMatthias Ringwald     printf("PLC: subsequent timeout %u ms\n", plc_timeout_subsequent_ms);
39279d6b605SMatthias Ringwald 
39324b69d49SMatthias Ringwald     le_audio_demo_util_sink_configure_unicast(num_streams, num_channels_per_stream, sampling_frequency_hz, frame_duration,
39424b69d49SMatthias Ringwald                                               octets_per_frame, iso_interval_1250us, pre_transmission_offset);
39524b69d49SMatthias Ringwald }
39624b69d49SMatthias Ringwald 
39724b69d49SMatthias Ringwald void le_audio_demo_util_sink_receive(uint8_t stream_index, uint8_t *packet, uint16_t size) {
39824b69d49SMatthias Ringwald     uint16_t header = little_endian_read_16(packet, 0);
39924b69d49SMatthias Ringwald     hci_con_handle_t con_handle = header & 0x0fff;
40024b69d49SMatthias Ringwald     uint8_t pb_flag = (header >> 12) & 3;
40124b69d49SMatthias Ringwald     uint8_t ts_flag = (header >> 14) & 1;
40224b69d49SMatthias Ringwald     uint16_t iso_load_len = little_endian_read_16(packet, 2);
40324b69d49SMatthias Ringwald 
40424b69d49SMatthias Ringwald     uint16_t offset = 4;
40524b69d49SMatthias Ringwald     uint32_t time_stamp = 0;
40624b69d49SMatthias Ringwald     if (ts_flag){
40724b69d49SMatthias Ringwald         uint32_t time_stamp = little_endian_read_32(packet, offset);
40824b69d49SMatthias Ringwald         offset += 4;
40924b69d49SMatthias Ringwald     }
41024b69d49SMatthias Ringwald 
41124b69d49SMatthias Ringwald     uint32_t receive_time_ms = btstack_run_loop_get_time_ms();
41224b69d49SMatthias Ringwald 
41324b69d49SMatthias Ringwald     uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
41424b69d49SMatthias Ringwald     offset += 2;
41524b69d49SMatthias Ringwald 
41624b69d49SMatthias Ringwald     uint16_t header_2 = little_endian_read_16(packet, offset);
41724b69d49SMatthias Ringwald     uint16_t iso_sdu_length = header_2 & 0x3fff;
41824b69d49SMatthias Ringwald     uint8_t packet_status_flag = (uint8_t) (header_2 >> 14);
41924b69d49SMatthias Ringwald     offset += 2;
42024b69d49SMatthias Ringwald 
42124b69d49SMatthias Ringwald     // start with first packet on first stream
42224b69d49SMatthias Ringwald     if (group_last_packet_received == false){
42324b69d49SMatthias Ringwald         if (stream_index != 0){
42424b69d49SMatthias Ringwald             printf("Ignore first packet for second stream\n");
42524b69d49SMatthias Ringwald             return;
42624b69d49SMatthias Ringwald         }
42724b69d49SMatthias Ringwald         group_last_packet_received = true;
42824b69d49SMatthias Ringwald         group_last_packet_sequence = packet_sequence_number;
42924b69d49SMatthias Ringwald     }
43024b69d49SMatthias Ringwald 
43124b69d49SMatthias Ringwald     if (stream_last_packet_received[stream_index]) {
43224b69d49SMatthias Ringwald         printf_plc("ISO #%u, receive %u\n", stream_index, packet_sequence_number);
43324b69d49SMatthias Ringwald 
43424b69d49SMatthias Ringwald         int16_t packet_sequence_delta = btstack_time16_delta(packet_sequence_number,
43524b69d49SMatthias Ringwald                                                              stream_last_packet_sequence[stream_index]);
43624b69d49SMatthias Ringwald         if (packet_sequence_delta < 1) {
43724b69d49SMatthias Ringwald             // drop delayed packet that had already been generated by PLC
43824b69d49SMatthias Ringwald             printf_plc("- dropping delayed packet. Current sequence number %u, last received or generated by PLC: %u\n",
43924b69d49SMatthias Ringwald                        packet_sequence_number, stream_last_packet_sequence[stream_index]);
44024b69d49SMatthias Ringwald             return;
44124b69d49SMatthias Ringwald         }
44224b69d49SMatthias Ringwald         // simple check
44324b69d49SMatthias Ringwald         if (packet_sequence_number != stream_last_packet_sequence[stream_index] + 1) {
44424b69d49SMatthias Ringwald             printf_plc("- ISO #%u, missing %u\n", stream_index, stream_last_packet_sequence[stream_index] + 1);
44524b69d49SMatthias Ringwald         }
44624b69d49SMatthias Ringwald     } else {
44724b69d49SMatthias Ringwald         printf_plc("ISO %u, first packet seq number %u\n", stream_index, packet_sequence_number);
44824b69d49SMatthias Ringwald         stream_last_packet_received[stream_index] = true;
44924b69d49SMatthias Ringwald     }
45024b69d49SMatthias Ringwald 
45124b69d49SMatthias Ringwald     plc_check(packet_sequence_number);
45224b69d49SMatthias Ringwald 
453f4c19309SMatthias Ringwald     // either empty packets or num channels * num octets
454f4c19309SMatthias Ringwald     if ((iso_sdu_length != 0) && (iso_sdu_length != le_audio_demo_sink_num_channels_per_stream * le_audio_demo_sink_octets_per_frame)) {
455f4c19309SMatthias 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);
456f4c19309SMatthias 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);
457f4c19309SMatthias Ringwald         return;
458f4c19309SMatthias Ringwald     }
459f4c19309SMatthias Ringwald 
460f4c19309SMatthias Ringwald     if (iso_sdu_length == 0) {
461f4c19309SMatthias Ringwald         // empty packet -> generate silence
462f4c19309SMatthias Ringwald         memset(pcm, 0, sizeof(pcm));
46324b69d49SMatthias Ringwald         uint8_t i;
46424b69d49SMatthias Ringwald         for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++) {
465f4c19309SMatthias Ringwald             have_pcm[stream_index + i] = true;
466f4c19309SMatthias Ringwald         }
467f4c19309SMatthias Ringwald     } else {
468f4c19309SMatthias Ringwald         // regular packet -> decode codec frame
469f4c19309SMatthias Ringwald         uint8_t i;
470f4c19309SMatthias Ringwald         for (i = 0 ; i < le_audio_demo_sink_num_channels_per_stream ; i++){
47124b69d49SMatthias Ringwald             uint8_t tmp_BEC_detect;
47224b69d49SMatthias Ringwald             uint8_t BFI = 0;
47324b69d49SMatthias Ringwald             uint8_t effective_channel = stream_index + i;
47424b69d49SMatthias Ringwald             (void) lc3_decoder->decode_signed_16(decoder_contexts[effective_channel], &packet[offset], BFI,
47524b69d49SMatthias Ringwald                                                  &pcm[effective_channel], le_audio_demo_sink_num_channels,
47624b69d49SMatthias Ringwald                                                  &tmp_BEC_detect);
47724b69d49SMatthias Ringwald             offset += le_audio_demo_sink_octets_per_frame;
47824b69d49SMatthias Ringwald             have_pcm[stream_index + i] = true;
47924b69d49SMatthias Ringwald         }
480f4c19309SMatthias Ringwald     }
48124b69d49SMatthias Ringwald 
48224b69d49SMatthias Ringwald     store_samples_in_ringbuffer();
48324b69d49SMatthias Ringwald 
48424b69d49SMatthias Ringwald     log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (le_audio_demo_sink_num_channels * 2));
48524b69d49SMatthias Ringwald 
48624b69d49SMatthias Ringwald     le_audio_demo_sink_lc3_frames++;
48724b69d49SMatthias Ringwald 
48824b69d49SMatthias Ringwald     // PLC
48924b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
49079d6b605SMatthias Ringwald     btstack_run_loop_set_timer(&next_packet_timer, plc_timeout_initial_ms);
49124b69d49SMatthias Ringwald     btstack_run_loop_set_timer_handler(&next_packet_timer, plc_timeout);
49224b69d49SMatthias Ringwald     btstack_run_loop_add_timer(&next_packet_timer);
49324b69d49SMatthias Ringwald 
49424b69d49SMatthias Ringwald     if (samples_received >= le_audio_demo_sink_sampling_frequency_hz){
49524b69d49SMatthias 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);
49624b69d49SMatthias Ringwald         samples_received = 0;
49724b69d49SMatthias Ringwald         samples_dropped  =  0;
49824b69d49SMatthias Ringwald         samples_played = 0;
49924b69d49SMatthias Ringwald     }
50024b69d49SMatthias Ringwald 
50124b69d49SMatthias Ringwald     stream_last_packet_sequence[stream_index] = packet_sequence_number;
50224b69d49SMatthias Ringwald }
50324b69d49SMatthias Ringwald 
50424b69d49SMatthias Ringwald /**
50524b69d49SMatthias Ringwald  * @brief Close sink: close wav file, stop playback
50624b69d49SMatthias Ringwald  */
50724b69d49SMatthias Ringwald void le_audio_demo_util_sink_close(void){
50824b69d49SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
50924b69d49SMatthias Ringwald     printf("Close WAV file\n");
51024b69d49SMatthias Ringwald     wav_writer_close();
51124b69d49SMatthias Ringwald #endif
51224b69d49SMatthias Ringwald     // stop playback
51324b69d49SMatthias Ringwald     const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
51424b69d49SMatthias Ringwald     if (sink != NULL){
51524b69d49SMatthias Ringwald         sink->stop_stream();
51624b69d49SMatthias Ringwald     }
51724b69d49SMatthias Ringwald     // stop timer
51824b69d49SMatthias Ringwald     btstack_run_loop_remove_timer(&next_packet_timer);
51924b69d49SMatthias Ringwald }