mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-29 22:20:37 +00:00
test/le_audio: basic unicast sink/source
This commit is contained in:
parent
e90f848d38
commit
5c483cdc06
679
test/le_audio/le_audio_unicast_sink.c
Normal file
679
test/le_audio/le_audio_unicast_sink.c
Normal file
@ -0,0 +1,679 @@
|
||||
/*
|
||||
* 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 BLUEKITCHEN
|
||||
* GMBH 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_unicast_sink.c"
|
||||
|
||||
/*
|
||||
* LE Audio Unicast Sink
|
||||
* Until GATT Services are available, we encode LC3 config in advertising
|
||||
*/
|
||||
|
||||
|
||||
#include "btstack_config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include <fcntl.h> // open
|
||||
#include <errno.h>
|
||||
|
||||
#include "ad_parser.h"
|
||||
#include "bluetooth_data_types.h"
|
||||
#include "bluetooth_company_id.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 "lc3.h"
|
||||
#include "lc3_ehima.h"
|
||||
#include "wav_util.h"
|
||||
|
||||
// max config
|
||||
#define MAX_CHANNELS 2
|
||||
#define MAX_SAMPLES_PER_FRAME 480
|
||||
|
||||
#define DUMP_LEN_LC3_FRAMES 1000
|
||||
|
||||
// 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)
|
||||
|
||||
// analysis
|
||||
#define PACKET_PREFIX_LEN 10
|
||||
|
||||
#define ANSI_COLOR_RED "\x1b[31m"
|
||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
static void show_usage(void);
|
||||
|
||||
static const char * filename_lc3 = "le_audio_unicast_sink.lc3";
|
||||
static const char * filename_wav = "le_audio_unicast_sink.wav";
|
||||
|
||||
static enum {
|
||||
APP_W4_WORKING,
|
||||
APP_SET_HOST_FEATURES,
|
||||
APP_W4_SOURCE_ADV,
|
||||
APP_CREATE_CIG,
|
||||
APP_W4_CIG_COMPLETE,
|
||||
APP_CREATE_CIS,
|
||||
APP_W4_CIS_CREATED,
|
||||
APP_SET_ISO_PATHS,
|
||||
APP_STREAMING,
|
||||
APP_IDLE
|
||||
} app_state = APP_W4_WORKING;
|
||||
|
||||
//
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
|
||||
uint32_t last_samples_report_ms;
|
||||
uint32_t samples_received;
|
||||
uint32_t samples_dropped;
|
||||
uint16_t frames_per_second[MAX_CHANNELS];
|
||||
|
||||
// remote info
|
||||
static char remote_name[20];
|
||||
static bd_addr_t remote_addr;
|
||||
static bd_addr_type_t remote_type;
|
||||
static hci_con_handle_t remote_handle;
|
||||
|
||||
static bool count_mode;
|
||||
static bool pts_mode;
|
||||
|
||||
// iso info
|
||||
static bool framed_pdus;
|
||||
static uint16_t frame_duration_us;
|
||||
static uint8_t num_cis;
|
||||
static hci_con_handle_t cis_con_handles[MAX_CHANNELS];
|
||||
static bool cis_established[MAX_CHANNELS];
|
||||
static unsigned int next_cis_index;
|
||||
|
||||
// analysis
|
||||
static uint16_t last_packet_sequence[MAX_CHANNELS];
|
||||
static uint32_t last_packet_time_ms[MAX_CHANNELS];
|
||||
static uint8_t last_packet_prefix[MAX_CHANNELS * PACKET_PREFIX_LEN];
|
||||
|
||||
// lc3 writer
|
||||
static int dump_file;
|
||||
static uint32_t lc3_frames;
|
||||
|
||||
// lc3 codec config
|
||||
static uint32_t sampling_frequency_hz;
|
||||
static lc3_frame_duration_t frame_duration;
|
||||
static uint16_t number_samples_per_frame;
|
||||
static uint16_t octets_per_frame;
|
||||
static uint8_t num_channels;
|
||||
|
||||
// lc3 decoder
|
||||
static const lc3_decoder_t * lc3_decoder;
|
||||
static lc3_decoder_ehima_t decoder_contexts[MAX_CHANNELS];
|
||||
static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
|
||||
|
||||
// playback
|
||||
static uint8_t playback_buffer_storage[PLAYBACK_BUFFER_SIZE];
|
||||
static btstack_ring_buffer_t playback_buffer;
|
||||
|
||||
static void le_audio_connection_sink_playback(int16_t * buffer, uint16_t num_samples){
|
||||
// called from lower-layer but guaranteed to be on main thread
|
||||
uint32_t bytes_needed = num_samples * num_channels * 2;
|
||||
|
||||
static bool underrun = true;
|
||||
|
||||
log_info("Playback: need %u, have %u", num_samples, btstack_ring_buffer_bytes_available(&playback_buffer) / (num_channels * 2));
|
||||
|
||||
if (bytes_needed > btstack_ring_buffer_bytes_available(&playback_buffer)){
|
||||
memset(buffer, 0, bytes_needed);
|
||||
if (underrun == false){
|
||||
log_info("Playback underrun");
|
||||
underrun = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (underrun){
|
||||
underrun = false;
|
||||
log_info("Playback started");
|
||||
}
|
||||
uint32_t bytes_read;
|
||||
btstack_ring_buffer_read(&playback_buffer, (uint8_t *) buffer, bytes_needed, &bytes_read);
|
||||
btstack_assert(bytes_read == bytes_needed);
|
||||
}
|
||||
|
||||
static void open_lc3_file(void) {
|
||||
// open lc3 file
|
||||
int oflags = O_WRONLY | O_CREAT | O_TRUNC;
|
||||
dump_file = open(filename_lc3, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
if (dump_file < 0) {
|
||||
printf("failed to open file %s, errno = %d\n", filename_lc3, errno);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("LC3 binary file: %s\n", filename_lc3);
|
||||
|
||||
// calc bps
|
||||
uint16_t frame_duration_100us = (frame_duration == LC3_FRAME_DURATION_7500US) ? 75 : 100;
|
||||
uint32_t bits_per_second = (uint32_t) octets_per_frame * num_channels * 8 * 10000 / frame_duration_100us;
|
||||
|
||||
// write header for floating point implementation
|
||||
uint8_t header[18];
|
||||
little_endian_store_16(header, 0, 0xcc1c);
|
||||
little_endian_store_16(header, 2, sizeof(header));
|
||||
little_endian_store_16(header, 4, sampling_frequency_hz / 100);
|
||||
little_endian_store_16(header, 6, bits_per_second / 100);
|
||||
little_endian_store_16(header, 8, num_channels);
|
||||
little_endian_store_16(header, 10, frame_duration_100us * 10);
|
||||
little_endian_store_16(header, 12, 0);
|
||||
little_endian_store_32(header, 14, DUMP_LEN_LC3_FRAMES * number_samples_per_frame);
|
||||
write(dump_file, header, sizeof(header));
|
||||
}
|
||||
|
||||
static void setup_lc3_decoder(void){
|
||||
uint8_t channel;
|
||||
for (channel = 0 ; channel < num_channels ; channel++){
|
||||
lc3_decoder_ehima_t * decoder_context = &decoder_contexts[channel];
|
||||
lc3_decoder = lc3_decoder_ehima_init_instance(decoder_context);
|
||||
lc3_decoder->configure(decoder_context, sampling_frequency_hz, frame_duration);
|
||||
}
|
||||
number_samples_per_frame = lc3_decoder->get_number_samples_per_frame(&decoder_contexts[0]);
|
||||
btstack_assert(number_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
|
||||
}
|
||||
|
||||
static void close_files(void){
|
||||
printf("Close files\n");
|
||||
close(dump_file);
|
||||
wav_writer_close();
|
||||
}
|
||||
|
||||
static void enter_streaming(void){
|
||||
|
||||
// init decoder
|
||||
setup_lc3_decoder();
|
||||
|
||||
printf("Configure: %u channels, sampling rate %u, samples per frame %u\n", num_channels, sampling_frequency_hz, number_samples_per_frame);
|
||||
|
||||
// create lc3 file
|
||||
open_lc3_file();
|
||||
|
||||
// create wav file
|
||||
printf("WAV file: %s\n", filename_wav);
|
||||
wav_writer_open(filename_wav, num_channels, sampling_frequency_hz);
|
||||
|
||||
// init playback buffer
|
||||
btstack_ring_buffer_init(&playback_buffer, playback_buffer_storage, PLAYBACK_BUFFER_SIZE);
|
||||
|
||||
// start playback
|
||||
const btstack_audio_sink_t * sink = btstack_audio_sink_get_instance();
|
||||
if (sink != NULL){
|
||||
sink->init(num_channels, sampling_frequency_hz, le_audio_connection_sink_playback);
|
||||
sink->start_stream();
|
||||
}
|
||||
}
|
||||
|
||||
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(channel);
|
||||
bd_addr_t event_addr;
|
||||
if (packet_type != HCI_EVENT_PACKET) return;
|
||||
unsigned int i;
|
||||
switch (packet[0]) {
|
||||
case BTSTACK_EVENT_STATE:
|
||||
switch(btstack_event_state_get_state(packet)) {
|
||||
case HCI_STATE_WORKING:
|
||||
if (app_state != APP_W4_WORKING) break;
|
||||
app_state = APP_SET_HOST_FEATURES;
|
||||
break;
|
||||
case HCI_STATE_OFF:
|
||||
printf("Goodbye\n");
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_COMMAND_COMPLETE:
|
||||
switch (hci_event_command_complete_get_command_opcode(packet)) {
|
||||
case HCI_OPCODE_HCI_LE_SET_CIG_PARAMETERS:
|
||||
if (app_state == APP_W4_CIG_COMPLETE){
|
||||
uint8_t i;
|
||||
printf("CIS Connection Handles: ");
|
||||
for (i=0; i < num_cis; i++){
|
||||
cis_con_handles[i] = little_endian_read_16(packet, 8 + 2*i);
|
||||
printf("0x%04x ", cis_con_handles[i]);
|
||||
}
|
||||
printf("\n");
|
||||
next_cis_index = 0;
|
||||
app_state = APP_CREATE_CIS;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case GAP_EVENT_ADVERTISING_REPORT:
|
||||
{
|
||||
if (app_state != APP_W4_SOURCE_ADV) break;
|
||||
|
||||
gap_event_advertising_report_get_address(packet, remote_addr);
|
||||
uint8_t adv_size = gap_event_advertising_report_get_data_length(packet);
|
||||
const uint8_t * adv_data = gap_event_advertising_report_get_data(packet);
|
||||
|
||||
ad_context_t context;
|
||||
bool found = false;
|
||||
remote_name[0] = '\0';
|
||||
uint16_t uuid;
|
||||
uint16_t company_id;
|
||||
for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
|
||||
uint8_t data_type = ad_iterator_get_data_type(&context);
|
||||
uint8_t size = ad_iterator_get_data_len(&context);
|
||||
const uint8_t *data = ad_iterator_get_data(&context);
|
||||
switch (data_type){
|
||||
case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA:
|
||||
company_id = little_endian_read_16(data, 0);
|
||||
if (company_id == BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH){
|
||||
// subtype = 0 -> le audio unicast source
|
||||
uint8_t subtype = data[2];
|
||||
if (subtype != 0) break;
|
||||
// flags
|
||||
uint8_t flags = data[3];
|
||||
pts_mode = (flags & 1) != 0;
|
||||
count_mode = (flags & 2) != 0;
|
||||
// num channels
|
||||
num_channels = data[4];
|
||||
if (num_channels > 2) break;
|
||||
// sampling frequency
|
||||
sampling_frequency_hz = 1000 * data[5];
|
||||
// frame duration
|
||||
frame_duration = data[6] == 0 ? LC3_FRAME_DURATION_7500US : LC3_FRAME_DURATION_10000US;
|
||||
// octets per frame
|
||||
octets_per_frame = data[7];
|
||||
// done
|
||||
found = true;
|
||||
}
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
|
||||
case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
|
||||
size = btstack_min(sizeof(remote_name) - 1, size);
|
||||
memcpy(remote_name, data, size);
|
||||
remote_name[size] = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) break;
|
||||
remote_type = gap_event_advertising_report_get_address_type(packet);
|
||||
pts_mode = false;
|
||||
count_mode = false;
|
||||
printf("Remote Broadcast source found, addr %s, name: '%s' (pts-mode: %u, count: %u)\n", bd_addr_to_str(remote_addr), remote_name, pts_mode, count_mode);
|
||||
// stop scanning
|
||||
app_state = APP_W4_CIS_CREATED;
|
||||
gap_stop_scan();
|
||||
gap_connect(remote_addr, remote_type);
|
||||
break;
|
||||
}
|
||||
|
||||
case HCI_EVENT_LE_META:
|
||||
switch(hci_event_le_meta_get_subevent_code(packet)) {
|
||||
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
|
||||
app_state = APP_CREATE_CIG;
|
||||
enter_streaming();
|
||||
hci_subevent_le_connection_complete_get_peer_address(packet, event_addr);
|
||||
remote_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
|
||||
printf("Connected, remote %s, handle %04x\n", bd_addr_to_str(event_addr), remote_handle);
|
||||
break;
|
||||
case HCI_SUBEVENT_LE_CIS_ESTABLISHED:
|
||||
{
|
||||
// only look for cis handle
|
||||
uint8_t i;
|
||||
hci_con_handle_t cis_handle = hci_subevent_le_cis_established_get_connection_handle(packet);
|
||||
for (i=0; i < num_cis; i++){
|
||||
if (cis_handle == cis_con_handles[i]){
|
||||
cis_established[i] = true;
|
||||
}
|
||||
}
|
||||
// check for complete
|
||||
bool complete = true;
|
||||
for (i=0; i < num_cis; i++) {
|
||||
complete &= cis_established[i];
|
||||
}
|
||||
if (complete) {
|
||||
printf("All CIS Established\n");
|
||||
next_cis_index = 0;
|
||||
app_state = APP_SET_ISO_PATHS;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!hci_can_send_command_packet_now()) return;
|
||||
|
||||
switch(app_state){
|
||||
case APP_SET_HOST_FEATURES:
|
||||
hci_send_cmd(&hci_le_set_host_feature, 32, 1);
|
||||
|
||||
app_state = APP_W4_SOURCE_ADV;
|
||||
gap_set_scan_params(1, 0x30, 0x30, 0);
|
||||
gap_start_scan();
|
||||
printf("Start scan..\n");
|
||||
break;
|
||||
case APP_CREATE_CIG:
|
||||
{
|
||||
if (sampling_frequency_hz == 44100){
|
||||
framed_pdus = 1;
|
||||
// same config as for 48k -> frame is longer by 48/44.1
|
||||
frame_duration_us = frame_duration == LC3_FRAME_DURATION_7500US ? 8163 : 10884;
|
||||
} else {
|
||||
framed_pdus = 0;
|
||||
frame_duration_us = frame_duration == LC3_FRAME_DURATION_7500US ? 7500 : 10000;
|
||||
}
|
||||
printf("Send: LE Set CIG Parameters\n");
|
||||
|
||||
app_state = APP_W4_CIG_COMPLETE;
|
||||
|
||||
num_cis = 1;
|
||||
|
||||
uint8_t cig_id = 0;
|
||||
uint32_t sdu_interval_c_to_p = frame_duration_us;
|
||||
uint32_t sdu_interval_p_to_c = frame_duration_us;
|
||||
uint8_t worst_case_sca = 0; // 251 ppm to 500 ppm
|
||||
uint8_t packing = 0; // sequential
|
||||
uint8_t framing = framed_pdus; // unframed (44.1 khz requires framed)
|
||||
uint16_t max_transport_latency_c_to_p = 40;
|
||||
uint16_t max_transport_latency_p_to_c = 40;
|
||||
uint8_t cis_id[MAX_CHANNELS];
|
||||
uint16_t max_sdu_c_to_p[MAX_CHANNELS];
|
||||
uint16_t max_sdu_p_to_c[MAX_CHANNELS];
|
||||
uint8_t phy_c_to_p[MAX_CHANNELS];
|
||||
uint8_t phy_p_to_c[MAX_CHANNELS];
|
||||
uint8_t rtn_c_to_p[MAX_CHANNELS];
|
||||
uint8_t rtn_p_to_c[MAX_CHANNELS];
|
||||
uint8_t i;
|
||||
for (i=0; i < num_cis; i++){
|
||||
cis_id[i] = i;
|
||||
max_sdu_c_to_p[i] = 0;
|
||||
max_sdu_p_to_c[i] = num_channels * octets_per_frame;
|
||||
phy_c_to_p[i] = 2; // 2M
|
||||
phy_p_to_c[i] = 2; // 2M
|
||||
rtn_c_to_p[i] = 2;
|
||||
rtn_p_to_c[i] = 2;
|
||||
}
|
||||
hci_send_cmd(&hci_le_set_cig_parameters,
|
||||
cig_id,
|
||||
sdu_interval_c_to_p,
|
||||
sdu_interval_p_to_c,
|
||||
worst_case_sca,
|
||||
packing,
|
||||
framing,
|
||||
max_transport_latency_c_to_p,
|
||||
max_transport_latency_p_to_c,
|
||||
num_cis,
|
||||
cis_id,
|
||||
max_sdu_c_to_p,
|
||||
max_sdu_p_to_c,
|
||||
phy_c_to_p,
|
||||
phy_p_to_c,
|
||||
rtn_c_to_p,
|
||||
rtn_p_to_c
|
||||
);
|
||||
break;
|
||||
}
|
||||
case APP_CREATE_CIS:
|
||||
{
|
||||
printf("Create CIS\n");
|
||||
app_state = APP_W4_CIS_CREATED;
|
||||
hci_con_handle_t cis_connection_handle[MAX_CHANNELS];
|
||||
hci_con_handle_t acl_connection_handle[MAX_CHANNELS];
|
||||
uint16_t i;
|
||||
for (i=0; i < num_cis; i++){
|
||||
cis_connection_handle[i] = cis_con_handles[i];
|
||||
acl_connection_handle[i] = remote_handle;
|
||||
}
|
||||
hci_send_cmd(&hci_le_create_cis, num_cis, cis_connection_handle, acl_connection_handle);
|
||||
break;
|
||||
}
|
||||
case APP_SET_ISO_PATHS:
|
||||
hci_send_cmd(&hci_le_setup_iso_data_path, cis_con_handles[next_cis_index++], 1, 0, 0, 0, 0, 0, 0, NULL);
|
||||
if (next_cis_index == num_cis){
|
||||
app_state = APP_STREAMING;
|
||||
last_samples_report_ms = btstack_run_loop_get_time_ms();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void iso_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
|
||||
uint16_t header = little_endian_read_16(packet, 0);
|
||||
hci_con_handle_t con_handle = header & 0x0fff;
|
||||
uint8_t pb_flag = (header >> 12) & 3;
|
||||
uint8_t ts_flag = (header >> 14) & 1;
|
||||
uint16_t iso_load_len = little_endian_read_16(packet, 2);
|
||||
|
||||
uint16_t offset = 4;
|
||||
uint32_t time_stamp = 0;
|
||||
if (ts_flag){
|
||||
uint32_t time_stamp = little_endian_read_32(packet, offset);
|
||||
offset += 4;
|
||||
}
|
||||
|
||||
uint16_t packet_sequence_number = little_endian_read_16(packet, offset);
|
||||
offset += 2;
|
||||
|
||||
uint16_t header_2 = little_endian_read_16(packet, offset);
|
||||
uint16_t iso_sdu_length = header_2 & 0x3fff;
|
||||
uint8_t packet_status_flag = (uint8_t) (header_2 >> 14);
|
||||
offset += 2;
|
||||
|
||||
if (iso_sdu_length == 0) return;
|
||||
|
||||
if (iso_sdu_length != num_channels * octets_per_frame) {
|
||||
printf("ISO Length %u != %u * %u\n", iso_sdu_length, num_channels, octets_per_frame);
|
||||
}
|
||||
|
||||
// infer channel from con handle - only works for up to 2 channels
|
||||
uint8_t cis_channel = (con_handle == cis_con_handles[0]) ? 0 : 1;
|
||||
|
||||
if (count_mode){
|
||||
// check for missing packet
|
||||
uint16_t last_seq_no = last_packet_sequence[cis_channel];
|
||||
uint32_t now = btstack_run_loop_get_time_ms();
|
||||
bool packet_missed = (last_seq_no != 0) && ((last_seq_no + 1) != packet_sequence_number);
|
||||
if (packet_missed){
|
||||
// print last packet
|
||||
printf("\n");
|
||||
printf("%04x %10u %u ", last_seq_no, last_packet_time_ms[cis_channel], cis_channel);
|
||||
printf_hexdump(&last_packet_prefix[num_channels * PACKET_PREFIX_LEN], PACKET_PREFIX_LEN);
|
||||
last_seq_no++;
|
||||
|
||||
printf(ANSI_COLOR_RED);
|
||||
while (last_seq_no < packet_sequence_number){
|
||||
printf("%04x %u MISSING\n", last_seq_no, cis_channel);
|
||||
last_seq_no++;
|
||||
}
|
||||
printf(ANSI_COLOR_RESET);
|
||||
|
||||
// print current packet
|
||||
printf("%04x %10u %u ", packet_sequence_number, now, cis_channel);
|
||||
printf_hexdump(&packet[offset], PACKET_PREFIX_LEN);
|
||||
}
|
||||
|
||||
// cache current packet
|
||||
last_packet_time_ms[cis_channel] = now;
|
||||
last_packet_sequence[cis_channel] = packet_sequence_number;
|
||||
memcpy(&last_packet_prefix[num_channels * PACKET_PREFIX_LEN], &packet[offset], PACKET_PREFIX_LEN);
|
||||
|
||||
} else {
|
||||
|
||||
if ((packet_sequence_number & 0x7c) == 0) {
|
||||
printf("%04x %10u %u ", packet_sequence_number, btstack_run_loop_get_time_ms(), cis_channel);
|
||||
printf_hexdump(&packet[offset], iso_sdu_length);
|
||||
}
|
||||
|
||||
if (lc3_frames < DUMP_LEN_LC3_FRAMES) {
|
||||
// store len header only for first bis
|
||||
if (cis_channel == 0) {
|
||||
uint8_t len_header[2];
|
||||
little_endian_store_16(len_header, 0, iso_sdu_length);
|
||||
write(dump_file, len_header, 2);
|
||||
}
|
||||
|
||||
// store complete sdu
|
||||
write(dump_file, &packet[offset], iso_sdu_length);
|
||||
}
|
||||
|
||||
uint8_t channel;
|
||||
for (channel = 0 ; channel < num_channels ; channel++){
|
||||
|
||||
// decode codec frame
|
||||
uint8_t tmp_BEC_detect;
|
||||
uint8_t BFI = 0;
|
||||
(void) lc3_decoder->decode(&decoder_contexts[channel], &packet[offset], octets_per_frame, BFI,
|
||||
&pcm[channel * MAX_SAMPLES_PER_FRAME], number_samples_per_frame,
|
||||
&tmp_BEC_detect);
|
||||
offset += octets_per_frame;
|
||||
}
|
||||
|
||||
// interleave channel samples
|
||||
uint16_t sample;
|
||||
int16_t wav_frame[MAX_CHANNELS];
|
||||
uint8_t wav_channel;
|
||||
for (sample = 0; sample < number_samples_per_frame; sample++) {
|
||||
for (wav_channel = 0; wav_channel < num_channels; wav_channel++) {
|
||||
wav_frame[wav_channel] = pcm[wav_channel * MAX_SAMPLES_PER_FRAME + sample];
|
||||
}
|
||||
|
||||
// write wav sample
|
||||
if (lc3_frames < DUMP_LEN_LC3_FRAMES) {
|
||||
wav_writer_write_int16(num_channels, wav_frame);
|
||||
}
|
||||
|
||||
// store sample in playback buffer
|
||||
uint32_t bytes_to_store = num_channels * 2;
|
||||
samples_received++;
|
||||
if (btstack_ring_buffer_bytes_free(&playback_buffer) >= bytes_to_store) {
|
||||
btstack_ring_buffer_write(&playback_buffer, (uint8_t *) wav_frame, bytes_to_store);
|
||||
} else {
|
||||
samples_dropped++;
|
||||
}
|
||||
}
|
||||
|
||||
log_info("Samples in playback buffer %5u", btstack_ring_buffer_bytes_available(&playback_buffer) / (num_channels * 2));
|
||||
|
||||
lc3_frames++;
|
||||
frames_per_second[cis_channel]++;
|
||||
|
||||
uint32_t time_ms = btstack_run_loop_get_time_ms();
|
||||
if (btstack_time_delta(time_ms, last_samples_report_ms) > 1000){
|
||||
last_samples_report_ms = time_ms;
|
||||
printf("LC3 Frames: %4u - ", lc3_frames / num_channels);
|
||||
uint8_t i;
|
||||
for (i=0; i < num_channels; i++){
|
||||
printf("%u ", frames_per_second[i]);
|
||||
frames_per_second[i] = 0;
|
||||
}
|
||||
printf(" frames per second, dropped %u of %u\n", samples_dropped, samples_received);
|
||||
samples_received = 0;
|
||||
samples_dropped = 0;
|
||||
}
|
||||
|
||||
if (lc3_frames == DUMP_LEN_LC3_FRAMES){
|
||||
close_files();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void show_usage(void){
|
||||
printf("\n--- LE Audio Unicast Sink Test Console ---\n");
|
||||
printf("x - close files and exit\n");
|
||||
printf("---\n");
|
||||
}
|
||||
|
||||
static void stdin_process(char c){
|
||||
switch (c){
|
||||
case 'x':
|
||||
close_files();
|
||||
printf("Shutdown...\n");
|
||||
hci_power_control(HCI_POWER_OFF);
|
||||
break;
|
||||
case '\n':
|
||||
case '\r':
|
||||
break;
|
||||
default:
|
||||
show_usage();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int btstack_main(int argc, const char * argv[]);
|
||||
int btstack_main(int argc, const char * argv[]){
|
||||
(void) argv;
|
||||
(void) argc;
|
||||
|
||||
// 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);
|
||||
|
||||
// turn on!
|
||||
hci_power_control(HCI_POWER_ON);
|
||||
|
||||
btstack_stdin_setup(stdin_process);
|
||||
return 0;
|
||||
}
|
735
test/le_audio/le_audio_unicast_source.c
Normal file
735
test/le_audio/le_audio_unicast_source.c
Normal file
@ -0,0 +1,735 @@
|
||||
/*
|
||||
* 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 BLUEKITCHEN
|
||||
* GMBH 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_unicast_source.c"
|
||||
|
||||
/*
|
||||
* LE Audio Unicast Source
|
||||
* Until GATT Services are available, we encode LC3 config in advertising
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <btstack_debug.h>
|
||||
|
||||
#include "bluetooth_data_types.h"
|
||||
#include "bluetooth_company_id.h"
|
||||
#include "btstack_stdin.h"
|
||||
#include "btstack_event.h"
|
||||
#include "btstack_run_loop.h"
|
||||
#include "gap.h"
|
||||
#include "hci.h"
|
||||
#include "hci_cmd.h"
|
||||
#include "hci_dump.h"
|
||||
#include "lc3.h"
|
||||
#include "lc3_ehima.h"
|
||||
|
||||
#include "hxcmod.h"
|
||||
#include "mods/mod.h"
|
||||
|
||||
// max config
|
||||
#define MAX_CHANNELS 2
|
||||
#define MAX_SAMPLES_PER_FRAME 480
|
||||
|
||||
static uint8_t adv_data[] = {
|
||||
// Manufacturer Specific Data to indicate codec
|
||||
9,
|
||||
BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA,
|
||||
BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH & 0xff,
|
||||
BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH >> 8,
|
||||
0, // subtype: LE Audio Connection Source
|
||||
0, // flags
|
||||
1, // num bis
|
||||
8, // sampling frequency in khz
|
||||
0, // frame duration
|
||||
26, // octets per frame
|
||||
// name
|
||||
7, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'o', 'u', 'r', 'c', 'e'
|
||||
};
|
||||
|
||||
// input signal: pre-computed int16 sine wave, 96000 Hz at 300 Hz
|
||||
static const int16_t sine_int16[] = {
|
||||
0, 643, 1286, 1929, 2571, 3212, 3851, 4489, 5126, 5760,
|
||||
6393, 7022, 7649, 8273, 8894, 9512, 10126, 10735, 11341, 11943,
|
||||
12539, 13131, 13718, 14300, 14876, 15446, 16011, 16569, 17121, 17666,
|
||||
18204, 18736, 19260, 19777, 20286, 20787, 21280, 21766, 22242, 22710,
|
||||
23170, 23620, 24062, 24494, 24916, 25329, 25732, 26126, 26509, 26882,
|
||||
27245, 27597, 27938, 28269, 28589, 28898, 29196, 29482, 29757, 30021,
|
||||
30273, 30513, 30742, 30958, 31163, 31356, 31537, 31705, 31862, 32006,
|
||||
32137, 32257, 32364, 32458, 32540, 32609, 32666, 32710, 32742, 32761,
|
||||
32767, 32761, 32742, 32710, 32666, 32609, 32540, 32458, 32364, 32257,
|
||||
32137, 32006, 31862, 31705, 31537, 31356, 31163, 30958, 30742, 30513,
|
||||
30273, 30021, 29757, 29482, 29196, 28898, 28589, 28269, 27938, 27597,
|
||||
27245, 26882, 26509, 26126, 25732, 25329, 24916, 24494, 24062, 23620,
|
||||
23170, 22710, 22242, 21766, 21280, 20787, 20286, 19777, 19260, 18736,
|
||||
18204, 17666, 17121, 16569, 16011, 15446, 14876, 14300, 13718, 13131,
|
||||
12539, 11943, 11341, 10735, 10126, 9512, 8894, 8273, 7649, 7022,
|
||||
6393, 5760, 5126, 4489, 3851, 3212, 2571, 1929, 1286, 643,
|
||||
0, -643, -1286, -1929, -2571, -3212, -3851, -4489, -5126, -5760,
|
||||
-6393, -7022, -7649, -8273, -8894, -9512, -10126, -10735, -11341, -11943,
|
||||
-12539, -13131, -13718, -14300, -14876, -15446, -16011, -16569, -17121, -17666,
|
||||
-18204, -18736, -19260, -19777, -20286, -20787, -21280, -21766, -22242, -22710,
|
||||
-23170, -23620, -24062, -24494, -24916, -25329, -25732, -26126, -26509, -26882,
|
||||
-27245, -27597, -27938, -28269, -28589, -28898, -29196, -29482, -29757, -30021,
|
||||
-30273, -30513, -30742, -30958, -31163, -31356, -31537, -31705, -31862, -32006,
|
||||
-32137, -32257, -32364, -32458, -32540, -32609, -32666, -32710, -32742, -32761,
|
||||
-32767, -32761, -32742, -32710, -32666, -32609, -32540, -32458, -32364, -32257,
|
||||
-32137, -32006, -31862, -31705, -31537, -31356, -31163, -30958, -30742, -30513,
|
||||
-30273, -30021, -29757, -29482, -29196, -28898, -28589, -28269, -27938, -27597,
|
||||
-27245, -26882, -26509, -26126, -25732, -25329, -24916, -24494, -24062, -23620,
|
||||
-23170, -22710, -22242, -21766, -21280, -20787, -20286, -19777, -19260, -18736,
|
||||
-18204, -17666, -17121, -16569, -16011, -15446, -14876, -14300, -13718, -13131,
|
||||
-12539, -11943, -11341, -10735, -10126, -9512, -8894, -8273, -7649, -7022,
|
||||
-6393, -5760, -5126, -4489, -3851, -3212, -2571, -1929, -1286, -643,
|
||||
};
|
||||
|
||||
static bd_addr_t remote;
|
||||
static hci_con_handle_t remote_handle;
|
||||
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
|
||||
static unsigned int next_cis_index;
|
||||
static hci_con_handle_t cis_con_handles[MAX_CHANNELS];
|
||||
static uint16_t packet_sequence_numbers[MAX_CHANNELS];
|
||||
static uint8_t framed_pdus;
|
||||
static bool cis_can_send[MAX_CHANNELS];
|
||||
static bool cis_has_data[MAX_CHANNELS];
|
||||
static bool cis_request[MAX_CHANNELS];
|
||||
static bool cis_established[MAX_CHANNELS];
|
||||
static uint8_t iso_frame_counter;
|
||||
static uint16_t frame_duration_us;
|
||||
static uint8_t num_cis;
|
||||
|
||||
// time stamping
|
||||
#ifdef COUNT_MODE
|
||||
#define MAX_PACKET_INTERVAL_BINS_MS 50
|
||||
static uint32_t send_time_bins[MAX_PACKET_INTERVAL_BINS_MS];
|
||||
static uint32_t send_last_ms;
|
||||
#endif
|
||||
|
||||
// time based sender
|
||||
#ifdef GENERATE_AUDIO_WITH_TIMER
|
||||
static uint32_t next_send_time_ms;
|
||||
static uint32_t next_send_time_additional_us;
|
||||
static btstack_timer_source_t send_timer;
|
||||
#endif
|
||||
|
||||
// lc3 codec config
|
||||
static uint32_t sampling_frequency_hz;
|
||||
static lc3_frame_duration_t frame_duration;
|
||||
static uint16_t number_samples_per_frame;
|
||||
static uint16_t octets_per_frame;
|
||||
static uint8_t num_channels = 1;
|
||||
|
||||
// lc3 encoder
|
||||
static const lc3_encoder_t * lc3_encoder;
|
||||
static lc3_encoder_ehima_t encoder_contexts[MAX_CHANNELS];
|
||||
static int16_t pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
|
||||
static uint32_t time_generation_ms;
|
||||
|
||||
// codec menu
|
||||
static uint8_t menu_sampling_frequency;
|
||||
static uint8_t menu_variant;
|
||||
|
||||
// mod player
|
||||
static int hxcmod_initialized;
|
||||
static modcontext mod_context;
|
||||
static tracker_buffer_state trkbuf;
|
||||
static int16_t mod_pcm[MAX_CHANNELS * MAX_SAMPLES_PER_FRAME];
|
||||
|
||||
// sine generator
|
||||
static uint8_t sine_step;
|
||||
static uint16_t sine_phases[MAX_CHANNELS];
|
||||
|
||||
// audio producer
|
||||
static enum {
|
||||
AUDIO_SOURCE_SINE,
|
||||
AUDIO_SOURCE_MODPLAYER
|
||||
} audio_source = AUDIO_SOURCE_MODPLAYER;
|
||||
|
||||
static enum {
|
||||
APP_W4_WORKING,
|
||||
APP_SET_HOST_FEATURES,
|
||||
APP_IDLE,
|
||||
APP_W4_CIS_COMPLETE,
|
||||
APP_SET_ISO_PATH,
|
||||
APP_STREAMING
|
||||
} app_state = APP_W4_WORKING;
|
||||
|
||||
// enumerate default codec configs
|
||||
static struct {
|
||||
uint32_t samplingrate_hz;
|
||||
uint8_t samplingrate_index;
|
||||
uint8_t num_variants;
|
||||
struct {
|
||||
const char * name;
|
||||
lc3_frame_duration_t frame_duration;
|
||||
uint16_t octets_per_frame;
|
||||
} variants[6];
|
||||
} codec_configurations[] = {
|
||||
{
|
||||
8000, 0x01, 2,
|
||||
{
|
||||
{ "8_1", LC3_FRAME_DURATION_7500US, 26},
|
||||
{ "8_2", LC3_FRAME_DURATION_10000US, 30}
|
||||
}
|
||||
},
|
||||
{
|
||||
16000, 0x03, 2,
|
||||
{
|
||||
{ "16_1", LC3_FRAME_DURATION_7500US, 30},
|
||||
{ "16_2", LC3_FRAME_DURATION_10000US, 40}
|
||||
}
|
||||
},
|
||||
{
|
||||
24000, 0x05, 2,
|
||||
{
|
||||
{ "24_1", LC3_FRAME_DURATION_7500US, 45},
|
||||
{ "24_2", LC3_FRAME_DURATION_10000US, 60}
|
||||
}
|
||||
},
|
||||
{
|
||||
32000, 0x06, 2,
|
||||
{
|
||||
{ "32_1", LC3_FRAME_DURATION_7500US, 60},
|
||||
{ "32_2", LC3_FRAME_DURATION_10000US, 80}
|
||||
}
|
||||
},
|
||||
{
|
||||
44100, 0x07, 2,
|
||||
{
|
||||
{ "441_1", LC3_FRAME_DURATION_7500US, 97},
|
||||
{ "441_2", LC3_FRAME_DURATION_10000US, 130}
|
||||
}
|
||||
},
|
||||
{
|
||||
48000, 0x08, 6,
|
||||
{
|
||||
{ "48_1", LC3_FRAME_DURATION_7500US, 75},
|
||||
{ "48_2", LC3_FRAME_DURATION_10000US, 100},
|
||||
{ "48_3", LC3_FRAME_DURATION_7500US, 90},
|
||||
{ "48_4", LC3_FRAME_DURATION_10000US, 120},
|
||||
{ "48_5", LC3_FRAME_DURATION_7500US, 117},
|
||||
{ "48_6", LC3_FRAME_DURATION_10000US, 155}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
static void show_usage(void);
|
||||
|
||||
static void print_config(void) {
|
||||
printf("Config '%s_%u': %u, %s ms, %u octets - %s\n",
|
||||
codec_configurations[menu_sampling_frequency].variants[menu_variant].name,
|
||||
num_channels,
|
||||
codec_configurations[menu_sampling_frequency].samplingrate_hz,
|
||||
codec_configurations[menu_sampling_frequency].variants[menu_variant].frame_duration == LC3_FRAME_DURATION_7500US ? "7.5" : "10",
|
||||
codec_configurations[menu_sampling_frequency].variants[menu_variant].octets_per_frame,
|
||||
audio_source == AUDIO_SOURCE_SINE ? "Sine" : "Modplayer");
|
||||
}
|
||||
|
||||
static void setup_lc3_encoder(void){
|
||||
uint8_t channel;
|
||||
for (channel = 0 ; channel < num_channels ; channel++){
|
||||
lc3_encoder_ehima_t * context = &encoder_contexts[channel];
|
||||
lc3_encoder = lc3_encoder_ehima_init_instance(context);
|
||||
lc3_encoder->configure(context, sampling_frequency_hz, frame_duration);
|
||||
}
|
||||
number_samples_per_frame = lc3_encoder->get_number_samples_per_frame(&encoder_contexts[0]);
|
||||
btstack_assert(number_samples_per_frame <= MAX_SAMPLES_PER_FRAME);
|
||||
printf("LC3 Encoder config: %u hz, frame duration %s ms, num samples %u, num octets %u\n",
|
||||
sampling_frequency_hz, frame_duration == LC3_FRAME_DURATION_7500US ? "7.5" : "10",
|
||||
number_samples_per_frame, octets_per_frame);
|
||||
}
|
||||
|
||||
static void setup_mod_player(void){
|
||||
if (!hxcmod_initialized) {
|
||||
hxcmod_initialized = hxcmod_init(&mod_context);
|
||||
btstack_assert(hxcmod_initialized != 0);
|
||||
}
|
||||
hxcmod_unload(&mod_context);
|
||||
hxcmod_setcfg(&mod_context, sampling_frequency_hz, 16, 1, 1, 1);
|
||||
hxcmod_load(&mod_context, (void *) &mod_data, mod_len);
|
||||
}
|
||||
|
||||
static void generate_audio(void){
|
||||
uint32_t start_ms = btstack_run_loop_get_time_ms();
|
||||
uint16_t sample;
|
||||
switch (audio_source) {
|
||||
case AUDIO_SOURCE_SINE:
|
||||
// generate sine wave for all channels
|
||||
for (sample = 0 ; sample < number_samples_per_frame ; sample++){
|
||||
uint8_t channel;
|
||||
for (channel = 0; channel < num_channels; channel++) {
|
||||
int16_t value = sine_int16[sine_phases[channel]] / 4;
|
||||
pcm[channel * MAX_SAMPLES_PER_FRAME + sample] = value;
|
||||
sine_phases[channel] += sine_step * (1+channel); // second channel, double frequency
|
||||
if (sine_phases[channel] >= (sizeof(sine_int16) / sizeof(int16_t))) {
|
||||
sine_phases[channel] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case AUDIO_SOURCE_MODPLAYER:
|
||||
// mod player configured for stereo
|
||||
hxcmod_fillbuffer(&mod_context, (unsigned short *) &mod_pcm[0], number_samples_per_frame, &trkbuf);
|
||||
uint16_t i;
|
||||
if (num_channels == 1){
|
||||
// stereo -> mono
|
||||
for (i=0;i<number_samples_per_frame;i++){
|
||||
pcm[i] = (mod_pcm[2*i] / 2) + (mod_pcm[2*i+1] / 2);
|
||||
}
|
||||
} else {
|
||||
// sort interleaved samples
|
||||
for (i=0;i<number_samples_per_frame;i++){
|
||||
pcm[i] = mod_pcm[2*i];
|
||||
pcm[MAX_SAMPLES_PER_FRAME+i] = mod_pcm[2*i+1];
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
btstack_unreachable();
|
||||
break;
|
||||
}
|
||||
time_generation_ms = btstack_run_loop_get_time_ms() - start_ms;
|
||||
iso_frame_counter++;
|
||||
}
|
||||
|
||||
static void encode_and_send(uint8_t cis_index){
|
||||
|
||||
#ifdef COUNT_MODE
|
||||
if (bis_index == 0) {
|
||||
uint32_t now = btstack_run_loop_get_time_ms();
|
||||
if (send_last_ms != 0) {
|
||||
uint16_t send_interval_ms = now - send_last_ms;
|
||||
if (send_interval_ms >= MAX_PACKET_INTERVAL_BINS_MS) {
|
||||
printf("ERROR: send interval %u\n", send_interval_ms);
|
||||
} else {
|
||||
send_time_bins[send_interval_ms]++;
|
||||
}
|
||||
}
|
||||
send_last_ms = now;
|
||||
}
|
||||
#endif
|
||||
bool ok = hci_reserve_packet_buffer();
|
||||
btstack_assert(ok);
|
||||
uint8_t * buffer = hci_get_outgoing_packet_buffer();
|
||||
// complete SDU, no TimeStamp
|
||||
little_endian_store_16(buffer, 0, cis_con_handles[cis_index] | (2 << 12));
|
||||
// len
|
||||
little_endian_store_16(buffer, 2, 0 + 4 + num_channels * octets_per_frame);
|
||||
// TimeStamp if TS flag is set
|
||||
// packet seq nr
|
||||
little_endian_store_16(buffer, 4, packet_sequence_numbers[cis_index]);
|
||||
// iso sdu len
|
||||
little_endian_store_16(buffer, 6, num_channels * octets_per_frame);
|
||||
#ifdef COUNT_MODE
|
||||
// test data: bis_index, counter
|
||||
buffer[8] = bis_index;
|
||||
memset(&buffer[9], iso_frame_counter, num_channels * octets_per_frame - 1);
|
||||
#else
|
||||
// encode as lc3
|
||||
uint8_t channel;
|
||||
uint16_t offset = 8;
|
||||
for (channel = 0; channel < num_channels; channel++){
|
||||
lc3_encoder->encode(&encoder_contexts[channel], &pcm[channel * MAX_SAMPLES_PER_FRAME], &buffer[offset], octets_per_frame);
|
||||
offset += octets_per_frame;
|
||||
}
|
||||
#endif
|
||||
// send
|
||||
hci_send_iso_packet_buffer(4 + 0 + 4 + num_channels * octets_per_frame);
|
||||
|
||||
if (((packet_sequence_numbers[cis_index] & 0x7f) == 0) && (cis_index == 0)) {
|
||||
printf("Encoding time: %u\n", time_generation_ms);
|
||||
}
|
||||
if ((packet_sequence_numbers[cis_index] & 0x7c) == 0){
|
||||
printf("%04x %10u %u ", packet_sequence_numbers[cis_index], btstack_run_loop_get_time_ms(), cis_index);
|
||||
printf_hexdump(&buffer[8], octets_per_frame);
|
||||
}
|
||||
|
||||
packet_sequence_numbers[cis_index]++;
|
||||
}
|
||||
|
||||
static void try_send(void){
|
||||
bool all_can_send = true;
|
||||
uint8_t i;
|
||||
for (i=0; i < num_cis; i++) {
|
||||
all_can_send &= cis_can_send[i];
|
||||
}
|
||||
#ifdef PTS_MODE
|
||||
static uint8_t next_sender;
|
||||
// PTS 8.2 sends a packet after the previous one was received -> it sends at half speed for stereo configuration
|
||||
if (all_can_send) {
|
||||
if (next_sender == 0) {
|
||||
generate_audio();
|
||||
}
|
||||
cis_can_send[next_sender] = false;
|
||||
encode_and_send(next_sender);
|
||||
next_sender = (num_cis - 1) - next_sender;
|
||||
}
|
||||
#else
|
||||
#ifdef GENERATE_AUDIO_WITH_TIMER
|
||||
for (i=0;i<num_cis;i++){
|
||||
if (hci_is_packet_buffer_reserved()) return;
|
||||
if (cis_has_data[i]){
|
||||
cis_can_send[i] = false;
|
||||
cis_has_data[i] = false;
|
||||
encode_and_send(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#else
|
||||
// check if next audio frame should be produced and send
|
||||
if (all_can_send){
|
||||
generate_audio();
|
||||
for (i=0; i < num_cis; i++) {
|
||||
cis_has_data[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i < num_cis; i++){
|
||||
if (hci_is_packet_buffer_reserved()) return;
|
||||
if (cis_can_send[i] && cis_has_data[i]){
|
||||
cis_can_send[i] = false;
|
||||
cis_has_data[i] = false;
|
||||
encode_and_send(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef GENERATE_AUDIO_WITH_TIMER
|
||||
static void generate_audio_timer_handler(btstack_timer_source_t *ts){
|
||||
|
||||
generate_audio();
|
||||
|
||||
uint8_t i;
|
||||
for (i=0; i<num_cis;i++) {
|
||||
cis_has_data[i] = true;
|
||||
}
|
||||
|
||||
// next send time based on frame_duration_us
|
||||
next_send_time_additional_us += frame_duration_us % 1000;
|
||||
if (next_send_time_additional_us > 1000){
|
||||
next_send_time_ms++;
|
||||
next_send_time_additional_us -= 1000;
|
||||
}
|
||||
next_send_time_ms += frame_duration_us / 1000;
|
||||
|
||||
uint32_t now = btstack_run_loop_get_time_ms();
|
||||
btstack_run_loop_set_timer(&send_timer, next_send_time_ms - now);
|
||||
btstack_run_loop_add_timer(&send_timer);
|
||||
|
||||
try_send();
|
||||
}
|
||||
#endif
|
||||
|
||||
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(channel);
|
||||
if (packet_type != HCI_EVENT_PACKET) return;
|
||||
|
||||
bd_addr_t event_addr;
|
||||
|
||||
switch (packet[0]) {
|
||||
case BTSTACK_EVENT_STATE:
|
||||
switch(btstack_event_state_get_state(packet)) {
|
||||
case HCI_STATE_WORKING:
|
||||
app_state = APP_SET_HOST_FEATURES;
|
||||
break;
|
||||
case HCI_STATE_OFF:
|
||||
printf("Goodbye\n");
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_COMMAND_COMPLETE:
|
||||
switch (hci_event_command_complete_get_command_opcode(packet)){
|
||||
case HCI_OPCODE_HCI_LE_CREATE_CIS:
|
||||
app_state = APP_SET_ISO_PATH;
|
||||
printf("Set ISO Paths\n");
|
||||
break;
|
||||
case HCI_OPCODE_HCI_LE_SETUP_ISO_DATA_PATH:
|
||||
next_cis_index++;
|
||||
if (next_cis_index == num_cis){
|
||||
printf("%u ISO path(s) set up\n", num_channels);
|
||||
// ready to send
|
||||
uint8_t i;
|
||||
for (i=0; i < num_cis; i++) {
|
||||
cis_can_send[i] = true;
|
||||
}
|
||||
app_state = APP_STREAMING;
|
||||
//
|
||||
#ifdef GENERATE_AUDIO_WITH_TIMER
|
||||
btstack_run_loop_set_timer_handler(&send_timer, &generate_audio_timer_handler);
|
||||
uint32_t next_send_time_ms = btstack_run_loop_get_time_ms() + 10;
|
||||
uint32_t now = btstack_run_loop_get_time_ms();
|
||||
btstack_run_loop_set_timer(&send_timer, next_send_time_ms - now);
|
||||
btstack_run_loop_add_timer(&send_timer);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_LE_META:
|
||||
switch(hci_event_le_meta_get_subevent_code(packet)){
|
||||
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
|
||||
next_cis_index = 0;
|
||||
break;
|
||||
case HCI_SUBEVENT_LE_CIS_REQUEST:
|
||||
cis_con_handles[next_cis_index] = hci_subevent_le_cis_request_get_cis_connection_handle(packet);
|
||||
cis_request[next_cis_index] = true;
|
||||
next_cis_index++;
|
||||
break;
|
||||
case HCI_SUBEVENT_LE_CIS_ESTABLISHED:
|
||||
{
|
||||
// only look for cis handle
|
||||
uint8_t i;
|
||||
hci_con_handle_t cis_handle = hci_subevent_le_cis_established_get_connection_handle(packet);
|
||||
for (i=0; i < num_cis; i++){
|
||||
if (cis_handle == cis_con_handles[i]){
|
||||
cis_established[i] = true;
|
||||
}
|
||||
}
|
||||
// check for complete
|
||||
bool complete = true;
|
||||
for (i=0; i < num_cis; i++) {
|
||||
complete &= cis_established[i];
|
||||
}
|
||||
if (complete) {
|
||||
printf("All CIS Established\n");
|
||||
next_cis_index = 0;
|
||||
app_state = APP_SET_ISO_PATH;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
|
||||
if (size >= 3){
|
||||
uint16_t num_handles = packet[2];
|
||||
if (size != (3u + num_handles * 4u)) break;
|
||||
uint16_t offset = 3;
|
||||
uint16_t i;
|
||||
for (i=0; i<num_handles;i++) {
|
||||
hci_con_handle_t handle = little_endian_read_16(packet, offset) & 0x0fffu;
|
||||
offset += 2u;
|
||||
uint16_t num_packets = little_endian_read_16(packet, offset);
|
||||
offset += 2u;
|
||||
uint8_t j;
|
||||
for (j=0 ; j < num_channels ; j++){
|
||||
if (handle == cis_con_handles[j]){
|
||||
// allow to send
|
||||
cis_can_send[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (hci_can_send_command_packet_now()) {
|
||||
switch(app_state){
|
||||
case APP_SET_HOST_FEATURES:
|
||||
hci_send_cmd(&hci_le_set_host_feature, 32, 1);
|
||||
app_state = APP_IDLE;
|
||||
show_usage();
|
||||
printf("Please select sample frequency and variation, then start advertising\n");
|
||||
break;
|
||||
case APP_W4_CIS_COMPLETE:
|
||||
{
|
||||
uint8_t i;
|
||||
for (i=0; i < num_cis; i++){
|
||||
if (cis_request[i]){
|
||||
cis_request[i] = false;
|
||||
printf("Accept CIS Request for conn handle %04x\n", cis_con_handles[i]);
|
||||
hci_send_cmd(&hci_le_accept_cis_request, cis_con_handles[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case APP_SET_ISO_PATH:
|
||||
hci_send_cmd(&hci_le_setup_iso_data_path, cis_con_handles[next_cis_index], 0, 0, 0, 0, 0, 0, 0, NULL);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try_send();
|
||||
}
|
||||
|
||||
static void show_usage(void){
|
||||
printf("\n--- LE Audio Unicast Source Test Console ---\n");
|
||||
print_config();
|
||||
printf("---\n");
|
||||
printf("c - toggle channels\n");
|
||||
printf("f - next sampling frequency\n");
|
||||
printf("v - next codec variant\n");
|
||||
printf("t - toggle sine / modplayer\n");
|
||||
printf("s - start advertising\n");
|
||||
printf("x - shutdown\n");
|
||||
printf("---\n");
|
||||
}
|
||||
|
||||
static void stdin_process(char c){
|
||||
switch (c){
|
||||
case 'c':
|
||||
if (app_state != APP_IDLE){
|
||||
printf("Codec configuration can only be changed in idle state\n");
|
||||
break;
|
||||
}
|
||||
num_channels = 3 - num_channels;
|
||||
print_config();
|
||||
break;
|
||||
case 'f':
|
||||
if (app_state != APP_IDLE){
|
||||
printf("Codec configuration can only be changed in idle state\n");
|
||||
break;
|
||||
}
|
||||
menu_sampling_frequency++;
|
||||
if (menu_sampling_frequency >= 6){
|
||||
menu_sampling_frequency = 0;
|
||||
}
|
||||
if (menu_variant >= codec_configurations[menu_sampling_frequency].num_variants){
|
||||
menu_variant = 0;
|
||||
}
|
||||
print_config();
|
||||
break;
|
||||
case 'v':
|
||||
if (app_state != APP_IDLE){
|
||||
printf("Codec configuration can only be changed in idle state\n");
|
||||
break;
|
||||
}
|
||||
menu_variant++;
|
||||
if (menu_variant >= codec_configurations[menu_sampling_frequency].num_variants){
|
||||
menu_variant = 0;
|
||||
}
|
||||
print_config();
|
||||
break;
|
||||
case 'x':
|
||||
#ifdef COUNT_MODE
|
||||
printf("Send statistic:\n");
|
||||
{
|
||||
uint16_t i;
|
||||
for (i=0;i<MAX_PACKET_INTERVAL_BINS_MS;i++){
|
||||
printf("%2u: %5u\n", i, send_time_bins[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
printf("Shutdown...\n");
|
||||
hci_power_control(HCI_POWER_OFF);
|
||||
break;
|
||||
case 's':
|
||||
if (app_state != APP_IDLE){
|
||||
printf("Cannot start advertising - not in idle state\n");
|
||||
break;
|
||||
}
|
||||
// use values from table
|
||||
sampling_frequency_hz = codec_configurations[menu_sampling_frequency].samplingrate_hz;
|
||||
octets_per_frame = codec_configurations[menu_sampling_frequency].variants[menu_variant].octets_per_frame;
|
||||
frame_duration = codec_configurations[menu_sampling_frequency].variants[menu_variant].frame_duration;
|
||||
|
||||
// get num samples per frame
|
||||
setup_lc3_encoder();
|
||||
|
||||
// setup mod player
|
||||
setup_mod_player();
|
||||
|
||||
// setup sine generator
|
||||
if (sampling_frequency_hz == 44100){
|
||||
sine_step = 2;
|
||||
} else {
|
||||
sine_step = 96000 / sampling_frequency_hz;
|
||||
}
|
||||
|
||||
// update adv / BASE
|
||||
adv_data[4] = 0; // subtype
|
||||
adv_data[5] = 0; // flags
|
||||
adv_data[6] = num_channels;
|
||||
adv_data[7] = sampling_frequency_hz / 1000;
|
||||
adv_data[8] = frame_duration == LC3_FRAME_DURATION_7500US ? 0 : 1;
|
||||
adv_data[9] = octets_per_frame;
|
||||
|
||||
// setup advertisements
|
||||
uint16_t adv_int_min = 0x0030;
|
||||
uint16_t adv_int_max = 0x0030;
|
||||
uint8_t adv_type = 0;
|
||||
bd_addr_t null_addr;
|
||||
memset(null_addr, 0, 6);
|
||||
gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
|
||||
gap_advertisements_set_data(sizeof(adv_data), adv_data);
|
||||
gap_advertisements_enable(1);
|
||||
num_cis = 1;
|
||||
app_state = APP_W4_CIS_COMPLETE;
|
||||
break;
|
||||
case 't':
|
||||
audio_source = 1 - audio_source;
|
||||
print_config();
|
||||
break;
|
||||
case '\n':
|
||||
case '\r':
|
||||
break;
|
||||
default:
|
||||
show_usage();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int btstack_main(int argc, const char * argv[]);
|
||||
int btstack_main(int argc, const char * argv[]){
|
||||
(void) argv;
|
||||
(void) argc;
|
||||
|
||||
// register for HCI events
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
|
||||
// turn on!
|
||||
hci_power_control(HCI_POWER_ON);
|
||||
|
||||
btstack_stdin_setup(stdin_process);
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user