xref: /btstack/src/classic/avdtp_source.c (revision 174a0c1c8d4a5183616f89c4f14b8b88926d49b0)
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__ "avdtp_source.c"
40 
41 
42 #include <stdint.h>
43 #include <string.h>
44 
45 #include "bluetooth_psm.h"
46 #include "bluetooth_sdp.h"
47 #include "btstack_debug.h"
48 #include "btstack_event.h"
49 #include "l2cap.h"
50 
51 #include "classic/avdtp.h"
52 #include "classic/avdtp_util.h"
53 #include "classic/avdtp_source.h"
54 
55 #define AVDTP_MEDIA_PAYLOAD_HEADER_SIZE 12
56 
57 void avdtp_source_register_media_transport_category(uint8_t seid){
58     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
59     avdtp_register_media_transport_category(stream_endpoint);
60 }
61 
62 void avdtp_source_register_reporting_category(uint8_t seid){
63     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
64     avdtp_register_reporting_category(stream_endpoint);
65 }
66 
67 void avdtp_source_register_delay_reporting_category(uint8_t seid){
68     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
69     avdtp_register_delay_reporting_category(stream_endpoint);
70 }
71 
72 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
73     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
74     avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets);
75 }
76 
77 void avdtp_source_register_content_protection_category(uint8_t seid, uint16_t cp_type, const uint8_t * cp_type_value, uint8_t cp_type_value_len){
78     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
79     avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len);
80 }
81 
82 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
83     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
84     avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery);
85 }
86 
87 void avdtp_source_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, const uint8_t *media_codec_info, uint16_t media_codec_info_len){
88     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
89     avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len);
90 }
91 
92 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
93     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(seid);
94     avdtp_register_multiplexing_category(stream_endpoint, fragmentation);
95 }
96 
97 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
98     return avdtp_create_stream_endpoint(sep_type, media_type);
99 }
100 
101 void avdtp_source_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
102     avdtp_finalize_stream_endpoint(stream_endpoint);
103 }
104 
105 uint8_t avdtp_source_connect(bd_addr_t remote, uint16_t * avdtp_cid){
106     return avdtp_connect(remote, AVDTP_ROLE_SOURCE, avdtp_cid);
107 }
108 
109 uint8_t avdtp_source_disconnect(uint16_t avdtp_cid){
110     return avdtp_disconnect(avdtp_cid);
111 }
112 
113 uint8_t avdtp_source_open_stream(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid){
114     return avdtp_open_stream(avdtp_cid, local_seid, remote_seid);
115 }
116 
117 uint8_t avdtp_source_start_stream(uint16_t avdtp_cid, uint8_t local_seid){
118     return avdtp_start_stream(avdtp_cid, local_seid);
119 }
120 
121 uint8_t avdtp_source_stop_stream(uint16_t avdtp_cid, uint8_t local_seid){
122     return avdtp_stop_stream(avdtp_cid, local_seid);
123 }
124 
125 uint8_t avdtp_source_abort_stream(uint16_t avdtp_cid, uint8_t local_seid){
126     return avdtp_abort_stream(avdtp_cid, local_seid);
127 }
128 
129 uint8_t avdtp_source_suspend(uint16_t avdtp_cid, uint8_t local_seid){
130     return avdtp_suspend_stream(avdtp_cid, local_seid);
131 }
132 
133 uint8_t avdtp_source_discover_stream_endpoints(uint16_t avdtp_cid){
134     return avdtp_discover_stream_endpoints(avdtp_cid);
135 }
136 
137 uint8_t avdtp_source_get_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
138     return avdtp_get_capabilities(avdtp_cid, remote_seid);
139 }
140 
141 uint8_t avdtp_source_get_all_capabilities(uint16_t avdtp_cid, uint8_t remote_seid){
142     return avdtp_get_all_capabilities(avdtp_cid, remote_seid);
143 }
144 
145 uint8_t avdtp_source_get_configuration(uint16_t avdtp_cid, uint8_t remote_seid){
146     return avdtp_get_configuration(avdtp_cid, remote_seid);
147 }
148 
149 uint8_t avdtp_source_set_configuration(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
150     return avdtp_set_configuration(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration);
151 }
152 
153 uint8_t avdtp_source_reconfigure(uint16_t avdtp_cid, uint8_t local_seid, uint8_t remote_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
154     return avdtp_reconfigure(avdtp_cid, local_seid, remote_seid, configured_services_bitmap, configuration);
155 }
156 
157 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){
158     btstack_assert(callback != NULL);
159 
160     avdtp_register_source_packet_handler(callback);
161 }
162 
163 void avdtp_source_init(void) {
164     avdtp_init();
165 }
166 
167 void avdtp_source_deinit(void){
168     avdtp_deinit();
169 }
170 
171 static void avdtp_source_setup_media_header(uint8_t * media_packet, uint8_t marker, uint16_t sequence_number){
172     uint8_t  rtp_version = 2;
173     uint8_t  padding = 0;
174     uint8_t  extension = 0;
175     uint8_t  csrc_count = 0;
176     uint8_t  payload_type = 0x60;
177     // uint16_t sequence_number = stream_endpoint->sequence_number;
178     uint32_t timestamp = btstack_run_loop_get_time_ms();
179     uint32_t ssrc = 0x11223344;
180 
181     // rtp header (min size 12B)
182     uint16_t pos = 0;
183     media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count;
184     media_packet[pos++] = (marker << 1) | payload_type;
185     big_endian_store_16(media_packet, pos, sequence_number);
186     pos += 2;
187     big_endian_store_32(media_packet, pos, timestamp);
188     pos += 4;
189     big_endian_store_32(media_packet, pos, ssrc); // only used for multicast
190 }
191 
192 // @deprecated, use avdtp_source_stream_send_media_payload_rtp instead
193 int avdtp_source_stream_send_media_payload(uint16_t avdtp_cid, uint8_t local_seid, uint8_t * payload, int payload_size, uint8_t num_frames, uint8_t marker){
194     UNUSED(avdtp_cid);
195 
196     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
197     if (!stream_endpoint) {
198         log_error("avdtp source: no stream_endpoint with seid %d", local_seid);
199         return 0;
200     }
201 
202     if (stream_endpoint->l2cap_media_cid == 0){
203         log_error("avdtp source: no media connection for seid %d", local_seid);
204         return 0;
205     }
206 
207     uint16_t buffer_size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
208     uint16_t packet_size = AVDTP_MEDIA_PAYLOAD_HEADER_SIZE + payload_size + 1;
209     if (packet_size >= buffer_size) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
210 
211     l2cap_reserve_packet_buffer();
212     uint8_t * media_packet = l2cap_get_outgoing_buffer();
213     avdtp_source_setup_media_header(media_packet, marker, stream_endpoint->sequence_number);
214     media_packet[AVDTP_MEDIA_PAYLOAD_HEADER_SIZE] = num_frames; // (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
215     (void)memcpy(&media_packet[AVDTP_MEDIA_PAYLOAD_HEADER_SIZE +1], payload, payload_size);
216     stream_endpoint->sequence_number++;
217     l2cap_send_prepared(stream_endpoint->l2cap_media_cid, packet_size);
218     return packet_size;
219 }
220 
221 uint8_t avdtp_source_stream_send_media_payload_rtp(uint16_t avdtp_cid, uint8_t local_seid, uint8_t marker, uint8_t * payload, uint16_t payload_size){
222     UNUSED(avdtp_cid);
223 
224     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
225     if (!stream_endpoint) {
226         log_error("avdtp source: no stream_endpoint with seid %d", local_seid);
227         return 0;
228     }
229 
230     if (stream_endpoint->l2cap_media_cid == 0){
231         log_error("avdtp source: no media connection for seid %d", local_seid);
232         return 0;
233     }
234 
235     uint16_t buffer_size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
236     uint16_t packet_size = AVDTP_MEDIA_PAYLOAD_HEADER_SIZE + payload_size;
237     if (packet_size >= buffer_size) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
238     l2cap_reserve_packet_buffer();
239     uint8_t * media_packet = l2cap_get_outgoing_buffer();
240     avdtp_source_setup_media_header(media_packet, marker, stream_endpoint->sequence_number);
241     (void)memcpy(&media_packet[AVDTP_MEDIA_PAYLOAD_HEADER_SIZE], payload, payload_size);
242     stream_endpoint->sequence_number++;
243     return l2cap_send_prepared(stream_endpoint->l2cap_media_cid, packet_size);
244 }
245 
246 uint8_t avdtp_source_stream_send_media_packet(uint16_t avdtp_cid, uint8_t local_seid, const uint8_t * packet, uint16_t size){
247     UNUSED(avdtp_cid);
248 
249     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
250     if (!stream_endpoint) {
251         log_error("avdtp source: no stream_endpoint with seid %d", local_seid);
252         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
253     }
254 
255     if (stream_endpoint->l2cap_media_cid == 0){
256         log_error("avdtp source: no media connection for seid %d", local_seid);
257         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
258     }
259 
260     return l2cap_send(stream_endpoint->l2cap_media_cid, (uint8_t*) packet, size);
261 }
262 
263 
264 
265 
266 void avdtp_source_stream_endpoint_request_can_send_now(uint16_t avdtp_cid, uint8_t local_seid){
267     UNUSED(avdtp_cid);
268 
269     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
270     if (!stream_endpoint) {
271         log_error("AVDTP source: no stream_endpoint with seid %d", local_seid);
272         return;
273     }
274     stream_endpoint->request_can_send_now = true;
275 	l2cap_request_can_send_now_event(stream_endpoint->l2cap_media_cid);
276 }
277 
278 int avdtp_max_media_payload_size(uint16_t avdtp_cid, uint8_t local_seid){
279     UNUSED(avdtp_cid);
280 
281     avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
282     if (!stream_endpoint) {
283         log_error("A2DP source: no stream_endpoint with seid %d", local_seid);
284         return 0;
285     }
286 
287     if (stream_endpoint->l2cap_media_cid == 0){
288         log_error("A2DP source: no media connection for seid %d", local_seid);
289         return 0;
290     }
291     return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - AVDTP_MEDIA_PAYLOAD_HEADER_SIZE;
292 }
293