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