test/pts: link against portaudio, rework avdtp_sink_test

This commit is contained in:
Matthias Ringwald 2021-01-22 12:02:48 +01:00
parent 2b6ecffc7f
commit 35015a8866
2 changed files with 208 additions and 271 deletions

View File

@ -10,6 +10,13 @@ include_directories(${LIBUSB_INCLUDE_DIRS})
link_directories(${LIBUSB_LIBRARY_DIRS}) link_directories(${LIBUSB_LIBRARY_DIRS})
link_libraries(${LIBUSB_LIBRARIES}) link_libraries(${LIBUSB_LIBRARIES})
# portaudio
pkg_check_modules(PORTAUDIO REQUIRED portaudio-2.0)
include_directories(${PORTAUDIO_INCLUDE_DIRS})
link_directories(${PORTAUDIO_LIBRARY_DIRS})
link_libraries(${PORTAUDIO_LIBRARIES})
add_compile_definitions(HAVE_PORTAUDIO)
include_directories(../../3rd-party/micro-ecc) include_directories(../../3rd-party/micro-ecc)
include_directories(../../3rd-party/bluedroid/decoder/include) include_directories(../../3rd-party/bluedroid/decoder/include)
include_directories(../../3rd-party/bluedroid/encoder/include) include_directories(../../3rd-party/bluedroid/encoder/include)

View File

@ -47,57 +47,30 @@
#include "btstack_resample.h" #include "btstack_resample.h"
#include "btstack_ring_buffer.h" #include "btstack_ring_buffer.h"
#ifdef HAVE_PORTAUDIO
#include <portaudio.h>
#endif
#ifdef HAVE_POSIX_FILE_IO
#include "wav_util.h" #include "wav_util.h"
#define STORE_SBC_TO_SBC_FILE
#define STORE_SBC_TO_WAV_FILE
#endif
#if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) //
#define DECODE_SBC // Configuration
#endif //
#define NUM_CHANNELS 2 #define NUM_CHANNELS 2
#define BYTES_PER_FRAME (2*NUM_CHANNELS) #define BYTES_PER_FRAME (2*NUM_CHANNELS)
// Playback
#define SAMPLE_RATE 48000
#define PREBUFFER_MS 100
#define AUDIO_BUFFER_SIZE (AUDIO_BUFFER_MS*SAMPLE_RATE/BYTES_PER_FRAME)
#define AUDIO_BUFFER_MS 200
#define MAX_SBC_FRAME_SIZE 120 #define MAX_SBC_FRAME_SIZE 120
// SBC Decoder for WAV file or PortAudio
#ifdef DECODE_SBC
static btstack_sbc_decoder_state_t state;
static btstack_sbc_mode_t mode = SBC_MODE_STANDARD;
#endif
// PortAudio - live playback
#if defined(HAVE_PORTAUDIO)
#define PREBUFFER_MS 200
#define PA_SAMPLE_TYPE paInt16
#define SAMPLE_RATE 48000
#define FRAMES_PER_BUFFER 128
#define PREBUFFER_BYTES (PREBUFFER_MS*SAMPLE_RATE/1000*BYTES_PER_FRAME)
static int audio_stream_started = 0;
#endif
static int media_initialized = 0;
static btstack_resample_t resample_instance;
// ring buffer for SBC Frames // ring buffer for SBC Frames
// below 30: add samples, 30-40: fine, above 40: drop samples // below 30: add samples, 30-40: fine, above 40: drop samples
#define OPTIMAL_FRAMES_MIN 30 #define OPTIMAL_FRAMES_MIN 30
#define OPTIMAL_FRAMES_MAX 40 #define OPTIMAL_FRAMES_MAX 40
#define ADDITIONAL_FRAMES 20 #define ADDITIONAL_FRAMES 20
static uint8_t sbc_frame_storage[(OPTIMAL_FRAMES_MAX + ADDITIONAL_FRAMES) * MAX_SBC_FRAME_SIZE];
static btstack_ring_buffer_t sbc_frame_ring_buffer;
static unsigned int sbc_frame_size;
// rest buffer for not fully used sbc frames, with additional frames for resampling static const char * wav_filename = "avdtp_sink_test.wav";
static uint8_t decoded_audio_storage[(128+16) * BYTES_PER_FRAME];
static btstack_ring_buffer_t decoded_audio_ring_buffer;
typedef struct { typedef struct {
// bitmaps // bitmaps
@ -122,30 +95,55 @@ typedef struct {
int max_bitpool_value; int max_bitpool_value;
} avdtp_media_codec_configuration_sbc_t; } avdtp_media_codec_configuration_sbc_t;
typedef struct {
uint8_t num_channels;
uint32_t sampling_frequency;
} playback_configuration_t;
// REMOTE Device
// mac 2011: static const char * device_addr_string = "04:0C:CE:E4:85:D3"; // mac 2011: static const char * device_addr_string = "04:0C:CE:E4:85:D3";
// mac 2013: static const char * device_addr_string = "84:38:35:65:d1:15"; // mac 2013: static const char * device_addr_string = "84:38:35:65:d1:15";
// phone 2013: static const char * device_addr_string = "D8:BB:2C:DF:F0:F2"; // phone 2013: static const char * device_addr_string = "D8:BB:2C:DF:F0:F2";
// minijambox: static const char * device_addr_string = "00:21:3C:AC:F7:38"; // minijambox: 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"; // 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"; // bt dongle: static const char * device_addr_string = "00:15:83:5F:9D:46";
// PTS:
static const char * device_addr_string = "00:1B:DC:08:E2:5C"; static const char * device_addr_string = "00:1B:DC:08:E2:5C";
static bd_addr_t device_addr; static bd_addr_t device_addr;
static uint8_t is_cmd_triggered_localy = 0;
static uint8_t is_media_header_reported_once = 0;
static uint16_t avdtp_cid = 0;
static uint8_t local_seid = 0;
static uint8_t sdp_avdtp_sink_service_buffer[150]; static uint8_t sdp_avdtp_sink_service_buffer[150];
static adtvp_media_codec_information_sbc_t sbc_capability; // stack stuff
static avdtp_media_codec_configuration_sbc_t sbc_configuration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static uint16_t avdtp_cid;
static uint8_t local_seid; static uint8_t local_seid;
static uint8_t remote_seid; static uint8_t remote_seid;
// static avdtp_sep_t sep;
static uint8_t is_cmd_triggered_locally = 0;
static uint8_t is_media_header_reported_once = 0;
// playback
static int playback_initialized = 0;
static bool playback_active = false;
static playback_configuration_t playback_configuration;
// large audio buffer to store up to AUDIO_MS of audio data
static uint8_t decoded_audio_storage[AUDIO_BUFFER_SIZE];
static btstack_ring_buffer_t decoded_audio_ring_buffer;
// active codec
static avdtp_media_codec_type_t codec_type;
// SBC
static btstack_sbc_decoder_state_t state;
static btstack_sbc_mode_t mode = SBC_MODE_STANDARD;
static adtvp_media_codec_information_sbc_t sbc_capability;
static avdtp_media_codec_configuration_sbc_t sbc_configuration;
static unsigned int sbc_frame_size;
// xxx
// ..
static int16_t * request_buffer; static int16_t * request_buffer;
static int request_frames; static int request_frames;
@ -153,162 +151,132 @@ static int request_frames;
static uint16_t remote_configuration_bitmap; static uint16_t remote_configuration_bitmap;
static avdtp_capabilities_t remote_configuration; static avdtp_capabilities_t remote_configuration;
static btstack_packet_callback_registration_t hci_event_callback_registration; // prototypes
#if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) static void handle_pcm_data(int16_t * data, uint16_t num_samples, uint8_t num_channels, int sample_rate, void * context);
static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context); static int read_media_data_header(uint8_t * packet, uint16_t size, uint16_t * offset, avdtp_media_packet_header_t * media_header);
#endif static int read_sbc_header(uint8_t * packet, uint16_t size, uint16_t * offset, avdtp_sbc_codec_header_t * sbc_header);
static void media_processing_start(void){
if (!media_initialized) return;
// setup audio playback
const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
if (audio){
audio->start_stream();
}
audio_stream_started = 1;
}
static void media_processing_pause(void){
if (!media_initialized) return;
// stop audio playback
audio_stream_started = 0;
const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
if (audio){
audio->stop_stream();
}
}
// Playback: PCM ring buffer
static void playback_handler(int16_t * buffer, uint16_t num_audio_frames){ static void playback_handler(int16_t * buffer, uint16_t num_audio_frames){
int wav_samples = num_audio_frames * playback_configuration.num_channels;
#ifdef STORE_TO_WAV_FILE uint32_t bytes_available = btstack_ring_buffer_bytes_available(&decoded_audio_ring_buffer);
int wav_samples = num_audio_frames * NUM_CHANNELS; uint32_t bytes_requested = wav_samples * 2;
int16_t * wav_buffer = buffer; uint32_t bytes_start = playback_configuration.sampling_frequency * PREBUFFER_MS / 1000 * playback_configuration.num_channels;
#endif bool sufficient_data_now = bytes_available > bytes_requested;
bool sufficient_data_start = bytes_available > bytes_start;
// called from lower-layer but guaranteed to be on main thread // check for underrun
if (sbc_frame_size == 0){ if (playback_active && !sufficient_data_now){
printf("Playback: Underrun, pause (have %u, need %u\n", bytes_available, bytes_requested);
playback_active = false;
}
// check for start
if (!playback_active && sufficient_data_start){
// printf("Playback: start - have %u, start %u\n", bytes_available, bytes_start);
playback_active = true;
}
// play silence
if (!playback_active){
playback_active = false;
memset(buffer, 0, num_audio_frames * BYTES_PER_FRAME); memset(buffer, 0, num_audio_frames * BYTES_PER_FRAME);
return; return;
} }
// first fill from resampled audio // read data from ring buffer
uint32_t bytes_read; uint32_t bytes_read;
btstack_ring_buffer_read(&decoded_audio_ring_buffer, (uint8_t *) buffer, num_audio_frames * BYTES_PER_FRAME, &bytes_read); btstack_ring_buffer_read(&decoded_audio_ring_buffer, (uint8_t *) buffer, num_audio_frames * BYTES_PER_FRAME, &bytes_read);
buffer += bytes_read / NUM_CHANNELS;
num_audio_frames -= bytes_read / BYTES_PER_FRAME;
// then start decoding sbc frames using request_* globals
request_buffer = buffer;
request_frames = num_audio_frames;
while (request_frames && btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) >= sbc_frame_size){
// decode frame
uint8_t sbc_frame[MAX_SBC_FRAME_SIZE];
btstack_ring_buffer_read(&sbc_frame_ring_buffer, sbc_frame, sbc_frame_size, &bytes_read);
btstack_sbc_decoder_process_data(&state, 0, sbc_frame, sbc_frame_size);
} }
#ifdef STORE_TO_WAV_FILE static int playback_init(playback_configuration_t * configuration){
audio_frame_count += num_audio_frames; if (playback_initialized) return 0;
wav_writer_write_int16(wav_samples, wav_buffer); playback_initialized = 1;
#endif
}
playback_active = false;
static int media_processing_init(avdtp_media_codec_configuration_sbc_t configuration){
if (media_initialized) return 0;
btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL);
#ifdef STORE_TO_WAV_FILE
wav_writer_open(wav_filename, configuration.num_channels, configuration.sampling_frequency);
#endif
#ifdef STORE_TO_SBC_FILE
sbc_file = fopen(sbc_filename, "wb");
#endif
btstack_ring_buffer_init(&sbc_frame_ring_buffer, sbc_frame_storage, sizeof(sbc_frame_storage));
btstack_ring_buffer_init(&decoded_audio_ring_buffer, decoded_audio_storage, sizeof(decoded_audio_storage)); btstack_ring_buffer_init(&decoded_audio_ring_buffer, decoded_audio_storage, sizeof(decoded_audio_storage));
btstack_resample_init(&resample_instance, configuration.num_channels);
// wav writer
printf("WAV Writer: open %s\n", wav_filename);
wav_writer_open(wav_filename, configuration->num_channels, configuration->sampling_frequency);
// setup audio playback // setup audio playback
const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
if (audio){ if (audio){
audio->init(NUM_CHANNELS, configuration.sampling_frequency, &playback_handler); printf("Open audio stream\n");
audio->init(NUM_CHANNELS, configuration->sampling_frequency, &playback_handler);
audio->start_stream();
} }
audio_stream_started = 0;
media_initialized = 1;
return 0; return 0;
} }
static void playback_start(void){
}
static void media_processing_close(void){ static void playback_pause(void){
if (!media_initialized) return; if (!playback_initialized) return;
media_initialized = 0; // discard audio, play silence
audio_stream_started = 0; playback_active = false;
sbc_frame_size = 0; btstack_ring_buffer_init(&decoded_audio_ring_buffer, decoded_audio_storage, sizeof(decoded_audio_storage));
}
#ifdef STORE_TO_WAV_FILE static void playback_close(void){
if (!playback_initialized) return;
playback_initialized = 0;
playback_active = false;
// close wav
printf("WAV Writer: close %s\n", wav_filename);
wav_writer_close(); wav_writer_close();
uint32_t total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr;
printf("WAV Writer: Decoding done. Processed %u SBC frames:\n - %d good\n - %d bad\n", total_frames_nr, state.good_frames_nr, total_frames_nr - state.good_frames_nr);
printf("WAV Writer: Wrote %u audio frames to wav file: %s\n", audio_frame_count, wav_filename);
#endif
#ifdef STORE_TO_SBC_FILE
fclose(sbc_file);
#endif
// stop audio playback // stop audio playback
const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
if (audio){ if (audio){
printf("close stream\n"); printf("Open audio stream\n");
audio->close(); audio->close();
} }
} }
static int read_media_data_header(uint8_t * packet, int size, int * offset, avdtp_media_packet_header_t * media_header); static void playback_queue_audio(int16_t * data, int num_audio_frames, int num_channels){
static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header); // write to wav file
wav_writer_write_int16(num_audio_frames * num_channels, data);
static void handle_pcm_data(int16_t * data, int num_audio_frames, int num_channels, int sample_rate, void * context){ // store in audio ring buffer
UNUSED(sample_rate); int status = btstack_ring_buffer_write(&decoded_audio_ring_buffer, (uint8_t *)data, num_audio_frames * num_channels * 2);
UNUSED(context);
UNUSED(num_channels); // must be stereo == 2
const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance();
if (!audio_sink){
#ifdef STORE_TO_WAV_FILE
audio_frame_count += num_audio_frames;
wav_writer_write_int16(num_audio_frames * NUM_CHANNELS, data);
#endif
return;
}
// resample into request buffer - add some additional space for resampling
int16_t output_buffer[(128+16) * NUM_CHANNELS]; // 16 * 8 * 2
uint32_t resampled_frames = btstack_resample_block(&resample_instance, data, num_audio_frames, output_buffer);
// store data in btstack_audio buffer first
int frames_to_copy = btstack_min(resampled_frames, request_frames);
memcpy(request_buffer, output_buffer, frames_to_copy * BYTES_PER_FRAME);
request_frames -= frames_to_copy;
request_buffer += frames_to_copy * NUM_CHANNELS;
// and rest in ring buffer
int frames_to_store = resampled_frames - frames_to_copy;
if (frames_to_store){
int status = btstack_ring_buffer_write(&decoded_audio_ring_buffer, (uint8_t *)&output_buffer[frames_to_copy * NUM_CHANNELS], frames_to_store * BYTES_PER_FRAME);
if (status){ if (status){
printf("Error storing samples in PCM ring buffer!!!\n"); printf("Error storing samples in PCM ring buffer!!!\n");
} }
} log_info("buffer: store %u frames, have %u bytes", num_audio_frames, btstack_ring_buffer_bytes_available(&decoded_audio_ring_buffer));
} }
static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header){ //
int sbc_header_len = 12; // without crc // SBC Codec
//
static void dump_sbc_capability(adtvp_media_codec_information_sbc_t capabilities){
printf(" - sampling_frequency: 0x%02x\n", capabilities.sampling_frequency_bitmap);
printf(" - channel_mode: 0x%02x\n", capabilities.channel_mode_bitmap);
printf(" - block_length: 0x%02x\n", capabilities.block_length_bitmap);
printf(" - subbands: 0x%02x\n", capabilities.subbands_bitmap);
printf(" - allocation_method: 0x%02x\n", capabilities.allocation_method_bitmap);
printf(" - bitpool_value [%d, %d] \n", capabilities.min_bitpool_value, capabilities.max_bitpool_value);
}
static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){
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);
}
static int read_sbc_header(uint8_t * packet, uint16_t size, uint16_t * offset, avdtp_sbc_codec_header_t * sbc_header){
int sbc_header_len = 1; // without crc
int pos = *offset; int pos = *offset;
if (size - pos < sbc_header_len){ if (size - pos < sbc_header_len){
@ -326,7 +294,31 @@ static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_c
return 1; return 1;
} }
static int read_media_data_header(uint8_t *packet, int size, int *offset, avdtp_media_packet_header_t *media_header){ static void sbc_handle_pcm_data(int16_t * data, int num_audio_frames, int num_channels, int sample_rate, void * context){
UNUSED(sample_rate);
UNUSED(context);
playback_queue_audio(data, num_audio_frames, num_channels);
}
static void handle_l2cap_media_data_packet_sbc(uint8_t *packet, uint16_t size){
uint16_t pos = 0;
avdtp_media_packet_header_t media_header;
if (!read_media_data_header(packet, size, &pos, &media_header)) return;
avdtp_sbc_codec_header_t sbc_header;
if (!read_sbc_header(packet, size, &pos, &sbc_header)) return;
btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos);
}
// Codec XXX
//
//
// A2DP Media Payload Processing
static int read_media_data_header(uint8_t *packet, u_int16_t size, uint16_t *offset, avdtp_media_packet_header_t *media_header){
int media_header_len = 12; // without crc int media_header_len = 12; // without crc
int pos = *offset; int pos = *offset;
@ -367,80 +359,16 @@ static int read_media_data_header(uint8_t *packet, int size, int *offset, avdtp_
return 1; return 1;
} }
static void dump_sbc_capability(adtvp_media_codec_information_sbc_t media_codec_sbc){
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);
}
static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){
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);
}
static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size){ static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size){
UNUSED(seid); UNUSED(seid);
int pos = 0; switch (codec_type){
case AVDTP_CODEC_SBC:
avdtp_media_packet_header_t media_header; handle_l2cap_media_data_packet_sbc(packet, size);
if (!read_media_data_header(packet, size, &pos, &media_header)) return; break;
default:
avdtp_sbc_codec_header_t sbc_header; printf("Media Codec %u not supported yet\n", codec_type);
if (!read_sbc_header(packet, size, &pos, &sbc_header)) return;
#ifdef STORE_TO_SBC_FILE
fwrite(packet+pos, size-pos, 1, sbc_file);
#endif
const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
// process data right away if there's no audio implementation active, e.g. on posix systems to store as .wav
if (!audio){
btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos);
return; return;
} }
// store sbc frame size for buffer management
sbc_frame_size = (size-pos)/ sbc_header.num_frames;
int status = btstack_ring_buffer_write(&sbc_frame_ring_buffer, packet+pos, size-pos);
if (status){
printf("Error storing samples in SBC ring buffer!!!\n");
}
// decide on audio sync drift based on number of sbc frames in queue
int sbc_frames_in_buffer = btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) / sbc_frame_size;
uint32_t resampling_factor;
// nomimal factor (fixed-point 2^16) and compensation offset
uint32_t nomimal_factor = 0x10000;
uint32_t compensation = 0x00100;
if (sbc_frames_in_buffer < OPTIMAL_FRAMES_MIN){
resampling_factor = nomimal_factor - compensation; // stretch samples
} else if (sbc_frames_in_buffer <= OPTIMAL_FRAMES_MAX){
resampling_factor = nomimal_factor; // nothing to do
} else {
resampling_factor = nomimal_factor + compensation; // compress samples
}
btstack_resample_set_factor(&resample_instance, resampling_factor);
// start stream if enough frames buffered
if (!audio_stream_started && sbc_frames_in_buffer >= OPTIMAL_FRAMES_MIN){
audio_stream_started = 1;
// setup audio playback
if (audio){
audio->start_stream();
}
}
} }
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
@ -529,7 +457,13 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
printf("Received non SBC codec, event not parsed.\n"); printf("Received non SBC codec, event not parsed.\n");
break; break;
case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
codec_type = AVDTP_CODEC_SBC;
playback_configuration.num_channels = avdtp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet);
playback_configuration.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);;
btstack_sbc_decoder_init(&state, mode, sbc_handle_pcm_data, NULL);
printf("Received SBC codec configuration: avdtp_cid 0x%02x.\n", avdtp_cid); printf("Received SBC codec configuration: avdtp_cid 0x%02x.\n", avdtp_cid);
sbc_configuration.reconfigure = avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 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.num_channels = avdtp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet);
@ -544,7 +478,6 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
avdtp_sink_delay_report(avdtp_cid, remote_seid, 100); avdtp_sink_delay_report(avdtp_cid, remote_seid, 100);
break; break;
}
case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
avdtp_cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); avdtp_cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
@ -553,47 +486,47 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
printf("Streaming connection released: avdtp_cid 0x%02x.\n", avdtp_cid); printf("Streaming connection released: avdtp_cid 0x%02x.\n", avdtp_cid);
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
is_media_header_reported_once = 0; is_media_header_reported_once = 0;
media_processing_close(); playback_close();
break; break;
case AVDTP_SUBEVENT_SIGNALING_ACCEPT:{ case AVDTP_SUBEVENT_SIGNALING_ACCEPT:{
switch (avdtp_subevent_signaling_accept_get_signal_identifier(packet)){ switch (avdtp_subevent_signaling_accept_get_signal_identifier(packet)){
case AVDTP_SI_START: case AVDTP_SI_START:
printf("Stream started\n"); printf("Stream started\n");
media_processing_close(); playback_close();
media_processing_init(sbc_configuration); playback_init(&playback_configuration);
media_processing_start(); playback_start();
break; break;
case AVDTP_SI_SUSPEND: case AVDTP_SI_SUSPEND:
printf("Stream paused\n"); printf("Stream paused\n");
media_processing_pause(); playback_pause();
break; break;
case AVDTP_SI_ABORT: case AVDTP_SI_ABORT:
case AVDTP_SI_CLOSE: case AVDTP_SI_CLOSE:
printf("Stream stoped\n"); printf("Stream stoped\n");
media_processing_close(); playback_close();
break; break;
default: default:
break; break;
} }
if (is_cmd_triggered_localy){ if (is_cmd_triggered_locally){
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
printf("AVDTP Sink command accepted\n"); printf("AVDTP Sink command accepted\n");
} }
break; break;
} }
case AVDTP_SUBEVENT_SIGNALING_REJECT: case AVDTP_SUBEVENT_SIGNALING_REJECT:
case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
if (is_cmd_triggered_localy){ if (is_cmd_triggered_locally){
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
printf("AVDTP Sink command rejected\n"); printf("AVDTP Sink command rejected\n");
} }
break; break;
default: default:
if (is_cmd_triggered_localy){ if (is_cmd_triggered_locally){
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
} }
printf("AVDTP Sink event 0x%02x not parsed\n", packet[2]); printf("AVDTP Sink event 0x%02x not parsed\n", packet[2]);
break; break;
@ -622,7 +555,6 @@ static uint8_t media_sbc_codec_reconfiguration[] = {
32, 32 32, 32
}; };
#ifdef HAVE_BTSTACK_STDIN
static void show_usage(void){ static void show_usage(void){
bd_addr_t iut_address; bd_addr_t iut_address;
gap_local_bd_addr(iut_address); gap_local_bd_addr(iut_address);
@ -639,7 +571,7 @@ static void show_usage(void){
printf("A - abort stream with %d\n", remote_seid); printf("A - abort stream with %d\n", remote_seid);
printf("P - suspend (pause) stream with %d\n", remote_seid); printf("P - suspend (pause) stream with %d\n", remote_seid);
printf("S - stop (release) stream with %d\n", remote_seid); printf("S - stop (release) stream with %d\n", remote_seid);
printf("D - send delay report"); printf("D - send delay report\n");
printf("C - disconnect\n"); printf("C - disconnect\n");
printf("Ctrl-c - exit\n"); printf("Ctrl-c - exit\n");
printf("---\n"); printf("---\n");
@ -648,7 +580,7 @@ static void show_usage(void){
static void stdin_process(char cmd){ static void stdin_process(char cmd){
uint8_t status = ERROR_CODE_SUCCESS; uint8_t status = ERROR_CODE_SUCCESS;
remote_seid = 1; remote_seid = 1;
is_cmd_triggered_localy = 1; is_cmd_triggered_locally = 1;
switch (cmd){ switch (cmd){
case 'c': case 'c':
printf("Establish AVDTP Sink connection to %s\n", device_addr_string); printf("Establish AVDTP Sink connection to %s\n", device_addr_string);
@ -719,10 +651,10 @@ static void stdin_process(char cmd){
break; break;
case '\n': case '\n':
case '\r': case '\r':
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
break; break;
default: default:
is_cmd_triggered_localy = 0; is_cmd_triggered_locally = 0;
show_usage(); show_usage();
break; break;
@ -731,7 +663,6 @@ static void stdin_process(char cmd){
printf("AVDTP Sink cmd \'%c\' failed, status 0x%02x\n", cmd, status); printf("AVDTP Sink cmd \'%c\' failed, status 0x%02x\n", cmd, status);
} }
} }
#endif
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
@ -772,11 +703,10 @@ int btstack_main(int argc, const char * argv[]){
gap_discoverable_control(1); gap_discoverable_control(1);
gap_set_class_of_device(0x200408); gap_set_class_of_device(0x200408);
#ifdef HAVE_BTSTACK_STDIN
// parse human readable Bluetooth address // parse human readable Bluetooth address
sscanf_bd_addr(device_addr_string, device_addr); sscanf_bd_addr(device_addr_string, device_addr);
btstack_stdin_setup(stdin_process); btstack_stdin_setup(stdin_process);
#endif
// turn on! // turn on!
hci_power_control(HCI_POWER_ON); hci_power_control(HCI_POWER_ON);
return 0; return 0;