1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // 18 // A2DP Codecs API 19 // 20 21 #ifndef A2DP_CODEC_API_H 22 #define A2DP_CODEC_API_H 23 24 #include <hardware/bt_av.h> 25 #include <stddef.h> 26 #include <stdint.h> 27 #include <string.h> 28 29 #include <functional> 30 #include <list> 31 #include <map> 32 #include <mutex> 33 #include <string> 34 35 #include "a2dp_api.h" 36 #include "audio_a2dp_hw/include/audio_a2dp_hw.h" 37 #include "avdt_api.h" 38 #include "stack/include/bt_hdr.h" 39 #include "types/raw_address.h" 40 41 class tBT_A2DP_OFFLOAD; 42 43 /** 44 * Structure used to initialize the A2DP encoder with A2DP peer information 45 */ 46 typedef struct { 47 bool is_peer_edr; // True if the A2DP peer supports EDR 48 bool peer_supports_3mbps; // True if the A2DP peer supports 3 Mbps EDR 49 uint16_t peer_mtu; // MTU of the A2DP peer 50 } tA2DP_ENCODER_INIT_PEER_PARAMS; 51 52 class A2dpCodecConfig { 53 friend class A2dpCodecs; 54 55 public: 56 // Creates a codec entry. The selected codec is defined by |codec_index|, 57 // Returns the codec entry on success, otherwise nullptr. 58 static A2dpCodecConfig* createCodec( 59 btav_a2dp_codec_index_t codec_index, 60 btav_a2dp_codec_priority_t codec_priority = 61 BTAV_A2DP_CODEC_PRIORITY_DEFAULT); 62 63 virtual ~A2dpCodecConfig() = 0; 64 65 // Gets the pre-defined codec index. codecIndex()66 btav_a2dp_codec_index_t codecIndex() const { return codec_index_; } 67 68 // Gets the codec name. name()69 const std::string& name() const { return name_; } 70 71 // Gets the current priority of the codec. codecPriority()72 btav_a2dp_codec_priority_t codecPriority() const { return codec_priority_; } 73 74 // gets current OTA codec specific config to |p_a2dp_offload->codec_info|. 75 // Returns true if the current codec config is valid and copied, 76 // otherwise false. 77 bool getCodecSpecificConfig(tBT_A2DP_OFFLOAD* p_a2dp_offload); 78 79 // Gets the bitRate for the A2DP codec. 80 // Returns the bitrate of current codec configuration, or 0 if not configured 81 int getTrackBitRate() const; 82 83 // Copies out the current OTA codec config to |p_codec_info|. 84 // Returns true if the current codec config is valid and copied, 85 // otherwise false. 86 bool copyOutOtaCodecConfig(uint8_t* p_codec_info); 87 88 // Gets the current codec configuration. 89 // Returns a copy of the current codec configuration. 90 btav_a2dp_codec_config_t getCodecConfig(); 91 92 // Gets the current codec capability. 93 // The capability is computed by intersecting the local codec's capability 94 // and the peer's codec capability. However, if there is an explicit user 95 // configuration for some of the parameters, the result codec configuration 96 // and capability is restricted to the user's configuration choice. 97 // Returns a copy of the current codec capability. 98 btav_a2dp_codec_config_t getCodecCapability(); 99 100 // Gets the codec local capability. 101 // Returns a copy of the codec local capability. 102 btav_a2dp_codec_config_t getCodecLocalCapability(); 103 104 // Gets the codec selectable capability. 105 // The capability is computed by intersecting the local codec's capability 106 // and the peer's codec capability. Any explicit user configuration is 107 // not included in the result. 108 // Returns a copy of the codec selectable capability. 109 btav_a2dp_codec_config_t getCodecSelectableCapability(); 110 111 // Gets the current codec user configuration. 112 // Returns a copy of the current codec user configuration. 113 btav_a2dp_codec_config_t getCodecUserConfig(); 114 115 // Gets the current codec audio configuration. 116 // Returns a copy of the current codec audio configuration. 117 btav_a2dp_codec_config_t getCodecAudioConfig(); 118 119 // Gets the number of bits per sample of the current codec configuration, 120 // or 0 if not configured. 121 uint8_t getAudioBitsPerSample(); 122 123 // Checks whether the codec uses the RTP Header Marker bit (see RFC 6416). 124 // NOTE: Even if the encoded data uses RTP headers, some codecs do not use 125 // the Marker bit - that bit is expected to be set to 0. 126 // Returns true if the encoded data packets have RTP headers, and 127 // the Marker bit in the header is set according to RFC 6416. 128 virtual bool useRtpHeaderMarkerBit() const = 0; 129 130 // Checks whether |codec_config| is empty and contains no configuration. 131 // Returns true if |codec_config| is empty, otherwise false. 132 static bool isCodecConfigEmpty(const btav_a2dp_codec_config_t& codec_config); 133 134 protected: 135 // Sets the current priority of the codec to |codec_priority|. 136 // If |codec_priority| is BTAV_A2DP_CODEC_PRIORITY_DEFAULT, the priority is 137 // reset to its default value. 138 void setCodecPriority(btav_a2dp_codec_priority_t codec_priority); 139 140 // Sets the current priority of the codec to its default value. 141 void setDefaultCodecPriority(); 142 143 // Sets the A2DP Source-to-Sink codec configuration to be used 144 // with a peer Sink device. 145 // |p_peer_codec_info| is the peer's A2DP Sink codec information 146 // to use. If |is_capability| is true, then |p_peer_codec_info| contains the 147 // peer's A2DP Sink codec capability, otherwise it contains the peer's 148 // preferred A2DP codec configuration to use. 149 // The result codec configuration is stored in |p_result_codec_config|. 150 // See |A2dpCodecs.setCodecConfig| for detailed description of 151 // the actual mechanism used to compute the configuration. 152 // Returns true on success, othewise false. 153 virtual bool setCodecConfig(const uint8_t* p_peer_codec_info, 154 bool is_capability, 155 uint8_t* p_result_codec_config) = 0; 156 157 // Sets the user prefered codec configuration. 158 // |codec_user_config| contains the preferred codec user configuration. 159 // |codec_audio_config| contains the selected audio feeding configuration. 160 // |p_peer_params| contains the A2DP peer information. 161 // |p_peer_codec_info| is the peer's A2DP Sink codec information 162 // to use. If |is_capability| is true, then |p_peer_codec_info| contains the 163 // peer's A2DP Sink codec capability, otherwise it contains the peer's 164 // preferred A2DP codec configuration to use. 165 // If there is a change in the codec configuration that requires restarting 166 // if the audio input stream, flag |p_restart_input| is set to true. 167 // If there is a change in the encoder configuration that requires restarting 168 // of the A2DP connection, the new codec configuration is stored in 169 // |p_result_codec_config|, and flag |p_restart_output| is set to true. 170 // If there is any change in the codec configuration, flag |p_config_updated| 171 // is set to true. 172 // Returns true on success, otherwise false. 173 virtual bool setCodecUserConfig( 174 const btav_a2dp_codec_config_t& codec_user_config, 175 const btav_a2dp_codec_config_t& codec_audio_config, 176 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 177 const uint8_t* p_peer_codec_info, bool is_capability, 178 uint8_t* p_result_codec_config, bool* p_restart_input, 179 bool* p_restart_output, bool* p_config_updated); 180 181 // Sets the codec capabilities for a peer. 182 // |p_peer_codec_capabiltities| is the peer codec capabilities to set. 183 // Returns true on success, otherwise false. 184 virtual bool setPeerCodecCapabilities( 185 const uint8_t* p_peer_codec_capabilities) = 0; 186 187 // Constructor where |codec_index| is the unique index that identifies the 188 // codec. The user-friendly name is |name|. 189 // The default codec priority is |codec_priority|. If the value is 190 // |BTAV_A2DP_CODEC_PRIORITY_DEFAULT|, the codec priority is computed 191 // internally. 192 A2dpCodecConfig(btav_a2dp_codec_index_t codec_index, const std::string& name, 193 btav_a2dp_codec_priority_t codec_priority); 194 195 // Initializes the codec entry. 196 // Returns true on success, otherwise false. 197 virtual bool init() = 0; 198 199 // Checks whether the internal state is valid 200 virtual bool isValid() const; 201 202 // Checks whether the A2DP Codec Configuration is valid. 203 // Returns true if A2DP Codec Configuration stored in |codec_config| 204 // is valid, otherwise false. 205 static bool codecConfigIsValid(const btav_a2dp_codec_config_t& codec_config); 206 207 // Gets the string representation of A2DP Codec Configuration. 208 // Returns the string representation of A2DP Codec Configuration stored 209 // in |codec_config|. The format is: 210 // "Rate=44100|48000 Bits=16|24 Mode=MONO|STEREO" 211 static std::string codecConfig2Str( 212 const btav_a2dp_codec_config_t& codec_config); 213 214 // Gets the string representation of A2DP Codec Sample Rate. 215 // Returns the string representation of A2DP Codec Sample Rate stored 216 // in |codec_sample_rate|. If there are multiple values stored in 217 // |codec_sample_rate|, the return string format is "rate1|rate2|rate3". 218 static std::string codecSampleRate2Str( 219 btav_a2dp_codec_sample_rate_t codec_sample_rate); 220 221 // Gets the string representation of A2DP Codec Bits Per Sample. 222 // Returns the string representation of A2DP Codec Bits Per Sample stored 223 // in |codec_bits_per_sample|. If there are multiple values stored in 224 // |codec_bits_per_sample|, the return string format is "bits1|bits2|bits3". 225 static std::string codecBitsPerSample2Str( 226 btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample); 227 228 // Gets the string representation of A2DP Codec Channel Mode. 229 // Returns the string representation of A2DP Channel Mode stored 230 // in |codec_channel_mode|. If there are multiple values stored in 231 // |codec_channel_mode|, the return string format is "mode1|mode2|mode3". 232 static std::string codecChannelMode2Str( 233 btav_a2dp_codec_channel_mode_t codec_channel_mode); 234 235 // Dumps codec-related information. 236 // The information is written in user-friendly form to file descriptor |fd|. 237 virtual void debug_codec_dump(int fd); 238 239 std::recursive_mutex codec_mutex_; 240 const btav_a2dp_codec_index_t codec_index_; // The unique codec index 241 const std::string name_; // The codec name 242 btav_a2dp_codec_priority_t codec_priority_; // Codec priority: must be unique 243 btav_a2dp_codec_priority_t default_codec_priority_; 244 245 btav_a2dp_codec_config_t codec_config_; 246 btav_a2dp_codec_config_t codec_capability_; 247 btav_a2dp_codec_config_t codec_local_capability_; 248 btav_a2dp_codec_config_t codec_selectable_capability_; 249 250 // The optional user configuration. The values (if set) are used 251 // as a preference when there is a choice. If a particular value 252 // is not supported by the local or remote device, it is ignored. 253 btav_a2dp_codec_config_t codec_user_config_; 254 255 // The selected audio feeding configuration. 256 btav_a2dp_codec_config_t codec_audio_config_; 257 258 uint8_t ota_codec_config_[AVDT_CODEC_SIZE]; 259 uint8_t ota_codec_peer_capability_[AVDT_CODEC_SIZE]; 260 uint8_t ota_codec_peer_config_[AVDT_CODEC_SIZE]; 261 }; 262 263 class A2dpCodecs { 264 public: 265 // Constructor for class |A2dpCodecs|. 266 // |codec_priorities| contains the codec priorities to use. 267 A2dpCodecs(const std::vector<btav_a2dp_codec_config_t>& codec_priorities); 268 ~A2dpCodecs(); 269 270 // Initializes all supported codecs. 271 // Returns true if at least one Source codec and one Sink codec were 272 // initialized, otherwise false. 273 bool init(); 274 275 // Finds the Source codec that corresponds to the A2DP over-the-air 276 // |p_codec_info| information. 277 // Returns the Source codec if found, otherwise nullptr. 278 A2dpCodecConfig* findSourceCodecConfig(const uint8_t* p_codec_info); 279 280 // Finds the Sink codec that corresponds to the A2DP over-the-air 281 // |p_codec_info| information. 282 // Returns the Sink codec if found, otherwise nullptr. 283 A2dpCodecConfig* findSinkCodecConfig(const uint8_t* p_codec_info); 284 285 // Checks whether the codec for |codec_index| is supported. 286 // Returns true if the codec is supported, otherwise false. 287 bool isSupportedCodec(btav_a2dp_codec_index_t codec_index); 288 289 // Gets the codec config that is currently selected. 290 // Returns the codec config that is currently selected, or nullptr if 291 // no codec is selected. getCurrentCodecConfig()292 A2dpCodecConfig* getCurrentCodecConfig() const { 293 return current_codec_config_; 294 } 295 296 // Gets the list of Source codecs ordered by priority: higher priority first. orderedSourceCodecs()297 const std::list<A2dpCodecConfig*> orderedSourceCodecs() const { 298 return ordered_source_codecs_; 299 } 300 301 // Gets the list of Sink codecs ordered by priority: higher priority first. orderedSinkCodecs()302 const std::list<A2dpCodecConfig*> orderedSinkCodecs() const { 303 return ordered_sink_codecs_; 304 } 305 306 // Sets the A2DP Source-to-Sink codec configuration to be used 307 // with a peer Sink device. 308 // |p_peer_codec_info| is the peer's A2DP Sink codec information 309 // to use. If |is_capability| is true, then |p_peer_codec_info| contains the 310 // peer's A2DP Sink codec capability, otherwise it contains the peer's 311 // preferred A2DP codec configuration to use. 312 // If the codec can be used and |select_current_codec| is true, then 313 // this codec is selected as the current one. 314 // 315 // The codec configuration is built by considering the optional user 316 // configuration, the local codec capabilities, the peer's codec 317 // capabilities, and the codec's locally-defined default values. 318 // For each codec parameter: 319 // 320 // 1. If it is user-configurable parameter (sample rate, bits per sample, 321 // channel mode, and some codec-specific parameters), 322 // if the user has an explicit preference, and that preference 323 // is supported by both the local and remote device, this is the 324 // parameter value that is used. 325 // 2. Otherwise, if the explicit internal default value is supported 326 // by both the local and remote device, this is the parameter value 327 // that is used. 328 // 3. Otherwise, the best match is chosen among all values supported by 329 // the local and remote device. 330 // 331 // In addition, the codec's internal state is updated to reflect 332 // the capabilities that are advertised to the upstream audio source 333 // (Media Framework) to make run-time audio parameter choices: 334 // 4. If the user-configurable parameter was selected, this is the 335 // only parameter value that is advertised to the Media Framework. 336 // 5. Otherwise, all values supported by both the local and remote 337 // devices are advertised to the Media Framework. 338 // 339 // The result codec configuration is stored in |p_result_codec_config|. 340 // Returns true on success, othewise false. 341 bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability, 342 uint8_t* p_result_codec_config, 343 bool select_current_codec); 344 345 // Sets the A2DP Sink codec configuration to be used with a peer Source 346 // device. 347 // [See setCodecConfig() for description] 348 bool setSinkCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability, 349 uint8_t* p_result_codec_config, 350 bool select_current_codec); 351 352 // Sets the user prefered codec configuration. 353 // |codec_user_config| contains the preferred codec configuration. 354 // |p_peer_params| contains the A2DP peer information. 355 // |p_peer_sink_capabilities| is the peer's A2DP Sink codec capabilities 356 // to use. 357 // If there is a change in the encoder configuration that requires restarting 358 // the audio input stream, flag |p_restart_input| is set to true. 359 // If there is a change in the encoder configuration that requires restarting 360 // of the A2DP connection, flag |p_restart_output| is set to true, and the 361 // new codec is stored in |p_result_codec_config|. 362 // If there is any change in the codec configuration, flag |p_config_updated| 363 // is set to true. 364 // Returns true on success, otherwise false. 365 bool setCodecUserConfig(const btav_a2dp_codec_config_t& codec_user_config, 366 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 367 const uint8_t* p_peer_sink_capabilities, 368 uint8_t* p_result_codec_config, bool* p_restart_input, 369 bool* p_restart_output, bool* p_config_updated); 370 371 // Sets the Audio HAL selected audio feeding parameters. 372 // Those parameters are applied only to the currently selected codec. 373 // |codec_audio_config| contains the selected audio feeding configuration. 374 // |p_peer_params| contains the A2DP peer information. 375 // |p_peer_sink_capabilities| is the peer's A2DP Sink codec capabilities 376 // to use. 377 // If there is a change in the encoder configuration that requires restarting 378 // of the A2DP connection, flag |p_restart_output| is set to true, and the 379 // new codec is stored in |p_result_codec_config|. 380 // If there is any change in the codec configuration, flag |p_config_updated| 381 // is set to true. 382 // Returns true on success, otherwise false. 383 bool setCodecAudioConfig(const btav_a2dp_codec_config_t& codec_audio_config, 384 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 385 const uint8_t* p_peer_sink_capabilities, 386 uint8_t* p_result_codec_config, 387 bool* p_restart_output, bool* p_config_updated); 388 389 // Sets the Over-The-Air preferred codec configuration. 390 // The OTA prefered codec configuration is ignored if the current 391 // codec configuration contains explicit user configuration, or if the 392 // codec configuration for the same codec contains explicit user 393 // configuration. 394 // |p_ota_codec_config| contains the received OTA A2DP codec configuration 395 // from the remote peer. Note: this is not the peer codec capability, 396 // but the codec configuration that the peer would like to use. 397 // |p_peer_params| contains the A2DP peer information. 398 // If there is a change in the encoder configuration that requires restarting 399 // the audio input stream, flag |p_restart_input| is set to true. 400 // If there is a change in the encoder configuration that requires restarting 401 // of the A2DP connection, flag |p_restart_output| is set to true, and the 402 // new codec is stored in |p_result_codec_config|. 403 // If there is any change in the codec configuration, flag |p_config_updated| 404 // is set to true. 405 // Returns true on success, otherwise false. 406 bool setCodecOtaConfig(const uint8_t* p_ota_codec_config, 407 const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 408 uint8_t* p_result_codec_config, bool* p_restart_input, 409 bool* p_restart_output, bool* p_config_updated); 410 411 // Sets the codec capabilities for a Sink peer. 412 // |p_peer_codec_capabiltities| is the peer codec capabilities to set. 413 // Returns true on success, otherwise false. 414 bool setPeerSinkCodecCapabilities(const uint8_t* p_peer_codec_capabilities); 415 416 // Sets the codec capabilities for a Source peer. 417 // |p_peer_codec_capabiltities| is the peer codec capabilities to set. 418 // Returns true on success, otherwise false. 419 bool setPeerSourceCodecCapabilities(const uint8_t* p_peer_codec_capabilities); 420 421 // Gets the current codec configuration and the capabilities of 422 // all configured codecs. 423 // The current codec configuration is stored in |p_codec_config|. 424 // Local device's codecs capabilities are stored in 425 // |p_codecs_local_capabilities|. 426 // The codecs capabilities that can be used between the local device 427 // and the remote device are stored in |p_codecs_selectable_capabilities|. 428 // Returns true on success, otherwise false. 429 bool getCodecConfigAndCapabilities( 430 btav_a2dp_codec_config_t* p_codec_config, 431 std::vector<btav_a2dp_codec_config_t>* p_codecs_local_capabilities, 432 std::vector<btav_a2dp_codec_config_t>* p_codecs_selectable_capabilities); 433 434 // Dumps codec-related information. 435 // The information is written in user-friendly form to file descriptor |fd|. 436 void debug_codec_dump(int fd); 437 438 private: 439 struct CompareBtBdaddr { operatorCompareBtBdaddr440 bool operator()(const RawAddress& lhs, const RawAddress& rhs) const { 441 return (memcmp(&lhs, &rhs, sizeof(lhs)) < 0); 442 } 443 }; 444 typedef std::map<btav_a2dp_codec_index_t, A2dpCodecConfig*> IndexedCodecs; 445 446 std::recursive_mutex codec_mutex_; 447 A2dpCodecConfig* current_codec_config_; // Currently selected codec 448 std::map<btav_a2dp_codec_index_t, btav_a2dp_codec_priority_t> 449 codec_priorities_; 450 451 IndexedCodecs indexed_codecs_; // The codecs indexed by codec index 452 IndexedCodecs disabled_codecs_; // The disabled codecs 453 454 // A2DP Source codecs ordered by priority 455 std::list<A2dpCodecConfig*> ordered_source_codecs_; 456 457 // A2DP Sink codecs ordered by priority 458 std::list<A2dpCodecConfig*> ordered_sink_codecs_; 459 460 std::map<RawAddress, IndexedCodecs*, CompareBtBdaddr> peer_codecs_; 461 }; 462 463 /** 464 * Structure used to configure the A2DP feeding. 465 */ 466 typedef struct { 467 tA2DP_SAMPLE_RATE sample_rate; // 44100, 48000, etc 468 tA2DP_BITS_PER_SAMPLE bits_per_sample; // 8, 16, 24, 32 469 tA2DP_CHANNEL_COUNT channel_count; // 1 for mono or 2 for stereo 470 } tA2DP_FEEDING_PARAMS; 471 472 // Prototype for a callback to read audio data for encoding. 473 // |p_buf| is the buffer to store the data. |len| is the number of octets to 474 // read. 475 // Returns the number of octets read. 476 typedef uint32_t (*a2dp_source_read_callback_t)(uint8_t* p_buf, uint32_t len); 477 478 // Prototype for a callback to enqueue A2DP Source packets for transmission. 479 // |p_buf| is the buffer with the audio data to enqueue. The callback is 480 // responsible for freeing |p_buf|. 481 // |frames_n| is the number of audio frames in |p_buf| - it is used for 482 // statistics purpose. 483 // |num_bytes| is the number of audio bytes in |p_buf| - it is used for 484 // delay reporting. 485 // Returns true if the packet was enqueued, otherwise false. 486 typedef bool (*a2dp_source_enqueue_callback_t)(BT_HDR* p_buf, size_t frames_n, 487 uint32_t num_bytes); 488 489 // 490 // A2DP encoder callbacks interface. 491 // 492 typedef struct { 493 // Initialize the A2DP encoder. 494 // |p_peer_params| contains the A2DP peer information 495 // The current A2DP codec config is in |a2dp_codec_config|. 496 // |read_callback| is the callback for reading the input audio data. 497 // |enqueue_callback| is the callback for enqueueing the encoded audio data. 498 void (*encoder_init)(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params, 499 A2dpCodecConfig* a2dp_codec_config, 500 a2dp_source_read_callback_t read_callback, 501 a2dp_source_enqueue_callback_t enqueue_callback); 502 503 // Cleanup the A2DP encoder. 504 void (*encoder_cleanup)(void); 505 506 // Reset the feeding for the A2DP encoder. 507 void (*feeding_reset)(void); 508 509 // Flush the feeding for the A2DP encoder. 510 void (*feeding_flush)(void); 511 512 // Get the A2DP encoder interval (in milliseconds). 513 uint64_t (*get_encoder_interval_ms)(void); 514 515 // Get the A2DP encoded maximum frame size (similar to MTU). 516 int (*get_effective_frame_size)(void); 517 518 // Prepare and send A2DP encoded frames. 519 // |timestamp_us| is the current timestamp (in microseconds). 520 void (*send_frames)(uint64_t timestamp_us); 521 522 // Set transmit queue length for the A2DP encoder. 523 void (*set_transmit_queue_length)(size_t transmit_queue_length); 524 } tA2DP_ENCODER_INTERFACE; 525 526 // Prototype for a callback to receive decoded audio data from a 527 // tA2DP_DECODER_INTERFACE|. 528 // |buf| is a pointer to the data. 529 // |len| is the number of octets pointed to by |buf|. 530 typedef void (*decoded_data_callback_t)(uint8_t* buf, uint32_t len); 531 532 // 533 // A2DP decoder callbacks interface. 534 // 535 typedef struct { 536 // Initialize the decoder. Can be called multiple times, will reinitalize. 537 bool (*decoder_init)(decoded_data_callback_t decode_callback); 538 539 // Cleanup the A2DP decoder. 540 void (*decoder_cleanup)(); 541 542 // Decodes |p_buf| and calls |decode_callback| passed into init for the 543 // decoded data. 544 bool (*decode_packet)(BT_HDR* p_buf); 545 546 // Start the A2DP decoder. 547 void (*decoder_start)(); 548 549 // Suspend the A2DP decoder. 550 void (*decoder_suspend)(); 551 552 // A2DP decoder configuration. 553 void (*decoder_configure)(const uint8_t* p_codec_info); 554 } tA2DP_DECODER_INTERFACE; 555 556 // Gets the A2DP codec type. 557 // |p_codec_info| contains information about the codec capabilities. 558 tA2DP_CODEC_TYPE A2DP_GetCodecType(const uint8_t* p_codec_info); 559 560 // Checks whether the codec capabilities contain a valid A2DP Source codec. 561 // NOTE: only codecs that are implemented are considered valid. 562 // Returns true if |p_codec_info| contains information about a valid codec, 563 // otherwise false. 564 bool A2DP_IsSourceCodecValid(const uint8_t* p_codec_info); 565 566 // Checks whether the codec capabilities contain a valid A2DP Sink codec. 567 // NOTE: only codecs that are implemented are considered valid. 568 // Returns true if |p_codec_info| contains information about a valid codec, 569 // otherwise false. 570 bool A2DP_IsSinkCodecValid(const uint8_t* p_codec_info); 571 572 // Checks whether the codec capabilities contain a valid peer A2DP Source 573 // codec. 574 // NOTE: only codecs that are implemented are considered valid. 575 // Returns true if |p_codec_info| contains information about a valid codec, 576 // otherwise false. 577 bool A2DP_IsPeerSourceCodecValid(const uint8_t* p_codec_info); 578 579 // Checks whether the codec capabilities contain a valid peer A2DP Sink codec. 580 // NOTE: only codecs that are implemented are considered valid. 581 // Returns true if |p_codec_info| contains information about a valid codec, 582 // otherwise false. 583 bool A2DP_IsPeerSinkCodecValid(const uint8_t* p_codec_info); 584 585 // Checks whether an A2DP Sink codec is supported. 586 // |p_codec_info| contains information about the codec capabilities. 587 // Returns true if the A2DP Sink codec is supported, otherwise false. 588 bool A2DP_IsSinkCodecSupported(const uint8_t* p_codec_info); 589 590 // Gets peer sink endpoint codec type. 591 // |p_codec_info| contains information about the codec capabilities. 592 int A2DP_IotGetPeerSinkCodecType(const uint8_t* p_codec_info); 593 594 // Checks whether an A2DP Source codec for a peer Source device is supported. 595 // |p_codec_info| contains information about the codec capabilities of the 596 // peer device. 597 // Returns true if the A2DP Source codec for a peer Source device is supported, 598 // otherwise false. 599 bool A2DP_IsPeerSourceCodecSupported(const uint8_t* p_codec_info); 600 601 // Initialize state with the default A2DP codec. 602 // The initialized state with the codec capabilities is stored in 603 // |p_codec_info|. 604 void A2DP_InitDefaultCodec(uint8_t* p_codec_info); 605 606 // Checks whether the A2DP data packets should contain RTP header. 607 // |content_protection_enabled| is true if Content Protection is 608 // enabled. |p_codec_info| contains information about the codec capabilities. 609 // Returns true if the A2DP data packets should contain RTP header, otherwise 610 // false. 611 bool A2DP_UsesRtpHeader(bool content_protection_enabled, 612 const uint8_t* p_codec_info); 613 614 // Gets the |AVDT_MEDIA_TYPE_*| media type from the codec capability 615 // in |p_codec_info|. 616 uint8_t A2DP_GetMediaType(const uint8_t* p_codec_info); 617 618 // Gets the A2DP codec name for a given |p_codec_info|. 619 const char* A2DP_CodecName(const uint8_t* p_codec_info); 620 621 // Checks whether two A2DP codecs |p_codec_info_a| and |p_codec_info_b| have 622 // the same type. 623 // Returns true if the two codecs have the same type, otherwise false. 624 // If the codec type is not recognized, the return value is false. 625 bool A2DP_CodecTypeEquals(const uint8_t* p_codec_info_a, 626 const uint8_t* p_codec_info_b); 627 628 // Checks whether two A2DP codecs p_codec_info_a| and |p_codec_info_b| are 629 // exactly the same. 630 // Returns true if the two codecs are exactly the same, otherwise false. 631 // If the codec type is not recognized, the return value is false. 632 bool A2DP_CodecEquals(const uint8_t* p_codec_info_a, 633 const uint8_t* p_codec_info_b); 634 635 // Gets the track sample rate value for the A2DP codec. 636 // |p_codec_info| is a pointer to the codec_info to decode. 637 // Returns the track sample rate on success, or -1 if |p_codec_info| 638 // contains invalid codec information. 639 int A2DP_GetTrackSampleRate(const uint8_t* p_codec_info); 640 641 // Gets the track bits per sample value for the A2DP codec. 642 // |p_codec_info| is a pointer to the codec_info to decode. 643 // Returns the track bits per sample on success, or -1 if |p_codec_info| 644 // contains invalid codec information. 645 int A2DP_GetTrackBitsPerSample(const uint8_t* p_codec_info); 646 647 // Gets the channel count for the A2DP codec. 648 // |p_codec_info| is a pointer to the codec_info to decode. 649 // Returns the channel count on success, or -1 if |p_codec_info| 650 // contains invalid codec information. 651 int A2DP_GetTrackChannelCount(const uint8_t* p_codec_info); 652 653 // Gets the channel type for the A2DP Sink codec: 654 // 1 for mono, or 3 for dual/stereo/joint. 655 // |p_codec_info| is a pointer to the codec_info to decode. 656 // Returns the channel type on success, or -1 if |p_codec_info| 657 // contains invalid codec information. 658 int A2DP_GetSinkTrackChannelType(const uint8_t* p_codec_info); 659 660 // Gets the A2DP audio data timestamp from an audio packet. 661 // |p_codec_info| contains the codec information. 662 // |p_data| contains the audio data. 663 // The timestamp is stored in |p_timestamp|. 664 // Returns true on success, otherwise false. 665 bool A2DP_GetPacketTimestamp(const uint8_t* p_codec_info, const uint8_t* p_data, 666 uint32_t* p_timestamp); 667 668 // Builds A2DP codec header for audio data. 669 // |p_codec_info| contains the codec information. 670 // |p_buf| contains the audio data. 671 // |frames_per_packet| is the number of frames in this packet. 672 // Returns true on success, otherwise false. 673 bool A2DP_BuildCodecHeader(const uint8_t* p_codec_info, BT_HDR* p_buf, 674 uint16_t frames_per_packet); 675 676 // Gets the A2DP encoder interface that can be used to encode and prepare 677 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|. 678 // |p_codec_info| contains the codec information. 679 // Returns the A2DP encoder interface if the |p_codec_info| is valid and 680 // supported, otherwise NULL. 681 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterface( 682 const uint8_t* p_codec_info); 683 684 // Gets the A2DP decoder interface that can be used to decode received A2DP 685 // packets - see |tA2DP_DECODER_INTERFACE|. 686 // |p_codec_info| contains the codec information. 687 // Returns the A2DP decoder interface if the |p_codec_info| is valid and 688 // supported, otherwise NULL. 689 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterface( 690 const uint8_t* p_codec_info); 691 692 // Adjusts the A2DP codec, based on local support and Bluetooth specification. 693 // |p_codec_info| contains the codec information to adjust. 694 // Returns true if |p_codec_info| is valid and supported, otherwise false. 695 bool A2DP_AdjustCodec(uint8_t* p_codec_info); 696 697 // Gets the A2DP Source codec index for a given |p_codec_info|. 698 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 699 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 700 btav_a2dp_codec_index_t A2DP_SourceCodecIndex(const uint8_t* p_codec_info); 701 702 // Gets the A2DP Sink codec index for a given |p_codec_info|. 703 // Returns the corresponding |btav_a2dp_codec_index_t| on success, 704 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|. 705 btav_a2dp_codec_index_t A2DP_SinkCodecIndex(const uint8_t* p_codec_info); 706 707 // Gets the A2DP codec name for a given |codec_index|. 708 const char* A2DP_CodecIndexStr(btav_a2dp_codec_index_t codec_index); 709 710 // Initializes A2DP codec-specific information into |AvdtpSepConfig| 711 // configuration entry pointed by |p_cfg|. The selected codec is defined 712 // by |codec_index|. 713 // Returns true on success, otherwise false. 714 bool A2DP_InitCodecConfig(btav_a2dp_codec_index_t codec_index, 715 AvdtpSepConfig* p_cfg); 716 717 // Gets the A2DP effective frame size that each encoded media frame should not 718 // exceed this value. 719 // |p_codec_info| contains the codec information. 720 // Returns the effective frame size if the encoder is configured with this 721 // |p_codec_info|, otherwise 0. 722 int A2DP_GetEecoderEffectiveFrameSize(const uint8_t* p_codec_info); 723 724 // Decodes A2DP codec info into a human readable string. 725 // |p_codec_info| is a pointer to the codec_info to decode. 726 // Returns a string describing the codec information. 727 std::string A2DP_CodecInfoString(const uint8_t* p_codec_info); 728 729 // Add enum-based flag operators to the btav_a2dp_codec_config_t fields 730 #ifndef DEFINE_ENUM_FLAG_OPERATORS 731 // Use NOLINT to suppress missing parentheses warnings around bitmask. 732 #define DEFINE_ENUM_FLAG_OPERATORS(bitmask) \ 733 extern "C++" { \ 734 inline constexpr bitmask operator&(bitmask X, bitmask Y) { /* NOLINT */ \ 735 return static_cast<bitmask>(static_cast<int>(X) & static_cast<int>(Y)); \ 736 } \ 737 inline constexpr bitmask operator|(bitmask X, bitmask Y) { /* NOLINT */ \ 738 return static_cast<bitmask>(static_cast<int>(X) | static_cast<int>(Y)); \ 739 } \ 740 inline constexpr bitmask operator^(bitmask X, bitmask Y) { /* NOLINT */ \ 741 return static_cast<bitmask>(static_cast<int>(X) ^ static_cast<int>(Y)); \ 742 } \ 743 inline constexpr bitmask operator~(bitmask X) { /* NOLINT */ \ 744 return static_cast<bitmask>(~static_cast<int>(X)); \ 745 } \ 746 inline bitmask& operator&=(bitmask& X, bitmask Y) { /* NOLINT */ \ 747 X = X & Y; \ 748 return X; \ 749 } \ 750 inline bitmask& operator|=(bitmask& X, bitmask Y) { /* NOLINT */ \ 751 X = X | Y; \ 752 return X; \ 753 } \ 754 inline bitmask& operator^=(bitmask& X, bitmask Y) { /* NOLINT */ \ 755 X = X ^ Y; \ 756 return X; \ 757 } \ 758 } 759 #endif // DEFINE_ENUM_FLAG_OPERATORS 760 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_sample_rate_t); 761 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_bits_per_sample_t); 762 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_channel_mode_t); 763 764 #endif // A2DP_CODEC_API_H 765