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   period_ms_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   period_ms_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 channel count for the A2DP SBC codec.
147 // |p_codec_info| is a pointer to the SBC codec_info to decode.
148 // Returns the channel count on success, or -1 if |p_codec_info|
149 // contains invalid codec information.
150 int A2DP_GetTrackChannelCountSbc(const uint8_t* p_codec_info);
151 
152 // Gets the number of subbands for the A2DP SBC codec.
153 // |p_codec_info| is a pointer to the SBC codec_info to decode.
154 // Returns the number of subbands on success, or -1 if |p_codec_info|
155 // contains invalid codec information.
156 int A2DP_GetNumberOfSubbandsSbc(const uint8_t* p_codec_info);
157 
158 // Gets the number of blocks for the A2DP SBC codec.
159 // |p_codec_info| is a pointer to the SBC codec_info to decode.
160 // Returns the number of blocks on success, or -1 if |p_codec_info|
161 // contains invalid codec information.
162 int A2DP_GetNumberOfBlocksSbc(const uint8_t* p_codec_info);
163 
164 // Gets the allocation method code for the A2DP SBC codec.
165 // The actual value is codec-specific - see |A2DP_SBC_IE_ALLOC_MD_*|.
166 // |p_codec_info| is a pointer to the SBC codec_info to decode.
167 // Returns the allocation method code on success, or -1 if |p_codec_info|
168 // contains invalid codec information.
169 int A2DP_GetAllocationMethodCodeSbc(const uint8_t* p_codec_info);
170 
171 // Gets the channel mode code for the A2DP SBC codec.
172 // The actual value is codec-specific - see |A2DP_SBC_IE_CH_MD_*|.
173 // |p_codec_info| is a pointer to the SBC codec_info to decode.
174 // Returns the channel mode code on success, or -1 if |p_codec_info|
175 // contains invalid codec information.
176 int A2DP_GetChannelModeCodeSbc(const uint8_t* p_codec_info);
177 
178 // Gets the sampling frequency code for the A2DP SBC codec.
179 // The actual value is codec-specific - see |A2DP_SBC_IE_SAMP_FREQ_*|.
180 // |p_codec_info| is a pointer to the SBC codec_info to decode.
181 // Returns the sampling frequency code on success, or -1 if |p_codec_info|
182 // contains invalid codec information.
183 int A2DP_GetSamplingFrequencyCodeSbc(const uint8_t* p_codec_info);
184 
185 // Gets the minimum bitpool for the A2DP SBC codec.
186 // The actual value is codec-specific - see |A2DP_SBC_IE_MIN_BITPOOL|.
187 // |p_codec_info| is a pointer to the SBC codec_info to decode.
188 // Returns the minimum bitpool on success, or -1 if |p_codec_info|
189 // contains invalid codec information.
190 int A2DP_GetMinBitpoolSbc(const uint8_t* p_codec_info);
191 
192 // Gets the maximum bitpool for the A2DP SBC codec.
193 // The actual value is codec-specific - see |A2DP_SBC_IE_MAX_BITPOOL|.
194 // |p_codec_info| is a pointer to the SBC codec_info to decode.
195 // Returns the maximum bitpool on success, or -1 if |p_codec_info|
196 // contains invalid codec information.
197 int A2DP_GetMaxBitpoolSbc(const uint8_t* p_codec_info);
198 
199 // Gets the channel type for the A2DP SBC Sink codec:
200 // 1 for mono, or 3 for dual/stereo/joint.
201 // |p_codec_info| is a pointer to the SBC codec_info to decode.
202 // Returns the channel type on success, or -1 if |p_codec_info|
203 // contains invalid codec information.
204 int A2DP_GetSinkTrackChannelTypeSbc(const uint8_t* p_codec_info);
205 
206 // Gets the A2DP SBC audio data timestamp from an audio packet.
207 // |p_codec_info| contains the codec information.
208 // |p_data| contains the audio data.
209 // The timestamp is stored in |p_timestamp|.
210 // Returns true on success, otherwise false.
211 bool A2DP_GetPacketTimestampSbc(const uint8_t* p_codec_info,
212                                 const uint8_t* p_data, uint32_t* p_timestamp);
213 
214 // Builds A2DP SBC codec header for audio data.
215 // |p_codec_info| contains the codec information.
216 // |p_buf| contains the audio data.
217 // |frames_per_packet| is the number of frames in this packet.
218 // Returns true on success, otherwise false.
219 bool A2DP_BuildCodecHeaderSbc(const uint8_t* p_codec_info, BT_HDR* p_buf,
220                               uint16_t frames_per_packet);
221 
222 // Decodes A2DP SBC codec info into a human readable string.
223 // |p_codec_info| is a pointer to the SBC codec_info to decode.
224 // Returns a string describing the codec information.
225 std::string A2DP_CodecInfoStringSbc(const uint8_t* p_codec_info);
226 
227 // Gets the A2DP SBC encoder interface that can be used to encode and prepare
228 // A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|.
229 // |p_codec_info| contains the codec information.
230 // Returns the A2DP SBC encoder interface if the |p_codec_info| is valid and
231 // supported, otherwise NULL.
232 const tA2DP_ENCODER_INTERFACE* A2DP_GetEncoderInterfaceSbc(
233     const uint8_t* p_codec_info);
234 
235 // Gets the A2DP SBC decoder interface that can be used to decode received A2DP
236 // packets - see |tA2DP_DECODER_INTERFACE|.
237 // |p_codec_info| contains the codec information.
238 // Returns the A2DP SBC decoder interface if the |p_codec_info| is valid and
239 // supported, otherwise NULL.
240 const tA2DP_DECODER_INTERFACE* A2DP_GetDecoderInterfaceSbc(
241     const uint8_t* p_codec_info);
242 
243 // Adjusts the A2DP SBC codec, based on local support and Bluetooth
244 // specification.
245 // |p_codec_info| contains the codec information to adjust.
246 // Returns true if |p_codec_info| is valid and supported, otherwise false.
247 bool A2DP_AdjustCodecSbc(uint8_t* p_codec_info);
248 
249 // Gets the A2DP SBC Source codec index for a given |p_codec_info|.
250 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
251 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
252 btav_a2dp_codec_index_t A2DP_SourceCodecIndexSbc(const uint8_t* p_codec_info);
253 
254 // Gets the A2DP SBC Sink codec index for a given |p_codec_info|.
255 // Returns the corresponding |btav_a2dp_codec_index_t| on success,
256 // otherwise |BTAV_A2DP_CODEC_INDEX_MAX|.
257 btav_a2dp_codec_index_t A2DP_SinkCodecIndexSbc(const uint8_t* p_codec_info);
258 
259 // Gets the A2DP SBC Source codec name.
260 const char* A2DP_CodecIndexStrSbc(void);
261 
262 // Gets the A2DP SBC Sink codec name.
263 const char* A2DP_CodecIndexStrSbcSink(void);
264 
265 // Initializes A2DP SBC Source codec information into |AvdtpSepConfig|
266 // configuration entry pointed by |p_cfg|.
267 bool A2DP_InitCodecConfigSbc(AvdtpSepConfig* p_cfg);
268 
269 // Initializes A2DP SBC Sink codec information into |AvdtpSepConfig|
270 // configuration entry pointed by |p_cfg|.
271 bool A2DP_InitCodecConfigSbcSink(AvdtpSepConfig* p_cfg);
272 
273 // Get SBC bitrate
274 // Returns |uint32_t| bitrate value in bits per second
275 uint32_t A2DP_GetBitrateSbc();
276 
277 #endif  // A2DP_SBC_H
278