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 low complexity subband codec (SBC)
19 //
20 
21 #ifndef A2DP_SBC_H
22 #define A2DP_SBC_H
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include "a2dp_codec_api.h"
28 #include "a2dp_sbc_constants.h"
29 #include "avdt_api.h"
30 #include "internal_include/bt_target.h"
31 #include "stack/include/bt_hdr.h"
32 
33 class A2dpCodecConfigSbcBase : public A2dpCodecConfig {
34  protected:
A2dpCodecConfigSbcBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)35   A2dpCodecConfigSbcBase(btav_a2dp_codec_index_t codec_index,
36                          const std::string& name,
37                          btav_a2dp_codec_priority_t codec_priority,
38                          bool is_source)
39       : A2dpCodecConfig(codec_index, A2DP_CODEC_ID_SBC, name, codec_priority),
40         is_source_(is_source) {}
41   bool setCodecConfig(const uint8_t* p_peer_codec_info, bool is_capability,
42                       uint8_t* p_result_codec_config) override;
43   bool setPeerCodecCapabilities(
44       const uint8_t* p_peer_codec_capabilities) override;
45 
46  private:
47   bool is_source_;  // True if local is Source
48 };
49 
50 class A2dpCodecConfigSbcSource : public A2dpCodecConfigSbcBase {
51  public:
52   A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority);
53   virtual ~A2dpCodecConfigSbcSource();
54 
55   bool init() override;
56 
57  private:
58   bool useRtpHeaderMarkerBit() const override;
59   void debug_codec_dump(int fd) override;
60 };
61 
62 class A2dpCodecConfigSbcSink : public A2dpCodecConfigSbcBase {
63  public:
64   A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority);
65   virtual ~A2dpCodecConfigSbcSink();
66 
67   bool init() override;
68 
69  private:
70   bool useRtpHeaderMarkerBit() const override;
71 };
72 
73 // Checks whether the codec capabilities contain a valid A2DP SBC Source codec.
74 // NOTE: only codecs that are implemented are considered valid.
75 // Returns true if |p_codec_info| contains information about a valid SBC codec,
76 // otherwise false.
77 bool A2DP_IsSourceCodecValidSbc(const uint8_t* p_codec_info);
78 
79 // Checks whether the codec capabilities contain a valid A2DP SBC Sink codec.
80 // NOTE: only codecs that are implemented are considered valid.
81 // Returns true if |p_codec_info| contains information about a valid SBC codec,
82 // otherwise false.
83 bool A2DP_IsSinkCodecValidSbc(const uint8_t* p_codec_info);
84 
85 // Checks whether the codec capabilities contain a valid peer A2DP SBC 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 SBC codec,
89 // otherwise false.
90 bool A2DP_IsPeerSourceCodecValidSbc(const uint8_t* p_codec_info);
91 
92 // Checks whether the codec capabilities contain a valid peer A2DP SBC 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 SBC codec,
96 // otherwise false.
97 bool A2DP_IsPeerSinkCodecValidSbc(const uint8_t* p_codec_info);
98 
99 // Checks whether A2DP SBC Sink codec is supported.
100 // |p_codec_info| contains information about the codec capabilities.
101 // Returns true if the A2DP SBC Sink codec is supported, otherwise false.
102 bool A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info);
103 
104 // Checks whether an A2DP SBC 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 SBC Source codec for a peer Source device is
109 // supported, otherwise false.
110 bool A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t* p_codec_info);
111 
112 // Initialize state with the default A2DP SBC codec.
113 // The initialized state with the codec capabilities is stored in
114 // |p_codec_info|.
115 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info);
116 
117 // Gets the A2DP SBC codec name for a given |p_codec_info|.
118 const char* A2DP_CodecNameSbc(const uint8_t* p_codec_info);
119 
120 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
121 // have the same type.
122 // Returns true if the two codecs have the same type, otherwise false.
123 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a,
124                              const uint8_t* p_codec_info_b);
125 
126 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
127 // are exactly the same.
128 // Returns true if the two codecs are exactly the same, otherwise false.
129 // If the codec type is not SBC, the return value is false.
130 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a,
131                          const uint8_t* p_codec_info_b);
132 
133 // Gets the track sample rate value for the A2DP SBC codec.
134 // |p_codec_info| is a pointer to the SBC codec_info to decode.
135 // Returns the track sample rate on success, or -1 if |p_codec_info|
136 // contains invalid codec information.
137 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info);
138 
139 // Gets the track bits per sample value for the A2DP SBC codec.
140 // |p_codec_info| is a pointer to the SBC codec_info to decode.
141 // Returns the track bits per sample on success, or -1 if |p_codec_info|
142 // contains invalid codec information.
143 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info);
144 
145 // Gets the channel count for the A2DP SBC codec.
146 // |p_codec_info| is a pointer to the SBC codec_info to decode.
147 // Returns the channel count on success, or -1 if |p_codec_info|
148 // contains invalid codec information.
149 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info);
150 
151 // Gets the number of subbands for the A2DP SBC codec.
152 // |p_codec_info| is a pointer to the SBC codec_info to decode.
153 // Returns the number of subbands on success, or -1 if |p_codec_info|
154 // contains invalid codec information.
155 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info);
156 
157 // Gets the number of blocks for the A2DP SBC codec.
158 // |p_codec_info| is a pointer to the SBC codec_info to decode.
159 // Returns the number of blocks on success, or -1 if |p_codec_info|
160 // contains invalid codec information.
161 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info);
162 
163 // Gets the allocation method code for the A2DP SBC codec.
164 // The actual value is codec-specific - see |A2DP_SBC_IE_ALLOC_MD_*|.
165 // |p_codec_info| is a pointer to the SBC codec_info to decode.
166 // Returns the allocation method code on success, or -1 if |p_codec_info|
167 // contains invalid codec information.
168 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info);
169 
170 // Gets the channel mode code for the A2DP SBC codec.
171 // The actual value is codec-specific - see |A2DP_SBC_IE_CH_MD_*|.
172 // |p_codec_info| is a pointer to the SBC codec_info to decode.
173 // Returns the channel mode code on success, or -1 if |p_codec_info|
174 // contains invalid codec information.
175 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info);
176 
177 // Gets the sampling frequency code for the A2DP SBC codec.
178 // The actual value is codec-specific - see |A2DP_SBC_IE_SAMP_FREQ_*|.
179 // |p_codec_info| is a pointer to the SBC codec_info to decode.
180 // Returns the sampling frequency code on success, or -1 if |p_codec_info|
181 // contains invalid codec information.
182 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info);
183 
184 // Gets the minimum bitpool for the A2DP SBC codec.
185 // The actual value is codec-specific - see |A2DP_SBC_IE_MIN_BITPOOL|.
186 // |p_codec_info| is a pointer to the SBC codec_info to decode.
187 // Returns the minimum bitpool on success, or -1 if |p_codec_info|
188 // contains invalid codec information.
189 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info);
190 
191 // Gets the maximum bitpool for the A2DP SBC codec.
192 // The actual value is codec-specific - see |A2DP_SBC_IE_MAX_BITPOOL|.
193 // |p_codec_info| is a pointer to the SBC codec_info to decode.
194 // Returns the maximum bitpool on success, or -1 if |p_codec_info|
195 // contains invalid codec information.
196 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info);
197 
198 // Gets the channel type for the A2DP SBC Sink codec:
199 // 1 for mono, or 3 for dual/stereo/joint.
200 // |p_codec_info| is a pointer to the SBC codec_info to decode.
201 // Returns the channel type on success, or -1 if |p_codec_info|
202 // contains invalid codec information.
203 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info);
204 
205 // Gets the A2DP SBC 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_GetPacketTimestampSbc(const uint8_t* p_codec_info,
211                                 const uint8_t* p_data, uint32_t* p_timestamp);
212 
213 // Builds A2DP SBC 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_BuildCodecHeaderSbc(const uint8_t* p_codec_info, BT_HDR* p_buf,
219                               uint16_t frames_per_packet);
220 
221 // Decodes A2DP SBC codec info into a human readable string.
222 // |p_codec_info| is a pointer to the SBC codec_info to decode.
223 // Returns a string describing the codec information.
224 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info);
225 
226 // Gets the A2DP SBC 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 SBC encoder interface if the |p_codec_info| is valid and
230 // supported, otherwise NULL.
231 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
232     const uint8_t* p_codec_info);
233 
234 // Gets the A2DP SBC decoder interface that can be used to decode received A2DP
235 // packets - see |tA2DP_DECODER_INTERFACE|.
236 // |p_codec_info| contains the codec information.
237 // Returns the A2DP SBC decoder interface if the |p_codec_info| is valid and
238 // supported, otherwise NULL.
239 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
240     const uint8_t* p_codec_info);
241 
242 // Adjusts the A2DP SBC 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_AdjustCodecSbc(uint8_t* p_codec_info);
247 
248 // Gets the A2DP SBC 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_SourceCodecIndexSbc(const uint8_t* p_codec_info);
252 
253 // Gets the A2DP SBC 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_SinkCodecIndexSbc(const uint8_t* p_codec_info);
257 
258 // Gets the A2DP SBC Source codec name.
259 const char* A2DP_CodecIndexStrSbc(void);
260 
261 // Gets the A2DP SBC Sink codec name.
262 const char* A2DP_CodecIndexStrSbcSink(void);
263 
264 // Initializes A2DP SBC Source codec information into |AvdtpSepConfig|
265 // configuration entry pointed by |p_cfg|.
266 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg);
267 
268 // Initializes A2DP SBC Sink codec information into |AvdtpSepConfig|
269 // configuration entry pointed by |p_cfg|.
270 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg);
271 
272 // Get SBC bitrate
273 // Returns |uint32_t| bitrate value in bits per second
274 uint32_t A2DP_GetBitrateSbc();
275 
276 #endif  // A2DP_SBC_H
277