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