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