xref: /btstack/src/classic/a2dp_source.c (revision b60d3b4722cad74670fd8e4fd32576e79b83f42b)
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 #define __BTSTACK_FILE__ "a2dp_source.c"
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack.h"
47 #include "classic/avdtp.h"
48 #include "classic/avdtp_util.h"
49 #include "classic/avdtp_source.h"
50 #include "classic/a2dp_source.h"
51 
52 #define AVDTP_MEDIA_PAYLOAD_HEADER_SIZE 12
53 
54 static const char * default_a2dp_source_service_name = "BTstack A2DP Source Service";
55 static const char * default_a2dp_source_service_provider_name = "BTstack A2DP Source Service Provider";
56 static avdtp_context_t a2dp_source_context;
57 
58 static a2dp_state_t app_state = A2DP_IDLE;
59 static avdtp_stream_endpoint_context_t sc;
60 
61 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
62 
63 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){
64     uint8_t* attribute;
65     de_create_sequence(service);
66 
67     // 0x0000 "Service Record Handle"
68     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
69     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
70 
71     // 0x0001 "Service Class ID List"
72     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
73     attribute = de_push_sequence(service);
74     {
75         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SOURCE);
76     }
77     de_pop_sequence(service, attribute);
78 
79     // 0x0004 "Protocol Descriptor List"
80     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
81     attribute = de_push_sequence(service);
82     {
83         uint8_t* l2cpProtocol = de_push_sequence(attribute);
84         {
85             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
86             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);
87         }
88         de_pop_sequence(attribute, l2cpProtocol);
89 
90         uint8_t* avProtocol = de_push_sequence(attribute);
91         {
92             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
93             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
94         }
95         de_pop_sequence(attribute, avProtocol);
96     }
97     de_pop_sequence(service, attribute);
98 
99     // 0x0005 "Public Browse Group"
100     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
101     attribute = de_push_sequence(service);
102     {
103         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
104     }
105     de_pop_sequence(service, attribute);
106 
107     // 0x0009 "Bluetooth Profile Descriptor List"
108     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
109     attribute = de_push_sequence(service);
110     {
111         uint8_t *a2dProfile = de_push_sequence(attribute);
112         {
113             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
114             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
115         }
116         de_pop_sequence(attribute, a2dProfile);
117     }
118     de_pop_sequence(service, attribute);
119 
120 
121     // 0x0100 "Service Name"
122     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
123     if (service_name){
124         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
125     } else {
126         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_name), (uint8_t *) default_a2dp_source_service_name);
127     }
128 
129     // 0x0100 "Provider Name"
130     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
131     if (service_provider_name){
132         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
133     } else {
134         de_add_data(service,  DE_STRING, strlen(default_a2dp_source_service_provider_name), (uint8_t *) default_a2dp_source_service_provider_name);
135     }
136 
137     // 0x0311 "Supported Features"
138     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
139     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
140 }
141 
142 
143 static void a2dp_streaming_emit_can_send_media_packet_now(btstack_packet_handler_t callback, uint16_t cid, uint8_t seid){
144     if (!callback) return;
145     uint8_t event[8];
146     int pos = 0;
147     event[pos++] = HCI_EVENT_A2DP_META;
148     event[pos++] = sizeof(event) - 2;
149     event[pos++] = A2DP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW;
150     little_endian_store_16(event, pos, cid);
151     pos += 2;
152     event[pos++] = seid;
153     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154 }
155 
156 static inline void a2dp_signaling_emit_media_codec_sbc(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
157     if (!callback) return;
158     if (event_size < 18) return;
159     event[0] = HCI_EVENT_A2DP_META;
160     event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION;
161     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
162 }
163 
164 static inline void a2dp_signaling_emit_reject_cmd(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
165     if (!callback) return;
166     if (event_size < 18) return;
167     event[0] = HCI_EVENT_A2DP_META;
168     event[2] = A2DP_SUBEVENT_COMMAND_REJECTED;
169     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
170 }
171 
172 static void a2dp_signaling_emit_connection_established(btstack_packet_handler_t callback, uint16_t cid, bd_addr_t addr, uint8_t status){
173     if (!callback) return;
174     uint8_t event[12];
175     int pos = 0;
176     event[pos++] = HCI_EVENT_A2DP_META;
177     event[pos++] = sizeof(event) - 2;
178     event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED;
179     little_endian_store_16(event, pos, cid);
180     pos += 2;
181     reverse_bd_addr(addr,&event[pos]);
182     pos += 6;
183     event[pos++] = status;
184     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
185 }
186 
187 static void a2dp_signaling_emit_control_command(btstack_packet_handler_t callback, uint16_t cid, uint8_t local_seid, uint8_t cmd){
188     if (!callback) return;
189     uint8_t event[6];
190     int pos = 0;
191     event[pos++] = HCI_EVENT_A2DP_META;
192     event[pos++] = sizeof(event) - 2;
193     event[pos++] = cmd;
194     little_endian_store_16(event, pos, cid);
195     pos += 2;
196     event[pos++] = local_seid;
197     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
198 }
199 
200 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
201     UNUSED(channel);
202     UNUSED(size);
203 
204     uint8_t signal_identifier;
205     uint8_t status;
206     uint8_t local_seid;
207     uint8_t remote_seid;
208     uint16_t cid;
209     bd_addr_t address;
210 
211     if (packet_type != HCI_EVENT_PACKET) return;
212     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
213 
214     switch (packet[2]){
215         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:{
216             avdtp_subevent_signaling_connection_established_get_bd_addr(packet, sc.remote_addr);
217             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
218             status = avdtp_subevent_signaling_connection_established_get_status(packet);
219 
220             if (status != 0){
221                 log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION failed status %d ---", status);
222                 app_state = A2DP_IDLE;
223                 a2dp_signaling_emit_connection_established(a2dp_source_context.a2dp_callback, cid, sc.remote_addr, status);
224                 break;
225             }
226             log_info("A2DP_SUBEVENT_SIGNALING_CONNECTION established avdtp_cid 0x%02x ---", a2dp_source_context.avdtp_cid);
227             sc.active_remote_sep = NULL;
228             sc.active_remote_sep_index = 0;
229             app_state = A2DP_W2_DISCOVER_SEPS;
230             a2dp_signaling_emit_connection_established(a2dp_source_context.a2dp_callback, cid, sc.remote_addr, status);
231             avdtp_source_discover_stream_endpoints(cid);
232             break;
233         }
234         case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND:
235             break;
236 
237         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:{
238             log_info("A2DP received SBC capability.");
239             if (!sc.local_stream_endpoint) {
240                 log_error("invalid local seid %d", avdtp_subevent_signaling_media_codec_sbc_capability_get_local_seid(packet));
241                 return;
242             }
243             log_info("A2DP received SBC capability.");
244 
245             uint8_t sampling_frequency = avdtp_choose_sbc_sampling_frequency(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_sampling_frequency_bitmap(packet));
246             uint8_t channel_mode = avdtp_choose_sbc_channel_mode(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_channel_mode_bitmap(packet));
247             uint8_t block_length = avdtp_choose_sbc_block_length(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_block_length_bitmap(packet));
248             uint8_t subbands = avdtp_choose_sbc_subbands(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
249 
250             uint8_t allocation_method = avdtp_choose_sbc_allocation_method(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_allocation_method_bitmap(packet));
251             uint8_t max_bitpool_value = avdtp_choose_sbc_max_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_max_bitpool_value(packet));
252             uint8_t min_bitpool_value = avdtp_choose_sbc_min_bitpool_value(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_min_bitpool_value(packet));
253 
254             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[0] = (sampling_frequency << 4) | channel_mode;
255             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[1] = (block_length << 4) | (subbands << 2) | allocation_method;
256             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[2] = min_bitpool_value;
257             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[3] = max_bitpool_value;
258 
259             sc.local_stream_endpoint->remote_configuration_bitmap = store_bit16(sc.local_stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
260             sc.local_stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
261             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
262 
263             app_state = A2DP_W2_SET_CONFIGURATION;
264             break;
265         }
266         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
267             log_info("received non SBC codec. not implemented");
268             break;
269 
270         case AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY:
271             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_MEDIA_TRANSPORT_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_media_transport_capability_get_remote_seid(packet));
272             break;
273         case AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY:
274             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_REPORTING_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_reporting_capability_get_remote_seid(packet));
275             break;
276         case AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY:
277             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_DELAY_REPORTING_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_delay_reporting_capability_get_remote_seid(packet));
278             break;
279         case AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY:
280             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_RECOVERY_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_recovery_capability_get_remote_seid(packet));
281             break;
282         case AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY:
283             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_CONTENT_PROTECTION_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_content_protection_capability_get_remote_seid(packet));
284             break;
285         case AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY:
286             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_HEADER_COMPRESSION_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_header_compression_capability_get_remote_seid(packet));
287             break;
288         case AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY:
289             log_info("received, but not forwarded: AVDTP_SUBEVENT_SIGNALING_MULTIPLEXING_CAPABILITY, remote seid %d\n", avdtp_subevent_signaling_multiplexing_capability_get_remote_seid(packet));
290             break;
291         case AVDTP_SUBEVENT_SIGNALING_CAPABILITY_DONE:
292             break;
293 
294         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{
295             // TODO check cid
296             sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);
297             sc.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet);
298             sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
299             sc.allocation_method = avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet) - 1;
300             sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
301             sc.channel_mode = avdtp_subevent_signaling_media_codec_sbc_configuration_get_channel_mode(packet);
302             // TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
303             log_info("A2DP received SBC Config: sample rate %u, max bitpool %u.", sc.sampling_frequency, sc.max_bitpool_value);
304             app_state = A2DP_W2_OPEN_STREAM_WITH_SEID;
305             a2dp_signaling_emit_media_codec_sbc(a2dp_source_context.a2dp_callback, packet, size);
306             break;
307         }
308 
309         case AVDTP_SUBEVENT_STREAMING_CAN_SEND_MEDIA_PACKET_NOW:
310             cid = avdtp_subevent_streaming_can_send_media_packet_now_get_avdtp_cid(packet);
311             a2dp_streaming_emit_can_send_media_packet_now(a2dp_source_context.a2dp_callback, cid, 0);
312             break;
313 
314         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
315             avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address);
316             status = avdtp_subevent_streaming_connection_established_get_status(packet);
317             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
318             remote_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet);
319             local_seid  = avdtp_subevent_streaming_connection_established_get_local_seid(packet);
320             if (status != 0){
321                 log_info("AVDTP_SUBEVENT_STREAMING_CONNECTION could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
322                 a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, cid, address, local_seid, remote_seid, status);
323                 break;
324             }
325             log_info("AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED --- avdtp_cid 0x%02x, local seid %d, remote seid %d", cid, local_seid, remote_seid);
326             app_state = A2DP_STREAMING_OPENED;
327             a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, cid, address, local_seid, remote_seid, 0);
328             break;
329 
330         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
331             // TODO check cid
332             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
333             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
334             log_info("A2DP Accepted %d, state %d", signal_identifier, app_state);
335 
336             switch (app_state){
337                 case A2DP_W2_DISCOVER_SEPS:
338                 case A2DP_W2_GET_CAPABILITIES:
339                 case A2DP_W2_GET_ALL_CAPABILITIES:
340                     app_state = A2DP_W2_GET_ALL_CAPABILITIES;
341                     sc.active_remote_sep = avdtp_source_remote_sep(cid, sc.active_remote_sep_index++);
342                     if (!sc.active_remote_sep) {
343                         app_state = A2DP_IDLE;
344                         a2dp_streaming_emit_connection_established(a2dp_source_context.a2dp_callback, cid, sc.remote_addr, 0, 0, AVDTP_SEID_DOES_NOT_EXIST);
345                         break;
346                     }
347                     avdtp_source_get_capabilities(cid, sc.active_remote_sep->seid);
348                     break;
349                 case A2DP_W2_SET_CONFIGURATION:{
350                     if (!sc.local_stream_endpoint) return;
351                     log_info("A2DP initiate set configuration locally and wait for response ... ");
352                     app_state = A2DP_IDLE;
353                     avdtp_source_set_configuration(cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid, sc.local_stream_endpoint->remote_configuration_bitmap, sc.local_stream_endpoint->remote_configuration);
354                     break;
355                 }
356                 case A2DP_W2_OPEN_STREAM_WITH_SEID:{
357                     log_info("A2DP open stream ");
358                     app_state = A2DP_W4_OPEN_STREAM_WITH_SEID;
359                     avdtp_source_open_stream(cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
360                     break;
361                 }
362                 case A2DP_STREAMING_OPENED:
363                     if (!a2dp_source_context.a2dp_callback) return;
364                     switch (signal_identifier){
365                         case  AVDTP_SI_START:
366                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_STARTED);
367                             break;
368                         case AVDTP_SI_SUSPEND:
369                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_SUSPENDED);
370                             break;
371                         case AVDTP_SI_ABORT:
372                         case AVDTP_SI_CLOSE:
373                             a2dp_signaling_emit_control_command(a2dp_source_context.a2dp_callback, cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), A2DP_SUBEVENT_STREAM_STOPPED);
374                             break;
375                         default:
376                             break;
377                     }
378                     break;
379                 default:
380                     app_state = A2DP_IDLE;
381                     break;
382             }
383 
384             break;
385         case AVDTP_SUBEVENT_SIGNALING_REJECT:
386         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
387             app_state = A2DP_IDLE;
388             a2dp_signaling_emit_reject_cmd(a2dp_source_context.a2dp_callback, packet, size);
389             break;
390         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:{
391             app_state = A2DP_IDLE;
392             uint8_t event[6];
393             int pos = 0;
394             event[pos++] = HCI_EVENT_A2DP_META;
395             event[pos++] = sizeof(event) - 2;
396             event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED;
397             little_endian_store_16(event, pos, avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet));
398             pos += 2;
399             event[pos++] = avdtp_subevent_streaming_connection_released_get_local_seid(packet);
400             (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
401             break;
402         }
403         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:{
404             app_state = A2DP_IDLE;
405             uint8_t event[6];
406             int pos = 0;
407             event[pos++] = HCI_EVENT_A2DP_META;
408             event[pos++] = sizeof(event) - 2;
409             event[pos++] = A2DP_SUBEVENT_STREAM_RELEASED;
410             little_endian_store_16(event, pos, avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet));
411             pos += 2;
412             event[pos++] = avdtp_subevent_streaming_connection_released_get_local_seid(packet);
413             (*a2dp_source_context.a2dp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
414             break;
415         }
416         default:
417             app_state = A2DP_IDLE;
418             log_info("not implemented");
419             break;
420     }
421 }
422 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){
423     if (callback == NULL){
424         log_error("a2dp_source_register_packet_handler called with NULL callback");
425         return;
426     }
427     avdtp_source_register_packet_handler(&packet_handler);
428     a2dp_source_context.a2dp_callback = callback;
429 }
430 
431 void a2dp_source_init(void){
432     avdtp_source_init(&a2dp_source_context);
433     l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
434 }
435 
436 uint8_t a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
437     uint8_t * codec_capabilities, uint16_t codec_capabilities_len,
438     uint8_t * media_codec_info, uint16_t media_codec_info_len, uint8_t * local_seid){
439     *local_seid = 0;
440     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type);
441     if (!local_stream_endpoint){
442         return BTSTACK_MEMORY_ALLOC_FAILED;
443     }
444     *local_seid = avdtp_local_seid(local_stream_endpoint);
445     avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
446     avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
447         codec_capabilities, codec_capabilities_len);
448     local_stream_endpoint->remote_configuration.media_codec.media_codec_information     = media_codec_info;
449     local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len;
450     return ERROR_CODE_SUCCESS;
451 }
452 
453 uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint8_t loc_seid, uint16_t * a2dp_cid){
454     sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(loc_seid, &a2dp_source_context);
455     if (!sc.local_stream_endpoint){
456         log_error(" no local_stream_endpoint for seid %d", loc_seid);
457         return AVDTP_SEID_DOES_NOT_EXIST;
458     }
459     memcpy(sc.remote_addr, remote_addr, 6);
460     return avdtp_source_connect(remote_addr, a2dp_cid);
461 }
462 
463 uint8_t a2dp_source_disconnect(uint16_t a2dp_cid){
464     return avdtp_disconnect(a2dp_cid, &a2dp_source_context);
465 }
466 
467 uint8_t a2dp_source_start_stream(uint16_t a2dp_cid, uint8_t local_seid){
468     return avdtp_start_stream(a2dp_cid, local_seid, &a2dp_source_context);
469 }
470 
471 uint8_t a2dp_source_pause_stream(uint16_t a2dp_cid, uint8_t local_seid){
472     return avdtp_suspend_stream(a2dp_cid, local_seid, &a2dp_source_context);
473 }
474 
475 static void a2dp_source_setup_media_header(uint8_t * media_packet, int size, int *offset, uint8_t marker, uint16_t sequence_number){
476     if (size < AVDTP_MEDIA_PAYLOAD_HEADER_SIZE){
477         log_error("small outgoing buffer");
478         return;
479     }
480 
481     uint8_t  rtp_version = 2;
482     uint8_t  padding = 0;
483     uint8_t  extension = 0;
484     uint8_t  csrc_count = 0;
485     uint8_t  payload_type = 0x60;
486     // uint16_t sequence_number = stream_endpoint->sequence_number;
487     uint32_t timestamp = btstack_run_loop_get_time_ms();
488     uint32_t ssrc = 0x11223344;
489 
490     // rtp header (min size 12B)
491     int pos = 0;
492     // int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
493 
494     media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count;
495     media_packet[pos++] = (marker << 1) | payload_type;
496     big_endian_store_16(media_packet, pos, sequence_number);
497     pos += 2;
498     big_endian_store_32(media_packet, pos, timestamp);
499     pos += 4;
500     big_endian_store_32(media_packet, pos, ssrc); // only used for multicast
501     pos += 4;
502     *offset = pos;
503 }
504 
505 void a2dp_source_stream_endpoint_request_can_send_now(uint16_t a2dp_cid, uint8_t local_seid){
506     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
507     if (!stream_endpoint) {
508         log_error("A2DP source: no stream_endpoint with seid %d", local_seid);
509         return;
510     }
511     if (a2dp_source_context.avdtp_cid != a2dp_cid){
512         log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid);
513         return;
514     }
515     stream_endpoint->send_stream = 1;
516     avdtp_request_can_send_now_initiator(stream_endpoint->connection, stream_endpoint->l2cap_media_cid);
517 }
518 
519 int a2dp_max_media_payload_size(uint16_t a2dp_cid, uint8_t local_seid){
520     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
521     if (!stream_endpoint) {
522         log_error("A2DP source: no stream_endpoint with seid %d", local_seid);
523         return 0;
524     }
525     if (a2dp_source_context.avdtp_cid != a2dp_cid){
526         log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid);
527         return 0;
528     }
529 
530     if (stream_endpoint->l2cap_media_cid == 0){
531         log_error("A2DP source: no media connection for seid %d", local_seid);
532         return 0;
533     }
534     return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - AVDTP_MEDIA_PAYLOAD_HEADER_SIZE;
535 }
536 
537 static void a2dp_source_copy_media_payload(uint8_t * media_packet, int size, int * offset, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames){
538     if (size < num_bytes_to_copy + 1){
539         log_error("small outgoing buffer: buffer size %u, but need %u", size, num_bytes_to_copy + 1);
540         return;
541     }
542 
543     int pos = *offset;
544     media_packet[pos++] = num_frames; // (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
545     memcpy(media_packet + pos, storage, num_bytes_to_copy);
546     pos += num_bytes_to_copy;
547     *offset = pos;
548 }
549 
550 int a2dp_source_stream_send_media_payload(uint16_t a2dp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){
551     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
552     if (!stream_endpoint) {
553         log_error("A2DP source: no stream_endpoint with seid %d", local_seid);
554         return 0;
555     }
556     if (a2dp_source_context.avdtp_cid != a2dp_cid){
557         log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid);
558         return 0;
559     }
560 
561     if (stream_endpoint->l2cap_media_cid == 0){
562         log_error("A2DP source: no media connection for seid %d", local_seid);
563         return 0;
564     }
565 
566     int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
567     int offset = 0;
568 
569     l2cap_reserve_packet_buffer();
570     uint8_t * media_packet = l2cap_get_outgoing_buffer();
571     //int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
572     a2dp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number);
573     a2dp_source_copy_media_payload(media_packet, size, &offset, storage, num_bytes_to_copy, num_frames);
574     stream_endpoint->sequence_number++;
575     l2cap_send_prepared(stream_endpoint->l2cap_media_cid, offset);
576     return size;
577 }
578