xref: /btstack/src/classic/avdtp_source.c (revision 274391e8b2f0d74a87eb98f291f95ef8eb95044e)
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 <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <math.h>
48 
49 #include "btstack.h"
50 #include "avdtp.h"
51 #include "avdtp_util.h"
52 #include "avdtp_source.h"
53 
54 static avdtp_context_t * avdtp_source_context;
55 
56 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
57 
58 void avdtp_source_register_media_transport_category(uint8_t seid){
59     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
60     avdtp_register_media_transport_category(stream_endpoint);
61 }
62 
63 void avdtp_source_register_reporting_category(uint8_t seid){
64     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
65     avdtp_register_reporting_category(stream_endpoint);
66 }
67 
68 void avdtp_source_register_delay_reporting_category(uint8_t seid){
69     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
70     avdtp_register_delay_reporting_category(stream_endpoint);
71 }
72 
73 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
74     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
75     avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets);
76 }
77 
78 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){
79     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
80     avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len);
81 }
82 
83 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
84     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
85     avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery);
86 }
87 
88 void avdtp_source_register_media_codec_category(uint8_t seid, avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type, uint8_t * media_codec_info, uint16_t media_codec_info_len){
89     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
90     avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len);
91 }
92 
93 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
94     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, avdtp_source_context);
95     avdtp_register_multiplexing_category(stream_endpoint, fragmentation);
96 }
97 
98 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
99     return avdtp_create_stream_endpoint(sep_type, media_type, avdtp_source_context);
100 }
101 
102 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){
103     if (callback == NULL){
104         log_error("avdtp_source_register_packet_handler called with NULL callback");
105         return;
106     }
107     avdtp_source_context->avdtp_callback = callback;
108 }
109 
110 void avdtp_source_connect(bd_addr_t bd_addr){
111     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(bd_addr, avdtp_source_context);
112     if (!connection){
113         connection = avdtp_create_connection(bd_addr, avdtp_source_context);
114     }
115     if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return;
116     connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
117     l2cap_create_channel(packet_handler, connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, NULL);
118 }
119 
120 void avdtp_source_disconnect(uint16_t con_handle){
121     avdtp_disconnect(con_handle, avdtp_source_context);
122 }
123 
124 void avdtp_source_open_stream(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid){
125     avdtp_open_stream(con_handle, int_seid, acp_seid, avdtp_source_context);
126 }
127 
128 void avdtp_source_start_stream(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid){
129     avdtp_start_stream(con_handle, int_seid, acp_seid, avdtp_source_context);
130 }
131 
132 void avdtp_source_stop_stream(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid){
133     avdtp_stop_stream(con_handle, int_seid, acp_seid, avdtp_source_context);
134 }
135 
136 void avdtp_source_abort_stream(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid){
137     avdtp_abort_stream(con_handle, int_seid, acp_seid, avdtp_source_context);
138 }
139 
140 void avdtp_source_discover_stream_endpoints(uint16_t con_handle){
141     avdtp_discover_stream_endpoints(con_handle, avdtp_source_context);
142 }
143 
144 void avdtp_source_get_capabilities(uint16_t con_handle, uint8_t acp_seid){
145     avdtp_get_capabilities(con_handle, acp_seid, avdtp_source_context);
146 }
147 
148 void avdtp_source_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){
149     avdtp_get_all_capabilities(con_handle, acp_seid, avdtp_source_context);
150 }
151 
152 void avdtp_source_get_configuration(uint16_t con_handle, uint8_t acp_seid){
153     avdtp_get_configuration(con_handle, acp_seid, avdtp_source_context);
154 }
155 
156 void avdtp_source_set_configuration(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
157     avdtp_set_configuration(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, avdtp_source_context);
158 }
159 
160 void avdtp_source_reconfigure(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
161     avdtp_reconfigure(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, avdtp_source_context);
162 }
163 
164 void avdtp_source_suspend(uint16_t con_handle, uint8_t int_seid, uint8_t acp_seid){
165     avdtp_suspend(con_handle, int_seid, acp_seid, avdtp_source_context);
166 }
167 
168 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
169     avdtp_packet_handler(packet_type, channel, packet, size, avdtp_source_context);
170 }
171 
172 void avdtp_source_init(avdtp_context_t * avdtp_context){
173     if (!avdtp_context){
174         log_error("avdtp_source_context is NULL");
175         return;
176     }
177     avdtp_source_context = avdtp_context;
178     avdtp_source_context->stream_endpoints = NULL;
179     avdtp_source_context->connections = NULL;
180     avdtp_source_context->stream_endpoints_id_counter = 0;
181     avdtp_source_context->packet_handler = packet_handler;
182 
183     l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
184 }
185 
186 int avdtp_source_stream_endpoint_ready(avdtp_stream_endpoint_t * stream_endpoint){
187     if (!stream_endpoint) {
188         printf("no stream_endpoint");
189         return 0;
190     }
191     return (stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING || stream_endpoint->state == AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND);
192 }
193 
194 void avdtp_source_stream_endpoint_request_can_send_now(avdtp_stream_endpoint_t * stream_endpoint){
195     if (!stream_endpoint) {
196         printf("no stream_endpoint");
197         return;
198     }
199     stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND;
200     avdtp_request_can_send_now_self(stream_endpoint->connection, stream_endpoint->l2cap_media_cid);
201 }
202 
203 
204 static void avdtp_source_setup_media_header(uint8_t * media_packet, int size, int *offset, uint8_t marker, uint16_t sequence_number){
205     if (size < 12){
206         printf("small outgoing buffer\n");
207         return;
208     }
209 
210     uint8_t  rtp_version = 2;
211     uint8_t  padding = 0;
212     uint8_t  extension = 0;
213     uint8_t  csrc_count = 0;
214     uint8_t  payload_type = 0x60;
215     // uint16_t sequence_number = stream_endpoint->sequence_number;
216     uint32_t timestamp = btstack_run_loop_get_time_ms();
217     uint32_t ssrc = 0x11223344;
218 
219     // rtp header (min size 12B)
220     int pos = 0;
221     // int mtu = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid);
222 
223     media_packet[pos++] = (rtp_version << 6) | (padding << 5) | (extension << 4) | csrc_count;
224     media_packet[pos++] = (marker << 1) | payload_type;
225     big_endian_store_16(media_packet, pos, sequence_number);
226     pos += 2;
227     big_endian_store_32(media_packet, pos, timestamp);
228     pos += 4;
229     big_endian_store_32(media_packet, pos, ssrc); // only used for multicast
230     pos += 4;
231     *offset = pos;
232 }
233 
234 static void avdtp_source_copy_media_payload(uint8_t * media_packet, int size, int * offset, btstack_ring_buffer_t * sbc_ring_buffer){
235     if (size < 18){
236         printf("small outgoing buffer\n");
237         return;
238     }
239     int pos = *offset;
240     // media payload
241     // sbc_header (size 1B)
242     uint8_t sbc_header_index = pos;
243     pos++;
244     uint8_t fragmentation = 0;
245     uint8_t starting_packet = 0; // set to 1 for the first packet of a fragmented SBC frame
246     uint8_t last_packet = 0;     // set to 1 for the last packet of a fragmented SBC frame
247     uint8_t num_frames = 0;
248 
249     uint32_t total_sbc_bytes_read = 0;
250     uint8_t  sbc_frame_size = 0;
251     // payload
252     uint16_t sbc_frame_bytes = btstack_sbc_encoder_sbc_buffer_length();
253 
254     while (size - 13 - total_sbc_bytes_read >= sbc_frame_bytes && btstack_ring_buffer_bytes_available(sbc_ring_buffer)){
255         uint32_t number_of_bytes_read = 0;
256         btstack_ring_buffer_read(sbc_ring_buffer, &sbc_frame_size, 1, &number_of_bytes_read);
257         btstack_ring_buffer_read(sbc_ring_buffer, media_packet + pos, sbc_frame_size, &number_of_bytes_read);
258         pos += sbc_frame_size;
259         total_sbc_bytes_read += sbc_frame_size;
260         num_frames++;
261         // printf("send sbc frame: timestamp %d, seq. nr %d\n", timestamp, stream_endpoint->sequence_number);
262     }
263     media_packet[sbc_header_index] =  (fragmentation << 7) | (starting_packet << 6) | (last_packet << 5) | num_frames;
264     *offset = pos;
265 }
266 
267 void avdtp_source_stream_send_media_payload(uint16_t l2cap_media_cid, btstack_ring_buffer_t * sbc_ring_buffer, uint8_t marker){
268     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_l2cap_cid(l2cap_media_cid, avdtp_source_context);
269     if (!stream_endpoint) {
270         printf("no stream_endpoint found for 0x%02x", l2cap_media_cid);
271         return;
272     }
273 
274     int size = l2cap_get_remote_mtu_for_local_cid(l2cap_media_cid);
275     int offset = 0;
276 
277     l2cap_reserve_packet_buffer();
278     uint8_t * media_packet = l2cap_get_outgoing_buffer();
279     //int size = l2cap_get_remote_mtu_for_local_cid(l2cap_media_cid);
280     avdtp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number);
281     avdtp_source_copy_media_payload(media_packet, size, &offset, sbc_ring_buffer);
282     stream_endpoint->sequence_number++;
283     l2cap_send_prepared(l2cap_media_cid, offset);
284 }
285 
286 uint8_t avdtp_source_remote_seps_num(uint16_t avdtp_cid){
287     return avdtp_remote_seps_num(avdtp_cid, avdtp_source_context);
288 }
289 
290 avdtp_sep_t * avdtp_source_remote_sep(uint16_t avdtp_cid, uint8_t index){
291     return avdtp_remote_sep(avdtp_cid, index, avdtp_source_context);
292 }
293