xref: /btstack/src/classic/avdtp_source.c (revision 842624026c42dd6dc202dcb2be5461e5faa3a7c2)
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 #include "btstack_ring_buffer.h"
55 
56 static const char * default_avdtp_source_service_name = "BTstack AVDTP Source Service";
57 static const char * default_avdtp_source_service_provider_name = "BTstack AVDTP Source Service Provider";
58 
59 static avdtp_context_t avdtp_source_context;
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, AUDIO_SOURCE_GROUP);
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, 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_avdtp_source_service_name), (uint8_t *) default_avdtp_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_avdtp_source_service_provider_name), (uint8_t *) default_avdtp_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 void avdtp_source_register_media_transport_category(uint8_t seid){
144     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
145     avdtp_register_media_transport_category(stream_endpoint);
146 }
147 
148 void avdtp_source_register_reporting_category(uint8_t seid){
149     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
150     avdtp_register_reporting_category(stream_endpoint);
151 }
152 
153 void avdtp_source_register_delay_reporting_category(uint8_t seid){
154     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
155     avdtp_register_delay_reporting_category(stream_endpoint);
156 }
157 
158 void avdtp_source_register_recovery_category(uint8_t seid, uint8_t maximum_recovery_window_size, uint8_t maximum_number_media_packets){
159     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
160     avdtp_register_recovery_category(stream_endpoint, maximum_recovery_window_size, maximum_number_media_packets);
161 }
162 
163 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){
164     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
165     avdtp_register_content_protection_category(stream_endpoint, cp_type, cp_type_value, cp_type_value_len);
166 }
167 
168 void avdtp_source_register_header_compression_category(uint8_t seid, uint8_t back_ch, uint8_t media, uint8_t recovery){
169     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
170     avdtp_register_header_compression_category(stream_endpoint, back_ch, media, recovery);
171 }
172 
173 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){
174     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
175     avdtp_register_media_codec_category(stream_endpoint, media_type, media_codec_type, media_codec_info, media_codec_info_len);
176 }
177 
178 void avdtp_source_register_multiplexing_category(uint8_t seid, uint8_t fragmentation){
179     avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(seid, &avdtp_source_context);
180     avdtp_register_multiplexing_category(stream_endpoint, fragmentation);
181 }
182 
183 avdtp_stream_endpoint_t * avdtp_source_create_stream_endpoint(avdtp_sep_type_t sep_type, avdtp_media_type_t media_type){
184     return avdtp_create_stream_endpoint(sep_type, media_type, &avdtp_source_context);
185 }
186 
187 void avdtp_source_register_packet_handler(btstack_packet_handler_t callback){
188     if (callback == NULL){
189         log_error("avdtp_source_register_packet_handler called with NULL callback");
190         return;
191     }
192     avdtp_source_context.avdtp_callback = callback;
193 }
194 
195 void avdtp_source_connect(bd_addr_t bd_addr){
196     avdtp_connection_t * connection = avdtp_connection_for_bd_addr(bd_addr, &avdtp_source_context);
197     if (!connection){
198         connection = avdtp_create_connection(bd_addr, &avdtp_source_context);
199     }
200     if (connection->state != AVDTP_SIGNALING_CONNECTION_IDLE) return;
201     connection->state = AVDTP_SIGNALING_CONNECTION_W4_L2CAP_CONNECTED;
202     l2cap_create_channel(packet_handler, connection->remote_addr, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, NULL);
203 }
204 
205 void avdtp_source_disconnect(uint16_t con_handle){
206     avdtp_disconnect(con_handle, &avdtp_source_context);
207 }
208 
209 void avdtp_source_open_stream(uint16_t con_handle, uint8_t acp_seid){
210     avdtp_open_stream(con_handle, acp_seid, &avdtp_source_context);
211 }
212 
213 void avdtp_source_start_stream(uint16_t con_handle, uint8_t acp_seid){
214     avdtp_start_stream(con_handle, acp_seid, &avdtp_source_context);
215 }
216 
217 void avdtp_source_stop_stream(uint16_t con_handle, uint8_t acp_seid){
218     avdtp_stop_stream(con_handle, acp_seid, &avdtp_source_context);
219 }
220 
221 void avdtp_source_abort_stream(uint16_t con_handle, uint8_t acp_seid){
222     avdtp_abort_stream(con_handle, acp_seid, &avdtp_source_context);
223 }
224 
225 void avdtp_source_discover_stream_endpoints(uint16_t con_handle){
226     avdtp_discover_stream_endpoints(con_handle, &avdtp_source_context);
227 }
228 
229 void avdtp_source_get_capabilities(uint16_t con_handle, uint8_t acp_seid){
230     avdtp_get_capabilities(con_handle, acp_seid, &avdtp_source_context);
231 }
232 
233 void avdtp_source_get_all_capabilities(uint16_t con_handle, uint8_t acp_seid){
234     avdtp_get_all_capabilities(con_handle, acp_seid, &avdtp_source_context);
235 }
236 
237 void avdtp_source_get_configuration(uint16_t con_handle, uint8_t acp_seid){
238     avdtp_get_configuration(con_handle, acp_seid, &avdtp_source_context);
239 }
240 
241 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){
242     avdtp_set_configuration(con_handle, int_seid, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context);
243 }
244 
245 void avdtp_source_reconfigure(uint16_t con_handle, uint8_t acp_seid, uint16_t configured_services_bitmap, avdtp_capabilities_t configuration){
246     avdtp_reconfigure(con_handle, acp_seid, configured_services_bitmap, configuration, &avdtp_source_context);
247 }
248 
249 void avdtp_source_suspend(uint16_t con_handle, uint8_t acp_seid){
250     avdtp_suspend(con_handle, acp_seid, &avdtp_source_context);
251 }
252 
253 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
254     avdtp_packet_handler(packet_type, channel, packet, size, &avdtp_source_context);
255 }
256 
257 
258 /* streaming part */
259 #define NUM_CHANNELS        2
260 #define SAMPLE_RATE         44100
261 #define BYTES_PER_AUDIO_SAMPLE   (2*NUM_CHANNELS)
262 #define LATENCY             300 // ms
263 
264 #ifndef M_PI
265 #define M_PI  3.14159265
266 #endif
267 #define TABLE_SIZE_441HZ   100
268 
269 typedef struct {
270     int16_t source[TABLE_SIZE_441HZ];
271     int left_phase;
272     int right_phase;
273 } paTestData;
274 
275 static paTestData sin_data;
276 
277 static void fill_audio_ring_buffer(void *userData, int num_samples_to_write, avdtp_stream_endpoint_t * stream_endpoint){
278     paTestData *data = (paTestData*)userData;
279     int count = 0;
280     while (btstack_ring_buffer_bytes_free(&stream_endpoint->audio_ring_buffer) >= BYTES_PER_AUDIO_SAMPLE && count < num_samples_to_write){
281         uint8_t write_data[BYTES_PER_AUDIO_SAMPLE];
282         *(int16_t*)&write_data[0] = data->source[data->left_phase];
283         *(int16_t*)&write_data[2] = data->source[data->right_phase];
284 
285         btstack_ring_buffer_write(&stream_endpoint->audio_ring_buffer, write_data, BYTES_PER_AUDIO_SAMPLE);
286         count++;
287 
288         data->left_phase += 1;
289         if (data->left_phase >= TABLE_SIZE_441HZ){
290             data->left_phase -= TABLE_SIZE_441HZ;
291         }
292         data->right_phase += 1;
293         if (data->right_phase >= TABLE_SIZE_441HZ){
294             data->right_phase -= TABLE_SIZE_441HZ;
295         }
296     }
297 }
298 
299 static void fill_sbc_ring_buffer(uint8_t * sbc_frame, int sbc_frame_size, avdtp_stream_endpoint_t * stream_endpoint){
300     if (btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= sbc_frame_size ){
301         // printf("    fill_sbc_ring_buffer\n");
302         uint8_t size_buffer = sbc_frame_size;
303         btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, &size_buffer, 1);
304         btstack_ring_buffer_write(&stream_endpoint->sbc_ring_buffer, sbc_frame, sbc_frame_size);
305     } else {
306         printf("No space in sbc buffer\n");
307     }
308 }
309 
310 
311 static void avdtp_source_stream_endpoint_run(avdtp_stream_endpoint_t * stream_endpoint){
312     // performe sbc encoding
313     int total_num_bytes_read = 0;
314     int num_audio_samples_to_read = btstack_sbc_encoder_num_audio_frames();
315     int audio_bytes_to_read = num_audio_samples_to_read * BYTES_PER_AUDIO_SAMPLE;
316 
317     // printf("run: audio_bytes_to_read:        %d\n", audio_bytes_to_read);
318     // printf("     audio buf, bytes available: %d\n", btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer));
319     // printf("     sbc buf,   bytes free:      %d\n", btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer));
320 
321     while (btstack_ring_buffer_bytes_available(&stream_endpoint->audio_ring_buffer) >= audio_bytes_to_read
322         && btstack_ring_buffer_bytes_free(&stream_endpoint->sbc_ring_buffer) >= 120){ // TODO use real value
323 
324         uint32_t number_of_bytes_read = 0;
325         uint8_t pcm_frame[256*BYTES_PER_AUDIO_SAMPLE];
326         btstack_ring_buffer_read(&stream_endpoint->audio_ring_buffer, pcm_frame, audio_bytes_to_read, &number_of_bytes_read);
327         // printf("     num audio bytes read %d\n", number_of_bytes_read);
328         btstack_sbc_encoder_process_data((int16_t *) pcm_frame);
329 
330         uint16_t sbc_frame_bytes = 119; //btstack_sbc_encoder_sbc_buffer_length();
331 
332         total_num_bytes_read += number_of_bytes_read;
333         fill_sbc_ring_buffer(btstack_sbc_encoder_sbc_buffer(), sbc_frame_bytes, stream_endpoint);
334     }
335     // schedule sending
336     if (total_num_bytes_read != 0){
337         stream_endpoint->state = AVDTP_STREAM_ENDPOINT_STREAMING_W2_SEND;
338         avdtp_request_can_send_now_self(stream_endpoint->connection, stream_endpoint->l2cap_media_cid);
339     }
340 }
341 
342 void avdtp_fill_audio_ring_buffer_timeout_handler(btstack_timer_source_t * timer){
343     avdtp_stream_endpoint_t * stream_endpoint = btstack_run_loop_get_timer_context(timer);
344     btstack_run_loop_set_timer(&stream_endpoint->fill_audio_ring_buffer_timer, stream_endpoint->fill_audio_ring_buffer_timeout_ms); // 2 seconds timeout
345     btstack_run_loop_add_timer(&stream_endpoint->fill_audio_ring_buffer_timer);
346     uint32_t now = btstack_run_loop_get_time_ms();
347 
348     uint32_t update_period_ms = stream_endpoint->fill_audio_ring_buffer_timeout_ms;
349     if (stream_endpoint->time_audio_data_sent > 0){
350         update_period_ms = now - stream_endpoint->time_audio_data_sent;
351     }
352     uint32_t num_samples = (update_period_ms * 44100) / 1000;
353     stream_endpoint->acc_num_missed_samples += (update_period_ms * 44100) % 1000;
354 
355     if (stream_endpoint->acc_num_missed_samples >= 1000){
356         num_samples++;
357         stream_endpoint->acc_num_missed_samples -= 1000;
358     }
359 
360     fill_audio_ring_buffer(&sin_data, num_samples, stream_endpoint);
361     stream_endpoint->time_audio_data_sent = now;
362 
363     avdtp_source_stream_endpoint_run(stream_endpoint);
364     //
365 }
366 
367 void avdtp_set_fill_audio_ring_buffer_timeout_ms(avdtp_stream_endpoint_t * stream_endpoint, uint32_t timeout_ms){
368     if (!stream_endpoint){
369         printf("avdtp_set_fill_audio_ring_buffer_timeout_ms: stream_endpoint does not exist\n");
370         return;
371     }
372     stream_endpoint->fill_audio_ring_buffer_timeout_ms = timeout_ms;
373 }
374 
375 void avdtp_source_init(void){
376     avdtp_source_context.stream_endpoints = NULL;
377     avdtp_source_context.connections = NULL;
378     avdtp_source_context.stream_endpoints_id_counter = 0;
379     avdtp_source_context.packet_handler = packet_handler;
380 
381     /* initialise sinusoidal wavetable */
382     int i;
383     for (i=0; i<TABLE_SIZE_441HZ; i++){
384         sin_data.source[i] = sin(((double)i/(double)TABLE_SIZE_441HZ) * M_PI * 2.)*32767;
385     }
386     sin_data.left_phase = sin_data.right_phase = 0;
387 
388     l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
389 }
390