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