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