1 2 /* 3 * Copyright (C) 2016 BlueKitchen GmbH 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the copyright holders nor the names of 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 4. Any redistribution, use, or modification is done solely for 18 * personal benefit and not for any commercial purpose or for 19 * monetary gain. 20 * 21 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 25 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Please inquire about commercial licensing options at 35 * [email protected] 36 * 37 */ 38 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <math.h> 46 47 #include "btstack.h" 48 #include "avdtp.h" 49 #include "avdtp_util.h" 50 #include "avdtp_source.h" 51 52 #include "btstack_ring_buffer.h" 53 #include "wav_util.h" 54 55 static const char * default_avdtp_source_service_name = "BTstack AVDTP Source Service"; 56 static const char * default_avdtp_source_service_provider_name = "BTstack AVDTP Source Service Provider"; 57 58 static avdtp_context_t avdtp_source_context; 59 60 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 61 62 void a2dp_source_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 63 uint8_t* attribute; 64 de_create_sequence(service); 65 66 // 0x0000 "Service Record Handle" 67 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 68 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 69 70 // 0x0001 "Service Class ID List" 71 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 72 attribute = de_push_sequence(service); 73 { 74 de_add_number(attribute, DE_UUID, DE_SIZE_16, AUDIO_SOURCE_GROUP); 75 } 76 de_pop_sequence(service, attribute); 77 78 // 0x0004 "Protocol Descriptor List" 79 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 80 attribute = de_push_sequence(service); 81 { 82 uint8_t* l2cpProtocol = de_push_sequence(attribute); 83 { 84 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 85 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_AVDTP); 86 } 87 de_pop_sequence(attribute, l2cpProtocol); 88 89 uint8_t* avProtocol = de_push_sequence(attribute); 90 { 91 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, PSM_AVDTP); // avProtocol_service 92 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 93 } 94 de_pop_sequence(attribute, avProtocol); 95 } 96 de_pop_sequence(service, attribute); 97 98 // 0x0005 "Public Browse Group" 99 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 100 attribute = de_push_sequence(service); 101 { 102 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 103 } 104 de_pop_sequence(service, attribute); 105 106 // 0x0009 "Bluetooth Profile Descriptor List" 107 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 108 attribute = de_push_sequence(service); 109 { 110 uint8_t *a2dProfile = de_push_sequence(attribute); 111 { 112 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, ADVANCED_AUDIO_DISTRIBUTION); 113 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 114 } 115 de_pop_sequence(attribute, a2dProfile); 116 } 117 de_pop_sequence(service, attribute); 118 119 120 // 0x0100 "Service Name" 121 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 122 if (service_name){ 123 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 124 } else { 125 de_add_data(service, DE_STRING, strlen(default_avdtp_source_service_name), (uint8_t *) default_avdtp_source_service_name); 126 } 127 128 // 0x0100 "Provider Name" 129 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 130 if (service_provider_name){ 131 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 132 } else { 133 de_add_data(service, DE_STRING, strlen(default_avdtp_source_service_provider_name), (uint8_t *) default_avdtp_source_service_provider_name); 134 } 135 136 // 0x0311 "Supported Features" 137 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 138 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 139 } 140 141 142 void avdtp_source_register_media_transport_category(uint8_t seid){ 143 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 144 avdtp_register_media_transport_category(stream_endpoint); 145 } 146 147 void avdtp_source_register_reporting_category(uint8_t seid){ 148 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 149 avdtp_register_reporting_category(stream_endpoint); 150 } 151 152 void avdtp_source_register_delay_reporting_category(uint8_t seid){ 153 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 154 avdtp_register_delay_reporting_category(stream_endpoint); 155 } 156 157 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 158 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 159 avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets); 160 } 161 162 void avdtp_source_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){ 163 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 164 avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len); 165 } 166 167 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){ 168 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 169 avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery); 170 } 171 172 void avdtp_source_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, const uint8_t * media_codec_info, uint16_t media_codec_info_len){ 173 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 174 avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len); 175 } 176 177 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){ 178 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 179 avdtp_register_multiplexing_category(stream_endpoint, fragmentation); 180 } 181 182 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){ 183 return avdtp_create_stream_endpoint(sep_type, media_type, &avdtp_source_context); 184 } 185 186 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){ 187 if (callback == NULL){ 188 log_error("avdtp_source_register_packet_handler called with NULL callback"); 189 return; 190 } 191 avdtp_source_context.avdtp_callback = callback; 192 } 193 194 void avdtp_source_connect(bd_addr_t bd_addr){ 195 avdtp_connection_t * connection = avdtp_connection_for_bd_addr(bd_addr, &avdtp_source_context); 196 if (!connection){ 197 connection = avdtp_create_connection(bd_addr, &avdtp_source_context); 198 } 199 if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; 200 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 201 l2cap_create_channel(packet_handler, connection->remote_addr, PSM_AVDTP, 0xffff, NULL); 202 } 203 204 void avdtp_source_disconnect(uint16_t con_handle){ 205 avdtp_disconnect(con_handle, &avdtp_source_context); 206 } 207 208 void avdtp_source_open_stream(uint16_t con_handle, uint8_t acp_seid){ 209 avdtp_open_stream(con_handle, acp_seid, &avdtp_source_context); 210 } 211 212 void avdtp_source_start_stream(uint16_t con_handle, uint8_t acp_seid){ 213 avdtp_start_stream(con_handle, acp_seid, &avdtp_source_context); 214 } 215 216 void avdtp_source_stop_stream(uint16_t con_handle, uint8_t acp_seid){ 217 avdtp_stop_stream(con_handle, acp_seid, &avdtp_source_context); 218 } 219 220 void avdtp_source_abort_stream(uint16_t con_handle, uint8_t acp_seid){ 221 avdtp_abort_stream(con_handle, acp_seid, &avdtp_source_context); 222 } 223 224 void avdtp_source_discover_stream_endpoints(uint16_t con_handle){ 225 avdtp_discover_stream_endpoints(con_handle, &avdtp_source_context); 226 } 227 228 void avdtp_source_get_capabilities(uint16_t con_handle, uint8_t acp_seid){ 229 avdtp_get_capabilities(con_handle, acp_seid, &avdtp_source_context); 230 } 231 232 void avdtp_source_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){ 233 avdtp_get_all_capabilities(con_handle, acp_seid, &avdtp_source_context); 234 } 235 236 void avdtp_source_get_configuration(uint16_t con_handle, uint8_t acp_seid){ 237 avdtp_get_configuration(con_handle, acp_seid, &avdtp_source_context); 238 } 239 240 void avdtp_source_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 241 avdtp_set_configuration(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context); 242 } 243 244 void avdtp_source_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 245 avdtp_reconfigure(con_handle, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context); 246 } 247 248 void avdtp_source_suspend(uint16_t con_handle, uint8_t acp_seid){ 249 avdtp_suspend(con_handle, acp_seid, &avdtp_source_context); 250 } 251 252 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 253 avdtp_packet_handler(packet_type, channel, packet, size, &avdtp_source_context); 254 } 255 256 257 /* streaming part */ 258 #define NUM_CHANNELS 2 259 #define SAMPLE_RATE 44100 260 #define BYTES_PER_AUDIO_SAMPLE (2*NUM_CHANNELS) 261 #define LATENCY 300 // ms 262 263 #ifndef M_PI 264 #define M_PI 3.14159265 265 #endif 266 #define TABLE_SIZE_441HZ 100 267 268 typedef struct { 269 int16_t source[TABLE_SIZE_441HZ]; 270 int left_phase; 271 int right_phase; 272 } paTestData; 273 274 static uint32_t fill_audio_ring_buffer_timeout = 50; //ms 275 static paTestData sin_data; 276 static int total_num_samples = 0; 277 static char * wav_filename = "test_output_sine.wav"; 278 279 static btstack_sbc_decoder_state_t state; 280 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 281 282 283 284 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 285 UNUSED(sample_rate); 286 UNUSED(context); 287 printf("store %d samples\n",num_samples*num_channels); 288 wav_writer_write_int16(num_samples*num_channels, data); 289 // frame_count++; 290 total_num_samples+=num_samples*num_channels; 291 } 292 293 static void fill_audio_ring_buffer(void *userData, int num_samples_to_write, avdtp_stream_endpoint_t * stream_endpoint){ 294 paTestData *data = (paTestData*)userData; 295 int count = 0; 296 while (btstack_ring_buffer_bytes_free(&stream_endpoint->audio_ring_buffer) >= BYTES_PER_AUDIO_SAMPLE && count < num_samples_to_write){ 297 uint8_t write_data[BYTES_PER_AUDIO_SAMPLE]; 298 *(int16_t*)&write_data[0] = data->source[data->left_phase]; 299 *(int16_t*)&write_data[2] = data->source[data->right_phase]; 300 301 btstack_ring_buffer_write(&stream_endpoint->audio_ring_buffer, write_data, BYTES_PER_AUDIO_SAMPLE); 302 count++; 303 304 data->left_phase += 1; 305 if (data->left_phase >= TABLE_SIZE_441HZ){ 306 data->left_phase -= TABLE_SIZE_441HZ; 307 } 308 data->right_phase += 1; 309 if (data->right_phase >= TABLE_SIZE_441HZ){ 310 data->right_phase -= TABLE_SIZE_441HZ; 311 } 312 } 313 } 314 315 static void fill_sbc_ring_buffer(uint8_t * sbc_frame, int sbc_frame_size, avdtp_stream_endpoint_t * stream_endpoint){ 316 if (btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= sbc_frame_size ){ 317 // printf(" fill_sbc_ring_buffer\n"); 318 uint8_t size_buffer = sbc_frame_size; 319 btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, &size_buffer, 1); 320 btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, sbc_frame, sbc_frame_size); 321 } else { 322 printf("No space in sbc buffer\n"); 323 } 324 } 325 326 327 static void avdtp_source_stream_endpoint_run(avdtp_stream_endpoint_t * stream_endpoint){ 328 // performe sbc encoding 329 int total_num_bytes_read = 0; 330 int num_audio_samples_to_read = btstack_sbc_encoder_num_audio_frames(); 331 int audio_bytes_to_read = num_audio_samples_to_read * BYTES_PER_AUDIO_SAMPLE; 332 333 // printf("run: audio_bytes_to_read: %d\n", audio_bytes_to_read); 334 // printf(" audio buf, bytes available: %d\n", btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer)); 335 // printf(" sbc buf, bytes free: %d\n", btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer)); 336 337 while (btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer) >= audio_bytes_to_read 338 && btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= 120){ // TODO use real value 339 340 uint32_t number_of_bytes_read = 0; 341 uint8_t pcm_frame[256*BYTES_PER_AUDIO_SAMPLE]; 342 btstack_ring_buffer_read(&stream_endpoint->audio_ring_buffer, pcm_frame, audio_bytes_to_read, &number_of_bytes_read); 343 // printf(" num audio bytes read %d\n", number_of_bytes_read); 344 btstack_sbc_encoder_process_data((int16_t *) pcm_frame); 345 346 uint16_t sbc_frame_bytes = 119; //btstack_sbc_encoder_sbc_buffer_length(); 347 348 total_num_bytes_read += number_of_bytes_read; 349 fill_sbc_ring_buffer(btstack_sbc_encoder_sbc_buffer(), sbc_frame_bytes, stream_endpoint); 350 } 351 // schedule sending 352 if (total_num_bytes_read != 0){ 353 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND; 354 avdtp_request_can_send_now_self(stream_endpoint->connection, stream_endpoint->l2cap_media_cid); 355 } 356 } 357 358 static void test_fill_audio_ring_buffer_timeout_handler(btstack_timer_source_t * timer){ 359 avdtp_stream_endpoint_t * stream_endpoint = btstack_run_loop_get_timer_context(timer); 360 btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout); // 2 seconds timeout 361 btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 362 uint32_t now = btstack_run_loop_get_time_ms(); 363 364 uint32_t update_period_ms = fill_audio_ring_buffer_timeout; 365 if (stream_endpoint->time_audio_data_sent > 0){ 366 update_period_ms = now - stream_endpoint->time_audio_data_sent; 367 } 368 uint32_t num_samples = (update_period_ms * 44100) / 1000; 369 stream_endpoint->acc_num_missed_samples += (update_period_ms * 44100) % 1000; 370 371 if (stream_endpoint->acc_num_missed_samples >= 1000){ 372 num_samples++; 373 stream_endpoint->acc_num_missed_samples -= 1000; 374 } 375 376 fill_audio_ring_buffer(&sin_data, num_samples, stream_endpoint); 377 stream_endpoint->time_audio_data_sent = now; 378 379 avdtp_source_stream_endpoint_run(stream_endpoint); 380 // 381 } 382 383 static void test_fill_audio_ring_buffer_timer_start(avdtp_stream_endpoint_t * stream_endpoint){ 384 btstack_run_loop_remove_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 385 btstack_run_loop_set_timer_handler(&stream_endpoint->fill_audio_ring_buffer_timer, test_fill_audio_ring_buffer_timeout_handler); 386 btstack_run_loop_set_timer_context(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint); 387 btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout); // 50 ms timeout 388 btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 389 } 390 391 static void test_fill_audio_ring_buffer_timer_stop(avdtp_stream_endpoint_t * stream_endpoint){ 392 btstack_run_loop_remove_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 393 } 394 395 void avdtp_source_stream_data_start(uint16_t con_handle){ 396 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(con_handle, &avdtp_source_context); 397 if (!stream_endpoint) { 398 printf("no stream_endpoint found for 0x%02x", con_handle); 399 return; 400 } 401 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING){ 402 printf("stream_endpoint in wrong state %d\n", stream_endpoint->state); 403 return; 404 } 405 406 test_fill_audio_ring_buffer_timer_start(stream_endpoint); 407 } 408 409 void avdtp_source_stream_data_stop(uint16_t con_handle){ 410 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(con_handle, &avdtp_source_context); 411 if (!stream_endpoint) { 412 log_error("no stream_endpoint found"); 413 return; 414 } 415 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING) return; 416 // TODO: initialize randomly sequence number 417 stream_endpoint->sequence_number = 0; 418 test_fill_audio_ring_buffer_timer_stop(stream_endpoint); 419 wav_writer_close(); 420 } 421 422 void avdtp_source_init(void){ 423 avdtp_source_context.stream_endpoints = NULL; 424 avdtp_source_context.connections = NULL; 425 avdtp_source_context.stream_endpoints_id_counter = 0; 426 avdtp_source_context.packet_handler = packet_handler; 427 428 /* initialise sinusoidal wavetable */ 429 int i; 430 for (i=0; i<TABLE_SIZE_441HZ; i++){ 431 sin_data.source[i] = sin(((double)i/(double)TABLE_SIZE_441HZ) * M_PI * 2.)*32767; 432 } 433 sin_data.left_phase = sin_data.right_phase = 0; 434 wav_writer_open(wav_filename, NUM_CHANNELS, SAMPLE_RATE); 435 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 436 437 l2cap_register_service(&packet_handler, PSM_AVDTP, 0xffff, LEVEL_0); 438 } 439