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