• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
440       : public std::binary_function<RawAddress, RawAddress, bool> {
operatorCompareBtBdaddr441     bool operator()(const RawAddress& lhs, const RawAddress& rhs) const {
442       return (memcmp(&lhs, &rhs, sizeof(lhs)) < 0);
443     }
444   };
445   typedef std::map<btav_a2dp_codec_index_t, A2dpCodecConfig*> IndexedCodecs;
446 
447   std::recursive_mutex codec_mutex_;
448   A2dpCodecConfig* current_codec_config_;  // Currently selected codec
449   std::map<btav_a2dp_codec_index_t, btav_a2dp_codec_priority_t>
450       codec_priorities_;
451 
452   IndexedCodecs indexed_codecs_;           // The codecs indexed by codec index
453   IndexedCodecs disabled_codecs_;          // The disabled codecs
454 
455   // A2DP Source codecs ordered by priority
456   std::list<A2dpCodecConfig*> ordered_source_codecs_;
457 
458   // A2DP Sink codecs ordered by priority
459   std::list<A2dpCodecConfig*> ordered_sink_codecs_;
460 
461   std::map<RawAddress, IndexedCodecs*, CompareBtBdaddr> peer_codecs_;
462 };
463 
464 /**
465  * Structure used to configure the A2DP feeding.
466  */
467 typedef struct {
468   tA2DP_SAMPLE_RATE sample_rate;          // 44100, 48000, etc
469   tA2DP_BITS_PER_SAMPLE bits_per_sample;  // 8, 16, 24, 32
470   tA2DP_CHANNEL_COUNT channel_count;      // 1 for mono or 2 for stereo
471 } tA2DP_FEEDING_PARAMS;
472 
473 // Prototype for a callback to read audio data for encoding.
474 // |p_buf| is the buffer to store the data. |len| is the number of octets to
475 // read.
476 // Returns the number of octets read.
477 typedef uint32_t (*a2dp_source_read_callback_t)(uint8_t* p_buf, uint32_t len);
478 
479 // Prototype for a callback to enqueue A2DP Source packets for transmission.
480 // |p_buf| is the buffer with the audio data to enqueue. The callback is
481 // responsible for freeing |p_buf|.
482 // |frames_n| is the number of audio frames in |p_buf| - it is used for
483 // statistics purpose.
484 // |num_bytes| is the number of audio bytes in |p_buf| - it is used for
485 // delay reporting.
486 // Returns true if the packet was enqueued, otherwise false.
487 typedef bool (*a2dp_source_enqueue_callback_t)(BT_HDR* p_buf, size_t frames_n,
488                                                uint32_t num_bytes);
489 
490 //
491 // A2DP encoder callbacks interface.
492 //
493 typedef struct {
494   // Initialize the A2DP encoder.
495   // |p_peer_params| contains the A2DP peer information
496   // The current A2DP codec config is in |a2dp_codec_config|.
497   // |read_callback| is the callback for reading the input audio data.
498   // |enqueue_callback| is the callback for enqueueing the encoded audio data.
499   void (*encoder_init)(const tA2DP_ENCODER_INIT_PEER_PARAMS* p_peer_params,
500                        A2dpCodecConfig* a2dp_codec_config,
501                        a2dp_source_read_callback_t read_callback,
502                        a2dp_source_enqueue_callback_t enqueue_callback);
503 
504   // Cleanup the A2DP encoder.
505   void (*encoder_cleanup)(void);
506 
507   // Reset the feeding for the A2DP encoder.
508   void (*feeding_reset)(void);
509 
510   // Flush the feeding for the A2DP encoder.
511   void (*feeding_flush)(void);
512 
513   // Get the A2DP encoder interval (in milliseconds).
514   uint64_t (*get_encoder_interval_ms)(void);
515 
516   // Get the A2DP encoded maximum frame size (similar to MTU).
517   int (*get_effective_frame_size)(void);
518 
519   // Prepare and send A2DP encoded frames.
520   // |timestamp_us| is the current timestamp (in microseconds).
521   void (*send_frames)(uint64_t timestamp_us);
522 
523   // Set transmit queue length for the A2DP encoder.
524   void (*set_transmit_queue_length)(size_t transmit_queue_length);
525 } tA2DP_ENCODER_INTERFACE;
526 
527 // Prototype for a callback to receive decoded audio data from a
528 // tA2DP_DECODER_INTERFACE|.
529 // |buf| is a pointer to the data.
530 // |len| is the number of octets pointed to by |buf|.
531 typedef void (*decoded_data_callback_t)(uint8_t* buf, uint32_t len);
532 
533 //
534 // A2DP decoder callbacks interface.
535 //
536 typedef struct {
537   // Initialize the decoder. Can be called multiple times, will reinitalize.
538   bool (*decoder_init)(decoded_data_callback_t decode_callback);
539 
540   // Cleanup the A2DP decoder.
541   void (*decoder_cleanup)();
542 
543   // Decodes |p_buf| and calls |decode_callback| passed into init for the
544   // decoded data.
545   bool (*decode_packet)(BT_HDR* p_buf);
546 
547   // Start the A2DP decoder.
548   void (*decoder_start)();
549 
550   // Suspend the A2DP decoder.
551   void (*decoder_suspend)();
552 
553   // A2DP decoder configuration.
554   void (*decoder_configure)(const uint8_t* p_codec_info);
555 } tA2DP_DECODER_INTERFACE;
556 
557 // Gets the A2DP codec type.
558 // |p_codec_info| contains information about the codec capabilities.
559 tA2DP_CODEC_TYPE A2DP_GetCodecType(const uint8_t* p_codec_info);
560 
561 // Checks whether the codec capabilities contain a valid A2DP Source codec.
562 // NOTE: only codecs that are implemented are considered valid.
563 // Returns true if |p_codec_info| contains information about a valid codec,
564 // otherwise false.
565 bool A2DP_IsSourceCodecValid(const uint8_t* p_codec_info);
566 
567 // Checks whether the codec capabilities contain a valid A2DP Sink codec.
568 // NOTE: only codecs that are implemented are considered valid.
569 // Returns true if |p_codec_info| contains information about a valid codec,
570 // otherwise false.
571 bool A2DP_IsSinkCodecValid(const uint8_t* p_codec_info);
572 
573 // Checks whether the codec capabilities contain a valid peer A2DP Source
574 // codec.
575 // NOTE: only codecs that are implemented are considered valid.
576 // Returns true if |p_codec_info| contains information about a valid codec,
577 // otherwise false.
578 bool A2DP_IsPeerSourceCodecValid(const uint8_t* p_codec_info);
579 
580 // Checks whether the codec capabilities contain a valid peer A2DP Sink codec.
581 // NOTE: only codecs that are implemented are considered valid.
582 // Returns true if |p_codec_info| contains information about a valid codec,
583 // otherwise false.
584 bool A2DP_IsPeerSinkCodecValid(const uint8_t* p_codec_info);
585 
586 // Checks whether an A2DP Sink codec is supported.
587 // |p_codec_info| contains information about the codec capabilities.
588 // Returns true if the A2DP Sink codec is supported, otherwise false.
589 bool A2DP_IsSinkCodecSupported(const uint8_t* p_codec_info);
590 
591 // Checks whether an A2DP Source codec for a peer Source device is supported.
592 // |p_codec_info| contains information about the codec capabilities of the
593 // peer device.
594 // Returns true if the A2DP Source codec for a peer Source device is supported,
595 // otherwise false.
596 bool A2DP_IsPeerSourceCodecSupported(const uint8_t* p_codec_info);
597 
598 // Initialize state with the default A2DP codec.
599 // The initialized state with the codec capabilities is stored in
600 // |p_codec_info|.
601 void A2DP_InitDefaultCodec(uint8_t* p_codec_info);
602 
603 // Checks whether the A2DP data packets should contain RTP header.
604 // |content_protection_enabled| is true if Content Protection is
605 // enabled. |p_codec_info| contains information about the codec capabilities.
606 // Returns true if the A2DP data packets should contain RTP header, otherwise
607 // false.
608 bool A2DP_UsesRtpHeader(bool content_protection_enabled,
609                         const uint8_t* p_codec_info);
610 
611 // Gets the |AVDT_MEDIA_TYPE_*| media type from the codec capability
612 // in |p_codec_info|.
613 uint8_t A2DP_GetMediaType(const uint8_t* p_codec_info);
614 
615 // Gets the A2DP codec name for a given |p_codec_info|.
616 const char* A2DP_CodecName(const uint8_t* p_codec_info);
617 
618 // Checks whether two A2DP codecs |p_codec_info_a| and |p_codec_info_b| have
619 // the same type.
620 // Returns true if the two codecs have the same type, otherwise false.
621 // If the codec type is not recognized, the return value is false.
622 bool A2DP_CodecTypeEquals(const uint8_t* p_codec_info_a,
623                           const uint8_t* p_codec_info_b);
624 
625 // Checks whether two A2DP codecs p_codec_info_a| and |p_codec_info_b| are
626 // exactly the same.
627 // Returns true if the two codecs are exactly the same, otherwise false.
628 // If the codec type is not recognized, the return value is false.
629 bool A2DP_CodecEquals(const uint8_t* p_codec_info_a,
630                       const uint8_t* p_codec_info_b);
631 
632 // Gets the track sample rate value for the A2DP codec.
633 // |p_codec_info| is a pointer to the codec_info to decode.
634 // Returns the track sample rate on success, or -1 if |p_codec_info|
635 // contains invalid codec information.
636 int A2DP_GetTrackSampleRate(const uint8_t* p_codec_info);
637 
638 // Gets the track bits per sample value for the A2DP codec.
639 // |p_codec_info| is a pointer to the codec_info to decode.
640 // Returns the track bits per sample on success, or -1 if |p_codec_info|
641 // contains invalid codec information.
642 int A2DP_GetTrackBitsPerSample(const uint8_t* p_codec_info);
643 
644 // Gets the channel count for the A2DP codec.
645 // |p_codec_info| is a pointer to the codec_info to decode.
646 // Returns the channel count on success, or -1 if |p_codec_info|
647 // contains invalid codec information.
648 int A2DP_GetTrackChannelCount(const uint8_t* p_codec_info);
649 
650 // Gets the channel type for the A2DP Sink codec:
651 // 1 for mono, or 3 for dual/stereo/joint.
652 // |p_codec_info| is a pointer to the codec_info to decode.
653 // Returns the channel type on success, or -1 if |p_codec_info|
654 // contains invalid codec information.
655 int A2DP_GetSinkTrackChannelType(const uint8_t* p_codec_info);
656 
657 // Gets the A2DP audio data timestamp from an audio packet.
658 // |p_codec_info| contains the codec information.
659 // |p_data| contains the audio data.
660 // The timestamp is stored in |p_timestamp|.
661 // Returns true on success, otherwise false.
662 bool A2DP_GetPacketTimestamp(const uint8_t* p_codec_info, const uint8_t* p_data,
663                              uint32_t* p_timestamp);
664 
665 // Builds A2DP codec header for audio data.
666 // |p_codec_info| contains the codec information.
667 // |p_buf| contains the audio data.
668 // |frames_per_packet| is the number of frames in this packet.
669 // Returns true on success, otherwise false.
670 bool A2DP_BuildCodecHeader(const uint8_t* p_codec_info, BT_HDR* p_buf,
671                            uint16_t frames_per_packet);
672 
673 // Gets the A2DP encoder interface that can be used to encode and prepare
674 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
675 // |p_codec_info| contains the codec information.
676 // Returns the A2DP encoder interface if the |p_codec_info| is valid and
677 // supported, otherwise NULL.
678 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterface(
679     const uint8_t* p_codec_info);
680 
681 // Gets the A2DP decoder interface that can be used to decode received A2DP
682 // packets - see |tA2DP_DECODER_INTERFACE|.
683 // |p_codec_info| contains the codec information.
684 // Returns the A2DP decoder interface if the |p_codec_info| is valid and
685 // supported, otherwise NULL.
686 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterface(
687     const uint8_t* p_codec_info);
688 
689 // Adjusts the A2DP codec, based on local support and Bluetooth specification.
690 // |p_codec_info| contains the codec information to adjust.
691 // Returns true if |p_codec_info| is valid and supported, otherwise false.
692 bool A2DP_AdjustCodec(uint8_t* p_codec_info);
693 
694 // Gets the A2DP Source codec index for a given |p_codec_info|.
695 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
696 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
697 btav_a2dp_codec_index_t A2DP_SourceCodecIndex(const uint8_t* p_codec_info);
698 
699 // Gets the A2DP Sink codec index for a given |p_codec_info|.
700 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
701 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
702 btav_a2dp_codec_index_t A2DP_SinkCodecIndex(const uint8_t* p_codec_info);
703 
704 // Gets the A2DP codec name for a given |codec_index|.
705 const char* A2DP_CodecIndexStr(btav_a2dp_codec_index_t codec_index);
706 
707 // Initializes A2DP codec-specific information into |AvdtpSepConfig|
708 // configuration entry pointed by |p_cfg|. The selected codec is defined
709 // by |codec_index|.
710 // Returns true on success, otherwise false.
711 bool A2DP_InitCodecConfig(btav_a2dp_codec_index_t codec_index,
712                           AvdtpSepConfig* p_cfg);
713 
714 // Gets the A2DP effective frame size that each encoded media frame should not
715 // exceed this value.
716 // |p_codec_info| contains the codec information.
717 // Returns the effective frame size if the encoder is configured with this
718 // |p_codec_info|, otherwise 0.
719 int A2DP_GetEecoderEffectiveFrameSize(const uint8_t* p_codec_info);
720 
721 // Decodes A2DP codec info into a human readable string.
722 // |p_codec_info| is a pointer to the codec_info to decode.
723 // Returns a string describing the codec information.
724 std::string A2DP_CodecInfoString(const uint8_t* p_codec_info);
725 
726 // Add enum-based flag operators to the btav_a2dp_codec_config_t fields
727 #ifndef DEFINE_ENUM_FLAG_OPERATORS
728 // Use NOLINT to suppress missing parentheses warnings around bitmask.
729 #define DEFINE_ENUM_FLAG_OPERATORS(bitmask)                                 \
730   extern "C++" {                                                            \
731   inline constexpr bitmask operator&(bitmask X, bitmask Y) {  /* NOLINT */  \
732     return static_cast<bitmask>(static_cast<int>(X) & static_cast<int>(Y)); \
733   }                                                                         \
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) {             /* NOLINT */  \
741     return static_cast<bitmask>(~static_cast<int>(X));                      \
742   }                                                                         \
743   inline bitmask& operator&=(bitmask& X, bitmask Y) {         /* NOLINT */  \
744     X = X & Y;                                                              \
745     return X;                                                               \
746   }                                                                         \
747   inline bitmask& operator|=(bitmask& X, bitmask Y) {         /* NOLINT */  \
748     X = X | Y;                                                              \
749     return X;                                                               \
750   }                                                                         \
751   inline bitmask& operator^=(bitmask& X, bitmask Y) {         /* NOLINT */  \
752     X = X ^ Y;                                                              \
753     return X;                                                               \
754   }                                                                         \
755   }
756 #endif  // DEFINE_ENUM_FLAG_OPERATORS
757 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_sample_rate_t);
758 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_bits_per_sample_t);
759 DEFINE_ENUM_FLAG_OPERATORS(btav_a2dp_codec_channel_mode_t);
760 
761 #endif  // A2DP_CODEC_API_H
762