xref: /btstack/src/classic/a2dp_sink.c (revision d3f12e45554c72052e9608afce24dc5314ceb0fb)
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 BLUEKITCHEN
24  * GMBH 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.h"
48 #include "classic/a2dp_sink.h"
49 #include "classic/avdtp_sink.h"
50 #include "classic/avdtp_util.h"
51 #include "classic/sdp_util.h"
52 
53 static const char * a2dp_sink_default_service_name = "BTstack A2DP Sink Service";
54 static const char * a2dp_sink_default_service_provider_name = "BTstack A2DP Sink Service Provider";
55 
56 static uint16_t a2dp_sink_cid;
57 static bool a2dp_sink_stream_endpoint_configured = false;
58 
59 static uint8_t (*a2dp_sink_media_config_validator)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size);
60 
61 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
62 
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 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){
64     if (service_provider_name == NULL){
65         service_provider_name = a2dp_sink_default_service_provider_name;
66     }
67     if (service_name == NULL){
68         service_name = a2dp_sink_default_service_name;
69     }
70     a2dp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_AUDIO_SINK,
71                            supported_features, service_name, service_provider_name);
72 }
73 
a2dp_sink_register_packet_handler(btstack_packet_handler_t callback)74 void a2dp_sink_register_packet_handler(btstack_packet_handler_t callback){
75     btstack_assert(callback);
76 
77     avdtp_sink_register_packet_handler(&a2dp_sink_packet_handler_internal);
78     a2dp_register_sink_packet_handler(callback);
79 }
80 
a2dp_sink_register_media_handler(void (* callback)(uint8_t local_seid,uint8_t * packet,uint16_t size))81 void a2dp_sink_register_media_handler(void (*callback)(uint8_t local_seid, uint8_t *packet, uint16_t size)){
82     if (callback == NULL){
83         log_error("a2dp_sink_register_media_handler called with NULL callback");
84         return;
85     }
86     avdtp_sink_register_media_handler(callback);
87 }
88 
a2dp_sink_init(void)89 void a2dp_sink_init(void){
90     a2dp_init();
91     avdtp_sink_init();
92 }
93 
a2dp_sink_deinit(void)94 void a2dp_sink_deinit(void){
95     a2dp_deinit();
96     avdtp_sink_deinit();
97 
98     a2dp_sink_cid = 0;
99     a2dp_sink_media_config_validator = NULL;
100     a2dp_sink_stream_endpoint_configured = false;
101 }
102 
a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type,avdtp_media_codec_type_t media_codec_type,const uint8_t * codec_capabilities,uint16_t codec_capabilities_len,uint8_t * codec_configuration,uint16_t codec_configuration_len)103 avdtp_stream_endpoint_t * a2dp_sink_create_stream_endpoint(avdtp_media_type_t media_type, avdtp_media_codec_type_t media_codec_type,
104                                                            const uint8_t *codec_capabilities, uint16_t codec_capabilities_len,
105                                                            uint8_t * codec_configuration, uint16_t codec_configuration_len){
106     avdtp_stream_endpoint_t * local_stream_endpoint = avdtp_sink_create_stream_endpoint(AVDTP_SINK, media_type);
107     if (!local_stream_endpoint){
108         return NULL;
109     }
110     avdtp_sink_register_media_transport_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
111     avdtp_sink_register_media_codec_category(avdtp_stream_endpoint_seid(local_stream_endpoint), media_type, media_codec_type,
112         codec_capabilities, codec_capabilities_len);
113 	avdtp_sink_register_delay_reporting_category(avdtp_stream_endpoint_seid(local_stream_endpoint));
114 
115 	// store user codec configuration buffer
116 	local_stream_endpoint->media_codec_configuration_info = codec_configuration;
117 	local_stream_endpoint->media_codec_configuration_len  = codec_configuration_len;
118 
119     return local_stream_endpoint;
120 }
121 
a2dp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint)122 void a2dp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){
123     avdtp_sink_finalize_stream_endpoint(stream_endpoint);
124 }
125 
a2dp_sink_establish_stream(bd_addr_t bd_addr,uint16_t * avdtp_cid)126 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint16_t * avdtp_cid){
127     uint16_t outgoing_cid;
128     uint8_t status = avdtp_sink_connect(bd_addr, &outgoing_cid);
129     if (status != ERROR_CODE_SUCCESS){
130         return status;
131     }
132 
133     avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(outgoing_cid);
134     btstack_assert(connection != NULL);
135 
136     // setup state
137     connection->a2dp_sink_config_process.outgoing_active = true;
138     *avdtp_cid = outgoing_cid;
139 
140     return ERROR_CODE_SUCCESS;
141 }
142 
143 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION
a2dp_sink_start_stream_accept(uint16_t a2dp_cid,uint8_t local_seid)144 uint8_t a2dp_sink_start_stream_accept(uint16_t a2dp_cid, uint8_t local_seid){
145     return avdtp_start_stream_accept(a2dp_cid, local_seid);
146 }
147 
a2dp_sink_start_stream_reject(uint16_t a2dp_cid,uint8_t local_seid)148 uint8_t a2dp_sink_start_stream_reject(uint16_t a2dp_cid, uint8_t local_seid){
149     return avdtp_start_stream_reject(a2dp_cid, local_seid);
150 }
151 #endif
152 
a2dp_sink_disconnect(uint16_t a2dp_cid)153 void a2dp_sink_disconnect(uint16_t a2dp_cid){
154     avdtp_disconnect(a2dp_cid);
155 }
156 
a2dp_sink_packet_handler_internal(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)157 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
158     UNUSED(channel);
159     UNUSED(size);
160 
161     if (packet_type != HCI_EVENT_PACKET) return;
162     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return;
163 
164     a2dp_config_process_avdtp_event_handler(AVDTP_ROLE_SINK, packet, size);
165 }
166 
a2dp_sink_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint,const uint8_t * event,uint16_t size)167 static uint8_t a2dp_sink_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size){
168     uint8_t error = 0;
169     if (a2dp_sink_media_config_validator != NULL) {
170         // update subevent id and call validator
171         uint8_t avdtp_subevent_id = event[2];
172         uint8_t a2dp_subevent_id = a2dp_subevent_id_for_avdtp_subevent_id(avdtp_subevent_id);
173         uint8_t * subevent_field = (uint8_t *) &event[2];
174         *subevent_field = a2dp_subevent_id;
175         error = (*a2dp_sink_media_config_validator)(stream_endpoint, event, size);
176         *subevent_field = avdtp_subevent_id;
177     }
178     return error;
179 }
180 
a2dp_sink_register_media_config_validator(uint8_t (* callback)(const avdtp_stream_endpoint_t * stream_endpoint,const uint8_t * event,uint16_t size))181 void a2dp_sink_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size)){
182     a2dp_sink_media_config_validator = callback;
183     avdtp_sink_register_media_config_validator(&a2dp_sink_media_config_validator_callback);
184 }
185 
186 
a2dp_sink_set_config_sbc(uint16_t a2dp_cid,uint8_t local_seid,uint8_t remote_seid,const avdtp_configuration_sbc_t * configuration)187 uint8_t a2dp_sink_set_config_sbc(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_sbc_t * configuration){
188     return a2dp_config_process_set_sbc(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration);
189 }
190 
a2dp_sink_set_config_mpeg_audio(uint16_t a2dp_cid,uint8_t local_seid,uint8_t remote_seid,const avdtp_configuration_mpeg_audio_t * configuration)191 uint8_t a2dp_sink_set_config_mpeg_audio(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_mpeg_audio_t * configuration){
192     return a2dp_config_process_set_mpeg_audio(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration);
193 }
194 
a2dp_sink_set_config_mpeg_aac(uint16_t a2dp_cid,uint8_t local_seid,uint8_t remote_seid,const avdtp_configuration_mpeg_aac_t * configuration)195 uint8_t a2dp_sink_set_config_mpeg_aac(uint16_t a2dp_cid,  uint8_t local_seid,  uint8_t remote_seid, const avdtp_configuration_mpeg_aac_t * configuration){
196     return a2dp_config_process_set_mpeg_aac(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration);
197 }
198 
a2dp_sink_set_config_atrac(uint16_t a2dp_cid,uint8_t local_seid,uint8_t remote_seid,const avdtp_configuration_atrac_t * configuration)199 uint8_t a2dp_sink_set_config_atrac(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, const avdtp_configuration_atrac_t * configuration){
200     return a2dp_config_process_set_atrac(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration);
201 }
202 
a2dp_sink_set_config_other(uint16_t a2dp_cid,uint8_t local_seid,uint8_t remote_seid,const uint8_t * media_codec_information,uint8_t media_codec_information_len)203 uint8_t a2dp_sink_set_config_other(uint16_t a2dp_cid,  uint8_t local_seid, uint8_t remote_seid,
204                                      const uint8_t * media_codec_information, uint8_t media_codec_information_len){
205     return a2dp_config_process_set_other(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, media_codec_information, media_codec_information_len);
206 }
207