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