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