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 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 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 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 89 void a2dp_sink_init(void){ 90 a2dp_init(); 91 avdtp_sink_init(); 92 } 93 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 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 122 void a2dp_sink_finalize_stream_endpoint(avdtp_stream_endpoint_t * stream_endpoint){ 123 avdtp_sink_finalize_stream_endpoint(stream_endpoint); 124 } 125 126 uint8_t a2dp_sink_establish_stream(bd_addr_t bd_addr, uint8_t local_seid, uint16_t * avdtp_cid){ 127 avdtp_stream_endpoint_t * stream_endpoint = avdtp_get_stream_endpoint_for_seid(local_seid); 128 if (stream_endpoint == NULL){ 129 log_info("No local_stream_endpoint for seid %d", local_seid); 130 return ERROR_CODE_COMMAND_DISALLOWED; 131 } 132 uint16_t outgoing_cid; 133 134 uint8_t status = avdtp_sink_connect(bd_addr, &outgoing_cid); 135 if (status != ERROR_CODE_SUCCESS){ 136 return status; 137 } 138 139 avdtp_connection_t * connection = avdtp_get_connection_for_avdtp_cid(outgoing_cid); 140 btstack_assert(connection != NULL); 141 142 // setup state 143 connection->a2dp_sink_config_process.outgoing_active = true; 144 *avdtp_cid = outgoing_cid; 145 146 return ERROR_CODE_SUCCESS; 147 } 148 149 #ifdef ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION 150 uint8_t a2dp_sink_start_stream_accept(uint16_t a2dp_cid, uint8_t local_seid){ 151 return avdtp_start_stream_accept(a2dp_cid, local_seid); 152 } 153 154 uint8_t a2dp_sink_start_stream_reject(uint16_t a2dp_cid, uint8_t local_seid){ 155 return avdtp_start_stream_reject(a2dp_cid, local_seid); 156 } 157 #endif 158 159 void a2dp_sink_disconnect(uint16_t a2dp_cid){ 160 avdtp_disconnect(a2dp_cid); 161 } 162 163 static void a2dp_sink_packet_handler_internal(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 164 UNUSED(channel); 165 UNUSED(size); 166 167 if (packet_type != HCI_EVENT_PACKET) return; 168 if (hci_event_packet_get_type(packet) != HCI_EVENT_AVDTP_META) return; 169 170 a2dp_config_process_avdtp_event_handler(AVDTP_ROLE_SINK, packet, size); 171 } 172 173 static uint8_t a2dp_sink_media_config_validator_callback(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size){ 174 uint8_t error = 0; 175 if (a2dp_sink_media_config_validator != NULL) { 176 // update subevent id and call validator 177 uint8_t avdtp_subevent_id = event[2]; 178 uint8_t a2dp_subevent_id = a2dp_subevent_id_for_avdtp_subevent_id(avdtp_subevent_id); 179 uint8_t * subevent_field = (uint8_t *) &event[2]; 180 *subevent_field = a2dp_subevent_id; 181 error = (*a2dp_sink_media_config_validator)(stream_endpoint, event, size); 182 *subevent_field = avdtp_subevent_id; 183 } 184 return error; 185 } 186 187 void a2dp_sink_register_media_config_validator(uint8_t (*callback)(const avdtp_stream_endpoint_t * stream_endpoint, const uint8_t * event, uint16_t size)){ 188 a2dp_sink_media_config_validator = callback; 189 avdtp_sink_register_media_config_validator(&a2dp_sink_media_config_validator_callback); 190 } 191 192 193 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){ 194 return a2dp_config_process_set_sbc(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration); 195 } 196 197 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){ 198 return a2dp_config_process_set_mpeg_audio(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration); 199 } 200 201 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){ 202 return a2dp_config_process_set_mpeg_aac(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration); 203 } 204 205 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){ 206 return a2dp_config_process_set_atrac(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, configuration); 207 } 208 209 uint8_t a2dp_sink_set_config_other(uint16_t a2dp_cid, uint8_t local_seid, uint8_t remote_seid, 210 const uint8_t * media_codec_information, uint8_t media_codec_information_len){ 211 return a2dp_config_process_set_other(AVDTP_ROLE_SINK, a2dp_cid, local_seid, remote_seid, media_codec_information, media_codec_information_len); 212 } 213