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 BLUEKITCHEN 25 * GMBH 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 * Supported use cases: 41 * - single incoming connection: sep discovery starts and stream will get setup if remote sink sep with SBC is found 42 * - single outgoing connection: see above 43 * - outgoing and incoming connection to same device: 44 * - if outgoing is triggered first, incoming will get ignored. 45 * - if incoming starts first, start ougoing will fail, but incoming will succeed. 46 * - outgoing and incoming connections to different devices: 47 * - if outgoing is first, incoming gets ignored. 48 * - if incoming starts first SEP discovery will get stopped and outgoing will succeed. 49 */ 50 51 #define BTSTACK_FILE__ "a2dp_source.c" 52 53 #include <stdint.h> 54 #include <string.h> 55 56 #include "bluetooth_psm.h" 57 #include "bluetooth_sdp.h" 58 #include "btstack_debug.h" 59 #include "btstack_event.h" 60 #include "classic/a2dp_source.h" 61 #include "classic/avdtp_source.h" 62 #include "classic/avdtp_util.h" 63 #include "classic/sdp_util.h" 64 #include "l2cap.h" 65 66 #define AVDTP_MAX_SEP_NUM 10 67 #define A2DP_SET_CONFIG_DELAY_MS 200 68 69 static const char * a2dp_default_source_service_name = "BTstack A2DP Source Service"; 70 static const char * a2dp_default_source_service_provider_name = "BTstack A2DP Source Service Provider"; 71 72 static btstack_packet_handler_t a2dp_source_packet_handler_user; 73 static uint8_t (*a2dp_source_media_config_validator)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size); 74 75 // config process - singletons using sep_discovery_cid is used as mutex 76 static uint16_t a2dp_source_sep_discovery_cid; 77 static uint16_t a2dp_source_sep_discovery_count; 78 static uint16_t a2dp_source_sep_discovery_index; 79 static avdtp_sep_t a2dp_source_sep_discovery_seps[AVDTP_MAX_SEP_NUM]; 80 static btstack_timer_source_t a2dp_source_set_config_timer; 81 static bool a2dp_source_set_config_timer_active; 82 83 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 84 static void a2dp_discover_seps_with_next_waiting_connection(void); 85 86 static void a2dp_source_streaming_emit_connection_failed(avdtp_connection_t *connection, uint8_t status) { 87 uint8_t event[14]; 88 int pos = 0; 89 event[pos++] = HCI_EVENT_A2DP_META; 90 event[pos++] = sizeof(event) - 2; 91 event[pos++] = A2DP_SUBEVENT_STREAM_ESTABLISHED; 92 little_endian_store_16(event, pos, connection->avdtp_cid); 93 pos += 2; 94 reverse_bd_addr(connection->remote_addr, &event[pos]); 95 pos += 6; 96 event[pos++] = 0; 97 event[pos++] = 0; 98 event[pos++] = status; 99 100 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 101 } 102 103 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){ 104 uint8_t* attribute; 105 de_create_sequence(service); 106 107 // 0x0000 "Service Record Handle" 108 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 109 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 110 111 // 0x0001 "Service Class ID List" 112 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 113 attribute = de_push_sequence(service); 114 { 115 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE); 116 } 117 de_pop_sequence(service, attribute); 118 119 // 0x0004 "Protocol Descriptor List" 120 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 121 attribute = de_push_sequence(service); 122 { 123 uint8_t* l2cpProtocol = de_push_sequence(attribute); 124 { 125 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 126 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP); 127 } 128 de_pop_sequence(attribute, l2cpProtocol); 129 130 uint8_t* avProtocol = de_push_sequence(attribute); 131 { 132 de_add_number(avProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP); // avProtocol_service 133 de_add_number(avProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 134 } 135 de_pop_sequence(attribute, avProtocol); 136 } 137 de_pop_sequence(service, attribute); 138 139 // 0x0005 "Public Browse Group" 140 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 141 attribute = de_push_sequence(service); 142 { 143 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 144 } 145 de_pop_sequence(service, attribute); 146 147 // 0x0009 "Bluetooth Profile Descriptor List" 148 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 149 attribute = de_push_sequence(service); 150 { 151 uint8_t *a2dProfile = de_push_sequence(attribute); 152 { 153 de_add_number(a2dProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION); 154 de_add_number(a2dProfile, DE_UINT, DE_SIZE_16, 0x0103); 155 } 156 de_pop_sequence(attribute, a2dProfile); 157 } 158 de_pop_sequence(service, attribute); 159 160 161 // 0x0100 "Service Name" 162 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 163 if (service_name){ 164 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 165 } else { 166 de_add_data(service, DE_STRING, strlen(a2dp_default_source_service_name), (uint8_t *) a2dp_default_source_service_name); 167 } 168 169 // 0x0100 "Provider Name" 170 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 171 if (service_provider_name){ 172 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 173 } else { 174 de_add_data(service, DE_STRING, strlen(a2dp_default_source_service_provider_name), (uint8_t *) a2dp_default_source_service_provider_name); 175 } 176 177 // 0x0311 "Supported Features" 178 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 179 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 180 } 181 182 static void a2dp_signaling_emit_reconfigured(uint16_t cid, uint8_t local_seid, uint8_t status){ 183 uint8_t event[7]; 184 int pos = 0; 185 event[pos++] = HCI_EVENT_A2DP_META; 186 event[pos++] = sizeof(event) - 2; 187 event[pos++] = A2DP_SUBEVENT_STREAM_RECONFIGURED; 188 little_endian_store_16(event, pos, cid); 189 pos += 2; 190 event[pos++] = local_seid; 191 event[pos++] = status; 192 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 193 } 194 195 static void a2dp_source_set_config_timer_handler(btstack_timer_source_t * timer){ 196 uint16_t avdtp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 197 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 198 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL); 199 a2dp_source_set_config_timer_active = false; 200 201 log_info("Set Config timer fired, avdtp_cid 0x%02x", avdtp_cid); 202 203 if (connection == NULL) { 204 a2dp_discover_seps_with_next_waiting_connection(); 205 return; 206 } 207 208 if (connection->a2dp_source_stream_endpoint_configured) return; 209 avdtp_source_discover_stream_endpoints(avdtp_cid); 210 } 211 212 static void a2dp_source_set_config_timer_start(uint16_t avdtp_cid){ 213 log_info("Set Config timer start for cid 0%02x", avdtp_cid); 214 a2dp_source_set_config_timer_active = true; 215 btstack_run_loop_remove_timer(&a2dp_source_set_config_timer); 216 btstack_run_loop_set_timer_handler(&a2dp_source_set_config_timer,a2dp_source_set_config_timer_handler); 217 btstack_run_loop_set_timer(&a2dp_source_set_config_timer, A2DP_SET_CONFIG_DELAY_MS); 218 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, (void *)(uintptr_t)avdtp_cid); 219 btstack_run_loop_add_timer(&a2dp_source_set_config_timer); 220 } 221 222 static void a2dp_source_set_config_timer_restart(void){ 223 log_info("Set Config timer restart"); 224 btstack_run_loop_remove_timer(&a2dp_source_set_config_timer); 225 btstack_run_loop_set_timer(&a2dp_source_set_config_timer, A2DP_SET_CONFIG_DELAY_MS); 226 btstack_run_loop_add_timer(&a2dp_source_set_config_timer); 227 } 228 229 static void a2dp_source_set_config_timer_stop(void){ 230 if (a2dp_source_set_config_timer_active == false) return; 231 log_info("Set Config timer stop"); 232 btstack_run_loop_remove_timer(&a2dp_source_set_config_timer); 233 btstack_run_loop_set_timer_context(&a2dp_source_set_config_timer, NULL); 234 a2dp_source_set_config_timer_active = false; 235 } 236 237 // Discover seps, both incoming and outgoing 238 static void a2dp_start_discovering_seps(avdtp_connection_t * connection){ 239 connection->a2dp_source_state = A2DP_DISCOVER_SEPS; 240 connection->a2dp_source_discover_seps = false; 241 242 a2dp_source_sep_discovery_index = 0; 243 a2dp_source_sep_discovery_count = 0; 244 memset(a2dp_source_sep_discovery_seps, 0, sizeof(avdtp_sep_t) * AVDTP_MAX_SEP_NUM); 245 a2dp_source_sep_discovery_cid = connection->avdtp_cid; 246 247 // if we initiated the connection, start config right away, else wait a bit to give remote a chance to do it first 248 if (connection->a2dp_source_outgoing_active){ 249 log_info("discover seps"); 250 avdtp_source_discover_stream_endpoints(connection->avdtp_cid); 251 } else { 252 log_info("wait a bit, then discover seps"); 253 a2dp_source_set_config_timer_start(connection->avdtp_cid); 254 } 255 } 256 257 static void a2dp_discover_seps_with_next_waiting_connection(void){ 258 btstack_assert(a2dp_source_sep_discovery_cid == 0); 259 btstack_linked_list_iterator_t it; 260 btstack_linked_list_iterator_init(&it, avdtp_get_connections()); 261 while (btstack_linked_list_iterator_has_next(&it)){ 262 avdtp_connection_t * next_connection = (avdtp_connection_t *)btstack_linked_list_iterator_next(&it); 263 if (!next_connection->a2dp_source_discover_seps) continue; 264 a2dp_start_discovering_seps(next_connection); 265 } 266 } 267 268 static void a2dp_source_ready_for_sep_discovery(avdtp_connection_t * connection){ 269 // start discover seps now if: 270 // - outgoing active: signaling for outgoing connection 271 // - outgoing not active: incoming connection and no sep discover ongoing 272 273 // sep discovery active? 274 if (a2dp_source_sep_discovery_cid == 0){ 275 a2dp_start_discovering_seps(connection); 276 } else { 277 // post-pone sep discovery 278 connection->a2dp_source_discover_seps = true; 279 } 280 } 281 282 static void a2dp_handle_received_configuration(const uint8_t *packet, uint8_t local_seid) { 283 uint16_t cid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_avdtp_cid(packet); 284 avdtp_connection_t *avdtp_connection = avdtp_get_connection_for_avdtp_cid(cid); 285 btstack_assert(avdtp_connection != NULL); 286 avdtp_connection->a2dp_source_local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 287 // bail out if local seid invalid 288 if (!avdtp_connection->a2dp_source_local_stream_endpoint) return; 289 290 // stop timer 291 if (a2dp_source_sep_discovery_cid == cid) { 292 a2dp_source_set_config_timer_stop(); 293 a2dp_source_sep_discovery_cid = 0; 294 } 295 296 avdtp_connection->a2dp_source_stream_endpoint_configured = true; 297 298 switch (avdtp_connection->a2dp_source_state) { 299 case A2DP_W4_SET_CONFIGURATION: 300 // outgoing: discovery and config of remote sink sep successful, trigger stream open 301 avdtp_connection->a2dp_source_state = A2DP_W2_OPEN_STREAM_WITH_SEID; 302 break; 303 case A2DP_DISCOVER_SEPS: 304 case A2DP_GET_CAPABILITIES: 305 case A2DP_W2_GET_ALL_CAPABILITIES: 306 case A2DP_DISCOVERY_DONE: 307 case A2DP_W4_GET_CONFIGURATION: 308 // incoming: wait for stream open 309 avdtp_connection->a2dp_source_state = A2DP_W4_OPEN_STREAM_WITH_SEID; 310 break; 311 default: 312 // wait for configuration after sending reconfigure - keep state 313 break; 314 } 315 } 316 317 static void a2dp_source_set_config(avdtp_connection_t * connection){ 318 uint8_t remote_seid = connection->a2dp_source_local_stream_endpoint->set_config_remote_seid; 319 log_info("A2DP initiate set configuration locally and wait for response ... local seid 0x%02x, remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), remote_seid); 320 connection->a2dp_source_state = A2DP_W4_SET_CONFIGURATION; 321 avdtp_source_set_configuration(connection->avdtp_cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), remote_seid, connection->a2dp_source_local_stream_endpoint->remote_configuration_bitmap, connection->a2dp_source_local_stream_endpoint->remote_configuration); 322 } 323 324 static void a2dp_source_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 325 UNUSED(channel); 326 UNUSED(size); 327 328 uint16_t cid; 329 avdtp_connection_t * connection; 330 331 uint8_t signal_identifier; 332 uint8_t status; 333 uint8_t local_seid; 334 uint8_t remote_seid; 335 336 if (packet_type != HCI_EVENT_PACKET) return; 337 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return; 338 339 switch (packet[2]){ 340 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED: 341 cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet); 342 connection = avdtp_get_connection_for_avdtp_cid(cid); 343 btstack_assert(connection != NULL); 344 345 status = avdtp_subevent_signaling_connection_established_get_status(packet); 346 if (status != ERROR_CODE_SUCCESS){ 347 // notify about connection error only if we're initiator 348 if (connection->a2dp_source_outgoing_active){ 349 log_info("A2DP source signaling connection failed status 0x%02x", status); 350 connection->a2dp_source_outgoing_active = false; 351 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 352 } 353 break; 354 } 355 log_info("A2DP source signaling connection established avdtp_cid 0x%02x", cid); 356 connection->a2dp_source_state = A2DP_CONNECTED; 357 358 // notify app 359 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED); 360 361 // Windows 10 as Source starts SEP discovery after 1500 ms, but only if it did not get a Discover command 362 // If BTstack is configured for both roles, we need to avoid sending Discover command in Source Role for outgoing Sink connections 363 364 // For this, we trigger SDP query only if: 365 // a) this is an outgoing source connection 366 // b) this connection wasn't caused by an outgoing sink request 367 if (connection->a2dp_source_outgoing_active || !connection->a2dp_sink_outgoing_active){ 368 a2dp_source_ready_for_sep_discovery(connection); 369 } 370 break; 371 372 case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND: 373 cid = avdtp_subevent_signaling_sep_found_get_avdtp_cid(packet); 374 connection = avdtp_get_connection_for_avdtp_cid(cid); 375 btstack_assert(connection != NULL); 376 377 if (connection->a2dp_source_state == A2DP_DISCOVER_SEPS) { 378 avdtp_sep_t sep; 379 memset(&sep, 0, sizeof(avdtp_sep_t)); 380 sep.seid = avdtp_subevent_signaling_sep_found_get_remote_seid(packet);; 381 sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet); 382 sep.media_type = (avdtp_media_type_t) avdtp_subevent_signaling_sep_found_get_media_type(packet); 383 sep.type = (avdtp_sep_type_t) avdtp_subevent_signaling_sep_found_get_sep_type(packet); 384 log_info("A2DP Found sep: remote seid 0x%02x, in_use %d, media type %d, sep type %s, index %d", 385 sep.seid, sep.in_use, sep.media_type, sep.type == AVDTP_SOURCE ? "source" : "sink", 386 a2dp_source_sep_discovery_count); 387 if ((sep.type == AVDTP_SINK) && (sep.in_use == false)) { 388 a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_count++] = sep; 389 } 390 } 391 break; 392 393 case AVDTP_SUBEVENT_SIGNALING_SEP_DICOVERY_DONE: 394 cid = avdtp_subevent_signaling_sep_dicovery_done_get_avdtp_cid(packet); 395 connection = avdtp_get_connection_for_avdtp_cid(cid); 396 btstack_assert(connection != NULL); 397 398 if (connection->a2dp_source_state != A2DP_DISCOVER_SEPS) break; 399 400 if (a2dp_source_sep_discovery_count > 0){ 401 connection->a2dp_source_state = A2DP_GET_CAPABILITIES; 402 a2dp_source_sep_discovery_index = 0; 403 connection->a2dp_source_have_config = false; 404 } else { 405 if (connection->a2dp_source_outgoing_active){ 406 connection->a2dp_source_outgoing_active = false; 407 connection = avdtp_get_connection_for_avdtp_cid(cid); 408 btstack_assert(connection != NULL); 409 a2dp_source_streaming_emit_connection_failed(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 410 } 411 412 // continue 413 connection->a2dp_source_state = A2DP_CONNECTED; 414 a2dp_source_sep_discovery_cid = 0; 415 a2dp_discover_seps_with_next_waiting_connection(); 416 } 417 break; 418 419 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY: 420 cid = avdtp_subevent_signaling_media_codec_sbc_capability_get_avdtp_cid(packet); 421 connection = avdtp_get_connection_for_avdtp_cid(cid); 422 btstack_assert(connection != NULL); 423 424 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 425 426 // forward codec capability 427 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY); 428 429 #ifndef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 430 // select SEP if none configured yet 431 if (connection->a2dp_source_have_config == false){ 432 // find SBC stream endpoint 433 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_source_stream_endpoint_for_media_codec(AVDTP_CODEC_SBC); 434 if (stream_endpoint != NULL){ 435 // choose SBC config params 436 avdtp_configuration_sbc_t configuration; 437 configuration.sampling_frequency = avdtp_choose_sbc_sampling_frequency(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet)); 438 configuration.channel_mode = avdtp_choose_sbc_channel_mode(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet)); 439 configuration.block_length = avdtp_choose_sbc_block_length(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet)); 440 configuration.subbands = avdtp_choose_sbc_subbands(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet)); 441 configuration.allocation_method = avdtp_choose_sbc_allocation_method(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet)); 442 configuration.max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet)); 443 configuration.min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet)); 444 445 // and pre-select this endpoint 446 local_seid = avdtp_stream_endpoint_seid(stream_endpoint); 447 remote_seid = avdtp_subevent_signaling_media_codec_sbc_capability_get_remote_seid(packet); 448 a2dp_source_set_config_sbc(cid, local_seid, remote_seid, &configuration); 449 } 450 } 451 #endif 452 break; 453 454 // forward codec capability 455 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY: 456 cid = avdtp_subevent_signaling_media_codec_mpeg_audio_capability_get_avdtp_cid(packet); 457 connection = avdtp_get_connection_for_avdtp_cid(cid); 458 btstack_assert(connection != NULL); 459 460 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 461 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CAPABILITY); 462 break; 463 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY: 464 cid = avdtp_subevent_signaling_media_codec_mpeg_aac_capability_get_avdtp_cid(packet); 465 connection = avdtp_get_connection_for_avdtp_cid(cid); 466 btstack_assert(connection != NULL); 467 468 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 469 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CAPABILITY); 470 break; 471 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY: 472 cid = avdtp_subevent_signaling_media_codec_atrac_capability_get_avdtp_cid(packet); 473 connection = avdtp_get_connection_for_avdtp_cid(cid); 474 btstack_assert(connection != NULL); 475 476 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 477 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CAPABILITY); 478 break; 479 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY: 480 cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet); 481 connection = avdtp_get_connection_for_avdtp_cid(cid); 482 btstack_assert(connection != NULL); 483 484 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 485 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY); 486 break; 487 488 // not forwarded 489 case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY: 490 case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY: 491 case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY: 492 case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY: 493 case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY: 494 case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY: 495 break; 496 497 case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY: 498 cid = avdtp_subevent_signaling_delay_reporting_capability_get_avdtp_cid(packet); 499 connection = avdtp_get_connection_for_avdtp_cid(cid); 500 btstack_assert(connection != NULL); 501 log_info("received AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, cid 0x%02x, state %d", cid, connection->a2dp_source_state); 502 503 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 504 505 // store delay reporting capability 506 a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_index].registered_service_categories |= 1 << AVDTP_DELAY_REPORTING; 507 508 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY); 509 break; 510 511 case AVDTP_SUBEVENT_SIGNALING_CAPABILITIES_DONE: 512 cid = avdtp_subevent_signaling_capabilities_done_get_avdtp_cid(packet); 513 connection = avdtp_get_connection_for_avdtp_cid(cid); 514 btstack_assert(connection != NULL); 515 516 if (connection->a2dp_source_state != A2DP_GET_CAPABILITIES) break; 517 518 // forward capabilities done for endpoint 519 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CAPABILITIES_DONE); 520 521 // endpoint was not suitable, check next one 522 a2dp_source_sep_discovery_index++; 523 if (a2dp_source_sep_discovery_index >= a2dp_source_sep_discovery_count){ 524 525 // emit 'all capabilities for all seps reported' 526 uint8_t event[6]; 527 uint8_t pos = 0; 528 event[pos++] = HCI_EVENT_A2DP_META; 529 event[pos++] = sizeof(event) - 2; 530 event[pos++] = A2DP_SUBEVENT_SIGNALING_CAPABILITIES_COMPLETE; 531 little_endian_store_16(event, pos, cid); 532 (*a2dp_source_packet_handler_user)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 533 534 // do we have a valid config? 535 if (connection->a2dp_source_have_config){ 536 connection->a2dp_source_state = A2DP_SET_CONFIGURATION; 537 connection->a2dp_source_have_config = false; 538 break; 539 } 540 541 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 542 connection->a2dp_source_state = A2DP_DISCOVERY_DONE; 543 // TODO call a2dp_discover_seps_with_next_waiting_connection? 544 break; 545 #else 546 // we didn't find a suitable SBC stream endpoint, sorry. 547 if (connection->a2dp_source_outgoing_active){ 548 connection->a2dp_source_outgoing_active = false; 549 connection = avdtp_get_connection_for_avdtp_cid(cid); 550 btstack_assert(connection != NULL); 551 a2dp_source_streaming_emit_connection_failed(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_NO_SUITABLE_CHANNEL_FOUND); 552 } 553 connection->a2dp_source_state = A2DP_CONNECTED; 554 a2dp_source_sep_discovery_cid = 0; 555 a2dp_discover_seps_with_next_waiting_connection(); 556 #endif 557 } 558 break; 559 560 case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORT: 561 cid = avdtp_subevent_signaling_delay_report_get_avdtp_cid(packet); 562 connection = avdtp_get_connection_for_avdtp_cid(cid); 563 btstack_assert(connection != NULL); 564 565 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_DELAY_REPORT); 566 break; 567 568 // forward codec configuration 569 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION: 570 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 571 a2dp_handle_received_configuration(packet, local_seid); 572 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION); 573 break; 574 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION: 575 local_seid = avdtp_subevent_signaling_media_codec_mpeg_audio_configuration_get_local_seid(packet); 576 a2dp_handle_received_configuration(packet, local_seid); 577 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AUDIO_CONFIGURATION); 578 break; 579 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION: 580 local_seid = avdtp_subevent_signaling_media_codec_mpeg_aac_configuration_get_local_seid(packet); 581 a2dp_handle_received_configuration(packet, local_seid); 582 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_MPEG_AAC_CONFIGURATION); 583 break; 584 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION: 585 local_seid = avdtp_subevent_signaling_media_codec_atrac_configuration_get_local_seid(packet); 586 a2dp_handle_received_configuration(packet, local_seid); 587 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_ATRAC_CONFIGURATION); 588 break; 589 case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION: 590 local_seid = avdtp_subevent_signaling_media_codec_sbc_configuration_get_local_seid(packet); 591 a2dp_handle_received_configuration(packet, local_seid); 592 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION); 593 break; 594 595 case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW: 596 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW); 597 break; 598 599 case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED: 600 cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet); 601 connection = avdtp_get_connection_for_avdtp_cid(cid); 602 btstack_assert(connection != NULL); 603 604 if (connection->a2dp_source_state != A2DP_W4_OPEN_STREAM_WITH_SEID) break; 605 606 connection->a2dp_source_outgoing_active = false; 607 status = avdtp_subevent_streaming_connection_established_get_status(packet); 608 if (status != ERROR_CODE_SUCCESS){ 609 log_info("A2DP source streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status); 610 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 611 break; 612 } 613 614 log_info("A2DP source streaming connection established --- avdtp_cid 0x%02x, local seid 0x%02x, remote seid 0x%02x", cid, 615 avdtp_subevent_streaming_connection_established_get_local_seid(packet), 616 avdtp_subevent_streaming_connection_established_get_remote_seid(packet)); 617 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 618 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_ESTABLISHED); 619 break; 620 621 case AVDTP_SUBEVENT_SIGNALING_ACCEPT: 622 cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet); 623 connection = avdtp_get_connection_for_avdtp_cid(cid); 624 btstack_assert(connection != NULL); 625 626 // restart set config timer while remote is active for current cid 627 if (a2dp_source_set_config_timer_active && 628 (avdtp_subevent_signaling_accept_get_is_initiator(packet) == 0) && 629 (cid == a2dp_source_sep_discovery_cid)){ 630 631 a2dp_source_set_config_timer_restart(); 632 break; 633 } 634 635 signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet); 636 637 log_info("A2DP cmd %s accepted, global state %d, cid 0x%02x", avdtp_si2str(signal_identifier), connection->a2dp_source_state, cid); 638 639 switch (connection->a2dp_source_state){ 640 case A2DP_GET_CAPABILITIES: 641 remote_seid = a2dp_source_sep_discovery_seps[a2dp_source_sep_discovery_index].seid; 642 log_info("A2DP get capabilities for remote seid 0x%02x", remote_seid); 643 avdtp_source_get_all_capabilities(cid, remote_seid); 644 return; 645 646 case A2DP_SET_CONFIGURATION: 647 a2dp_source_set_config(connection); 648 return; 649 650 case A2DP_W2_OPEN_STREAM_WITH_SEID: 651 log_info("A2DP open stream ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 652 connection->a2dp_source_state = A2DP_W4_OPEN_STREAM_WITH_SEID; 653 avdtp_source_open_stream(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 654 break; 655 656 case A2DP_W2_RECONFIGURE_WITH_SEID: 657 log_info("A2DP reconfigured ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 658 a2dp_signaling_emit_reconfigured(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), ERROR_CODE_SUCCESS); 659 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 660 break; 661 662 case A2DP_STREAMING_OPENED: 663 switch (signal_identifier){ 664 case AVDTP_SI_START: 665 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_STARTED); 666 break; 667 case AVDTP_SI_SUSPEND: 668 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_SUSPENDED); 669 break; 670 case AVDTP_SI_ABORT: 671 case AVDTP_SI_CLOSE: 672 a2dp_emit_stream_event(a2dp_source_packet_handler_user, cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), A2DP_SUBEVENT_STREAM_STOPPED); 673 break; 674 default: 675 break; 676 } 677 break; 678 679 default: 680 break; 681 } 682 break; 683 684 case AVDTP_SUBEVENT_SIGNALING_REJECT: 685 cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet); 686 connection = avdtp_get_connection_for_avdtp_cid(cid); 687 btstack_assert(connection != NULL); 688 689 if (avdtp_subevent_signaling_reject_get_is_initiator(packet) == 0) break; 690 691 switch (connection->a2dp_source_state) { 692 case A2DP_W2_RECONFIGURE_WITH_SEID: 693 log_info("A2DP reconfigure failed ... local seid 0x%02x, active remote seid 0x%02x", avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), connection->a2dp_source_local_stream_endpoint->remote_sep.seid); 694 a2dp_signaling_emit_reconfigured(cid, avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), ERROR_CODE_UNSPECIFIED_ERROR); 695 connection->a2dp_source_state = A2DP_STREAMING_OPENED; 696 break; 697 default: 698 connection->a2dp_source_state = A2DP_CONNECTED; 699 break; 700 } 701 702 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 703 break; 704 705 case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT: 706 cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet); 707 connection = avdtp_get_connection_for_avdtp_cid(cid); 708 btstack_assert(connection != NULL); 709 710 if (avdtp_subevent_signaling_general_reject_get_is_initiator(packet) == 0) break; 711 712 connection->a2dp_source_state = A2DP_CONNECTED; 713 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED); 714 break; 715 716 case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED: 717 cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet); 718 connection = avdtp_get_connection_for_avdtp_cid(cid); 719 btstack_assert(connection != NULL); 720 721 connection->a2dp_source_state = A2DP_CONFIGURED; 722 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_RELEASED); 723 break; 724 725 case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED: 726 cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet); 727 connection = avdtp_get_connection_for_avdtp_cid(cid); 728 btstack_assert(connection != NULL); 729 730 // connect/release are passed on to app 731 if (a2dp_source_sep_discovery_cid == cid){ 732 a2dp_source_set_config_timer_stop(); 733 connection->a2dp_source_stream_endpoint_configured = false; 734 connection->a2dp_source_local_stream_endpoint = NULL; 735 736 connection->a2dp_source_state = A2DP_IDLE; 737 a2dp_source_sep_discovery_cid = 0; 738 } 739 a2dp_replace_subevent_id_and_emit_cmd(a2dp_source_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED); 740 break; 741 742 default: 743 break; 744 } 745 } 746 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){ 747 btstack_assert(callback != NULL); 748 749 avdtp_source_register_packet_handler(&a2dp_source_packet_handler_internal); 750 a2dp_source_packet_handler_user = callback; 751 } 752 753 void a2dp_source_init(void){ 754 avdtp_source_init(); 755 } 756 757 void a2dp_source_deinit(void){ 758 avdtp_source_deinit(); 759 a2dp_source_packet_handler_user = NULL; 760 a2dp_source_media_config_validator = NULL; 761 a2dp_source_sep_discovery_cid = 0; 762 } 763 764 avdtp_stream_endpoint_t * a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, 765 const uint8_t *codec_capabilities, uint16_t codec_capabilities_len, 766 uint8_t * codec_configuration, uint16_t codec_configuration_len){ 767 avdtp_stream_endpoint_t * stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type); 768 if (!stream_endpoint){ 769 return NULL; 770 } 771 avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(stream_endpoint)); 772 avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(stream_endpoint), media_type, media_codec_type, 773 codec_capabilities, codec_capabilities_len); 774 avdtp_source_register_delay_reporting_category(avdtp_stream_endpoint_seid(stream_endpoint)); 775 776 // store user codec configuration buffer 777 stream_endpoint->media_codec_type = media_codec_type; 778 stream_endpoint->media_codec_configuration_info = codec_configuration; 779 stream_endpoint->media_codec_configuration_len = codec_configuration_len; 780 781 return stream_endpoint; 782 } 783 784 void a2dp_source_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 785 avdtp_source_finalize_stream_endpoint(stream_endpoint); 786 } 787 788 uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint16_t *avdtp_cid) { 789 790 uint16_t outgoing_cid; 791 792 avdtp_connection_t * connection = avdtp_get_connection_for_bd_addr(remote_addr); 793 if (connection == NULL){ 794 uint8_t status = avdtp_source_connect(remote_addr, &outgoing_cid); 795 if (status != ERROR_CODE_SUCCESS) { 796 // if there's already a connection for for remote addr, avdtp_source_connect fails, 797 // but the stream will get set-up nevertheless 798 return status; 799 } 800 connection = avdtp_get_connection_for_avdtp_cid(outgoing_cid); 801 btstack_assert(connection != NULL); 802 803 // setup state 804 connection->a2dp_source_outgoing_active = true; 805 connection->a2dp_source_state = A2DP_W4_CONNECTED; 806 *avdtp_cid = outgoing_cid; 807 808 } else { 809 if (connection->a2dp_source_outgoing_active || connection->a2dp_source_stream_endpoint_configured) { 810 return ERROR_CODE_COMMAND_DISALLOWED; 811 } 812 813 // check state 814 switch (connection->a2dp_source_state){ 815 case A2DP_IDLE: 816 case A2DP_CONNECTED: 817 // restart process e.g. if there no suitable stream endpoints or they had been in use 818 connection->a2dp_source_outgoing_active = true; 819 *avdtp_cid = connection->avdtp_cid; 820 a2dp_source_ready_for_sep_discovery(connection); 821 break; 822 default: 823 return ERROR_CODE_COMMAND_DISALLOWED; 824 } 825 } 826 return ERROR_CODE_SUCCESS; 827 } 828 829 uint8_t a2dp_source_disconnect(uint16_t avdtp_cid){ 830 return avdtp_disconnect(avdtp_cid); 831 } 832 833 834 uint8_t a2dp_source_start_stream(uint16_t avdtp_cid, uint8_t local_seid){ 835 return avdtp_start_stream(avdtp_cid, local_seid); 836 } 837 838 uint8_t a2dp_source_pause_stream(uint16_t avdtp_cid, uint8_t local_seid){ 839 return avdtp_suspend_stream(avdtp_cid, local_seid); 840 } 841 842 void a2dp_source_stream_endpoint_request_can_send_now(uint16_t avdtp_cid, uint8_t local_seid){ 843 avdtp_source_stream_endpoint_request_can_send_now(avdtp_cid, local_seid); 844 } 845 846 int a2dp_max_media_payload_size(uint16_t avdtp_cid, uint8_t local_seid){ 847 return avdtp_max_media_payload_size(avdtp_cid, local_seid); 848 } 849 850 int a2dp_source_stream_send_media_payload(uint16_t avdtp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){ 851 return avdtp_source_stream_send_media_payload(avdtp_cid, local_seid, storage, num_bytes_to_copy, num_frames, marker); 852 } 853 854 uint8_t a2dp_source_stream_send_media_payload_rtp(uint16_t a2dp_cid, uint8_t local_seid, uint8_t marker, uint8_t * payload, uint16_t payload_size){ 855 return avdtp_source_stream_send_media_payload_rtp(a2dp_cid, local_seid, marker, payload, payload_size); 856 } 857 858 uint8_t a2dp_source_stream_send_media_packet(uint16_t a2dp_cid, uint8_t local_seid, const uint8_t * packet, uint16_t size){ 859 return avdtp_source_stream_send_media_packet(a2dp_cid, local_seid, packet, size); 860 } 861 862 static uint8_t a2dp_source_config_init(avdtp_connection_t *connection, uint8_t local_seid, uint8_t remote_seid, 863 avdtp_media_codec_type_t codec_type) { 864 865 // check state 866 switch (connection->a2dp_source_state){ 867 case A2DP_DISCOVERY_DONE: 868 case A2DP_GET_CAPABILITIES: 869 break; 870 default: 871 return ERROR_CODE_COMMAND_DISALLOWED; 872 } 873 874 // lookup local stream endpoint 875 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 876 if (stream_endpoint == NULL){ 877 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 878 } 879 880 // lookup remote stream endpoint 881 avdtp_sep_t * remote_sep = NULL; 882 uint8_t i; 883 for (i=0; i < a2dp_source_sep_discovery_count; i++){ 884 if (a2dp_source_sep_discovery_seps[i].seid == remote_seid){ 885 remote_sep = &a2dp_source_sep_discovery_seps[i]; 886 } 887 } 888 if (remote_sep == NULL){ 889 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 890 } 891 892 // set media configuration 893 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1); 894 stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO; 895 stream_endpoint->remote_configuration.media_codec.media_codec_type = codec_type; 896 // remote seid to use 897 stream_endpoint->set_config_remote_seid = remote_seid; 898 // enable delay reporting if supported 899 if (remote_sep->registered_service_categories & (1<<AVDTP_DELAY_REPORTING)){ 900 stream_endpoint->remote_configuration_bitmap = store_bit16(stream_endpoint->remote_configuration_bitmap, AVDTP_DELAY_REPORTING, 1); 901 } 902 903 // suitable Sink stream endpoint found, configure it 904 connection->a2dp_source_local_stream_endpoint = stream_endpoint; 905 connection->a2dp_source_have_config = true; 906 907 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 908 // continue outgoing configuration 909 if (connection->a2dp_source_state == A2DP_DISCOVERY_DONE){ 910 connection->a2dp_source_state = A2DP_SET_CONFIGURATION; 911 } 912 #endif 913 914 return ERROR_CODE_SUCCESS; 915 } 916 917 uint8_t a2dp_source_set_config_sbc(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){ 918 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 919 if (connection == NULL){ 920 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 921 } 922 923 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_SBC); 924 if (status != 0) { 925 return status; 926 } 927 // set config in reserved buffer 928 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 929 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 930 avdtp_config_sbc_store(connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 931 932 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 933 a2dp_source_set_config(connection); 934 #endif 935 936 return ERROR_CODE_SUCCESS; 937 } 938 939 uint8_t a2dp_source_set_config_mpeg_audio(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){ 940 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 941 if (connection == NULL){ 942 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 943 } 944 945 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_1_2_AUDIO); 946 if (status != 0) { 947 return status; 948 } 949 950 // set config in reserved buffer 951 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *)connection->a2dp_source_local_stream_endpoint->media_codec_info; 952 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 4; 953 avdtp_config_mpeg_audio_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 954 955 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 956 a2dp_source_set_config(connection); 957 #endif 958 959 return ERROR_CODE_SUCCESS; 960 } 961 962 uint8_t a2dp_source_set_config_mpeg_aac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){ 963 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 964 if (connection == NULL){ 965 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 966 } 967 968 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_MPEG_2_4_AAC); 969 if (status != 0) { 970 return status; 971 } 972 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 973 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 6; 974 avdtp_config_mpeg_aac_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 975 976 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 977 a2dp_source_set_config(connection); 978 #endif 979 980 return ERROR_CODE_SUCCESS; 981 } 982 983 uint8_t a2dp_source_set_config_atrac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){ 984 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 985 if (connection == NULL){ 986 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 987 } 988 989 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_ATRAC_FAMILY); 990 if (status != 0) { 991 return status; 992 } 993 994 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) connection->a2dp_source_local_stream_endpoint->media_codec_info; 995 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = 7; 996 avdtp_config_atrac_store( connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information, configuration); 997 998 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 999 a2dp_source_set_config(connection); 1000 #endif 1001 1002 return ERROR_CODE_SUCCESS; 1003 } 1004 1005 uint8_t a2dp_source_set_config_other(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, 1006 const uint8_t * media_codec_information, uint8_t media_codec_information_len){ 1007 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(a2dp_cid); 1008 if (connection == NULL){ 1009 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1010 } 1011 1012 uint8_t status = a2dp_source_config_init(connection, local_seid, remote_seid, AVDTP_CODEC_NON_A2DP); 1013 if (status != 0) { 1014 return status; 1015 } 1016 1017 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information = (uint8_t *) media_codec_information; 1018 connection->a2dp_source_local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_information_len; 1019 1020 #ifdef ENABLE_A2DP_SOURCE_EXPLICIT_CONFIG 1021 a2dp_source_set_config(connection); 1022 #endif 1023 1024 return status; 1025 } 1026 1027 uint8_t a2dp_source_reconfigure_stream_sampling_frequency(uint16_t avdtp_cid, uint32_t sampling_frequency){ 1028 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(avdtp_cid); 1029 if (connection == NULL){ 1030 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1031 } 1032 1033 if (connection->a2dp_source_state != A2DP_STREAMING_OPENED) { 1034 return ERROR_CODE_COMMAND_DISALLOWED; 1035 } 1036 1037 btstack_assert(connection->a2dp_source_local_stream_endpoint != NULL); 1038 1039 log_info("Reconfigure avdtp_cid 0x%02x", avdtp_cid); 1040 1041 avdtp_media_codec_type_t codec_type = connection->a2dp_source_local_stream_endpoint->sep.capabilities.media_codec.media_codec_type; 1042 uint8_t codec_info_len; 1043 switch (codec_type){ 1044 case AVDTP_CODEC_SBC: 1045 codec_info_len = 4; 1046 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1047 avdtp_config_sbc_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1048 break; 1049 case AVDTP_CODEC_MPEG_1_2_AUDIO: 1050 codec_info_len = 4; 1051 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1052 avdtp_config_mpeg_audio_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1053 break; 1054 case AVDTP_CODEC_MPEG_2_4_AAC: 1055 codec_info_len = 6; 1056 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1057 avdtp_config_mpeg_aac_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1058 break; 1059 case AVDTP_CODEC_ATRAC_FAMILY: 1060 codec_info_len = 7; 1061 (void)memcpy(connection->a2dp_source_local_stream_endpoint->media_codec_info, connection->a2dp_source_local_stream_endpoint->remote_sep.configuration.media_codec.media_codec_information, codec_info_len); 1062 avdtp_config_atrac_set_sampling_frequency(connection->a2dp_source_local_stream_endpoint->media_codec_info, sampling_frequency); 1063 break; 1064 default: 1065 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1066 } 1067 1068 avdtp_capabilities_t new_configuration; 1069 new_configuration.media_codec.media_type = AVDTP_AUDIO; 1070 new_configuration.media_codec.media_codec_type = codec_type; 1071 new_configuration.media_codec.media_codec_information_len = codec_info_len; 1072 new_configuration.media_codec.media_codec_information = connection->a2dp_source_local_stream_endpoint->media_codec_info; 1073 1074 // start reconfigure 1075 connection->a2dp_source_state = A2DP_W2_RECONFIGURE_WITH_SEID; 1076 1077 return avdtp_source_reconfigure( 1078 avdtp_cid, 1079 avdtp_stream_endpoint_seid(connection->a2dp_source_local_stream_endpoint), 1080 connection->a2dp_source_local_stream_endpoint->remote_sep.seid, 1081 1 << AVDTP_MEDIA_CODEC, 1082 new_configuration 1083 ); 1084 } 1085 1086 static uint8_t a2dp_source_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size){ 1087 uint8_t error = 0; 1088 if (a2dp_source_media_config_validator != NULL) { 1089 // update subevent id and call validator 1090 uint8_t avdtp_subevent_id = event[2]; 1091 uint8_t a2dp_subevent_id = a2dp_subevent_id_for_avdtp_subevent_id(avdtp_subevent_id); 1092 uint8_t * subevent_field = (uint8_t *) &event[2]; 1093 *subevent_field = a2dp_subevent_id; 1094 error = (*a2dp_source_media_config_validator)(stream_endpoint, event, size); 1095 *subevent_field = avdtp_subevent_id; 1096 } 1097 return error; 1098 } 1099 1100 void a2dp_source_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size)){ 1101 a2dp_source_media_config_validator = callback; 1102 avdtp_source_register_media_config_validator(&a2dp_source_media_config_validator_callback); 1103 } 1104 1105