110277393SMatthias Ringwald /* 210277393SMatthias Ringwald * Copyright (C) 2022 BlueKitchen GmbH 310277393SMatthias Ringwald * 410277393SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 510277393SMatthias Ringwald * modification, are permitted provided that the following conditions 610277393SMatthias Ringwald * are met: 710277393SMatthias Ringwald * 810277393SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 910277393SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1010277393SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1110277393SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1210277393SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1310277393SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1410277393SMatthias Ringwald * contributors may be used to endorse or promote products derived 1510277393SMatthias Ringwald * from this software without specific prior written permission. 1610277393SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1710277393SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1810277393SMatthias Ringwald * monetary gain. 1910277393SMatthias Ringwald * 2010277393SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2110277393SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2210277393SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2310277393SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2410277393SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2510277393SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2610277393SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2710277393SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2810277393SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2910277393SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3010277393SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3110277393SMatthias Ringwald * SUCH DAMAGE. 3210277393SMatthias Ringwald * 3310277393SMatthias Ringwald * Please inquire about commercial licensing options at 3410277393SMatthias Ringwald * [email protected] 3510277393SMatthias Ringwald * 3610277393SMatthias Ringwald */ 3710277393SMatthias Ringwald 3810277393SMatthias Ringwald #define BTSTACK_FILE__ "le_audio_broadcast_sink.c" 3910277393SMatthias Ringwald 4010277393SMatthias Ringwald /* 4110277393SMatthias Ringwald * LE Audio Broadcast Sink 4210277393SMatthias Ringwald */ 4310277393SMatthias Ringwald 4410277393SMatthias Ringwald 4510277393SMatthias Ringwald #include "btstack_config.h" 4610277393SMatthias Ringwald 4710277393SMatthias Ringwald #include <stdint.h> 4810277393SMatthias Ringwald #include <stdio.h> 4910277393SMatthias Ringwald #include <stdlib.h> 5010277393SMatthias Ringwald #include <string.h> 5110277393SMatthias Ringwald #include <unistd.h> 5210277393SMatthias Ringwald #include <inttypes.h> 5310277393SMatthias Ringwald #include <fcntl.h> // open 5410277393SMatthias Ringwald #include <errno.h> 5510277393SMatthias Ringwald 5610277393SMatthias Ringwald #include "ad_parser.h" 5710277393SMatthias Ringwald #include "bluetooth_data_types.h" 5810277393SMatthias Ringwald #include "bluetooth_gatt.h" 5910277393SMatthias Ringwald #include "btstack_debug.h" 6010277393SMatthias Ringwald #include "btstack_audio.h" 6110277393SMatthias Ringwald #include "btstack_event.h" 6210277393SMatthias Ringwald #include "btstack_run_loop.h" 6310277393SMatthias Ringwald #include "btstack_ring_buffer.h" 6410277393SMatthias Ringwald #include "btstack_stdin.h" 6510277393SMatthias Ringwald #include "btstack_util.h" 6610277393SMatthias Ringwald #include "gap.h" 6710277393SMatthias Ringwald #include "hci.h" 6810277393SMatthias Ringwald #include "hci_cmd.h" 692fd68da2SMatthias Ringwald #include "btstack_lc3.h" 70e40ee29aSMatthias Ringwald #include "btstack_lc3_google.h" 714a5b1c65SMatthias Ringwald 724a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 7310277393SMatthias Ringwald #include "wav_util.h" 744a5b1c65SMatthias Ringwald #endif 7510277393SMatthias Ringwald 7610277393SMatthias Ringwald // max config 7710277393SMatthias Ringwald #define MAX_NUM_BIS 2 7810277393SMatthias Ringwald #define MAX_SAMPLES_PER_FRAME 480 7910277393SMatthias Ringwald 802fe0253dSMatthias Ringwald #define DUMP_LEN_LC3_FRAMES 10000 8110277393SMatthias Ringwald 8210277393SMatthias Ringwald // playback 8310277393SMatthias Ringwald #define MAX_NUM_LC3_FRAMES 5 8410277393SMatthias Ringwald #define MAX_BYTES_PER_SAMPLE 4 8510277393SMatthias Ringwald #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE) 8610277393SMatthias Ringwald 8710277393SMatthias Ringwald // analysis 8810277393SMatthias Ringwald #define PACKET_PREFIX_LEN 10 8910277393SMatthias Ringwald 9010277393SMatthias Ringwald #define ANSI_COLOR_RED "\x1b[31m" 9110277393SMatthias Ringwald #define ANSI_COLOR_GREEN "\x1b[32m" 9210277393SMatthias Ringwald #define ANSI_COLOR_YELLOW "\x1b[33m" 9310277393SMatthias Ringwald #define ANSI_COLOR_BLUE "\x1b[34m" 9410277393SMatthias Ringwald #define ANSI_COLOR_MAGENTA "\x1b[35m" 9510277393SMatthias Ringwald #define ANSI_COLOR_CYAN "\x1b[36m" 9610277393SMatthias Ringwald #define ANSI_COLOR_RESET "\x1b[0m" 9710277393SMatthias Ringwald 9810277393SMatthias Ringwald static void show_usage(void); 9910277393SMatthias Ringwald 10010277393SMatthias Ringwald static const char * filename_lc3 = "le_audio_broadcast_sink.lc3"; 10110277393SMatthias Ringwald static const char * filename_wav = "le_audio_broadcast_sink.wav"; 10210277393SMatthias Ringwald 10310277393SMatthias Ringwald static enum { 10410277393SMatthias Ringwald APP_W4_WORKING, 10510277393SMatthias Ringwald APP_W4_BROADCAST_ADV, 10610277393SMatthias Ringwald APP_W4_PA_AND_BIG_INFO, 10710277393SMatthias Ringwald APP_W4_BIG_SYNC_ESTABLISHED, 10810277393SMatthias Ringwald APP_STREAMING, 10910277393SMatthias Ringwald APP_IDLE 11010277393SMatthias Ringwald } app_state = APP_W4_WORKING; 11110277393SMatthias Ringwald 11210277393SMatthias Ringwald // 11310277393SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 11410277393SMatthias Ringwald 11510277393SMatthias Ringwald static bool have_base; 11610277393SMatthias Ringwald static bool have_big_info; 11710277393SMatthias Ringwald 11810277393SMatthias Ringwald uint32_t last_samples_report_ms; 1194a5b1c65SMatthias Ringwald uint16_t samples_received; 1204a5b1c65SMatthias Ringwald uint16_t samples_dropped; 12110277393SMatthias Ringwald uint16_t frames_per_second[MAX_NUM_BIS]; 12210277393SMatthias Ringwald 12310277393SMatthias Ringwald // remote info 12410277393SMatthias Ringwald static char remote_name[20]; 12510277393SMatthias Ringwald static bd_addr_t remote; 12610277393SMatthias Ringwald static bd_addr_type_t remote_type; 12710277393SMatthias Ringwald static uint8_t remote_sid; 12810277393SMatthias Ringwald static bool count_mode; 12910277393SMatthias Ringwald static bool pts_mode; 1302fe0253dSMatthias Ringwald static bool nrf5340_audio_demo; 1312fe0253dSMatthias Ringwald 13210277393SMatthias Ringwald 13310277393SMatthias Ringwald // broadcast info 13410277393SMatthias Ringwald static const uint8_t big_handle = 1; 13510277393SMatthias Ringwald static hci_con_handle_t sync_handle; 13610277393SMatthias Ringwald static hci_con_handle_t bis_con_handles[MAX_NUM_BIS]; 13710277393SMatthias Ringwald static unsigned int next_bis_index; 13810277393SMatthias Ringwald 13910277393SMatthias Ringwald // analysis 14010277393SMatthias Ringwald static uint16_t last_packet_sequence[MAX_NUM_BIS]; 14110277393SMatthias Ringwald static uint32_t last_packet_time_ms[MAX_NUM_BIS]; 14210277393SMatthias Ringwald static uint8_t last_packet_prefix[MAX_NUM_BIS * PACKET_PREFIX_LEN]; 14310277393SMatthias Ringwald 1445c0d69beSMatthias Ringwald // BIG Sync 1455c0d69beSMatthias Ringwald static le_audio_big_sync_t big_sync_storage; 1465c0d69beSMatthias Ringwald static le_audio_big_sync_params_t big_sync_params; 1475c0d69beSMatthias Ringwald 14810277393SMatthias Ringwald // lc3 writer 14910277393SMatthias Ringwald static int dump_file; 15010277393SMatthias Ringwald static uint32_t lc3_frames; 15110277393SMatthias Ringwald 15210277393SMatthias Ringwald // lc3 codec config 1534a5b1c65SMatthias Ringwald static uint16_t sampling_frequency_hz; 1542fd68da2SMatthias Ringwald static btstack_lc3_frame_duration_t frame_duration; 15510277393SMatthias Ringwald static uint16_t number_samples_per_frame; 15610277393SMatthias Ringwald static uint16_t octets_per_frame; 15710277393SMatthias Ringwald static uint8_t num_bis; 15810277393SMatthias Ringwald 15910277393SMatthias Ringwald // lc3 decoder 1602fd68da2SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder; 161e40ee29aSMatthias Ringwald static btstack_lc3_decoder_google_t decoder_contexts[MAX_NUM_BIS]; 16210277393SMatthias Ringwald static int16_t pcm[MAX_NUM_BIS * MAX_SAMPLES_PER_FRAME]; 16310277393SMatthias Ringwald 16410277393SMatthias Ringwald // playback 16510277393SMatthias Ringwald static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE]; 16610277393SMatthias Ringwald static btstack_ring_buffer_t playback_buffer; 16710277393SMatthias Ringwald 168*22d64f35SMatthias Ringwald // PLC state 169*22d64f35SMatthias Ringwald #define PLC_GUARD_MS 1 170*22d64f35SMatthias Ringwald 171*22d64f35SMatthias Ringwald static btstack_timer_source_t next_packet_timer[MAX_NUM_BIS]; 172*22d64f35SMatthias Ringwald static uint16_t cached_iso_sdu_len; 173*22d64f35SMatthias Ringwald static bool have_pcm[MAX_NUM_BIS]; 174*22d64f35SMatthias Ringwald 17510277393SMatthias Ringwald static void le_audio_broadcast_sink_playback(int16_t * buffer, uint16_t num_samples){ 17610277393SMatthias Ringwald // called from lower-layer but guaranteed to be on main thread 17710277393SMatthias Ringwald uint32_t bytes_needed = num_samples * num_bis * 2; 17810277393SMatthias Ringwald 17910277393SMatthias Ringwald static bool underrun = true; 18010277393SMatthias Ringwald 18110277393SMatthias Ringwald log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / ( num_bis * 2)); 18210277393SMatthias Ringwald 18310277393SMatthias Ringwald if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)){ 18410277393SMatthias Ringwald memset(buffer, 0, bytes_needed); 18510277393SMatthias Ringwald if (underrun == false){ 18610277393SMatthias Ringwald log_info("Playback underrun"); 18710277393SMatthias Ringwald underrun = true; 18810277393SMatthias Ringwald } 18910277393SMatthias Ringwald return; 19010277393SMatthias Ringwald } 19110277393SMatthias Ringwald 19210277393SMatthias Ringwald if (underrun){ 19310277393SMatthias Ringwald underrun = false; 19410277393SMatthias Ringwald log_info("Playback started"); 19510277393SMatthias Ringwald } 19610277393SMatthias Ringwald uint32_t bytes_read; 19710277393SMatthias Ringwald btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read); 19810277393SMatthias Ringwald btstack_assert(bytes_read == bytes_needed); 19910277393SMatthias Ringwald } 20010277393SMatthias Ringwald 2014a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 20210277393SMatthias Ringwald static void open_lc3_file(void) { 20310277393SMatthias Ringwald // open lc3 file 20410277393SMatthias Ringwald int oflags = O_WRONLY | O_CREAT | O_TRUNC; 20510277393SMatthias Ringwald dump_file = open(filename_lc3, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 20610277393SMatthias Ringwald if (dump_file < 0) { 20710277393SMatthias Ringwald printf("failed to open file %s, errno = %d\n", filename_lc3, errno); 20810277393SMatthias Ringwald return; 20910277393SMatthias Ringwald } 21010277393SMatthias Ringwald 21110277393SMatthias Ringwald printf("LC3 binary file: %s\n", filename_lc3); 21210277393SMatthias Ringwald 21310277393SMatthias Ringwald // calc bps 2142fd68da2SMatthias Ringwald uint16_t frame_duration_100us = (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? 75 : 100; 21510277393SMatthias Ringwald uint32_t bits_per_second = (uint32_t) octets_per_frame * num_bis * 8 * 10000 / frame_duration_100us; 21610277393SMatthias Ringwald 21710277393SMatthias Ringwald // write header for floating point implementation 21810277393SMatthias Ringwald uint8_t header[18]; 21910277393SMatthias Ringwald little_endian_store_16(header, 0, 0xcc1c); 22010277393SMatthias Ringwald little_endian_store_16(header, 2, sizeof(header)); 22110277393SMatthias Ringwald little_endian_store_16(header, 4, sampling_frequency_hz / 100); 22210277393SMatthias Ringwald little_endian_store_16(header, 6, bits_per_second / 100); 22310277393SMatthias Ringwald little_endian_store_16(header, 8, num_bis); 22410277393SMatthias Ringwald little_endian_store_16(header, 10, frame_duration_100us * 10); 22510277393SMatthias Ringwald little_endian_store_16(header, 12, 0); 22610277393SMatthias Ringwald little_endian_store_32(header, 14, DUMP_LEN_LC3_FRAMES * number_samples_per_frame); 22710277393SMatthias Ringwald write(dump_file, header, sizeof(header)); 22810277393SMatthias Ringwald } 2294a5b1c65SMatthias Ringwald #endif 23010277393SMatthias Ringwald 23110277393SMatthias Ringwald static void setup_lc3_decoder(void){ 23210277393SMatthias Ringwald uint8_t channel; 23310277393SMatthias Ringwald for (channel = 0 ; channel < num_bis ; channel++){ 234e40ee29aSMatthias Ringwald btstack_lc3_decoder_google_t * decoder_context = &decoder_contexts[channel]; 235e40ee29aSMatthias Ringwald lc3_decoder = btstack_lc3_decoder_google_init_instance(decoder_context); 23610277393SMatthias Ringwald lc3_decoder->configure(decoder_context, sampling_frequency_hz, frame_duration); 23710277393SMatthias Ringwald } 23810277393SMatthias Ringwald number_samples_per_frame = lc3_decoder->get_number_samples_per_frame(&decoder_contexts[0]); 23910277393SMatthias Ringwald btstack_assert(number_samples_per_frame <= MAX_SAMPLES_PER_FRAME); 24010277393SMatthias Ringwald } 24110277393SMatthias Ringwald 24210277393SMatthias Ringwald static void close_files(void){ 2434a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 24410277393SMatthias Ringwald printf("Close files\n"); 24510277393SMatthias Ringwald close(dump_file); 24610277393SMatthias Ringwald wav_writer_close(); 2474a5b1c65SMatthias Ringwald #endif 24810277393SMatthias Ringwald } 24910277393SMatthias Ringwald 25010277393SMatthias Ringwald static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ 2512fe0253dSMatthias Ringwald // nRF534_audio quirk - no BASE in periodic advertisement 2522fe0253dSMatthias Ringwald if (nrf5340_audio_demo){ 2532fe0253dSMatthias Ringwald // hard coded config LC3 2542fe0253dSMatthias Ringwald // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame 2552fe0253dSMatthias Ringwald count_mode = 0; 2562fe0253dSMatthias Ringwald pts_mode = 0; 2572fe0253dSMatthias Ringwald num_bis = 1; 2582fe0253dSMatthias Ringwald sampling_frequency_hz = 48000; 2592fe0253dSMatthias Ringwald frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US; 2602fe0253dSMatthias Ringwald octets_per_frame = 120; 2612fe0253dSMatthias Ringwald have_base = true; 2622fe0253dSMatthias Ringwald return; 2632fe0253dSMatthias Ringwald } 2642fe0253dSMatthias Ringwald 26510277393SMatthias Ringwald // periodic advertisement contains the BASE 26610277393SMatthias Ringwald // TODO: BASE might be split across multiple advertisements 26710277393SMatthias Ringwald const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); 26810277393SMatthias Ringwald uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); 2698206d906SMatthias Ringwald uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); 2708206d906SMatthias Ringwald 2718206d906SMatthias Ringwald if (adv_status != 0) { 2728206d906SMatthias Ringwald printf("Periodic Advertisement (status %u): ", adv_status); 2738206d906SMatthias Ringwald printf_hexdump(adv_data, adv_size); 2748206d906SMatthias Ringwald return; 2758206d906SMatthias Ringwald } 27610277393SMatthias Ringwald 27710277393SMatthias Ringwald ad_context_t context; 27810277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 27910277393SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 2804a5b1c65SMatthias Ringwald // TODO: avoid out-of-bounds read 2814a5b1c65SMatthias Ringwald // uint8_t data_size = ad_iterator_get_data_len(&context); 28210277393SMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 28310277393SMatthias Ringwald uint16_t uuid; 28410277393SMatthias Ringwald switch (data_type){ 28510277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 28610277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 28710277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BASIC_AUDIO_ANNOUNCEMENT_SERVICE){ 28810277393SMatthias Ringwald have_base = true; 28910277393SMatthias Ringwald // Level 1: Group Level 29010277393SMatthias Ringwald const uint8_t * base_data = &data[2]; 2914a5b1c65SMatthias Ringwald // TODO: avoid out-of-bounds read 2924a5b1c65SMatthias Ringwald // uint16_t base_len = data_size - 2; 29310277393SMatthias Ringwald printf("BASE:\n"); 29410277393SMatthias Ringwald uint32_t presentation_delay = little_endian_read_24(base_data, 0); 29510277393SMatthias Ringwald printf("- presentation delay: %"PRIu32" us\n", presentation_delay); 29610277393SMatthias Ringwald uint8_t num_subgroups = base_data[3]; 29710277393SMatthias Ringwald printf("- num subgroups: %u\n", num_subgroups); 29810277393SMatthias Ringwald uint8_t i; 29910277393SMatthias Ringwald uint16_t offset = 4; 30010277393SMatthias Ringwald for (i=0;i<num_subgroups;i++){ 30110277393SMatthias Ringwald // Level 2: Subgroup Level 30210277393SMatthias Ringwald num_bis = base_data[offset++]; 30310277393SMatthias Ringwald printf(" - num bis[%u]: %u\n", i, num_bis); 30410277393SMatthias Ringwald // codec_id: coding format = 0x06, vendor and coded id = 0 30510277393SMatthias Ringwald offset += 5; 30610277393SMatthias Ringwald uint8_t codec_specific_configuration_length = base_data[offset++]; 30710277393SMatthias Ringwald const uint8_t * codec_specific_configuration = &base_data[offset]; 30810277393SMatthias Ringwald printf(" - codec specific config[%u]: ", i); 30910277393SMatthias Ringwald printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); 31010277393SMatthias Ringwald // parse config to get sampling frequency and frame duration 31110277393SMatthias Ringwald uint8_t codec_offset = 0; 31210277393SMatthias Ringwald while ((codec_offset + 1) < codec_specific_configuration_length){ 31310277393SMatthias Ringwald uint8_t ltv_len = codec_specific_configuration[codec_offset++]; 31410277393SMatthias Ringwald uint8_t ltv_type = codec_specific_configuration[codec_offset]; 31510277393SMatthias Ringwald const uint32_t sampling_frequency_map[] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000 }; 31610277393SMatthias Ringwald uint8_t sampling_frequency_index; 31710277393SMatthias Ringwald uint8_t frame_duration_index; 31810277393SMatthias Ringwald switch (ltv_type){ 31910277393SMatthias Ringwald case 0x01: // sampling frequency 32010277393SMatthias Ringwald sampling_frequency_index = codec_specific_configuration[codec_offset+1]; 32110277393SMatthias Ringwald // TODO: check range 32210277393SMatthias Ringwald sampling_frequency_hz = sampling_frequency_map[sampling_frequency_index - 1]; 3234a5b1c65SMatthias Ringwald printf(" - sampling frequency[%u]: %u\n", i, sampling_frequency_hz); 32410277393SMatthias Ringwald break; 32510277393SMatthias Ringwald case 0x02: // 0 = 7.5, 1 = 10 ms 32610277393SMatthias Ringwald frame_duration_index = codec_specific_configuration[codec_offset+1]; 3272fd68da2SMatthias Ringwald frame_duration = (frame_duration_index == 0) ? BTSTACK_LC3_FRAME_DURATION_7500US : BTSTACK_LC3_FRAME_DURATION_10000US; 3282fd68da2SMatthias Ringwald printf(" - frame duration[%u]: %s ms\n", i, (frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US) ? "7.5" : "10"); 32910277393SMatthias Ringwald break; 33010277393SMatthias Ringwald case 0x04: // octets per coding frame 33110277393SMatthias Ringwald octets_per_frame = little_endian_read_16(codec_specific_configuration, codec_offset+1); 33210277393SMatthias Ringwald printf(" - octets per codec frame[%u]: %u\n", i, octets_per_frame); 33310277393SMatthias Ringwald break; 33410277393SMatthias Ringwald default: 33510277393SMatthias Ringwald break; 33610277393SMatthias Ringwald } 33710277393SMatthias Ringwald codec_offset += ltv_len; 33810277393SMatthias Ringwald } 33910277393SMatthias Ringwald // 34010277393SMatthias Ringwald offset += codec_specific_configuration_length; 34110277393SMatthias Ringwald uint8_t metadata_length = base_data[offset++]; 34210277393SMatthias Ringwald const uint8_t * meta_data = &base_data[offset]; 34310277393SMatthias Ringwald offset += metadata_length; 34410277393SMatthias Ringwald printf(" - meta data[%u]: ", i); 34510277393SMatthias Ringwald printf_hexdump(meta_data, metadata_length); 34610277393SMatthias Ringwald uint8_t k; 34710277393SMatthias Ringwald for (k=0;k<num_bis;k++){ 34810277393SMatthias Ringwald // Level 3: BIS Level 34910277393SMatthias Ringwald uint8_t bis_index = base_data[offset++]; 35010277393SMatthias Ringwald printf(" - bis index[%u][%u]: %u\n", i, k, bis_index); 35110277393SMatthias Ringwald uint8_t codec_specific_configuration_length2 = base_data[offset++]; 35210277393SMatthias Ringwald const uint8_t * codec_specific_configuration2 = &base_data[offset]; 35310277393SMatthias Ringwald printf(" - codec specific config[%u][%u]: ", i, k); 35410277393SMatthias Ringwald printf_hexdump(codec_specific_configuration2, codec_specific_configuration_length2); 35510277393SMatthias Ringwald offset += codec_specific_configuration_length2; 35610277393SMatthias Ringwald } 35710277393SMatthias Ringwald } 35810277393SMatthias Ringwald } 35910277393SMatthias Ringwald break; 36010277393SMatthias Ringwald default: 36110277393SMatthias Ringwald break; 36210277393SMatthias Ringwald } 36310277393SMatthias Ringwald } 36410277393SMatthias Ringwald } 36510277393SMatthias Ringwald 36610277393SMatthias Ringwald static void handle_big_info(const uint8_t * packet, uint16_t size){ 36710277393SMatthias Ringwald printf("BIG Info advertising report\n"); 36810277393SMatthias Ringwald sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); 36910277393SMatthias Ringwald have_big_info = true; 37010277393SMatthias Ringwald } 37110277393SMatthias Ringwald 37210277393SMatthias Ringwald static void enter_create_big_sync(void){ 37310277393SMatthias Ringwald // stop scanning 37410277393SMatthias Ringwald gap_stop_scan(); 37510277393SMatthias Ringwald 37610277393SMatthias Ringwald // init decoder 37710277393SMatthias Ringwald setup_lc3_decoder(); 37810277393SMatthias Ringwald 37910277393SMatthias Ringwald printf("Configure: %u channels, sampling rate %u, samples per frame %u\n", num_bis, sampling_frequency_hz, number_samples_per_frame); 38010277393SMatthias Ringwald 3814a5b1c65SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 38210277393SMatthias Ringwald // create lc3 file 38310277393SMatthias Ringwald open_lc3_file(); 38410277393SMatthias Ringwald 38510277393SMatthias Ringwald // create wav file 38610277393SMatthias Ringwald printf("WAV file: %s\n", filename_wav); 38710277393SMatthias Ringwald wav_writer_open(filename_wav, num_bis, sampling_frequency_hz); 3884a5b1c65SMatthias Ringwald #endif 38910277393SMatthias Ringwald 39010277393SMatthias Ringwald // init playback buffer 39110277393SMatthias Ringwald btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE); 39210277393SMatthias Ringwald 39310277393SMatthias Ringwald // start playback 39410277393SMatthias Ringwald // PTS 8.2 sends stereo at half speed for stereo, for now playback at half speed 39510277393SMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 39610277393SMatthias Ringwald if (sink != NULL){ 3974a5b1c65SMatthias Ringwald uint16_t playback_speed; 39810277393SMatthias Ringwald if ((num_bis > 1) && pts_mode){ 39910277393SMatthias Ringwald playback_speed = sampling_frequency_hz / num_bis; 40010277393SMatthias Ringwald printf("PTS workaround: playback at %u hz\n", playback_speed); 40110277393SMatthias Ringwald } else { 40210277393SMatthias Ringwald playback_speed = sampling_frequency_hz; 40310277393SMatthias Ringwald }; 40410277393SMatthias Ringwald sink->init(num_bis, sampling_frequency_hz, le_audio_broadcast_sink_playback); 40510277393SMatthias Ringwald sink->start_stream(); 40610277393SMatthias Ringwald } 40710277393SMatthias Ringwald 4085c0d69beSMatthias Ringwald big_sync_params.big_handle = big_handle; 4095c0d69beSMatthias Ringwald big_sync_params.sync_handle = sync_handle; 4105c0d69beSMatthias Ringwald big_sync_params.encryption = 0; 4115c0d69beSMatthias Ringwald memset(big_sync_params.broadcast_code, 0, 16); 4125c0d69beSMatthias Ringwald big_sync_params.mse = 0; 4135c0d69beSMatthias Ringwald big_sync_params.big_sync_timeout_10ms = 100; 4145c0d69beSMatthias Ringwald big_sync_params.num_bis = num_bis; 4155c0d69beSMatthias Ringwald uint8_t i; 4165c0d69beSMatthias Ringwald printf("BIG Create Sync for BIS: "); 4175c0d69beSMatthias Ringwald for (i=0;i<num_bis;i++){ 4185c0d69beSMatthias Ringwald big_sync_params.bis_indices[i] = i + 1; 4195c0d69beSMatthias Ringwald printf("%u ", big_sync_params.bis_indices[i]); 4205c0d69beSMatthias Ringwald } 4215c0d69beSMatthias Ringwald printf("\n"); 4225c0d69beSMatthias Ringwald app_state = APP_W4_BIG_SYNC_ESTABLISHED; 4235c0d69beSMatthias Ringwald gap_big_sync_create(&big_sync_storage, &big_sync_params); 42410277393SMatthias Ringwald } 42510277393SMatthias Ringwald 426111967cbSMatthias Ringwald static void start_scanning() { 427111967cbSMatthias Ringwald app_state = APP_W4_BROADCAST_ADV; 428111967cbSMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 0); 429111967cbSMatthias Ringwald gap_start_scan(); 430111967cbSMatthias Ringwald printf("Start scan..\n"); 431111967cbSMatthias Ringwald } 432111967cbSMatthias Ringwald 43310277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 43410277393SMatthias Ringwald UNUSED(channel); 43510277393SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 43610277393SMatthias Ringwald switch (packet[0]) { 43710277393SMatthias Ringwald case BTSTACK_EVENT_STATE: 43810277393SMatthias Ringwald switch(btstack_event_state_get_state(packet)) { 43910277393SMatthias Ringwald case HCI_STATE_WORKING: 44010277393SMatthias Ringwald if (app_state != APP_W4_WORKING) break; 441111967cbSMatthias Ringwald start_scanning(); 44210277393SMatthias Ringwald break; 44310277393SMatthias Ringwald case HCI_STATE_OFF: 44410277393SMatthias Ringwald printf("Goodbye\n"); 44510277393SMatthias Ringwald exit(0); 44610277393SMatthias Ringwald break; 44710277393SMatthias Ringwald default: 44810277393SMatthias Ringwald break; 44910277393SMatthias Ringwald } 45010277393SMatthias Ringwald break; 45110277393SMatthias Ringwald case GAP_EVENT_EXTENDED_ADVERTISING_REPORT: 45210277393SMatthias Ringwald { 45310277393SMatthias Ringwald if (app_state != APP_W4_BROADCAST_ADV) break; 45410277393SMatthias Ringwald 45510277393SMatthias Ringwald gap_event_extended_advertising_report_get_address(packet, remote); 45610277393SMatthias Ringwald uint8_t adv_size = gap_event_extended_advertising_report_get_data_length(packet); 45710277393SMatthias Ringwald const uint8_t * adv_data = gap_event_extended_advertising_report_get_data(packet); 45810277393SMatthias Ringwald 45910277393SMatthias Ringwald ad_context_t context; 46010277393SMatthias Ringwald bool found = false; 46110277393SMatthias Ringwald remote_name[0] = '\0'; 46210277393SMatthias Ringwald uint16_t uuid; 46310277393SMatthias Ringwald for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 46410277393SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 46510277393SMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 46610277393SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 46710277393SMatthias Ringwald switch (data_type){ 46810277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID: 46910277393SMatthias Ringwald uuid = little_endian_read_16(data, 0); 47010277393SMatthias Ringwald if (uuid == ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_ANNOUNCEMENT_SERVICE){ 47110277393SMatthias Ringwald found = true; 47210277393SMatthias Ringwald } 47310277393SMatthias Ringwald break; 47410277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 47510277393SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 47610277393SMatthias Ringwald size = btstack_min(sizeof(remote_name) - 1, size); 47710277393SMatthias Ringwald memcpy(remote_name, data, size); 47810277393SMatthias Ringwald remote_name[size] = 0; 4792fe0253dSMatthias Ringwald // support for nRF5340 Audio DK 4802fe0253dSMatthias Ringwald if (strncmp("NRF5340", remote_name, 7) == 0){ 4812fe0253dSMatthias Ringwald nrf5340_audio_demo = true; 4822fe0253dSMatthias Ringwald found = true; 4832fe0253dSMatthias Ringwald } 48410277393SMatthias Ringwald break; 48510277393SMatthias Ringwald default: 48610277393SMatthias Ringwald break; 48710277393SMatthias Ringwald } 48810277393SMatthias Ringwald } 48910277393SMatthias Ringwald if (!found) break; 49010277393SMatthias Ringwald remote_type = gap_event_extended_advertising_report_get_address_type(packet); 49110277393SMatthias Ringwald remote_sid = gap_event_extended_advertising_report_get_advertising_sid(packet); 49210277393SMatthias Ringwald pts_mode = strncmp("PTS-", remote_name, 4) == 0; 49310277393SMatthias Ringwald count_mode = strncmp("COUNT", remote_name, 5) == 0; 49410277393SMatthias Ringwald printf("Remote Broadcast sink found, addr %s, name: '%s' (pts-mode: %u, count: %u)\n", bd_addr_to_str(remote), remote_name, pts_mode, count_mode); 49510277393SMatthias Ringwald // ignore other advertisements 49610277393SMatthias Ringwald gap_whitelist_add(remote_type, remote); 49710277393SMatthias Ringwald gap_set_scan_params(1, 0x30, 0x30, 1); 49810277393SMatthias Ringwald // sync to PA 49910277393SMatthias Ringwald gap_periodic_advertiser_list_clear(); 50010277393SMatthias Ringwald gap_periodic_advertiser_list_add(remote_type, remote, remote_sid); 50110277393SMatthias Ringwald app_state = APP_W4_PA_AND_BIG_INFO; 50210277393SMatthias Ringwald printf("Start Periodic Advertising Sync\n"); 50310277393SMatthias Ringwald gap_periodic_advertising_create_sync(0x01, remote_sid, remote_type, remote, 0, 1000, 0); 50410277393SMatthias Ringwald break; 50510277393SMatthias Ringwald } 50610277393SMatthias Ringwald 50710277393SMatthias Ringwald case HCI_EVENT_LE_META: 50810277393SMatthias Ringwald switch(hci_event_le_meta_get_subevent_code(packet)) { 50910277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 51010277393SMatthias Ringwald printf("Periodic advertising sync established\n"); 51110277393SMatthias Ringwald break; 51210277393SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT: 51310277393SMatthias Ringwald if (have_base) break; 51410277393SMatthias Ringwald handle_periodic_advertisement(packet, size); 5158206d906SMatthias Ringwald if (have_base & have_big_info){ 51610277393SMatthias Ringwald enter_create_big_sync(); 51710277393SMatthias Ringwald } 51810277393SMatthias Ringwald break; 51910277393SMatthias Ringwald case HCI_SUBEVENT_LE_BIGINFO_ADVERTISING_REPORT: 52010277393SMatthias Ringwald if (have_big_info) break; 52110277393SMatthias Ringwald handle_big_info(packet, size); 5228206d906SMatthias Ringwald if (have_base & have_big_info){ 52310277393SMatthias Ringwald enter_create_big_sync(); 52410277393SMatthias Ringwald } 52510277393SMatthias Ringwald break; 526111967cbSMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 527111967cbSMatthias Ringwald printf("BIG Sync Lost\n"); 528111967cbSMatthias Ringwald { 529111967cbSMatthias Ringwald const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance(); 530111967cbSMatthias Ringwald if (sink != NULL) { 531111967cbSMatthias Ringwald sink->stop_stream(); 532111967cbSMatthias Ringwald sink->close(); 533111967cbSMatthias Ringwald } 534111967cbSMatthias Ringwald } 535111967cbSMatthias Ringwald // start over 536111967cbSMatthias Ringwald start_scanning(); 537111967cbSMatthias Ringwald break; 53810277393SMatthias Ringwald default: 53910277393SMatthias Ringwald break; 54010277393SMatthias Ringwald } 54110277393SMatthias Ringwald break; 5425c0d69beSMatthias Ringwald case HCI_EVENT_META_GAP: 5435c0d69beSMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)){ 5445c0d69beSMatthias Ringwald case GAP_SUBEVENT_BIG_SYNC_CREATED: { 5455c0d69beSMatthias Ringwald printf("BIG Sync created with BIS Connection handles: "); 5465c0d69beSMatthias Ringwald uint8_t i; 54710277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 5485c0d69beSMatthias Ringwald bis_con_handles[i] = gap_subevent_big_sync_created_get_bis_con_handles(packet, i); 5495c0d69beSMatthias Ringwald printf("0x%04x ", bis_con_handles[i]); 55010277393SMatthias Ringwald } 55110277393SMatthias Ringwald app_state = APP_STREAMING; 55210277393SMatthias Ringwald last_samples_report_ms = btstack_run_loop_get_time_ms(); 5535c0d69beSMatthias Ringwald printf("Start streaming\n"); 55410277393SMatthias Ringwald break; 5555c0d69beSMatthias Ringwald } 5565c0d69beSMatthias Ringwald default: 5575c0d69beSMatthias Ringwald break; 5585c0d69beSMatthias Ringwald } 55910277393SMatthias Ringwald break; 56010277393SMatthias Ringwald default: 56110277393SMatthias Ringwald break; 56210277393SMatthias Ringwald } 56310277393SMatthias Ringwald } 56410277393SMatthias Ringwald 565fc31f34cSMatthias Ringwald static void store_samples_in_ringbuffer(void){ 566*22d64f35SMatthias Ringwald // check if we have all channels 567*22d64f35SMatthias Ringwald uint8_t bis_channel; 568*22d64f35SMatthias Ringwald for (bis_channel = 0; bis_channel < num_bis ; bis_channel++){ 569*22d64f35SMatthias Ringwald if (have_pcm[bis_channel] == false) return; 570*22d64f35SMatthias Ringwald } 571fc31f34cSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 572fc31f34cSMatthias Ringwald // write wav samples 573fc31f34cSMatthias Ringwald wav_writer_write_int16(num_bis * number_samples_per_frame, pcm); 574fc31f34cSMatthias Ringwald #endif 575fc31f34cSMatthias Ringwald // store samples in playback buffer 576fc31f34cSMatthias Ringwald uint32_t bytes_to_store = num_bis * number_samples_per_frame * 2; 577fc31f34cSMatthias Ringwald samples_received += number_samples_per_frame; 578fc31f34cSMatthias Ringwald if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) { 579fc31f34cSMatthias Ringwald btstack_ring_buffer_write(&playback_buffer, (uint8_t *) pcm, bytes_to_store); 580fc31f34cSMatthias Ringwald } else { 581fc31f34cSMatthias Ringwald printf("Samples dropped\n"); 582fc31f34cSMatthias Ringwald samples_dropped += number_samples_per_frame; 583fc31f34cSMatthias Ringwald } 584*22d64f35SMatthias Ringwald // reset 585*22d64f35SMatthias Ringwald for (bis_channel = 0; bis_channel < num_bis ; bis_channel++){ 586*22d64f35SMatthias Ringwald have_pcm[bis_channel] = false; 587*22d64f35SMatthias Ringwald } 588*22d64f35SMatthias Ringwald } 589*22d64f35SMatthias Ringwald 590*22d64f35SMatthias Ringwald 591*22d64f35SMatthias Ringwald static void plc_timeout(btstack_timer_source_t * timer) { 592*22d64f35SMatthias Ringwald 593*22d64f35SMatthias Ringwald uint8_t bis_channel = (uint8_t) (uintptr_t) btstack_run_loop_get_timer_context(timer); 594*22d64f35SMatthias Ringwald log_info("PLC channel %u", bis_channel); 595*22d64f35SMatthias Ringwald 596*22d64f35SMatthias Ringwald // set timer again 597*22d64f35SMatthias Ringwald uint32_t frame_duration_ms = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 8 : 10; 598*22d64f35SMatthias Ringwald btstack_run_loop_set_timer(&next_packet_timer[bis_channel], frame_duration_ms); 599*22d64f35SMatthias Ringwald btstack_run_loop_set_timer_handler(&next_packet_timer[bis_channel], plc_timeout); 600*22d64f35SMatthias Ringwald btstack_run_loop_add_timer(&next_packet_timer[bis_channel]); 601*22d64f35SMatthias Ringwald 602*22d64f35SMatthias Ringwald // inject packet 603*22d64f35SMatthias Ringwald uint8_t tmp_BEC_detect; 604*22d64f35SMatthias Ringwald uint8_t BFI = 1; 605*22d64f35SMatthias Ringwald (void) lc3_decoder->decode_signed_16(&decoder_contexts[bis_channel], NULL, cached_iso_sdu_len, BFI, 606*22d64f35SMatthias Ringwald &pcm[bis_channel], num_bis, 607*22d64f35SMatthias Ringwald &tmp_BEC_detect); 608*22d64f35SMatthias Ringwald have_pcm[bis_channel] = true; 609*22d64f35SMatthias Ringwald store_samples_in_ringbuffer(); 610fc31f34cSMatthias Ringwald } 611fc31f34cSMatthias Ringwald 61210277393SMatthias Ringwald static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 61310277393SMatthias Ringwald 61410277393SMatthias Ringwald uint16_t header = little_endian_read_16(packet, 0); 61510277393SMatthias Ringwald hci_con_handle_t con_handle = header & 0x0fff; 61610277393SMatthias Ringwald uint8_t pb_flag = (header >> 12) & 3; 61710277393SMatthias Ringwald uint8_t ts_flag = (header >> 14) & 1; 61810277393SMatthias Ringwald uint16_t iso_load_len = little_endian_read_16(packet, 2); 61910277393SMatthias Ringwald 62010277393SMatthias Ringwald uint16_t offset = 4; 62110277393SMatthias Ringwald uint32_t time_stamp = 0; 62210277393SMatthias Ringwald if (ts_flag){ 62310277393SMatthias Ringwald uint32_t time_stamp = little_endian_read_32(packet, offset); 62410277393SMatthias Ringwald offset += 4; 62510277393SMatthias Ringwald } 62610277393SMatthias Ringwald 627*22d64f35SMatthias Ringwald uint32_t receive_time_ms = btstack_run_loop_get_time_ms(); 628*22d64f35SMatthias Ringwald 62910277393SMatthias Ringwald uint16_t packet_sequence_number = little_endian_read_16(packet, offset); 63010277393SMatthias Ringwald offset += 2; 63110277393SMatthias Ringwald 63210277393SMatthias Ringwald uint16_t header_2 = little_endian_read_16(packet, offset); 63310277393SMatthias Ringwald uint16_t iso_sdu_length = header_2 & 0x3fff; 63410277393SMatthias Ringwald uint8_t packet_status_flag = (uint8_t) (header_2 >> 14); 63510277393SMatthias Ringwald offset += 2; 63610277393SMatthias Ringwald 63710277393SMatthias Ringwald if (iso_sdu_length == 0) return; 63810277393SMatthias Ringwald 63910277393SMatthias Ringwald // infer channel from con handle - only works for up to 2 channels 64010277393SMatthias Ringwald uint8_t bis_channel = (con_handle == bis_con_handles[0]) ? 0 : 1; 64110277393SMatthias Ringwald 64210277393SMatthias Ringwald if (count_mode){ 64310277393SMatthias Ringwald // check for missing packet 64410277393SMatthias Ringwald uint16_t last_seq_no = last_packet_sequence[bis_channel]; 64510277393SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 64610277393SMatthias Ringwald bool packet_missed = (last_seq_no != 0) && ((last_seq_no + 1) != packet_sequence_number); 64710277393SMatthias Ringwald if (packet_missed){ 64810277393SMatthias Ringwald // print last packet 64910277393SMatthias Ringwald printf("\n"); 6504a5b1c65SMatthias Ringwald printf("%04x %10"PRIu32" %u ", last_seq_no, last_packet_time_ms[bis_channel], bis_channel); 65110277393SMatthias Ringwald printf_hexdump(&last_packet_prefix[num_bis*PACKET_PREFIX_LEN], PACKET_PREFIX_LEN); 65210277393SMatthias Ringwald last_seq_no++; 65310277393SMatthias Ringwald 65410277393SMatthias Ringwald printf(ANSI_COLOR_RED); 65510277393SMatthias Ringwald while (last_seq_no < packet_sequence_number){ 65610277393SMatthias Ringwald printf("%04x %u MISSING\n", last_seq_no, bis_channel); 65710277393SMatthias Ringwald last_seq_no++; 65810277393SMatthias Ringwald } 65910277393SMatthias Ringwald printf(ANSI_COLOR_RESET); 66010277393SMatthias Ringwald 66110277393SMatthias Ringwald // print current packet 6624a5b1c65SMatthias Ringwald printf("%04x %10"PRIu32" %u ", packet_sequence_number, now, bis_channel); 66310277393SMatthias Ringwald printf_hexdump(&packet[offset], PACKET_PREFIX_LEN); 66410277393SMatthias Ringwald } 66510277393SMatthias Ringwald 66610277393SMatthias Ringwald // cache current packet 66710277393SMatthias Ringwald last_packet_time_ms[bis_channel] = now; 66810277393SMatthias Ringwald last_packet_sequence[bis_channel] = packet_sequence_number; 66910277393SMatthias Ringwald memcpy(&last_packet_prefix[num_bis*PACKET_PREFIX_LEN], &packet[offset], PACKET_PREFIX_LEN); 67010277393SMatthias Ringwald 67110277393SMatthias Ringwald } else { 67210277393SMatthias Ringwald 67310277393SMatthias Ringwald if ((packet_sequence_number & 0x7c) == 0) { 6744a5b1c65SMatthias Ringwald printf("%04x %10"PRIu32" %u ", packet_sequence_number, btstack_run_loop_get_time_ms(), bis_channel); 67510277393SMatthias Ringwald printf_hexdump(&packet[offset], iso_sdu_length); 67610277393SMatthias Ringwald } 67710277393SMatthias Ringwald 67810277393SMatthias Ringwald if (lc3_frames < DUMP_LEN_LC3_FRAMES) { 67910277393SMatthias Ringwald // store len header only for first bis 68010277393SMatthias Ringwald if (bis_channel == 0) { 68110277393SMatthias Ringwald uint8_t len_header[2]; 68210277393SMatthias Ringwald little_endian_store_16(len_header, 0, num_bis * iso_sdu_length); 68310277393SMatthias Ringwald write(dump_file, len_header, 2); 68410277393SMatthias Ringwald } 68510277393SMatthias Ringwald 68610277393SMatthias Ringwald // store single channel codec frame 68710277393SMatthias Ringwald write(dump_file, &packet[offset], iso_sdu_length); 68810277393SMatthias Ringwald } 68910277393SMatthias Ringwald 69010277393SMatthias Ringwald // decode codec frame 69110277393SMatthias Ringwald uint8_t tmp_BEC_detect; 69210277393SMatthias Ringwald uint8_t BFI = 0; 6939b947857SMatthias Ringwald (void) lc3_decoder->decode_signed_16(&decoder_contexts[bis_channel], &packet[offset], iso_sdu_length, BFI, 694c3e2434dSMatthias Ringwald &pcm[bis_channel], num_bis, 69510277393SMatthias Ringwald &tmp_BEC_detect); 696*22d64f35SMatthias Ringwald have_pcm[bis_channel] = true; 697fc31f34cSMatthias Ringwald store_samples_in_ringbuffer(); 69810277393SMatthias Ringwald 69910277393SMatthias Ringwald lc3_frames++; 70010277393SMatthias Ringwald frames_per_second[bis_channel]++; 70110277393SMatthias Ringwald 702*22d64f35SMatthias Ringwald // PLC 703*22d64f35SMatthias Ringwald cached_iso_sdu_len = iso_sdu_length; 704*22d64f35SMatthias Ringwald uint32_t frame_duration_ms = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 8 : 10; 705*22d64f35SMatthias Ringwald btstack_run_loop_remove_timer(&next_packet_timer[bis_channel]); 706*22d64f35SMatthias Ringwald btstack_run_loop_set_timer(&next_packet_timer[bis_channel], frame_duration_ms + PLC_GUARD_MS); 707*22d64f35SMatthias Ringwald btstack_run_loop_set_timer_context(&next_packet_timer[bis_channel], (void *) (uintptr_t) bis_channel); 708*22d64f35SMatthias Ringwald btstack_run_loop_set_timer_handler(&next_packet_timer[bis_channel], plc_timeout); 709*22d64f35SMatthias Ringwald btstack_run_loop_add_timer(&next_packet_timer[bis_channel]); 710*22d64f35SMatthias Ringwald 71110277393SMatthias Ringwald uint32_t time_ms = btstack_run_loop_get_time_ms(); 7122fe0253dSMatthias Ringwald if (btstack_time_delta(time_ms, last_samples_report_ms) >= 1000){ 71310277393SMatthias Ringwald last_samples_report_ms = time_ms; 7144a5b1c65SMatthias Ringwald printf("LC3 Frames: %4u - ", (int) (lc3_frames / num_bis)); 71510277393SMatthias Ringwald uint8_t i; 71610277393SMatthias Ringwald for (i=0;i<num_bis;i++){ 71710277393SMatthias Ringwald printf("%u ", frames_per_second[i]); 71810277393SMatthias Ringwald frames_per_second[i] = 0; 71910277393SMatthias Ringwald } 72010277393SMatthias Ringwald printf(" frames per second, dropped %u of %u\n", samples_dropped, samples_received); 72110277393SMatthias Ringwald samples_received = 0; 72210277393SMatthias Ringwald samples_dropped = 0; 72310277393SMatthias Ringwald } 72410277393SMatthias Ringwald 72510277393SMatthias Ringwald if (lc3_frames == DUMP_LEN_LC3_FRAMES){ 72610277393SMatthias Ringwald close_files(); 72710277393SMatthias Ringwald } 72810277393SMatthias Ringwald } 72910277393SMatthias Ringwald } 73010277393SMatthias Ringwald 73110277393SMatthias Ringwald static void show_usage(void){ 73210277393SMatthias Ringwald printf("\n--- LE Audio Broadcast Sink Test Console ---\n"); 73310277393SMatthias Ringwald printf("x - close files and exit\n"); 73410277393SMatthias Ringwald printf("---\n"); 73510277393SMatthias Ringwald } 73610277393SMatthias Ringwald 73710277393SMatthias Ringwald static void stdin_process(char c){ 73810277393SMatthias Ringwald switch (c){ 73910277393SMatthias Ringwald case 'x': 74010277393SMatthias Ringwald close_files(); 74110277393SMatthias Ringwald printf("Shutdown...\n"); 74210277393SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 74310277393SMatthias Ringwald break; 74410277393SMatthias Ringwald case '\n': 74510277393SMatthias Ringwald case '\r': 74610277393SMatthias Ringwald break; 74710277393SMatthias Ringwald default: 74810277393SMatthias Ringwald show_usage(); 74910277393SMatthias Ringwald break; 75010277393SMatthias Ringwald 75110277393SMatthias Ringwald } 75210277393SMatthias Ringwald } 75310277393SMatthias Ringwald 75410277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 75510277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 75610277393SMatthias Ringwald (void) argv; 75710277393SMatthias Ringwald (void) argc; 75810277393SMatthias Ringwald 75910277393SMatthias Ringwald // register for HCI events 76010277393SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 76110277393SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 76210277393SMatthias Ringwald 76310277393SMatthias Ringwald // register for ISO Packet 76410277393SMatthias Ringwald hci_register_iso_packet_handler(&iso_packet_handler); 76510277393SMatthias Ringwald 76610277393SMatthias Ringwald // turn on! 76710277393SMatthias Ringwald hci_power_control(HCI_POWER_ON); 76810277393SMatthias Ringwald 76910277393SMatthias Ringwald btstack_stdin_setup(stdin_process); 77010277393SMatthias Ringwald return 0; 77110277393SMatthias Ringwald } 772