xref: /btstack/src/classic/a2dp_sink.c (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
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 <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "classic/avdtp_util.h"
47 #include "classic/avdtp_sink.h"
48 #include "classic/a2dp_sink.h"
49 
50 static const char * default_a2dp_sink_service_name = "BTstack A2DP Sink Service";
51 static const char * default_a2dp_sink_service_provider_name = "BTstack A2DP Sink Service Provider";
52 
53 static avdtp_context_t a2dp_sink_context;
54 
55 static a2dp_state_t app_state = A2DP_IDLE;
56 static int send_stream_established_for_outgoing_connection;
57 
58 static avdtp_stream_endpoint_context_t sc;
59 
60 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
61 
62 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){
63     uint8_t* attribute;
64     de_create_sequence(service);
65 
66     // 0x0000 "Service Record Handle"
67     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
68     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
69 
70     // 0x0001 "Service Class ID List"
71     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
72     attribute = de_push_sequence(service);
73     {
74         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK);
75     }
76     de_pop_sequence(service, attribute);
77 
78     // 0x0004 "Protocol Descriptor List"
79     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
80     attribute = de_push_sequence(service);
81     {
82         uint8_t* l2cpProtocol = de_push_sequence(attribute);
83         {
84             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
85             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);
86         }
87         de_pop_sequence(attribute, l2cpProtocol);
88 
89         uint8_t* avProtocol = de_push_sequence(attribute);
90         {
91             de_add_number(avProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVDTP);  // avProtocol_service
92             de_add_number(avProtocol,  DE_UINT, DE_SIZE_16,  0x0103);  // version
93         }
94         de_pop_sequence(attribute, avProtocol);
95     }
96     de_pop_sequence(service, attribute);
97 
98     // 0x0005 "Public Browse Group"
99     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
100     attribute = de_push_sequence(service);
101     {
102         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
103     }
104     de_pop_sequence(service, attribute);
105 
106     // 0x0009 "Bluetooth Profile Descriptor List"
107     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
108     attribute = de_push_sequence(service);
109     {
110         uint8_t *a2dProfile = de_push_sequence(attribute);
111         {
112             de_add_number(a2dProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION);
113             de_add_number(a2dProfile,  DE_UINT, DE_SIZE_16, 0x0103);
114         }
115         de_pop_sequence(attribute, a2dProfile);
116     }
117     de_pop_sequence(service, attribute);
118 
119 
120     // 0x0100 "Service Name"
121     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
122     if (service_name){
123         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
124     } else {
125         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_name), (uint8_t *) default_a2dp_sink_service_name);
126     }
127 
128     // 0x0100 "Provider Name"
129     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
130     if (service_provider_name){
131         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
132     } else {
133         de_add_data(service,  DE_STRING, strlen(default_a2dp_sink_service_provider_name), (uint8_t *) default_a2dp_sink_service_provider_name);
134     }
135 
136     // 0x0311 "Supported Features"
137     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
138     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
139 }
140 
141 void a2dp_sink_register_packet_handler(btstack_packet_handler_t callback){
142     // avdtp_sink_register_packet_handler(callback);
143     // return;
144     if (callback == NULL){
145         log_error("a2dp_sink_register_packet_handler called with NULL callback");
146         return;
147     }
148     avdtp_sink_register_packet_handler(&packet_handler);
149     a2dp_sink_context.a2dp_callback = callback;
150 }
151 
152 void a2dp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){
153     if (callback == NULL){
154         log_error("a2dp_sink_register_media_handler called with NULL callback");
155         return;
156     }
157     avdtp_sink_register_media_handler(callback);
158 }
159 
160 void a2dp_sink_init(void){
161     avdtp_sink_init(&a2dp_sink_context);
162 }
163 
164 uint8_t a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
165     uint8_t * codec_capabilities, uint16_t codec_capabilities_len,
166     uint8_t * media_codec_info, uint16_t media_codec_info_len, uint8_t * local_seid){
167     *local_seid = 0;
168     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, media_type);
169     if (!local_stream_endpoint){
170         return BTSTACK_MEMORY_ALLOC_FAILED;
171     }
172     *local_seid = avdtp_local_seid(local_stream_endpoint);
173     avdtp_sink_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
174     avdtp_sink_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
175         codec_capabilities, codec_capabilities_len);
176     local_stream_endpoint->remote_configuration.media_codec.media_codec_information     = media_codec_info;
177     local_stream_endpoint->remote_configuration.media_codec.media_codec_information_len = media_codec_info_len;
178     return ERROR_CODE_SUCCESS;
179 }
180 
181 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){
182     sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_sink_context);
183     if (!sc.local_stream_endpoint){
184         log_info("No local_stream_endpoint for seid %d", local_seid);
185         return AVDTP_SEID_DOES_NOT_EXIST;
186     }
187     // remember to tell client
188     send_stream_established_for_outgoing_connection = 1;
189     return avdtp_sink_connect(bd_addr, avdtp_cid);
190 }
191 
192 void a2dp_sink_disconnect(uint16_t a2dp_cid){
193     avdtp_disconnect(a2dp_cid, &a2dp_sink_context);
194 }
195 
196 static inline void a2dp_signaling_emit_media_codec_sbc(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
197     if (!callback) return;
198     if (event_size < 18) return;
199     event[0] = HCI_EVENT_A2DP_META;
200     event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION;
201     (*callback)(HCI_EVENT_PACKET, 0, event, event_size);
202 }
203 
204 static inline void avdtp_signaling_emit_media_codec_other(btstack_packet_handler_t callback, uint8_t * event, uint16_t event_size){
205     if (!callback) return;
206     if (event_size < 113) return;
207     event[0] = HCI_EVENT_A2DP_META;
208     event[2] = A2DP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION;
209 }
210 
211 static inline void a2dp_emit_stream_event(btstack_packet_handler_t callback, uint16_t a2dp_cid, uint8_t eventID, uint8_t local_seid){
212     if (!callback) return;
213     uint8_t event[6];
214     int pos = 0;
215     event[pos++] = HCI_EVENT_A2DP_META;
216     event[pos++] = sizeof(event) - 2;
217     event[pos++] = eventID;
218     little_endian_store_16(event, pos, a2dp_cid);
219     pos += 2;
220     event[pos++] = local_seid;
221     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
222 }
223 
224 static inline void a2dp_emit_signaling_connection_released(btstack_packet_handler_t callback, uint16_t a2dp_cid){
225     if (!callback) return;
226     uint8_t event[5];
227     int pos = 0;
228     event[pos++] = HCI_EVENT_A2DP_META;
229     event[pos++] = sizeof(event) - 2;
230     event[pos++] = A2DP_SUBEVENT_SIGNALING_CONNECTION_RELEASED;
231     little_endian_store_16(event, pos, a2dp_cid);
232     pos += 2;
233     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
234 }
235 
236 // static inline void a2dp_emit_cmd_accepted(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
237 //     if (!callback) return;
238 //     UNUSED(size);
239 //     packet[0] = HCI_EVENT_A2DP_META;
240 //     packet[2] = A2DP_SUBEVENT_COMMAND_ACCEPTED;
241 //     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
242 // }
243 
244 static inline void a2dp_emit_cmd_rejected(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
245     if (!callback) return;
246     UNUSED(size);
247     packet[0] = HCI_EVENT_A2DP_META;
248     packet[2] = A2DP_SUBEVENT_COMMAND_REJECTED;
249     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
250 }
251 
252 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
253     UNUSED(channel);
254     UNUSED(size);
255     bd_addr_t address;
256     uint8_t status;
257     uint8_t  signal_identifier;
258     uint16_t cid;
259     uint8_t loc_seid;
260     uint8_t rem_seid;
261 
262     if (packet_type != HCI_EVENT_PACKET) return;
263     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
264 
265     switch (packet[2]){
266         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED:
267             avdtp_subevent_signaling_connection_established_get_bd_addr(packet, address);
268             cid = avdtp_subevent_signaling_connection_established_get_avdtp_cid(packet);
269             status = avdtp_subevent_signaling_connection_established_get_status(packet);
270             if (status != 0){
271                 app_state = A2DP_IDLE;
272 
273                 // only care for outgoing connections
274                 if (send_stream_established_for_outgoing_connection == 0) break;
275                 send_stream_established_for_outgoing_connection = 0;
276 
277                 log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION failed status %d ---", status);
278                 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, 0, 0, status);
279                 break;
280             }
281             app_state = A2DP_CONNECTED;
282             log_info("AVDTP_SUBEVENT_SIGNALING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x ---", cid);
283             break;
284 
285         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_OTHER_CONFIGURATION:
286             log_info("received non SBC codec. not implemented");
287             avdtp_signaling_emit_media_codec_other(a2dp_sink_context.a2dp_callback, packet, size);
288             break;
289 
290         case AVDTP_SUBEVENT_SIGNALING_MEDIA_CODEC_SBC_CONFIGURATION:
291             if (app_state < A2DP_CONNECTED) return;
292             a2dp_signaling_emit_media_codec_sbc(a2dp_sink_context.a2dp_callback, packet, size);
293             break;
294 
295         case AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED:
296             avdtp_subevent_streaming_connection_established_get_bd_addr(packet, address);
297             status = avdtp_subevent_streaming_connection_established_get_status(packet);
298             cid = avdtp_subevent_streaming_connection_established_get_avdtp_cid(packet);
299             loc_seid = avdtp_subevent_streaming_connection_established_get_local_seid(packet);
300             rem_seid = avdtp_subevent_streaming_connection_established_get_remote_seid(packet);
301             log_info("AVDTP_SUBEVENT_STREAMING_CONNECTION_ESTABLISHED, avdtp cid 0x%02x, status %d ---", cid, status);
302 
303             // about to notify client
304             send_stream_established_for_outgoing_connection = 0;
305 
306             if (status){
307                 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, loc_seid, rem_seid, status);
308                 break;
309             }
310             app_state = A2DP_STREAMING_OPENED;
311             a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, loc_seid, rem_seid, 0);
312             break;
313 
314         case AVDTP_SUBEVENT_SIGNALING_ACCEPT:
315             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
316             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
317             loc_seid = avdtp_subevent_signaling_accept_get_local_seid(packet);
318 
319             switch (signal_identifier){
320                 case  AVDTP_SI_START:
321                     a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_STARTED, loc_seid);
322                     break;
323                 case AVDTP_SI_SUSPEND:
324                     a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_SUSPENDED, loc_seid);
325                     break;
326                 case AVDTP_SI_ABORT:
327                 case AVDTP_SI_CLOSE:
328                     a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_STOPPED, loc_seid);
329                     break;
330                 default:
331                     // a2dp_emit_cmd_accepted(a2dp_sink_context.a2dp_callback, packet, size);
332                     break;
333             }
334             break;
335 
336         case AVDTP_SUBEVENT_SIGNALING_REJECT:
337         case AVDTP_SUBEVENT_SIGNALING_GENERAL_REJECT:
338             signal_identifier = avdtp_subevent_signaling_accept_get_signal_identifier(packet);
339             cid = avdtp_subevent_signaling_accept_get_avdtp_cid(packet);
340             loc_seid = avdtp_subevent_signaling_accept_get_local_seid(packet);
341             a2dp_emit_cmd_rejected(a2dp_sink_context.a2dp_callback, packet, size);
342             app_state = A2DP_IDLE;
343             break;
344         case AVDTP_SUBEVENT_STREAMING_CONNECTION_RELEASED:
345             cid = avdtp_subevent_streaming_connection_released_get_avdtp_cid(packet);
346             loc_seid = avdtp_subevent_streaming_connection_released_get_local_seid(packet);
347             app_state = A2DP_IDLE;
348             a2dp_emit_stream_event(a2dp_sink_context.a2dp_callback, cid, A2DP_SUBEVENT_STREAM_RELEASED, loc_seid);
349             break;
350         case AVDTP_SUBEVENT_SIGNALING_CONNECTION_RELEASED:
351             cid = avdtp_subevent_signaling_connection_released_get_avdtp_cid(packet);
352 
353             // for outgoing connections, suppress release event and report stream established failed
354             if (send_stream_established_for_outgoing_connection){
355                 send_stream_established_for_outgoing_connection = 0;
356                 log_info("A2DP sink outgoing connnection failed - disconnect");
357                 a2dp_streaming_emit_connection_established(a2dp_sink_context.a2dp_callback, cid, address, 0, 0, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION);
358                 break;
359             }
360 
361             a2dp_emit_signaling_connection_released(a2dp_sink_context.a2dp_callback, cid);
362             app_state = A2DP_IDLE;
363             break;
364         default:
365             app_state = A2DP_IDLE;
366             log_info("not implemented");
367             break;
368     }
369 
370 }
371 
372