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