mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-10 06:40:23 +00:00
avdtp source: move audio buffer to the app
This commit is contained in:
parent
1bb14c3802
commit
af68b735bb
@ -190,14 +190,6 @@ avdtp_connection_t * avdtp_create_connection(bd_addr_t remote_addr, avdtp_contex
|
||||
return connection;
|
||||
}
|
||||
|
||||
void avdtp_init_audio_buffer(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * storage, int storage_size){
|
||||
if (!stream_endpoint){
|
||||
printf("cannot init audio buffer, no stream_endpoint\n");
|
||||
return;
|
||||
}
|
||||
btstack_ring_buffer_init(&stream_endpoint->audio_ring_buffer, storage, storage_size);
|
||||
}
|
||||
|
||||
void avdtp_init_sbc_buffer(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * storage, int storage_size){
|
||||
if (!stream_endpoint){
|
||||
printf("cannot init audio buffer, no stream_endpoint\n");
|
||||
|
@ -454,9 +454,7 @@ typedef struct avdtp_stream_endpoint {
|
||||
uint32_t acc_num_missed_samples;
|
||||
btstack_sbc_encoder_state_t sbc_encoder_state;
|
||||
uint16_t sequence_number;
|
||||
uint32_t fill_audio_ring_buffer_timeout_ms;
|
||||
|
||||
btstack_ring_buffer_t audio_ring_buffer;
|
||||
btstack_ring_buffer_t sbc_ring_buffer;
|
||||
} avdtp_stream_endpoint_t;
|
||||
|
||||
@ -496,9 +494,7 @@ void avdtp_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_
|
||||
void avdtp_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration, avdtp_context_t * context);
|
||||
void avdtp_suspend(uint16_t con_handle, uint8_t acp_seid, avdtp_context_t * context);
|
||||
|
||||
void avdtp_init_audio_buffer(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * storage, int storage_size);
|
||||
void avdtp_init_sbc_buffer(avdtp_stream_endpoint_t * stream_endpoint, uint8_t * storage, int storage_size);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -71,7 +71,8 @@ AVDTP += \
|
||||
avdtp_sink.c \
|
||||
btstack_ring_buffer.c \
|
||||
|
||||
AVDTP_TESTS = avdtp_source_demo avdtp_sink_test portaudio_test sine_encode_decode_ring_buffer_test sine_encode_decode_test sine_encode_decode_performance_test
|
||||
AVDTP_TESTS = avdtp_source_demo avdtp_sink_test portaudio_test
|
||||
#sine_encode_decode_ring_buffer_test sine_encode_decode_test sine_encode_decode_performance_test
|
||||
|
||||
CORE_OBJ = $(CORE:.c=.o)
|
||||
COMMON_OBJ = $(COMMON:.c=.o)
|
||||
|
@ -468,8 +468,10 @@ typedef struct {
|
||||
int right_phase;
|
||||
} paTestData;
|
||||
|
||||
static uint32_t fill_audio_ring_buffer_timeout_ms = 50;
|
||||
|
||||
static uint8_t audio_samples_storage[44100*4]; // 1s buffer
|
||||
// static btstack_ring_buffer_t audio_ring_buffer;
|
||||
static btstack_ring_buffer_t audio_ring_buffer;
|
||||
|
||||
static uint8_t sbc_samples_storage[44100*4];
|
||||
// static btstack_ring_buffer_t sbc_ring_buffer;
|
||||
@ -493,15 +495,15 @@ static void avdtp_source_stream_endpoint_run(avdtp_stream_endpoint_t * stream_en
|
||||
int audio_bytes_to_read = num_audio_samples_to_read * BYTES_PER_AUDIO_SAMPLE;
|
||||
|
||||
// printf("run: audio_bytes_to_read: %d\n", audio_bytes_to_read);
|
||||
// printf(" audio buf, bytes available: %d\n", btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer));
|
||||
// printf(" audio buf, bytes available: %d\n", btstack_ring_buffer_bytes_available(&audio_ring_buffer));
|
||||
// printf(" sbc buf, bytes free: %d\n", btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer));
|
||||
|
||||
while (btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer) >= audio_bytes_to_read
|
||||
while (btstack_ring_buffer_bytes_available(&audio_ring_buffer) >= audio_bytes_to_read
|
||||
&& btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= 120){ // TODO use real value
|
||||
|
||||
uint32_t number_of_bytes_read = 0;
|
||||
uint8_t pcm_frame[256*BYTES_PER_AUDIO_SAMPLE];
|
||||
btstack_ring_buffer_read(&stream_endpoint->audio_ring_buffer, pcm_frame, audio_bytes_to_read, &number_of_bytes_read);
|
||||
btstack_ring_buffer_read(&audio_ring_buffer, pcm_frame, audio_bytes_to_read, &number_of_bytes_read);
|
||||
// printf(" num audio bytes read %d\n", number_of_bytes_read);
|
||||
btstack_sbc_encoder_process_data((int16_t *) pcm_frame);
|
||||
|
||||
@ -517,15 +519,15 @@ static void avdtp_source_stream_endpoint_run(avdtp_stream_endpoint_t * stream_en
|
||||
}
|
||||
}
|
||||
|
||||
static void fill_audio_ring_buffer(void *userData, int num_samples_to_write, avdtp_stream_endpoint_t * stream_endpoint){
|
||||
static void fill_audio_ring_buffer(void *userData, int num_samples_to_write){
|
||||
paTestData *data = (paTestData*)userData;
|
||||
int count = 0;
|
||||
while (btstack_ring_buffer_bytes_free(&stream_endpoint->audio_ring_buffer) >= BYTES_PER_AUDIO_SAMPLE && count < num_samples_to_write){
|
||||
while (btstack_ring_buffer_bytes_free(&audio_ring_buffer) >= BYTES_PER_AUDIO_SAMPLE && count < num_samples_to_write){
|
||||
uint8_t write_data[BYTES_PER_AUDIO_SAMPLE];
|
||||
*(int16_t*)&write_data[0] = data->source[data->left_phase];
|
||||
*(int16_t*)&write_data[2] = data->source[data->right_phase];
|
||||
|
||||
btstack_ring_buffer_write(&stream_endpoint->audio_ring_buffer, write_data, BYTES_PER_AUDIO_SAMPLE);
|
||||
btstack_ring_buffer_write(&audio_ring_buffer, write_data, BYTES_PER_AUDIO_SAMPLE);
|
||||
count++;
|
||||
|
||||
data->left_phase += 1;
|
||||
@ -541,11 +543,11 @@ static void fill_audio_ring_buffer(void *userData, int num_samples_to_write, avd
|
||||
|
||||
static void avdtp_fill_audio_ring_buffer_timeout_handler(btstack_timer_source_t * timer){
|
||||
avdtp_stream_endpoint_t * stream_endpoint = btstack_run_loop_get_timer_context(timer);
|
||||
btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint->fill_audio_ring_buffer_timeout_ms); // 2 seconds timeout
|
||||
btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout_ms); // 2 seconds timeout
|
||||
btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer);
|
||||
uint32_t now = btstack_run_loop_get_time_ms();
|
||||
|
||||
uint32_t update_period_ms = stream_endpoint->fill_audio_ring_buffer_timeout_ms;
|
||||
uint32_t update_period_ms = fill_audio_ring_buffer_timeout_ms;
|
||||
if (stream_endpoint->time_audio_data_sent > 0){
|
||||
update_period_ms = now - stream_endpoint->time_audio_data_sent;
|
||||
}
|
||||
@ -557,7 +559,7 @@ static void avdtp_fill_audio_ring_buffer_timeout_handler(btstack_timer_source_t
|
||||
stream_endpoint->acc_num_missed_samples -= 1000;
|
||||
}
|
||||
|
||||
fill_audio_ring_buffer(&sin_data, num_samples, stream_endpoint);
|
||||
fill_audio_ring_buffer(&sin_data, num_samples);
|
||||
stream_endpoint->time_audio_data_sent = now;
|
||||
|
||||
avdtp_source_stream_endpoint_run(stream_endpoint);
|
||||
@ -568,7 +570,7 @@ static void avdtp_fill_audio_ring_buffer_timer_start(avdtp_stream_endpoint_t * s
|
||||
btstack_run_loop_remove_timer(&stream_endpoint->fill_audio_ring_buffer_timer);
|
||||
btstack_run_loop_set_timer_handler(&stream_endpoint->fill_audio_ring_buffer_timer, avdtp_fill_audio_ring_buffer_timeout_handler);
|
||||
btstack_run_loop_set_timer_context(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint);
|
||||
btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint->fill_audio_ring_buffer_timeout_ms); // 50 ms timeout
|
||||
btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout_ms); // 50 ms timeout
|
||||
btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer);
|
||||
}
|
||||
|
||||
@ -593,13 +595,19 @@ static void avdtp_source_stream_data_stop(avdtp_stream_endpoint_t * stream_endpo
|
||||
log_error("no stream_endpoint found");
|
||||
return;
|
||||
}
|
||||
if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING) {
|
||||
printf("stream_endpoint in wrong state %d\n", stream_endpoint->state);
|
||||
return;
|
||||
switch (stream_endpoint->state){
|
||||
case AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND:
|
||||
case AVDTP_STREAM_ENDPOINT_STREAMING:
|
||||
// TODO: initialize randomly sequence number
|
||||
stream_endpoint->sequence_number = 0;
|
||||
stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING;
|
||||
avdtp_fill_audio_ring_buffer_timer_stop(stream_endpoint);
|
||||
break;
|
||||
default:
|
||||
printf("stream_endpoint in wrong state %d\n", stream_endpoint->state);
|
||||
return;
|
||||
}
|
||||
// TODO: initialize randomly sequence number
|
||||
stream_endpoint->sequence_number = 0;
|
||||
avdtp_fill_audio_ring_buffer_timer_stop(stream_endpoint);
|
||||
|
||||
}
|
||||
|
||||
static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
|
||||
@ -707,7 +715,7 @@ int btstack_main(int argc, const char * argv[]){
|
||||
memset(audio_samples_storage, 0, sizeof(audio_samples_storage));
|
||||
memset(sbc_samples_storage, 0, sizeof(sbc_samples_storage));
|
||||
|
||||
avdtp_init_audio_buffer(local_stream_endpoint, audio_samples_storage, sizeof(audio_samples_storage));
|
||||
btstack_ring_buffer_init(&audio_ring_buffer, audio_samples_storage, sizeof(audio_samples_storage));
|
||||
avdtp_init_sbc_buffer(local_stream_endpoint, sbc_samples_storage, sizeof(sbc_samples_storage));
|
||||
|
||||
/* initialise sinusoidal wavetable */
|
||||
|
Loading…
x
Reference in New Issue
Block a user