1 /*
2  * Copyright (C) 2012 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 #ifndef ANDROID_INCLUDE_BT_AV_H
18 #define ANDROID_INCLUDE_BT_AV_H
19 
20 #include <bluetooth/log.h>
21 #include <hardware/bluetooth.h>
22 
23 #include <optional>
24 #include <sstream>
25 #include <vector>
26 
27 #include "types/raw_address.h"
28 
29 __BEGIN_DECLS
30 
31 // Must be kept in sync with BluetoothProfile.java
32 typedef enum {
33   BTAV_CONNECTION_STATE_DISCONNECTED = 0,
34   BTAV_CONNECTION_STATE_CONNECTING,
35   BTAV_CONNECTION_STATE_CONNECTED,
36   BTAV_CONNECTION_STATE_DISCONNECTING
37 } btav_connection_state_t;
38 
39 /* Bluetooth AV datapath states */
40 typedef enum {
41   BTAV_AUDIO_STATE_REMOTE_SUSPEND = 0,
42   BTAV_AUDIO_STATE_STOPPED,
43   BTAV_AUDIO_STATE_STARTED,
44 } btav_audio_state_t;
45 
46 /*
47  * Enum values for each A2DP supported codec.
48  * There should be a separate entry for each A2DP codec that is supported
49  * for encoding (SRC), and for decoding purpose (SINK).
50  */
51 typedef enum {
52   BTAV_A2DP_CODEC_INDEX_SOURCE_MIN = 0,
53 
54   // Add an entry for each source codec here.
55   // NOTE: The values should be same as those listed in the following file:
56   //   BluetoothCodecConfig.java
57   BTAV_A2DP_CODEC_INDEX_SOURCE_SBC = 0,
58   BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
59   BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
60   BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
61   BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
62   BTAV_A2DP_CODEC_INDEX_SOURCE_LC3,
63   BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS,
64 
65   BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
66 
67   // Range of codec indexes reserved for Offload codec extensibility.
68   // Indexes in this range will be allocated for offloaded codecs
69   // that the stack does not recognize.
70   BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
71   BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX = BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN + 4,
72 
73   BTAV_A2DP_CODEC_INDEX_SINK_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX,
74 
75   // Add an entry for each sink codec here
76   BTAV_A2DP_CODEC_INDEX_SINK_SBC = BTAV_A2DP_CODEC_INDEX_SINK_MIN,
77   BTAV_A2DP_CODEC_INDEX_SINK_AAC,
78   BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
79   BTAV_A2DP_CODEC_INDEX_SINK_OPUS,
80 
81   BTAV_A2DP_CODEC_INDEX_SINK_MAX,
82 
83   // Range of codec indexes reserved for Offload codec extensibility.
84   // Indexes in this range will be allocated for offloaded codecs
85   // that the stack does not recognize.
86   BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN = BTAV_A2DP_CODEC_INDEX_SINK_MAX,
87   BTAV_A2DP_CODEC_INDEX_SINK_EXT_MAX = BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN + 4,
88 
89   BTAV_A2DP_CODEC_INDEX_MIN = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN,
90   BTAV_A2DP_CODEC_INDEX_MAX = BTAV_A2DP_CODEC_INDEX_SINK_EXT_MAX
91 } btav_a2dp_codec_index_t;
92 
93 typedef struct {
94   btav_a2dp_codec_index_t codec_type;
95   uint64_t codec_id;
96   std::string codec_name;
97 } btav_a2dp_codec_info_t;
98 
99 typedef enum {
100   // Disable the codec.
101   // NOTE: This value can be used only during initialization when
102   // function btif_av_source_init() is called.
103   BTAV_A2DP_CODEC_PRIORITY_DISABLED = -1,
104 
105   // Reset the codec priority to its default value.
106   BTAV_A2DP_CODEC_PRIORITY_DEFAULT = 0,
107 
108   // Highest codec priority.
109   BTAV_A2DP_CODEC_PRIORITY_HIGHEST = 1000 * 1000
110 } btav_a2dp_codec_priority_t;
111 
112 typedef enum {
113   BTAV_A2DP_CODEC_SAMPLE_RATE_NONE = 0x0,
114   BTAV_A2DP_CODEC_SAMPLE_RATE_44100 = 0x1 << 0,
115   BTAV_A2DP_CODEC_SAMPLE_RATE_48000 = 0x1 << 1,
116   BTAV_A2DP_CODEC_SAMPLE_RATE_88200 = 0x1 << 2,
117   BTAV_A2DP_CODEC_SAMPLE_RATE_96000 = 0x1 << 3,
118   BTAV_A2DP_CODEC_SAMPLE_RATE_176400 = 0x1 << 4,
119   BTAV_A2DP_CODEC_SAMPLE_RATE_192000 = 0x1 << 5,
120   BTAV_A2DP_CODEC_SAMPLE_RATE_16000 = 0x1 << 6,
121   BTAV_A2DP_CODEC_SAMPLE_RATE_24000 = 0x1 << 7
122 } btav_a2dp_codec_sample_rate_t;
123 
124 typedef enum {
125   BTAV_A2DP_CODEC_FRAME_SIZE_NONE = 0x0,
126   BTAV_A2DP_CODEC_FRAME_SIZE_20MS = 0x1 << 0,
127   BTAV_A2DP_CODEC_FRAME_SIZE_15MS = 0x1 << 1,
128   BTAV_A2DP_CODEC_FRAME_SIZE_10MS = 0x1 << 2,
129   BTAV_A2DP_CODEC_FRAME_SIZE_75MS = 0x1 << 3,
130 } btav_a2dp_codec_frame_size_t;
131 
132 typedef enum {
133   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE = 0x0,
134   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16 = 0x1 << 0,
135   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24 = 0x1 << 1,
136   BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32 = 0x1 << 2
137 } btav_a2dp_codec_bits_per_sample_t;
138 
139 typedef enum {
140   BTAV_A2DP_CODEC_CHANNEL_MODE_NONE = 0x0,
141   BTAV_A2DP_CODEC_CHANNEL_MODE_MONO = 0x1 << 0,
142   BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO = 0x1 << 1
143 } btav_a2dp_codec_channel_mode_t;
144 
145 typedef enum {
146   BTAV_A2DP_SCMST_DISABLED = 0x00,
147   BTAV_A2DP_SCMST_ENABLED = 0x01
148 } btav_a2dp_scmst_enable_status_t;
149 
150 /*
151  * Structure for representing codec capability or configuration.
152  * It is used for configuring A2DP codec preference, and for reporting back
153  * current configuration or codec capability.
154  * For codec capability, fields "sample_rate", "bits_per_sample" and
155  * "channel_mode" can contain bit-masks with all supported features.
156  */
157 struct btav_a2dp_codec_config_t {
158   btav_a2dp_codec_index_t codec_type;
159   btav_a2dp_codec_priority_t codec_priority;  // Codec selection priority
160                                               // relative to other codecs: larger value
161                                               // means higher priority. If 0, reset to
162                                               // default.
163   btav_a2dp_codec_sample_rate_t sample_rate;
164   btav_a2dp_codec_bits_per_sample_t bits_per_sample;
165   btav_a2dp_codec_channel_mode_t channel_mode;
166   int64_t codec_specific_1;  // Codec-specific value 1
167   int64_t codec_specific_2;  // Codec-specific value 2
168   int64_t codec_specific_3;  // Codec-specific value 3
169   int64_t codec_specific_4;  // Codec-specific value 4
170 
CodecNameStrbtav_a2dp_codec_config_t171   std::string CodecNameStr() const {
172     switch (codec_type) {
173       case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
174         return "SBC";
175       case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
176         return "AAC";
177       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
178         return "aptX";
179       case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
180         return "aptX HD";
181       case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
182         return "LDAC";
183       case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
184         return "SBC (Sink)";
185       case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
186         return "AAC (Sink)";
187       case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
188         return "LDAC (Sink)";
189       case BTAV_A2DP_CODEC_INDEX_SOURCE_LC3:
190         return "LC3";
191       case BTAV_A2DP_CODEC_INDEX_SINK_OPUS:
192         return "Opus (Sink)";
193       case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS:
194         return "Opus";
195       case BTAV_A2DP_CODEC_INDEX_MAX:
196         return "Unknown(CODEC_INDEX_MAX)";
197       case BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN:
198       case BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN:
199         return "Unknown(CODEC_EXT)";
200     }
201     return "Unknown";
202   }
203 
ToStringbtav_a2dp_codec_config_t204   std::string ToString() const {
205     std::string sample_rate_str;
206     AppendCapability(sample_rate_str, (sample_rate == BTAV_A2DP_CODEC_SAMPLE_RATE_NONE), "NONE");
207     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100), "44100");
208     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000), "48000");
209     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200), "88200");
210     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000), "96000");
211     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400), "176400");
212     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000), "192000");
213     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000), "16000");
214     AppendCapability(sample_rate_str, (sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000), "24000");
215 
216     std::string bits_per_sample_str;
217     AppendCapability(bits_per_sample_str, (bits_per_sample == BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE),
218                      "NONE");
219     AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16),
220                      "16");
221     AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24),
222                      "24");
223     AppendCapability(bits_per_sample_str, (bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32),
224                      "32");
225 
226     std::string channel_mode_str;
227     AppendCapability(channel_mode_str, (channel_mode == BTAV_A2DP_CODEC_CHANNEL_MODE_NONE), "NONE");
228     AppendCapability(channel_mode_str, (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO), "MONO");
229     AppendCapability(channel_mode_str, (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
230                      "STEREO");
231 
232     return "codec: " + CodecNameStr() + " priority: " + std::to_string(codec_priority) +
233            " sample_rate: " + sample_rate_str + " bits_per_sample: " + bits_per_sample_str +
234            " channel_mode: " + channel_mode_str +
235            " codec_specific_1: " + std::to_string(codec_specific_1) +
236            " codec_specific_2: " + std::to_string(codec_specific_2) +
237            " codec_specific_3: " + std::to_string(codec_specific_3) +
238            " codec_specific_4: " + std::to_string(codec_specific_4);
239   }
240 
PrintCodecsbtav_a2dp_codec_config_t241   static std::string PrintCodecs(std::vector<btav_a2dp_codec_config_t> codecs) {
242     std::ostringstream oss;
243     for (size_t i = 0; i < codecs.size(); i++) {
244       oss << codecs[i].CodecNameStr();
245       if (i != (codecs.size() - 1)) {
246         oss << ", ";
247       }
248     }
249 
250     return oss.str();
251   }
252 
253 private:
AppendCapabilitybtav_a2dp_codec_config_t254   static std::string AppendCapability(std::string& result, bool append, const std::string& name) {
255     if (!append) {
256       return result;
257     }
258     if (!result.empty()) {
259       result += "|";
260     }
261     result += name;
262     return result;
263   }
264 };
265 
266 typedef struct {
267   btav_a2dp_scmst_enable_status_t enable_status;
268   uint8_t cp_header;
269 } btav_a2dp_scmst_info_t;
270 
271 typedef struct {
272   bt_status_t status;
273   uint8_t error_code;
274   std::optional<std::string> error_msg;
275 } btav_error_t;
276 
277 /**
278  * NOTE:
279  *
280  * 1. AVRCP 1.0 shall be supported initially. AVRCP passthrough commands
281  *    shall be handled internally via uinput
282  *
283  * 2. A2DP data path shall be handled via a socket pipe between the AudioFlinger
284  *    android_audio_hw library and the Bluetooth stack.
285  *
286  */
287 
288 __END_DECLS
289 
290 namespace std {
291 template <>
292 struct formatter<btav_connection_state_t> : enum_formatter<btav_connection_state_t> {};
293 template <>
294 struct formatter<btav_audio_state_t> : enum_formatter<btav_audio_state_t> {};
295 template <>
296 struct formatter<btav_a2dp_codec_bits_per_sample_t>
297     : enum_formatter<btav_a2dp_codec_bits_per_sample_t> {};
298 template <>
299 struct formatter<btav_a2dp_codec_priority_t> : enum_formatter<btav_a2dp_codec_priority_t> {};
300 template <>
301 struct formatter<btav_a2dp_codec_index_t> : enum_formatter<btav_a2dp_codec_index_t> {};
302 template <>
303 struct formatter<btav_a2dp_codec_sample_rate_t> : enum_formatter<btav_a2dp_codec_sample_rate_t> {};
304 template <>
305 struct formatter<btav_a2dp_codec_channel_mode_t> : enum_formatter<btav_a2dp_codec_channel_mode_t> {
306 };
307 template <>
308 struct formatter<btav_a2dp_scmst_enable_status_t>
309     : enum_formatter<btav_a2dp_scmst_enable_status_t> {};
310 }  // namespace std
311 
312 #endif /* ANDROID_INCLUDE_BT_AV_H */
313