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