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