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