/* * Copyright (C) 2022 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define BTSTACK_FILE__ "le_audio_broadcast_sink.c" /* * LE Audio Broadcast Sink */ #include "btstack_config.h" #include #include #include #include #include #include "ad_parser.h" #include "ble/att_server.h" #include "ble/sm.h" #include "bluetooth_data_types.h" #include "bluetooth_gatt.h" #include "btstack_debug.h" #include "btstack_audio.h" #include "btstack_event.h" #include "btstack_run_loop.h" #include "btstack_ring_buffer.h" #include "btstack_stdin.h" #include "btstack_util.h" #include "gap.h" #include "hci.h" #include "hci_cmd.h" #include "btstack_lc3.h" #include "btstack_lc3_google.h" #include "btstack_lc3plus_fraunhofer.h" #include "l2cap.h" #include "le-audio/le_audio_base_parser.h" #include "le-audio/gatt-service/broadcast_audio_scan_service_server.h" #include "le_audio_broadcast_sink.h" #include "le_audio_demo_util_sink.h" // max config #define MAX_NUM_BIS 2 #define MAX_SAMPLES_PER_FRAME 480 // playback #define MAX_NUM_LC3_FRAMES 5 #define MAX_BYTES_PER_SAMPLE 4 #define PLAYBACK_BUFFER_SIZE (MAX_NUM_LC3_FRAMES * MAX_SAMPLES_PER_FRAME * MAX_BYTES_PER_SAMPLE) static void show_usage(void); static const char * filename_wav = "le_audio_broadcast_sink.wav"; static enum { APP_W4_WORKING, APP_W4_BROADCAST_ADV, APP_W4_PA_AND_BIG_INFO, APP_W4_BROADCAST_CODE, APP_W4_BIG_SYNC_ESTABLISHED, APP_STREAMING, APP_IDLE } app_state = APP_W4_WORKING; static const uint8_t adv_sid = 0; static le_advertising_set_t le_advertising_set; static uint8_t adv_handle = 0; static le_extended_advertising_parameters_t extended_params = { .advertising_event_properties = 1, // connectable .primary_advertising_interval_min = 0x4b0, // 750 ms .primary_advertising_interval_max = 0x4b0, // 750 ms .primary_advertising_channel_map = 7, .own_address_type = BD_ADDR_TYPE_LE_PUBLIC, .peer_address_type = 0, .peer_address = { 0 }, .advertising_filter_policy = 0, .advertising_tx_power = 10, // 10 dBm .primary_advertising_phy = 1, // LE 1M PHY .secondary_advertising_max_skip = 0, .secondary_advertising_phy = 1, // LE 1M PHY .advertising_sid = adv_sid, .scan_request_notification_enable = 0, }; static const uint8_t extended_adv_data[] = { // 16 bit service data, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE, 3, BLUETOOTH_DATA_TYPE_SERVICE_DATA_16_BIT_UUID, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE & 0xff, ORG_BLUETOOTH_SERVICE_BROADCAST_AUDIO_SCAN_SERVICE >> 8, // name 5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'i', 'n', 'k' }; #define BASS_NUM_CLIENTS 1 #define BASS_NUM_SOURCES 1 static bass_source_data_t bass_source_new; static bass_server_source_t bass_sources[BASS_NUM_SOURCES]; static bass_server_connection_t bass_clients[BASS_NUM_CLIENTS]; // static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t sm_event_callback_registration; static bool have_base; static bool have_big_info; static bool have_broadcast_code; static bool standalone_mode; static uint32_t bis_sync_mask; uint16_t samples_received; uint16_t samples_dropped; // remote info static char remote_name[20]; static bd_addr_t remote; static bd_addr_type_t remote_type; static uint8_t remote_sid; static bool count_mode; static bool pts_mode; static bool nrf5340_audio_demo; // broadcast info static const uint8_t big_handle = 1; static hci_con_handle_t sync_handle; static hci_con_handle_t bis_con_handles[MAX_NUM_BIS]; static uint8_t encryption; static uint8_t broadcast_code[16]; static btstack_timer_source_t broadcast_sink_pa_sync_timer; // BIG Sync static le_audio_big_sync_t big_sync_storage; static le_audio_big_sync_params_t big_sync_params; // lc3 codec config static uint16_t sampling_frequency_hz; static btstack_lc3_frame_duration_t frame_duration; static uint16_t number_samples_per_frame; static uint16_t octets_per_frame; static uint8_t num_bis; static void handle_periodic_advertisement(const uint8_t * packet, uint16_t size){ // nRF534_audio quirk - no BASE in periodic advertisement if (nrf5340_audio_demo){ // hard coded config LC3 // default: mono bitrate 96000, 10 ms with USB audio source, 120 octets per frame count_mode = 0; pts_mode = 0; num_bis = 1; sampling_frequency_hz = 48000; frame_duration = BTSTACK_LC3_FRAME_DURATION_10000US; octets_per_frame = 120; have_base = true; return; } // periodic advertisement contains the BASE // TODO: BASE might be split across multiple advertisements const uint8_t * adv_data = hci_subevent_le_periodic_advertising_report_get_data(packet); uint16_t adv_size = hci_subevent_le_periodic_advertising_report_get_data_length(packet); uint8_t adv_status = hci_subevent_le_periodic_advertising_report_get_data_status(packet); if (adv_status != 0) { printf("Periodic Advertisement (status %u): ", adv_status); printf_hexdump(adv_data, adv_size); return; } le_audio_base_parser_t parser; bool ok = le_audio_base_parser_init(&parser, adv_data, adv_size); if (ok == false){ return; } have_base = true; printf("BASE:\n"); uint32_t presentation_delay = le_audio_base_parser_get_presentation_delay(&parser); printf("- presentation delay: %"PRIu32" us\n", presentation_delay); uint8_t num_subgroups = le_audio_base_parser_get_num_subgroups(&parser); // Cache in new source struct bass_source_new.subgroups_num = num_subgroups; printf("- num subgroups: %u\n", num_subgroups); uint8_t i; for (i=0;i 30)){ continue; } // collect bis sync mask bis_sync_mask |= 1 << (bis_index - 1); bass_source_new.subgroups[i].metadata_length = 0; memset(&bass_source_new.subgroups[i].metadata, 0, sizeof(le_audio_metadata_t)); printf(" - bis index[%u][%u]: %u\n", i, k, bis_index); codec_specific_configuration_length = le_audio_base_parser_bis_get_codec_specific_configuration_length(&parser); codec_specific_configuration = le_audio_base_bis_parser_get_codec_specific_configuration(&parser); printf(" - codec specific config[%u][%u]: ", i, k); printf_hexdump(codec_specific_configuration, codec_specific_configuration_length); // parse next BIS le_audio_base_parser_bis_next(&parser); } // parse next subgroup le_audio_base_parser_subgroup_next(&parser); } } static void handle_big_info(const uint8_t * packet, uint16_t size){ printf("BIG Info advertising report\n"); sync_handle = hci_subevent_le_biginfo_advertising_report_get_sync_handle(packet); encryption = hci_subevent_le_biginfo_advertising_report_get_encryption(packet); if (encryption) { printf("Stream is encrypted\n"); } have_big_info = true; } static void enter_create_big_sync(void){ // stop scanning gap_stop_scan(); // init sink uint16_t iso_interval_1250us = frame_duration == BTSTACK_LC3_FRAME_DURATION_7500US ? 6 : 8; uint8_t pre_transmission_offset = 1; le_audio_demo_util_sink_configure_broadcast(num_bis, 1, sampling_frequency_hz, frame_duration, octets_per_frame, iso_interval_1250us, pre_transmission_offset); big_sync_params.big_handle = big_handle; big_sync_params.sync_handle = sync_handle; big_sync_params.encryption = encryption; if (encryption) { memcpy(big_sync_params.broadcast_code, &broadcast_code[0], 16); } else { memset(big_sync_params.broadcast_code, 0, 16); } big_sync_params.mse = 0; big_sync_params.big_sync_timeout_10ms = 100; big_sync_params.num_bis = num_bis; uint8_t i; printf("BIG Create Sync for BIS: "); for (i=0;istop_stream(); sink->close(); } } // start over if (!standalone_mode) break; start_scanning(); break; default: break; } break; case HCI_EVENT_META_GAP: switch (hci_event_gap_meta_get_subevent_code(packet)){ case GAP_SUBEVENT_BIG_SYNC_CREATED: { printf("BIG Sync created with BIS Connection handles: "); uint8_t i; for (i=0;i LE_AUDIO_PA_SYNC_STATE_NO_PAST\n"); broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_NO_PAST); } static void bass_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ UNUSED(packet_type); UNUSED(channel); btstack_assert (packet_type == HCI_EVENT_PACKET); btstack_assert(hci_event_packet_get_type(packet) == HCI_EVENT_GATTSERVICE_META); printf("BASS Event 0x%02x: ", hci_event_gattservice_meta_get_subevent_code(packet)); printf_hexdump(packet, size); switch (hci_event_gattservice_meta_get_subevent_code(packet)){ case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_ADDED: printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_ADDED, source_id 0x%04x, pa_sync %u\n", gattservice_subevent_bass_server_source_added_get_source_id(packet), gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); switch (gattservice_subevent_bass_server_source_added_get_pa_sync(packet)){ case LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE: printf("LE_AUDIO_PA_SYNC_SYNCHRONIZE_TO_PA_PAST_AVAILABLE -> Request SyncInfo\n"); broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCINFO_REQUEST); // start timeout btstack_run_loop_set_timer_handler(&broadcast_sink_pa_sync_timer, broadcast_sync_pa_sync_timeout_handler); btstack_run_loop_set_timer(&broadcast_sink_pa_sync_timer, 10000); // 10 seconds btstack_run_loop_add_timer(&broadcast_sink_pa_sync_timer); break; } break; case GATTSERVICE_SUBEVENT_BASS_SERVER_SOURCE_MODIFIED: printf("GATTSERVICE_SUBEVENT_BASS_SOURCE_MODIFIED, source_id 0x%04x, pa_sync %u\n", gattservice_subevent_bass_server_source_added_get_source_id(packet), gattservice_subevent_bass_server_source_added_get_pa_sync(packet)); // handle 'bis sync == 0' printf("PA Sync %u, bis_sync[0] = %u\n", bass_sources[0].data.pa_sync, bass_sources[0].data.subgroups[0].bis_sync); if (bass_sources[0].data.subgroups[0].bis_sync == 0){ printf("Simulate BIS Sync has stopped\n"); bass_sources[0].data.subgroups[0].bis_sync_state = 0; broadcast_audio_scan_service_server_set_pa_sync_state(0, LE_AUDIO_PA_SYNC_STATE_SYNCHRONIZED_TO_PA); } break; case GATTSERVICE_SUBEVENT_BASS_SERVER_BROADCAST_CODE: gattservice_subevent_bass_server_broadcast_code_get_broadcast_code(packet, broadcast_code); printf("GATTSERVICE_SUBEVENT_BASS_BROADCAST_CODE received: "); printf_hexdump(broadcast_code, 16); bass_sources[0].big_encryption = LE_AUDIO_BIG_ENCRYPTION_DECRYPTING; if (have_base && have_big_info){ enter_create_big_sync(); } break; default: break; } } int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ (void) argv; (void) argc; l2cap_init(); sm_init(); // setup ATT server att_server_init(profile_data, NULL, NULL); // register for HCI events hci_event_callback_registration.callback = &packet_handler; hci_add_event_handler(&hci_event_callback_registration); // register for ISO Packet hci_register_iso_packet_handler(&iso_packet_handler); // register for SM events sm_event_callback_registration.callback = &packet_handler; sm_add_event_handler(&sm_event_callback_registration); // setup BASS Server broadcast_audio_scan_service_server_init(BASS_NUM_SOURCES, bass_sources, BASS_NUM_CLIENTS, bass_clients); broadcast_audio_scan_service_server_register_packet_handler(&bass_packet_handler); // setup advertising and allow to receive periodic advertising sync trasnfers setup_advertising(); gap_periodic_advertising_sync_transfer_set_default_parameters(2, 0, 0x2000, 0); // turn on! hci_power_control(HCI_POWER_ON); btstack_stdin_setup(stdin_process); return 0; }