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