xref: /btstack/src/classic/a2dp_sink.c (revision cfd54eb73cd29e7bf738f261fb454a84a1bb66b0)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "a2dp_sink.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 
43 #include "bluetooth_psm.h"
44 #include "bluetooth_sdp.h"
45 #include "btstack_debug.h"
46 #include "btstack_event.h"
47 #include "classic/a2dp_sink.h"
48 #include "classic/avdtp_sink.h"
49 #include "classic/avdtp_util.h"
50 #include "classic/sdp_util.h"
51 
52 static const char * default_a2dp_sink_service_name = "BTstack A2DP Sink Service";
53 static const char * default_a2dp_sink_service_provider_name = "BTstack A2DP Sink Service Provider";
54 
55 static bool outgoing_active = false;
56 static uint16_t a2dp_sink_cid;
57 static bool stream_endpoint_configured = false;
58 
59 static avdtp_stream_endpoint_context_t sc;
60 
61 static btstack_packet_handler_t a2dp_sink_packet_handler_user;
62 
63 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
64 
65 void a2dp_sink_create_sdp_record(uint8_t * service,  uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){
66     uint8_t* attribute;
67     de_create_sequence(service);
68 
69     // 0x0000 "Service Record Handle"
70     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
71     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
72 
73     // 0x0001 "Service Class ID List"
74     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
75     attribute = de_push_sequence(service);
76     {
77         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK);
78     }
79     de_pop_sequence(service, attribute);
80 
81     // 0x0004 "Protocol Descriptor List"
82     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
83     attribute = de_push_sequence(service);
84     {
85         uint8_t* l2cpProtocol = de_push_sequence(attribute);
86         {
87             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
88             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVDTP);
89         }
90         de_pop_sequence(attribute, l2cpProtocol);
91 
92         uint8_t* avProtocol = de_push_sequence(attribute);
93         {
94             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
95             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
96         }
97         de_pop_sequence(attribute, avProtocol);
98     }
99     de_pop_sequence(service, attribute);
100 
101     // 0x0005 "Public Browse Group"
102     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
103     attribute = de_push_sequence(service);
104     {
105         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
106     }
107     de_pop_sequence(service, attribute);
108 
109     // 0x0009 "Bluetooth Profile Descriptor List"
110     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
111     attribute = de_push_sequence(service);
112     {
113         uint8_t *a2dProfile = de_push_sequence(attribute);
114         {
115             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
116             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
117         }
118         de_pop_sequence(attribute, a2dProfile);
119     }
120     de_pop_sequence(service, attribute);
121 
122 
123     // 0x0100 "Service Name"
124     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
125     if (service_name){
126         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
127     } else {
128         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_name), (uint8_t *) default_a2dp_sink_service_name);
129     }
130 
131     // 0x0100 "Provider Name"
132     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
133     if (service_provider_name){
134         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
135     } else {
136         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_provider_name), (uint8_t *) default_a2dp_sink_service_provider_name);
137     }
138 
139     // 0x0311 "Supported Features"
140     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
141     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
142 }
143 
144 void a2dp_sink_register_packet_handler(btstack_packet_handler_t callback){
145     // avdtp_sink_register_packet_handler(callback);
146     // return;
147     if (callback == NULL){
148         log_error("a2dp_sink_register_packet_handler called with NULL callback");
149         return;
150     }
151     avdtp_sink_register_packet_handler(&a2dp_sink_packet_handler_internal);
152     a2dp_sink_packet_handler_user = callback;
153 }
154 
155 void a2dp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){
156     if (callback == NULL){
157         log_error("a2dp_sink_register_media_handler called with NULL callback");
158         return;
159     }
160     avdtp_sink_register_media_handler(callback);
161 }
162 
163 void a2dp_sink_init(void){
164     avdtp_sink_init();
165 }
166 
167 avdtp_stream_endpoint_t * a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
168     uint8_t * codec_capabilities, uint16_t codec_capabilities_len,
169     uint8_t * media_codec_info, uint16_t media_codec_info_len){
170     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, media_type);
171     if (!local_stream_endpoint){
172         return NULL;
173     }
174     avdtp_sink_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
175     avdtp_sink_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
176         codec_capabilities, codec_capabilities_len);
177     local_stream_endpoint->remote_configuration.media_codec.media_codec_information     = media_codec_info;
178     local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len;
179     avdtp_sink_register_delay_reporting_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
180     return local_stream_endpoint;
181 }
182 
183 void a2dp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
184     avdtp_sink_finalize_stream_endpoint(stream_endpoint);
185 }
186 
187 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){
188     sc.local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
189     if (!sc.local_stream_endpoint){
190         log_info("No local_stream_endpoint for seid %d", local_seid);
191         return ERROR_CODE_COMMAND_DISALLOWED;
192     }
193     // remember to tell client
194     outgoing_active = true;
195     return avdtp_sink_connect(bd_addr, avdtp_cid);
196 }
197 
198 void a2dp_sink_disconnect(uint16_t a2dp_cid){
199     avdtp_disconnect(a2dp_cid);
200 }
201 
202 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
203     UNUSED(channel);
204     UNUSED(size);
205     bd_addr_t address;
206     uint16_t cid;
207     uint8_t status;
208 
209     uint8_t local_seid;
210     uint8_t remote_seid;
211     uint8_t signal_identifier;
212 
213     if (packet_type != HCI_EVENT_PACKET) return;
214     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
215 
216     switch (packet[2]){
217         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
218             if (stream_endpoint_configured) return;
219 
220             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
221 
222             avdtp_subevent_signaling_connection_established_get_bd_addr(packet, address);
223             status = avdtp_subevent_signaling_connection_established_get_status(packet);
224             if (status != ERROR_CODE_SUCCESS){
225                 // only care for outgoing connections
226                 if (!outgoing_active) break;
227                 outgoing_active = false;
228                 a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
229                 log_info("A2DP sink signaling connection failed status %d", status);
230                 break;
231             }
232 
233             a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
234             log_info("A2DP sink signaling connection established avdtp_cid 0x%02x", cid);
235             break;
236 
237         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
238             if (stream_endpoint_configured) break;
239             stream_endpoint_configured = true;
240             a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
241             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION);
242             break;
243 
244         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
245             if (stream_endpoint_configured) break;
246             stream_endpoint_configured = true;
247             a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
248             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION);
249             break;
250 
251         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
252             if (stream_endpoint_configured == false) break;
253             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
254             if (cid != a2dp_sink_cid) break;
255 
256             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
257 
258             avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address);
259             local_seid = avdtp_subevent_streaming_connection_established_get_local_seid(packet);
260             remote_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet);
261 
262             // about to notify client
263             outgoing_active = false;
264             status = avdtp_subevent_streaming_connection_established_get_status(packet);
265             if (status != ERROR_CODE_SUCCESS){
266                 log_info("A2DP sink streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
267                 a2dp_emit_streaming_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
268                 break;
269             }
270 
271             log_info("A2DP streaming connection established --- avdtp_cid 0x%02x, local seid %d, remote seid %d", cid, local_seid, remote_seid);
272             a2dp_emit_streaming_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
273             break;
274 
275         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
276             if (stream_endpoint_configured == false) break;
277             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
278             if (cid != a2dp_sink_cid) break;
279 
280             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
281             local_seid = avdtp_subevent_signaling_accept_get_local_seid(packet);
282 
283             switch (signal_identifier){
284                 case  AVDTP_SI_START:
285                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_STARTED);
286                     break;
287                 case AVDTP_SI_SUSPEND:
288                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_SUSPENDED);
289                     break;
290                 case AVDTP_SI_ABORT:
291                 case AVDTP_SI_CLOSE:
292                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_STOPPED);
293                     break;
294                 default:
295                     break;
296             }
297             break;
298 
299         case AVDTP_SUBEVENT_SIGNALING_REJECT:
300             if (stream_endpoint_configured == false) break;
301             cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet);
302             if (cid != a2dp_sink_cid) break;
303 
304             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
305             break;
306 
307         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
308             if (stream_endpoint_configured == false) break;
309             cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet);
310             if (cid != a2dp_sink_cid) break;
311 
312             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
313             break;
314 
315         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
316             if (stream_endpoint_configured == false) break;
317             cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet);
318             if (cid != a2dp_sink_cid) break;
319 
320             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_RELEASED);
321             break;
322 
323         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
324             cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet);
325             if (cid != a2dp_sink_cid) break;
326 
327             stream_endpoint_configured = false;
328             // for outgoing connections, suppress release event and report stream established failed
329             if (outgoing_active){
330                 outgoing_active = false;
331                 log_info("A2DP sink outgoing connection failed - disconnect");
332                 a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION);
333                 break;
334             }
335 
336             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED);
337             break;
338         default:
339             break;
340     }
341 
342 }
343 
344