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