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