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