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