1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "a2dp_sink_demo.c" 39 40 /* 41 * a2dp_sink_demo.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(a2dp_sink_demo): A2DP Sink - Receive Audio Stream and Control Playback 46 * 47 * @text This A2DP Sink example demonstrates how to use the A2DP Sink service to 48 * receive an audio data stream from a remote A2DP Source device. In addition, 49 * the AVRCP Controller is used to get information on currently played media, 50 * such are title, artist and album, as well as to control the playback, 51 * i.e. to play, stop, repeat, etc. If HAVE_BTSTACK_STDIN is set, press SPACE on 52 * the console to show the available AVDTP and AVRCP commands. 53 * 54 * @text To test with a remote device, e.g. a mobile phone, 55 * pair from the remote device with the demo, then start playing music on the remote device. 56 * Alternatively, set the device_addr_string to the Bluetooth address of your 57 * remote device in the code, and call connect from the UI. 58 * 59 * @text For more info on BTstack audio, see our blog post 60 * [A2DP Sink and Source on STM32 F4 Discovery Board](http://bluekitchen-gmbh.com/a2dp-sink-and-source-on-stm32-f4-discovery-board/). 61 * 62 */ 63 // ***************************************************************************** 64 65 #include <inttypes.h> 66 #include <stdint.h> 67 #include <stdio.h> 68 #include <string.h> 69 70 #include "btstack.h" 71 #include "btstack_resample.h" 72 73 //#define AVRCP_BROWSING_ENABLED 74 75 #ifdef HAVE_BTSTACK_STDIN 76 #include "btstack_stdin.h" 77 #endif 78 79 #ifdef HAVE_BTSTACK_AUDIO_EFFECTIVE_SAMPLERATE 80 #include "btstack_sample_rate_compensation.h" 81 #endif 82 83 #include "btstack_ring_buffer.h" 84 85 #ifdef HAVE_POSIX_FILE_IO 86 #include "wav_util.h" 87 #define STORE_TO_WAV_FILE 88 #endif 89 90 #define NUM_CHANNELS 2 91 #define BYTES_PER_FRAME (2*NUM_CHANNELS) 92 #define MAX_SBC_FRAME_SIZE 120 93 94 #ifdef HAVE_BTSTACK_STDIN 95 static const char * device_addr_string = "5C:F3:70:60:7B:87"; // pts 96 static bd_addr_t device_addr; 97 #endif 98 99 #ifdef HAVE_BTSTACK_AUDIO_EFFECTIVE_SAMPLERATE 100 static btstack_sample_rate_compensation_t sample_rate_compensation; 101 #endif 102 103 static btstack_packet_callback_registration_t hci_event_callback_registration; 104 105 static uint8_t sdp_avdtp_sink_service_buffer[150]; 106 static uint8_t sdp_avrcp_target_service_buffer[150]; 107 static uint8_t sdp_avrcp_controller_service_buffer[200]; 108 static uint8_t device_id_sdp_service_buffer[100]; 109 110 // we support all configurations with bitpool 2-53 111 static uint8_t media_sbc_codec_capabilities[] = { 112 0xFF,//(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 113 0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 114 2, 53 115 }; 116 117 // WAV File 118 #ifdef STORE_TO_WAV_FILE 119 static uint32_t audio_frame_count = 0; 120 static char * wav_filename = "a2dp_sink_demo.wav"; 121 #endif 122 123 // SBC Decoder for WAV file or live playback 124 static btstack_sbc_decoder_state_t state; 125 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 126 127 // ring buffer for SBC Frames 128 // below 30: add samples, 30-40: fine, above 40: drop samples 129 #define OPTIMAL_FRAMES_MIN 60 130 #define OPTIMAL_FRAMES_MAX 80 131 #define ADDITIONAL_FRAMES 30 132 static uint8_t sbc_frame_storage[(OPTIMAL_FRAMES_MAX + ADDITIONAL_FRAMES) * MAX_SBC_FRAME_SIZE]; 133 static btstack_ring_buffer_t sbc_frame_ring_buffer; 134 static unsigned int sbc_frame_size; 135 136 // overflow buffer for not fully used sbc frames, with additional frames for resampling 137 static uint8_t decoded_audio_storage[(128+16) * BYTES_PER_FRAME]; 138 static btstack_ring_buffer_t decoded_audio_ring_buffer; 139 140 static int media_initialized = 0; 141 static int audio_stream_started; 142 static btstack_resample_t resample_instance; 143 144 // temp storage of lower-layer request for audio samples 145 static int16_t * request_buffer; 146 static int request_frames; 147 148 // sink state 149 static int volume_percentage = 0; 150 static avrcp_battery_status_t battery_status = AVRCP_BATTERY_STATUS_WARNING; 151 152 typedef struct { 153 uint8_t reconfigure; 154 uint8_t num_channels; 155 uint16_t sampling_frequency; 156 uint8_t block_length; 157 uint8_t subbands; 158 uint8_t min_bitpool_value; 159 uint8_t max_bitpool_value; 160 btstack_sbc_channel_mode_t channel_mode; 161 btstack_sbc_allocation_method_t allocation_method; 162 } media_codec_configuration_sbc_t; 163 164 typedef enum { 165 STREAM_STATE_CLOSED, 166 STREAM_STATE_OPEN, 167 STREAM_STATE_PLAYING, 168 STREAM_STATE_PAUSED, 169 } stream_state_t; 170 171 typedef struct { 172 uint8_t a2dp_local_seid; 173 uint8_t media_sbc_codec_configuration[4]; 174 } a2dp_sink_demo_stream_endpoint_t; 175 static a2dp_sink_demo_stream_endpoint_t a2dp_sink_demo_stream_endpoint; 176 177 typedef struct { 178 bd_addr_t addr; 179 uint16_t a2dp_cid; 180 uint8_t a2dp_local_seid; 181 stream_state_t stream_state; 182 media_codec_configuration_sbc_t sbc_configuration; 183 } a2dp_sink_demo_a2dp_connection_t; 184 static a2dp_sink_demo_a2dp_connection_t a2dp_sink_demo_a2dp_connection; 185 186 typedef struct { 187 bd_addr_t addr; 188 uint16_t avrcp_cid; 189 bool playing; 190 uint16_t notifications_supported_by_target; 191 } a2dp_sink_demo_avrcp_connection_t; 192 static a2dp_sink_demo_avrcp_connection_t a2dp_sink_demo_avrcp_connection; 193 194 /* @section Main Application Setup 195 * 196 * @text The Listing MainConfiguration shows how to set up AD2P Sink and AVRCP services. 197 * Besides calling init() method for each service, you'll also need to register several packet handlers: 198 * - hci_packet_handler - handles legacy pairing, here by using fixed '0000' pin code. 199 * - a2dp_sink_packet_handler - handles events on stream connection status (established, released), the media codec configuration, and, the status of the stream itself (opened, paused, stopped). 200 * - handle_l2cap_media_data_packet - used to receive streaming data. If STORE_TO_WAV_FILE directive (check btstack_config.h) is used, the SBC decoder will be used to decode the SBC data into PCM frames. The resulting PCM frames are then processed in the SBC Decoder callback. 201 * - avrcp_packet_handler - receives AVRCP connect/disconnect event. 202 * - avrcp_controller_packet_handler - receives answers for sent AVRCP commands. 203 * - avrcp_target_packet_handler - receives AVRCP commands, and registered notifications. 204 * - stdin_process - used to trigger AVRCP commands to the A2DP Source device, such are get now playing info, start, stop, volume control. Requires HAVE_BTSTACK_STDIN. 205 * 206 * @text To announce A2DP Sink and AVRCP services, you need to create corresponding 207 * SDP records and register them with the SDP service. 208 * 209 * @text Note, currently only the SBC codec is supported. 210 * If you want to store the audio data in a file, you'll need to define STORE_TO_WAV_FILE. 211 * If STORE_TO_WAV_FILE directive is defined, the SBC decoder needs to get initialized when a2dp_sink_packet_handler receives event A2DP_SUBEVENT_STREAM_STARTED. 212 * The initialization of the SBC decoder requires a callback that handles PCM data: 213 * - handle_pcm_data - handles PCM audio frames. Here, they are stored in a wav file if STORE_TO_WAV_FILE is defined, and/or played using the audio library. 214 */ 215 216 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP services */ 217 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 218 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t event_size); 219 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size); 220 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 221 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 222 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 223 #ifdef HAVE_BTSTACK_STDIN 224 static void stdin_process(char cmd); 225 #endif 226 227 static int setup_demo(void){ 228 229 // init protocols 230 l2cap_init(); 231 sdp_init(); 232 #ifdef ENABLE_BLE 233 // Initialize LE Security Manager. Needed for cross-transport key derivation 234 sm_init(); 235 #endif 236 237 238 // Init profiles 239 a2dp_sink_init(); 240 avrcp_init(); 241 avrcp_controller_init(); 242 avrcp_target_init(); 243 244 245 // Configure A2DP Sink 246 a2dp_sink_register_packet_handler(&a2dp_sink_packet_handler); 247 a2dp_sink_register_media_handler(&handle_l2cap_media_data_packet); 248 a2dp_sink_demo_stream_endpoint_t * stream_endpoint = &a2dp_sink_demo_stream_endpoint; 249 avdtp_stream_endpoint_t * local_stream_endpoint = a2dp_sink_create_stream_endpoint(AVDTP_AUDIO, 250 AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), 251 stream_endpoint->media_sbc_codec_configuration, sizeof(stream_endpoint->media_sbc_codec_configuration)); 252 btstack_assert(local_stream_endpoint != NULL); 253 // - Store stream enpoint's SEP ID, as it is used by A2DP API to identify the stream endpoint 254 stream_endpoint->a2dp_local_seid = avdtp_local_seid(local_stream_endpoint); 255 256 257 // Configure AVRCP Controller + Target 258 avrcp_register_packet_handler(&avrcp_packet_handler); 259 avrcp_controller_register_packet_handler(&avrcp_controller_packet_handler); 260 avrcp_target_register_packet_handler(&avrcp_target_packet_handler); 261 262 263 // Configure SDP 264 265 // - Create and register A2DP Sink service record 266 memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer)); 267 a2dp_sink_create_sdp_record(sdp_avdtp_sink_service_buffer, sdp_create_service_record_handle(), 268 AVDTP_SINK_FEATURE_MASK_HEADPHONE, NULL, NULL); 269 sdp_register_service(sdp_avdtp_sink_service_buffer); 270 271 // - Create AVRCP Controller service record and register it with SDP. We send Category 1 commands to the media player, e.g. play/pause 272 memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer)); 273 uint16_t controller_supported_features = AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER; 274 #ifdef AVRCP_BROWSING_ENABLED 275 controller_supported_features |= AVRCP_FEATURE_MASK_BROWSING; 276 #endif 277 avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, sdp_create_service_record_handle(), 278 controller_supported_features, NULL, NULL); 279 sdp_register_service(sdp_avrcp_controller_service_buffer); 280 281 // - Create and register A2DP Sink service record 282 // - We receive Category 2 commands from the media player, e.g. volume up/down 283 memset(sdp_avrcp_target_service_buffer, 0, sizeof(sdp_avrcp_target_service_buffer)); 284 uint16_t target_supported_features = AVRCP_FEATURE_MASK_CATEGORY_MONITOR_OR_AMPLIFIER; 285 avrcp_target_create_sdp_record(sdp_avrcp_target_service_buffer, 286 sdp_create_service_record_handle(), target_supported_features, NULL, NULL); 287 sdp_register_service(sdp_avrcp_target_service_buffer); 288 289 // - Create and register Device ID (PnP) service record 290 memset(device_id_sdp_service_buffer, 0, sizeof(device_id_sdp_service_buffer)); 291 device_id_create_sdp_record(device_id_sdp_service_buffer, 292 sdp_create_service_record_handle(), DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1); 293 sdp_register_service(device_id_sdp_service_buffer); 294 295 296 // Configure GAP - discovery / connection 297 298 // - Set local name with a template Bluetooth address, that will be automatically 299 // replaced with an actual address once it is available, i.e. when BTstack boots 300 // up and starts talking to a Bluetooth module. 301 gap_set_local_name("A2DP Sink Demo 00:00:00:00:00:00"); 302 303 // - Allow to show up in Bluetooth inquiry 304 gap_discoverable_control(1); 305 306 // - Set Class of Device - Service Class: Audio, Major Device Class: Audio, Minor: Loudspeaker 307 gap_set_class_of_device(0x200414); 308 309 // - Allow for role switch in general and sniff mode 310 gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE ); 311 312 // - Allow for role switch on outgoing connections 313 // - This allows A2DP Source, e.g. smartphone, to become master when we re-connect to it. 314 gap_set_allow_role_switch(true); 315 316 317 // Register for HCI events 318 hci_event_callback_registration.callback = &hci_packet_handler; 319 hci_add_event_handler(&hci_event_callback_registration); 320 321 // Inform about audio playback / test options 322 #ifdef HAVE_POSIX_FILE_IO 323 if (!btstack_audio_sink_get_instance()){ 324 printf("No audio playback.\n"); 325 } else { 326 printf("Audio playback supported.\n"); 327 } 328 #ifdef STORE_TO_WAV_FILE 329 printf("Audio will be stored to \'%s\' file.\n", wav_filename); 330 #endif 331 #endif 332 return 0; 333 } 334 /* LISTING_END */ 335 336 337 static void playback_handler(int16_t * buffer, uint16_t num_audio_frames){ 338 339 #ifdef STORE_TO_WAV_FILE 340 int wav_samples = num_audio_frames * NUM_CHANNELS; 341 int16_t * wav_buffer = buffer; 342 #endif 343 344 // called from lower-layer but guaranteed to be on main thread 345 if (sbc_frame_size == 0){ 346 memset(buffer, 0, num_audio_frames * BYTES_PER_FRAME); 347 return; 348 } 349 350 // first fill from resampled audio 351 uint32_t bytes_read; 352 btstack_ring_buffer_read(&decoded_audio_ring_buffer, (uint8_t *) buffer, num_audio_frames * BYTES_PER_FRAME, &bytes_read); 353 buffer += bytes_read / NUM_CHANNELS; 354 num_audio_frames -= bytes_read / BYTES_PER_FRAME; 355 356 // then start decoding sbc frames using request_* globals 357 request_buffer = buffer; 358 request_frames = num_audio_frames; 359 while (request_frames && btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) >= sbc_frame_size){ 360 // decode frame 361 uint8_t sbc_frame[MAX_SBC_FRAME_SIZE]; 362 btstack_ring_buffer_read(&sbc_frame_ring_buffer, sbc_frame, sbc_frame_size, &bytes_read); 363 btstack_sbc_decoder_process_data(&state, 0, sbc_frame, sbc_frame_size); 364 } 365 366 #ifdef STORE_TO_WAV_FILE 367 audio_frame_count += num_audio_frames; 368 wav_writer_write_int16(wav_samples, wav_buffer); 369 #endif 370 } 371 372 static void handle_pcm_data(int16_t * data, int num_audio_frames, int num_channels, int sample_rate, void * context){ 373 UNUSED(sample_rate); 374 UNUSED(context); 375 UNUSED(num_channels); // must be stereo == 2 376 377 const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 378 if (!audio_sink){ 379 #ifdef STORE_TO_WAV_FILE 380 audio_frame_count += num_audio_frames; 381 wav_writer_write_int16(num_audio_frames * NUM_CHANNELS, data); 382 #endif 383 return; 384 } 385 386 // resample into request buffer - add some additional space for resampling 387 int16_t output_buffer[(128+16) * NUM_CHANNELS]; // 16 * 8 * 2 388 uint32_t resampled_frames = btstack_resample_block(&resample_instance, data, num_audio_frames, output_buffer); 389 390 // store data in btstack_audio buffer first 391 int frames_to_copy = btstack_min(resampled_frames, request_frames); 392 memcpy(request_buffer, output_buffer, frames_to_copy * BYTES_PER_FRAME); 393 request_frames -= frames_to_copy; 394 request_buffer += frames_to_copy * NUM_CHANNELS; 395 396 // and rest in ring buffer 397 int frames_to_store = resampled_frames - frames_to_copy; 398 if (frames_to_store){ 399 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); 400 if (status){ 401 printf("Error storing samples in PCM ring buffer!!!\n"); 402 } 403 } 404 } 405 406 static int media_processing_init(media_codec_configuration_sbc_t * configuration){ 407 if (media_initialized) return 0; 408 #ifdef HAVE_BTSTACK_AUDIO_EFFECTIVE_SAMPLERATE 409 btstack_sample_rate_compensation_init( &sample_rate_compensation, btstack_run_loop_get_time_ms(), configuration->sampling_frequency, FLOAT_TO_Q15(1.f) ); 410 #endif 411 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 412 413 #ifdef STORE_TO_WAV_FILE 414 wav_writer_open(wav_filename, configuration->num_channels, configuration->sampling_frequency); 415 #endif 416 417 btstack_ring_buffer_init(&sbc_frame_ring_buffer, sbc_frame_storage, sizeof(sbc_frame_storage)); 418 btstack_ring_buffer_init(&decoded_audio_ring_buffer, decoded_audio_storage, sizeof(decoded_audio_storage)); 419 btstack_resample_init(&resample_instance, configuration->num_channels); 420 421 // setup audio playback 422 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 423 if (audio){ 424 audio->init(NUM_CHANNELS, configuration->sampling_frequency, &playback_handler); 425 } 426 427 audio_stream_started = 0; 428 media_initialized = 1; 429 return 0; 430 } 431 432 static void media_processing_start(void){ 433 if (!media_initialized) return; 434 #ifdef HAVE_BTSTACK_AUDIO_EFFECTIVE_SAMPLERATE 435 btstack_sample_rate_compensation_reset( &sample_rate_compensation, btstack_run_loop_get_time_ms() ); 436 #endif 437 // setup audio playback 438 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 439 if (audio){ 440 audio->start_stream(); 441 } 442 audio_stream_started = 1; 443 } 444 445 static void media_processing_pause(void){ 446 if (!media_initialized) return; 447 // stop audio playback 448 audio_stream_started = 0; 449 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 450 if (audio){ 451 audio->stop_stream(); 452 } 453 // discard pending data 454 btstack_ring_buffer_reset(&decoded_audio_ring_buffer); 455 btstack_ring_buffer_reset(&sbc_frame_ring_buffer); 456 } 457 458 static void media_processing_close(void){ 459 if (!media_initialized) return; 460 media_initialized = 0; 461 audio_stream_started = 0; 462 sbc_frame_size = 0; 463 464 #ifdef STORE_TO_WAV_FILE 465 wav_writer_close(); 466 uint32_t total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 467 468 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); 469 printf("WAV Writer: Wrote %u audio frames to wav file: %s\n", audio_frame_count, wav_filename); 470 #endif 471 472 // stop audio playback 473 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 474 if (audio){ 475 printf("close stream\n"); 476 audio->close(); 477 } 478 } 479 480 /* @section Handle Media Data Packet 481 * 482 * @text Here the audio data, are received through the handle_l2cap_media_data_packet callback. 483 * Currently, only the SBC media codec is supported. Hence, the media data consists of the media packet header and the SBC packet. 484 * The SBC frame will be stored in a ring buffer for later processing (instead of decoding it to PCM right away which would require a much larger buffer). 485 * If the audio stream wasn't started already and there are enough SBC frames in the ring buffer, start playback. 486 */ 487 488 static int read_media_data_header(uint8_t * packet, int size, int * offset, avdtp_media_packet_header_t * media_header); 489 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header); 490 491 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size){ 492 UNUSED(seid); 493 int pos = 0; 494 495 avdtp_media_packet_header_t media_header; 496 if (!read_media_data_header(packet, size, &pos, &media_header)) return; 497 498 avdtp_sbc_codec_header_t sbc_header; 499 if (!read_sbc_header(packet, size, &pos, &sbc_header)) return; 500 501 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 502 // process data right away if there's no audio implementation active, e.g. on posix systems to store as .wav 503 if (!audio){ 504 btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos); 505 return; 506 } 507 508 509 // store sbc frame size for buffer management 510 sbc_frame_size = (size-pos)/ sbc_header.num_frames; 511 512 int status = btstack_ring_buffer_write(&sbc_frame_ring_buffer, packet+pos, size-pos); 513 if (status != ERROR_CODE_SUCCESS){ 514 printf("Error storing samples in SBC ring buffer!!!\n"); 515 } 516 517 // decide on audio sync drift based on number of sbc frames in queue 518 int sbc_frames_in_buffer = btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer) / sbc_frame_size; 519 #ifdef HAVE_BTSTACK_AUDIO_EFFECTIVE_SAMPLERATE 520 // update sample rate compensation 521 if( audio_stream_started && (audio != NULL)) { 522 uint32_t resampling_factor = btstack_sample_rate_compensation_update( &sample_rate_compensation, btstack_run_loop_get_time_ms(), sbc_header.num_frames*128, audio->get_samplerate() ); 523 btstack_resample_set_factor(&resample_instance, resampling_factor); 524 // printf("sbc buffer level : %d\n", btstack_ring_buffer_bytes_available(&sbc_frame_ring_buffer)); 525 } 526 #else 527 uint32_t resampling_factor; 528 529 // nominal factor (fixed-point 2^16) and compensation offset 530 uint32_t nominal_factor = 0x10000; 531 uint32_t compensation = 0x00100; 532 533 if (sbc_frames_in_buffer < OPTIMAL_FRAMES_MIN){ 534 resampling_factor = nominal_factor - compensation; // stretch samples 535 } else if (sbc_frames_in_buffer <= OPTIMAL_FRAMES_MAX){ 536 resampling_factor = nominal_factor; // nothing to do 537 } else { 538 resampling_factor = nominal_factor + compensation; // compress samples 539 } 540 541 btstack_resample_set_factor(&resample_instance, resampling_factor); 542 #endif 543 // start stream if enough frames buffered 544 if (!audio_stream_started && sbc_frames_in_buffer >= OPTIMAL_FRAMES_MIN){ 545 media_processing_start(); 546 } 547 } 548 549 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header){ 550 int sbc_header_len = 12; // without crc 551 int pos = *offset; 552 553 if (size - pos < sbc_header_len){ 554 printf("Not enough data to read SBC header, expected %d, received %d\n", sbc_header_len, size-pos); 555 return 0; 556 } 557 558 sbc_header->fragmentation = get_bit16(packet[pos], 7); 559 sbc_header->starting_packet = get_bit16(packet[pos], 6); 560 sbc_header->last_packet = get_bit16(packet[pos], 5); 561 sbc_header->num_frames = packet[pos] & 0x0f; 562 pos++; 563 *offset = pos; 564 return 1; 565 } 566 567 static int read_media_data_header(uint8_t *packet, int size, int *offset, avdtp_media_packet_header_t *media_header){ 568 int media_header_len = 12; // without crc 569 int pos = *offset; 570 571 if (size - pos < media_header_len){ 572 printf("Not enough data to read media packet header, expected %d, received %d\n", media_header_len, size-pos); 573 return 0; 574 } 575 576 media_header->version = packet[pos] & 0x03; 577 media_header->padding = get_bit16(packet[pos],2); 578 media_header->extension = get_bit16(packet[pos],3); 579 media_header->csrc_count = (packet[pos] >> 4) & 0x0F; 580 pos++; 581 582 media_header->marker = get_bit16(packet[pos],0); 583 media_header->payload_type = (packet[pos] >> 1) & 0x7F; 584 pos++; 585 586 media_header->sequence_number = big_endian_read_16(packet, pos); 587 pos+=2; 588 589 media_header->timestamp = big_endian_read_32(packet, pos); 590 pos+=4; 591 592 media_header->synchronization_source = big_endian_read_32(packet, pos); 593 pos+=4; 594 *offset = pos; 595 return 1; 596 } 597 598 static void dump_sbc_configuration(media_codec_configuration_sbc_t * configuration){ 599 printf(" - num_channels: %d\n", configuration->num_channels); 600 printf(" - sampling_frequency: %d\n", configuration->sampling_frequency); 601 printf(" - channel_mode: %d\n", configuration->channel_mode); 602 printf(" - block_length: %d\n", configuration->block_length); 603 printf(" - subbands: %d\n", configuration->subbands); 604 printf(" - allocation_method: %d\n", configuration->allocation_method); 605 printf(" - bitpool_value [%d, %d] \n", configuration->min_bitpool_value, configuration->max_bitpool_value); 606 printf("\n"); 607 } 608 609 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 610 UNUSED(channel); 611 UNUSED(size); 612 uint16_t local_cid; 613 uint8_t status; 614 bd_addr_t address; 615 616 a2dp_sink_demo_avrcp_connection_t * connection = &a2dp_sink_demo_avrcp_connection; 617 618 if (packet_type != HCI_EVENT_PACKET) return; 619 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 620 switch (packet[2]){ 621 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { 622 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 623 status = avrcp_subevent_connection_established_get_status(packet); 624 if (status != ERROR_CODE_SUCCESS){ 625 printf("AVRCP: Connection failed, status 0x%02x\n", status); 626 connection->avrcp_cid = 0; 627 return; 628 } 629 630 connection->avrcp_cid = local_cid; 631 avrcp_subevent_connection_established_get_bd_addr(packet, address); 632 printf("AVRCP: Connected to %s, cid 0x%02x\n", bd_addr_to_str(address), connection->avrcp_cid); 633 634 avrcp_target_support_event(connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED); 635 avrcp_target_support_event(connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED); 636 avrcp_target_battery_status_changed(connection->avrcp_cid, battery_status); 637 638 // query supported events: 639 avrcp_controller_get_supported_events(connection->avrcp_cid); 640 return; 641 } 642 643 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 644 printf("AVRCP: Channel released: cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 645 connection->avrcp_cid = 0; 646 connection->notifications_supported_by_target = 0; 647 return; 648 default: 649 break; 650 } 651 } 652 653 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 654 UNUSED(channel); 655 UNUSED(size); 656 657 // helper to print c strings 658 uint8_t avrcp_subevent_value[256]; 659 uint8_t play_status; 660 uint8_t event_id; 661 662 a2dp_sink_demo_avrcp_connection_t * avrcp_connection = &a2dp_sink_demo_avrcp_connection; 663 664 if (packet_type != HCI_EVENT_PACKET) return; 665 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 666 if (avrcp_connection->avrcp_cid == 0) return; 667 668 memset(avrcp_subevent_value, 0, sizeof(avrcp_subevent_value)); 669 switch (packet[2]){ 670 case AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID: 671 avrcp_connection->notifications_supported_by_target |= (1 << avrcp_subevent_get_capability_event_id_get_event_id(packet)); 672 break; 673 case AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE: 674 675 printf("\nSupported remote AVRCP Target notifications:\n"); 676 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 677 printf(" - [%s] %s\n", 678 (avrcp_connection->notifications_supported_by_target & (1 << event_id)) != 0 ? "X" : " ", 679 avrcp_notification2str((avrcp_notification_event_id_t)event_id)); 680 } 681 printf("\n\n"); 682 683 // automatically enable notifications 684 avrcp_controller_enable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED); 685 avrcp_controller_enable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED); 686 avrcp_controller_enable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 687 break; 688 689 case AVRCP_SUBEVENT_NOTIFICATION_STATE: 690 event_id = (avrcp_notification_event_id_t)avrcp_subevent_notification_state_get_event_id(packet); 691 printf("AVRCP Controller: %s notification registered\n", avrcp_notification2str(event_id)); 692 break; 693 694 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED: 695 printf("AVRCP Controller: Playback position changed, position %d ms\n", (unsigned int) avrcp_subevent_notification_playback_pos_changed_get_playback_position_ms(packet)); 696 break; 697 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED: 698 printf("AVRCP Controller: Playback status changed %s\n", avrcp_play_status2str(avrcp_subevent_notification_playback_status_changed_get_play_status(packet))); 699 play_status = avrcp_subevent_notification_playback_status_changed_get_play_status(packet); 700 switch (play_status){ 701 case AVRCP_PLAYBACK_STATUS_PLAYING: 702 avrcp_connection->playing = true; 703 break; 704 default: 705 avrcp_connection->playing = false; 706 break; 707 } 708 break; 709 710 case AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED: 711 printf("AVRCP Controller: Playing content changed\n"); 712 break; 713 714 case AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED: 715 printf("AVRCP Controller: Track changed\n"); 716 break; 717 718 case AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED: 719 printf("AVRCP Controller: Available Players Changed\n"); 720 break; 721 722 case AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE:{ 723 uint8_t shuffle_mode = avrcp_subevent_shuffle_and_repeat_mode_get_shuffle_mode(packet); 724 uint8_t repeat_mode = avrcp_subevent_shuffle_and_repeat_mode_get_repeat_mode(packet); 725 printf("AVRCP Controller: %s, %s\n", avrcp_shuffle2str(shuffle_mode), avrcp_repeat2str(repeat_mode)); 726 break; 727 } 728 case AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO: 729 printf("AVRCP Controller: Track %d\n", avrcp_subevent_now_playing_track_info_get_track(packet)); 730 break; 731 732 case AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO: 733 printf("AVRCP Controller: Total Tracks %d\n", avrcp_subevent_now_playing_total_tracks_info_get_total_tracks(packet)); 734 break; 735 736 case AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO: 737 if (avrcp_subevent_now_playing_title_info_get_value_len(packet) > 0){ 738 memcpy(avrcp_subevent_value, avrcp_subevent_now_playing_title_info_get_value(packet), avrcp_subevent_now_playing_title_info_get_value_len(packet)); 739 printf("AVRCP Controller: Title %s\n", avrcp_subevent_value); 740 } 741 break; 742 743 case AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO: 744 if (avrcp_subevent_now_playing_artist_info_get_value_len(packet) > 0){ 745 memcpy(avrcp_subevent_value, avrcp_subevent_now_playing_artist_info_get_value(packet), avrcp_subevent_now_playing_artist_info_get_value_len(packet)); 746 printf("AVRCP Controller: Artist %s\n", avrcp_subevent_value); 747 } 748 break; 749 750 case AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO: 751 if (avrcp_subevent_now_playing_album_info_get_value_len(packet) > 0){ 752 memcpy(avrcp_subevent_value, avrcp_subevent_now_playing_album_info_get_value(packet), avrcp_subevent_now_playing_album_info_get_value_len(packet)); 753 printf("AVRCP Controller: Album %s\n", avrcp_subevent_value); 754 } 755 break; 756 757 case AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO: 758 if (avrcp_subevent_now_playing_genre_info_get_value_len(packet) > 0){ 759 memcpy(avrcp_subevent_value, avrcp_subevent_now_playing_genre_info_get_value(packet), avrcp_subevent_now_playing_genre_info_get_value_len(packet)); 760 printf("AVRCP Controller: Genre %s\n", avrcp_subevent_value); 761 } 762 break; 763 764 case AVRCP_SUBEVENT_PLAY_STATUS: 765 printf("AVRCP Controller: Song length %"PRIu32" ms, Song position %"PRIu32" ms, Play status %s\n", 766 avrcp_subevent_play_status_get_song_length(packet), 767 avrcp_subevent_play_status_get_song_position(packet), 768 avrcp_play_status2str(avrcp_subevent_play_status_get_play_status(packet))); 769 break; 770 771 case AVRCP_SUBEVENT_OPERATION_COMPLETE: 772 printf("AVRCP Controller: %s complete\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 773 break; 774 775 case AVRCP_SUBEVENT_OPERATION_START: 776 printf("AVRCP Controller: %s start\n", avrcp_operation2str(avrcp_subevent_operation_start_get_operation_id(packet))); 777 break; 778 779 case AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END: 780 printf("AVRCP Controller: Track reached end\n"); 781 break; 782 783 case AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE: 784 printf("AVRCP Controller: Set Player App Value %s\n", avrcp_ctype2str(avrcp_subevent_player_application_value_response_get_command_type(packet))); 785 break; 786 787 default: 788 break; 789 } 790 } 791 792 static void avrcp_volume_changed(uint8_t volume){ 793 const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 794 if (audio){ 795 audio->set_volume(volume); 796 } 797 } 798 799 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 800 UNUSED(channel); 801 UNUSED(size); 802 803 if (packet_type != HCI_EVENT_PACKET) return; 804 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 805 806 uint8_t volume; 807 char const * button_state; 808 avrcp_operation_id_t operation_id; 809 810 switch (packet[2]){ 811 case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED: 812 volume = avrcp_subevent_notification_volume_changed_get_absolute_volume(packet); 813 volume_percentage = volume * 100 / 127; 814 printf("AVRCP Target : Volume set to %d%% (%d)\n", volume_percentage, volume); 815 avrcp_volume_changed(volume); 816 break; 817 818 case AVRCP_SUBEVENT_OPERATION: 819 operation_id = avrcp_subevent_operation_get_operation_id(packet); 820 button_state = avrcp_subevent_operation_get_button_pressed(packet) > 0 ? "PRESS" : "RELEASE"; 821 switch (operation_id){ 822 case AVRCP_OPERATION_ID_VOLUME_UP: 823 printf("AVRCP Target : VOLUME UP (%s)\n", button_state); 824 break; 825 case AVRCP_OPERATION_ID_VOLUME_DOWN: 826 printf("AVRCP Target : VOLUME DOWN (%s)\n", button_state); 827 break; 828 default: 829 return; 830 } 831 break; 832 default: 833 printf("AVRCP Target : Event 0x%02x is not parsed\n", packet[2]); 834 break; 835 } 836 } 837 838 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 839 UNUSED(channel); 840 UNUSED(size); 841 if (packet_type != HCI_EVENT_PACKET) return; 842 if (hci_event_packet_get_type(packet) == HCI_EVENT_PIN_CODE_REQUEST) { 843 bd_addr_t address; 844 printf("Pin code request - using '0000'\n"); 845 hci_event_pin_code_request_get_bd_addr(packet, address); 846 gap_pin_code_response(address, "0000"); 847 } 848 } 849 850 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 851 UNUSED(channel); 852 UNUSED(size); 853 uint8_t status; 854 855 uint8_t allocation_method; 856 857 if (packet_type != HCI_EVENT_PACKET) return; 858 if (hci_event_packet_get_type(packet) != HCI_EVENT_A2DP_META) return; 859 860 a2dp_sink_demo_a2dp_connection_t * a2dp_conn = &a2dp_sink_demo_a2dp_connection; 861 862 switch (packet[2]){ 863 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 864 printf("A2DP Sink : Received non SBC codec - not implemented\n"); 865 break; 866 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ 867 printf("A2DP Sink : Received SBC codec configuration\n"); 868 a2dp_conn->sbc_configuration.reconfigure = a2dp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 869 a2dp_conn->sbc_configuration.num_channels = a2dp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet); 870 a2dp_conn->sbc_configuration.sampling_frequency = a2dp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); 871 a2dp_conn->sbc_configuration.block_length = a2dp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet); 872 a2dp_conn->sbc_configuration.subbands = a2dp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet); 873 a2dp_conn->sbc_configuration.min_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); 874 a2dp_conn->sbc_configuration.max_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); 875 876 allocation_method = a2dp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet); 877 878 // Adapt Bluetooth spec definition to SBC Encoder expected input 879 a2dp_conn->sbc_configuration.allocation_method = (btstack_sbc_allocation_method_t)(allocation_method - 1); 880 881 switch (a2dp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet)){ 882 case AVDTP_CHANNEL_MODE_JOINT_STEREO: 883 a2dp_conn->sbc_configuration.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO; 884 break; 885 case AVDTP_CHANNEL_MODE_STEREO: 886 a2dp_conn->sbc_configuration.channel_mode = SBC_CHANNEL_MODE_STEREO; 887 break; 888 case AVDTP_CHANNEL_MODE_DUAL_CHANNEL: 889 a2dp_conn->sbc_configuration.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL; 890 break; 891 case AVDTP_CHANNEL_MODE_MONO: 892 a2dp_conn->sbc_configuration.channel_mode = SBC_CHANNEL_MODE_MONO; 893 break; 894 default: 895 btstack_assert(false); 896 break; 897 } 898 dump_sbc_configuration(&a2dp_conn->sbc_configuration); 899 break; 900 } 901 902 case A2DP_SUBEVENT_STREAM_ESTABLISHED: 903 status = a2dp_subevent_stream_established_get_status(packet); 904 if (status != ERROR_CODE_SUCCESS){ 905 printf("A2DP Sink : Streaming connection failed, status 0x%02x\n", status); 906 break; 907 } 908 909 a2dp_subevent_stream_established_get_bd_addr(packet, a2dp_conn->addr); 910 a2dp_conn->a2dp_cid = a2dp_subevent_stream_established_get_a2dp_cid(packet); 911 a2dp_conn->stream_state = STREAM_STATE_OPEN; 912 913 printf("A2DP Sink : Streaming connection is established, address %s, cid 0x%02x, local seid %d\n", 914 bd_addr_to_str(a2dp_conn->addr), a2dp_conn->a2dp_cid, a2dp_conn->a2dp_local_seid); 915 #ifdef HAVE_BTSTACK_STDIN 916 // use address for outgoing connections 917 memcpy(device_addr, a2dp_conn->addr, 6); 918 #endif 919 break; 920 921 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION 922 case A2DP_SUBEVENT_START_STREAM_REQUESTED: 923 printf("A2DP Sink : Explicit Accept to start stream, local_seid %d\n", a2dp_subevent_start_stream_requested_get_local_seid(packet)); 924 a2dp_sink_start_stream_accept(a2dp_cid, a2dp_local_seid); 925 break; 926 #endif 927 case A2DP_SUBEVENT_STREAM_STARTED: 928 printf("A2DP Sink : Stream started\n"); 929 a2dp_conn->stream_state = STREAM_STATE_PLAYING; 930 if (a2dp_conn->sbc_configuration.reconfigure){ 931 media_processing_close(); 932 } 933 // prepare media processing 934 media_processing_init(&a2dp_conn->sbc_configuration); 935 // audio stream is started when buffer reaches minimal level 936 break; 937 938 case A2DP_SUBEVENT_STREAM_SUSPENDED: 939 printf("A2DP Sink : Stream paused\n"); 940 a2dp_conn->stream_state = STREAM_STATE_PAUSED; 941 media_processing_pause(); 942 break; 943 944 case A2DP_SUBEVENT_STREAM_RELEASED: 945 printf("A2DP Sink : Stream released\n"); 946 a2dp_conn->stream_state = STREAM_STATE_CLOSED; 947 media_processing_close(); 948 break; 949 950 case A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 951 printf("A2DP Sink : Signaling connection released\n"); 952 a2dp_conn->a2dp_cid = 0; 953 media_processing_close(); 954 break; 955 956 default: 957 break; 958 } 959 } 960 961 #ifdef HAVE_BTSTACK_STDIN 962 static void show_usage(void){ 963 bd_addr_t iut_address; 964 gap_local_bd_addr(iut_address); 965 printf("\n--- Bluetooth AVDTP Sink/AVRCP Connection Test Console %s ---\n", bd_addr_to_str(iut_address)); 966 printf("b - AVDTP Sink create connection to addr %s\n", bd_addr_to_str(device_addr)); 967 printf("B - AVDTP Sink disconnect\n"); 968 printf("c - AVRCP create connection to addr %s\n", bd_addr_to_str(device_addr)); 969 printf("C - AVRCP disconnect\n"); 970 971 printf("w - delay report\n"); 972 973 printf("\n--- Bluetooth AVRCP Commands %s ---\n", bd_addr_to_str(iut_address)); 974 printf("O - get play status\n"); 975 printf("j - get now playing info\n"); 976 printf("k - play\n"); 977 printf("K - stop\n"); 978 printf("L - pause\n"); 979 printf("u - start fast forward\n"); 980 printf("U - stop fast forward\n"); 981 printf("n - start rewind\n"); 982 printf("N - stop rewind\n"); 983 printf("i - forward\n"); 984 printf("I - backward\n"); 985 printf("M - mute\n"); 986 printf("r - skip\n"); 987 printf("q - query repeat and shuffle mode\n"); 988 printf("v - repeat single track\n"); 989 printf("x - repeat all tracks\n"); 990 printf("X - disable repeat mode\n"); 991 printf("z - shuffle all tracks\n"); 992 printf("Z - disable shuffle mode\n"); 993 994 printf("a/A - register/deregister TRACK_CHANGED\n"); 995 printf("R/P - register/deregister PLAYBACK_POS_CHANGED\n"); 996 997 printf("s/S - send/release long button press REWIND\n"); 998 999 printf("\n--- Volume and Battery Control ---\n"); 1000 printf("t - volume up for 10 percent\n"); 1001 printf("T - volume down for 10 percent\n"); 1002 printf("V - toggle Battery status from AVRCP_BATTERY_STATUS_NORMAL to AVRCP_BATTERY_STATUS_FULL_CHARGE\n"); 1003 printf("---\n"); 1004 } 1005 #endif 1006 1007 #ifdef HAVE_BTSTACK_STDIN 1008 static void stdin_process(char cmd){ 1009 uint8_t status = ERROR_CODE_SUCCESS; 1010 uint8_t volume; 1011 avrcp_battery_status_t old_battery_status; 1012 1013 a2dp_sink_demo_stream_endpoint_t * stream_endpoint = &a2dp_sink_demo_stream_endpoint; 1014 a2dp_sink_demo_a2dp_connection_t * a2dp_connection = &a2dp_sink_demo_a2dp_connection; 1015 a2dp_sink_demo_avrcp_connection_t * avrcp_connection = &a2dp_sink_demo_avrcp_connection; 1016 1017 switch (cmd){ 1018 case 'b': 1019 status = a2dp_sink_establish_stream(device_addr, stream_endpoint->a2dp_local_seid, &a2dp_connection->a2dp_cid); 1020 printf(" - Create AVDTP connection to addr %s, and local seid %d, cid 0x%02x.\n", 1021 bd_addr_to_str(device_addr), a2dp_connection->a2dp_local_seid, a2dp_connection->a2dp_cid); 1022 break; 1023 case 'B': 1024 printf(" - AVDTP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 1025 a2dp_sink_disconnect(a2dp_connection->a2dp_cid); 1026 break; 1027 case 'c': 1028 printf(" - Create AVRCP connection to addr %s.\n", bd_addr_to_str(device_addr)); 1029 status = avrcp_connect(device_addr, &avrcp_connection->avrcp_cid); 1030 break; 1031 case 'C': 1032 printf(" - AVRCP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 1033 status = avrcp_disconnect(avrcp_connection->avrcp_cid); 1034 break; 1035 1036 case '\n': 1037 case '\r': 1038 break; 1039 case 'w': 1040 printf("Send delay report\n"); 1041 avdtp_sink_delay_report(a2dp_connection->a2dp_cid, a2dp_connection->a2dp_local_seid, 100); 1042 break; 1043 // Volume Control 1044 case 't': 1045 volume_percentage = volume_percentage <= 90 ? volume_percentage + 10 : 100; 1046 volume = volume_percentage * 127 / 100; 1047 printf(" - volume up for 10 percent, %d%% (%d) \n", volume_percentage, volume); 1048 status = avrcp_target_volume_changed(avrcp_connection->avrcp_cid, volume); 1049 avrcp_volume_changed(volume); 1050 break; 1051 case 'T': 1052 volume_percentage = volume_percentage >= 10 ? volume_percentage - 10 : 0; 1053 volume = volume_percentage * 127 / 100; 1054 printf(" - volume down for 10 percent, %d%% (%d) \n", volume_percentage, volume); 1055 status = avrcp_target_volume_changed(avrcp_connection->avrcp_cid, volume); 1056 avrcp_volume_changed(volume); 1057 break; 1058 case 'V': 1059 old_battery_status = battery_status; 1060 1061 if (battery_status < AVRCP_BATTERY_STATUS_FULL_CHARGE){ 1062 battery_status = (avrcp_battery_status_t)((uint8_t) battery_status + 1); 1063 } else { 1064 battery_status = AVRCP_BATTERY_STATUS_NORMAL; 1065 } 1066 printf(" - toggle battery value, old %d, new %d\n", old_battery_status, battery_status); 1067 status = avrcp_target_battery_status_changed(avrcp_connection->avrcp_cid, battery_status); 1068 break; 1069 case 'O': 1070 printf(" - get play status\n"); 1071 status = avrcp_controller_get_play_status(avrcp_connection->avrcp_cid); 1072 break; 1073 case 'j': 1074 printf(" - get now playing info\n"); 1075 status = avrcp_controller_get_now_playing_info(avrcp_connection->avrcp_cid); 1076 break; 1077 case 'k': 1078 printf(" - play\n"); 1079 status = avrcp_controller_play(avrcp_connection->avrcp_cid); 1080 break; 1081 case 'K': 1082 printf(" - stop\n"); 1083 status = avrcp_controller_stop(avrcp_connection->avrcp_cid); 1084 break; 1085 case 'L': 1086 printf(" - pause\n"); 1087 status = avrcp_controller_pause(avrcp_connection->avrcp_cid); 1088 break; 1089 case 'u': 1090 printf(" - start fast forward\n"); 1091 status = avrcp_controller_press_and_hold_fast_forward(avrcp_connection->avrcp_cid); 1092 break; 1093 case 'U': 1094 printf(" - stop fast forward\n"); 1095 status = avrcp_controller_release_press_and_hold_cmd(avrcp_connection->avrcp_cid); 1096 break; 1097 case 'n': 1098 printf(" - start rewind\n"); 1099 status = avrcp_controller_press_and_hold_rewind(avrcp_connection->avrcp_cid); 1100 break; 1101 case 'N': 1102 printf(" - stop rewind\n"); 1103 status = avrcp_controller_release_press_and_hold_cmd(avrcp_connection->avrcp_cid); 1104 break; 1105 case 'i': 1106 printf(" - forward\n"); 1107 status = avrcp_controller_forward(avrcp_connection->avrcp_cid); 1108 break; 1109 case 'I': 1110 printf(" - backward\n"); 1111 status = avrcp_controller_backward(avrcp_connection->avrcp_cid); 1112 break; 1113 case 'M': 1114 printf(" - mute\n"); 1115 status = avrcp_controller_mute(avrcp_connection->avrcp_cid); 1116 break; 1117 case 'r': 1118 printf(" - skip\n"); 1119 status = avrcp_controller_skip(avrcp_connection->avrcp_cid); 1120 break; 1121 case 'q': 1122 printf(" - query repeat and shuffle mode\n"); 1123 status = avrcp_controller_query_shuffle_and_repeat_modes(avrcp_connection->avrcp_cid); 1124 break; 1125 case 'v': 1126 printf(" - repeat single track\n"); 1127 status = avrcp_controller_set_repeat_mode(avrcp_connection->avrcp_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK); 1128 break; 1129 case 'x': 1130 printf(" - repeat all tracks\n"); 1131 status = avrcp_controller_set_repeat_mode(avrcp_connection->avrcp_cid, AVRCP_REPEAT_MODE_ALL_TRACKS); 1132 break; 1133 case 'X': 1134 printf(" - disable repeat mode\n"); 1135 status = avrcp_controller_set_repeat_mode(avrcp_connection->avrcp_cid, AVRCP_REPEAT_MODE_OFF); 1136 break; 1137 case 'z': 1138 printf(" - shuffle all tracks\n"); 1139 status = avrcp_controller_set_shuffle_mode(avrcp_connection->avrcp_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS); 1140 break; 1141 case 'Z': 1142 printf(" - disable shuffle mode\n"); 1143 status = avrcp_controller_set_shuffle_mode(avrcp_connection->avrcp_cid, AVRCP_SHUFFLE_MODE_OFF); 1144 break; 1145 case 'a': 1146 printf("AVRCP: enable notification TRACK_CHANGED\n"); 1147 status = avrcp_controller_enable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 1148 break; 1149 case 'A': 1150 printf("AVRCP: disable notification TRACK_CHANGED\n"); 1151 status = avrcp_controller_disable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 1152 break; 1153 case 'R': 1154 printf("AVRCP: enable notification PLAYBACK_POS_CHANGED\n"); 1155 status = avrcp_controller_enable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED); 1156 break; 1157 case 'P': 1158 printf("AVRCP: disable notification PLAYBACK_POS_CHANGED\n"); 1159 status = avrcp_controller_disable_notification(avrcp_connection->avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED); 1160 break; 1161 case 's': 1162 printf("AVRCP: send long button press REWIND\n"); 1163 status = avrcp_controller_start_press_and_hold_cmd(avrcp_connection->avrcp_cid, AVRCP_OPERATION_ID_REWIND); 1164 break; 1165 case 'S': 1166 printf("AVRCP: release long button press REWIND\n"); 1167 status = avrcp_controller_release_press_and_hold_cmd(avrcp_connection->avrcp_cid); 1168 break; 1169 default: 1170 show_usage(); 1171 return; 1172 } 1173 if (status != ERROR_CODE_SUCCESS){ 1174 printf("Could not perform command, status 0x%02x\n", status); 1175 } 1176 } 1177 #endif 1178 1179 int btstack_main(int argc, const char * argv[]); 1180 int btstack_main(int argc, const char * argv[]){ 1181 UNUSED(argc); 1182 (void)argv; 1183 1184 setup_demo(); 1185 1186 #ifdef HAVE_BTSTACK_STDIN 1187 // parse human-readable Bluetooth address 1188 sscanf_bd_addr(device_addr_string, device_addr); 1189 btstack_stdin_setup(stdin_process); 1190 #endif 1191 1192 // turn on! 1193 printf("Starting BTstack ...\n"); 1194 hci_power_control(HCI_POWER_ON); 1195 return 0; 1196 } 1197 /* EXAMPLE_END */ 1198