diff --git a/example/a2dp_source_demo.c b/example/a2dp_source_demo.c index 9484d55b6..da062740d 100644 --- a/example/a2dp_source_demo.c +++ b/example/a2dp_source_demo.c @@ -436,6 +436,18 @@ static void a2dp_demo_timer_stop(a2dp_media_sending_context_t * context){ btstack_run_loop_remove_timer(&context->audio_timer); } +static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t * configuration){ + printf("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); +} + + static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ UNUSED(channel); UNUSED(size); @@ -494,6 +506,26 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length; printf("A2DP Source: Received SBC codec configuration, sampling frequency %u.\n", sbc_configuration.sampling_frequency); + // Adapt Bluetooth spec definition to SBC Encoder expected input + sbc_configuration.allocation_method -= 1; + sbc_configuration.num_channels = 2; + switch (sbc_configuration.channel_mode){ + case AVDTP_SBC_JOINT_STEREO: + sbc_configuration.channel_mode = 3; + break; + case AVDTP_SBC_STEREO: + sbc_configuration.channel_mode = 2; + break; + case AVDTP_SBC_DUAL_CHANNEL: + sbc_configuration.channel_mode = 1; + break; + case AVDTP_SBC_MONO: + sbc_configuration.channel_mode = 0; + sbc_configuration.num_channels = 1; + break; + } + dump_sbc_configuration(&sbc_configuration); + btstack_sbc_encoder_init(&sbc_encoder_state, SBC_MODE_STANDARD, sbc_configuration.block_length, sbc_configuration.subbands, sbc_configuration.allocation_method, sbc_configuration.sampling_frequency, diff --git a/src/classic/a2dp_source.c b/src/classic/a2dp_source.c index 3efabab24..25c735f88 100644 --- a/src/classic/a2dp_source.c +++ b/src/classic/a2dp_source.c @@ -213,7 +213,6 @@ static void a2dp_signaling_emit_reconfigured(btstack_packet_handler_t callback, (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); } - static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ UNUSED(channel); UNUSED(size); @@ -278,16 +277,6 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe 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; - // printf(" - num_channels: %d\n", num_channels); - printf(" - sampling_frequency: %d\n", sampling_frequency); - printf(" - channel_mode: %d\n", channel_mode); - printf(" - block_length: %d\n", block_length); - printf(" - subbands: %d\n", subbands); - printf(" - allocation_method: %d\n", allocation_method); - printf(" - bitpool_value orig [%d, %d] \n", avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet), avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); - printf(" - bitpool_value [%d, %d] \n", min_bitpool_value, max_bitpool_value); - printf("\n"); - app_state = A2DP_W2_SET_CONFIGURATION; break; } @@ -327,13 +316,15 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ // TODO check cid sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); + sc.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(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.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet); 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); + sc.min_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); // TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); log_info("A2DP received SBC Config: sample rate %u, max bitpool %u.", sc.sampling_frequency, sc.max_bitpool_value); + log_info("A2DP received SBC Config: sample rate %u, max bitpool %u.", sc.sampling_frequency, sc.max_bitpool_value); app_state = A2DP_W2_OPEN_STREAM_WITH_SEID; a2dp_signaling_emit_media_codec_sbc(a2dp_source_context.a2dp_callback, packet, size); break;