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