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 "a2dp_codec_api.h"
25 #include "a2dp_sbc_constants.h"
26 #include "avdt_api.h"
27 
28 class A2dpCodecConfigSbcBase : public A2dpCodecConfig {
29  protected:
A2dpCodecConfigSbcBase(btav_a2dp_codec_index_t codec_index,const std::string & name,btav_a2dp_codec_priority_t codec_priority,bool is_source)30   A2dpCodecConfigSbcBase(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 A2dpCodecConfigSbcSource : public A2dpCodecConfigSbcBase {
46  public:
47   A2dpCodecConfigSbcSource(btav_a2dp_codec_priority_t codec_priority);
48   virtual ~A2dpCodecConfigSbcSource();
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 A2dpCodecConfigSbcSink : public A2dpCodecConfigSbcBase {
64  public:
65   A2dpCodecConfigSbcSink(btav_a2dp_codec_priority_t codec_priority);
66   virtual ~A2dpCodecConfigSbcSink();
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 SBC Source codec.
81 // NOTE: only codecs that are implemented are considered valid.
82 // Returns true if |p_codec_info| contains information about a valid SBC codec,
83 // otherwise false.
84 bool A2DP_IsSourceCodecValidSbc(const uint8_t* p_codec_info);
85 
86 // Checks whether the codec capabilities contain a valid A2DP SBC Sink 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_IsSinkCodecValidSbc(const uint8_t* p_codec_info);
91 
92 // Checks whether the codec capabilities contain a valid peer A2DP SBC Source
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_IsPeerSourceCodecValidSbc(const uint8_t* p_codec_info);
98 
99 // Checks whether the codec capabilities contain a valid peer A2DP SBC Sink
100 // codec.
101 // NOTE: only codecs that are implemented are considered valid.
102 // Returns true if |p_codec_info| contains information about a valid SBC codec,
103 // otherwise false.
104 bool A2DP_IsPeerSinkCodecValidSbc(const uint8_t* p_codec_info);
105 
106 // Checks whether A2DP SBC Sink codec is supported.
107 // |p_codec_info| contains information about the codec capabilities.
108 // Returns true if the A2DP SBC Sink codec is supported, otherwise false.
109 bool A2DP_IsSinkCodecSupportedSbc(const uint8_t* p_codec_info);
110 
111 // Checks whether an A2DP SBC Source codec for a peer Source device is
112 // supported.
113 // |p_codec_info| contains information about the codec capabilities of the
114 // peer device.
115 // Returns true if the A2DP SBC Source codec for a peer Source device is
116 // supported, otherwise false.
117 bool A2DP_IsPeerSourceCodecSupportedSbc(const uint8_t* p_codec_info);
118 
119 // Initialize state with the default A2DP SBC codec.
120 // The initialized state with the codec capabilities is stored in
121 // |p_codec_info|.
122 void A2DP_InitDefaultCodecSbc(uint8_t* p_codec_info);
123 
124 // Gets the A2DP SBC codec name for a given |p_codec_info|.
125 const char* A2DP_CodecNameSbc(const uint8_t* p_codec_info);
126 
127 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
128 // have the same type.
129 // Returns true if the two codecs have the same type, otherwise false.
130 bool A2DP_CodecTypeEqualsSbc(const uint8_t* p_codec_info_a,
131                              const uint8_t* p_codec_info_b);
132 
133 // Checks whether two A2DP SBC codecs |p_codec_info_a| and |p_codec_info_b|
134 // are exactly the same.
135 // Returns true if the two codecs are exactly the same, otherwise false.
136 // If the codec type is not SBC, the return value is false.
137 bool A2DP_CodecEqualsSbc(const uint8_t* p_codec_info_a,
138                          const uint8_t* p_codec_info_b);
139 
140 // Gets the track sample rate value for the A2DP SBC codec.
141 // |p_codec_info| is a pointer to the SBC codec_info to decode.
142 // Returns the track sample rate on success, or -1 if |p_codec_info|
143 // contains invalid codec information.
144 int A2DP_GetTrackSampleRateSbc(const uint8_t* p_codec_info);
145 
146 // Gets the track bits per sample value for the A2DP SBC codec.
147 // |p_codec_info| is a pointer to the SBC codec_info to decode.
148 // Returns the track bits per sample on success, or -1 if |p_codec_info|
149 // contains invalid codec information.
150 int A2DP_GetTrackBitsPerSampleSbc(const uint8_t* p_codec_info);
151 
152 // Gets the channel count for the A2DP SBC codec.
153 // |p_codec_info| is a pointer to the SBC codec_info to decode.
154 // Returns the channel count on success, or -1 if |p_codec_info|
155 // contains invalid codec information.
156 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info);
157 
158 // Gets the number of subbands for the A2DP SBC codec.
159 // |p_codec_info| is a pointer to the SBC codec_info to decode.
160 // Returns the number of subbands on success, or -1 if |p_codec_info|
161 // contains invalid codec information.
162 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info);
163 
164 // Gets the number of blocks for the A2DP SBC codec.
165 // |p_codec_info| is a pointer to the SBC codec_info to decode.
166 // Returns the number of blocks on success, or -1 if |p_codec_info|
167 // contains invalid codec information.
168 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info);
169 
170 // Gets the allocation method code for the A2DP SBC codec.
171 // The actual value is codec-specific - see |A2DP_SBC_IE_ALLOC_MD_*|.
172 // |p_codec_info| is a pointer to the SBC codec_info to decode.
173 // Returns the allocation method code on success, or -1 if |p_codec_info|
174 // contains invalid codec information.
175 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info);
176 
177 // Gets the channel mode code for the A2DP SBC codec.
178 // The actual value is codec-specific - see |A2DP_SBC_IE_CH_MD_*|.
179 // |p_codec_info| is a pointer to the SBC codec_info to decode.
180 // Returns the channel mode code on success, or -1 if |p_codec_info|
181 // contains invalid codec information.
182 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info);
183 
184 // Gets the sampling frequency code for the A2DP SBC codec.
185 // The actual value is codec-specific - see |A2DP_SBC_IE_SAMP_FREQ_*|.
186 // |p_codec_info| is a pointer to the SBC codec_info to decode.
187 // Returns the sampling frequency code on success, or -1 if |p_codec_info|
188 // contains invalid codec information.
189 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info);
190 
191 // Gets the minimum bitpool for the A2DP SBC codec.
192 // The actual value is codec-specific - see |A2DP_SBC_IE_MIN_BITPOOL|.
193 // |p_codec_info| is a pointer to the SBC codec_info to decode.
194 // Returns the minimum bitpool on success, or -1 if |p_codec_info|
195 // contains invalid codec information.
196 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info);
197 
198 // Gets the maximum bitpool for the A2DP SBC codec.
199 // The actual value is codec-specific - see |A2DP_SBC_IE_MAX_BITPOOL|.
200 // |p_codec_info| is a pointer to the SBC codec_info to decode.
201 // Returns the maximum bitpool on success, or -1 if |p_codec_info|
202 // contains invalid codec information.
203 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info);
204 
205 // Gets the channel type for the A2DP SBC Sink codec:
206 // 1 for mono, or 3 for dual/stereo/joint.
207 // |p_codec_info| is a pointer to the SBC codec_info to decode.
208 // Returns the channel type on success, or -1 if |p_codec_info|
209 // contains invalid codec information.
210 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info);
211 
212 // Gets the A2DP SBC audio data timestamp from an audio packet.
213 // |p_codec_info| contains the codec information.
214 // |p_data| contains the audio data.
215 // The timestamp is stored in |p_timestamp|.
216 // Returns true on success, otherwise false.
217 bool A2DP_GetPacketTimestampSbc(const uint8_t* p_codec_info,
218                                 const uint8_t* p_data, uint32_t* p_timestamp);
219 
220 // Builds A2DP SBC codec header for audio data.
221 // |p_codec_info| contains the codec information.
222 // |p_buf| contains the audio data.
223 // |frames_per_packet| is the number of frames in this packet.
224 // Returns true on success, otherwise false.
225 bool A2DP_BuildCodecHeaderSbc(const uint8_t* p_codec_info, BT_HDR* p_buf,
226                               uint16_t frames_per_packet);
227 
228 // Decodes A2DP SBC codec info into a human readable string.
229 // |p_codec_info| is a pointer to the SBC codec_info to decode.
230 // Returns a string describing the codec information.
231 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info);
232 
233 // Gets the A2DP SBC encoder interface that can be used to encode and prepare
234 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
235 // |p_codec_info| contains the codec information.
236 // Returns the A2DP SBC encoder interface if the |p_codec_info| is valid and
237 // supported, otherwise NULL.
238 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
239     const uint8_t* p_codec_info);
240 
241 // Gets the A2DP SBC decoder interface that can be used to decode received A2DP
242 // packets - see |tA2DP_DECODER_INTERFACE|.
243 // |p_codec_info| contains the codec information.
244 // Returns the A2DP SBC decoder interface if the |p_codec_info| is valid and
245 // supported, otherwise NULL.
246 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
247     const uint8_t* p_codec_info);
248 
249 // Adjusts the A2DP SBC codec, based on local support and Bluetooth
250 // specification.
251 // |p_codec_info| contains the codec information to adjust.
252 // Returns true if |p_codec_info| is valid and supported, otherwise false.
253 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info);
254 
255 // Gets the A2DP SBC Source codec index for a given |p_codec_info|.
256 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
257 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
258 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(const uint8_t* p_codec_info);
259 
260 // Gets the A2DP SBC Sink codec index for a given |p_codec_info|.
261 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
262 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
263 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(const uint8_t* p_codec_info);
264 
265 // Gets the A2DP SBC Source codec name.
266 const char* A2DP_CodecIndexStrSbc(void);
267 
268 // Gets the A2DP SBC Sink codec name.
269 const char* A2DP_CodecIndexStrSbcSink(void);
270 
271 // Initializes A2DP SBC Source codec information into |AvdtpSepConfig|
272 // configuration entry pointed by |p_cfg|.
273 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg);
274 
275 // Initializes A2DP SBC Sink codec information into |AvdtpSepConfig|
276 // configuration entry pointed by |p_cfg|.
277 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg);
278 
279 // Get SBC bitrate
280 // Returns |uint32_t| bitrate value in bits per second
281 uint32_t A2DP_GetBitrateSbc();
282 
283 #endif  // A2DP_SBC_H
284