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