blob: f3d39e2f9ea4e311c5b8b5b8593a96941c0761b0 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2004-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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 Radoslavovd7522292017-11-24 19:12:11 -080026#include <mutex>
27
28#include <base/bind.h>
Jack Hef2af1c42016-12-13 01:59:12 -080029#include <base/logging.h>
Pavlin Radoslavovb18f03e2016-09-13 15:02:52 -070030#include <string.h>
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080031
32#include "bt_target.h"
33
Pavlin Radoslavovf66f4e92016-10-14 15:14:37 -070034#include "a2dp_api.h"
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -080035#include "a2dp_sbc.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include "bta_av_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "bta_av_ci.h"
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080038#include "bta_av_co.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070039#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -080041#include "btif_av.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include "btif_av_co.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080043#include "btif_util.h"
Pavlin Radoslavovc66edf32016-09-15 17:50:18 -070044#include "osi/include/osi.h"
Sunny Kapdid66d9542018-02-12 21:52:50 -080045#include "osi/include/properties.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080046
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080047// Macro to retrieve the number of elements in a statically allocated array
Myles Watson6bd442f2016-10-19 09:50:22 -070048#define BTA_AV_CO_NUM_ELEMENTS(__a) (sizeof(__a) / sizeof((__a)[0]))
The Android Open Source Project5738f832012-12-12 16:00:35 -080049
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080050// 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 Project5738f832012-12-12 16:00:35 -080055
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080056class BtaAvCoSep {
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -080057 public:
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080058 BtaAvCoSep()
59 : sep_info_idx(0), seid(0), codec_caps{}, num_protect(0), protect_info{} {
60 Reset();
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -080061 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080062
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 Radoslavov5ce01162016-12-05 13:02:26 -080079};
The Android Open Source Project5738f832012-12-12 16:00:35 -080080
Pavlin Radoslavovd7522292017-11-24 19:12:11 -080081class 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 Watson6bd442f2016-10-19 09:50:22 -0700104 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800105
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
164class 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 Radoslavov7240fe92018-09-11 16:47:47 -0700182 * 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 Radoslavovd7522292017-11-24 19:12:11 -0800190 * 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 Radoslavov42840e02018-02-28 23:34:28 -0800197 * 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 Radoslavovd7522292017-11-24 19:12:11 -0800205 * 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 Radoslavovd5a05322018-04-09 18:01:41 -0700230 * Peer Sink SEP.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800231 *
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 Radoslavovf8dd02a2018-03-26 23:30:46 -0700442 * 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 Radoslavovd7522292017-11-24 19:12:11 -0800450 * 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 Radoslavovd7522292017-11-24 19:12:11 -0800481 /**
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 Radoslavovd5a05322018-04-09 18:01:41 -0700489 /**
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 Kapdid66d9542018-02-12 21:52:50 -0800509 private:
510 /**
511 * Reset the state.
512 */
513 void Reset();
514
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800515 /**
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 Radoslavovd7522292017-11-24 19:12:11 -0800535 * 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 Radoslavovd5a05322018-04-09 18:01:41 -0700544 const BtaAvCoSep* SelectSourceCodec(BtaAvCoPeer* p_peer);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800545
546 /**
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700547 * 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 Radoslavovd5a05322018-04-09 18:01:41 -0700556 const BtaAvCoSep* SelectSinkCodec(BtaAvCoPeer* p_peer);
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700557
558 /**
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800559 * 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 Radoslavovd5a05322018-04-09 18:01:41 -0700593 * 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 Radoslavovf8dd02a2018-03-26 23:30:46 -0700602 * Update a selectable Source codec with the corresponding codec information
603 * from a Sink peer.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800604 *
605 * @param codec_config the codec config info to identify the codec to update
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700606 * @param p_peer the peer Sink SEP to use
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800607 * @return true if the codec is updated, otherwise false
608 */
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700609 bool UpdateSelectableSourceCodec(const A2dpCodecConfig& codec_config,
610 BtaAvCoPeer* p_peer);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800611
612 /**
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700613 * 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 Radoslavovf8dd02a2018-03-26 23:30:46 -0700622 * 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 Radoslavovd7522292017-11-24 19:12:11 -0800634 *
635 * @param codec_config the codec configuration to use
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700636 * @param p_peer the Sink peer to use
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800637 * @return a pointer to the corresponding SEP Sink entry on success,
638 * otnerwise nullptr
639 */
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700640 const BtaAvCoSep* AttemptSourceCodecSelection(
641 const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800642
643 /**
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700644 * Attempt to select Sink codec configuration for a Source peer.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800645 *
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700646 * @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 Radoslavovd5a05322018-04-09 18:01:41 -0700651 const BtaAvCoSep* AttemptSinkCodecSelection(
652 const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer);
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700653
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 Radoslavovd7522292017-11-24 19:12:11 -0800659 * otherwise false
660 */
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -0700661 bool AudioSepHasContentProtection(const BtaAvCoSep* p_sep);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800662
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
694const 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)
698static const bool kContentProtectEnabled = true;
699#else
700static const bool kContentProtectEnabled = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800701#endif
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800702static BtaAvCo bta_av_co_cb(kContentProtectEnabled);
703
704void BtaAvCoPeer::Init(
705 const std::vector<btav_a2dp_codec_config_t>& codec_priorities) {
706 Reset(bta_av_handle_);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800707 // Reset the current config
708 codecs_ = new A2dpCodecs(codec_priorities);
709 codecs_->init();
710 A2DP_InitDefaultCodec(codec_config);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800711}
712
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800713void 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
745void 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
761void 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 Radoslavov7240fe92018-09-11 16:47:47 -0700780bool 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 Radoslavovd7522292017-11-24 19:12:11 -0800788A2dpCodecConfig* 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 Radoslavov42840e02018-02-28 23:34:28 -0800797A2dpCodecConfig* 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 Radoslavovd7522292017-11-24 19:12:11 -0800807BtaAvCoPeer* 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
817BtaAvCoPeer* BtaAvCo::FindPeer(tBTA_AV_HNDL bta_av_handle) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700818 uint8_t index;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800819
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800820 index = BTA_AV_CO_AUDIO_HANDLE_TO_INDEX(bta_av_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800821
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800822 APPL_TRACE_DEBUG("%s: bta_av_handle = 0x%x index = %d", __func__,
823 bta_av_handle, index);
Pavlin Radoslavov79506e82016-09-07 21:57:47 -0700824
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800825 // 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 Watson6bd442f2016-10-19 09:50:22 -0700831 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800832
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800833 return &peers_[index];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800834}
835
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800836BtaAvCoPeer* 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 Radoslavov148a10d2018-04-13 22:07:30 -0700843 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 Radoslavovd7522292017-11-24 19:12:11 -0800845 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 Project5738f832012-12-12 16:00:35 -0800853}
854
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800855uint16_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 Project5738f832012-12-12 16:00:35 -0800862
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800863void 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 Project5738f832012-12-12 16:00:35 -0800871
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800872 // 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 Watson6bd442f2016-10-19 09:50:22 -0700878 return;
879 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800880
Myles Watson6bd442f2016-10-19 09:50:22 -0700881 /* Sanity check : this should never happen */
882 if (p_peer->opened) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800883 APPL_TRACE_ERROR("%s: peer %s already opened", __func__,
884 peer_address.ToString().c_str());
Myles Watson6bd442f2016-10-19 09:50:22 -0700885 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886
Myles Watson6bd442f2016-10-19 09:50:22 -0700887 /* Copy the discovery results */
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800888 p_peer->addr = peer_address;
889 p_peer->num_sinks = num_sinks;
890 p_peer->num_sources = num_sources;
Myles Watson6bd442f2016-10-19 09:50:22 -0700891 p_peer->num_seps = num_seps;
892 p_peer->num_rx_sinks = 0;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800893 p_peer->num_rx_sources = 0;
Myles Watson6bd442f2016-10-19 09:50:22 -0700894 p_peer->num_sup_sinks = 0;
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700895 p_peer->num_sup_sources = 0;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800896 if (uuid_local == UUID_SERVCLASS_AUDIO_SINK) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700897 p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SOURCE;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800898 } else if (uuid_local == UUID_SERVCLASS_AUDIO_SOURCE) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700899 p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SINK;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800900 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800901}
902
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800903tA2DP_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 Watson6bd442f2016-10-19 09:50:22 -0700909 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 Radoslavov25cd0542018-04-06 17:05:47 -0700913 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
914 A2DP_CodecInfoString(p_codec_info).c_str());
The Android Open Source Project5738f832012-12-12 16:00:35 -0800915
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800916 // 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 Watson6bd442f2016-10-19 09:50:22 -0700922 return A2DP_FAIL;
923 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700924 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 Radoslavovd7522292017-11-24 19:12:11 -0800930 // Check the peer's Sink codec
Myles Watson6bd442f2016-10-19 09:50:22 -0700931 if (A2DP_IsPeerSinkCodecValid(p_codec_info)) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800932 // If there is room for a new one
Myles Watson6bd442f2016-10-19 09:50:22 -0700933 if (p_peer->num_sup_sinks < BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks)) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800934 BtaAvCoSep* p_sink = &p_peer->sinks[p_peer->num_sup_sinks++];
Myles Watson6bd442f2016-10-19 09:50:22 -0700935
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 Radoslavovfeee26c2016-10-25 10:17:56 -0700944 memcpy(p_sink->protect_info, p_protect_info, AVDT_CP_INFO_LEN);
Myles Watson6bd442f2016-10-19 09:50:22 -0700945 } else {
Pavlin Radoslavov148a10d2018-04-13 22:07:30 -0700946 APPL_TRACE_ERROR("%s: peer %s : no more room for Sink info", __func__,
947 p_peer->addr.ToString().c_str());
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700949 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800950
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800951 // Check if this is the last Sink get capabilities or all supported codec
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -0700952 // 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 Radoslavovd5a05322018-04-09 18:01:41 -0700957 APPL_TRACE_DEBUG("%s: last Sink codec reached for peer %s", __func__,
958 p_peer->addr.ToString().c_str());
The Android Open Source Project5738f832012-12-12 16:00:35 -0800959
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800960 // Select the Source codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700961 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 Radoslavovcd02ce92016-11-04 17:18:51 -0700982 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -0700984 // By default, no content protection
985 *p_num_protect = 0;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800986 if (ContentProtectEnabled() && p_peer->ContentProtectActive()) {
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -0700987 *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 Project5738f832012-12-12 16:00:35 -0800990
Pavlin Radoslavovd7522292017-11-24 19:12:11 -0800991 // If acceptor -> reconfig otherwise reply for configuration
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700992 *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 Radoslavovd7522292017-11-24 19:12:11 -0800997 if (p_peer->acceptor) {
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -0700998 if (p_peer->reconfig_needed) {
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -0700999 APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x) for peer %s", __func__,
1000 bta_av_handle, p_peer->addr.ToString().c_str());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001001 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 Project5738f832012-12-12 16:00:35 -08001003 }
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001004 } else {
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001005 memcpy(p_codec_info, p_peer->codec_config, AVDT_CODEC_SIZE);
Myles Watson6bd442f2016-10-19 09:50:22 -07001006 }
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001007
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001008 // 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 Radoslavovcd02ce92016-11-04 17:18:51 -07001013 return A2DP_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014}
1015
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001016tA2DP_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 Radoslavov25cd0542018-04-06 17:05:47 -07001030 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1031 A2DP_CodecInfoString(p_codec_info).c_str());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001032
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 Radoslavov148a10d2018-04-13 22:07:30 -07001065 APPL_TRACE_ERROR("%s: peer %s : no more room for Source info", __func__,
1066 p_peer->addr.ToString().c_str());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001067 }
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 Radoslavovd5a05322018-04-09 18:01:41 -07001076 APPL_TRACE_DEBUG("%s: last Source codec reached for peer %s", __func__,
1077 p_peer->addr.ToString().c_str());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001078
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001079 // Select the Sink codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001080 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 Radoslavovd7522292017-11-24 19:12:11 -08001101 }
1102
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001103 // By default, no content protection
1104 *p_num_protect = 0;
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001105 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 Radoslavovd7522292017-11-24 19:12:11 -08001108 }
1109
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001110 // If acceptor -> reconfig otherwise reply for configuration
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001111 *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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001116 if (p_peer->acceptor) {
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001117 if (p_peer->reconfig_needed) {
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001118 APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x) for peer %s", __func__,
1119 bta_av_handle, p_peer->addr.ToString().c_str());
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001120 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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001124 memcpy(p_codec_info, p_peer->codec_config, AVDT_CODEC_SIZE);
1125 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001126
1127 return A2DP_SUCCESS;
1128}
1129
1130void 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 Radoslavovcd02ce92016-11-04 17:18:51 -07001134 const uint8_t* p_protect_info,
1135 uint8_t t_local_sep, uint8_t avdt_handle) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001136 tA2DP_STATUS status = A2DP_SUCCESS;
1137 uint8_t category = A2DP_SUCCESS;
Pavlin Radoslavov82fcd302016-10-23 20:39:46 -07001138 bool reconfig_needed = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001140 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 Watson6bd442f2016-10-19 09:50:22 -07001145 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 Radoslavovd7522292017-11-24 19:12:11 -08001148 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 Watson6bd442f2016-10-19 09:50:22 -07001150 p_protect_info[2]);
Pavlin Radoslavov25cd0542018-04-06 17:05:47 -07001151 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1152 A2DP_CodecInfoString(p_codec_info).c_str());
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001154 // 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 Watson6bd442f2016-10-19 09:50:22 -07001163 return;
1164 }
1165
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001166 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 Watson6bd442f2016-10-19 09:50:22 -07001171
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001172 // Sanity check: should not be opened at this point
Myles Watson6bd442f2016-10-19 09:50:22 -07001173 if (p_peer->opened) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001174 APPL_TRACE_ERROR("%s: peer %s already in use", __func__,
1175 p_peer->addr.ToString().c_str());
Myles Watson6bd442f2016-10-19 09:50:22 -07001176 }
1177
1178 if (num_protect != 0) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001179 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 Watson6bd442f2016-10-19 09:50:22 -07001191 status = A2DP_BAD_CP_TYPE;
1192 category = AVDT_ASC_PROTECT;
1193 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001194 }
1195
1196 if (status == A2DP_SUCCESS) {
Pavlin Radoslavov82fcd302016-10-23 20:39:46 -07001197 bool codec_config_supported = false;
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001198
Myles Watson6bd442f2016-10-19 09:50:22 -07001199 if (t_local_sep == AVDT_TSEP_SNK) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001200 APPL_TRACE_DEBUG("%s: peer %s is A2DP Source", __func__,
1201 p_peer->addr.ToString().c_str());
Pavlin Radoslavov82fcd302016-10-23 20:39:46 -07001202 codec_config_supported = A2DP_IsSinkCodecSupported(p_codec_info);
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001203 if (codec_config_supported) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001204 // If Peer is Source, and our config subset matches with what is
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001205 // requested by peer, then just accept what peer wants.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001206 SaveNewCodecConfig(p_peer, p_codec_info, num_protect, p_protect_info);
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001207 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001208 }
1209 if (t_local_sep == AVDT_TSEP_SRC) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001210 APPL_TRACE_DEBUG("%s: peer %s is A2DP SINK", __func__,
1211 p_peer->addr.ToString().c_str());
Pavlin Radoslavov1c21a922018-03-27 00:17:29 -07001212 // 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 Radoslavovd7522292017-11-24 19:12:11 -08001215 if ((p_peer->GetCodecs() == nullptr) ||
1216 !SetCodecOtaConfig(p_peer, p_codec_info, num_protect, p_protect_info,
Pavlin Radoslavov1c21a922018-03-27 00:17:29 -07001217 &dummy_restart_output)) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001218 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 Radoslavovcd02ce92016-11-04 17:18:51 -07001221 } else {
1222 codec_config_supported = true;
1223 // Check if reconfiguration is needed
Pavlin Radoslavov1c21a922018-03-27 00:17:29 -07001224 if (((num_protect == 1) && !p_peer->ContentProtectActive())) {
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001225 reconfig_needed = true;
1226 }
1227 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001228 }
1229
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001230 // Check if codec configuration is supported
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001231 if (!codec_config_supported) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001232 category = AVDT_ASC_CODEC;
1233 status = A2DP_WRONG_CODEC;
1234 }
1235 }
1236
1237 if (status != A2DP_SUCCESS) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001238 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 Watson6bd442f2016-10-19 09:50:22 -07001243 return;
1244 }
1245
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001246 // Mark that this is an acceptor peer
1247 p_peer->acceptor = true;
Pavlin Radoslavov82fcd302016-10-23 20:39:46 -07001248 p_peer->reconfig_needed = reconfig_needed;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001249 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 Radoslavov82fcd302016-10-23 20:39:46 -07001253 reconfig_needed, avdt_handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254}
1255
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001256void 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 Project5738f832012-12-12 16:00:35 -08001260
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001261 // 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 Project5738f832012-12-12 16:00:35 -08001271
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001272 // The first connected peer becomes the active peer
1273 if (active_peer_ == nullptr) {
1274 active_peer_ = p_peer;
Myles Watson6bd442f2016-10-19 09:50:22 -07001275 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001276}
1277
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001278void 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 Panickeref673c52016-12-21 15:37:34 -08001282 btif_av_reset_audio_delay();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001283
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001284 // 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 Watson6bd442f2016-10-19 09:50:22 -07001291 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001292 // 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 Project5738f832012-12-12 16:00:35 -08001298}
1299
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001300void 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 Project5738f832012-12-12 16:00:35 -08001321}
1322
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001323void 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 Project5738f832012-12-12 16:00:35 -08001328}
1329
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001330BT_HDR* BtaAvCo::GetNextSourceDataPacket(const uint8_t* p_codec_info,
1331 uint32_t* p_timestamp) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001332 BT_HDR* p_buf;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08001333
Myles Watson6bd442f2016-10-19 09:50:22 -07001334 APPL_TRACE_DEBUG("%s: codec: %s", __func__, A2DP_CodecName(p_codec_info));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335
Myles Watson6bd442f2016-10-19 09:50:22 -07001336 p_buf = btif_a2dp_source_audio_readbuf();
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001337 if (p_buf == nullptr) return nullptr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001338
Myles Watson6bd442f2016-10-19 09:50:22 -07001339 /*
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 Radoslavovc66edf32016-09-15 17:50:18 -07001353
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001354 if (ContentProtectEnabled() && (active_peer_ != nullptr) &&
1355 active_peer_->ContentProtectActive()) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001356 p_buf->len++;
1357 p_buf->offset--;
1358 uint8_t* p = (uint8_t*)(p_buf + 1) + p_buf->offset;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001359 *p = ContentProtectFlag();
Myles Watson6bd442f2016-10-19 09:50:22 -07001360 }
Pavlin Radoslavovc66edf32016-09-15 17:50:18 -07001361
Myles Watson6bd442f2016-10-19 09:50:22 -07001362 return p_buf;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363}
1364
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001365void 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 Project5738f832012-12-12 16:00:35 -08001369}
1370
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001371void BtaAvCo::ProcessAudioDelay(tBTA_AV_HNDL bta_av_handle,
1372 const RawAddress& peer_address,
1373 uint16_t delay) {
Ajay Panickeref673c52016-12-21 15:37:34 -08001374 APPL_TRACE_DEBUG("%s: peer %s bta_av_handle: 0x%x delay:0x%x", __func__,
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001375 peer_address.ToString().c_str(), bta_av_handle, delay);
Ajay Panickeref673c52016-12-21 15:37:34 -08001376
1377 btif_av_set_audio_delay(delay);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378}
1379
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001380void 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 Radoslavov31d373b2017-02-14 12:18:44 -08001384
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001385 // 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 Radoslavov31d373b2017-02-14 12:18:44 -08001391 return;
1392 }
Cheney Ni51d80cf2019-06-10 21:46:03 +08001393
1394 if (p_peer->mtu == mtu) return;
1395
Pavlin Radoslavov31d373b2017-02-14 12:18:44 -08001396 p_peer->mtu = mtu;
Cheney Ni51d80cf2019-06-10 21:46:03 +08001397 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 Radoslavov31d373b2017-02-14 12:18:44 -08001407}
1408
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001409bool BtaAvCo::SetActivePeer(const RawAddress& peer_address) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001410 VLOG(1) << __func__ << ": peer_address=" << peer_address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001411
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001412 std::lock_guard<std::recursive_mutex> lock(codec_lock_);
Pavlin Radoslavov79506e82016-09-07 21:57:47 -07001413
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001414 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 Watson6bd442f2016-10-19 09:50:22 -07001419 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001420
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001421 // Find the peer
1422 BtaAvCoPeer* p_peer = FindPeer(peer_address);
1423 if (p_peer == nullptr) {
1424 return false;
Pavlin Radoslavovcd02ce92016-11-04 17:18:51 -07001425 }
Pavlin Radoslavovc66edf32016-09-15 17:50:18 -07001426
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001427 active_peer_ = p_peer;
1428 memcpy(codec_config_, active_peer_->codec_config, AVDT_CODEC_SIZE);
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001429 LOG(INFO) << __func__ << ": codec = " << A2DP_CodecInfoString(codec_config_);
1430 // report the selected codec configuration of this new active peer.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001431 ReportSourceCodecState(active_peer_);
Myles Watson6bd442f2016-10-19 09:50:22 -07001432 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001433}
1434
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001435void BtaAvCo::GetPeerEncoderParameters(
1436 const RawAddress& peer_address,
1437 tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001438 uint16_t min_mtu = 0xFFFF;
Pavlin Radoslavov1200c752018-05-15 19:21:03 -07001439 CHECK(p_peer_params != nullptr) << "Peer address " << peer_address;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001440
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001441 std::lock_guard<std::recursive_mutex> lock(codec_lock_);
Pavlin Radoslavovb18f03e2016-09-13 15:02:52 -07001442
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001443 // 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 Watson6bd442f2016-10-19 09:50:22 -07001446 if (!p_peer->opened) continue;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001447 if (p_peer->addr != peer_address) continue;
Myles Watson6bd442f2016-10-19 09:50:22 -07001448 if (p_peer->mtu < min_mtu) min_mtu = p_peer->mtu;
1449 }
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001450 p_peer_params->peer_mtu = min_mtu;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001451 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 Radoslavov1200c752018-05-15 19:21:03 -07001454 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 Radoslavov4ebaa862016-11-22 11:42:11 -08001459}
1460
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001461const tA2DP_ENCODER_INTERFACE* BtaAvCo::GetSourceEncoderInterface() {
1462 std::lock_guard<std::recursive_mutex> lock(codec_lock_);
Pavlin Radoslavov1394c192016-10-02 18:34:46 -07001463
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001464 return A2DP_GetEncoderInterface(codec_config_);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001465}
1466
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001467const tA2DP_DECODER_INTERFACE* BtaAvCo::GetSinkDecoderInterface() {
1468 std::lock_guard<std::recursive_mutex> lock(codec_lock_);
Bailey Forrest7e2ab692017-06-16 15:38:03 -07001469
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001470 return A2DP_GetDecoderInterface(codec_config_);
Bailey Forrest7e2ab692017-06-16 15:38:03 -07001471}
1472
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001473bool BtaAvCo::SetCodecUserConfig(
1474 const RawAddress& peer_address,
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001475 const btav_a2dp_codec_config_t& codec_user_config) {
1476 uint8_t result_codec_config[AVDT_CODEC_SIZE];
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001477 const BtaAvCoSep* p_sink = nullptr;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001478 bool restart_input = false;
1479 bool restart_output = false;
1480 bool config_updated = false;
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001481 bool success = true;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001482
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001483 VLOG(1) << __func__ << ": peer_address=" << peer_address
1484 << " codec_user_config={" << codec_user_config.ToString() << "}";
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001485
1486 BtaAvCoPeer* p_peer = FindPeer(peer_address);
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001487 if (p_peer == nullptr) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001488 LOG(ERROR) << __func__ << ": cannot find peer " << peer_address
Ugo Yu06f42242019-02-15 22:42:39 +08001489 << " 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 Ni37e8d9e2019-08-06 21:51:05 +08001497 LOG(WARNING) << __func__ << ": peer " << p_peer->addr
Ugo Yu06f42242019-02-15 22:42:39 +08001498 << " : not all peer's capabilities have been retrieved";
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001499 success = false;
1500 goto done;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001501 }
1502
1503 // Find the peer SEP codec to use
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001504 if (codec_user_config.codec_type < BTAV_A2DP_CODEC_INDEX_MAX) {
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001505 p_sink = FindPeerSink(p_peer, codec_user_config.codec_type);
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001506 } else {
1507 // Use the current sink codec
1508 p_sink = p_peer->p_sink;
1509 }
1510 if (p_sink == nullptr) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001511 LOG(ERROR) << __func__ << ": peer " << p_peer->addr
Ugo Yu06f42242019-02-15 22:42:39 +08001512 << " : cannot find peer SEP to configure for codec type "
1513 << codec_user_config.codec_type;
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001514 success = false;
1515 goto done;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001516 }
1517
1518 tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001519 GetPeerEncoderParameters(p_peer->addr, &peer_params);
1520 if (!p_peer->GetCodecs()->setCodecUserConfig(
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001521 codec_user_config, &peer_params, p_sink->codec_caps,
1522 result_codec_config, &restart_input, &restart_output,
1523 &config_updated)) {
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001524 success = false;
1525 goto done;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001526 }
1527
1528 if (restart_output) {
1529 uint8_t num_protect = 0;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001530 if (ContentProtectEnabled() && p_peer->ContentProtectActive()) {
1531 num_protect = AVDT_CP_INFO_LEN;
1532 }
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001533
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001534 p_sink = SelectSourceCodec(p_peer);
1535 if (p_sink == nullptr) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001536 LOG(ERROR) << __func__ << ": peer " << p_peer->addr
Ugo Yu06f42242019-02-15 22:42:39 +08001537 << " : cannot set up codec for the peer SINK";
Pavlin Radoslavov8da87be2017-08-02 13:52:14 -07001538 success = false;
1539 goto done;
1540 }
1541
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001542 p_peer->acceptor = false;
Ugo Yu06f42242019-02-15 22:42:39 +08001543 VLOG(1) << __func__ << ": call BTA_AvReconfig("
1544 << loghex(p_peer->BtaAvHandle()) << ")";
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001545 BTA_AvReconfig(p_peer->BtaAvHandle(), true, p_sink->sep_info_idx,
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001546 p_peer->codec_config, num_protect, bta_av_co_cp_scmst);
1547 }
1548
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001549done:
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001550 // 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 Radoslavov494e53e2017-01-25 17:00:23 -08001556 // NOTE: Currently, the input is restarted by sending an upcall
1557 // and informing the Media Framework about the change.
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001558 if (p_peer != nullptr &&
1559 (!restart_output || !success || p_peer != active_peer_)) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001560 return ReportSourceCodecState(p_peer);
Pavlin Radoslavovb0dceae2017-11-09 19:30:58 -08001561 }
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001562
Pavlin Radoslavov494e53e2017-01-25 17:00:23 -08001563 return success;
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08001564}
1565
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001566bool 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 Yu06f42242019-02-15 22:42:39 +08001572 VLOG(1) << __func__
1573 << ": codec_audio_config: " << codec_audio_config.ToString();
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001574
1575 // Find the peer that is currently open
1576 BtaAvCoPeer* p_peer = active_peer_;
1577 if (p_peer == nullptr) {
Ugo Yu06f42242019-02-15 22:42:39 +08001578 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 Ni37e8d9e2019-08-06 21:51:05 +08001585 LOG(WARNING) << __func__ << ": peer " << p_peer->addr
Ugo Yu06f42242019-02-15 22:42:39 +08001586 << " : not all peer's capabilities have been retrieved";
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001587 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 Ni37e8d9e2019-08-06 21:51:05 +08001593 LOG(ERROR) << __func__ << ": peer " << p_peer->addr
Ugo Yu06f42242019-02-15 22:42:39 +08001594 << " : cannot find peer SEP to configure";
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001595 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 Yu06f42242019-02-15 22:42:39 +08001615 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 Radoslavovd7522292017-11-24 19:12:11 -08001620 }
1621
1622 if (config_updated) {
1623 // NOTE: Currently, the input is restarted by sending an upcall
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001624 // and informing the Media Framework about the change of selected codec.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001625 return ReportSourceCodecState(p_peer);
1626 }
1627
1628 return true;
1629}
1630
1631bool 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 Ni37e8d9e2019-08-06 21:51:05 +08001636 VLOG(1) << __func__ << ": peer_address=" << p_peer->addr;
Pavlin Radoslavovb27aa7e2018-03-16 11:56:37 -07001637 A2dpCodecs* codecs = p_peer->GetCodecs();
1638 CHECK(codecs != nullptr);
1639 if (!codecs->getCodecConfigAndCapabilities(&codec_config,
1640 &codecs_local_capabilities,
1641 &codecs_selectable_capabilities)) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001642 LOG(WARNING) << __func__ << ": Peer " << p_peer->addr
1643 << " : error reporting audio source codec state: cannot get "
1644 "codec config and capabilities";
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001645 return false;
1646 }
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001647 LOG(INFO) << __func__ << ": peer " << p_peer->addr << " codec_config={"
1648 << codec_config.ToString() << "}";
Pavlin Radoslavov2a3b9b52018-04-30 13:39:20 -07001649 btif_av_report_source_codec_state(p_peer->addr, codec_config,
1650 codecs_local_capabilities,
1651 codecs_selectable_capabilities);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001652 return true;
1653}
1654
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001655bool 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 Radoslavov8757bc72018-05-23 07:40:09 -07001661
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001662void BtaAvCo::DebugDump(int fd) {
1663 std::lock_guard<std::recursive_mutex> lock(codec_lock_);
1664
Pavlin Radoslavov8757bc72018-05-23 07:40:09 -07001665 //
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 Radoslavovd7522292017-11-24 19:12:11 -08001678 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 Radoslavov8757bc72018-05-23 07:40:09 -07001684 if (peer.addr.IsEmpty()) {
1685 continue;
1686 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001687 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 Radoslavovd7522292017-11-24 19:12:11 -08001703}
1704
1705bool 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
1720bool 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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001732bool BtaAvCo::AudioSepHasContentProtection(const BtaAvCoSep* p_sep) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001733 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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001737 return BtaAvCo::AudioProtectHasScmst(p_sep->num_protect,
1738 p_sep->protect_info);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001739 }
1740
1741 APPL_TRACE_DEBUG("%s: not required", __func__);
1742 return true;
1743}
1744
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001745const BtaAvCoSep* BtaAvCo::SelectSourceCodec(BtaAvCoPeer* p_peer) {
1746 const BtaAvCoSep* p_sink = nullptr;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001747
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 Radoslavovd5a05322018-04-09 18:01:41 -07001751 UpdateAllSelectableSourceCodecs(p_peer);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001752
1753 // Select the codec
1754 for (const auto& iter : p_peer->GetCodecs()->orderedSourceCodecs()) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001755 VLOG(1) << __func__ << ": trying codec " << iter->name();
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001756 p_sink = AttemptSourceCodecSelection(*iter, p_peer);
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001757 if (p_sink != nullptr) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001758 VLOG(1) << __func__ << ": selected codec " << iter->name();
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001759 break;
1760 }
Cheney Ni37e8d9e2019-08-06 21:51:05 +08001761 VLOG(1) << __func__ << ": cannot use codec " << iter->name();
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001762 }
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001763 return p_sink;
1764}
1765
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001766const BtaAvCoSep* BtaAvCo::SelectSinkCodec(BtaAvCoPeer* p_peer) {
1767 const BtaAvCoSep* p_source = nullptr;
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001768
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 Radoslavovd5a05322018-04-09 18:01:41 -07001772 UpdateAllSelectableSinkCodecs(p_peer);
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001773
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 Radoslavovd5a05322018-04-09 18:01:41 -07001792BtaAvCoSep* 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
1820BtaAvCoSep* 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
1848const BtaAvCoSep* BtaAvCo::AttemptSourceCodecSelection(
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001849 const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001850 uint8_t new_codec_config[AVDT_CODEC_SIZE];
1851
1852 APPL_TRACE_DEBUG("%s", __func__);
1853
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001854 // Find the peer Sink for the codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001855 BtaAvCoSep* p_sink = FindPeerSink(p_peer, codec_config.codecIndex());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001856 if (p_sink == nullptr) {
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001857 APPL_TRACE_DEBUG("%s: peer Sink for codec %s not found", __func__,
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001858 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 Radoslavovd5a05322018-04-09 18:01:41 -07001876const BtaAvCoSep* BtaAvCo::AttemptSinkCodecSelection(
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001877 const A2dpCodecConfig& codec_config, BtaAvCoPeer* p_peer) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001878 uint8_t new_codec_config[AVDT_CODEC_SIZE];
1879
1880 APPL_TRACE_DEBUG("%s", __func__);
1881
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001882 // Find the peer Source for the codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001883 BtaAvCoSep* p_source = FindPeerSource(p_peer, codec_config.codecIndex());
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001884 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 Radoslavovd5a05322018-04-09 18:01:41 -07001904size_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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001918bool BtaAvCo::UpdateSelectableSourceCodec(const A2dpCodecConfig& codec_config,
1919 BtaAvCoPeer* p_peer) {
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001920 APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str());
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001921
1922 // Find the peer Sink for the codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001923 const BtaAvCoSep* p_sink = FindPeerSink(p_peer, codec_config.codecIndex());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001924 if (p_sink == nullptr) {
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001925 // The peer Sink device does not support this codec
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001926 return false;
1927 }
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001928 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 Radoslavovd7522292017-11-24 19:12:11 -08001932 return false;
1933 }
1934 return true;
1935}
1936
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001937size_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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001951bool BtaAvCo::UpdateSelectableSinkCodec(const A2dpCodecConfig& codec_config,
1952 BtaAvCoPeer* p_peer) {
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001953 APPL_TRACE_DEBUG("%s: peer %s", __func__, p_peer->addr.ToString().c_str());
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001954
1955 // Find the peer Source for the codec
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001956 const BtaAvCoSep* p_source =
1957 FindPeerSource(p_peer, codec_config.codecIndex());
Pavlin Radoslavovf8dd02a2018-03-26 23:30:46 -07001958 if (p_source == nullptr) {
1959 // The peer Source device does not support this codec
1960 return false;
1961 }
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07001962 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 Radoslavovf8dd02a2018-03-26 23:30:46 -07001967 return false;
1968 }
1969 return true;
1970}
1971
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001972void 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 Radoslavov25cd0542018-04-06 17:05:47 -07001977 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1978 A2DP_CodecInfoString(new_codec_config).c_str());
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08001979
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
1992bool 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 Radoslavov5ce01162016-12-05 13:02:26 -08001997 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 Ni37e8d9e2019-08-06 21:51:05 +08002002 LOG(INFO) << __func__ << ": peer_address=" << p_peer->addr
2003 << ", codec: " << A2DP_CodecInfoString(p_ota_codec_config);
Pavlin Radoslavov8da87be2017-08-02 13:52:14 -07002004
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002005 *p_restart_output = false;
2006
2007 // Find the peer SEP codec to use
Pavlin Radoslavovd5a05322018-04-09 18:01:41 -07002008 const BtaAvCoSep* p_sink =
2009 FindPeerSink(p_peer, A2DP_SourceCodecIndex(p_ota_codec_config));
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002010 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 Ni37e8d9e2019-08-06 21:51:05 +08002014 LOG(ERROR) << __func__ << ": peer " << p_peer->addr
2015 << " : cannot find peer SEP to configure";
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002016 return false;
2017 }
2018
2019 tA2DP_ENCODER_INIT_PEER_PARAMS peer_params;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002020 GetPeerEncoderParameters(p_peer->addr, &peer_params);
2021 if (!p_peer->GetCodecs()->setCodecOtaConfig(
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002022 p_ota_codec_config, &peer_params, result_codec_config, &restart_input,
2023 &restart_output, &config_updated)) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08002024 LOG(ERROR) << __func__ << ": peer " << p_peer->addr
2025 << " : cannot set OTA config";
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002026 return false;
2027 }
2028
2029 if (restart_output) {
Cheney Ni37e8d9e2019-08-06 21:51:05 +08002030 VLOG(1) << __func__ << ": restart output for codec: "
2031 << A2DP_CodecInfoString(result_codec_config);
Pavlin Radoslavov8da87be2017-08-02 13:52:14 -07002032
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002033 *p_restart_output = true;
2034 p_peer->p_sink = p_sink;
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002035 SaveNewCodecConfig(p_peer, result_codec_config, num_protect,
2036 p_protect_info);
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002037 }
2038
2039 if (restart_input || config_updated) {
2040 // NOTE: Currently, the input is restarted by sending an upcall
Cheney Ni37e8d9e2019-08-06 21:51:05 +08002041 // and informing the Media Framework about the change of selected codec.
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002042 ReportSourceCodecState(p_peer);
Pavlin Radoslavov5ce01162016-12-05 13:02:26 -08002043 }
2044
2045 return true;
2046}
2047
Pavlin Radoslavova6ba5ac2017-01-31 20:51:06 -08002048void bta_av_co_init(
2049 const std::vector<btav_a2dp_codec_config_t>& codec_priorities) {
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002050 bta_av_co_cb.Init(codec_priorities);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002051}
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002052
Pavlin Radoslavov7240fe92018-09-11 16:47:47 -07002053bool 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 Radoslavovd7522292017-11-24 19:12:11 -08002057A2dpCodecConfig* bta_av_get_a2dp_current_codec(void) {
2058 return bta_av_co_cb.GetActivePeerCurrentCodec();
2059}
2060
Pavlin Radoslavov42840e02018-02-28 23:34:28 -08002061A2dpCodecConfig* bta_av_get_a2dp_peer_current_codec(
2062 const RawAddress& peer_address) {
2063 return bta_av_co_cb.GetPeerCurrentCodec(peer_address);
2064}
2065
Pavlin Radoslavovd7522292017-11-24 19:12:11 -08002066bool 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
2071void 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
2079tA2DP_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 Radoslavov148a10d2018-04-13 22:07:30 -07002102 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 Radoslavovd7522292017-11-24 19:12:11 -08002105 return A2DP_FAIL;
2106}
2107
2108void 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
2119void 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
2124void 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
2129void 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
2136void 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
2141BT_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
2146void 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
2151void 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
2156void 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
2161bool bta_av_co_set_active_peer(const RawAddress& peer_address) {
2162 return bta_av_co_cb.SetActivePeer(peer_address);
2163}
2164
2165void 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
2170const tA2DP_ENCODER_INTERFACE* bta_av_co_get_encoder_interface(void) {
2171 return bta_av_co_cb.GetSourceEncoderInterface();
2172}
2173
2174const tA2DP_DECODER_INTERFACE* bta_av_co_get_decoder_interface(void) {
2175 return bta_av_co_cb.GetSinkDecoderInterface();
2176}
2177
2178bool 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
2184bool 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 Kapdid66d9542018-02-12 21:52:50 -08002189bool 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 Radoslavovd7522292017-11-24 19:12:11 -08002195void btif_a2dp_codec_debug_dump(int fd) { bta_av_co_cb.DebugDump(fd); }