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