xref: /btstack/src/classic/a2dp_sink.c (revision f400efd4c34c2c3d094122d9c99ddcc5357e2a2e)
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 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){
184     sc.local_stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid);
185     if (!sc.local_stream_endpoint){
186         log_info("No local_stream_endpoint for seid %d", local_seid);
187         return ERROR_CODE_COMMAND_DISALLOWED;
188     }
189     // remember to tell client
190     outgoing_active = true;
191     return avdtp_sink_connect(bd_addr, avdtp_cid);
192 }
193 
194 void a2dp_sink_disconnect(uint16_t a2dp_cid){
195     avdtp_disconnect(a2dp_cid);
196 }
197 
198 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
199     UNUSED(channel);
200     UNUSED(size);
201     bd_addr_t address;
202     uint16_t cid;
203     uint8_t status;
204 
205     uint8_t local_seid;
206     uint8_t remote_seid;
207     uint8_t signal_identifier;
208 
209     if (packet_type != HCI_EVENT_PACKET) return;
210     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
211 
212     switch (packet[2]){
213         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
214             if (stream_endpoint_configured) return;
215 
216             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
217 
218             avdtp_subevent_signaling_connection_established_get_bd_addr(packet, address);
219             status = avdtp_subevent_signaling_connection_established_get_status(packet);
220             if (status != ERROR_CODE_SUCCESS){
221                 // only care for outgoing connections
222                 if (!outgoing_active) break;
223                 outgoing_active = false;
224                 a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
225                 log_info("A2DP sink signaling connection failed status %d", status);
226                 break;
227             }
228 
229             a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
230             log_info("A2DP sink signaling connection established avdtp_cid 0x%02x", cid);
231             break;
232 
233         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
234             if (stream_endpoint_configured) break;
235             stream_endpoint_configured = true;
236             a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
237             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION);
238             break;
239 
240         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
241             if (stream_endpoint_configured) break;
242             stream_endpoint_configured = true;
243             a2dp_sink_cid = avdtp_subevent_signaling_media_codec_other_capability_get_avdtp_cid(packet);
244             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION);
245             break;
246 
247         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
248             if (stream_endpoint_configured == false) break;
249             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
250             if (cid != a2dp_sink_cid) break;
251 
252             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
253 
254             avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address);
255             local_seid = avdtp_subevent_streaming_connection_established_get_local_seid(packet);
256             remote_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet);
257 
258             // about to notify client
259             outgoing_active = false;
260             status = avdtp_subevent_streaming_connection_established_get_status(packet);
261             if (status != ERROR_CODE_SUCCESS){
262                 log_info("A2DP sink streaming connection could not be established, avdtp_cid 0x%02x, status 0x%02x ---", cid, status);
263                 a2dp_emit_streaming_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
264                 break;
265             }
266 
267             log_info("A2DP streaming connection established --- avdtp_cid 0x%02x, local seid %d, remote seid %d", cid, local_seid, remote_seid);
268             a2dp_emit_streaming_connection_established(a2dp_sink_packet_handler_user, packet, size, status);
269             break;
270 
271         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
272             if (stream_endpoint_configured == false) break;
273             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
274             if (cid != a2dp_sink_cid) break;
275 
276             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
277             local_seid = avdtp_subevent_signaling_accept_get_local_seid(packet);
278 
279             switch (signal_identifier){
280                 case  AVDTP_SI_START:
281                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_STARTED);
282                     break;
283                 case AVDTP_SI_SUSPEND:
284                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_SUSPENDED);
285                     break;
286                 case AVDTP_SI_ABORT:
287                 case AVDTP_SI_CLOSE:
288                     a2dp_emit_stream_event(a2dp_sink_packet_handler_user, cid, local_seid, A2DP_SUBEVENT_STREAM_STOPPED);
289                     break;
290                 default:
291                     break;
292             }
293             break;
294 
295         case AVDTP_SUBEVENT_SIGNALING_REJECT:
296             if (stream_endpoint_configured == false) break;
297             cid = avdtp_subevent_signaling_reject_get_avdtp_cid(packet);
298             if (cid != a2dp_sink_cid) break;
299 
300             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
301             break;
302 
303         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
304             if (stream_endpoint_configured == false) break;
305             cid = avdtp_subevent_signaling_general_reject_get_avdtp_cid(packet);
306             if (cid != a2dp_sink_cid) break;
307 
308             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_COMMAND_REJECTED);
309             break;
310 
311         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
312             if (stream_endpoint_configured == false) break;
313             cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet);
314             if (cid != a2dp_sink_cid) break;
315 
316             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_STREAM_RELEASED);
317             break;
318 
319         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
320             cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet);
321             if (cid != a2dp_sink_cid) break;
322 
323             stream_endpoint_configured = false;
324             // for outgoing connections, suppress release event and report stream established failed
325             if (outgoing_active){
326                 outgoing_active = false;
327                 log_info("A2DP sink outgoing connection failed - disconnect");
328                 a2dp_emit_signaling_connection_established(a2dp_sink_packet_handler_user, packet, size, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION);
329                 break;
330             }
331 
332             a2dp_replace_subevent_id_and_emit_cmd(a2dp_sink_packet_handler_user, packet, size, A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED);
333             break;
334         default:
335             break;
336     }
337 
338 }
339 
340