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__ "a2dp_source.c" 40 41 #include <stdint.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include "btstack.h" 48 #include "avdtp.h" 49 #include "avdtp_util.h" 50 #include "avdtp_source.h" 51 #include "a2dp_source.h" 52 53 static const char * default_a2dp_source_service_name = "BTstack A2DP Source Service"; 54 static const char * default_a2dp_source_service_provider_name = "BTstack A2DP Source Service Provider"; 55 static avdtp_context_t a2dp_source_context; 56 57 typedef struct { 58 // to app 59 uint32_t fill_audio_ring_buffer_timeout_ms; 60 uint32_t time_audio_data_sent; // ms 61 uint32_t acc_num_missed_samples; 62 uint32_t samples_ready; 63 btstack_timer_source_t fill_audio_ring_buffer_timer; 64 btstack_ring_buffer_t sbc_ring_buffer; 65 btstack_sbc_encoder_state_t sbc_encoder_state; 66 67 int reconfigure; 68 int num_channels; 69 int sampling_frequency; 70 int channel_mode; 71 int block_length; 72 int subbands; 73 int allocation_method; 74 int min_bitpool_value; 75 int max_bitpool_value; 76 avdtp_stream_endpoint_t * local_stream_endpoint; 77 avdtp_sep_t * active_remote_sep; 78 } avdtp_stream_endpoint_context_t; 79 80 static a2dp_state_t app_state = A2DP_IDLE; 81 static avdtp_stream_endpoint_context_t sc; 82 static uint16_t avdtp_cid = 0; 83 static int next_remote_sep_index_to_query = 0; 84 85 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 86 87 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){ 88 uint8_t* attribute; 89 de_create_sequence(service); 90 91 // 0x0000 "Service Record Handle" 92 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 93 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 94 95 // 0x0001 "Service Class ID List" 96 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 97 attribute = de_push_sequence(service); 98 { 99 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE); 100 } 101 de_pop_sequence(service, attribute); 102 103 // 0x0004 "Protocol Descriptor List" 104 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 105 attribute = de_push_sequence(service); 106 { 107 uint8_t* l2cpProtocol = de_push_sequence(attribute); 108 { 109 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 110 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); 111 } 112 de_pop_sequence(attribute, l2cpProtocol); 113 114 uint8_t* avProtocol = de_push_sequence(attribute); 115 { 116 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 117 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 118 } 119 de_pop_sequence(attribute, avProtocol); 120 } 121 de_pop_sequence(service, attribute); 122 123 // 0x0005 "Public Browse Group" 124 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 125 attribute = de_push_sequence(service); 126 { 127 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 128 } 129 de_pop_sequence(service, attribute); 130 131 // 0x0009 "Bluetooth Profile Descriptor List" 132 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 133 attribute = de_push_sequence(service); 134 { 135 uint8_t *a2dProfile = de_push_sequence(attribute); 136 { 137 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION); 138 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 139 } 140 de_pop_sequence(attribute, a2dProfile); 141 } 142 de_pop_sequence(service, attribute); 143 144 145 // 0x0100 "Service Name" 146 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 147 if (service_name){ 148 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 149 } else { 150 de_add_data(service, DE_STRING, strlen(default_a2dp_source_service_name), (uint8_t *) default_a2dp_source_service_name); 151 } 152 153 // 0x0100 "Provider Name" 154 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 155 if (service_provider_name){ 156 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 157 } else { 158 de_add_data(service, DE_STRING, strlen(default_a2dp_source_service_provider_name), (uint8_t *) default_a2dp_source_service_provider_name); 159 } 160 161 // 0x0311 "Supported Features" 162 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 163 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 164 } 165 166 static void a2dp_streaming_emit_connection_established(btstack_packet_handler_t callback, uint16_t cid, uint8_t local_seid, uint8_t remote_seid, uint8_t status){ 167 if (!callback) return; 168 uint8_t event[8]; 169 int pos = 0; 170 event[pos++] = HCI_EVENT_A2DP_META; 171 event[pos++] = sizeof(event) - 2; 172 event[pos++] = A2DP_SUBEVENT_STREAM_ESTABLISHED; 173 little_endian_store_16(event, pos, cid); 174 pos += 2; 175 event[pos++] = local_seid; 176 event[pos++] = remote_seid; 177 event[pos++] = status; 178 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 179 } 180 181 static void a2dp_streaming_emit_can_send_media_packet_now(btstack_packet_handler_t callback, uint16_t cid, uint8_t seid){ 182 if (!callback) return; 183 uint8_t event[8]; 184 int pos = 0; 185 event[pos++] = HCI_EVENT_A2DP_META; 186 event[pos++] = sizeof(event) - 2; 187 event[pos++] = A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW; 188 little_endian_store_16(event, pos, cid); 189 pos += 2; 190 event[pos++] = seid; 191 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 192 } 193 194 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 195 UNUSED(channel); 196 UNUSED(size); 197 198 bd_addr_t event_addr; 199 uint8_t signal_identifier; 200 uint8_t status; 201 avdtp_sep_t sep; 202 uint8_t int_seid; 203 uint8_t acp_seid; 204 205 switch (packet_type) { 206 207 case HCI_EVENT_PACKET: 208 switch (hci_event_packet_get_type(packet)) { 209 case HCI_EVENT_PIN_CODE_REQUEST: 210 // inform about pin code request 211 printf("Pin code request - using '0000'\n"); 212 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 213 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 214 break; 215 case HCI_EVENT_DISCONNECTION_COMPLETE: 216 // connection closed -> quit test app 217 printf("\n --- a2dp source --- HCI_EVENT_DISCONNECTION_COMPLETE ---\n"); 218 219 break; 220 case HCI_EVENT_AVDTP_META: 221 switch (packet[2]){ 222 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 223 avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 224 status = avdtp_subevent_signaling_connection_established_get_status(packet); 225 if (status != 0){ 226 printf(" --- a2dp source --- AVDTP_SUBEVENT_SIGNALING_CONNECTION could not be established, status %d ---\n", status); 227 break; 228 } 229 sc.active_remote_sep = NULL; 230 next_remote_sep_index_to_query = 0; 231 app_state = A2DP_W2_DISCOVER_SEPS; 232 printf(" --- a2dp source --- AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x ---\n", avdtp_cid); 233 avdtp_source_discover_stream_endpoints(avdtp_cid); 234 break; 235 236 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 237 status = avdtp_subevent_streaming_connection_established_get_status(packet); 238 avdtp_cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); 239 int_seid = avdtp_subevent_streaming_connection_established_get_int_seid(packet); 240 acp_seid = avdtp_subevent_streaming_connection_established_get_acp_seid(packet); 241 242 if (status != 0){ 243 printf(" --- a2dp source --- AVDTP_SUBEVENT_STREAMING_CONNECTION could not be established, status %d ---\n", status); 244 break; 245 } 246 app_state = A2DP_STREAMING_OPENED; 247 a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, avdtp_cid, int_seid, acp_seid, 0); 248 printf(" --- a2dp source --- AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED --- avdtp_cid 0x%02x, local seid %d, remote seid %d\n", avdtp_cid, int_seid, acp_seid); 249 break; 250 251 case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND: 252 if (app_state != A2DP_W2_DISCOVER_SEPS) return; 253 sep.seid = avdtp_subevent_signaling_sep_found_get_seid(packet); 254 sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet); 255 sep.media_type = avdtp_subevent_signaling_sep_found_get_media_type(packet); 256 sep.type = avdtp_subevent_signaling_sep_found_get_sep_type(packet); 257 printf(" --- a2dp source --- Found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", sep.seid, sep.in_use, sep.media_type, sep.type); 258 break; 259 260 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:{ 261 printf(" AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY 0\n"); 262 if (!sc.local_stream_endpoint) return; 263 printf(" AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY 1\n"); 264 uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet)); 265 uint8_t channel_mode = avdtp_choose_sbc_channel_mode(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet)); 266 uint8_t block_length = avdtp_choose_sbc_block_length(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet)); 267 uint8_t subbands = avdtp_choose_sbc_subbands(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet)); 268 269 uint8_t allocation_method = avdtp_choose_sbc_allocation_method(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet)); 270 uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); 271 uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet)); 272 273 sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[0] = (sampling_frequency << 4) | channel_mode; 274 sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[1] = (block_length << 4) | (subbands << 2) | allocation_method; 275 sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[2] = min_bitpool_value; 276 sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[3] = max_bitpool_value; 277 278 sc.local_stream_endpoint->remote_configuration_bitmap = store_bit16(sc.local_stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 279 sc.local_stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO; 280 sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC; 281 282 app_state = A2DP_W2_SET_CONFIGURATION; 283 break; 284 } 285 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY: 286 printf(" --- a2dp source --- received non SBC codec. not implemented\n"); 287 break; 288 289 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{ 290 sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet); 291 sc.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet); 292 sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet); 293 // switch (avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet)){ 294 // case AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS: 295 // sc.allocation_method = SBC_LOUDNESS; 296 // break; 297 // case AVDTP_SBC_ALLOCATION_METHOD_SNR: 298 // sc.allocation_method = SBC_SNR; 299 // break; 300 // } 301 sc.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet) - 1; 302 sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet); 303 // TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet); 304 break; 305 } 306 case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW: 307 a2dp_streaming_emit_can_send_media_packet_now(a2dp_source_context.a2dp_callback, avdtp_cid, 0); 308 break; 309 310 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 311 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 312 status = avdtp_subevent_signaling_accept_get_status(packet); 313 printf(" --- a2dp source --- Accepted %d\n", signal_identifier); 314 315 switch (app_state){ 316 case A2DP_W2_DISCOVER_SEPS: 317 app_state = A2DP_W2_GET_ALL_CAPABILITIES; 318 sc.active_remote_sep = avdtp_source_remote_sep(avdtp_cid, next_remote_sep_index_to_query++); 319 printf(" --- a2dp source --- Query get caps for seid %d\n", sc.active_remote_sep->seid); 320 avdtp_source_get_capabilities(avdtp_cid, sc.active_remote_sep->seid); 321 break; 322 case A2DP_W2_GET_CAPABILITIES: 323 case A2DP_W2_GET_ALL_CAPABILITIES: 324 if (next_remote_sep_index_to_query < avdtp_source_remote_seps_num(avdtp_cid)){ 325 sc.active_remote_sep = avdtp_source_remote_sep(avdtp_cid, next_remote_sep_index_to_query++); 326 printf(" --- a2dp source --- Query get caps for seid %d\n", sc.active_remote_sep->seid); 327 avdtp_source_get_capabilities(avdtp_cid, sc.active_remote_sep->seid); 328 } else { 329 printf(" --- a2dp source --- No more remote seps found\n"); 330 app_state = A2DP_IDLE; 331 } 332 break; 333 case A2DP_W2_SET_CONFIGURATION:{ 334 if (!sc.local_stream_endpoint) return; 335 app_state = A2DP_W2_GET_CONFIGURATION; 336 avdtp_source_set_configuration(avdtp_cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid, sc.local_stream_endpoint->remote_configuration_bitmap, sc.local_stream_endpoint->remote_configuration); 337 break; 338 } 339 case A2DP_W2_GET_CONFIGURATION: 340 app_state = A2DP_W2_OPEN_STREAM_WITH_SEID; 341 avdtp_source_get_configuration(avdtp_cid, sc.active_remote_sep->seid); 342 break; 343 case A2DP_W2_OPEN_STREAM_WITH_SEID:{ 344 app_state = A2DP_W4_OPEN_STREAM_WITH_SEID; 345 btstack_sbc_encoder_init(&sc.sbc_encoder_state, SBC_MODE_STANDARD, 346 sc.block_length, sc.subbands, 347 sc.allocation_method, sc.sampling_frequency, 348 sc.max_bitpool_value); 349 avdtp_source_open_stream(avdtp_cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid); 350 break; 351 } 352 case A2DP_STREAMING_OPENED: 353 if (!a2dp_source_context.a2dp_callback) return; 354 switch (signal_identifier){ 355 case AVDTP_SI_START:{ 356 uint8_t event[6]; 357 int pos = 0; 358 event[pos++] = HCI_EVENT_A2DP_META; 359 event[pos++] = sizeof(event) - 2; 360 event[pos++] = A2DP_SUBEVENT_STREAM_START_ACCEPTED; 361 little_endian_store_16(event, pos, avdtp_cid); 362 pos += 2; 363 event[pos++] = avdtp_stream_endpoint_seid(sc.local_stream_endpoint); 364 (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 365 break; 366 } 367 case AVDTP_SI_CLOSE:{ 368 uint8_t event[6]; 369 int pos = 0; 370 event[pos++] = HCI_EVENT_A2DP_META; 371 event[pos++] = sizeof(event) - 2; 372 event[pos++] = A2DP_SUBEVENT_STREAM_RELEASED; 373 little_endian_store_16(event, pos, avdtp_cid); 374 pos += 2; 375 printf("send A2DP_SUBEVENT_STREAM_RELEASED to app\n"); 376 event[pos++] = avdtp_stream_endpoint_seid(sc.local_stream_endpoint); 377 (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 378 break; 379 } 380 case AVDTP_SI_SUSPEND:{ 381 uint8_t event[6]; 382 int pos = 0; 383 event[pos++] = HCI_EVENT_A2DP_META; 384 event[pos++] = sizeof(event) - 2; 385 event[pos++] = A2DP_SUBEVENT_STREAM_SUSPENDED; 386 little_endian_store_16(event, pos, avdtp_cid); 387 pos += 2; 388 event[pos++] = avdtp_stream_endpoint_seid(sc.local_stream_endpoint); 389 (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 390 break; 391 } 392 default: 393 break; 394 } 395 break; 396 default: 397 app_state = A2DP_IDLE; 398 break; 399 } 400 401 break; 402 case AVDTP_SUBEVENT_SIGNALING_REJECT: 403 app_state = A2DP_IDLE; 404 signal_identifier = avdtp_subevent_signaling_reject_get_signal_identifier(packet); 405 printf(" --- a2dp source --- Rejected %d\n", signal_identifier); 406 break; 407 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 408 app_state = A2DP_IDLE; 409 signal_identifier = avdtp_subevent_signaling_general_reject_get_signal_identifier(packet); 410 printf(" --- a2dp source --- Rejected %d\n", signal_identifier); 411 break; 412 default: 413 app_state = A2DP_IDLE; 414 printf(" --- a2dp source --- not implemented\n"); 415 break; 416 } 417 break; 418 default: 419 break; 420 } 421 break; 422 default: 423 // other packet type 424 break; 425 } 426 } 427 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){ 428 if (callback == NULL){ 429 log_error("a2dp_source_register_packet_handler called with NULL callback"); 430 return; 431 } 432 avdtp_source_register_packet_handler(&packet_handler); 433 a2dp_source_context.a2dp_callback = callback; 434 } 435 436 void a2dp_source_init(void){ 437 avdtp_source_init(&a2dp_source_context); 438 l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0); 439 } 440 441 uint8_t a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, 442 uint8_t * codec_capabilities, uint16_t codec_capabilities_len, 443 uint8_t * media_codec_info, uint16_t media_codec_info_len){ 444 avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type); 445 avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint)); 446 avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type, 447 codec_capabilities, codec_capabilities_len); 448 local_stream_endpoint->remote_configuration.media_codec.media_codec_information = media_codec_info; 449 local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len; 450 451 return local_stream_endpoint->sep.seid; 452 } 453 454 void a2dp_source_establish_stream(bd_addr_t bd_addr, uint8_t local_seid){ 455 sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); 456 if (!sc.local_stream_endpoint){ 457 printf(" no local_stream_endpoint for seid %d\n", local_seid); 458 } 459 avdtp_source_connect(bd_addr); 460 461 } 462 463 void a2dp_source_disconnect(uint16_t con_handle){ 464 avdtp_disconnect(con_handle, &a2dp_source_context); 465 } 466 467 void a2dp_source_start_stream(uint8_t int_seid){ 468 avdtp_start_stream(int_seid, &a2dp_source_context); 469 } 470 471 void a2dp_source_release_stream(uint8_t int_seid){ 472 avdtp_stop_stream(int_seid, &a2dp_source_context); 473 } 474 475 void a2dp_source_pause_stream(uint8_t int_seid){ 476 avdtp_suspend_stream(int_seid, &a2dp_source_context); 477 } 478 479 uint8_t a2dp_source_stream_endpoint_ready(uint8_t local_seid){ 480 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); 481 if (!stream_endpoint) { 482 printf("no stream_endpoint"); 483 return 0; 484 } 485 return (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING); 486 } 487 488 489 static void a2dp_source_setup_media_header(uint8_t * media_packet, int size, int *offset, uint8_t marker, uint16_t sequence_number){ 490 if (size < 12){ 491 printf("small outgoing buffer\n"); 492 return; 493 } 494 495 uint8_t rtp_version = 2; 496 uint8_t padding = 0; 497 uint8_t extension = 0; 498 uint8_t csrc_count = 0; 499 uint8_t payload_type = 0x60; 500 // uint16_t sequence_number = stream_endpoint->sequence_number; 501 uint32_t timestamp = btstack_run_loop_get_time_ms(); 502 uint32_t ssrc = 0x11223344; 503 504 // rtp header (min size 12B) 505 int pos = 0; 506 // int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); 507 508 media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count; 509 media_packet[pos++] = (marker << 1) | payload_type; 510 big_endian_store_16(media_packet, pos, sequence_number); 511 pos += 2; 512 big_endian_store_32(media_packet, pos, timestamp); 513 pos += 4; 514 big_endian_store_32(media_packet, pos, ssrc); // only used for multicast 515 pos += 4; 516 *offset = pos; 517 } 518 519 void a2dp_source_stream_endpoint_request_can_send_now(uint8_t local_seid){ 520 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); 521 if (!stream_endpoint) { 522 printf("no stream_endpoint"); 523 return; 524 } 525 stream_endpoint->send_stream = 1; 526 avdtp_request_can_send_now_initiator(stream_endpoint->connection, stream_endpoint->l2cap_media_cid); 527 } 528 529 int a2dp_max_media_payload_size(uint8_t int_seid){ 530 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, &a2dp_source_context); 531 if (!stream_endpoint) { 532 printf("no stream_endpoint found for seid %d", int_seid); 533 return 0; 534 } 535 536 if (stream_endpoint->l2cap_media_cid == 0){ 537 printf("no media cid found for seid %d", int_seid); 538 return 0; 539 } 540 int sbc_header_size = 12; 541 return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - sbc_header_size; 542 } 543 544 uint8_t a2dp_num_frames(uint8_t int_seid, int header_size, int num_bytes_in_frame, int bytes_in_storage){ 545 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, &a2dp_source_context); 546 if (!stream_endpoint) { 547 printf("no stream_endpoint found for seid %d", int_seid); 548 return 0; 549 } 550 551 if (stream_endpoint->l2cap_media_cid == 0){ 552 printf("no media cid found for seid %d", int_seid); 553 return 0; 554 } 555 556 int num_bytes_to_populate = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - header_size; 557 int calc_num_frames = btstack_min(num_bytes_to_populate/num_bytes_in_frame, bytes_in_storage/num_bytes_in_frame); 558 return (uint8_t)calc_num_frames; 559 } 560 561 static void a2dp_source_copy_media_payload(uint8_t * media_packet, int size, int * offset, btstack_ring_buffer_t * sbc_ring_buffer, uint16_t sbc_frame_bytes, uint8_t num_frames){ 562 if (size < 18){ 563 printf("small outgoing buffer\n"); 564 return; 565 } 566 int pos = *offset; 567 // media payload 568 // sbc_header (size 1B) 569 uint8_t sbc_header_index = pos; 570 pos++; 571 uint8_t fragmentation = 0; 572 uint8_t starting_packet = 0; // set to 1 for the first packet of a fragmented SBC frame 573 uint8_t last_packet = 0; // set to 1 for the last packet of a fragmented SBC frame 574 uint8_t _num_frames = 0; 575 576 uint32_t total_sbc_bytes_read = 0; 577 uint8_t sbc_frame_size = 0; 578 // payload 579 while (size - 13 - total_sbc_bytes_read >= sbc_frame_bytes && btstack_ring_buffer_bytes_available(sbc_ring_buffer)){ 580 uint32_t number_of_bytes_read = 0; 581 btstack_ring_buffer_read(sbc_ring_buffer, &sbc_frame_size, 1, &number_of_bytes_read); 582 btstack_ring_buffer_read(sbc_ring_buffer, media_packet + pos, sbc_frame_size, &number_of_bytes_read); 583 pos += sbc_frame_size; 584 total_sbc_bytes_read += sbc_frame_size; 585 _num_frames++; 586 // printf("send sbc frame: timestamp %d, seq. nr %d\n", timestamp, stream_endpoint->sequence_number); 587 } 588 media_packet[sbc_header_index] = (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | _num_frames; 589 printf("calculated num frames %d, num frames %d\n", num_frames, _num_frames); 590 *offset = pos; 591 } 592 593 int a2dp_source_stream_send_media_payload(uint8_t int_seid, btstack_ring_buffer_t * sbc_ring_buffer, uint16_t sbc_frame_bytes, uint8_t num_frames, uint8_t marker){ 594 avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(int_seid, &a2dp_source_context); 595 if (!stream_endpoint) { 596 printf("no stream_endpoint found for seid %d", int_seid); 597 return 0; 598 } 599 600 if (stream_endpoint->l2cap_media_cid == 0){ 601 printf("no media cid found for seid %d", int_seid); 602 return 0; 603 } 604 605 int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); 606 int offset = 0; 607 608 l2cap_reserve_packet_buffer(); 609 uint8_t * media_packet = l2cap_get_outgoing_buffer(); 610 //int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); 611 a2dp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number); 612 a2dp_source_copy_media_payload(media_packet, size, &offset, sbc_ring_buffer, sbc_frame_bytes, num_frames); 613 stream_endpoint->sequence_number++; 614 l2cap_send_prepared(stream_endpoint->l2cap_media_cid, offset); 615 return size; 616 } 617