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 Codec API for AAC
19 //
20 
21 #ifndef A2DP_AAC_H
22 #define A2DP_AAC_H
23 
24 #include <stdint.h>
25 
26 #include "a2dp_aac_constants.h"
27 #include "a2dp_codec_api.h"
28 #include "avdt_api.h"
29 #include "internal_include/bt_target.h"
30 #include "stack/include/bt_hdr.h"
31 
32 class A2dpCodecConfigAacBase : public A2dpCodecConfig {
33  protected:
A2dpCodecConfigAacBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)34   A2dpCodecConfigAacBase(btav_a2dp_codec_index_t codec_index,
35                          const std::string& name,
36                          btav_a2dp_codec_priority_t codec_priority,
37                          bool is_source)
38       : A2dpCodecConfig(codec_index, A2DP_CODEC_ID_AAC, name, codec_priority),
39         is_source_(is_source) {}
40   bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability,
41                       uint8_t* p_result_codec_config) override;
42   bool setPeerCodecCapabilities(
43       const uint8_t* p_peer_codec_capabilities) override;
44 
45  private:
46   bool is_source_;  // True if local is Source
47 };
48 
49 class A2dpCodecConfigAacSource : public A2dpCodecConfigAacBase {
50  public:
51   A2dpCodecConfigAacSource(btav_a2dp_codec_priority_t codec_priority);
52   virtual ~A2dpCodecConfigAacSource();
53 
54   bool init() override;
55 
56  private:
57   bool useRtpHeaderMarkerBit() const override;
58   void debug_codec_dump(int fd) override;
59 };
60 
61 class A2dpCodecConfigAacSink : public A2dpCodecConfigAacBase {
62  public:
63   A2dpCodecConfigAacSink(btav_a2dp_codec_priority_t codec_priority);
64   virtual ~A2dpCodecConfigAacSink();
65 
66   bool init() override;
67 
68  private:
69   bool useRtpHeaderMarkerBit() const override;
70 };
71 
72 // Checks whether the codec capabilities contain a valid A2DP AAC Source
73 // codec.
74 // NOTE: only codecs that are implemented are considered valid.
75 // Returns true if |p_codec_info| contains information about a valid AAC
76 // codec, otherwise false.
77 bool A2DP_IsSourceCodecValidAac(const uint8_t* p_codec_info);
78 
79 // Checks whether the codec capabilities contain a valid A2DP AAC Sink codec.
80 // NOTE: only codecs that are implemented are considered valid.
81 // Returns true if |p_codec_info| contains information about a valid AAC codec,
82 // otherwise false.
83 bool A2DP_IsSinkCodecValidAac(const uint8_t* p_codec_info);
84 
85 // Checks whether the codec capabilities contain a valid peer A2DP AAC Source
86 // codec.
87 // NOTE: only codecs that are implemented are considered valid.
88 // Returns true if |p_codec_info| contains information about a valid AAC codec,
89 // otherwise false.
90 bool A2DP_IsPeerSourceCodecValidAac(const uint8_t* p_codec_info);
91 
92 // Checks whether the codec capabilities contain a valid peer A2DP AAC Sink
93 // codec.
94 // NOTE: only codecs that are implemented are considered valid.
95 // Returns true if |p_codec_info| contains information about a valid AAC
96 // codec, otherwise false.
97 bool A2DP_IsPeerSinkCodecValidAac(const uint8_t* p_codec_info);
98 
99 // Checks whether A2DP AAC Sink codec is supported.
100 // |p_codec_info| contains information about the codec capabilities.
101 // Returns true if the A2DP AAC Sink codec is supported, otherwise false.
102 bool A2DP_IsSinkCodecSupportedAac(const uint8_t* p_codec_info);
103 
104 // Checks whether an A2DP AAC Source codec for a peer Source device is
105 // supported.
106 // |p_codec_info| contains information about the codec capabilities of the
107 // peer device.
108 // Returns true if the A2DP AAC Source codec for a peer Source device is
109 // supported, otherwise false.
110 bool A2DP_IsPeerSourceCodecSupportedAac(const uint8_t* p_codec_info);
111 
112 // Checks whether the A2DP data packets should contain RTP header.
113 // |content_protection_enabled| is true if Content Protection is
114 // enabled. |p_codec_info| contains information about the codec capabilities.
115 // Returns true if the A2DP data packets should contain RTP header, otherwise
116 // false.
117 bool A2DP_UsesRtpHeaderAac(bool content_protection_enabled,
118                            const uint8_t* p_codec_info);
119 
120 // Gets the A2DP AAC codec name for a given |p_codec_info|.
121 const char* A2DP_CodecNameAac(const uint8_t* p_codec_info);
122 
123 // Checks whether two A2DP AAC codecs |p_codec_info_a| and |p_codec_info_b|
124 // have the same type.
125 // Returns true if the two codecs have the same type, otherwise false.
126 bool A2DP_CodecTypeEqualsAac(const uint8_t* p_codec_info_a,
127                              const uint8_t* p_codec_info_b);
128 
129 // Checks whether two A2DP AAC codecs |p_codec_info_a| and |p_codec_info_b|
130 // are exactly the same.
131 // Returns true if the two codecs are exactly the same, otherwise false.
132 // If the codec type is not AAC, the return value is false.
133 bool A2DP_CodecEqualsAac(const uint8_t* p_codec_info_a,
134                          const uint8_t* p_codec_info_b);
135 
136 // Gets the track sample rate value for the A2DP AAC codec.
137 // |p_codec_info| is a pointer to the AAC codec_info to decode.
138 // Returns the track sample rate on success, or -1 if |p_codec_info|
139 // contains invalid codec information.
140 int A2DP_GetTrackSampleRateAac(const uint8_t* p_codec_info);
141 
142 // Gets the track bits per sample value for the A2DP AAC codec.
143 // |p_codec_info| is a pointer to the AAC codec_info to decode.
144 // Returns the track bits per sample on success, or -1 if |p_codec_info|
145 // contains invalid codec information.
146 int A2DP_GetTrackBitsPerSampleAac(const uint8_t* p_codec_info);
147 
148 // Gets the channel count for the A2DP AAC codec.
149 // |p_codec_info| is a pointer to the AAC codec_info to decode.
150 // Returns the channel count on success, or -1 if |p_codec_info|
151 // contains invalid codec information.
152 int A2DP_GetTrackChannelCountAac(const uint8_t* p_codec_info);
153 
154 // Gets the channel type for the A2DP AAC Sink codec:
155 // 1 for mono, or 3 for dual/stereo/joint.
156 // |p_codec_info| is a pointer to the AAC codec_info to decode.
157 // Returns the channel type on success, or -1 if |p_codec_info|
158 // contains invalid codec information.
159 int A2DP_GetSinkTrackChannelTypeAac(const uint8_t* p_codec_info);
160 
161 // Gets the object type code for the A2DP AAC codec.
162 // The actual value is codec-specific - see |A2DP_AAC_OBJECT_TYPE_*|.
163 // |p_codec_info| is a pointer to the AAC codec_info to decode.
164 // Returns the object type code on success, or -1 if |p_codec_info|
165 // contains invalid codec information.
166 int A2DP_GetObjectTypeCodeAac(const uint8_t* p_codec_info);
167 
168 // Gets the channel mode code for the A2DP AAC codec.
169 // The actual value is codec-specific - see |A2DP_AAC_CHANNEL_MODE_*|.
170 // |p_codec_info| is a pointer to the AAC codec_info to decode.
171 // Returns the channel mode code on success, or -1 if |p_codec_info|
172 // contains invalid codec information.
173 int A2DP_GetChannelModeCodeAac(const uint8_t* p_codec_info);
174 
175 // Gets the variable bit rate support for the A2DP AAC codec.
176 // The actual value is codec-specific - see |A2DP_AAC_VARIABLE_BIT_RATE_*|.
177 // |p_codec_info| is a pointer to the AAC codec_info to decode.
178 // Returns the variable bit rate support on success, or -1 if |p_codec_info|
179 // contains invalid codec information.
180 int A2DP_GetVariableBitRateSupportAac(const uint8_t* p_codec_info);
181 
182 // Gets the bit rate for the A2DP AAC codec.
183 // The actual value is codec-specific - for AAC it is in bits per second.
184 // |p_codec_info| is a pointer to the AAC codec_info to decode.
185 // Returns the bit rate on success, or -1 if |p_codec_info|
186 // contains invalid codec information.
187 int A2DP_GetBitRateAac(const uint8_t* p_codec_info);
188 
189 // Computes the maximum bit rate for the A2DP AAC codec based on the MTU.
190 // The actual value is codec-specific - for AAC it is in bits per second.
191 // |p_codec_info| is a pointer to the AAC codec_info to decode.
192 // |mtu| is the MTU to use for the computation.
193 // Returns the maximum bit rate on success, or -1 if |p_codec_info|
194 // contains invalid codec information.
195 int A2DP_ComputeMaxBitRateAac(const uint8_t* p_codec_info, uint16_t mtu);
196 
197 // Gets the A2DP AAC audio data timestamp from an audio packet.
198 // |p_codec_info| contains the codec information.
199 // |p_data| contains the audio data.
200 // The timestamp is stored in |p_timestamp|.
201 // Returns true on success, otherwise false.
202 bool A2DP_GetPacketTimestampAac(const uint8_t* p_codec_info,
203                                 const uint8_t* p_data, uint32_t* p_timestamp);
204 
205 // Builds A2DP AAC codec header for audio data.
206 // |p_codec_info| contains the codec information.
207 // |p_buf| contains the audio data.
208 // |frames_per_packet| is the number of frames in this packet.
209 // Returns true on success, otherwise false.
210 bool A2DP_BuildCodecHeaderAac(const uint8_t* p_codec_info, BT_HDR* p_buf,
211                               uint16_t frames_per_packet);
212 
213 // Decodes A2DP AAC codec info into a human readable string.
214 // |p_codec_info| is a pointer to the AAC codec_info to decode.
215 // Returns a string describing the codec information.
216 std::string A2DP_CodecInfoStringAac(const uint8_t* p_codec_info);
217 
218 // Gets the A2DP AAC encoder interface that can be used to encode and prepare
219 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
220 // |p_codec_info| contains the codec information.
221 // Returns the A2DP AAC encoder interface if the |p_codec_info| is valid and
222 // supported, otherwise NULL.
223 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceAac(
224     const uint8_t* p_codec_info);
225 
226 // Gets the current A2DP AAC decoder interface that can be used to decode
227 // received A2DP packets - see |tA2DP_DECODER_INTERFACE|.
228 // |p_codec_info| contains the codec information.
229 // Returns the A2DP AAC decoder interface if the |p_codec_info| is valid and
230 // supported, otherwise NULL.
231 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceAac(
232     const uint8_t* p_codec_info);
233 
234 // Adjusts the A2DP AAC codec, based on local support and Bluetooth
235 // specification.
236 // |p_codec_info| contains the codec information to adjust.
237 // Returns true if |p_codec_info| is valid and supported, otherwise false.
238 bool A2DP_AdjustCodecAac(uint8_t* p_codec_info);
239 
240 // Gets the A2DP AAC Source codec index for a given |p_codec_info|.
241 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
242 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
243 btav_a2dp_codec_index_t A2DP_SourceCodecIndexAac(const uint8_t* p_codec_info);
244 
245 // Gets the A2DP AAC Sink codec index for a given |p_codec_info|.
246 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
247 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
248 btav_a2dp_codec_index_t A2DP_SinkCodecIndexAac(const uint8_t* p_codec_info);
249 
250 // Gets the A2DP AAC Source codec name.
251 const char* A2DP_CodecIndexStrAac(void);
252 
253 // Gets the A2DP AAC Sink codec name.
254 const char* A2DP_CodecIndexStrAacSink(void);
255 
256 // Initializes A2DP AAC Source codec information into |AvdtpSepConfig|
257 // configuration entry pointed by |p_cfg|.
258 bool A2DP_InitCodecConfigAac(AvdtpSepConfig* p_cfg);
259 
260 // Initializes A2DP AAC Sink codec information into |AvdtpSepConfig|
261 // configuration entry pointed by |p_cfg|.
262 bool A2DP_InitCodecConfigAacSink(AvdtpSepConfig* p_cfg);
263 
264 #endif  // A2DP_AAC_H
265