The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2004-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | /****************************************************************************** |
| 20 | * |
| 21 | * This is the advanced audio/video call-out function implementation for |
| 22 | * BTIF. |
| 23 | * |
| 24 | ******************************************************************************/ |
| 25 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 26 | #include <mutex> |
| 27 | |
| 28 | #include <base/bind.h> |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 29 | #include <base/logging.h> |
Pavlin Radoslavov | b18f03e | 2016-09-13 15:02:52 -0700 | [diff] [blame] | 30 | #include <string.h> |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 31 | |
| 32 | #include "bt_target.h" |
| 33 | |
Pavlin Radoslavov | f66f4e9 | 2016-10-14 15:14:37 -0700 | [diff] [blame] | 34 | #include "a2dp_api.h" |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 35 | #include "a2dp_sbc.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 36 | #include "bta_av_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 37 | #include "bta_av_ci.h" |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 38 | #include "bta_av_co.h" |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 39 | #include "bta_sys.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 40 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 41 | #include "btif_av.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 42 | #include "btif_av_co.h" |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 43 | #include "btif_util.h" |
Pavlin Radoslavov | c66edf3 | 2016-09-15 17:50:18 -0700 | [diff] [blame] | 44 | #include "osi/include/osi.h" |
Sunny Kapdi | d66d954 | 2018-02-12 21:52:50 -0800 | [diff] [blame] | 45 | #include "osi/include/properties.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 46 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 47 | // Macro to retrieve the number of elements in a statically allocated array |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 48 | #define BTA_AV_CO_NUM_ELEMENTS(__a) (sizeof(__a) / sizeof((__a)[0])) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 49 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 50 | // Macro to convert BTA AV audio handle to index and vice versa |
| 51 | #define BTA_AV_CO_AUDIO_HANDLE_TO_INDEX(bta_av_handle) \ |
| 52 | (((bta_av_handle) & (~BTA_AV_CHNL_MSK)) - 1) |
| 53 | #define BTA_AV_CO_AUDIO_INDEX_TO_HANDLE(index) \ |
| 54 | (((index) + 1) | BTA_AV_CHNL_AUDIO) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 55 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 56 | class BtaAvCoSep { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 57 | public: |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 58 | BtaAvCoSep() |
| 59 | : sep_info_idx(0), seid(0), codec_caps{}, num_protect(0), protect_info{} { |
| 60 | Reset(); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 61 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Reset the state. |
| 65 | */ |
| 66 | void Reset() { |
| 67 | sep_info_idx = 0; |
| 68 | seid = 0; |
| 69 | memset(codec_caps, 0, sizeof(codec_caps)); |
| 70 | num_protect = 0; |
| 71 | memset(protect_info, 0, sizeof(protect_info)); |
| 72 | } |
| 73 | |
| 74 | uint8_t sep_info_idx; // Local SEP index (in BTA tables) |
| 75 | uint8_t seid; // Peer SEP index (in peer tables) |
| 76 | uint8_t codec_caps[AVDT_CODEC_SIZE]; // Peer SEP codec capabilities |
| 77 | uint8_t num_protect; // Peer SEP number of CP elements |
| 78 | uint8_t protect_info[AVDT_CP_INFO_LEN]; // Peer SEP content protection info |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 79 | }; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 81 | class BtaAvCoPeer { |
| 82 | public: |
| 83 | BtaAvCoPeer() |
| 84 | : addr(RawAddress::kEmpty), |
| 85 | num_sinks(0), |
| 86 | num_sources(0), |
| 87 | num_seps(0), |
| 88 | num_rx_sinks(0), |
| 89 | num_rx_sources(0), |
| 90 | num_sup_sinks(0), |
| 91 | num_sup_sources(0), |
| 92 | p_sink(nullptr), |
| 93 | p_source(nullptr), |
| 94 | codec_config{}, |
| 95 | acceptor(false), |
| 96 | reconfig_needed(false), |
| 97 | opened(false), |
| 98 | mtu(0), |
| 99 | uuid_to_connect(0), |
| 100 | bta_av_handle_(0), |
| 101 | codecs_(nullptr), |
| 102 | content_protect_active_(false) { |
| 103 | Reset(0); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 104 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Initialize the state. |
| 108 | * |
| 109 | * @param codec_priorities the codec priorities to use for the initialization |
| 110 | */ |
| 111 | void Init(const std::vector<btav_a2dp_codec_config_t>& codec_priorities); |
| 112 | |
| 113 | /** |
| 114 | * Reset the state. |
| 115 | * |
| 116 | * @param bta_av_handle the BTA AV handle to use |
| 117 | */ |
| 118 | void Reset(tBTA_AV_HNDL bta_av_handle); |
| 119 | |
| 120 | /** |
| 121 | * Get the BTA AV handle. |
| 122 | * |
| 123 | * @return the BTA AV handle |
| 124 | */ |
| 125 | tBTA_AV_HNDL BtaAvHandle() const { return bta_av_handle_; } |
| 126 | |
| 127 | /** |
| 128 | * Get the A2DP codecs. |
| 129 | * |
| 130 | * @return the A2DP codecs |
| 131 | */ |
| 132 | A2dpCodecs* GetCodecs() { return codecs_; } |
| 133 | |
| 134 | bool ContentProtectActive() const { return content_protect_active_; } |
| 135 | void SetContentProtectActive(bool cp_active) { |
| 136 | content_protect_active_ = cp_active; |
| 137 | } |
| 138 | |
| 139 | RawAddress addr; // Peer address |
| 140 | BtaAvCoSep sinks[BTAV_A2DP_CODEC_INDEX_MAX]; // Supported sinks |
| 141 | BtaAvCoSep sources[BTAV_A2DP_CODEC_INDEX_MAX]; // Supported sources |
| 142 | uint8_t num_sinks; // Total number of sinks at peer |
| 143 | uint8_t num_sources; // Total number of sources at peer |
| 144 | uint8_t num_seps; // Total number of SEPs at peer |
| 145 | uint8_t num_rx_sinks; // Number of received sinks |
| 146 | uint8_t num_rx_sources; // Number of received sources |
| 147 | uint8_t num_sup_sinks; // Number of supported sinks |
| 148 | uint8_t num_sup_sources; // Number of supported sources |
| 149 | const BtaAvCoSep* p_sink; // Currently selected sink |
| 150 | const BtaAvCoSep* p_source; // Currently selected source |
| 151 | uint8_t codec_config[AVDT_CODEC_SIZE]; // Current codec configuration |
| 152 | bool acceptor; // True if acceptor |
| 153 | bool reconfig_needed; // True if reconfiguration is needed |
| 154 | bool opened; // True if opened |
| 155 | uint16_t mtu; // Maximum Transmit Unit size |
| 156 | uint16_t uuid_to_connect; // UUID of peer device |
| 157 | |
| 158 | private: |
| 159 | tBTA_AV_HNDL bta_av_handle_; // BTA AV handle to use |
| 160 | A2dpCodecs* codecs_; // Locally supported codecs |
| 161 | bool content_protect_active_; // True if Content Protect is active |
| 162 | }; |
| 163 | |
| 164 | class BtaAvCo { |
| 165 | public: |
| 166 | BtaAvCo(bool content_protect_enabled) |
| 167 | : active_peer_(nullptr), |
| 168 | codec_config_{}, |
| 169 | content_protect_enabled_(content_protect_enabled), |
| 170 | content_protect_flag_(0) { |
| 171 | Reset(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Initialize the state. |
| 176 | * |
| 177 | * @param codec_priorities the codec priorities to use for the initialization |
| 178 | */ |
| 179 | void Init(const std::vector<btav_a2dp_codec_config_t>& codec_priorities); |
| 180 | |
| 181 | /** |
Pavlin Radoslavov | 7240fe9 | 2018-09-11 16:47:47 -0700 | [diff] [blame] | 182 | * Checks whether a codec is supported. |
| 183 | * |
| 184 | * @param codec_index the index of the codec to check |
| 185 | * @return true if the codec is supported, otherwise false |
| 186 | */ |
| 187 | bool IsSupportedCodec(btav_a2dp_codec_index_t codec_index); |
| 188 | |
| 189 | /** |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 190 | * Get the current codec configuration for the active peer. |
| 191 | * |
| 192 | * @return the current codec configuration if found, otherwise nullptr |
| 193 | */ |
| 194 | A2dpCodecConfig* GetActivePeerCurrentCodec(); |
| 195 | |
| 196 | /** |
Pavlin Radoslavov | 42840e0 | 2018-02-28 23:34:28 -0800 | [diff] [blame] | 197 | * Get the current codec configuration for a peer. |
| 198 | * |
| 199 | * @param peer_address the peer address |
| 200 | * @return the current codec configuration if found, otherwise nullptr |
| 201 | */ |
| 202 | A2dpCodecConfig* GetPeerCurrentCodec(const RawAddress& peer_address); |
| 203 | |
| 204 | /** |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 205 | * Find the peer UUID for a given BTA AV handle. |
| 206 | * |
| 207 | * @param bta_av_handle the BTA AV handle to use |
| 208 | * @return the peer UUID if found, otherwise 0 |
| 209 | */ |
| 210 | uint16_t FindPeerUuid(tBTA_AV_HNDL bta_av_handle); |
| 211 | |
| 212 | /** |
| 213 | * Process the AVDTP discovery result: number of Stream End Points (SEP) |
| 214 | * found during the AVDTP stream discovery process. |
| 215 | * |
| 216 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 217 | * @param peer_address the peer address |
| 218 | * @param num_seps the number of discovered SEPs |
| 219 | * @param num_sinks number of discovered Sink SEPs |
| 220 | * @param num_sources number of discovered Source SEPs |
| 221 | * @param uuid_local local UUID |
| 222 | */ |
| 223 | void ProcessDiscoveryResult(tBTA_AV_HNDL bta_av_handle, |
| 224 | const RawAddress& peer_address, uint8_t num_seps, |
| 225 | uint8_t num_sinks, uint8_t num_sources, |
| 226 | uint16_t uuid_local); |
| 227 | |
| 228 | /** |
| 229 | * Process retrieved codec configuration and content protection from |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 230 | * Peer Sink SEP. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 231 | * |
| 232 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 233 | * @param peer_address the peer address |
| 234 | * @param p_codec_info the peer sink capability filled-in by the caller. |
| 235 | * On success, it will contain the current codec configuration for the peer. |
| 236 | * @param p_sep_info_idx the peer SEP index for the corresponding peer |
| 237 | * sink capability filled-in by the caller. On success, it will contain |
| 238 | * the SEP index for the current codec configuration for the peer. |
| 239 | * @param seid the peer SEP index in peer tables |
| 240 | * @param p_num_protect the peer SEP number of content protection elements |
| 241 | * filled-in by the caller. On success, it will contain the SEP number of |
| 242 | * content protection elements for the current codec configuration for the |
| 243 | * peer. |
| 244 | * @param p_protect_info the peer SEP content protection info filled-in by |
| 245 | * the caller. On success, it will contain the SEP content protection info |
| 246 | * for the current codec configuration for the peer. |
| 247 | * @return A2DP_SUCCESS on success, otherwise A2DP_FAIL |
| 248 | */ |
| 249 | tA2DP_STATUS ProcessSourceGetConfig(tBTA_AV_HNDL bta_av_handle, |
| 250 | const RawAddress& peer_address, |
| 251 | uint8_t* p_codec_info, |
| 252 | uint8_t* p_sep_info_idx, uint8_t seid, |
| 253 | uint8_t* p_num_protect, |
| 254 | uint8_t* p_protect_info); |
| 255 | |
| 256 | /** |
| 257 | * Process retrieved codec configuration and content protection from |
| 258 | * Peer Source SEP. |
| 259 | * |
| 260 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 261 | * @param peer_address the peer address |
| 262 | * @param p_codec_info the peer source capability filled-in by the caller. |
| 263 | * On success, it will contain the current codec configuration for the peer. |
| 264 | * @param p_sep_info_idx the peer SEP index for the corresponding peer |
| 265 | * source capability filled-in by the caller. On success, it will contain |
| 266 | * the SEP index for the current codec configuration for the peer. |
| 267 | * @param seid the peer SEP index in peer tables |
| 268 | * @param p_num_protect the peer SEP number of content protection elements |
| 269 | * filled-in by the caller. On success, it will contain the SEP number of |
| 270 | * content protection elements for the current codec configuration for the |
| 271 | * peer. |
| 272 | * @param p_protect_info the peer SEP content protection info filled-in by |
| 273 | * the caller. On success, it will contain the SEP content protection info |
| 274 | * for the current codec configuration for the peer. |
| 275 | * @return A2DP_SUCCESS on success, otherwise A2DP_FAIL |
| 276 | */ |
| 277 | tA2DP_STATUS ProcessSinkGetConfig(tBTA_AV_HNDL bta_av_handle, |
| 278 | const RawAddress& peer_address, |
| 279 | uint8_t* p_codec_info, |
| 280 | uint8_t* p_sep_info_idx, uint8_t seid, |
| 281 | uint8_t* p_num_protect, |
| 282 | uint8_t* p_protect_info); |
| 283 | |
| 284 | /** |
| 285 | * Process AVDTP Set Config to set the codec and content protection |
| 286 | * configuration of the audio stream. |
| 287 | * |
| 288 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 289 | * @param peer_address the peer address |
| 290 | * @param p_codec_info the codec configuration to set |
| 291 | * @param seid stream endpoint ID of stream initiating the operation |
| 292 | * @param peer_address the peer address |
| 293 | * @param num_protect the peer SEP number of content protection elements |
| 294 | * @param p_protect_info the peer SEP conntent protection info |
| 295 | * @param t_local_sep the local SEP: AVDT_TSEP_SRC or AVDT_TSEP_SNK |
| 296 | * @param avdt_handle the AVDTP handle |
| 297 | */ |
| 298 | void ProcessSetConfig(tBTA_AV_HNDL bta_av_handle, |
| 299 | const RawAddress& peer_address, |
| 300 | const uint8_t* p_codec_info, uint8_t seid, |
| 301 | uint8_t num_protect, const uint8_t* p_protect_info, |
| 302 | uint8_t t_local_sep, uint8_t avdt_handle); |
| 303 | |
| 304 | /** |
| 305 | * Process AVDTP Open when the stream connection is opened. |
| 306 | * |
| 307 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 308 | * @param peer_address the peer address |
| 309 | * @param mtu the MTU of the connection |
| 310 | */ |
| 311 | void ProcessOpen(tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address, |
| 312 | uint16_t mtu); |
| 313 | |
| 314 | /** |
| 315 | * Process AVDTP Close when the stream connection is closed. |
| 316 | * |
| 317 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 318 | * @param peer_address the peer address |
| 319 | */ |
| 320 | void ProcessClose(tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address); |
| 321 | |
| 322 | /** |
| 323 | * Process AVDTP Start when the audio data streaming is started. |
| 324 | * |
| 325 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 326 | * @param peer_address the peer address |
| 327 | * @param p_codec_info the codec configuration |
| 328 | * @param p_no_rtp_header on return, set to true if the audio data packets |
| 329 | * should not contain RTP header |
| 330 | */ |
| 331 | void ProcessStart(tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address, |
| 332 | const uint8_t* p_codec_info, bool* p_no_rtp_header); |
| 333 | |
| 334 | /** |
| 335 | * Process AVDTP Stop when the audio data streaming is stopped. |
| 336 | * |
| 337 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 338 | * @param peer_address the peer address |
| 339 | */ |
| 340 | void ProcessStop(tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address); |
| 341 | |
| 342 | /** |
| 343 | * Get the next encoded audio data packet to send. |
| 344 | * |
| 345 | * @param p_codec_info the codec configuration |
| 346 | * @param p_timestamp on return, set to the timestamp of the data packet |
| 347 | * @return the next encoded data packet or nullptr if no encoded data to send |
| 348 | */ |
| 349 | BT_HDR* GetNextSourceDataPacket(const uint8_t* p_codec_info, |
| 350 | uint32_t* p_timestamp); |
| 351 | |
| 352 | /** |
| 353 | * An audio packet has been dropped. |
| 354 | * This signal can be used by the encoder to reduce the encoder bit rate |
| 355 | * setting. |
| 356 | * |
| 357 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 358 | * @param peer_address the peer address |
| 359 | */ |
| 360 | void DataPacketWasDropped(tBTA_AV_HNDL bta_av_handle, |
| 361 | const RawAddress& peer_address); |
| 362 | |
| 363 | /** |
| 364 | * Process AVDTP Audio Delay when the initial delay report is received by |
| 365 | * the Source. |
| 366 | * |
| 367 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 368 | * @param peer_address the peer address |
| 369 | * @param delay the reported delay in 1/10th of a millisecond |
| 370 | */ |
| 371 | void ProcessAudioDelay(tBTA_AV_HNDL bta_av_handle, |
| 372 | const RawAddress& peer_address, uint16_t delay); |
| 373 | |
| 374 | /** |
| 375 | * Update the MTU of the audio data connection. |
| 376 | * |
| 377 | * @param bta_av_handle the BTA AV handle to identify the peer |
| 378 | * @param peer_address the peer address |
| 379 | * @param mtu the new MTU of the audio data connection |
| 380 | */ |
| 381 | void UpdateMtu(tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address, |
| 382 | uint16_t mtu); |
| 383 | |
| 384 | /** |
| 385 | * Set the active peer. |
| 386 | * |
| 387 | * @param peer_address the peer address |
| 388 | * @return true on success, otherwise false |
| 389 | */ |
| 390 | bool SetActivePeer(const RawAddress& peer_address); |
| 391 | |
| 392 | /** |
| 393 | * Get the encoder parameters for a peer. |
| 394 | * |
| 395 | * @param peer_address the peer address |
| 396 | * @param p_peer_params on return, set to the peer's encoder parameters |
| 397 | */ |
| 398 | void GetPeerEncoderParameters(const RawAddress& peer_address, |
| 399 | tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params); |
| 400 | |
| 401 | /** |
| 402 | * Get the Source encoder interface for the current codec. |
| 403 | * |
| 404 | * @return the Source encoder interface for the current codec |
| 405 | */ |
| 406 | const tA2DP_ENCODER_INTERFACE* GetSourceEncoderInterface(); |
| 407 | |
| 408 | /** |
| 409 | * Get the Sink decoder interface for the current codec. |
| 410 | * |
| 411 | * @return the Sink decoder interface for the current codec |
| 412 | */ |
| 413 | const tA2DP_DECODER_INTERFACE* GetSinkDecoderInterface(); |
| 414 | |
| 415 | /** |
| 416 | * Set the codec user configuration. |
| 417 | * |
| 418 | * @param peer_address the peer address |
| 419 | * @param codec_user_config the codec user configuration to set |
| 420 | * @return true on success, otherwise false |
| 421 | */ |
| 422 | bool SetCodecUserConfig(const RawAddress& peer_address, |
| 423 | const btav_a2dp_codec_config_t& codec_user_config); |
| 424 | |
| 425 | /** |
| 426 | * Set the codec audio configuration. |
| 427 | * |
| 428 | * @param codec_audio_config the codec audio configuration to set |
| 429 | * @return true on success, otherwise false |
| 430 | */ |
| 431 | bool SetCodecAudioConfig(const btav_a2dp_codec_config_t& codec_audio_config); |
| 432 | |
| 433 | /** |
| 434 | * Report the source codec state for a peer |
| 435 | * |
| 436 | * @param p_peer the peer to report |
| 437 | * @return true on success, otherwise false |
| 438 | */ |
| 439 | bool ReportSourceCodecState(BtaAvCoPeer* p_peer); |
| 440 | |
| 441 | /** |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 442 | * Report the sink codec state for a peer |
| 443 | * |
| 444 | * @param p_peer the peer to report |
| 445 | * @return true on success, otherwise false |
| 446 | */ |
| 447 | bool ReportSinkCodecState(BtaAvCoPeer* p_peer); |
| 448 | |
| 449 | /** |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 450 | * Get the content protection flag. |
| 451 | * |
| 452 | * @return the content protection flag. It should be one of the following: |
| 453 | * AVDT_CP_SCMS_COPY_NEVER, AVDT_CP_SCMS_COPY_ONCE, AVDT_CP_SCMS_COPY_FREE |
| 454 | */ |
| 455 | uint8_t ContentProtectFlag() const { return content_protect_flag_; } |
| 456 | |
| 457 | /** |
| 458 | * Set the content protection flag. |
| 459 | * |
| 460 | * @param cp_flag the content protection flag. It should be one of the |
| 461 | * following: |
| 462 | * AVDT_CP_SCMS_COPY_NEVER, AVDT_CP_SCMS_COPY_ONCE, AVDT_CP_SCMS_COPY_FREE |
| 463 | * NOTE: If Content Protection is not enabled on the system, then |
| 464 | * the only acceptable vailue is AVDT_CP_SCMS_COPY_FREE. |
| 465 | */ |
| 466 | void SetContentProtectFlag(uint8_t cp_flag) { |
| 467 | if (!ContentProtectEnabled() && (cp_flag != AVDT_CP_SCMS_COPY_FREE)) { |
| 468 | return; |
| 469 | } |
| 470 | content_protect_flag_ = cp_flag; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Dump debug-related information. |
| 475 | * |
| 476 | * @param fd the file descritor to use for writing the ASCII formatted |
| 477 | * information |
| 478 | */ |
| 479 | void DebugDump(int fd); |
| 480 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 481 | /** |
| 482 | * Find the peer entry for a given peer address. |
| 483 | * |
| 484 | * @param peer_address the peer address to use |
| 485 | * @return the peer entry if found, otherwise nullptr |
| 486 | */ |
| 487 | BtaAvCoPeer* FindPeer(const RawAddress& peer_address); |
| 488 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Find the peer Sink SEP entry for a given codec index. |
| 491 | * |
| 492 | * @param p_peer the peer to use |
| 493 | * @param codec_index the codec index to use |
| 494 | * @return the peer Sink SEP for the codec index if found, otherwise nullptr |
| 495 | */ |
| 496 | BtaAvCoSep* FindPeerSink(BtaAvCoPeer* p_peer, |
| 497 | btav_a2dp_codec_index_t codec_index); |
| 498 | |
| 499 | /** |
| 500 | * Find the peer Source SEP entry for a given codec index. |
| 501 | * |
| 502 | * @param p_peer the peer to use |
| 503 | * @param codec_config the codec index to use |
| 504 | * @return the peer Source SEP for the codec index if found, otherwise nullptr |
| 505 | */ |
| 506 | BtaAvCoSep* FindPeerSource(BtaAvCoPeer* p_peer, |
| 507 | btav_a2dp_codec_index_t codec_index); |
| 508 | |
Sunny Kapdi | d66d954 | 2018-02-12 21:52:50 -0800 | [diff] [blame] | 509 | private: |
| 510 | /** |
| 511 | * Reset the state. |
| 512 | */ |
| 513 | void Reset(); |
| 514 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 515 | /** |
| 516 | * Find the peer entry for a given BTA AV handle. |
| 517 | * |
| 518 | * @param bta_av_handle the BTA AV handle to use |
| 519 | * @return the peer entry if found, otherwise nullptr |
| 520 | */ |
| 521 | BtaAvCoPeer* FindPeer(tBTA_AV_HNDL bta_av_handle); |
| 522 | |
| 523 | /** |
| 524 | * Find the peer entry for a given BTA AV handle and update it with the |
| 525 | * peer address. |
| 526 | * |
| 527 | * @param bta_av_handle the BTA AV handle to use |
| 528 | * @param peer_address the peer address |
| 529 | * @return the peer entry if found, otherwise nullptr |
| 530 | */ |
| 531 | BtaAvCoPeer* FindPeerAndUpdate(tBTA_AV_HNDL bta_av_handle, |
| 532 | const RawAddress& peer_address); |
| 533 | |
| 534 | /** |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 535 | * Select the Source codec configuration based on peer codec support. |
| 536 | * |
| 537 | * Furthermore, the local state for the remaining non-selected codecs is |
| 538 | * updated to reflect whether the codec is selectable. |
| 539 | * |
| 540 | * @param p_peer the peer to use |
| 541 | * @return a pointer to the corresponding SEP Sink entry on success, |
| 542 | * otherwise nullptr |
| 543 | */ |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 544 | const BtaAvCoSep* SelectSourceCodec(BtaAvCoPeer* p_peer); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 545 | |
| 546 | /** |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 547 | * Select the Sink codec configuration based on peer codec support. |
| 548 | * |
| 549 | * Furthermore, the local state for the remaining non-selected codecs is |
| 550 | * updated to reflect whether the codec is selectable. |
| 551 | * |
| 552 | * @param p_peer the peer to use |
| 553 | * @return a pointer to the corresponding SEP Source entry on success, |
| 554 | * otherwise nullptr |
| 555 | */ |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 556 | const BtaAvCoSep* SelectSinkCodec(BtaAvCoPeer* p_peer); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 557 | |
| 558 | /** |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 559 | * Save new codec configuration. |
| 560 | * |
| 561 | * @param p_peer the peer to use |
| 562 | * @param new_codec_config the new codec configuration to use |
| 563 | * @param num_protect the number of content protection elements |
| 564 | * @param p_protect_info the content protection info to use |
| 565 | */ |
| 566 | void SaveNewCodecConfig(BtaAvCoPeer* p_peer, const uint8_t* new_codec_config, |
| 567 | uint8_t num_protect, const uint8_t* p_protect_info); |
| 568 | |
| 569 | /** |
| 570 | * Set the Over-The-Air preferred codec configuration. |
| 571 | * |
| 572 | * The OTA prefered codec configuration is ignored if the current |
| 573 | * codec configuration contains explicit user configuration, or if the |
| 574 | * codec configuration for the same codec contains explicit user |
| 575 | * configuration. |
| 576 | * |
| 577 | * @param p_peer is the peer device that sent the OTA codec configuration |
| 578 | * @param p_ota_codec_config contains the received OTA A2DP codec |
| 579 | * configuration from the remote peer. Note: this is not the peer codec |
| 580 | * capability, but the codec configuration that the peer would like to use. |
| 581 | * @param num_protect is the number of content protection methods to use |
| 582 | * @param p_protect_info contains the content protection information to use. |
| 583 | * @param p_restart_output if there is a change in the encoder configuration |
| 584 | * that requires restarting of the A2DP connection, flag |p_restart_output| |
| 585 | * is set to true. |
| 586 | * @return true on success, otherwise false |
| 587 | */ |
| 588 | bool SetCodecOtaConfig(BtaAvCoPeer* p_peer, const uint8_t* p_ota_codec_config, |
| 589 | uint8_t num_protect, const uint8_t* p_protect_info, |
| 590 | bool* p_restart_output); |
| 591 | |
| 592 | /** |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 593 | * Update all selectable Source codecs with the corresponding codec |
| 594 | * information from a Sink peer. |
| 595 | * |
| 596 | * @param p_peer the peer Sink SEP to use |
| 597 | * @return the number of codecs that have been updated |
| 598 | */ |
| 599 | size_t UpdateAllSelectableSourceCodecs(BtaAvCoPeer* p_peer); |
| 600 | |
| 601 | /** |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 602 | * Update a selectable Source codec with the corresponding codec information |
| 603 | * from a Sink peer. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 604 | * |
| 605 | * @param codec_config the codec config info to identify the codec to update |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 606 | * @param p_peer the peer Sink SEP to use |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 607 | * @return true if the codec is updated, otherwise false |
| 608 | */ |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 609 | bool UpdateSelectableSourceCodec(const A2dpCodecConfig& codec_config, |
| 610 | BtaAvCoPeer* p_peer); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 611 | |
| 612 | /** |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 613 | * Update all selectable Sink codecs with the corresponding codec |
| 614 | * information from a Source peer. |
| 615 | * |
| 616 | * @param p_peer the peer Source SEP to use |
| 617 | * @return the number of codecs that have been updated |
| 618 | */ |
| 619 | size_t UpdateAllSelectableSinkCodecs(BtaAvCoPeer* p_peer); |
| 620 | |
| 621 | /** |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 622 | * Update a selectable Sink codec with the corresponding codec information |
| 623 | * from a Source peer. |
| 624 | * |
| 625 | * @param codec_config the codec config info to identify the codec to update |
| 626 | * @param p_peer the peer Source SEP to use |
| 627 | * @return true if the codec is updated, otherwise false |
| 628 | */ |
| 629 | bool UpdateSelectableSinkCodec(const A2dpCodecConfig& codec_config, |
| 630 | BtaAvCoPeer* p_peer); |
| 631 | |
| 632 | /** |
| 633 | * Attempt to select Source codec configuration for a Sink peer. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 634 | * |
| 635 | * @param codec_config the codec configuration to use |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 636 | * @param p_peer the Sink peer to use |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 637 | * @return a pointer to the corresponding SEP Sink entry on success, |
| 638 | * otnerwise nullptr |
| 639 | */ |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 640 | const BtaAvCoSep* AttemptSourceCodecSelection( |
| 641 | const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 642 | |
| 643 | /** |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 644 | * Attempt to select Sink codec configuration for a Source peer. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 645 | * |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 646 | * @param codec_config the codec configuration to use |
| 647 | * @param p_peer the Source peer to use |
| 648 | * @return a pointer to the corresponding SEP Source entry on success, |
| 649 | * otnerwise nullptr |
| 650 | */ |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 651 | const BtaAvCoSep* AttemptSinkCodecSelection( |
| 652 | const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 653 | |
| 654 | /** |
| 655 | * Check if a peer SEP has content protection enabled. |
| 656 | * |
| 657 | * @param p_sep the peer SEP to check |
| 658 | * @return true if the peer SEP has content protection enabled, |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 659 | * otherwise false |
| 660 | */ |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 661 | bool AudioSepHasContentProtection(const BtaAvCoSep* p_sep); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 662 | |
| 663 | /** |
| 664 | * Check if a content protection service is SCMS-T. |
| 665 | * |
| 666 | * @param p_orotect_info the content protection info to check |
| 667 | * @return true if the Contention Protection in @param p_protect_info |
| 668 | * is SCMS-T, otherwise false |
| 669 | */ |
| 670 | static bool ContentProtectIsScmst(const uint8_t* p_protect_info); |
| 671 | |
| 672 | /** |
| 673 | * Check if audio protect info contains SCMS-T Content Protection. |
| 674 | * |
| 675 | * @param num_protect number of protect schemes |
| 676 | * @param p_protect_info the protect info to check |
| 677 | * @return true if @param p_protect_info contains SCMS-T, otherwise false |
| 678 | */ |
| 679 | static bool AudioProtectHasScmst(uint8_t num_protect, |
| 680 | const uint8_t* p_protect_info); |
| 681 | |
| 682 | bool ContentProtectEnabled() const { return content_protect_enabled_; } |
| 683 | |
| 684 | std::recursive_mutex codec_lock_; // Protect access to the codec state |
| 685 | std::vector<btav_a2dp_codec_config_t> codec_priorities_; // Configured |
| 686 | BtaAvCoPeer peers_[BTA_AV_NUM_STRS]; // Connected peer information |
| 687 | BtaAvCoPeer* active_peer_; // The current active peer |
| 688 | uint8_t codec_config_[AVDT_CODEC_SIZE]; // Current codec configuration |
| 689 | const bool content_protect_enabled_; // True if Content Protect is enabled |
| 690 | uint8_t content_protect_flag_; // Content Protect flag |
| 691 | }; |
| 692 | |
| 693 | // SCMS-T protect info |
| 694 | const uint8_t bta_av_co_cp_scmst[AVDT_CP_INFO_LEN] = {0x02, 0x02, 0x00}; |
| 695 | |
| 696 | // Control block instance |
| 697 | #if (BTA_AV_CO_CP_SCMS_T == TRUE) |
| 698 | static const bool kContentProtectEnabled = true; |
| 699 | #else |
| 700 | static const bool kContentProtectEnabled = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 701 | #endif |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 702 | static BtaAvCo bta_av_co_cb(kContentProtectEnabled); |
| 703 | |
| 704 | void BtaAvCoPeer::Init( |
| 705 | const std::vector<btav_a2dp_codec_config_t>& codec_priorities) { |
| 706 | Reset(bta_av_handle_); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 707 | // Reset the current config |
| 708 | codecs_ = new A2dpCodecs(codec_priorities); |
| 709 | codecs_->init(); |
| 710 | A2DP_InitDefaultCodec(codec_config); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 713 | void BtaAvCoPeer::Reset(tBTA_AV_HNDL bta_av_handle) { |
| 714 | addr = RawAddress::kEmpty; |
| 715 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(sinks); i++) { |
| 716 | BtaAvCoSep& sink = sinks[i]; |
| 717 | sink.Reset(); |
| 718 | } |
| 719 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(sources); i++) { |
| 720 | BtaAvCoSep& source = sources[i]; |
| 721 | source.Reset(); |
| 722 | } |
| 723 | num_sinks = 0; |
| 724 | num_sources = 0; |
| 725 | num_seps = 0; |
| 726 | num_rx_sinks = 0; |
| 727 | num_rx_sources = 0; |
| 728 | num_sup_sinks = 0; |
| 729 | num_sup_sources = 0; |
| 730 | p_sink = nullptr; |
| 731 | p_source = nullptr; |
| 732 | memset(codec_config, 0, sizeof(codec_config)); |
| 733 | acceptor = false; |
| 734 | reconfig_needed = false; |
| 735 | opened = false; |
| 736 | mtu = 0; |
| 737 | uuid_to_connect = 0; |
| 738 | |
| 739 | bta_av_handle_ = bta_av_handle; |
| 740 | delete codecs_; |
| 741 | codecs_ = nullptr; |
| 742 | content_protect_active_ = false; |
| 743 | } |
| 744 | |
| 745 | void BtaAvCo::Init( |
| 746 | const std::vector<btav_a2dp_codec_config_t>& codec_priorities) { |
| 747 | APPL_TRACE_DEBUG("%s", __func__); |
| 748 | |
| 749 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 750 | |
| 751 | // Reset the control block |
| 752 | Reset(); |
| 753 | codec_priorities_ = codec_priorities; |
| 754 | |
| 755 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(peers_); i++) { |
| 756 | BtaAvCoPeer* p_peer = &peers_[i]; |
| 757 | p_peer->Init(codec_priorities); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | void BtaAvCo::Reset() { |
| 762 | codec_priorities_.clear(); |
| 763 | active_peer_ = nullptr; |
| 764 | content_protect_flag_ = 0; |
| 765 | memset(codec_config_, 0, sizeof(codec_config_)); |
| 766 | |
| 767 | if (ContentProtectEnabled()) { |
| 768 | SetContentProtectFlag(AVDT_CP_SCMS_COPY_NEVER); |
| 769 | } else { |
| 770 | SetContentProtectFlag(AVDT_CP_SCMS_COPY_FREE); |
| 771 | } |
| 772 | |
| 773 | // Reset the peers and initialize the handles |
| 774 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(peers_); i++) { |
| 775 | BtaAvCoPeer* p_peer = &peers_[i]; |
| 776 | p_peer->Reset(BTA_AV_CO_AUDIO_INDEX_TO_HANDLE(i)); |
| 777 | } |
| 778 | } |
| 779 | |
Pavlin Radoslavov | 7240fe9 | 2018-09-11 16:47:47 -0700 | [diff] [blame] | 780 | bool BtaAvCo::IsSupportedCodec(btav_a2dp_codec_index_t codec_index) { |
| 781 | // All peer state is initialized with the same local codec config, |
| 782 | // hence we check only the first peer. |
| 783 | A2dpCodecs* codecs = peers_[0].GetCodecs(); |
| 784 | CHECK(codecs != nullptr); |
| 785 | return codecs->isSupportedCodec(codec_index); |
| 786 | } |
| 787 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 788 | A2dpCodecConfig* BtaAvCo::GetActivePeerCurrentCodec() { |
| 789 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 790 | |
| 791 | if (active_peer_ == nullptr || active_peer_->GetCodecs() == nullptr) { |
| 792 | return nullptr; |
| 793 | } |
| 794 | return active_peer_->GetCodecs()->getCurrentCodecConfig(); |
| 795 | } |
| 796 | |
Pavlin Radoslavov | 42840e0 | 2018-02-28 23:34:28 -0800 | [diff] [blame] | 797 | A2dpCodecConfig* BtaAvCo::GetPeerCurrentCodec(const RawAddress& peer_address) { |
| 798 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 799 | |
| 800 | BtaAvCoPeer* peer = FindPeer(peer_address); |
| 801 | if (peer == nullptr || peer->GetCodecs() == nullptr) { |
| 802 | return nullptr; |
| 803 | } |
| 804 | return peer->GetCodecs()->getCurrentCodecConfig(); |
| 805 | } |
| 806 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 807 | BtaAvCoPeer* BtaAvCo::FindPeer(const RawAddress& peer_address) { |
| 808 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(peers_); i++) { |
| 809 | BtaAvCoPeer* p_peer = &peers_[i]; |
| 810 | if (p_peer->addr == peer_address) { |
| 811 | return p_peer; |
| 812 | } |
| 813 | } |
| 814 | return nullptr; |
| 815 | } |
| 816 | |
| 817 | BtaAvCoPeer* BtaAvCo::FindPeer(tBTA_AV_HNDL bta_av_handle) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 818 | uint8_t index; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 819 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 820 | index = BTA_AV_CO_AUDIO_HANDLE_TO_INDEX(bta_av_handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 821 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 822 | APPL_TRACE_DEBUG("%s: bta_av_handle = 0x%x index = %d", __func__, |
| 823 | bta_av_handle, index); |
Pavlin Radoslavov | 79506e8 | 2016-09-07 21:57:47 -0700 | [diff] [blame] | 824 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 825 | // Sanity check |
| 826 | if (index >= BTA_AV_CO_NUM_ELEMENTS(peers_)) { |
| 827 | APPL_TRACE_ERROR( |
| 828 | "%s: peer index %d for BTA AV handle 0x%x is out of bounds", __func__, |
| 829 | index, bta_av_handle); |
| 830 | return nullptr; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 831 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 832 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 833 | return &peers_[index]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 836 | BtaAvCoPeer* BtaAvCo::FindPeerAndUpdate(tBTA_AV_HNDL bta_av_handle, |
| 837 | const RawAddress& peer_address) { |
| 838 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle = 0x%x", __func__, |
| 839 | peer_address.ToString().c_str(), bta_av_handle); |
| 840 | |
| 841 | BtaAvCoPeer* p_peer = FindPeer(bta_av_handle); |
| 842 | if (p_peer == nullptr) { |
Pavlin Radoslavov | 148a10d | 2018-04-13 22:07:30 -0700 | [diff] [blame] | 843 | APPL_TRACE_ERROR("%s: peer entry for BTA AV handle 0x%x peer %s not found", |
| 844 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 845 | return nullptr; |
| 846 | } |
| 847 | |
| 848 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle = 0x%x previous address %s", |
| 849 | __func__, peer_address.ToString().c_str(), bta_av_handle, |
| 850 | p_peer->addr.ToString().c_str()); |
| 851 | p_peer->addr = peer_address; |
| 852 | return p_peer; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 855 | uint16_t BtaAvCo::FindPeerUuid(tBTA_AV_HNDL bta_av_handle) { |
| 856 | BtaAvCoPeer* p_peer = FindPeer(bta_av_handle); |
| 857 | if (p_peer == nullptr) { |
| 858 | return 0; |
| 859 | } |
| 860 | return p_peer->uuid_to_connect; |
| 861 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 862 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 863 | void BtaAvCo::ProcessDiscoveryResult(tBTA_AV_HNDL bta_av_handle, |
| 864 | const RawAddress& peer_address, |
| 865 | uint8_t num_seps, uint8_t num_sinks, |
| 866 | uint8_t num_sources, uint16_t uuid_local) { |
| 867 | APPL_TRACE_DEBUG( |
| 868 | "%s: peer %s bta_av_handle:0x%x num_seps:%d num_sinks:%d num_sources:%d", |
| 869 | __func__, peer_address.ToString().c_str(), bta_av_handle, num_seps, |
| 870 | num_sinks, num_sources); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 871 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 872 | // Find the peer |
| 873 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 874 | if (p_peer == nullptr) { |
| 875 | APPL_TRACE_ERROR( |
| 876 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 877 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 878 | return; |
| 879 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 880 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 881 | /* Sanity check : this should never happen */ |
| 882 | if (p_peer->opened) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 883 | APPL_TRACE_ERROR("%s: peer %s already opened", __func__, |
| 884 | peer_address.ToString().c_str()); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 885 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 886 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 887 | /* Copy the discovery results */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 888 | p_peer->addr = peer_address; |
| 889 | p_peer->num_sinks = num_sinks; |
| 890 | p_peer->num_sources = num_sources; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 891 | p_peer->num_seps = num_seps; |
| 892 | p_peer->num_rx_sinks = 0; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 893 | p_peer->num_rx_sources = 0; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 894 | p_peer->num_sup_sinks = 0; |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 895 | p_peer->num_sup_sources = 0; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 896 | if (uuid_local == UUID_SERVCLASS_AUDIO_SINK) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 897 | p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SOURCE; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 898 | } else if (uuid_local == UUID_SERVCLASS_AUDIO_SOURCE) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 899 | p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SINK; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 900 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 903 | tA2DP_STATUS BtaAvCo::ProcessSourceGetConfig( |
| 904 | tBTA_AV_HNDL bta_av_handle, const RawAddress& peer_address, |
| 905 | uint8_t* p_codec_info, uint8_t* p_sep_info_idx, uint8_t seid, |
| 906 | uint8_t* p_num_protect, uint8_t* p_protect_info) { |
| 907 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle:0x%x codec:%s seid:%d", __func__, |
| 908 | peer_address.ToString().c_str(), bta_av_handle, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 909 | A2DP_CodecName(p_codec_info), seid); |
| 910 | APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", |
| 911 | __func__, *p_num_protect, p_protect_info[0], |
| 912 | p_protect_info[1], p_protect_info[2]); |
Pavlin Radoslavov | 25cd054 | 2018-04-06 17:05:47 -0700 | [diff] [blame] | 913 | APPL_TRACE_DEBUG("%s: codec: %s", __func__, |
| 914 | A2DP_CodecInfoString(p_codec_info).c_str()); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 915 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 916 | // Find the peer |
| 917 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 918 | if (p_peer == nullptr) { |
| 919 | APPL_TRACE_ERROR( |
| 920 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 921 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 922 | return A2DP_FAIL; |
| 923 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 924 | APPL_TRACE_DEBUG("%s: peer(o=%d, n_sinks=%d, n_rx_sinks=%d, n_sup_sinks=%d)", |
| 925 | __func__, p_peer->opened, p_peer->num_sinks, |
| 926 | p_peer->num_rx_sinks, p_peer->num_sup_sinks); |
| 927 | |
| 928 | p_peer->num_rx_sinks++; |
| 929 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 930 | // Check the peer's Sink codec |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 931 | if (A2DP_IsPeerSinkCodecValid(p_codec_info)) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 932 | // If there is room for a new one |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 933 | if (p_peer->num_sup_sinks < BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks)) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 934 | BtaAvCoSep* p_sink = &p_peer->sinks[p_peer->num_sup_sinks++]; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 935 | |
| 936 | APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", __func__, |
| 937 | p_codec_info[1], p_codec_info[2], p_codec_info[3], |
| 938 | p_codec_info[4], p_codec_info[5], p_codec_info[6]); |
| 939 | |
| 940 | memcpy(p_sink->codec_caps, p_codec_info, AVDT_CODEC_SIZE); |
| 941 | p_sink->sep_info_idx = *p_sep_info_idx; |
| 942 | p_sink->seid = seid; |
| 943 | p_sink->num_protect = *p_num_protect; |
Pavlin Radoslavov | feee26c | 2016-10-25 10:17:56 -0700 | [diff] [blame] | 944 | memcpy(p_sink->protect_info, p_protect_info, AVDT_CP_INFO_LEN); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 945 | } else { |
Pavlin Radoslavov | 148a10d | 2018-04-13 22:07:30 -0700 | [diff] [blame] | 946 | APPL_TRACE_ERROR("%s: peer %s : no more room for Sink info", __func__, |
| 947 | p_peer->addr.ToString().c_str()); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 948 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 949 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 950 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 951 | // Check if this is the last Sink get capabilities or all supported codec |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 952 | // capabilities are retrieved. |
| 953 | if ((p_peer->num_rx_sinks != p_peer->num_sinks) && |
| 954 | (p_peer->num_sup_sinks != BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks))) { |
| 955 | return A2DP_FAIL; |
| 956 | } |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 957 | APPL_TRACE_DEBUG("%s: last Sink codec reached for peer %s", __func__, |
| 958 | p_peer->addr.ToString().c_str()); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 959 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 960 | // Select the Source codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 961 | const BtaAvCoSep* p_sink = nullptr; |
| 962 | if (p_peer->acceptor) { |
| 963 | UpdateAllSelectableSourceCodecs(p_peer); |
| 964 | if (p_peer->p_sink == nullptr) { |
| 965 | // Update the selected codec |
| 966 | p_peer->p_sink = |
| 967 | FindPeerSink(p_peer, A2DP_SourceCodecIndex(p_peer->codec_config)); |
| 968 | } |
| 969 | p_sink = p_peer->p_sink; |
| 970 | if (p_sink == nullptr) { |
| 971 | APPL_TRACE_ERROR("%s: cannot find the selected codec for peer %s", |
| 972 | __func__, p_peer->addr.ToString().c_str()); |
| 973 | return A2DP_FAIL; |
| 974 | } |
| 975 | } else { |
| 976 | p_sink = SelectSourceCodec(p_peer); |
| 977 | if (p_sink == nullptr) { |
| 978 | APPL_TRACE_ERROR("%s: cannot set up codec for peer %s", __func__, |
| 979 | p_peer->addr.ToString().c_str()); |
| 980 | return A2DP_FAIL; |
| 981 | } |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 982 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 983 | |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 984 | // By default, no content protection |
| 985 | *p_num_protect = 0; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 986 | if (ContentProtectEnabled() && p_peer->ContentProtectActive()) { |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 987 | *p_num_protect = AVDT_CP_INFO_LEN; |
| 988 | memcpy(p_protect_info, bta_av_co_cp_scmst, AVDT_CP_INFO_LEN); |
| 989 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 990 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 991 | // If acceptor -> reconfig otherwise reply for configuration |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 992 | *p_sep_info_idx = p_sink->sep_info_idx; |
| 993 | APPL_TRACE_EVENT("%s: peer %s acceptor:%s reconfig_needed:%s", __func__, |
| 994 | p_peer->addr.ToString().c_str(), |
| 995 | (p_peer->acceptor) ? "true" : "false", |
| 996 | (p_peer->reconfig_needed) ? "true" : "false"); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 997 | if (p_peer->acceptor) { |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 998 | if (p_peer->reconfig_needed) { |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 999 | APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x) for peer %s", __func__, |
| 1000 | bta_av_handle, p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1001 | BTA_AvReconfig(bta_av_handle, true, p_sink->sep_info_idx, |
| 1002 | p_peer->codec_config, *p_num_protect, bta_av_co_cp_scmst); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1003 | } |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1004 | } else { |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1005 | memcpy(p_codec_info, p_peer->codec_config, AVDT_CODEC_SIZE); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1006 | } |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1007 | |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1008 | // report this peer selectable codecs after retrieved all its capabilities. |
| 1009 | LOG(INFO) << __func__ << ": retrieved " << +p_peer->num_rx_sinks |
| 1010 | << " capabilities from peer " << p_peer->addr; |
| 1011 | ReportSourceCodecState(p_peer); |
| 1012 | |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1013 | return A2DP_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1016 | tA2DP_STATUS BtaAvCo::ProcessSinkGetConfig(tBTA_AV_HNDL bta_av_handle, |
| 1017 | const RawAddress& peer_address, |
| 1018 | uint8_t* p_codec_info, |
| 1019 | uint8_t* p_sep_info_idx, |
| 1020 | uint8_t seid, uint8_t* p_num_protect, |
| 1021 | uint8_t* p_protect_info) { |
| 1022 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 1023 | |
| 1024 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle:0x%x codec:%s seid:%d", __func__, |
| 1025 | peer_address.ToString().c_str(), bta_av_handle, |
| 1026 | A2DP_CodecName(p_codec_info), seid); |
| 1027 | APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", |
| 1028 | __func__, *p_num_protect, p_protect_info[0], |
| 1029 | p_protect_info[1], p_protect_info[2]); |
Pavlin Radoslavov | 25cd054 | 2018-04-06 17:05:47 -0700 | [diff] [blame] | 1030 | APPL_TRACE_DEBUG("%s: codec: %s", __func__, |
| 1031 | A2DP_CodecInfoString(p_codec_info).c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1032 | |
| 1033 | // Find the peer |
| 1034 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1035 | if (p_peer == nullptr) { |
| 1036 | APPL_TRACE_ERROR( |
| 1037 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1038 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
| 1039 | return A2DP_FAIL; |
| 1040 | } |
| 1041 | APPL_TRACE_DEBUG( |
| 1042 | "%s: peer %s found (o=%d, n_sources=%d, n_rx_sources=%d, " |
| 1043 | "n_sup_sources=%d)", |
| 1044 | __func__, p_peer->addr.ToString().c_str(), p_peer->opened, |
| 1045 | p_peer->num_sources, p_peer->num_rx_sources, p_peer->num_sup_sources); |
| 1046 | |
| 1047 | p_peer->num_rx_sources++; |
| 1048 | |
| 1049 | // Check the peer's Source codec |
| 1050 | if (A2DP_IsPeerSourceCodecValid(p_codec_info)) { |
| 1051 | // If there is room for a new one |
| 1052 | if (p_peer->num_sup_sources < BTA_AV_CO_NUM_ELEMENTS(p_peer->sources)) { |
| 1053 | BtaAvCoSep* p_source = &p_peer->sources[p_peer->num_sup_sources++]; |
| 1054 | |
| 1055 | APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", __func__, |
| 1056 | p_codec_info[1], p_codec_info[2], p_codec_info[3], |
| 1057 | p_codec_info[4], p_codec_info[5], p_codec_info[6]); |
| 1058 | |
| 1059 | memcpy(p_source->codec_caps, p_codec_info, AVDT_CODEC_SIZE); |
| 1060 | p_source->sep_info_idx = *p_sep_info_idx; |
| 1061 | p_source->seid = seid; |
| 1062 | p_source->num_protect = *p_num_protect; |
| 1063 | memcpy(p_source->protect_info, p_protect_info, AVDT_CP_INFO_LEN); |
| 1064 | } else { |
Pavlin Radoslavov | 148a10d | 2018-04-13 22:07:30 -0700 | [diff] [blame] | 1065 | APPL_TRACE_ERROR("%s: peer %s : no more room for Source info", __func__, |
| 1066 | p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | // Check if this is the last Source get capabilities or all supported codec |
| 1071 | // capabilities are retrieved. |
| 1072 | if ((p_peer->num_rx_sources != p_peer->num_sources) && |
| 1073 | (p_peer->num_sup_sources != BTA_AV_CO_NUM_ELEMENTS(p_peer->sources))) { |
| 1074 | return A2DP_FAIL; |
| 1075 | } |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1076 | APPL_TRACE_DEBUG("%s: last Source codec reached for peer %s", __func__, |
| 1077 | p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1078 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1079 | // Select the Sink codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1080 | const BtaAvCoSep* p_source = nullptr; |
| 1081 | if (p_peer->acceptor) { |
| 1082 | UpdateAllSelectableSinkCodecs(p_peer); |
| 1083 | if (p_peer->p_source == nullptr) { |
| 1084 | // Update the selected codec |
| 1085 | p_peer->p_source = |
| 1086 | FindPeerSource(p_peer, A2DP_SinkCodecIndex(p_peer->codec_config)); |
| 1087 | } |
| 1088 | p_source = p_peer->p_source; |
| 1089 | if (p_source == nullptr) { |
| 1090 | APPL_TRACE_ERROR("%s: cannot find the selected codec for peer %s", |
| 1091 | __func__, p_peer->addr.ToString().c_str()); |
| 1092 | return A2DP_FAIL; |
| 1093 | } |
| 1094 | } else { |
| 1095 | p_source = SelectSinkCodec(p_peer); |
| 1096 | if (p_source == nullptr) { |
| 1097 | APPL_TRACE_ERROR("%s: cannot set up codec for the peer %s", __func__, |
| 1098 | p_peer->addr.ToString().c_str()); |
| 1099 | return A2DP_FAIL; |
| 1100 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1103 | // By default, no content protection |
| 1104 | *p_num_protect = 0; |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1105 | if (ContentProtectEnabled() && p_peer->ContentProtectActive()) { |
| 1106 | *p_num_protect = AVDT_CP_INFO_LEN; |
| 1107 | memcpy(p_protect_info, bta_av_co_cp_scmst, AVDT_CP_INFO_LEN); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1110 | // If acceptor -> reconfig otherwise reply for configuration |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1111 | *p_sep_info_idx = p_source->sep_info_idx; |
| 1112 | APPL_TRACE_EVENT("%s: peer %s acceptor:%s reconfig_needed:%s", __func__, |
| 1113 | p_peer->addr.ToString().c_str(), |
| 1114 | (p_peer->acceptor) ? "true" : "false", |
| 1115 | (p_peer->reconfig_needed) ? "true" : "false"); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1116 | if (p_peer->acceptor) { |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1117 | if (p_peer->reconfig_needed) { |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1118 | APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x) for peer %s", __func__, |
| 1119 | bta_av_handle, p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1120 | BTA_AvReconfig(bta_av_handle, true, p_source->sep_info_idx, |
| 1121 | p_peer->codec_config, *p_num_protect, bta_av_co_cp_scmst); |
| 1122 | } |
| 1123 | } else { |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1124 | memcpy(p_codec_info, p_peer->codec_config, AVDT_CODEC_SIZE); |
| 1125 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1126 | |
| 1127 | return A2DP_SUCCESS; |
| 1128 | } |
| 1129 | |
| 1130 | void BtaAvCo::ProcessSetConfig(tBTA_AV_HNDL bta_av_handle, |
| 1131 | UNUSED_ATTR const RawAddress& peer_address, |
| 1132 | const uint8_t* p_codec_info, |
| 1133 | UNUSED_ATTR uint8_t seid, uint8_t num_protect, |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1134 | const uint8_t* p_protect_info, |
| 1135 | uint8_t t_local_sep, uint8_t avdt_handle) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1136 | tA2DP_STATUS status = A2DP_SUCCESS; |
| 1137 | uint8_t category = A2DP_SUCCESS; |
Pavlin Radoslavov | 82fcd30 | 2016-10-23 20:39:46 -0700 | [diff] [blame] | 1138 | bool reconfig_needed = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1139 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1140 | APPL_TRACE_DEBUG( |
| 1141 | "%s: bta_av_handle=0x%x peer_address=%s seid=%d " |
| 1142 | "num_protect=%d t_local_sep=%d avdt_handle=%d", |
| 1143 | __func__, bta_av_handle, peer_address.ToString().c_str(), seid, |
| 1144 | num_protect, t_local_sep, avdt_handle); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1145 | APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", __func__, |
| 1146 | p_codec_info[1], p_codec_info[2], p_codec_info[3], |
| 1147 | p_codec_info[4], p_codec_info[5], p_codec_info[6]); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1148 | APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", |
| 1149 | __func__, num_protect, p_protect_info[0], p_protect_info[1], |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1150 | p_protect_info[2]); |
Pavlin Radoslavov | 25cd054 | 2018-04-06 17:05:47 -0700 | [diff] [blame] | 1151 | APPL_TRACE_DEBUG("%s: codec: %s", __func__, |
| 1152 | A2DP_CodecInfoString(p_codec_info).c_str()); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1153 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1154 | // Find the peer |
| 1155 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1156 | if (p_peer == nullptr) { |
| 1157 | APPL_TRACE_ERROR( |
| 1158 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1159 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
| 1160 | // Call call-in rejecting the configuration |
| 1161 | bta_av_ci_setconfig(bta_av_handle, A2DP_BUSY, AVDT_ASC_CODEC, 0, nullptr, |
| 1162 | false, avdt_handle); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1163 | return; |
| 1164 | } |
| 1165 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1166 | APPL_TRACE_DEBUG( |
| 1167 | "%s: peer %s found (o=%d, n_sinks=%d, n_rx_sinks=%d, " |
| 1168 | "n_sup_sinks=%d)", |
| 1169 | __func__, p_peer->addr.ToString().c_str(), p_peer->opened, |
| 1170 | p_peer->num_sinks, p_peer->num_rx_sinks, p_peer->num_sup_sinks); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1171 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1172 | // Sanity check: should not be opened at this point |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1173 | if (p_peer->opened) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1174 | APPL_TRACE_ERROR("%s: peer %s already in use", __func__, |
| 1175 | p_peer->addr.ToString().c_str()); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (num_protect != 0) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1179 | if (ContentProtectEnabled()) { |
| 1180 | if ((num_protect != 1) || |
| 1181 | !BtaAvCo::ContentProtectIsScmst(p_protect_info)) { |
| 1182 | APPL_TRACE_ERROR("%s: wrong CP configuration for peer %s", __func__, |
| 1183 | p_peer->addr.ToString().c_str()); |
| 1184 | status = A2DP_BAD_CP_TYPE; |
| 1185 | category = AVDT_ASC_PROTECT; |
| 1186 | } |
| 1187 | } else { |
| 1188 | // Do not support content protection for the time being |
| 1189 | APPL_TRACE_ERROR("%s: wrong CP configuration for peer %s", __func__, |
| 1190 | p_peer->addr.ToString().c_str()); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1191 | status = A2DP_BAD_CP_TYPE; |
| 1192 | category = AVDT_ASC_PROTECT; |
| 1193 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (status == A2DP_SUCCESS) { |
Pavlin Radoslavov | 82fcd30 | 2016-10-23 20:39:46 -0700 | [diff] [blame] | 1197 | bool codec_config_supported = false; |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1198 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1199 | if (t_local_sep == AVDT_TSEP_SNK) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1200 | APPL_TRACE_DEBUG("%s: peer %s is A2DP Source", __func__, |
| 1201 | p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | 82fcd30 | 2016-10-23 20:39:46 -0700 | [diff] [blame] | 1202 | codec_config_supported = A2DP_IsSinkCodecSupported(p_codec_info); |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1203 | if (codec_config_supported) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1204 | // If Peer is Source, and our config subset matches with what is |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1205 | // requested by peer, then just accept what peer wants. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1206 | SaveNewCodecConfig(p_peer, p_codec_info, num_protect, p_protect_info); |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1207 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1208 | } |
| 1209 | if (t_local_sep == AVDT_TSEP_SRC) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1210 | APPL_TRACE_DEBUG("%s: peer %s is A2DP SINK", __func__, |
| 1211 | p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | 1c21a92 | 2018-03-27 00:17:29 -0700 | [diff] [blame] | 1212 | // Ignore the restart_output flag: accepting the remote device's |
| 1213 | // codec selection should not trigger codec reconfiguration. |
| 1214 | bool dummy_restart_output = false; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1215 | if ((p_peer->GetCodecs() == nullptr) || |
| 1216 | !SetCodecOtaConfig(p_peer, p_codec_info, num_protect, p_protect_info, |
Pavlin Radoslavov | 1c21a92 | 2018-03-27 00:17:29 -0700 | [diff] [blame] | 1217 | &dummy_restart_output)) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1218 | APPL_TRACE_ERROR("%s: cannot set source codec %s for peer %s", __func__, |
| 1219 | A2DP_CodecName(p_codec_info), |
| 1220 | p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1221 | } else { |
| 1222 | codec_config_supported = true; |
| 1223 | // Check if reconfiguration is needed |
Pavlin Radoslavov | 1c21a92 | 2018-03-27 00:17:29 -0700 | [diff] [blame] | 1224 | if (((num_protect == 1) && !p_peer->ContentProtectActive())) { |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1225 | reconfig_needed = true; |
| 1226 | } |
| 1227 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1230 | // Check if codec configuration is supported |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1231 | if (!codec_config_supported) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1232 | category = AVDT_ASC_CODEC; |
| 1233 | status = A2DP_WRONG_CODEC; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | if (status != A2DP_SUCCESS) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1238 | APPL_TRACE_DEBUG("%s: peer %s reject s=%d c=%d", __func__, |
| 1239 | p_peer->addr.ToString().c_str(), status, category); |
| 1240 | // Call call-in rejecting the configuration |
| 1241 | bta_av_ci_setconfig(bta_av_handle, status, category, 0, nullptr, false, |
| 1242 | avdt_handle); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1243 | return; |
| 1244 | } |
| 1245 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1246 | // Mark that this is an acceptor peer |
| 1247 | p_peer->acceptor = true; |
Pavlin Radoslavov | 82fcd30 | 2016-10-23 20:39:46 -0700 | [diff] [blame] | 1248 | p_peer->reconfig_needed = reconfig_needed; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1249 | APPL_TRACE_DEBUG("%s: peer %s accept reconf=%d", __func__, |
| 1250 | p_peer->addr.ToString().c_str(), reconfig_needed); |
| 1251 | // Call call-in accepting the configuration |
| 1252 | bta_av_ci_setconfig(bta_av_handle, A2DP_SUCCESS, A2DP_SUCCESS, 0, nullptr, |
Pavlin Radoslavov | 82fcd30 | 2016-10-23 20:39:46 -0700 | [diff] [blame] | 1253 | reconfig_needed, avdt_handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1256 | void BtaAvCo::ProcessOpen(tBTA_AV_HNDL bta_av_handle, |
| 1257 | const RawAddress& peer_address, uint16_t mtu) { |
| 1258 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x mtu:%d", __func__, |
| 1259 | peer_address.ToString().c_str(), bta_av_handle, mtu); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1260 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1261 | // Find the peer |
| 1262 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1263 | if (p_peer == nullptr) { |
| 1264 | APPL_TRACE_ERROR( |
| 1265 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1266 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
| 1267 | return; |
| 1268 | } |
| 1269 | p_peer->opened = true; |
| 1270 | p_peer->mtu = mtu; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1271 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1272 | // The first connected peer becomes the active peer |
| 1273 | if (active_peer_ == nullptr) { |
| 1274 | active_peer_ = p_peer; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1275 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1278 | void BtaAvCo::ProcessClose(tBTA_AV_HNDL bta_av_handle, |
| 1279 | const RawAddress& peer_address) { |
| 1280 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x", __func__, |
| 1281 | peer_address.ToString().c_str(), bta_av_handle); |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1282 | btif_av_reset_audio_delay(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1283 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1284 | // Find the peer |
| 1285 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1286 | if (p_peer == nullptr) { |
| 1287 | APPL_TRACE_ERROR( |
| 1288 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1289 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
| 1290 | return; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1291 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1292 | // Reset the active peer |
| 1293 | if (active_peer_ == p_peer) { |
| 1294 | active_peer_ = nullptr; |
| 1295 | } |
| 1296 | // Mark the peer closed and clean the peer info |
| 1297 | p_peer->Init(codec_priorities_); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1300 | void BtaAvCo::ProcessStart(tBTA_AV_HNDL bta_av_handle, |
| 1301 | const RawAddress& peer_address, |
| 1302 | const uint8_t* p_codec_info, bool* p_no_rtp_header) { |
| 1303 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x", __func__, |
| 1304 | peer_address.ToString().c_str(), bta_av_handle); |
| 1305 | |
| 1306 | // Find the peer |
| 1307 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1308 | if (p_peer == nullptr) { |
| 1309 | APPL_TRACE_ERROR( |
| 1310 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1311 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
| 1312 | return; |
| 1313 | } |
| 1314 | |
| 1315 | bool add_rtp_header = |
| 1316 | A2DP_UsesRtpHeader(p_peer->ContentProtectActive(), p_codec_info); |
| 1317 | |
| 1318 | APPL_TRACE_DEBUG("%s: bta_av_handle: 0x%x add_rtp_header: %s", __func__, |
| 1319 | bta_av_handle, add_rtp_header ? "true" : "false"); |
| 1320 | *p_no_rtp_header = !add_rtp_header; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1323 | void BtaAvCo::ProcessStop(tBTA_AV_HNDL bta_av_handle, |
| 1324 | const RawAddress& peer_address) { |
| 1325 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x", __func__, |
| 1326 | peer_address.ToString().c_str(), bta_av_handle); |
| 1327 | // Nothing to do |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1330 | BT_HDR* BtaAvCo::GetNextSourceDataPacket(const uint8_t* p_codec_info, |
| 1331 | uint32_t* p_timestamp) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1332 | BT_HDR* p_buf; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1333 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1334 | APPL_TRACE_DEBUG("%s: codec: %s", __func__, A2DP_CodecName(p_codec_info)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1335 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1336 | p_buf = btif_a2dp_source_audio_readbuf(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1337 | if (p_buf == nullptr) return nullptr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1338 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1339 | /* |
| 1340 | * Retrieve the timestamp information from the media packet, |
| 1341 | * and set up the packet header. |
| 1342 | * |
| 1343 | * In media packet, the following information is available: |
| 1344 | * p_buf->layer_specific : number of audio frames in the packet |
| 1345 | * p_buf->word[0] : timestamp |
| 1346 | */ |
| 1347 | if (!A2DP_GetPacketTimestamp(p_codec_info, (const uint8_t*)(p_buf + 1), |
| 1348 | p_timestamp) || |
| 1349 | !A2DP_BuildCodecHeader(p_codec_info, p_buf, p_buf->layer_specific)) { |
| 1350 | APPL_TRACE_ERROR("%s: unsupported codec type (%d)", __func__, |
| 1351 | A2DP_GetCodecType(p_codec_info)); |
| 1352 | } |
Pavlin Radoslavov | c66edf3 | 2016-09-15 17:50:18 -0700 | [diff] [blame] | 1353 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1354 | if (ContentProtectEnabled() && (active_peer_ != nullptr) && |
| 1355 | active_peer_->ContentProtectActive()) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1356 | p_buf->len++; |
| 1357 | p_buf->offset--; |
| 1358 | uint8_t* p = (uint8_t*)(p_buf + 1) + p_buf->offset; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1359 | *p = ContentProtectFlag(); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1360 | } |
Pavlin Radoslavov | c66edf3 | 2016-09-15 17:50:18 -0700 | [diff] [blame] | 1361 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1362 | return p_buf; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1363 | } |
| 1364 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1365 | void BtaAvCo::DataPacketWasDropped(tBTA_AV_HNDL bta_av_handle, |
| 1366 | const RawAddress& peer_address) { |
| 1367 | APPL_TRACE_ERROR("%s: peer %s dropped audio packet on handle 0x%x", __func__, |
| 1368 | peer_address.ToString().c_str(), bta_av_handle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1371 | void BtaAvCo::ProcessAudioDelay(tBTA_AV_HNDL bta_av_handle, |
| 1372 | const RawAddress& peer_address, |
| 1373 | uint16_t delay) { |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1374 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x delay:0x%x", __func__, |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1375 | peer_address.ToString().c_str(), bta_av_handle, delay); |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1376 | |
| 1377 | btif_av_set_audio_delay(delay); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1380 | void BtaAvCo::UpdateMtu(tBTA_AV_HNDL bta_av_handle, |
| 1381 | const RawAddress& peer_address, uint16_t mtu) { |
| 1382 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x mtu: %d", __func__, |
| 1383 | peer_address.ToString().c_str(), bta_av_handle, mtu); |
Pavlin Radoslavov | 31d373b | 2017-02-14 12:18:44 -0800 | [diff] [blame] | 1384 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1385 | // Find the peer |
| 1386 | BtaAvCoPeer* p_peer = FindPeerAndUpdate(bta_av_handle, peer_address); |
| 1387 | if (p_peer == nullptr) { |
| 1388 | APPL_TRACE_ERROR( |
| 1389 | "%s: could not find peer entry for bta_av_handle 0x%x peer %s", |
| 1390 | __func__, bta_av_handle, peer_address.ToString().c_str()); |
Pavlin Radoslavov | 31d373b | 2017-02-14 12:18:44 -0800 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Cheney Ni | 51d80cf | 2019-06-10 21:46:03 +0800 | [diff] [blame] | 1393 | |
| 1394 | if (p_peer->mtu == mtu) return; |
| 1395 | |
Pavlin Radoslavov | 31d373b | 2017-02-14 12:18:44 -0800 | [diff] [blame] | 1396 | p_peer->mtu = mtu; |
Cheney Ni | 51d80cf | 2019-06-10 21:46:03 +0800 | [diff] [blame] | 1397 | if (active_peer_ == p_peer) { |
| 1398 | LOG(INFO) << __func__ << ": update the codec encoder with peer " |
| 1399 | << peer_address << " bta_av_handle: " << loghex(bta_av_handle) |
| 1400 | << ", new MTU: " << mtu; |
| 1401 | // Send a request with NONE config values to update only the MTU. |
| 1402 | SetCodecAudioConfig( |
| 1403 | {.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE, |
| 1404 | .bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE, |
| 1405 | .channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE}); |
| 1406 | } |
Pavlin Radoslavov | 31d373b | 2017-02-14 12:18:44 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1409 | bool BtaAvCo::SetActivePeer(const RawAddress& peer_address) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1410 | VLOG(1) << __func__ << ": peer_address=" << peer_address; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1411 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1412 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
Pavlin Radoslavov | 79506e8 | 2016-09-07 21:57:47 -0700 | [diff] [blame] | 1413 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1414 | if (peer_address.IsEmpty()) { |
| 1415 | // Reset the active peer; |
| 1416 | active_peer_ = nullptr; |
| 1417 | memset(codec_config_, 0, sizeof(codec_config_)); |
| 1418 | return true; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1419 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1420 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1421 | // Find the peer |
| 1422 | BtaAvCoPeer* p_peer = FindPeer(peer_address); |
| 1423 | if (p_peer == nullptr) { |
| 1424 | return false; |
Pavlin Radoslavov | cd02ce9 | 2016-11-04 17:18:51 -0700 | [diff] [blame] | 1425 | } |
Pavlin Radoslavov | c66edf3 | 2016-09-15 17:50:18 -0700 | [diff] [blame] | 1426 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1427 | active_peer_ = p_peer; |
| 1428 | memcpy(codec_config_, active_peer_->codec_config, AVDT_CODEC_SIZE); |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1429 | LOG(INFO) << __func__ << ": codec = " << A2DP_CodecInfoString(codec_config_); |
| 1430 | // report the selected codec configuration of this new active peer. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1431 | ReportSourceCodecState(active_peer_); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1432 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1435 | void BtaAvCo::GetPeerEncoderParameters( |
| 1436 | const RawAddress& peer_address, |
| 1437 | tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1438 | uint16_t min_mtu = 0xFFFF; |
Pavlin Radoslavov | 1200c75 | 2018-05-15 19:21:03 -0700 | [diff] [blame] | 1439 | CHECK(p_peer_params != nullptr) << "Peer address " << peer_address; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1440 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1441 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
Pavlin Radoslavov | b18f03e | 2016-09-13 15:02:52 -0700 | [diff] [blame] | 1442 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1443 | // Compute the MTU |
| 1444 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(peers_); i++) { |
| 1445 | const BtaAvCoPeer* p_peer = &peers_[i]; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1446 | if (!p_peer->opened) continue; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1447 | if (p_peer->addr != peer_address) continue; |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 1448 | if (p_peer->mtu < min_mtu) min_mtu = p_peer->mtu; |
| 1449 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1450 | p_peer_params->peer_mtu = min_mtu; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1451 | p_peer_params->is_peer_edr = btif_av_is_peer_edr(peer_address); |
| 1452 | p_peer_params->peer_supports_3mbps = |
| 1453 | btif_av_peer_supports_3mbps(peer_address); |
Pavlin Radoslavov | 1200c75 | 2018-05-15 19:21:03 -0700 | [diff] [blame] | 1454 | APPL_TRACE_DEBUG( |
| 1455 | "%s: peer_address=%s peer_mtu=%d is_peer_edr=%s peer_supports_3mbps=%s", |
| 1456 | __func__, peer_address.ToString().c_str(), p_peer_params->peer_mtu, |
| 1457 | logbool(p_peer_params->is_peer_edr).c_str(), |
| 1458 | logbool(p_peer_params->peer_supports_3mbps).c_str()); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1459 | } |
| 1460 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1461 | const tA2DP_ENCODER_INTERFACE* BtaAvCo::GetSourceEncoderInterface() { |
| 1462 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 1463 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1464 | return A2DP_GetEncoderInterface(codec_config_); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1467 | const tA2DP_DECODER_INTERFACE* BtaAvCo::GetSinkDecoderInterface() { |
| 1468 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
Bailey Forrest | 7e2ab69 | 2017-06-16 15:38:03 -0700 | [diff] [blame] | 1469 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1470 | return A2DP_GetDecoderInterface(codec_config_); |
Bailey Forrest | 7e2ab69 | 2017-06-16 15:38:03 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1473 | bool BtaAvCo::SetCodecUserConfig( |
| 1474 | const RawAddress& peer_address, |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1475 | const btav_a2dp_codec_config_t& codec_user_config) { |
| 1476 | uint8_t result_codec_config[AVDT_CODEC_SIZE]; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1477 | const BtaAvCoSep* p_sink = nullptr; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1478 | bool restart_input = false; |
| 1479 | bool restart_output = false; |
| 1480 | bool config_updated = false; |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1481 | bool success = true; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1482 | |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1483 | VLOG(1) << __func__ << ": peer_address=" << peer_address |
| 1484 | << " codec_user_config={" << codec_user_config.ToString() << "}"; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1485 | |
| 1486 | BtaAvCoPeer* p_peer = FindPeer(peer_address); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1487 | if (p_peer == nullptr) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1488 | LOG(ERROR) << __func__ << ": cannot find peer " << peer_address |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1489 | << " to configure"; |
| 1490 | success = false; |
| 1491 | goto done; |
| 1492 | } |
| 1493 | |
| 1494 | // Don't call BTA_AvReconfig() prior to retrieving all peer's capabilities |
| 1495 | if ((p_peer->num_rx_sinks != p_peer->num_sinks) && |
| 1496 | (p_peer->num_sup_sinks != BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks))) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1497 | LOG(WARNING) << __func__ << ": peer " << p_peer->addr |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1498 | << " : not all peer's capabilities have been retrieved"; |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1499 | success = false; |
| 1500 | goto done; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // Find the peer SEP codec to use |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1504 | if (codec_user_config.codec_type < BTAV_A2DP_CODEC_INDEX_MAX) { |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1505 | p_sink = FindPeerSink(p_peer, codec_user_config.codec_type); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1506 | } else { |
| 1507 | // Use the current sink codec |
| 1508 | p_sink = p_peer->p_sink; |
| 1509 | } |
| 1510 | if (p_sink == nullptr) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1511 | LOG(ERROR) << __func__ << ": peer " << p_peer->addr |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1512 | << " : cannot find peer SEP to configure for codec type " |
| 1513 | << codec_user_config.codec_type; |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1514 | success = false; |
| 1515 | goto done; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | tA2DP_ENCODER_INIT_PEER_PARAMS peer_params; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1519 | GetPeerEncoderParameters(p_peer->addr, &peer_params); |
| 1520 | if (!p_peer->GetCodecs()->setCodecUserConfig( |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1521 | codec_user_config, &peer_params, p_sink->codec_caps, |
| 1522 | result_codec_config, &restart_input, &restart_output, |
| 1523 | &config_updated)) { |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1524 | success = false; |
| 1525 | goto done; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | if (restart_output) { |
| 1529 | uint8_t num_protect = 0; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1530 | if (ContentProtectEnabled() && p_peer->ContentProtectActive()) { |
| 1531 | num_protect = AVDT_CP_INFO_LEN; |
| 1532 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1533 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1534 | p_sink = SelectSourceCodec(p_peer); |
| 1535 | if (p_sink == nullptr) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1536 | LOG(ERROR) << __func__ << ": peer " << p_peer->addr |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1537 | << " : cannot set up codec for the peer SINK"; |
Pavlin Radoslavov | 8da87be | 2017-08-02 13:52:14 -0700 | [diff] [blame] | 1538 | success = false; |
| 1539 | goto done; |
| 1540 | } |
| 1541 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1542 | p_peer->acceptor = false; |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1543 | VLOG(1) << __func__ << ": call BTA_AvReconfig(" |
| 1544 | << loghex(p_peer->BtaAvHandle()) << ")"; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1545 | BTA_AvReconfig(p_peer->BtaAvHandle(), true, p_sink->sep_info_idx, |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1546 | p_peer->codec_config, num_protect, bta_av_co_cp_scmst); |
| 1547 | } |
| 1548 | |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1549 | done: |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1550 | // We send the upcall if there is no change or the user config failed for |
| 1551 | // current active peer, so the caller would know it failed. If there is no |
| 1552 | // error, the new selected codec configuration would be sent after we are |
| 1553 | // ready to start a new session with the audio HAL. |
| 1554 | // For none active peer, we unconditionally send the upcall, so the caller |
| 1555 | // would always know the result. |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1556 | // NOTE: Currently, the input is restarted by sending an upcall |
| 1557 | // and informing the Media Framework about the change. |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1558 | if (p_peer != nullptr && |
| 1559 | (!restart_output || !success || p_peer != active_peer_)) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1560 | return ReportSourceCodecState(p_peer); |
Pavlin Radoslavov | b0dceae | 2017-11-09 19:30:58 -0800 | [diff] [blame] | 1561 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1562 | |
Pavlin Radoslavov | 494e53e | 2017-01-25 17:00:23 -0800 | [diff] [blame] | 1563 | return success; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1566 | bool BtaAvCo::SetCodecAudioConfig( |
| 1567 | const btav_a2dp_codec_config_t& codec_audio_config) { |
| 1568 | uint8_t result_codec_config[AVDT_CODEC_SIZE]; |
| 1569 | bool restart_output = false; |
| 1570 | bool config_updated = false; |
| 1571 | |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1572 | VLOG(1) << __func__ |
| 1573 | << ": codec_audio_config: " << codec_audio_config.ToString(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1574 | |
| 1575 | // Find the peer that is currently open |
| 1576 | BtaAvCoPeer* p_peer = active_peer_; |
| 1577 | if (p_peer == nullptr) { |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1578 | LOG(ERROR) << __func__ << ": no active peer to configure"; |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
| 1582 | // Don't call BTA_AvReconfig() prior to retrieving all peer's capabilities |
| 1583 | if ((p_peer->num_rx_sinks != p_peer->num_sinks) && |
| 1584 | (p_peer->num_sup_sinks != BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks))) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1585 | LOG(WARNING) << __func__ << ": peer " << p_peer->addr |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1586 | << " : not all peer's capabilities have been retrieved"; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
| 1589 | |
| 1590 | // Use the current sink codec |
| 1591 | const BtaAvCoSep* p_sink = p_peer->p_sink; |
| 1592 | if (p_sink == nullptr) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1593 | LOG(ERROR) << __func__ << ": peer " << p_peer->addr |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1594 | << " : cannot find peer SEP to configure"; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1595 | return false; |
| 1596 | } |
| 1597 | |
| 1598 | tA2DP_ENCODER_INIT_PEER_PARAMS peer_params; |
| 1599 | GetPeerEncoderParameters(p_peer->addr, &peer_params); |
| 1600 | if (!p_peer->GetCodecs()->setCodecAudioConfig( |
| 1601 | codec_audio_config, &peer_params, p_sink->codec_caps, |
| 1602 | result_codec_config, &restart_output, &config_updated)) { |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
| 1606 | if (restart_output) { |
| 1607 | uint8_t num_protect = 0; |
| 1608 | if (ContentProtectEnabled() && p_peer->ContentProtectActive()) { |
| 1609 | num_protect = AVDT_CP_INFO_LEN; |
| 1610 | } |
| 1611 | |
| 1612 | SaveNewCodecConfig(p_peer, result_codec_config, p_sink->num_protect, |
| 1613 | p_sink->protect_info); |
| 1614 | |
Ugo Yu | 06f4224 | 2019-02-15 22:42:39 +0800 | [diff] [blame] | 1615 | p_peer->acceptor = false; |
| 1616 | VLOG(1) << __func__ << ": call BTA_AvReconfig(" |
| 1617 | << loghex(p_peer->BtaAvHandle()) << ")"; |
| 1618 | BTA_AvReconfig(p_peer->BtaAvHandle(), true, p_sink->sep_info_idx, |
| 1619 | p_peer->codec_config, num_protect, bta_av_co_cp_scmst); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | if (config_updated) { |
| 1623 | // NOTE: Currently, the input is restarted by sending an upcall |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1624 | // and informing the Media Framework about the change of selected codec. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1625 | return ReportSourceCodecState(p_peer); |
| 1626 | } |
| 1627 | |
| 1628 | return true; |
| 1629 | } |
| 1630 | |
| 1631 | bool BtaAvCo::ReportSourceCodecState(BtaAvCoPeer* p_peer) { |
| 1632 | btav_a2dp_codec_config_t codec_config; |
| 1633 | std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities; |
| 1634 | std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities; |
| 1635 | |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1636 | VLOG(1) << __func__ << ": peer_address=" << p_peer->addr; |
Pavlin Radoslavov | b27aa7e | 2018-03-16 11:56:37 -0700 | [diff] [blame] | 1637 | A2dpCodecs* codecs = p_peer->GetCodecs(); |
| 1638 | CHECK(codecs != nullptr); |
| 1639 | if (!codecs->getCodecConfigAndCapabilities(&codec_config, |
| 1640 | &codecs_local_capabilities, |
| 1641 | &codecs_selectable_capabilities)) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1642 | LOG(WARNING) << __func__ << ": Peer " << p_peer->addr |
| 1643 | << " : error reporting audio source codec state: cannot get " |
| 1644 | "codec config and capabilities"; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1645 | return false; |
| 1646 | } |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1647 | LOG(INFO) << __func__ << ": peer " << p_peer->addr << " codec_config={" |
| 1648 | << codec_config.ToString() << "}"; |
Pavlin Radoslavov | 2a3b9b5 | 2018-04-30 13:39:20 -0700 | [diff] [blame] | 1649 | btif_av_report_source_codec_state(p_peer->addr, codec_config, |
| 1650 | codecs_local_capabilities, |
| 1651 | codecs_selectable_capabilities); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1652 | return true; |
| 1653 | } |
| 1654 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1655 | bool BtaAvCo::ReportSinkCodecState(BtaAvCoPeer* p_peer) { |
| 1656 | APPL_TRACE_DEBUG("%s: peer_address=%s", __func__, |
| 1657 | p_peer->addr.ToString().c_str()); |
| 1658 | // Nothing to do (for now) |
| 1659 | return true; |
| 1660 | } |
Pavlin Radoslavov | 8757bc7 | 2018-05-23 07:40:09 -0700 | [diff] [blame] | 1661 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1662 | void BtaAvCo::DebugDump(int fd) { |
| 1663 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 1664 | |
Pavlin Radoslavov | 8757bc7 | 2018-05-23 07:40:09 -0700 | [diff] [blame] | 1665 | // |
| 1666 | // Active peer codec-specific stats |
| 1667 | // |
| 1668 | if (active_peer_ != nullptr) { |
| 1669 | A2dpCodecs* a2dp_codecs = active_peer_->GetCodecs(); |
| 1670 | if (a2dp_codecs != nullptr) { |
| 1671 | a2dp_codecs->debug_codec_dump(fd); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return; |
| 1676 | |
| 1677 | dprintf(fd, "\nA2DP Peers State:\n"); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1678 | dprintf(fd, " Active peer: %s\n", |
| 1679 | (active_peer_ != nullptr) ? active_peer_->addr.ToString().c_str() |
| 1680 | : "null"); |
| 1681 | |
| 1682 | for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(peers_); i++) { |
| 1683 | const BtaAvCoPeer& peer = peers_[i]; |
Pavlin Radoslavov | 8757bc7 | 2018-05-23 07:40:09 -0700 | [diff] [blame] | 1684 | if (peer.addr.IsEmpty()) { |
| 1685 | continue; |
| 1686 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1687 | dprintf(fd, " Peer: %s\n", peer.addr.ToString().c_str()); |
| 1688 | dprintf(fd, " Number of sinks: %u\n", peer.num_sinks); |
| 1689 | dprintf(fd, " Number of sources: %u\n", peer.num_sources); |
| 1690 | dprintf(fd, " Number of SEPs: %u\n", peer.num_seps); |
| 1691 | dprintf(fd, " Number of received sinks: %u\n", peer.num_rx_sinks); |
| 1692 | dprintf(fd, " Number of received sources: %u\n", peer.num_rx_sources); |
| 1693 | dprintf(fd, " Number of supported sinks: %u\n", peer.num_sup_sinks); |
| 1694 | dprintf(fd, " Number of supported sources: %u\n", peer.num_sup_sources); |
| 1695 | dprintf(fd, " Acceptor: %s\n", (peer.acceptor) ? "true" : "false"); |
| 1696 | dprintf(fd, " Reconfig needed: %s\n", |
| 1697 | (peer.reconfig_needed) ? "true" : "false"); |
| 1698 | dprintf(fd, " Opened: %s\n", (peer.opened) ? "true" : "false"); |
| 1699 | dprintf(fd, " MTU: %u\n", peer.mtu); |
| 1700 | dprintf(fd, " UUID to connect: 0x%x\n", peer.uuid_to_connect); |
| 1701 | dprintf(fd, " BTA AV handle: %u\n", peer.BtaAvHandle()); |
| 1702 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | bool BtaAvCo::ContentProtectIsScmst(const uint8_t* p_protect_info) { |
| 1706 | APPL_TRACE_DEBUG("%s", __func__); |
| 1707 | |
| 1708 | if (*p_protect_info >= AVDT_CP_LOSC) { |
| 1709 | uint16_t cp_id; |
| 1710 | p_protect_info++; |
| 1711 | STREAM_TO_UINT16(cp_id, p_protect_info); |
| 1712 | if (cp_id == AVDT_CP_SCMS_T_ID) { |
| 1713 | APPL_TRACE_DEBUG("%s: SCMS-T found", __func__); |
| 1714 | return true; |
| 1715 | } |
| 1716 | } |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
| 1720 | bool BtaAvCo::AudioProtectHasScmst(uint8_t num_protect, |
| 1721 | const uint8_t* p_protect_info) { |
| 1722 | APPL_TRACE_DEBUG("%s", __func__); |
| 1723 | while (num_protect--) { |
| 1724 | if (BtaAvCo::ContentProtectIsScmst(p_protect_info)) return true; |
| 1725 | // Move to the next Content Protect schema |
| 1726 | p_protect_info += *p_protect_info + 1; |
| 1727 | } |
| 1728 | APPL_TRACE_DEBUG("%s: SCMS-T not found", __func__); |
| 1729 | return false; |
| 1730 | } |
| 1731 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1732 | bool BtaAvCo::AudioSepHasContentProtection(const BtaAvCoSep* p_sep) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1733 | APPL_TRACE_DEBUG("%s", __func__); |
| 1734 | |
| 1735 | // Check if content protection is enabled for this stream |
| 1736 | if (ContentProtectFlag() != AVDT_CP_SCMS_COPY_FREE) { |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1737 | return BtaAvCo::AudioProtectHasScmst(p_sep->num_protect, |
| 1738 | p_sep->protect_info); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | APPL_TRACE_DEBUG("%s: not required", __func__); |
| 1742 | return true; |
| 1743 | } |
| 1744 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1745 | const BtaAvCoSep* BtaAvCo::SelectSourceCodec(BtaAvCoPeer* p_peer) { |
| 1746 | const BtaAvCoSep* p_sink = nullptr; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1747 | |
| 1748 | // Update all selectable codecs. |
| 1749 | // This is needed to update the selectable parameters for each codec. |
| 1750 | // NOTE: The selectable codec info is used only for informational purpose. |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1751 | UpdateAllSelectableSourceCodecs(p_peer); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1752 | |
| 1753 | // Select the codec |
| 1754 | for (const auto& iter : p_peer->GetCodecs()->orderedSourceCodecs()) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1755 | VLOG(1) << __func__ << ": trying codec " << iter->name(); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1756 | p_sink = AttemptSourceCodecSelection(*iter, p_peer); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1757 | if (p_sink != nullptr) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1758 | VLOG(1) << __func__ << ": selected codec " << iter->name(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1759 | break; |
| 1760 | } |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 1761 | VLOG(1) << __func__ << ": cannot use codec " << iter->name(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1762 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1763 | return p_sink; |
| 1764 | } |
| 1765 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1766 | const BtaAvCoSep* BtaAvCo::SelectSinkCodec(BtaAvCoPeer* p_peer) { |
| 1767 | const BtaAvCoSep* p_source = nullptr; |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1768 | |
| 1769 | // Update all selectable codecs. |
| 1770 | // This is needed to update the selectable parameters for each codec. |
| 1771 | // NOTE: The selectable codec info is used only for informational purpose. |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1772 | UpdateAllSelectableSinkCodecs(p_peer); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1773 | |
| 1774 | // Select the codec |
| 1775 | for (const auto& iter : p_peer->GetCodecs()->orderedSinkCodecs()) { |
| 1776 | APPL_TRACE_DEBUG("%s: trying codec %s", __func__, iter->name().c_str()); |
| 1777 | p_source = AttemptSinkCodecSelection(*iter, p_peer); |
| 1778 | if (p_source != nullptr) { |
| 1779 | APPL_TRACE_DEBUG("%s: selected codec %s", __func__, iter->name().c_str()); |
| 1780 | break; |
| 1781 | } |
| 1782 | APPL_TRACE_DEBUG("%s: cannot use codec %s", __func__, iter->name().c_str()); |
| 1783 | } |
| 1784 | |
| 1785 | // NOTE: Unconditionally dispatch the event to make sure a callback with |
| 1786 | // the most recent codec info is generated. |
| 1787 | ReportSinkCodecState(p_peer); |
| 1788 | |
| 1789 | return p_source; |
| 1790 | } |
| 1791 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1792 | BtaAvCoSep* BtaAvCo::FindPeerSink(BtaAvCoPeer* p_peer, |
| 1793 | btav_a2dp_codec_index_t codec_index) { |
| 1794 | if (codec_index == BTAV_A2DP_CODEC_INDEX_MAX) { |
| 1795 | APPL_TRACE_WARNING("%s: invalid codec index for peer %s", __func__, |
| 1796 | p_peer->addr.ToString().c_str()); |
| 1797 | return nullptr; |
| 1798 | } |
| 1799 | |
| 1800 | // Find the peer Sink for the codec |
| 1801 | for (size_t index = 0; index < p_peer->num_sup_sinks; index++) { |
| 1802 | BtaAvCoSep* p_sink = &p_peer->sinks[index]; |
| 1803 | btav_a2dp_codec_index_t peer_codec_index = |
| 1804 | A2DP_SourceCodecIndex(p_sink->codec_caps); |
| 1805 | if (peer_codec_index != codec_index) { |
| 1806 | continue; |
| 1807 | } |
| 1808 | if (!AudioSepHasContentProtection(p_sink)) { |
| 1809 | APPL_TRACE_DEBUG( |
| 1810 | "%s: peer Sink for codec %s does not support " |
| 1811 | "Content Protection", |
| 1812 | __func__, A2DP_CodecIndexStr(codec_index)); |
| 1813 | continue; |
| 1814 | } |
| 1815 | return p_sink; |
| 1816 | } |
| 1817 | return nullptr; |
| 1818 | } |
| 1819 | |
| 1820 | BtaAvCoSep* BtaAvCo::FindPeerSource(BtaAvCoPeer* p_peer, |
| 1821 | btav_a2dp_codec_index_t codec_index) { |
| 1822 | if (codec_index == BTAV_A2DP_CODEC_INDEX_MAX) { |
| 1823 | APPL_TRACE_WARNING("%s: invalid codec index for peer %s", __func__, |
| 1824 | p_peer->addr.ToString().c_str()); |
| 1825 | return nullptr; |
| 1826 | } |
| 1827 | |
| 1828 | // Find the peer Source for the codec |
| 1829 | for (size_t index = 0; index < p_peer->num_sup_sources; index++) { |
| 1830 | BtaAvCoSep* p_source = &p_peer->sources[index]; |
| 1831 | btav_a2dp_codec_index_t peer_codec_index = |
| 1832 | A2DP_SinkCodecIndex(p_source->codec_caps); |
| 1833 | if (peer_codec_index != codec_index) { |
| 1834 | continue; |
| 1835 | } |
| 1836 | if (!AudioSepHasContentProtection(p_source)) { |
| 1837 | APPL_TRACE_DEBUG( |
| 1838 | "%s: peer Source for codec %s does not support " |
| 1839 | "Content Protection", |
| 1840 | __func__, A2DP_CodecIndexStr(codec_index)); |
| 1841 | continue; |
| 1842 | } |
| 1843 | return p_source; |
| 1844 | } |
| 1845 | return nullptr; |
| 1846 | } |
| 1847 | |
| 1848 | const BtaAvCoSep* BtaAvCo::AttemptSourceCodecSelection( |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1849 | const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1850 | uint8_t new_codec_config[AVDT_CODEC_SIZE]; |
| 1851 | |
| 1852 | APPL_TRACE_DEBUG("%s", __func__); |
| 1853 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1854 | // Find the peer Sink for the codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1855 | BtaAvCoSep* p_sink = FindPeerSink(p_peer, codec_config.codecIndex()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1856 | if (p_sink == nullptr) { |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1857 | APPL_TRACE_DEBUG("%s: peer Sink for codec %s not found", __func__, |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1858 | codec_config.name().c_str()); |
| 1859 | return nullptr; |
| 1860 | } |
| 1861 | if (!p_peer->GetCodecs()->setCodecConfig( |
| 1862 | p_sink->codec_caps, true /* is_capability */, new_codec_config, |
| 1863 | true /* select_current_codec */)) { |
| 1864 | APPL_TRACE_DEBUG("%s: cannot set source codec %s", __func__, |
| 1865 | codec_config.name().c_str()); |
| 1866 | return nullptr; |
| 1867 | } |
| 1868 | p_peer->p_sink = p_sink; |
| 1869 | |
| 1870 | SaveNewCodecConfig(p_peer, new_codec_config, p_sink->num_protect, |
| 1871 | p_sink->protect_info); |
| 1872 | |
| 1873 | return p_sink; |
| 1874 | } |
| 1875 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1876 | const BtaAvCoSep* BtaAvCo::AttemptSinkCodecSelection( |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1877 | const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1878 | uint8_t new_codec_config[AVDT_CODEC_SIZE]; |
| 1879 | |
| 1880 | APPL_TRACE_DEBUG("%s", __func__); |
| 1881 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1882 | // Find the peer Source for the codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1883 | BtaAvCoSep* p_source = FindPeerSource(p_peer, codec_config.codecIndex()); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1884 | if (p_source == nullptr) { |
| 1885 | APPL_TRACE_DEBUG("%s: peer Source for codec %s not found", __func__, |
| 1886 | codec_config.name().c_str()); |
| 1887 | return nullptr; |
| 1888 | } |
| 1889 | if (!p_peer->GetCodecs()->setSinkCodecConfig( |
| 1890 | p_source->codec_caps, true /* is_capability */, new_codec_config, |
| 1891 | true /* select_current_codec */)) { |
| 1892 | APPL_TRACE_DEBUG("%s: cannot set sink codec %s", __func__, |
| 1893 | codec_config.name().c_str()); |
| 1894 | return nullptr; |
| 1895 | } |
| 1896 | p_peer->p_source = p_source; |
| 1897 | |
| 1898 | SaveNewCodecConfig(p_peer, new_codec_config, p_source->num_protect, |
| 1899 | p_source->protect_info); |
| 1900 | |
| 1901 | return p_source; |
| 1902 | } |
| 1903 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1904 | size_t BtaAvCo::UpdateAllSelectableSourceCodecs(BtaAvCoPeer* p_peer) { |
| 1905 | APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str()); |
| 1906 | |
| 1907 | size_t updated_codecs = 0; |
| 1908 | for (const auto& iter : p_peer->GetCodecs()->orderedSourceCodecs()) { |
| 1909 | APPL_TRACE_DEBUG("%s: updating selectable codec %s", __func__, |
| 1910 | iter->name().c_str()); |
| 1911 | if (UpdateSelectableSourceCodec(*iter, p_peer)) { |
| 1912 | updated_codecs++; |
| 1913 | } |
| 1914 | } |
| 1915 | return updated_codecs; |
| 1916 | } |
| 1917 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1918 | bool BtaAvCo::UpdateSelectableSourceCodec(const A2dpCodecConfig& codec_config, |
| 1919 | BtaAvCoPeer* p_peer) { |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1920 | APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1921 | |
| 1922 | // Find the peer Sink for the codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1923 | const BtaAvCoSep* p_sink = FindPeerSink(p_peer, codec_config.codecIndex()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1924 | if (p_sink == nullptr) { |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1925 | // The peer Sink device does not support this codec |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1926 | return false; |
| 1927 | } |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1928 | if (!p_peer->GetCodecs()->setPeerSinkCodecCapabilities(p_sink->codec_caps)) { |
| 1929 | APPL_TRACE_WARNING("%s: cannot update peer %s codec capabilities for %s", |
| 1930 | __func__, p_peer->addr.ToString().c_str(), |
| 1931 | A2DP_CodecName(p_sink->codec_caps)); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1932 | return false; |
| 1933 | } |
| 1934 | return true; |
| 1935 | } |
| 1936 | |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1937 | size_t BtaAvCo::UpdateAllSelectableSinkCodecs(BtaAvCoPeer* p_peer) { |
| 1938 | APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str()); |
| 1939 | |
| 1940 | size_t updated_codecs = 0; |
| 1941 | for (const auto& iter : p_peer->GetCodecs()->orderedSinkCodecs()) { |
| 1942 | APPL_TRACE_DEBUG("%s: updating selectable codec %s", __func__, |
| 1943 | iter->name().c_str()); |
| 1944 | if (UpdateSelectableSinkCodec(*iter, p_peer)) { |
| 1945 | updated_codecs++; |
| 1946 | } |
| 1947 | } |
| 1948 | return updated_codecs; |
| 1949 | } |
| 1950 | |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1951 | bool BtaAvCo::UpdateSelectableSinkCodec(const A2dpCodecConfig& codec_config, |
| 1952 | BtaAvCoPeer* p_peer) { |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1953 | APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1954 | |
| 1955 | // Find the peer Source for the codec |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1956 | const BtaAvCoSep* p_source = |
| 1957 | FindPeerSource(p_peer, codec_config.codecIndex()); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1958 | if (p_source == nullptr) { |
| 1959 | // The peer Source device does not support this codec |
| 1960 | return false; |
| 1961 | } |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 1962 | if (!p_peer->GetCodecs()->setPeerSourceCodecCapabilities( |
| 1963 | p_source->codec_caps)) { |
| 1964 | APPL_TRACE_WARNING("%s: cannot update peer %s codec capabilities for %s", |
| 1965 | __func__, p_peer->addr.ToString().c_str(), |
| 1966 | A2DP_CodecName(p_source->codec_caps)); |
Pavlin Radoslavov | f8dd02a | 2018-03-26 23:30:46 -0700 | [diff] [blame] | 1967 | return false; |
| 1968 | } |
| 1969 | return true; |
| 1970 | } |
| 1971 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1972 | void BtaAvCo::SaveNewCodecConfig(BtaAvCoPeer* p_peer, |
| 1973 | const uint8_t* new_codec_config, |
| 1974 | uint8_t num_protect, |
| 1975 | const uint8_t* p_protect_info) { |
| 1976 | APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str()); |
Pavlin Radoslavov | 25cd054 | 2018-04-06 17:05:47 -0700 | [diff] [blame] | 1977 | APPL_TRACE_DEBUG("%s: codec: %s", __func__, |
| 1978 | A2DP_CodecInfoString(new_codec_config).c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1979 | |
| 1980 | std::lock_guard<std::recursive_mutex> lock(codec_lock_); |
| 1981 | |
| 1982 | memcpy(codec_config_, new_codec_config, sizeof(codec_config_)); |
| 1983 | memcpy(p_peer->codec_config, new_codec_config, AVDT_CODEC_SIZE); |
| 1984 | |
| 1985 | if (ContentProtectEnabled()) { |
| 1986 | // Check if this Sink supports SCMS |
| 1987 | bool cp_active = BtaAvCo::AudioProtectHasScmst(num_protect, p_protect_info); |
| 1988 | p_peer->SetContentProtectActive(cp_active); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | bool BtaAvCo::SetCodecOtaConfig(BtaAvCoPeer* p_peer, |
| 1993 | const uint8_t* p_ota_codec_config, |
| 1994 | uint8_t num_protect, |
| 1995 | const uint8_t* p_protect_info, |
| 1996 | bool* p_restart_output) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1997 | uint8_t result_codec_config[AVDT_CODEC_SIZE]; |
| 1998 | bool restart_input = false; |
| 1999 | bool restart_output = false; |
| 2000 | bool config_updated = false; |
| 2001 | |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 2002 | LOG(INFO) << __func__ << ": peer_address=" << p_peer->addr |
| 2003 | << ", codec: " << A2DP_CodecInfoString(p_ota_codec_config); |
Pavlin Radoslavov | 8da87be | 2017-08-02 13:52:14 -0700 | [diff] [blame] | 2004 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2005 | *p_restart_output = false; |
| 2006 | |
| 2007 | // Find the peer SEP codec to use |
Pavlin Radoslavov | d5a0532 | 2018-04-09 18:01:41 -0700 | [diff] [blame] | 2008 | const BtaAvCoSep* p_sink = |
| 2009 | FindPeerSink(p_peer, A2DP_SourceCodecIndex(p_ota_codec_config)); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2010 | if ((p_peer->num_sup_sinks > 0) && (p_sink == nullptr)) { |
| 2011 | // There are no peer SEPs if we didn't do the discovery procedure yet. |
| 2012 | // We have all the information we need from the peer, so we can |
| 2013 | // proceed with the OTA codec configuration. |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 2014 | LOG(ERROR) << __func__ << ": peer " << p_peer->addr |
| 2015 | << " : cannot find peer SEP to configure"; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2016 | return false; |
| 2017 | } |
| 2018 | |
| 2019 | tA2DP_ENCODER_INIT_PEER_PARAMS peer_params; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2020 | GetPeerEncoderParameters(p_peer->addr, &peer_params); |
| 2021 | if (!p_peer->GetCodecs()->setCodecOtaConfig( |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2022 | p_ota_codec_config, &peer_params, result_codec_config, &restart_input, |
| 2023 | &restart_output, &config_updated)) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 2024 | LOG(ERROR) << __func__ << ": peer " << p_peer->addr |
| 2025 | << " : cannot set OTA config"; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | if (restart_output) { |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 2030 | VLOG(1) << __func__ << ": restart output for codec: " |
| 2031 | << A2DP_CodecInfoString(result_codec_config); |
Pavlin Radoslavov | 8da87be | 2017-08-02 13:52:14 -0700 | [diff] [blame] | 2032 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2033 | *p_restart_output = true; |
| 2034 | p_peer->p_sink = p_sink; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2035 | SaveNewCodecConfig(p_peer, result_codec_config, num_protect, |
| 2036 | p_protect_info); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | if (restart_input || config_updated) { |
| 2040 | // NOTE: Currently, the input is restarted by sending an upcall |
Cheney Ni | 37e8d9e | 2019-08-06 21:51:05 +0800 | [diff] [blame] | 2041 | // and informing the Media Framework about the change of selected codec. |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2042 | ReportSourceCodecState(p_peer); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
Pavlin Radoslavov | a6ba5ac | 2017-01-31 20:51:06 -0800 | [diff] [blame] | 2048 | void bta_av_co_init( |
| 2049 | const std::vector<btav_a2dp_codec_config_t>& codec_priorities) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2050 | bta_av_co_cb.Init(codec_priorities); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 2051 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2052 | |
Pavlin Radoslavov | 7240fe9 | 2018-09-11 16:47:47 -0700 | [diff] [blame] | 2053 | bool bta_av_co_is_supported_codec(btav_a2dp_codec_index_t codec_index) { |
| 2054 | return bta_av_co_cb.IsSupportedCodec(codec_index); |
| 2055 | } |
| 2056 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2057 | A2dpCodecConfig* bta_av_get_a2dp_current_codec(void) { |
| 2058 | return bta_av_co_cb.GetActivePeerCurrentCodec(); |
| 2059 | } |
| 2060 | |
Pavlin Radoslavov | 42840e0 | 2018-02-28 23:34:28 -0800 | [diff] [blame] | 2061 | A2dpCodecConfig* bta_av_get_a2dp_peer_current_codec( |
| 2062 | const RawAddress& peer_address) { |
| 2063 | return bta_av_co_cb.GetPeerCurrentCodec(peer_address); |
| 2064 | } |
| 2065 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2066 | bool bta_av_co_audio_init(btav_a2dp_codec_index_t codec_index, |
| 2067 | AvdtpSepConfig* p_cfg) { |
| 2068 | return A2DP_InitCodecConfig(codec_index, p_cfg); |
| 2069 | } |
| 2070 | |
| 2071 | void bta_av_co_audio_disc_res(tBTA_AV_HNDL bta_av_handle, |
| 2072 | const RawAddress& peer_address, uint8_t num_seps, |
| 2073 | uint8_t num_sinks, uint8_t num_sources, |
| 2074 | uint16_t uuid_local) { |
| 2075 | bta_av_co_cb.ProcessDiscoveryResult(bta_av_handle, peer_address, num_seps, |
| 2076 | num_sinks, num_sources, uuid_local); |
| 2077 | } |
| 2078 | |
| 2079 | tA2DP_STATUS bta_av_co_audio_getconfig(tBTA_AV_HNDL bta_av_handle, |
| 2080 | const RawAddress& peer_address, |
| 2081 | uint8_t* p_codec_info, |
| 2082 | uint8_t* p_sep_info_idx, uint8_t seid, |
| 2083 | uint8_t* p_num_protect, |
| 2084 | uint8_t* p_protect_info) { |
| 2085 | uint16_t peer_uuid = bta_av_co_cb.FindPeerUuid(bta_av_handle); |
| 2086 | |
| 2087 | APPL_TRACE_DEBUG("%s: peer %s bta_av_handle=0x%x peer_uuid=0x%x", __func__, |
| 2088 | peer_address.ToString().c_str(), bta_av_handle, peer_uuid); |
| 2089 | |
| 2090 | switch (peer_uuid) { |
| 2091 | case UUID_SERVCLASS_AUDIO_SOURCE: |
| 2092 | return bta_av_co_cb.ProcessSinkGetConfig( |
| 2093 | bta_av_handle, peer_address, p_codec_info, p_sep_info_idx, seid, |
| 2094 | p_num_protect, p_protect_info); |
| 2095 | case UUID_SERVCLASS_AUDIO_SINK: |
| 2096 | return bta_av_co_cb.ProcessSourceGetConfig( |
| 2097 | bta_av_handle, peer_address, p_codec_info, p_sep_info_idx, seid, |
| 2098 | p_num_protect, p_protect_info); |
| 2099 | default: |
| 2100 | break; |
| 2101 | } |
Pavlin Radoslavov | 148a10d | 2018-04-13 22:07:30 -0700 | [diff] [blame] | 2102 | APPL_TRACE_ERROR( |
| 2103 | "%s: peer %s : Invalid peer UUID: 0x%x for bta_av_handle 0x%x", |
| 2104 | peer_address.ToString().c_str(), peer_uuid, bta_av_handle); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2105 | return A2DP_FAIL; |
| 2106 | } |
| 2107 | |
| 2108 | void bta_av_co_audio_setconfig(tBTA_AV_HNDL bta_av_handle, |
| 2109 | const RawAddress& peer_address, |
| 2110 | const uint8_t* p_codec_info, uint8_t seid, |
| 2111 | uint8_t num_protect, |
| 2112 | const uint8_t* p_protect_info, |
| 2113 | uint8_t t_local_sep, uint8_t avdt_handle) { |
| 2114 | bta_av_co_cb.ProcessSetConfig(bta_av_handle, peer_address, p_codec_info, seid, |
| 2115 | num_protect, p_protect_info, t_local_sep, |
| 2116 | avdt_handle); |
| 2117 | } |
| 2118 | |
| 2119 | void bta_av_co_audio_open(tBTA_AV_HNDL bta_av_handle, |
| 2120 | const RawAddress& peer_address, uint16_t mtu) { |
| 2121 | bta_av_co_cb.ProcessOpen(bta_av_handle, peer_address, mtu); |
| 2122 | } |
| 2123 | |
| 2124 | void bta_av_co_audio_close(tBTA_AV_HNDL bta_av_handle, |
| 2125 | const RawAddress& peer_address) { |
| 2126 | bta_av_co_cb.ProcessClose(bta_av_handle, peer_address); |
| 2127 | } |
| 2128 | |
| 2129 | void bta_av_co_audio_start(tBTA_AV_HNDL bta_av_handle, |
| 2130 | const RawAddress& peer_address, |
| 2131 | const uint8_t* p_codec_info, bool* p_no_rtp_header) { |
| 2132 | bta_av_co_cb.ProcessStart(bta_av_handle, peer_address, p_codec_info, |
| 2133 | p_no_rtp_header); |
| 2134 | } |
| 2135 | |
| 2136 | void bta_av_co_audio_stop(tBTA_AV_HNDL bta_av_handle, |
| 2137 | const RawAddress& peer_address) { |
| 2138 | bta_av_co_cb.ProcessStop(bta_av_handle, peer_address); |
| 2139 | } |
| 2140 | |
| 2141 | BT_HDR* bta_av_co_audio_source_data_path(const uint8_t* p_codec_info, |
| 2142 | uint32_t* p_timestamp) { |
| 2143 | return bta_av_co_cb.GetNextSourceDataPacket(p_codec_info, p_timestamp); |
| 2144 | } |
| 2145 | |
| 2146 | void bta_av_co_audio_drop(tBTA_AV_HNDL bta_av_handle, |
| 2147 | const RawAddress& peer_address) { |
| 2148 | bta_av_co_cb.DataPacketWasDropped(bta_av_handle, peer_address); |
| 2149 | } |
| 2150 | |
| 2151 | void bta_av_co_audio_delay(tBTA_AV_HNDL bta_av_handle, |
| 2152 | const RawAddress& peer_address, uint16_t delay) { |
| 2153 | bta_av_co_cb.ProcessAudioDelay(bta_av_handle, peer_address, delay); |
| 2154 | } |
| 2155 | |
| 2156 | void bta_av_co_audio_update_mtu(tBTA_AV_HNDL bta_av_handle, |
| 2157 | const RawAddress& peer_address, uint16_t mtu) { |
| 2158 | bta_av_co_cb.UpdateMtu(bta_av_handle, peer_address, mtu); |
| 2159 | } |
| 2160 | |
| 2161 | bool bta_av_co_set_active_peer(const RawAddress& peer_address) { |
| 2162 | return bta_av_co_cb.SetActivePeer(peer_address); |
| 2163 | } |
| 2164 | |
| 2165 | void bta_av_co_get_peer_params(const RawAddress& peer_address, |
| 2166 | tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params) { |
| 2167 | bta_av_co_cb.GetPeerEncoderParameters(peer_address, p_peer_params); |
| 2168 | } |
| 2169 | |
| 2170 | const tA2DP_ENCODER_INTERFACE* bta_av_co_get_encoder_interface(void) { |
| 2171 | return bta_av_co_cb.GetSourceEncoderInterface(); |
| 2172 | } |
| 2173 | |
| 2174 | const tA2DP_DECODER_INTERFACE* bta_av_co_get_decoder_interface(void) { |
| 2175 | return bta_av_co_cb.GetSinkDecoderInterface(); |
| 2176 | } |
| 2177 | |
| 2178 | bool bta_av_co_set_codec_user_config( |
| 2179 | const RawAddress& peer_address, |
| 2180 | const btav_a2dp_codec_config_t& codec_user_config) { |
| 2181 | return bta_av_co_cb.SetCodecUserConfig(peer_address, codec_user_config); |
| 2182 | } |
| 2183 | |
| 2184 | bool bta_av_co_set_codec_audio_config( |
| 2185 | const btav_a2dp_codec_config_t& codec_audio_config) { |
| 2186 | return bta_av_co_cb.SetCodecAudioConfig(codec_audio_config); |
| 2187 | } |
| 2188 | |
Sunny Kapdi | d66d954 | 2018-02-12 21:52:50 -0800 | [diff] [blame] | 2189 | bool bta_av_co_content_protect_is_active(const RawAddress& peer_address) { |
| 2190 | BtaAvCoPeer* p_peer = bta_av_co_cb.FindPeer(peer_address); |
| 2191 | CHECK(p_peer != nullptr); |
| 2192 | return p_peer->ContentProtectActive(); |
| 2193 | } |
| 2194 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 2195 | void btif_a2dp_codec_debug_dump(int fd) { bta_av_co_cb.DebugDump(fd); } |