avdtp source pts tes: extend sbc encoder init with channel mode param

This commit is contained in:
Milanka Ringwald 2017-07-19 16:04:58 +02:00
parent f53d6fa721
commit 45f49b5884
4 changed files with 418 additions and 94 deletions

View File

@ -262,6 +262,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
sc.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet) - 1;
sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
sc.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet);
// TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
break;
}
@ -342,7 +343,8 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
btstack_sbc_encoder_init(&sc.sbc_encoder_state, SBC_MODE_STANDARD,
sc.block_length, sc.subbands,
sc.allocation_method, sc.sampling_frequency,
sc.max_bitpool_value);
sc.max_bitpool_value,
sc.channel_mode);
avdtp_source_open_stream(cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
break;
}

View File

@ -123,9 +123,10 @@ int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state);
* @param allocation_method
* @param sample_rate
* @param bitpool
* @param channel_mode
*/
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
int blocks, int subbands, int allocation_method, int sample_rate, int bitpool);
int blocks, int subbands, int allocation_method, int sample_rate, int bitpool, int channel_mode);
/**
* @brief Encode PCM data

View File

@ -503,7 +503,7 @@ void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int p
// *****************************************************************************
void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_mode_t mode,
int blocks, int subbands, int allmethod, int sample_rate, int bitpool){
int blocks, int subbands, int allmethod, int sample_rate, int bitpool, int channel_mode){
if (sbc_encoder_state_singleton && sbc_encoder_state_singleton != state ){
log_error("SBC encoder: different sbc decoder state is allready registered");
@ -524,9 +524,11 @@ void btstack_sbc_encoder_init(btstack_sbc_encoder_state_t * state, btstack_sbc_m
bd_encoder_state.context.s16AllocationMethod = allmethod;
bd_encoder_state.context.s16BitPool = bitpool;
bd_encoder_state.context.mSBCEnabled = 0;
bd_encoder_state.context.s16ChannelMode = SBC_STEREO;
bd_encoder_state.context.s16ChannelMode = channel_mode;
bd_encoder_state.context.s16NumOfChannels = 2;
if (bd_encoder_state.context.s16ChannelMode == SBC_MONO){
bd_encoder_state.context.s16NumOfChannels = 1;
}
switch(sample_rate){
case 16000: bd_encoder_state.context.s16SamplingFreq = SBC_sf16000; break;
case 32000: bd_encoder_state.context.s16SamplingFreq = SBC_sf32000; break;

View File

@ -45,6 +45,26 @@
#include "btstack.h"
#define NUM_CHANNELS 2
#define A2DP_SAMPLE_RATE 44100
#define BYTES_PER_AUDIO_SAMPLE (2*NUM_CHANNELS)
#define AUDIO_TIMEOUT_MS 10
#define TABLE_SIZE_441HZ 100
#define AVDTP_MEDIA_PAYLOAD_HEADER_SIZE 12
static const int16_t sine_int16[] = {
0, 2057, 4107, 6140, 8149, 10126, 12062, 13952, 15786, 17557,
19260, 20886, 22431, 23886, 25247, 26509, 27666, 28714, 29648, 30466,
31163, 31738, 32187, 32509, 32702, 32767, 32702, 32509, 32187, 31738,
31163, 30466, 29648, 28714, 27666, 26509, 25247, 23886, 22431, 20886,
19260, 17557, 15786, 13952, 12062, 10126, 8149, 6140, 4107, 2057,
0, -2057, -4107, -6140, -8149, -10126, -12062, -13952, -15786, -17557,
-19260, -20886, -22431, -23886, -25247, -26509, -27666, -28714, -29648, -30466,
-31163, -31738, -32187, -32509, -32702, -32767, -32702, -32509, -32187, -31738,
-31163, -30466, -29648, -28714, -27666, -26509, -25247, -23886, -22431, -20886,
-19260, -17557, -15786, -13952, -12062, -10126, -8149, -6140, -4107, -2057,
};
typedef struct {
// bitmaps
uint8_t sampling_frequency_bitmap;
@ -79,15 +99,32 @@ static const char * device_addr_string = "00:21:3C:AC:F7:38";
// head phones: static const char * device_addr_string = "00:18:09:28:50:18";
// bt dongle: static const char * device_addr_string = "00:15:83:5F:9D:46";
#endif
typedef struct {
uint16_t avdtp_cid;
uint8_t local_seid;
uint8_t remote_seid;
uint32_t time_audio_data_sent; // ms
uint32_t acc_num_missed_samples;
uint32_t samples_ready;
btstack_timer_source_t audio_timer;
uint8_t streaming;
int max_media_payload_size;
uint8_t sbc_storage[1030];
uint16_t sbc_storage_count;
uint8_t sbc_ready_to_send;
} a2dp_media_sending_context_t;
a2dp_media_sending_context_t media_tracker;
static bd_addr_t device_addr;
static int sine_phase;
static uint16_t avdtp_cid = 0;
static uint8_t sdp_avdtp_source_service_buffer[150];
static uint8_t remote_seid;
static adtvp_media_codec_information_sbc_t sbc_capability;
static avdtp_media_codec_configuration_sbc_t sbc_configuration;
static avdtp_stream_endpoint_t * local_stream_endpoint;
static avdtp_stream_endpoint_context_t sc;
static uint16_t remote_configuration_bitmap;
static avdtp_capabilities_t remote_configuration;
@ -95,27 +132,269 @@ static avdtp_context_t a2dp_source_context;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static void dump_sbc_capability(adtvp_media_codec_information_sbc_t media_codec_sbc){
printf(" --- avdtp source --- Received media codec capability:\n");
printf(" - sampling_frequency: 0x%02x\n", media_codec_sbc.sampling_frequency_bitmap);
printf(" - channel_mode: 0x%02x\n", media_codec_sbc.channel_mode_bitmap);
printf(" - block_length: 0x%02x\n", media_codec_sbc.block_length_bitmap);
printf(" - subbands: 0x%02x\n", media_codec_sbc.subbands_bitmap);
printf(" - allocation_method: 0x%02x\n", media_codec_sbc.allocation_method_bitmap);
printf(" - bitpool_value [%d, %d] \n", media_codec_sbc.min_bitpool_value, media_codec_sbc.max_bitpool_value);
printf("\n");
static void a2dp_source_setup_media_header(uint8_t * media_packet, int size, int *offset, uint8_t marker, uint16_t sequence_number){
if (size < AVDTP_MEDIA_PAYLOAD_HEADER_SIZE){
log_error("small outgoing buffer");
return;
}
uint8_t rtp_version = 2;
uint8_t padding = 0;
uint8_t extension = 0;
uint8_t csrc_count = 0;
uint8_t payload_type = 1; //0x60;
// uint16_t sequence_number = stream_endpoint->sequence_number;
uint32_t timestamp = btstack_run_loop_get_time_ms();
uint32_t ssrc = 0x11223344;
// rtp header (min size 12B)
int pos = 0;
// int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count;
media_packet[pos++] = (marker << 1) | payload_type;
big_endian_store_16(media_packet, pos, sequence_number);
pos += 2;
big_endian_store_32(media_packet, pos, timestamp);
pos += 4;
big_endian_store_32(media_packet, pos, ssrc); // only used for multicast
pos += 4;
*offset = pos;
}
static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){
printf(" --- avdtp source --- Received media codec configuration:\n");
printf(" - num_channels: %d\n", configuration.num_channels);
printf(" - sampling_frequency: %d\n", configuration.sampling_frequency);
printf(" - channel_mode: %d\n", configuration.channel_mode);
printf(" - block_length: %d\n", configuration.block_length);
printf(" - subbands: %d\n", configuration.subbands);
printf(" - allocation_method: %d\n", configuration.allocation_method);
printf(" - bitpool_value [%d, %d] \n", configuration.min_bitpool_value, configuration.max_bitpool_value);
printf("\n");
static void a2dp_source_copy_media_payload(uint8_t * media_packet, int size, int * offset, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames){
if (size < num_bytes_to_copy + 1){
log_error("small outgoing buffer: buffer size %u, but need %u", size, num_bytes_to_copy + 1);
return;
}
int pos = *offset;
media_packet[pos++] = num_frames; // (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
memcpy(media_packet + pos, storage, num_bytes_to_copy);
pos += num_bytes_to_copy;
*offset = pos;
}
static int avdtp_source_stream_send_media_payload(uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){
avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
if (!stream_endpoint) {
log_error("no stream_endpoint found for seid %d", local_seid);
return 0;
}
if (stream_endpoint->l2cap_media_cid == 0){
log_error("no media cid found for seid %d", local_seid);
return 0;
}
int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
int offset = 0;
// HACK / PTS requests ERTM although we did not offer it in L2CAP Information Request. Withouth ERTM support, default MTU of 48 will be used, but that's to
// small for a 44.1kHz/16/8/Stereo/bitpool 53 sbc frame of 119 bytes (+ media info)
size = 0x290;
l2cap_reserve_packet_buffer();
uint8_t * media_packet = l2cap_get_outgoing_buffer();
//int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
a2dp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number);
a2dp_source_copy_media_payload(media_packet, size, &offset, storage, num_bytes_to_copy, num_frames);
stream_endpoint->sequence_number++;
l2cap_send_prepared(stream_endpoint->l2cap_media_cid, offset);
return size;
}
static void a2dp_demo_send_media_packet(void){
int num_bytes_in_frame = btstack_sbc_encoder_sbc_buffer_length();
int bytes_in_storage = media_tracker.sbc_storage_count;
uint8_t num_frames = bytes_in_storage / num_bytes_in_frame;
avdtp_source_stream_send_media_payload(media_tracker.local_seid, media_tracker.sbc_storage, bytes_in_storage, num_frames, 0);
media_tracker.sbc_storage_count = 0;
media_tracker.sbc_ready_to_send = 0;
}
static void produce_sine_audio(int16_t * pcm_buffer, int num_samples_to_write){
int count;
for (count = 0; count < num_samples_to_write ; count++){
pcm_buffer[count * 2] = sine_int16[sine_phase];
pcm_buffer[count * 2 + 1] = sine_int16[sine_phase];
sine_phase++;
if (sine_phase >= TABLE_SIZE_441HZ){
sine_phase -= TABLE_SIZE_441HZ;
}
}
}
static int fill_sbc_audio_buffer(a2dp_media_sending_context_t * context){
// perform sbc encodin
int total_num_bytes_read = 0;
int num_audio_samples_per_sbc_buffer = btstack_sbc_encoder_num_audio_frames();
// printf("num_audio_samples_per_sbc_buffer %d, btstack_sbc_encoder_sbc_buffer_length() %d\n", num_audio_samples_per_sbc_buffer, btstack_sbc_encoder_sbc_buffer_length());
while (context->samples_ready >= num_audio_samples_per_sbc_buffer
&& (context->max_media_payload_size - context->sbc_storage_count) >= btstack_sbc_encoder_sbc_buffer_length()){
uint8_t pcm_frame[256*BYTES_PER_AUDIO_SAMPLE];
produce_sine_audio((int16_t *) pcm_frame, num_audio_samples_per_sbc_buffer);
btstack_sbc_encoder_process_data((int16_t *) pcm_frame);
uint16_t sbc_frame_size = btstack_sbc_encoder_sbc_buffer_length();
uint8_t * sbc_frame = btstack_sbc_encoder_sbc_buffer();
total_num_bytes_read += num_audio_samples_per_sbc_buffer;
memcpy(&context->sbc_storage[context->sbc_storage_count], sbc_frame, sbc_frame_size);
context->sbc_storage_count += sbc_frame_size;
context->samples_ready -= num_audio_samples_per_sbc_buffer;
}
return total_num_bytes_read;
}
static void avdtp_audio_timeout_handler(btstack_timer_source_t * timer){
a2dp_media_sending_context_t * context = (a2dp_media_sending_context_t *) btstack_run_loop_get_timer_context(timer);
btstack_run_loop_set_timer(&context->audio_timer, AUDIO_TIMEOUT_MS);
btstack_run_loop_add_timer(&context->audio_timer);
uint32_t now = btstack_run_loop_get_time_ms();
uint32_t update_period_ms = AUDIO_TIMEOUT_MS;
if (context->time_audio_data_sent > 0){
update_period_ms = now - context->time_audio_data_sent;
}
uint32_t num_samples = (update_period_ms * A2DP_SAMPLE_RATE) / 1000;
context->acc_num_missed_samples += (update_period_ms * A2DP_SAMPLE_RATE) % 1000;
while (context->acc_num_missed_samples >= 1000){
num_samples++;
context->acc_num_missed_samples -= 1000;
}
context->time_audio_data_sent = now;
context->samples_ready += num_samples;
if (context->sbc_ready_to_send) return;
fill_sbc_audio_buffer(context);
if ((context->sbc_storage_count + btstack_sbc_encoder_sbc_buffer_length()) > context->max_media_payload_size){
// schedule sending
context->sbc_ready_to_send = 1;
// a2dp_source_stream_endpoint_request_can_send_now(context->local_seid);
avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(context->local_seid, &a2dp_source_context);
if (!stream_endpoint) {
printf("no stream_endpoint for seid %d\n", context->local_seid);
return;
}
stream_endpoint->send_stream = 1;
if (stream_endpoint->connection){
avdtp_request_can_send_now_initiator(stream_endpoint->connection, stream_endpoint->l2cap_media_cid);
}
}
}
// static int avdtp_max_media_payload_size(uint8_t local_seid){
// avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
// if (!stream_endpoint) {
// log_error("no stream_endpoint found for seid %d", local_seid);
// return 0;
// }
// if (stream_endpoint->l2cap_media_cid == 0){
// log_error("no media cid found for seid %d", local_seid);
// return 0;
// }
// return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - AVDTP_MEDIA_PAYLOAD_HEADER_SIZE;
// }
static void a2dp_demo_timer_start(a2dp_media_sending_context_t * context){
context->max_media_payload_size = 0x290;// avdtp_max_media_payload_size(context->local_seid);
context->sbc_storage_count = 0;
context->sbc_ready_to_send = 0;
context->streaming = 1;
btstack_run_loop_remove_timer(&context->audio_timer);
btstack_run_loop_set_timer_handler(&context->audio_timer, avdtp_audio_timeout_handler);
btstack_run_loop_set_timer_context(&context->audio_timer, context);
btstack_run_loop_set_timer(&context->audio_timer, AUDIO_TIMEOUT_MS);
btstack_run_loop_add_timer(&context->audio_timer);
}
static void a2dp_demo_timer_stop(a2dp_media_sending_context_t * context){
context->time_audio_data_sent = 0;
context->acc_num_missed_samples = 0;
context->samples_ready = 0;
context->streaming = 1;
context->sbc_storage_count = 0;
context->sbc_ready_to_send = 0;
btstack_run_loop_remove_timer(&context->audio_timer);
}
static void a2dp_demo_timer_pause(a2dp_media_sending_context_t * context){
btstack_run_loop_remove_timer(&context->audio_timer);
}
static void initialize_streaming_context(const uint8_t * configuration){
sc.channel_mode = configuration[0] & 0x0F;
sc.sampling_frequency = configuration[0] >> 4;
sc.block_length = configuration[1] >> 4;
sc.subbands = (configuration[1] & 0x0F) >> 1;
sc.allocation_method = (configuration[1] & 0x03) >> 1;
sc.max_bitpool_value = configuration[3];
if (sc.channel_mode & AVDTP_SBC_JOINT_STEREO){
sc.channel_mode = 3;
} else if (sc.channel_mode & AVDTP_SBC_STEREO){
sc.channel_mode = 2;
} else if (sc.channel_mode & AVDTP_SBC_DUAL_CHANNEL){
sc.channel_mode = 1;
} else if (sc.channel_mode & AVDTP_SBC_MONO){
sc.channel_mode = 0;
}
if (sc.sampling_frequency & AVDTP_SBC_16000){
sc.sampling_frequency = 16000;
}
if (sc.sampling_frequency & AVDTP_SBC_32000){
sc.sampling_frequency = 32000;
}
if (sc.sampling_frequency & AVDTP_SBC_44100){
sc.sampling_frequency = 44100;
}
if (sc.sampling_frequency & AVDTP_SBC_48000){
sc.sampling_frequency = 48000;
}
if (sc.subbands & AVDTP_SBC_SUBBANDS_4){
sc.subbands = 4;
}
if (sc.subbands & AVDTP_SBC_SUBBANDS_8){
sc.subbands = 8;
}
if (sc.block_length & AVDTP_SBC_BLOCK_LENGTH_4){
sc.block_length = 4;
}
if (sc.block_length & AVDTP_SBC_BLOCK_LENGTH_8){
sc.block_length = 8;
}
if (sc.block_length & AVDTP_SBC_BLOCK_LENGTH_12){
sc.block_length = 12;
}
if (sc.block_length & AVDTP_SBC_BLOCK_LENGTH_16){
sc.block_length = 16;
}
}
static void initialize_sbc_encoder(void){
printf("streaming_context: block length %d, subbands %d, allocation_method %d, sampling_frequency %d, max_bitpool_value %d, channel_mode %d\n",
sc.block_length, sc.subbands,
sc.allocation_method, sc.sampling_frequency,
sc.max_bitpool_value, sc.channel_mode);
btstack_sbc_encoder_init(&sc.sbc_encoder_state, SBC_MODE_STANDARD,
sc.block_length, sc.subbands,
sc.allocation_method, sc.sampling_frequency,
sc.max_bitpool_value, sc.channel_mode);
}
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
@ -126,6 +405,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
uint8_t signal_identifier;
uint8_t status;
avdtp_sep_t sep;
uint16_t avdtp_cid;
switch (packet[2]){
case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
@ -135,6 +415,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
printf("AVDTP source signaling connection failed: status %d\n", status);
break;
}
media_tracker.avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
printf("AVDTP source signaling connection established: avdtp_cid 0x%02x\n", avdtp_cid);
break;
@ -155,51 +436,80 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
sep.type = avdtp_subevent_signaling_sep_found_get_sep_type(packet);
printf("Found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", sep.seid, sep.in_use, sep.media_type, sep.type);
break;
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:
sbc_capability.sampling_frequency_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet);
sbc_capability.channel_mode_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet);
sbc_capability.block_length_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet);
sbc_capability.subbands_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet);
sbc_capability.allocation_method_bitmap = avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet);
sbc_capability.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet);
sbc_capability.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet);
dump_sbc_capability(sbc_capability);
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:{
// TODO check cid
if (!sc.local_stream_endpoint) return;
uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
uint8_t channel_mode = avdtp_choose_sbc_channel_mode(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
uint8_t block_length = avdtp_choose_sbc_block_length(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
uint8_t subbands = avdtp_choose_sbc_subbands(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
uint8_t allocation_method = avdtp_choose_sbc_allocation_method(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[0] = (sampling_frequency << 4) | channel_mode;
sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[1] = (block_length << 4) | (subbands << 2) | allocation_method;
sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[2] = min_bitpool_value;
sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[3] = max_bitpool_value;
sc.local_stream_endpoint->remote_configuration_bitmap = store_bit16(sc.local_stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
sc.local_stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
break;
}
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{
sbc_configuration.reconfigure = avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
sbc_configuration.num_channels = avdtp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet);
sbc_configuration.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);
sbc_configuration.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet);
sbc_configuration.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet);
sbc_configuration.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
sbc_configuration.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet);
sbc_configuration.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet);
sbc_configuration.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length;
dump_sbc_configuration(sbc_configuration);
// TODO check cid
printf(" AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION \n");
sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);
sc.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet);
sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
sc.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet) - 1;
sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
// TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
break;
}
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
printf("Received non SBC codec: not parsed\n");
break;
case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW:
a2dp_demo_send_media_packet();
break;
case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
// TODO check cid
signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
printf("Accepted %s\n", avdtp_si2str(signal_identifier));
switch (signal_identifier){
case AVDTP_SI_START:
case AVDTP_SI_OPEN:
initialize_sbc_encoder();
break;
case AVDTP_SI_CLOSE:
case AVDTP_SI_SET_CONFIGURATION:
break;
case AVDTP_SI_START:
printf("Stream started.\n");
a2dp_demo_timer_start(&media_tracker);
break;
case AVDTP_SI_SUSPEND:
printf("Stream paused.\n");
a2dp_demo_timer_pause(&media_tracker);
break;
case AVDTP_SI_ABORT:
case AVDTP_SI_CLOSE:
printf("Stream released.\n");
a2dp_demo_timer_stop(&media_tracker);
break;
default:
break;
}
break;
case AVDTP_SUBEVENT_SIGNALING_REJECT:
signal_identifier = avdtp_subevent_signaling_reject_get_signal_identifier(packet);
printf("Rejected %s\n", avdtp_si2str(signal_identifier));
@ -208,8 +518,16 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
signal_identifier = avdtp_subevent_signaling_general_reject_get_signal_identifier(packet);
printf("Rejected %s\n", avdtp_si2str(signal_identifier));
break;
case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
a2dp_demo_timer_stop(&media_tracker);
printf("Streaming connection released.\n");
break;
case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
a2dp_demo_timer_stop(&media_tracker);
printf("Signaling connection released.\n");
break;
default:
printf("AVDTP Source PTS test: event not parsed\n");
printf("AVDTP Source PTS test: event not parsed 0x%02x\n", packet[2]);
break;
}
}
@ -233,6 +551,8 @@ static const uint8_t media_sbc_codec_reconfiguration[] = {
2, 53
};
#ifdef HAVE_BTSTACK_STDIN
static void show_usage(void){
@ -246,92 +566,90 @@ static void show_usage(void){
printf("a - get all capabilities\n");
printf("s - set configuration\n");
printf("f - get configuration\n");
printf("R - reconfigure stream with %d\n", remote_seid);
printf("o - open stream with seid %d\n", remote_seid);
printf("m - start stream with %d\n", remote_seid);
printf("x - start data stream\n");
printf("X - stop data stream\n");
printf("A - abort stream with %d\n", remote_seid);
printf("S - stop stream with %d\n", remote_seid);
printf("P - suspend stream with %d\n", remote_seid);
printf("x - start streaming sine\n");
printf("R - reconfigure stream with %d\n", media_tracker.remote_seid);
printf("o - open stream with seid %d\n", media_tracker.remote_seid);
printf("m - start stream with %d\n", media_tracker.remote_seid);
printf("A - abort stream with %d\n", media_tracker.remote_seid);
printf("S - stop stream with %d\n", media_tracker.remote_seid);
printf("P - suspend stream with %d\n", media_tracker.remote_seid);
printf("X - stop streaming sine\n");
printf("Ctrl-c - exit\n");
printf("---\n");
}
static void stdin_process(char cmd){
remote_seid = 1;
switch (cmd){
case 'c':
printf("Establish AVDTP Source connection to %s\n", device_addr_string);
avdtp_source_connect(device_addr, &avdtp_cid);
avdtp_source_connect(device_addr, &media_tracker.avdtp_cid);
break;
case 'C':
printf("Disconnect AVDTP Source\n");
avdtp_source_disconnect(avdtp_cid);
avdtp_source_disconnect(media_tracker.avdtp_cid);
break;
case 'd':
printf("Discover stream endpoints of %s\n", device_addr_string);
avdtp_source_discover_stream_endpoints(avdtp_cid);
avdtp_source_discover_stream_endpoints(media_tracker.avdtp_cid);
break;
case 'g':
printf("Get capabilities of stream endpoint with seid %d\n", remote_seid);
avdtp_source_get_capabilities(avdtp_cid, remote_seid);
printf("Get capabilities of stream endpoint with seid %d\n", media_tracker.remote_seid);
avdtp_source_get_capabilities(media_tracker.avdtp_cid, media_tracker.remote_seid);
break;
case 'a':
printf("Get all capabilities of stream endpoint with seid %d\n", remote_seid);
avdtp_source_get_all_capabilities(avdtp_cid, remote_seid);
printf("Get all capabilities of stream endpoint with seid %d\n", media_tracker.remote_seid);
avdtp_source_get_all_capabilities(media_tracker.avdtp_cid, media_tracker.remote_seid);
break;
case 'f':
printf("Get configuration of stream endpoint with seid %d\n", remote_seid);
avdtp_source_get_configuration(avdtp_cid, remote_seid);
printf("Get configuration of stream endpoint with seid %d\n", media_tracker.remote_seid);
avdtp_source_get_configuration(media_tracker.avdtp_cid, media_tracker.remote_seid);
break;
case 's':
printf("Set configuration of stream endpoint with seid %d\n", remote_seid);
printf("Set configuration of stream endpoint with seid %d\n", media_tracker.remote_seid);
remote_configuration_bitmap = store_bit16(remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
remote_configuration.media_codec.media_type = AVDTP_AUDIO;
remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
remote_configuration.media_codec.media_codec_information_len = sizeof(media_sbc_codec_configuration);
remote_configuration.media_codec.media_codec_information = (uint8_t *)media_sbc_codec_configuration;
avdtp_source_set_configuration(avdtp_cid, avdtp_local_seid(local_stream_endpoint), remote_seid, remote_configuration_bitmap, remote_configuration);
initialize_streaming_context(media_sbc_codec_configuration);
avdtp_source_set_configuration(media_tracker.avdtp_cid, media_tracker.local_seid, media_tracker.remote_seid, remote_configuration_bitmap, remote_configuration);
break;
case 'R':
printf("Reconfigure stream endpoint with seid %d\n", remote_seid);
printf("Reconfigure stream endpoint with seid %d\n", media_tracker.remote_seid);
remote_configuration_bitmap = store_bit16(remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
remote_configuration.media_codec.media_type = AVDTP_AUDIO;
remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
remote_configuration.media_codec.media_codec_information_len = sizeof(media_sbc_codec_reconfiguration);
remote_configuration.media_codec.media_codec_information = (uint8_t *)media_sbc_codec_reconfiguration;
avdtp_source_reconfigure(avdtp_cid, avdtp_local_seid(local_stream_endpoint), remote_seid, remote_configuration_bitmap, remote_configuration);
initialize_streaming_context(media_sbc_codec_reconfiguration);
avdtp_source_reconfigure(media_tracker.avdtp_cid, media_tracker.local_seid, media_tracker.remote_seid, remote_configuration_bitmap, remote_configuration);
break;
case 'o':
printf("Establish stream between local %d and remote %d seid\n", avdtp_local_seid(local_stream_endpoint), remote_seid);
avdtp_source_open_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint), remote_seid);
printf("Establish stream between local %d and remote %d seid\n", media_tracker.local_seid, media_tracker.remote_seid);
initialize_sbc_encoder();
avdtp_source_open_stream(media_tracker.avdtp_cid, media_tracker.local_seid, media_tracker.remote_seid);
break;
case 'm':
printf("Start stream between local %d and remote %d seid\n", avdtp_local_seid(local_stream_endpoint), avdtp_remote_seid(local_stream_endpoint));
avdtp_source_start_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
printf("Start stream between local %d and remote %d seid, \n", media_tracker.local_seid, media_tracker.remote_seid);
avdtp_source_start_stream(media_tracker.avdtp_cid, media_tracker.local_seid);
break;
case 'A':
printf("Abort stream between local %d and remote %d seid\n", avdtp_local_seid(local_stream_endpoint), avdtp_remote_seid(local_stream_endpoint));
avdtp_source_abort_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
printf("Abort stream between local %d and remote %d seid\n", media_tracker.local_seid, media_tracker.remote_seid);
avdtp_source_abort_stream(media_tracker.avdtp_cid, media_tracker.local_seid);
break;
case 'S':
printf("Release stream between local %d and remote %d seid\n", avdtp_local_seid(local_stream_endpoint), avdtp_remote_seid(local_stream_endpoint));
avdtp_source_stop_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
printf("Release stream between local %d and remote %d seid\n", media_tracker.local_seid, media_tracker.remote_seid);
avdtp_source_stop_stream(media_tracker.avdtp_cid, media_tracker.local_seid);
break;
case 'P':
printf("Susspend stream between local %d and remote %d seid\n", avdtp_local_seid(local_stream_endpoint), avdtp_remote_seid(local_stream_endpoint));
avdtp_source_suspend(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
break;
case 'x':
printf("Start streaming sine\n");
avdtp_source_start_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
printf("Susspend stream between local %d and remote %d seid\n", media_tracker.local_seid, media_tracker.remote_seid);
avdtp_source_suspend(media_tracker.avdtp_cid, media_tracker.local_seid);
break;
case 'X':
printf("Stop streaming sine\n");
avdtp_source_stop_stream(avdtp_cid, avdtp_local_seid(local_stream_endpoint));
printf("Stop streaming\n");
avdtp_source_stop_stream(media_tracker.avdtp_cid, media_tracker.local_seid);
break;
case '\n':
@ -345,6 +663,7 @@ static void stdin_process(char cmd){
}
#endif
int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
UNUSED(argc);
@ -358,9 +677,9 @@ int btstack_main(int argc, const char * argv[]){
// Initialize AVDTP Sink
avdtp_source_init(&a2dp_source_context);
avdtp_source_register_packet_handler(&packet_handler);
local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, AVDTP_AUDIO);
avdtp_source_register_media_transport_category(avdtp_local_seid(local_stream_endpoint));
avdtp_source_register_media_codec_category(avdtp_local_seid(local_stream_endpoint), AVDTP_AUDIO, AVDTP_CODEC_SBC, (uint8_t *)media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities));
media_tracker.local_seid = a2dp_source_create_stream_endpoint(AVDTP_AUDIO, AVDTP_CODEC_SBC, (uint8_t *)media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), (uint8_t *)media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration));
media_tracker.remote_seid = 1;
// Initialize SDP
sdp_init();