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): Receive audio stream and control its 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. 52 * 53 * @test To test with a remote device, e.g. a mobile phone, 54 * pair from the remote device with the demo, then start playing music on the remote device. 55 * Alternatively, set the device_addr_string to the Bluetooth address of your 56 * remote device in the code, and call connect from the UI. 57 * 58 * @test To controll the playback, tap SPACE on the console to show the available 59 * AVRCP commands. 60 */ 61 // ***************************************************************************** 62 63 #include <stdint.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 68 #include "btstack.h" 69 70 #define AVRCP_BROWSING_ENABLED 0 71 72 #ifdef HAVE_BTSTACK_STDIN 73 #include "btstack_stdin.h" 74 #endif 75 76 #ifdef HAVE_AUDIO_DMA 77 #include "btstack_ring_buffer.h" 78 #include "hal_audio_dma.h" 79 #endif 80 81 #ifdef HAVE_PORTAUDIO 82 #include "btstack_ring_buffer.h" 83 #include <portaudio.h> 84 #endif 85 86 #ifdef HAVE_POSIX_FILE_IO 87 #include "wav_util.h" 88 #define STORE_SBC_TO_SBC_FILE 89 #define STORE_SBC_TO_WAV_FILE 90 #endif 91 92 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) || defined(HAVE_AUDIO_DMA) 93 #define DECODE_SBC 94 #endif 95 96 #define NUM_CHANNELS 2 97 #define BYTES_PER_FRAME (2*NUM_CHANNELS) 98 #define MAX_SBC_FRAME_SIZE 120 99 100 // SBC Decoder for WAV file or PortAudio 101 #ifdef DECODE_SBC 102 static btstack_sbc_decoder_state_t state; 103 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 104 #endif 105 106 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 107 #define PREBUFFER_MS 200 108 static int audio_stream_started = 0; 109 static int audio_stream_paused = 0; 110 static btstack_ring_buffer_t ring_buffer; 111 #endif 112 113 #ifdef HAVE_AUDIO_DMA 114 // below 30: add samples, 30-40: fine, above 40: drop samples 115 #define OPTIMAL_FRAMES_MIN 30 116 #define OPTIMAL_FRAMES_MAX 40 117 #define ADDITIONAL_FRAMES 10 118 #define DMA_AUDIO_FRAMES 128 119 #define DMA_MAX_FILL_FRAMES 1 120 #define NUM_AUDIO_BUFFERS 2 121 122 static uint16_t audio_samples[(DMA_AUDIO_FRAMES + DMA_MAX_FILL_FRAMES)*2*NUM_AUDIO_BUFFERS]; 123 static uint16_t audio_samples_len[NUM_AUDIO_BUFFERS]; 124 static uint8_t ring_buffer_storage[(OPTIMAL_FRAMES_MAX + ADDITIONAL_FRAMES) * MAX_SBC_FRAME_SIZE]; 125 static const uint16_t silent_buffer[DMA_AUDIO_FRAMES*2]; 126 static volatile int playback_buffer; 127 static int write_buffer; 128 static uint8_t sbc_frame_size; 129 static int sbc_samples_fix; 130 #endif 131 132 // PortAudio - live playback 133 #ifdef HAVE_PORTAUDIO 134 #define PA_SAMPLE_TYPE paInt16 135 #define SAMPLE_RATE 48000 136 #define FRAMES_PER_BUFFER 128 137 #define PREBUFFER_BYTES (PREBUFFER_MS*SAMPLE_RATE/1000*BYTES_PER_FRAME) 138 static PaStream * stream; 139 static uint8_t ring_buffer_storage[2*PREBUFFER_BYTES]; 140 #endif 141 142 // WAV File 143 #ifdef STORE_SBC_TO_WAV_FILE 144 static int frame_count = 0; 145 static char * wav_filename = "avdtp_sink.wav"; 146 #endif 147 148 #ifdef STORE_SBC_TO_SBC_FILE 149 static FILE * sbc_file; 150 static char * sbc_filename = "avdtp_sink.sbc"; 151 #endif 152 153 typedef struct { 154 // bitmaps 155 uint8_t sampling_frequency_bitmap; 156 uint8_t channel_mode_bitmap; 157 uint8_t block_length_bitmap; 158 uint8_t subbands_bitmap; 159 uint8_t allocation_method_bitmap; 160 uint8_t min_bitpool_value; 161 uint8_t max_bitpool_value; 162 } adtvp_media_codec_information_sbc_t; 163 164 typedef struct { 165 int reconfigure; 166 int num_channels; 167 int sampling_frequency; 168 int channel_mode; 169 int block_length; 170 int subbands; 171 int allocation_method; 172 int min_bitpool_value; 173 int max_bitpool_value; 174 int frames_per_buffer; 175 } avdtp_media_codec_configuration_sbc_t; 176 177 #ifdef HAVE_BTSTACK_STDIN 178 // mac 2011: static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3}; 179 // pts: static bd_addr_t remote = {0x00, 0x1B, 0xDC, 0x08, 0x0A, 0xA5}; 180 // mac 2013: 181 // mac 2013: static const char * device_addr_string = "84:38:35:65:d1:15"; 182 // iPhone 5S: static const char * device_addr_string = "54:E4:3A:26:A2:39"; 183 // BT dongle: 184 static const char * device_addr_string = "00:02:72:DC:31:C1"; 185 #endif 186 187 // bt dongle: -u 02-02 static bd_addr_t remote = {0x00, 0x02, 0x72, 0xDC, 0x31, 0xC1}; 188 189 static uint8_t sdp_avdtp_sink_service_buffer[150]; 190 static avdtp_media_codec_configuration_sbc_t sbc_configuration; 191 static uint16_t a2dp_cid = 0; 192 static uint8_t local_seid = 0; 193 static uint8_t value[100]; 194 195 static btstack_packet_callback_registration_t hci_event_callback_registration; 196 197 static int media_initialized = 0; 198 199 #ifdef HAVE_BTSTACK_STDIN 200 static bd_addr_t device_addr; 201 #endif 202 203 static uint16_t a2dp_sink_connected = 0; 204 static uint16_t avrcp_cid = 0; 205 static uint8_t avrcp_connected = 0; 206 static uint8_t sdp_avrcp_controller_service_buffer[200]; 207 208 static uint8_t media_sbc_codec_capabilities[] = { 209 0xFF,//(AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 210 0xFF,//(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 211 2, 53 212 }; 213 214 static uint8_t media_sbc_codec_configuration[] = { 215 (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO, 216 (AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS, 217 2, 53 218 }; 219 220 221 /* @section Main Application Setup 222 * 223 * @text The Listing MainConfiguration shows how to setup AD2P Sink and AVRCP controller services. 224 * To announce A2DP Sink and AVRCP Controller services, you need to create corresponding 225 * SDP records and register them with the SDP service. 226 * You'll also need to register several packet handlers: 227 * - 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). 228 * - handle_l2cap_media_data_packet - used to receive streaming data. If HAVE_PORTAUDIO or STORE_SBC_TO_WAV_FILE directives (check btstack_config.h) are 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. 229 * - stdin_process callback - used to trigger AVRCP commands to the A2DP Source device, such are get now playing info, start, stop, volume control. Requires HAVE_BTSTACK_STDIN. 230 * - avrcp_controller_packet_handler - used to receive answers for AVRCP commands, 231 * 232 * @text Note, currently only the SBC codec is supported. 233 * If you want to store the audio data in a file, you'll need to define STORE_SBC_TO_WAV_FILE. The HAVE_PORTAUDIO directive indicates if the audio is played back via PortAudio. 234 * If HAVE_PORTAUDIO or STORE_SBC_TO_WAV_FILE directives is defined, the SBC decoder needs to get initialized when a2dp_sink_packet_handler receives event A2DP_SUBEVENT_STREAM_STARTED. 235 * The initialization of the SBC decoder requires a callback that handles PCM data: 236 * - handle_pcm_data - handles PCM audio frames. Here, they are stored a in wav file if STORE_SBC_TO_WAV_FILE is defined, and/or played using the PortAudio library if HAVE_PORTAUDIO is defined. 237 */ 238 239 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP Controller services */ 240 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size); 241 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 242 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size); 243 #ifdef HAVE_BTSTACK_STDIN 244 static void stdin_process(char cmd); 245 #endif 246 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) 247 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context); 248 #endif 249 250 static int a2dp_sink_and_avrcp_services_init(void){ 251 // Register for HCI events. 252 hci_event_callback_registration.callback = &a2dp_sink_packet_handler; 253 hci_add_event_handler(&hci_event_callback_registration); 254 255 // Initialize L2CAP. 256 l2cap_init(); 257 258 // Initialize A2DP Sink. 259 a2dp_sink_init(); 260 // Register A2DP Sink for HCI events. 261 a2dp_sink_register_packet_handler(&a2dp_sink_packet_handler); 262 // Register A2DP Sink for receiving media data. 263 a2dp_sink_register_media_handler(&handle_l2cap_media_data_packet); 264 // Create a stream endpoint to which the streaming channel will be opened. 265 uint8_t status = a2dp_sink_create_stream_endpoint(AVDTP_AUDIO, AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration), &local_seid); 266 if (status != ERROR_CODE_SUCCESS){ 267 printf("A2DP Sink: not enough memory to create local stream endpoint\n"); 268 return 1; 269 } 270 271 // Initialize AVRCP Controller. 272 avrcp_controller_init(); 273 // Register AVRCP for HCI events. 274 avrcp_controller_register_packet_handler(&avrcp_controller_packet_handler); 275 276 // Initialize SDP. 277 sdp_init(); 278 279 // Create A2DP sink service record and register it with SDP. 280 memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer)); 281 a2dp_sink_create_sdp_record(sdp_avdtp_sink_service_buffer, 0x10001, 1, NULL, NULL); 282 sdp_register_service(sdp_avdtp_sink_service_buffer); 283 284 // Create AVRCP service record and register it with SDP. 285 memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer)); 286 avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, 0x10001, AVRCP_BROWSING_ENABLED, 1, NULL, NULL); 287 sdp_register_service(sdp_avrcp_controller_service_buffer); 288 289 // Set local name with a template Bluetooth address, that will be automatically 290 // replaced with a actual address once it is available, i.e. when BTstack boots 291 // up and starts talking to a Bluetooth module. 292 gap_set_local_name("A2DP Sink Demo 00:00:00:00:00:00"); 293 gap_discoverable_control(1); 294 gap_set_class_of_device(0x200408); 295 296 #ifdef HAVE_AUDIO_DMA 297 static btstack_data_source_t hal_audio_dma_data_source; 298 // Set up polling data source. 299 btstack_run_loop_set_data_source_handler(&hal_audio_dma_data_source, &hal_audio_dma_process); 300 btstack_run_loop_enable_data_source_callbacks(&hal_audio_dma_data_source, DATA_SOURCE_CALLBACK_POLL); 301 btstack_run_loop_add_data_source(&hal_audio_dma_data_source); 302 #endif 303 304 #ifdef HAVE_BTSTACK_STDIN 305 // Parse human readable Bluetooth address. 306 sscanf_bd_addr(device_addr_string, device_addr); 307 btstack_stdin_setup(stdin_process); 308 #endif 309 return 0; 310 } 311 /* LISTING_END */ 312 313 #ifdef HAVE_PORTAUDIO 314 static int portaudio_callback( const void *inputBuffer, void *outputBuffer, 315 unsigned long framesPerBuffer, 316 const PaStreamCallbackTimeInfo* timeInfo, 317 PaStreamCallbackFlags statusFlags, 318 void *userData ) { 319 320 /** portaudio_callback is called from different thread, don't use hci_dump / log_info here without additional checks */ 321 322 // Prevent unused variable warnings. 323 (void) timeInfo; 324 (void) statusFlags; 325 (void) inputBuffer; 326 (void) userData; 327 328 int bytes_to_copy = framesPerBuffer * BYTES_PER_FRAME; 329 330 // fill ring buffer with silence while stream is paused 331 if (audio_stream_paused){ 332 if (btstack_ring_buffer_bytes_available(&ring_buffer) < PREBUFFER_BYTES){ 333 memset(outputBuffer, 0, bytes_to_copy); 334 return 0; 335 } else { 336 // resume playback 337 audio_stream_paused = 0; 338 } 339 } 340 341 // get data from ring buffer 342 uint32_t bytes_read = 0; 343 btstack_ring_buffer_read(&ring_buffer, outputBuffer, bytes_to_copy, &bytes_read); 344 bytes_to_copy -= bytes_read; 345 346 // fill ring buffer with silence if there are not enough bytes to copy 347 if (bytes_to_copy){ 348 memset(outputBuffer + bytes_read, 0, bytes_to_copy); 349 audio_stream_paused = 1; 350 } 351 return 0; 352 } 353 #endif 354 355 #ifdef HAVE_AUDIO_DMA 356 static int next_buffer(int current){ 357 if (current == NUM_AUDIO_BUFFERS-1) return 0; 358 return current + 1; 359 } 360 static uint8_t * start_of_buffer(int num){ 361 return (uint8_t *) &audio_samples[num * DMA_AUDIO_FRAMES * 2]; 362 } 363 void hal_audio_dma_done(void){ 364 if (audio_stream_paused){ 365 hal_audio_dma_play((const uint8_t *) silent_buffer, DMA_AUDIO_FRAMES*4); 366 return; 367 } 368 // next buffer 369 int next_playback_buffer = next_buffer(playback_buffer); 370 uint8_t * playback_data; 371 if (next_playback_buffer == write_buffer){ 372 373 // TODO: stop codec while playing silence when getting 'stream paused' 374 375 // start playing silence 376 audio_stream_paused = 1; 377 hal_audio_dma_play((const uint8_t *) silent_buffer, DMA_AUDIO_FRAMES*4); 378 printf("%6u - paused - bytes in buffer %u\n", (int) btstack_run_loop_get_time_ms(), btstack_ring_buffer_bytes_available(&ring_buffer)); 379 return; 380 } 381 playback_buffer = next_playback_buffer; 382 playback_data = start_of_buffer(playback_buffer); 383 hal_audio_dma_play(playback_data, audio_samples_len[playback_buffer]); 384 // btstack_run_loop_embedded_trigger(); 385 } 386 #endif 387 388 389 #ifdef HAVE_AUDIO_DMA 390 static void hal_audio_dma_process(btstack_data_source_t * ds, btstack_data_source_callback_type_t callback_type){ 391 UNUSED(ds); 392 UNUSED(callback_type); 393 394 if (!media_initialized) return; 395 396 int trigger_resume = 0; 397 if (audio_stream_paused) { 398 if (sbc_frame_size && btstack_ring_buffer_bytes_available(&ring_buffer) >= OPTIMAL_FRAMES_MIN * sbc_frame_size){ 399 trigger_resume = 1; 400 // reset buffers 401 playback_buffer = NUM_AUDIO_BUFFERS - 1; 402 write_buffer = 0; 403 } else { 404 return; 405 } 406 } 407 408 while (playback_buffer != write_buffer && btstack_ring_buffer_bytes_available(&ring_buffer) >= sbc_frame_size ){ 409 uint8_t frame[MAX_SBC_FRAME_SIZE]; 410 uint32_t bytes_read = 0; 411 btstack_ring_buffer_read(&ring_buffer, frame, sbc_frame_size, &bytes_read); 412 btstack_sbc_decoder_process_data(&state, 0, frame, sbc_frame_size); 413 } 414 415 if (trigger_resume){ 416 printf("%6u - resume\n", (int) btstack_run_loop_get_time_ms()); 417 audio_stream_paused = 0; 418 } 419 } 420 #endif 421 422 static int media_processing_init(avdtp_media_codec_configuration_sbc_t configuration){ 423 if (media_initialized) return 0; 424 #ifdef DECODE_SBC 425 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 426 #endif 427 428 #ifdef STORE_SBC_TO_WAV_FILE 429 wav_writer_open(wav_filename, configuration.num_channels, configuration.sampling_frequency); 430 #endif 431 432 #ifdef STORE_SBC_TO_SBC_FILE 433 sbc_file = fopen(sbc_filename, "wb"); 434 #endif 435 436 #ifdef HAVE_PORTAUDIO 437 // int frames_per_buffer = configuration.frames_per_buffer; 438 PaError err; 439 PaStreamParameters outputParameters; 440 const PaDeviceInfo *deviceInfo; 441 442 /* -- initialize PortAudio -- */ 443 err = Pa_Initialize(); 444 if (err != paNoError){ 445 printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); 446 return err; 447 } 448 /* -- setup input and output -- */ 449 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 450 outputParameters.channelCount = configuration.num_channels; 451 outputParameters.sampleFormat = PA_SAMPLE_TYPE; 452 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; 453 outputParameters.hostApiSpecificStreamInfo = NULL; 454 deviceInfo = Pa_GetDeviceInfo( outputParameters.device ); 455 printf("PortAudio: Output device: %s\n", deviceInfo->name); 456 log_info("PortAudio: Output device: %s", deviceInfo->name); 457 /* -- setup stream -- */ 458 err = Pa_OpenStream( 459 &stream, 460 NULL, /* &inputParameters */ 461 &outputParameters, 462 configuration.sampling_frequency, 463 0, 464 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 465 portaudio_callback, /* use callback */ 466 NULL ); 467 468 if (err != paNoError){ 469 printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); 470 return err; 471 } 472 log_info("PortAudio: stream opened"); 473 printf("PortAudio: stream opened\n"); 474 #endif 475 #ifdef HAVE_AUDIO_DMA 476 audio_stream_paused = 1; 477 hal_audio_dma_init(configuration.sampling_frequency); 478 hal_audio_dma_set_audio_played(&hal_audio_dma_done); 479 // start playing silence 480 hal_audio_dma_done(); 481 #endif 482 483 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 484 memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage)); 485 btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage)); 486 audio_stream_started = 0; 487 audio_stream_paused = 0; 488 #endif 489 media_initialized = 1; 490 return 0; 491 } 492 493 static void media_processing_close(void){ 494 if (!media_initialized) return; 495 media_initialized = 0; 496 497 #ifdef STORE_SBC_TO_WAV_FILE 498 wav_writer_close(); 499 int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 500 501 printf("WAV Writer: Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", total_frames_nr, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr); 502 printf("WAV Writer: Written %d frames to wav file: %s\n", frame_count, wav_filename); 503 #endif 504 505 #ifdef STORE_SBC_TO_SBC_FILE 506 fclose(sbc_file); 507 #endif 508 509 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA) 510 audio_stream_started = 0; 511 #endif 512 513 #ifdef HAVE_PORTAUDIO 514 printf("PortAudio: Stream closed\n"); 515 log_info("PortAudio: Stream closed"); 516 517 PaError err = Pa_StopStream(stream); 518 if (err != paNoError){ 519 printf("Error stopping the stream: \"%s\"\n", Pa_GetErrorText(err)); 520 log_error("Error stopping the stream: \"%s\"", Pa_GetErrorText(err)); 521 return; 522 } 523 err = Pa_CloseStream(stream); 524 if (err != paNoError){ 525 printf("Error closing the stream: \"%s\"\n", Pa_GetErrorText(err)); 526 log_error("Error closing the stream: \"%s\"", Pa_GetErrorText(err)); 527 return; 528 } 529 err = Pa_Terminate(); 530 if (err != paNoError){ 531 printf("Error terminating portaudio: \"%s\"\n", Pa_GetErrorText(err)); 532 log_error("Error terminating portaudio: \"%s\"", Pa_GetErrorText(err)); 533 return; 534 } 535 #endif 536 537 #ifdef HAVE_AUDIO_DMA 538 hal_audio_dma_close(); 539 #endif 540 } 541 542 543 /* @section Handle Media Data Packet 544 * 545 * @text Media data packets, in this case the audio data, are received through the handle_l2cap_media_data_packet callback. 546 * Currently, only the SBC media codec is supported. Hence, the media data consists of the media packet header and the SBC packet. 547 * The SBC data will be decoded using an SBC decoder if either HAVE_PORTAUDIO or STORE_SBC_TO_WAV_FILE directive is defined. 548 * The resulting PCM frames can be then captured through a PCM data callback registered during SBC decoder setup, i.e. the 549 * handle_pcm_data callback. 550 */ 551 552 static int read_media_data_header(uint8_t * packet, int size, int * offset, avdtp_media_packet_header_t * media_header); 553 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header); 554 555 static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16_t size){ 556 UNUSED(seid); 557 int pos = 0; 558 559 avdtp_media_packet_header_t media_header; 560 if (!read_media_data_header(packet, size, &pos, &media_header)) return; 561 562 avdtp_sbc_codec_header_t sbc_header; 563 if (!read_sbc_header(packet, size, &pos, &sbc_header)) return; 564 565 #ifdef HAVE_AUDIO_DMA 566 // store sbc frame size for buffer management 567 sbc_frame_size = (size-pos)/ sbc_header.num_frames; 568 #endif 569 570 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) 571 btstack_sbc_decoder_process_data(&state, 0, packet+pos, size-pos); 572 #endif 573 574 #ifdef HAVE_AUDIO_DMA 575 btstack_ring_buffer_write(&ring_buffer, packet+pos, size-pos); 576 577 // decide on audio sync drift based on number of sbc frames in queue 578 int sbc_frames_in_buffer = btstack_ring_buffer_bytes_available(&ring_buffer) / sbc_frame_size; 579 if (sbc_frames_in_buffer < OPTIMAL_FRAMES_MIN){ 580 sbc_samples_fix = 1; // duplicate last sample 581 } else if (sbc_frames_in_buffer <= OPTIMAL_FRAMES_MAX){ 582 sbc_samples_fix = 0; // nothing to do 583 } else { 584 sbc_samples_fix = -1; // drop last sample 585 } 586 587 // dump 588 printf("%6u %03u %d\n", (int) btstack_run_loop_get_time_ms(), sbc_frames_in_buffer, sbc_samples_fix); 589 #endif 590 591 #ifdef STORE_SBC_TO_SBC_FILE 592 fwrite(packet+pos, size-pos, 1, sbc_file); 593 #endif 594 } 595 596 /* @section Handle PCM Data 597 * 598 * @text In this example, we use the [PortAudio library](http://www.portaudio.com) to play the audio stream. 599 * The PCM data are bufferd in a ring buffer. 600 * Aditionally, tha audio data can be stored in the avdtp_sink.wav file. 601 */ 602 #if defined(HAVE_PORTAUDIO) || defined(STORE_SBC_TO_WAV_FILE) || defined(HAVE_AUDIO_DMA) 603 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 604 UNUSED(sample_rate); 605 UNUSED(context); 606 607 #ifdef STORE_SBC_TO_WAV_FILE 608 wav_writer_write_int16(num_samples*num_channels, data); 609 frame_count++; 610 #endif 611 612 #ifdef HAVE_PORTAUDIO 613 // store pcm samples in ring buffer 614 btstack_ring_buffer_write(&ring_buffer, (uint8_t *)data, num_samples*num_channels*2); 615 616 if (!audio_stream_started){ 617 audio_stream_paused = 1; 618 /* -- start stream -- */ 619 PaError err = Pa_StartStream(stream); 620 if (err != paNoError){ 621 printf("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err)); 622 return; 623 } 624 audio_stream_started = 1; 625 } 626 #endif 627 628 #ifdef HAVE_AUDIO_DMA 629 // store in ring buffer 630 uint8_t * write_data = start_of_buffer(write_buffer); 631 uint16_t len = num_samples*num_channels*2; 632 memcpy(write_data, data, len); 633 audio_samples_len[write_buffer] = len; 634 635 // add/drop audio frame to fix drift 636 if (sbc_samples_fix > 0){ 637 memcpy(write_data + len, write_data + len - 4, 4); 638 audio_samples_len[write_buffer] += 4; 639 } 640 if (sbc_samples_fix < 0){ 641 audio_samples_len[write_buffer] -= 4; 642 } 643 644 write_buffer = next_buffer(write_buffer); 645 #endif 646 } 647 #endif 648 649 static int read_sbc_header(uint8_t * packet, int size, int * offset, avdtp_sbc_codec_header_t * sbc_header){ 650 int sbc_header_len = 12; // without crc 651 int pos = *offset; 652 653 if (size - pos < sbc_header_len){ 654 printf("Not enough data to read SBC header, expected %d, received %d\n", sbc_header_len, size-pos); 655 return 0; 656 } 657 658 sbc_header->fragmentation = get_bit16(packet[pos], 7); 659 sbc_header->starting_packet = get_bit16(packet[pos], 6); 660 sbc_header->last_packet = get_bit16(packet[pos], 5); 661 sbc_header->num_frames = packet[pos] & 0x0f; 662 pos++; 663 // printf("SBC HEADER: num_frames %u, fragmented %u, start %u, stop %u\n", sbc_header.num_frames, sbc_header.fragmentation, sbc_header.starting_packet, sbc_header.last_packet); 664 *offset = pos; 665 return 1; 666 } 667 668 static int read_media_data_header(uint8_t *packet, int size, int *offset, avdtp_media_packet_header_t *media_header){ 669 int media_header_len = 12; // without crc 670 int pos = *offset; 671 672 if (size - pos < media_header_len){ 673 printf("Not enough data to read media packet header, expected %d, received %d\n", media_header_len, size-pos); 674 return 0; 675 } 676 677 media_header->version = packet[pos] & 0x03; 678 media_header->padding = get_bit16(packet[pos],2); 679 media_header->extension = get_bit16(packet[pos],3); 680 media_header->csrc_count = (packet[pos] >> 4) & 0x0F; 681 pos++; 682 683 media_header->marker = get_bit16(packet[pos],0); 684 media_header->payload_type = (packet[pos] >> 1) & 0x7F; 685 pos++; 686 687 media_header->sequence_number = big_endian_read_16(packet, pos); 688 pos+=2; 689 690 media_header->timestamp = big_endian_read_32(packet, pos); 691 pos+=4; 692 693 media_header->synchronization_source = big_endian_read_32(packet, pos); 694 pos+=4; 695 *offset = pos; 696 // TODO: read csrc list 697 698 // printf_hexdump( packet, pos ); 699 // printf("MEDIA HEADER: %u timestamp, version %u, padding %u, extension %u, csrc_count %u\n", 700 // media_header->timestamp, media_header->version, media_header->padding, media_header->extension, media_header->csrc_count); 701 // printf("MEDIA HEADER: marker %02x, payload_type %02x, sequence_number %u, synchronization_source %u\n", 702 // media_header->marker, media_header->payload_type, media_header->sequence_number, media_header->synchronization_source); 703 return 1; 704 } 705 706 static void dump_sbc_configuration(avdtp_media_codec_configuration_sbc_t configuration){ 707 printf("Received SBC configuration:\n"); 708 printf(" - num_channels: %d\n", configuration.num_channels); 709 printf(" - sampling_frequency: %d\n", configuration.sampling_frequency); 710 printf(" - channel_mode: %d\n", configuration.channel_mode); 711 printf(" - block_length: %d\n", configuration.block_length); 712 printf(" - subbands: %d\n", configuration.subbands); 713 printf(" - allocation_method: %d\n", configuration.allocation_method); 714 printf(" - bitpool_value [%d, %d] \n", configuration.min_bitpool_value, configuration.max_bitpool_value); 715 printf("\n"); 716 } 717 718 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 719 UNUSED(channel); 720 UNUSED(size); 721 uint16_t local_cid; 722 uint8_t status = 0xFF; 723 bd_addr_t adress; 724 725 if (packet_type != HCI_EVENT_PACKET) return; 726 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return; 727 switch (packet[2]){ 728 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: { 729 local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet); 730 if (avrcp_cid != 0 && avrcp_cid != local_cid) { 731 printf("AVRCP demo: Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", avrcp_cid, local_cid); 732 return; 733 } 734 735 status = avrcp_subevent_connection_established_get_status(packet); 736 if (status != ERROR_CODE_SUCCESS){ 737 printf("AVRCP demo: Connection failed: status 0x%02x\n", status); 738 avrcp_cid = 0; 739 return; 740 } 741 742 avrcp_cid = local_cid; 743 avrcp_connected = 1; 744 avrcp_subevent_connection_established_get_bd_addr(packet, adress); 745 printf("AVRCP demo: Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(adress), avrcp_cid); 746 747 // automatically enable notifications 748 avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED); 749 avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED); 750 avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED); 751 avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 752 return; 753 } 754 case AVRCP_SUBEVENT_CONNECTION_RELEASED: 755 printf("AVRCP demo: Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet)); 756 avrcp_cid = 0; 757 avrcp_connected = 0; 758 return; 759 default: 760 break; 761 } 762 763 status = packet[5]; 764 if (!avrcp_cid) return; 765 766 // ignore INTERIM status 767 if (status == AVRCP_CTYPE_RESPONSE_INTERIM) return; 768 769 printf("AVRCP demo: command status: %s, ", avrcp_ctype2str(status)); 770 switch (packet[2]){ 771 case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED: 772 printf("notification, playback status changed %s\n", avrcp_play_status2str(avrcp_subevent_notification_playback_status_changed_get_play_status(packet))); 773 return; 774 case AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED: 775 printf("notification, playing content changed\n"); 776 return; 777 case AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED: 778 printf("notification track changed\n"); 779 return; 780 case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED: 781 printf("notification absolute volume changed %d\n", avrcp_subevent_notification_volume_changed_get_absolute_volume(packet)); 782 return; 783 case AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED: 784 printf("notification changed\n"); 785 return; 786 case AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE:{ 787 uint8_t shuffle_mode = avrcp_subevent_shuffle_and_repeat_mode_get_shuffle_mode(packet); 788 uint8_t repeat_mode = avrcp_subevent_shuffle_and_repeat_mode_get_repeat_mode(packet); 789 printf("%s, %s\n", avrcp_shuffle2str(shuffle_mode), avrcp_repeat2str(repeat_mode)); 790 break; 791 } 792 case AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO: 793 if (avrcp_subevent_now_playing_title_info_get_value_len(packet) > 0){ 794 memcpy(value, avrcp_subevent_now_playing_title_info_get_value(packet), avrcp_subevent_now_playing_title_info_get_value_len(packet)); 795 printf(" Title: %s\n", value); 796 } 797 break; 798 799 case AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO: 800 if (avrcp_subevent_now_playing_artist_info_get_value_len(packet) > 0){ 801 memcpy(value, avrcp_subevent_now_playing_artist_info_get_value(packet), avrcp_subevent_now_playing_artist_info_get_value_len(packet)); 802 printf(" Artist: %s\n", value); 803 } 804 break; 805 806 case AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO: 807 if (avrcp_subevent_now_playing_album_info_get_value_len(packet) > 0){ 808 memcpy(value, avrcp_subevent_now_playing_album_info_get_value(packet), avrcp_subevent_now_playing_album_info_get_value_len(packet)); 809 printf(" Album: %s\n", value); 810 } 811 break; 812 813 case AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO: 814 if (avrcp_subevent_now_playing_genre_info_get_value_len(packet) > 0){ 815 memcpy(value, avrcp_subevent_now_playing_genre_info_get_value(packet), avrcp_subevent_now_playing_genre_info_get_value_len(packet)); 816 printf(" Genre: %s\n", value); 817 } 818 break; 819 820 case AVRCP_SUBEVENT_PLAY_STATUS: 821 printf("song length: %d ms, song position: %d ms, play status: %s\n", 822 avrcp_subevent_play_status_get_song_length(packet), 823 avrcp_subevent_play_status_get_song_position(packet), 824 avrcp_play_status2str(avrcp_subevent_play_status_get_play_status(packet))); 825 break; 826 case AVRCP_SUBEVENT_OPERATION_COMPLETE: 827 printf("operation done %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 828 break; 829 case AVRCP_SUBEVENT_OPERATION_START: 830 printf("operation start %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet))); 831 break; 832 case AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE: 833 // response to set shuffle and repeat mode 834 printf("\n"); 835 break; 836 default: 837 printf("AVRCP demo: event is not parsed\n"); 838 break; 839 } 840 } 841 842 static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 843 UNUSED(channel); 844 UNUSED(size); 845 uint16_t cid; 846 bd_addr_t address; 847 uint8_t status; 848 849 if (packet_type != HCI_EVENT_PACKET) return; 850 if (hci_event_packet_get_type(packet) == HCI_EVENT_PIN_CODE_REQUEST) { 851 printf("Pin code request - using '0000'\n"); 852 hci_event_pin_code_request_get_bd_addr(packet, address); 853 gap_pin_code_response(address, "0000"); 854 return; 855 } 856 857 if (hci_event_packet_get_type(packet) != HCI_EVENT_A2DP_META) return; 858 859 switch (packet[2]){ 860 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 861 printf("A2DP Sink demo: received non SBC codec. not implemented.\n"); 862 break; 863 case A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ 864 printf("A2DP Sink demo: received SBC codec configuration.\n"); 865 sbc_configuration.reconfigure = a2dp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 866 sbc_configuration.num_channels = a2dp_subevent_signaling_media_codec_sbc_configuration_get_num_channels(packet); 867 sbc_configuration.sampling_frequency = a2dp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); 868 sbc_configuration.channel_mode = a2dp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet); 869 sbc_configuration.block_length = a2dp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet); 870 sbc_configuration.subbands = a2dp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet); 871 sbc_configuration.allocation_method = a2dp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet); 872 sbc_configuration.min_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_min_bitpool_value(packet); 873 sbc_configuration.max_bitpool_value = a2dp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); 874 sbc_configuration.frames_per_buffer = sbc_configuration.subbands * sbc_configuration.block_length; 875 dump_sbc_configuration(sbc_configuration); 876 877 if (sbc_configuration.reconfigure){ 878 media_processing_close(); 879 } 880 // prepare media processing 881 media_processing_init(sbc_configuration); 882 break; 883 } 884 case A2DP_SUBEVENT_STREAM_ESTABLISHED: 885 a2dp_subevent_stream_established_get_bd_addr(packet, address); 886 status = a2dp_subevent_stream_established_get_status(packet); 887 cid = a2dp_subevent_stream_established_get_a2dp_cid(packet); 888 printf("A2DP_SUBEVENT_STREAM_ESTABLISHED %d, %d \n", cid, a2dp_cid); 889 if (!a2dp_cid){ 890 // incoming connection 891 a2dp_cid = cid; 892 } else if (cid != a2dp_cid) { 893 break; 894 } 895 if (status){ 896 a2dp_sink_connected = 0; 897 printf("A2DP Sink demo: streaming connection failed, status 0x%02x\n", status); 898 break; 899 } 900 printf("A2DP Sink demo: streaming connection is established, address %s, a2dp cid 0x%02X, local_seid %d\n", bd_addr_to_str(address), a2dp_cid, local_seid); 901 902 #ifdef HAVE_BTSTACK_STDIN 903 memcpy(device_addr, address, 6); 904 #endif 905 local_seid = a2dp_subevent_stream_established_get_local_seid(packet); 906 a2dp_sink_connected = 1; 907 break; 908 909 case A2DP_SUBEVENT_STREAM_STARTED: 910 cid = a2dp_subevent_stream_started_get_a2dp_cid(packet); 911 if (cid != a2dp_cid) break; 912 local_seid = a2dp_subevent_stream_started_get_local_seid(packet); 913 printf("A2DP Sink demo: stream started, a2dp cid 0x%02X, local_seid %d\n", a2dp_cid, local_seid); 914 // started 915 media_processing_init(sbc_configuration); 916 break; 917 918 case A2DP_SUBEVENT_STREAM_SUSPENDED: 919 cid = a2dp_subevent_stream_suspended_get_a2dp_cid(packet); 920 if (cid != a2dp_cid) break; 921 local_seid = a2dp_subevent_stream_suspended_get_local_seid(packet); 922 printf("A2DP Sink demo: stream paused, a2dp cid 0x%02X, local_seid %d\n", a2dp_cid, local_seid); 923 media_processing_close(); 924 break; 925 926 case A2DP_SUBEVENT_STREAM_RELEASED: 927 cid = a2dp_subevent_stream_released_get_a2dp_cid(packet); 928 if (cid != a2dp_cid) { 929 printf("A2DP Sink demo: unexpected cid 0x%02x instead of 0x%02x\n", cid, a2dp_cid); 930 break; 931 } 932 local_seid = a2dp_subevent_stream_released_get_local_seid(packet); 933 printf("A2DP Sink demo: stream released, a2dp cid 0x%02X, local_seid %d\n", a2dp_cid, local_seid); 934 media_processing_close(); 935 break; 936 case A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 937 cid = a2dp_subevent_signaling_connection_released_get_a2dp_cid(packet); 938 if (cid != a2dp_cid) { 939 printf("A2DP Sink demo: unexpected cid 0x%02x instead of 0x%02x\n", cid, a2dp_cid); 940 break; 941 } 942 a2dp_sink_connected = 0; 943 printf("A2DP Sink demo: signaling connection released\n"); 944 break; 945 default: 946 printf("A2DP Sink demo: not parsed 0x%02x\n", packet[2]); 947 break; 948 } 949 } 950 951 #ifdef HAVE_BTSTACK_STDIN 952 static void show_usage(void){ 953 bd_addr_t iut_address; 954 gap_local_bd_addr(iut_address); 955 printf("\n--- Bluetooth AVDTP Sink/AVRCP Connection Test Console %s ---\n", bd_addr_to_str(iut_address)); 956 printf("b - AVDTP Sink create connection to addr %s\n", bd_addr_to_str(device_addr)); 957 printf("B - AVDTP Sink disconnect\n"); 958 printf("c - AVRCP create connection to addr %s\n", bd_addr_to_str(device_addr)); 959 printf("C - AVRCP disconnect\n"); 960 961 printf("\n--- Bluetooth AVRCP Commands %s ---\n", bd_addr_to_str(iut_address)); 962 printf("O - get play status\n"); 963 printf("j - get now playing info\n"); 964 printf("k - play\n"); 965 printf("K - stop\n"); 966 printf("L - pause\n"); 967 printf("u - start fast forward\n"); 968 printf("U - stop fast forward\n"); 969 printf("n - start rewind\n"); 970 printf("N - stop rewind\n"); 971 printf("i - forward\n"); 972 printf("I - backward\n"); 973 printf("t - volume up\n"); 974 printf("T - volume down\n"); 975 printf("p - absolute volume of 50 percent\n"); 976 printf("M - mute\n"); 977 printf("r - skip\n"); 978 printf("q - query repeat and shuffle mode\n"); 979 printf("v - repeat single track\n"); 980 printf("x - repeat all tracks\n"); 981 printf("X - disable repeat mode\n"); 982 printf("z - shuffle all tracks\n"); 983 printf("Z - disable shuffle mode\n"); 984 printf("---\n"); 985 } 986 #endif 987 988 #ifdef HAVE_BTSTACK_STDIN 989 static void stdin_process(char cmd){ 990 uint8_t status = ERROR_CODE_SUCCESS; 991 printf("stdin_process \n"); 992 if (!avrcp_connected){ 993 switch (cmd){ 994 case 'b': 995 case 'B': 996 case 'c': 997 break; 998 default: 999 printf("Command '%c' cannot be performed - please use 'c' to establish an AVRCP connection with device (addr %s).\n", cmd, bd_addr_to_str(device_addr)); 1000 return; 1001 } 1002 1003 } 1004 1005 switch (cmd){ 1006 case 'b': 1007 status = a2dp_sink_establish_stream(device_addr, local_seid, &a2dp_cid); 1008 printf(" - Create AVDTP connection to addr %s, and local seid %d, expected cid 0x%02x.\n", bd_addr_to_str(device_addr), local_seid, a2dp_cid); 1009 break; 1010 case 'B': 1011 printf(" - AVDTP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 1012 status = avdtp_sink_disconnect(a2dp_cid); 1013 break; 1014 case 'c': 1015 printf(" - Create AVRCP connection to addr %s.\n", bd_addr_to_str(device_addr)); 1016 status = avrcp_controller_connect(device_addr, &avrcp_cid); 1017 break; 1018 case 'C': 1019 printf(" - AVRCP disconnect from addr %s.\n", bd_addr_to_str(device_addr)); 1020 status = avrcp_controller_disconnect(avrcp_cid); 1021 break; 1022 1023 case '\n': 1024 case '\r': 1025 break; 1026 case 'O': 1027 printf(" - get play status\n"); 1028 status = avrcp_controller_get_play_status(avrcp_cid); 1029 break; 1030 case 'j': 1031 printf(" - get now playing info\n"); 1032 status = avrcp_controller_get_now_playing_info(avrcp_cid); 1033 break; 1034 case 'k': 1035 printf(" - play\n"); 1036 status = avrcp_controller_play(avrcp_cid); 1037 break; 1038 case 'K': 1039 printf(" - stop\n"); 1040 status = avrcp_controller_stop(avrcp_cid); 1041 break; 1042 case 'L': 1043 printf(" - pause\n"); 1044 status = avrcp_controller_pause(avrcp_cid); 1045 break; 1046 case 'u': 1047 printf(" - start fast forward\n"); 1048 status = avrcp_controller_start_fast_forward(avrcp_cid); 1049 break; 1050 case 'U': 1051 printf(" - stop fast forward\n"); 1052 status = avrcp_controller_stop_fast_forward(avrcp_cid); 1053 break; 1054 case 'n': 1055 printf(" - start rewind\n"); 1056 status = avrcp_controller_start_rewind(avrcp_cid); 1057 break; 1058 case 'N': 1059 printf(" - stop rewind\n"); 1060 status = avrcp_controller_stop_rewind(avrcp_cid); 1061 break; 1062 case 'i': 1063 printf(" - forward\n"); 1064 status = avrcp_controller_forward(avrcp_cid); 1065 break; 1066 case 'I': 1067 printf(" - backward\n"); 1068 status = avrcp_controller_backward(avrcp_cid); 1069 break; 1070 case 't': 1071 printf(" - volume up\n"); 1072 status = avrcp_controller_volume_up(avrcp_cid); 1073 break; 1074 case 'T': 1075 printf(" - volume down\n"); 1076 status = avrcp_controller_volume_down(avrcp_cid); 1077 break; 1078 case 'p': 1079 printf(" - absolute volume of 50 percent\n"); 1080 status = avrcp_controller_set_absolute_volume(avrcp_cid, 50); 1081 break; 1082 case 'M': 1083 printf(" - mute\n"); 1084 status = avrcp_controller_mute(avrcp_cid); 1085 break; 1086 case 'r': 1087 printf(" - skip\n"); 1088 status = avrcp_controller_skip(avrcp_cid); 1089 break; 1090 case 'q': 1091 printf(" - query repeat and shuffle mode\n"); 1092 status = avrcp_controller_query_shuffle_and_repeat_modes(avrcp_cid); 1093 break; 1094 case 'v': 1095 printf(" - repeat single track\n"); 1096 status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK); 1097 break; 1098 case 'x': 1099 printf(" - repeat all tracks\n"); 1100 status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_ALL_TRACKS); 1101 break; 1102 case 'X': 1103 printf(" - disable repeat mode\n"); 1104 status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_OFF); 1105 break; 1106 case 'z': 1107 printf(" - shuffle all tracks\n"); 1108 status = avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS); 1109 break; 1110 case 'Z': 1111 printf(" - disable shuffle mode\n"); 1112 status = avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_OFF); 1113 break; 1114 default: 1115 show_usage(); 1116 return; 1117 } 1118 if (status != ERROR_CODE_SUCCESS){ 1119 printf("Could not perform command, status 0x%2x\n", status); 1120 } 1121 } 1122 #endif 1123 1124 int btstack_main(int argc, const char * argv[]); 1125 int btstack_main(int argc, const char * argv[]){ 1126 (void)argc; 1127 (void)argv; 1128 1129 int err = a2dp_sink_and_avrcp_services_init(); 1130 if (err) return err; 1131 // turn on! 1132 printf("Starting BTstack ...\n"); 1133 hci_power_control(HCI_POWER_ON); 1134 return 0; 1135 } 1136 /* EXAMPLE_END */ 1137