From f3eb711492475475eb30b40c240919693d1a4ffa Mon Sep 17 00:00:00 2001 From: Milanka Ringwald Date: Tue, 15 Sep 2020 14:20:00 +0200 Subject: [PATCH] a2dp_source_demo: cleanup console output --- example/a2dp_source_demo.c | 87 +++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/example/a2dp_source_demo.c b/example/a2dp_source_demo.c index 6e41399dc..640e5545c 100644 --- a/example/a2dp_source_demo.c +++ b/example/a2dp_source_demo.c @@ -175,6 +175,8 @@ static const char * device_addr_string = "00:21:3C:AC:F7:38"; // Sony MDR-ZX330BT static const char * device_addr_string = "00:18:09:28:50:18"; // Panda (BM6) static const char * device_addr_string = "4F:3F:66:52:8B:E0"; // BeatsX: static const char * device_addr_string = "DC:D3:A2:89:57:FB"; +// Green mushroom: static const char * device_addr_string = "41:42:2A:ED:D6:F3"; + static bd_addr_t device_addr; static uint8_t sdp_a2dp_source_service_buffer[150]; @@ -190,7 +192,8 @@ static a2dp_media_sending_context_t media_tracker; static stream_data_source_t data_source; static int sine_phase; -static int sample_rate = 44100; +static int current_sample_rate = 44100; +static int new_sample_rate = 44100; static int hxcmod_initialized; static modcontext mod_context; @@ -269,7 +272,7 @@ static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channe static void stdin_process(char cmd); #endif -static void a2dp_demo_reconfigure_sample_rate(int new_sample_rate); +static void a2dp_demo_hexcmod_configure_sample_rate(int sample_rate); static int a2dp_source_and_avrcp_services_init(void){ // request role change on reconnecting headset to always use them in slave mode @@ -333,7 +336,7 @@ static int a2dp_source_and_avrcp_services_init(void){ hci_event_callback_registration.callback = &hci_packet_handler; hci_add_event_handler(&hci_event_callback_registration); - a2dp_demo_reconfigure_sample_rate(sample_rate); + a2dp_demo_hexcmod_configure_sample_rate(current_sample_rate); #ifdef HAVE_BTSTACK_STDIN // Parse human readable Bluetooth address. @@ -344,7 +347,7 @@ static int a2dp_source_and_avrcp_services_init(void){ } /* LISTING_END */ -static void a2dp_demo_reconfigure_sample_rate(int new_sample_rate){ +static void a2dp_demo_hexcmod_configure_sample_rate(int sample_rate){ if (!hxcmod_initialized){ hxcmod_initialized = hxcmod_init(&mod_context); if (!hxcmod_initialized) { @@ -352,11 +355,11 @@ static void a2dp_demo_reconfigure_sample_rate(int new_sample_rate){ return; } } - sample_rate = new_sample_rate; + current_sample_rate = sample_rate; media_tracker.sbc_storage_count = 0; media_tracker.samples_ready = 0; hxcmod_unload(&mod_context); - hxcmod_setcfg(&mod_context, sample_rate, 16, 1, 1, 1); + hxcmod_setcfg(&mod_context, current_sample_rate, 16, 1, 1, 1); hxcmod_load(&mod_context, (void *) &mod_data, mod_len); } @@ -372,7 +375,7 @@ static void a2dp_demo_send_media_packet(void){ 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++){ - switch (sample_rate){ + switch (current_sample_rate){ case 44100: pcm_buffer[count * 2] = sine_int16_44100[sine_phase]; pcm_buffer[count * 2 + 1] = sine_int16_44100[sine_phase]; @@ -458,8 +461,8 @@ static void a2dp_demo_audio_timeout_handler(btstack_timer_source_t * timer){ update_period_ms = now - context->time_audio_data_sent; } - uint32_t num_samples = (update_period_ms * sample_rate) / 1000; - context->acc_num_missed_samples += (update_period_ms * sample_rate) % 1000; + uint32_t num_samples = (update_period_ms * current_sample_rate) / 1000; + context->acc_num_missed_samples += (update_period_ms * current_sample_rate) % 1000; while (context->acc_num_missed_samples >= 1000){ num_samples++; @@ -560,14 +563,15 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui break; } media_tracker.a2dp_cid = cid; - media_tracker.volume = 64; + media_tracker.volume = 32; - printf("A2DP Source: Connected to address %s, a2dp cid 0x%02x, local seid %d.\n", bd_addr_to_str(address), media_tracker.a2dp_cid, media_tracker.local_seid); + printf("A2DP Source: Connected to address %s, a2dp cid 0x%02x, local seid 0x%02x.\n", bd_addr_to_str(address), media_tracker.a2dp_cid, media_tracker.local_seid); break; case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet); if (cid != media_tracker.a2dp_cid) return; + media_tracker.remote_seid = a2dp_subevent_signaling_media_codec_sbc_configuration_get_acp_seid(packet); sbc_configuration.reconfigure = a2dp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); @@ -580,11 +584,10 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui sbc_configuration.min_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); sbc_configuration.max_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length; - printf("A2DP Source: Received SBC codec configuration, sampling frequency %u, a2dp_cid 0x%02x, local seid %d (expected %d), remote seid %d .\n", + printf("A2DP Source: Received SBC codec configuration, sampling frequency %u, a2dp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x.\n", sbc_configuration.sampling_frequency, cid, a2dp_subevent_signaling_media_codec_sbc_configuration_get_int_seid(packet), - media_tracker.local_seid, - media_tracker.remote_seid); + a2dp_subevent_signaling_media_codec_sbc_configuration_get_acp_seid(packet)); // Adapt Bluetooth spec definition to SBC Encoder expected input sbc_configuration.allocation_method -= 1; @@ -639,17 +642,10 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui local_seid = a2dp_subevent_stream_established_get_local_seid(packet); cid = a2dp_subevent_stream_established_get_a2dp_cid(packet); - printf("A2DP_SUBEVENT_STREAM_ESTABLISHED: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid %d (expected %d), remote_seid %d (expected %d)\n", - media_tracker.a2dp_cid, cid, - local_seid, media_tracker.local_seid, - a2dp_subevent_stream_established_get_remote_seid(packet), media_tracker.remote_seid); - if (local_seid != media_tracker.local_seid){ - printf("A2DP Source: Stream failed, wrong local seid %d, expected %d.\n", local_seid, media_tracker.local_seid); - break; - } - printf("A2DP Source: Stream established, address %s, a2dp cid 0x%02x, local seid %d, remote seid %d.\n", bd_addr_to_str(address), - media_tracker.a2dp_cid, media_tracker.local_seid, a2dp_subevent_stream_established_get_remote_seid(packet)); + printf("A2DP Source: Stream established a2dp_cid 0x%02x, local_seid 0x%02x, remote_seid 0x%02x\n", cid, local_seid, a2dp_subevent_stream_established_get_remote_seid(packet)); + + a2dp_demo_hexcmod_configure_sample_rate(current_sample_rate); media_tracker.stream_opened = 1; data_source = STREAM_MOD; status = a2dp_source_start_stream(media_tracker.a2dp_cid, media_tracker.local_seid); @@ -660,27 +656,32 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui local_seid = a2dp_subevent_stream_reconfigured_get_local_seid(packet); cid = a2dp_subevent_stream_reconfigured_get_a2dp_cid(packet); - printf("A2DP Source: Reconfigured: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid [expected %d, received %d]\n", media_tracker.a2dp_cid, cid, media_tracker.local_seid, local_seid); - printf("Status 0x%02x\n", status); + if (status != ERROR_CODE_SUCCESS){ + printf("A2DP Source: Stream reconfiguration failed with status 0x%02x\n", status); + break; + } + + printf("A2DP Source: Stream reconfigured a2dp_cid 0x%02x, local_seid 0x%02x\n", cid, local_seid); + a2dp_demo_hexcmod_configure_sample_rate(new_sample_rate); + status = a2dp_source_start_stream(media_tracker.a2dp_cid, media_tracker.local_seid); break; case A2DP_SUBEVENT_STREAM_STARTED: local_seid = a2dp_subevent_stream_started_get_local_seid(packet); cid = a2dp_subevent_stream_started_get_a2dp_cid(packet); - + play_info.status = AVRCP_PLAYBACK_STATUS_PLAYING; if (media_tracker.avrcp_cid){ avrcp_target_set_now_playing_info(media_tracker.avrcp_cid, &tracks[data_source], sizeof(tracks)/sizeof(avrcp_track_t)); avrcp_target_set_playback_status(media_tracker.avrcp_cid, AVRCP_PLAYBACK_STATUS_PLAYING); } a2dp_demo_timer_start(&media_tracker); - printf("A2DP Source: Stream started: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid [expected %d, received %d]\n", media_tracker.a2dp_cid, cid, media_tracker.local_seid, local_seid); + printf("A2DP Source: Stream started, a2dp_cid 0x%02x, local_seid 0x%02x\n", cid, local_seid); break; case A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW: local_seid = a2dp_subevent_streaming_can_send_media_packet_now_get_local_seid(packet); cid = a2dp_subevent_signaling_media_codec_sbc_configuration_get_a2dp_cid(packet); - // printf("A2DP Source: can send media packet: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid [expected %d, received %d]\n", media_tracker.a2dp_cid, cid, media_tracker.local_seid, local_seid); a2dp_demo_send_media_packet(); break; @@ -692,7 +693,7 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui if (media_tracker.avrcp_cid){ avrcp_target_set_playback_status(media_tracker.avrcp_cid, AVRCP_PLAYBACK_STATUS_PAUSED); } - printf("A2DP Source: Stream paused: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid [expected %d, received %d]\n", media_tracker.a2dp_cid, cid, media_tracker.local_seid, local_seid); + printf("A2DP Source: Stream paused, a2dp_cid 0x%02x, local_seid 0x%02x\n", cid, local_seid); a2dp_demo_timer_stop(&media_tracker); break; @@ -702,7 +703,7 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui cid = a2dp_subevent_stream_released_get_a2dp_cid(packet); local_seid = a2dp_subevent_stream_released_get_local_seid(packet); - printf("A2DP Source: Stream released: a2dp_cid [expected 0x%02x, received 0x%02x], local_seid [expected %d, received %d]\n", media_tracker.a2dp_cid, cid, media_tracker.local_seid, local_seid); + printf("A2DP Source: Stream released, a2dp_cid 0x%02x, local_seid 0x%02x\n", cid, local_seid); if (cid == media_tracker.a2dp_cid) { media_tracker.stream_opened = 0; @@ -712,7 +713,6 @@ static void a2dp_source_packet_handler(uint8_t packet_type, uint16_t channel, ui avrcp_target_set_now_playing_info(media_tracker.avrcp_cid, NULL, sizeof(tracks)/sizeof(avrcp_track_t)); avrcp_target_set_playback_status(media_tracker.avrcp_cid, AVRCP_PLAYBACK_STATUS_STOPPED); } - a2dp_demo_timer_stop(&media_tracker); break; case A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: @@ -951,9 +951,6 @@ static void stdin_process(char cmd){ data_source = STREAM_MOD; if (!media_tracker.stream_opened) break; status = a2dp_source_start_stream(media_tracker.a2dp_cid, media_tracker.local_seid); - if (status == ERROR_CODE_SUCCESS){ - a2dp_demo_reconfigure_sample_rate(sample_rate); - } break; case 'p': @@ -968,10 +965,12 @@ static void stdin_process(char cmd){ printf("Stream cannot be reconfigured while playing, please pause stream first\n"); break; } - printf("%c - Reconfigure for %d Hz.\n", cmd, sample_rate); - status = a2dp_source_reconfigure_stream_sampling_frequency(media_tracker.a2dp_cid, 44100); - if (status == ERROR_CODE_SUCCESS){ - a2dp_demo_reconfigure_sample_rate(44100); + new_sample_rate = 44100; + if (current_sample_rate == new_sample_rate){ + printf("%c - Stream already configured for %d Hz.\n", cmd, new_sample_rate); + } else { + printf("%c - Reconfigure for %d Hz.\n", cmd, new_sample_rate); + status = a2dp_source_reconfigure_stream_sampling_frequency(media_tracker.a2dp_cid, new_sample_rate); } break; @@ -981,10 +980,12 @@ static void stdin_process(char cmd){ printf("Stream cannot be reconfigured while playing, please pause stream first\n"); break; } - printf("%c - Reconfigure for %d Hz.\n", cmd, sample_rate); - status = a2dp_source_reconfigure_stream_sampling_frequency(media_tracker.a2dp_cid, 48000); - if (status == ERROR_CODE_SUCCESS){ - a2dp_demo_reconfigure_sample_rate(48000); + new_sample_rate = 48000; + if (current_sample_rate == new_sample_rate){ + printf("%c - Stream already configured for %d Hz.\n", cmd, new_sample_rate); + } else { + printf("%c - Reconfigure for %d Hz.\n", cmd, new_sample_rate); + status = a2dp_source_reconfigure_stream_sampling_frequency(media_tracker.a2dp_cid, new_sample_rate); } break;