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