xref: /btstack/src/classic/a2dp_source.c (revision ba155c2213004b1e1416627baa06a0ada97020a2)
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 #include <unistd.h>
46 
47 #include "btstack.h"
48 #include "avdtp.h"
49 #include "avdtp_util.h"
50 #include "avdtp_source.h"
51 #include "a2dp_source.h"
52 #include "sbc_encoder.h"
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 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 packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
168     UNUSED(channel);
169     UNUSED(size);
170 
171     bd_addr_t event_addr;
172     uint8_t signal_identifier;
173     uint8_t status;
174     avdtp_sep_t sep;
175     //
176 
177     switch (packet_type) {
178 
179         case HCI_EVENT_PACKET:
180             switch (hci_event_packet_get_type(packet)) {
181                 case HCI_EVENT_PIN_CODE_REQUEST:
182                     // inform about pin code request
183                     printf("Pin code request - using '0000'\n");
184                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
185                     hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
186                     break;
187                 case HCI_EVENT_DISCONNECTION_COMPLETE:
188                     // connection closed -> quit test app
189                     printf("\n --- a2dp source --- HCI_EVENT_DISCONNECTION_COMPLETE ---\n");
190                     break;
191                 case HCI_EVENT_AVDTP_META:
192                     switch (packet[2]){
193                         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
194                             avdtp_cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
195                             status = avdtp_subevent_signaling_connection_established_get_status(packet);
196                             if (status != 0){
197                                 printf(" --- a2dp source --- AVDTP_SUBEVENT_SIGNALING_CONNECTION could not be established, status %d ---\n", status);
198                                 break;
199                             }
200                             sc.active_remote_sep = NULL;
201                             next_remote_sep_index_to_query = 0;
202                             app_state = A2DP_W2_DISCOVER_SEPS;
203                             printf(" --- a2dp source --- AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x ---\n", avdtp_cid);
204                             avdtp_source_discover_stream_endpoints(avdtp_cid);
205                             break;
206 
207                         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
208                             status = avdtp_subevent_streaming_connection_established_get_status(packet);
209                             if (status != 0){
210                                 printf(" --- a2dp source --- AVDTP_SUBEVENT_STREAMING_CONNECTION could not be established, status %d ---\n", status);
211                                 break;
212                             }
213                             app_state = A2DP_STREAMING_OPENED;
214                             printf(" --- a2dp source --- AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED ---\n");
215                             break;
216 
217                         case AVDTP_SUBEVENT_SIGNALING_SEP_FOUND:
218                             if (app_state != A2DP_W2_DISCOVER_SEPS) return;
219                             sep.seid = avdtp_subevent_signaling_sep_found_get_seid(packet);
220                             sep.in_use = avdtp_subevent_signaling_sep_found_get_in_use(packet);
221                             sep.media_type = avdtp_subevent_signaling_sep_found_get_media_type(packet);
222                             sep.type = avdtp_subevent_signaling_sep_found_get_sep_type(packet);
223                             printf(" --- a2dp source --- Found sep: seid %u, in_use %d, media type %d, sep type %d (1-SNK)\n", sep.seid, sep.in_use, sep.media_type, sep.type);
224                             break;
225 
226                         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY:{
227                             printf(" AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY 0\n");
228                             if (!sc.local_stream_endpoint) return;
229                             printf(" AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CAPABILITY 1\n");
230                             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));
231                             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));
232                             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));
233                             uint8_t subbands = avdtp_choose_sbc_subbands(sc.local_stream_endpoint, avdtp_subevent_signaling_media_codec_sbc_capability_get_subbands_bitmap(packet));
234 
235                             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));
236                             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));
237                             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));
238 
239                             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[0] = (sampling_frequency << 4) | channel_mode;
240                             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[1] = (block_length << 4) | (subbands << 2) | allocation_method;
241                             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[2] = min_bitpool_value;
242                             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_information[3] = max_bitpool_value;
243 
244                             sc.local_stream_endpoint->remote_configuration_bitmap = store_bit16(sc.local_stream_endpoint->remote_configuration_bitmap, AVDTP_MEDIA_CODEC, 1);
245                             sc.local_stream_endpoint->remote_configuration.media_codec.media_type = AVDTP_AUDIO;
246                             sc.local_stream_endpoint->remote_configuration.media_codec.media_codec_type = AVDTP_CODEC_SBC;
247 
248                             app_state = A2DP_W2_SET_CONFIGURATION;
249                             break;
250                         }
251                         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CAPABILITY:
252                             printf(" --- a2dp source ---  received non SBC codec. not implemented\n");
253                             break;
254 
255                         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:{
256                             sc.sampling_frequency = avdtp_subevent_signaling_media_codec_sbc_configuration_get_sampling_frequency(packet);
257                             sc.block_length = avdtp_subevent_signaling_media_codec_sbc_configuration_get_block_length(packet);
258                             sc.subbands = avdtp_subevent_signaling_media_codec_sbc_configuration_get_subbands(packet);
259                             switch (avdtp_subevent_signaling_media_codec_sbc_configuration_get_allocation_method(packet)){
260                                 case AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS:
261                                     sc.allocation_method = SBC_LOUDNESS;
262                                     break;
263                                 case AVDTP_SBC_ALLOCATION_METHOD_SNR:
264                                     sc.allocation_method = SBC_SNR;
265                                     break;
266                             }
267                             sc.max_bitpool_value = avdtp_subevent_signaling_media_codec_sbc_configuration_get_max_bitpool_value(packet);
268                             // TODO: deal with reconfigure: avdtp_subevent_signaling_media_codec_sbc_configuration_get_reconfigure(packet);
269                             break;
270                         }
271 
272                         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
273                             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
274                             status = avdtp_subevent_signaling_accept_get_status(packet);
275                             printf(" --- a2dp source ---  Accepted %d\n", signal_identifier);
276 
277                             switch (app_state){
278                                 case A2DP_W2_DISCOVER_SEPS:
279                                     app_state = A2DP_W2_GET_ALL_CAPABILITIES;
280                                     sc.active_remote_sep = avdtp_source_remote_sep(avdtp_cid, next_remote_sep_index_to_query++);
281                                     printf(" --- a2dp source ---  Query get caps for seid %d\n", sc.active_remote_sep->seid);
282                                     avdtp_source_get_capabilities(avdtp_cid, sc.active_remote_sep->seid);
283                                     break;
284                                 case A2DP_W2_GET_CAPABILITIES:
285                                 case A2DP_W2_GET_ALL_CAPABILITIES:
286                                     if (next_remote_sep_index_to_query < avdtp_source_remote_seps_num(avdtp_cid)){
287                                         sc.active_remote_sep = avdtp_source_remote_sep(avdtp_cid, next_remote_sep_index_to_query++);
288                                         printf(" --- a2dp source ---  Query get caps for seid %d\n", sc.active_remote_sep->seid);
289                                         avdtp_source_get_capabilities(avdtp_cid, sc.active_remote_sep->seid);
290                                     } else {
291                                         printf(" --- a2dp source ---  No more remote seps found\n");
292                                         app_state = A2DP_IDLE;
293                                     }
294                                     break;
295                                 case A2DP_W2_SET_CONFIGURATION:{
296                                     if (!sc.local_stream_endpoint) return;
297                                     app_state = A2DP_W2_GET_CONFIGURATION;
298                                     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);
299                                     break;
300                                 }
301                                 case A2DP_W2_GET_CONFIGURATION:
302                                     app_state = A2DP_W2_OPEN_STREAM_WITH_SEID;
303                                     avdtp_source_get_configuration(avdtp_cid, sc.active_remote_sep->seid);
304                                     break;
305                                 case A2DP_W2_OPEN_STREAM_WITH_SEID:{
306                                     app_state = A2DP_W4_OPEN_STREAM_WITH_SEID;
307                                     btstack_sbc_encoder_init(&sc.sbc_encoder_state, SBC_MODE_STANDARD,
308                                         sc.block_length, sc.subbands,
309                                         sc.allocation_method, sc.sampling_frequency,
310                                         sc.max_bitpool_value);
311                                     avdtp_source_open_stream(avdtp_cid, avdtp_stream_endpoint_seid(sc.local_stream_endpoint), sc.active_remote_sep->seid);
312                                     break;
313                                 }
314                                 case A2DP_STREAMING_OPENED:
315                                     switch (signal_identifier){
316                                         case AVDTP_SI_START:
317                                             break;
318                                         case AVDTP_SI_CLOSE:
319                                             break;
320                                         case AVDTP_SI_SUSPEND:
321                                             break;
322                                         case AVDTP_SI_ABORT:
323                                             break;
324                                         default:
325                                             break;
326                                     }
327                                     break;
328                                 default:
329                                     app_state = A2DP_IDLE;
330                                     break;
331                             }
332 
333                             break;
334                         case AVDTP_SUBEVENT_SIGNALING_REJECT:
335                             app_state = A2DP_IDLE;
336                             signal_identifier = avdtp_subevent_signaling_reject_get_signal_identifier(packet);
337                             printf(" --- a2dp source ---  Rejected %d\n", signal_identifier);
338                             break;
339                         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
340                             app_state = A2DP_IDLE;
341                             signal_identifier = avdtp_subevent_signaling_general_reject_get_signal_identifier(packet);
342                             printf(" --- a2dp source ---  Rejected %d\n", signal_identifier);
343                             break;
344                         default:
345                             app_state = A2DP_IDLE;
346                             printf(" --- a2dp source ---  not implemented\n");
347                             break;
348                     }
349                     break;
350                 default:
351                     break;
352             }
353             break;
354         default:
355             // other packet type
356             break;
357     }
358 }
359 void a2dp_source_register_packet_handler(btstack_packet_handler_t callback){
360     if (callback == NULL){
361         log_error("a2dp_source_register_packet_handler called with NULL callback");
362         return;
363     }
364     avdtp_source_register_packet_handler(&packet_handler);
365     a2dp_source_context.a2dp_callback = callback;
366 }
367 
368 void a2dp_source_init(void){
369     avdtp_source_init(&a2dp_source_context);
370     l2cap_register_service(&packet_handler, BLUETOOTH_PROTOCOL_AVDTP, 0xffff, LEVEL_0);
371 }
372 
373 uint8_t a2dp_source_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
374     uint8_t * codec_capabilities, uint16_t codec_capabilities_len,
375     uint8_t * media_codec_info, uint16_t media_codec_info_len){
376     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_source_create_stream_endpoint(AVDTP_SOURCE, media_type);
377     avdtp_source_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
378     avdtp_source_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
379         codec_capabilities, codec_capabilities_len);
380     local_stream_endpoint->remote_configuration.media_codec.media_codec_information     = media_codec_info;
381     local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len;
382 
383     return local_stream_endpoint->sep.seid;
384 }
385 
386 void a2dp_source_connect(bd_addr_t bd_addr, uint8_t local_seid){
387     sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context);
388     if (!sc.local_stream_endpoint){
389         printf(" no local_stream_endpoint for seid %d\n", local_seid);
390     }
391     avdtp_source_connect(bd_addr);
392 
393 }
394 
395 void a2dp_source_disconnect(uint16_t con_handle){
396     avdtp_disconnect(con_handle, &a2dp_source_context);
397 }
398 
399