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 #define __BTSTACK_FILE__ "avdtp_source.c" 40 41 42 #include <stdint.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <math.h> 48 49 #include "btstack.h" 50 #include "avdtp.h" 51 #include "avdtp_util.h" 52 #include "avdtp_source.h" 53 54 #include "btstack_ring_buffer.h" 55 #include "wav_util.h" 56 57 static const char * default_avdtp_source_service_name = "BTstack AVDTP Source Service"; 58 static const char * default_avdtp_source_service_provider_name = "BTstack AVDTP Source Service Provider"; 59 60 static avdtp_context_t avdtp_source_context; 61 62 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 63 64 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){ 65 uint8_t* attribute; 66 de_create_sequence(service); 67 68 // 0x0000 "Service Record Handle" 69 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 70 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 71 72 // 0x0001 "Service Class ID List" 73 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 74 attribute = de_push_sequence(service); 75 { 76 de_add_number(attribute, DE_UUID, DE_SIZE_16, AUDIO_SOURCE_GROUP); 77 } 78 de_pop_sequence(service, attribute); 79 80 // 0x0004 "Protocol Descriptor List" 81 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 82 attribute = de_push_sequence(service); 83 { 84 uint8_t* l2cpProtocol = de_push_sequence(attribute); 85 { 86 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 87 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_AVDTP); 88 } 89 de_pop_sequence(attribute, l2cpProtocol); 90 91 uint8_t* avProtocol = de_push_sequence(attribute); 92 { 93 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, PSM_AVDTP); // avProtocol_service 94 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 95 } 96 de_pop_sequence(attribute, avProtocol); 97 } 98 de_pop_sequence(service, attribute); 99 100 // 0x0005 "Public Browse Group" 101 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 102 attribute = de_push_sequence(service); 103 { 104 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 105 } 106 de_pop_sequence(service, attribute); 107 108 // 0x0009 "Bluetooth Profile Descriptor List" 109 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 110 attribute = de_push_sequence(service); 111 { 112 uint8_t *a2dProfile = de_push_sequence(attribute); 113 { 114 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, ADVANCED_AUDIO_DISTRIBUTION); 115 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 116 } 117 de_pop_sequence(attribute, a2dProfile); 118 } 119 de_pop_sequence(service, attribute); 120 121 122 // 0x0100 "Service Name" 123 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 124 if (service_name){ 125 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 126 } else { 127 de_add_data(service, DE_STRING, strlen(default_avdtp_source_service_name), (uint8_t *) default_avdtp_source_service_name); 128 } 129 130 // 0x0100 "Provider Name" 131 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 132 if (service_provider_name){ 133 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 134 } else { 135 de_add_data(service, DE_STRING, strlen(default_avdtp_source_service_provider_name), (uint8_t *) default_avdtp_source_service_provider_name); 136 } 137 138 // 0x0311 "Supported Features" 139 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 140 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 141 } 142 143 144 void avdtp_source_register_media_transport_category(uint8_t seid){ 145 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 146 avdtp_register_media_transport_category(stream_endpoint); 147 } 148 149 void avdtp_source_register_reporting_category(uint8_t seid){ 150 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 151 avdtp_register_reporting_category(stream_endpoint); 152 } 153 154 void avdtp_source_register_delay_reporting_category(uint8_t seid){ 155 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 156 avdtp_register_delay_reporting_category(stream_endpoint); 157 } 158 159 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){ 160 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 161 avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets); 162 } 163 164 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){ 165 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 166 avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len); 167 } 168 169 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){ 170 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 171 avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery); 172 } 173 174 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){ 175 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 176 avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len); 177 } 178 179 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){ 180 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context); 181 avdtp_register_multiplexing_category(stream_endpoint, fragmentation); 182 } 183 184 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){ 185 return avdtp_create_stream_endpoint(sep_type, media_type, &avdtp_source_context); 186 } 187 188 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){ 189 if (callback == NULL){ 190 log_error("avdtp_source_register_packet_handler called with NULL callback"); 191 return; 192 } 193 avdtp_source_context.avdtp_callback = callback; 194 } 195 196 void avdtp_source_connect(bd_addr_t bd_addr){ 197 avdtp_connection_t * connection = avdtp_connection_for_bd_addr(bd_addr, &avdtp_source_context); 198 if (!connection){ 199 connection = avdtp_create_connection(bd_addr, &avdtp_source_context); 200 } 201 if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return; 202 connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED; 203 l2cap_create_channel(packet_handler, connection->remote_addr, PSM_AVDTP, 0xffff, NULL); 204 } 205 206 void avdtp_source_disconnect(uint16_t con_handle){ 207 avdtp_disconnect(con_handle, &avdtp_source_context); 208 } 209 210 void avdtp_source_open_stream(uint16_t con_handle, uint8_t acp_seid){ 211 avdtp_open_stream(con_handle, acp_seid, &avdtp_source_context); 212 } 213 214 void avdtp_source_start_stream(uint16_t con_handle, uint8_t acp_seid){ 215 avdtp_start_stream(con_handle, acp_seid, &avdtp_source_context); 216 } 217 218 void avdtp_source_stop_stream(uint16_t con_handle, uint8_t acp_seid){ 219 avdtp_stop_stream(con_handle, acp_seid, &avdtp_source_context); 220 } 221 222 void avdtp_source_abort_stream(uint16_t con_handle, uint8_t acp_seid){ 223 avdtp_abort_stream(con_handle, acp_seid, &avdtp_source_context); 224 } 225 226 void avdtp_source_discover_stream_endpoints(uint16_t con_handle){ 227 avdtp_discover_stream_endpoints(con_handle, &avdtp_source_context); 228 } 229 230 void avdtp_source_get_capabilities(uint16_t con_handle, uint8_t acp_seid){ 231 avdtp_get_capabilities(con_handle, acp_seid, &avdtp_source_context); 232 } 233 234 void avdtp_source_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){ 235 avdtp_get_all_capabilities(con_handle, acp_seid, &avdtp_source_context); 236 } 237 238 void avdtp_source_get_configuration(uint16_t con_handle, uint8_t acp_seid){ 239 avdtp_get_configuration(con_handle, acp_seid, &avdtp_source_context); 240 } 241 242 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){ 243 avdtp_set_configuration(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context); 244 } 245 246 void avdtp_source_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){ 247 avdtp_reconfigure(con_handle, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context); 248 } 249 250 void avdtp_source_suspend(uint16_t con_handle, uint8_t acp_seid){ 251 avdtp_suspend(con_handle, acp_seid, &avdtp_source_context); 252 } 253 254 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 255 avdtp_packet_handler(packet_type, channel, packet, size, &avdtp_source_context); 256 } 257 258 259 /* streaming part */ 260 #define NUM_CHANNELS 2 261 #define SAMPLE_RATE 44100 262 #define BYTES_PER_AUDIO_SAMPLE (2*NUM_CHANNELS) 263 #define LATENCY 300 // ms 264 265 #ifndef M_PI 266 #define M_PI 3.14159265 267 #endif 268 #define TABLE_SIZE_441HZ 100 269 270 typedef struct { 271 int16_t source[TABLE_SIZE_441HZ]; 272 int left_phase; 273 int right_phase; 274 } paTestData; 275 276 static uint32_t fill_audio_ring_buffer_timeout = 50; //ms 277 static paTestData sin_data; 278 static int total_num_samples = 0; 279 static char * wav_filename = "test_output_sine.wav"; 280 281 static btstack_sbc_decoder_state_t state; 282 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD; 283 284 285 286 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 287 UNUSED(sample_rate); 288 UNUSED(context); 289 printf("store %d samples\n",num_samples*num_channels); 290 wav_writer_write_int16(num_samples*num_channels, data); 291 // frame_count++; 292 total_num_samples+=num_samples*num_channels; 293 } 294 295 static void fill_audio_ring_buffer(void *userData, int num_samples_to_write, avdtp_stream_endpoint_t * stream_endpoint){ 296 paTestData *data = (paTestData*)userData; 297 int count = 0; 298 while (btstack_ring_buffer_bytes_free(&stream_endpoint->audio_ring_buffer) >= BYTES_PER_AUDIO_SAMPLE && count < num_samples_to_write){ 299 uint8_t write_data[BYTES_PER_AUDIO_SAMPLE]; 300 *(int16_t*)&write_data[0] = data->source[data->left_phase]; 301 *(int16_t*)&write_data[2] = data->source[data->right_phase]; 302 303 btstack_ring_buffer_write(&stream_endpoint->audio_ring_buffer, write_data, BYTES_PER_AUDIO_SAMPLE); 304 count++; 305 306 data->left_phase += 1; 307 if (data->left_phase >= TABLE_SIZE_441HZ){ 308 data->left_phase -= TABLE_SIZE_441HZ; 309 } 310 data->right_phase += 1; 311 if (data->right_phase >= TABLE_SIZE_441HZ){ 312 data->right_phase -= TABLE_SIZE_441HZ; 313 } 314 } 315 } 316 317 static void fill_sbc_ring_buffer(uint8_t * sbc_frame, int sbc_frame_size, avdtp_stream_endpoint_t * stream_endpoint){ 318 if (btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= sbc_frame_size ){ 319 // printf(" fill_sbc_ring_buffer\n"); 320 uint8_t size_buffer = sbc_frame_size; 321 btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, &size_buffer, 1); 322 btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, sbc_frame, sbc_frame_size); 323 } else { 324 printf("No space in sbc buffer\n"); 325 } 326 } 327 328 329 static void avdtp_source_stream_endpoint_run(avdtp_stream_endpoint_t * stream_endpoint){ 330 // performe sbc encoding 331 int total_num_bytes_read = 0; 332 int num_audio_samples_to_read = btstack_sbc_encoder_num_audio_frames(); 333 int audio_bytes_to_read = num_audio_samples_to_read * BYTES_PER_AUDIO_SAMPLE; 334 335 // printf("run: audio_bytes_to_read: %d\n", audio_bytes_to_read); 336 // printf(" audio buf, bytes available: %d\n", btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer)); 337 // printf(" sbc buf, bytes free: %d\n", btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer)); 338 339 while (btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer) >= audio_bytes_to_read 340 && btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= 120){ // TODO use real value 341 342 uint32_t number_of_bytes_read = 0; 343 uint8_t pcm_frame[256*BYTES_PER_AUDIO_SAMPLE]; 344 btstack_ring_buffer_read(&stream_endpoint->audio_ring_buffer, pcm_frame, audio_bytes_to_read, &number_of_bytes_read); 345 // printf(" num audio bytes read %d\n", number_of_bytes_read); 346 btstack_sbc_encoder_process_data((int16_t *) pcm_frame); 347 348 uint16_t sbc_frame_bytes = 119; //btstack_sbc_encoder_sbc_buffer_length(); 349 350 total_num_bytes_read += number_of_bytes_read; 351 fill_sbc_ring_buffer(btstack_sbc_encoder_sbc_buffer(), sbc_frame_bytes, stream_endpoint); 352 } 353 // schedule sending 354 if (total_num_bytes_read != 0){ 355 stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND; 356 avdtp_request_can_send_now_self(stream_endpoint->connection, stream_endpoint->l2cap_media_cid); 357 } 358 } 359 360 static void test_fill_audio_ring_buffer_timeout_handler(btstack_timer_source_t * timer){ 361 avdtp_stream_endpoint_t * stream_endpoint = btstack_run_loop_get_timer_context(timer); 362 btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout); // 2 seconds timeout 363 btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 364 uint32_t now = btstack_run_loop_get_time_ms(); 365 366 uint32_t update_period_ms = fill_audio_ring_buffer_timeout; 367 if (stream_endpoint->time_audio_data_sent > 0){ 368 update_period_ms = now - stream_endpoint->time_audio_data_sent; 369 } 370 uint32_t num_samples = (update_period_ms * 44100) / 1000; 371 stream_endpoint->acc_num_missed_samples += (update_period_ms * 44100) % 1000; 372 373 if (stream_endpoint->acc_num_missed_samples >= 1000){ 374 num_samples++; 375 stream_endpoint->acc_num_missed_samples -= 1000; 376 } 377 378 fill_audio_ring_buffer(&sin_data, num_samples, stream_endpoint); 379 stream_endpoint->time_audio_data_sent = now; 380 381 avdtp_source_stream_endpoint_run(stream_endpoint); 382 // 383 } 384 385 static void test_fill_audio_ring_buffer_timer_start(avdtp_stream_endpoint_t * stream_endpoint){ 386 btstack_run_loop_remove_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 387 btstack_run_loop_set_timer_handler(&stream_endpoint->fill_audio_ring_buffer_timer, test_fill_audio_ring_buffer_timeout_handler); 388 btstack_run_loop_set_timer_context(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint); 389 btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, fill_audio_ring_buffer_timeout); // 50 ms timeout 390 btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 391 } 392 393 static void test_fill_audio_ring_buffer_timer_stop(avdtp_stream_endpoint_t * stream_endpoint){ 394 btstack_run_loop_remove_timer(&stream_endpoint->fill_audio_ring_buffer_timer); 395 } 396 397 void avdtp_source_stream_data_start(uint16_t con_handle){ 398 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(con_handle, &avdtp_source_context); 399 if (!stream_endpoint) { 400 printf("no stream_endpoint found for 0x%02x", con_handle); 401 return; 402 } 403 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING){ 404 printf("stream_endpoint in wrong state %d\n", stream_endpoint->state); 405 return; 406 } 407 408 test_fill_audio_ring_buffer_timer_start(stream_endpoint); 409 } 410 411 void avdtp_source_stream_data_stop(uint16_t con_handle){ 412 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(con_handle, &avdtp_source_context); 413 if (!stream_endpoint) { 414 log_error("no stream_endpoint found"); 415 return; 416 } 417 if (stream_endpoint->state != AVDTP_STREAM_ENDPOINT_STREAMING) return; 418 // TODO: initialize randomly sequence number 419 stream_endpoint->sequence_number = 0; 420 test_fill_audio_ring_buffer_timer_stop(stream_endpoint); 421 wav_writer_close(); 422 } 423 424 void avdtp_source_init(void){ 425 avdtp_source_context.stream_endpoints = NULL; 426 avdtp_source_context.connections = NULL; 427 avdtp_source_context.stream_endpoints_id_counter = 0; 428 avdtp_source_context.packet_handler = packet_handler; 429 430 /* initialise sinusoidal wavetable */ 431 int i; 432 for (i=0; i<TABLE_SIZE_441HZ; i++){ 433 sin_data.source[i] = sin(((double)i/(double)TABLE_SIZE_441HZ) * M_PI * 2.)*32767; 434 } 435 sin_data.left_phase = sin_data.right_phase = 0; 436 wav_writer_open(wav_filename, NUM_CHANNELS, SAMPLE_RATE); 437 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL); 438 439 l2cap_register_service(&packet_handler, PSM_AVDTP, 0xffff, LEVEL_0); 440 } 441