• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_AUDIO_BASE_UTILS_H
18 #define ANDROID_AUDIO_BASE_UTILS_H
19 
20 #include "audio-base.h"
21 #include "audio-hal-enums.h"
22 #include "audio_common-base.h"
23 
24 /** Define helper values to iterate over enum, extend them or checking value validity.
25  *  Those values are compatible with the corresponding enum values.
26  *  They are not macro like similar values in audio.h to avoid conflicting
27  *  with the libhardware_legacy audio.h.
28  */
29 enum {
30     AUDIO_STREAM_MIN = AUDIO_STREAM_VOICE_CALL,
31     /** Number of audio streams available to vendors. */
32     AUDIO_STREAM_PUBLIC_CNT = AUDIO_STREAM_ASSISTANT + 1,
33 
34 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
35     /** Total number of streams handled by the policy. */
36     AUDIO_STREAM_FOR_POLICY_CNT= AUDIO_STREAM_REROUTING + 1,
37 #endif
38 
39    /** Total number of streams. */
40     AUDIO_STREAM_CNT          = AUDIO_STREAM_CALL_ASSISTANT + 1,
41 
42     AUDIO_SOURCE_MAX          = AUDIO_SOURCE_VOICE_PERFORMANCE,
43     AUDIO_SOURCE_CNT          = AUDIO_SOURCE_MAX + 1,
44 
45     AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT = AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED + 1,
46     AUDIO_MICROPHONE_LOCATION_CNT = AUDIO_MICROPHONE_LOCATION_PERIPHERAL + 1,
47     AUDIO_MICROPHONE_DIRECTIONALITY_CNT = AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID + 1,
48 
49     /** For retrocompatibility AUDIO_MODE_* and AUDIO_STREAM_* must be signed. */
50     AUDIO_DETAIL_NEGATIVE_VALUE = -1,
51 };
52 
53 // TODO: remove audio device combination as it is not allowed to use as bit mask since R.
54 enum {
55     AUDIO_DEVICE_OUT_ALL      = AUDIO_DEVICE_OUT_EARPIECE |
56                                 AUDIO_DEVICE_OUT_SPEAKER |
57                                 AUDIO_DEVICE_OUT_WIRED_HEADSET |
58                                 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
59                                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
60                                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
61                                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
62                                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
63                                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
64                                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
65                                 AUDIO_DEVICE_OUT_HDMI |
66                                 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
67                                 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
68                                 AUDIO_DEVICE_OUT_USB_ACCESSORY |
69                                 AUDIO_DEVICE_OUT_USB_DEVICE |
70                                 AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
71                                 AUDIO_DEVICE_OUT_TELEPHONY_TX |
72                                 AUDIO_DEVICE_OUT_LINE |
73                                 AUDIO_DEVICE_OUT_HDMI_ARC |
74                                 // AUDIO_DEVICE_OUT_HDMI_EARC | not a bitfield
75                                 AUDIO_DEVICE_OUT_SPDIF |
76                                 AUDIO_DEVICE_OUT_FM |
77                                 AUDIO_DEVICE_OUT_AUX_LINE |
78                                 AUDIO_DEVICE_OUT_SPEAKER_SAFE |
79                                 AUDIO_DEVICE_OUT_IP |
80                                 AUDIO_DEVICE_OUT_BUS |
81                                 AUDIO_DEVICE_OUT_PROXY |
82                                 AUDIO_DEVICE_OUT_USB_HEADSET |
83                                 AUDIO_DEVICE_OUT_HEARING_AID |
84                                 AUDIO_DEVICE_OUT_ECHO_CANCELLER |
85                                 AUDIO_DEVICE_OUT_DEFAULT,
86 
87     AUDIO_DEVICE_OUT_ALL_A2DP = AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
88                                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
89                                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,
90 
91     AUDIO_DEVICE_OUT_ALL_SCO  = AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
92                                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
93                                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,
94 
95     AUDIO_DEVICE_OUT_ALL_USB  = AUDIO_DEVICE_OUT_USB_ACCESSORY |
96                                 AUDIO_DEVICE_OUT_USB_DEVICE |
97                                 AUDIO_DEVICE_OUT_USB_HEADSET,
98 
99     AUDIO_DEVICE_IN_ALL       = AUDIO_DEVICE_IN_COMMUNICATION |
100                                 AUDIO_DEVICE_IN_AMBIENT |
101                                 AUDIO_DEVICE_IN_BUILTIN_MIC |
102                                 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET |
103                                 AUDIO_DEVICE_IN_WIRED_HEADSET |
104                                 AUDIO_DEVICE_IN_HDMI |
105                                 AUDIO_DEVICE_IN_TELEPHONY_RX |
106                                 AUDIO_DEVICE_IN_BACK_MIC |
107                                 AUDIO_DEVICE_IN_REMOTE_SUBMIX |
108                                 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET |
109                                 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET |
110                                 AUDIO_DEVICE_IN_USB_ACCESSORY |
111                                 AUDIO_DEVICE_IN_USB_DEVICE |
112                                 AUDIO_DEVICE_IN_FM_TUNER |
113                                 AUDIO_DEVICE_IN_TV_TUNER |
114                                 AUDIO_DEVICE_IN_LINE |
115                                 AUDIO_DEVICE_IN_SPDIF |
116                                 AUDIO_DEVICE_IN_BLUETOOTH_A2DP |
117                                 AUDIO_DEVICE_IN_LOOPBACK |
118                                 AUDIO_DEVICE_IN_IP |
119                                 AUDIO_DEVICE_IN_BUS |
120                                 AUDIO_DEVICE_IN_PROXY |
121                                 AUDIO_DEVICE_IN_USB_HEADSET |
122                                 AUDIO_DEVICE_IN_BLUETOOTH_BLE |
123                                 AUDIO_DEVICE_IN_HDMI_ARC |
124                                 // AUDIO_DEVICE_IN_HDMI_EARC | // not a bitfield
125                                 AUDIO_DEVICE_IN_ECHO_REFERENCE |
126                                 AUDIO_DEVICE_IN_DEFAULT,
127 
128     AUDIO_DEVICE_IN_ALL_SCO   = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
129 
130     AUDIO_DEVICE_IN_ALL_USB   = AUDIO_DEVICE_IN_USB_ACCESSORY |
131                                 AUDIO_DEVICE_IN_USB_DEVICE |
132                                 AUDIO_DEVICE_IN_USB_HEADSET,
133 
134     AUDIO_USAGE_MAX           = AUDIO_USAGE_CALL_ASSISTANT,
135     AUDIO_USAGE_CNT           = AUDIO_USAGE_CALL_ASSISTANT + 1,
136 
137     AUDIO_LATENCY_MODE_INVALID = -1,
138     AUDIO_LATENCY_MODE_CNT    = AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE + 1,
139 }; // enum
140 
141 // Microphone Field Dimension Constants
142 #define MIC_FIELD_DIMENSION_WIDE (-1.0f)
143 #define MIC_FIELD_DIMENSION_NORMAL (0.0f)
144 #define MIC_FIELD_DIMENSION_NARROW (1.0f)
145 #define MIC_FIELD_DIMENSION_DEFAULT MIC_FIELD_DIMENSION_NORMAL
146 
147 #ifdef __cplusplus
148 #define CONST_ARRAY inline constexpr
149 #else
150 #define CONST_ARRAY const
151 #endif
152 
153 // Keep the device arrays in order from low to high as they may be needed to do binary search.
154 // inline constexpr
155 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_ARRAY[] = {
156     AUDIO_DEVICE_OUT_EARPIECE,                  // 0x00000001u
157     AUDIO_DEVICE_OUT_SPEAKER,                   // 0x00000002u
158     AUDIO_DEVICE_OUT_WIRED_HEADSET,             // 0x00000004u
159     AUDIO_DEVICE_OUT_WIRED_HEADPHONE,           // 0x00000008u
160     AUDIO_DEVICE_OUT_BLUETOOTH_SCO,             // 0x00000010u
161     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     // 0x00000020u
162     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      // 0x00000040u
163     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            // 0x00000080u
164     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, // 0x00000100u
165     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    // 0x00000200u
166     AUDIO_DEVICE_OUT_HDMI,                      // 0x00000400u, OUT_AUX_DIGITAL
167     AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,         // 0x00000800u
168     AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,         // 0x00001000u
169     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
170     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
171     AUDIO_DEVICE_OUT_REMOTE_SUBMIX,             // 0x00008000u
172     AUDIO_DEVICE_OUT_TELEPHONY_TX,              // 0x00010000u
173     AUDIO_DEVICE_OUT_LINE,                      // 0x00020000u
174     AUDIO_DEVICE_OUT_HDMI_ARC,                  // 0x00040000u
175     AUDIO_DEVICE_OUT_HDMI_EARC,                 // 0x00040001u,
176     AUDIO_DEVICE_OUT_SPDIF,                     // 0x00080000u
177     AUDIO_DEVICE_OUT_FM,                        // 0x00100000u
178     AUDIO_DEVICE_OUT_AUX_LINE,                  // 0x00200000u
179     AUDIO_DEVICE_OUT_SPEAKER_SAFE,              // 0x00400000u
180     AUDIO_DEVICE_OUT_IP,                        // 0x00800000u
181     AUDIO_DEVICE_OUT_BUS,                       // 0x01000000u
182     AUDIO_DEVICE_OUT_PROXY,                     // 0x02000000u
183     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
184     AUDIO_DEVICE_OUT_HEARING_AID,               // 0x08000000u
185     AUDIO_DEVICE_OUT_ECHO_CANCELLER,            // 0x10000000u
186     AUDIO_DEVICE_OUT_BLE_HEADSET,               // 0x20000000u
187     AUDIO_DEVICE_OUT_BLE_SPEAKER,               // 0x20000001u
188     AUDIO_DEVICE_OUT_BLE_BROADCAST,             // 0x20000002u
189     AUDIO_DEVICE_OUT_DEFAULT,                   // 0x40000000u, BIT_DEFAULT
190 };
191 
192 // inline constexpr
193 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY[] = {
194     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            // 0x00000080u,
195     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, // 0x00000100u,
196     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    // 0x00000200u,
197 };
198 
199 // inline constexpr
200 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_SCO_ARRAY[] = {
201     AUDIO_DEVICE_OUT_BLUETOOTH_SCO,             // 0x00000010u,
202     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     // 0x00000020u,
203     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      // 0x00000040u,
204 };
205 
206 // inline constexpr
207 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_USB_ARRAY[] = {
208     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
209     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
210     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
211 };
212 
213 // Digital out device array should contain all usb out devices
214 // inline constexpr
215 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY[] = {
216     AUDIO_DEVICE_OUT_HDMI,                      // 0x00000400u, OUT_AUX_DIGITAL
217     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
218     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
219     AUDIO_DEVICE_OUT_HDMI_ARC,                  // 0x00040000u
220     AUDIO_DEVICE_OUT_HDMI_EARC,                 // 0x00040001u
221     AUDIO_DEVICE_OUT_SPDIF,                     // 0x00080000u
222     AUDIO_DEVICE_OUT_IP,                        // 0x00800000u
223     AUDIO_DEVICE_OUT_BUS,                       // 0x01000000u
224     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
225 };
226 
227 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_BLE_ARRAY[] = {
228     AUDIO_DEVICE_OUT_BLE_HEADSET,               // 0x20000000u
229     AUDIO_DEVICE_OUT_BLE_SPEAKER,               // 0x20000001u
230     AUDIO_DEVICE_OUT_BLE_BROADCAST,             // 0x20000002u
231 };
232 
233 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY[] = {
234     AUDIO_DEVICE_OUT_BLE_HEADSET,               // 0x20000000u
235     AUDIO_DEVICE_OUT_BLE_SPEAKER,               // 0x20000001u
236 };
237 
238 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY[] = {
239     AUDIO_DEVICE_OUT_BLE_BROADCAST,             // 0x20000002u
240 };
241 
242 // inline constexpr
243 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_ARRAY[] = {
244     AUDIO_DEVICE_IN_COMMUNICATION,              // 0x80000001u
245     AUDIO_DEVICE_IN_AMBIENT,                    // 0x80000002u
246     AUDIO_DEVICE_IN_BUILTIN_MIC,                // 0x80000004u
247     AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,      // 0x80000008u
248     AUDIO_DEVICE_IN_WIRED_HEADSET,              // 0x80000010u
249     AUDIO_DEVICE_IN_HDMI,                       // 0x80000020u, IN_AUX_DIGITAL
250     AUDIO_DEVICE_IN_TELEPHONY_RX,               // 0x80000040u, IN_VOICE_CALL
251     AUDIO_DEVICE_IN_BACK_MIC,                   // 0x80000080u
252     AUDIO_DEVICE_IN_REMOTE_SUBMIX,              // 0x80000100u
253     AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET,          // 0x80000200u
254     AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET,          // 0x80000400u
255     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
256     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
257     AUDIO_DEVICE_IN_FM_TUNER,                   // 0x80002000u
258     AUDIO_DEVICE_IN_TV_TUNER,                   // 0x80004000u
259     AUDIO_DEVICE_IN_LINE,                       // 0x80008000u
260     AUDIO_DEVICE_IN_SPDIF,                      // 0x80010000u
261     AUDIO_DEVICE_IN_BLUETOOTH_A2DP,             // 0x80020000u
262     AUDIO_DEVICE_IN_LOOPBACK,                   // 0x80040000u
263     AUDIO_DEVICE_IN_IP,                         // 0x80080000u
264     AUDIO_DEVICE_IN_BUS,                        // 0x80100000u
265     AUDIO_DEVICE_IN_PROXY,                      // 0x81000000u
266     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
267     AUDIO_DEVICE_IN_BLUETOOTH_BLE,              // 0x84000000u
268     AUDIO_DEVICE_IN_HDMI_ARC,                   // 0x88000000u
269     AUDIO_DEVICE_IN_HDMI_EARC,                  // 0x88000001u
270     AUDIO_DEVICE_IN_ECHO_REFERENCE,             // 0x90000000u
271     AUDIO_DEVICE_IN_BLE_HEADSET,                // 0xA0000000u
272     AUDIO_DEVICE_IN_DEFAULT,                    // 0xC0000000u
273 };
274 
275 // inline constexpr
276 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_SCO_ARRAY[] = {
277     AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,      // 0x80000008u
278 };
279 
280 // inline constexpr
281 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_USB_ARRAY[] = {
282     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
283     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
284     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
285 };
286 
287 // Digital in device array should contain all usb in devices
288 // inline constexpr
289 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY[] = {
290     AUDIO_DEVICE_IN_HDMI,                       // 0x80000020u, IN_AUX_DIGITAL
291     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
292     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
293     AUDIO_DEVICE_IN_SPDIF,                      // 0x80010000u
294     AUDIO_DEVICE_IN_IP,                         // 0x80080000u
295     AUDIO_DEVICE_IN_BUS,                        // 0x80100000u
296     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
297     AUDIO_DEVICE_IN_HDMI_ARC,                   // 0x88000000u
298     AUDIO_DEVICE_IN_HDMI_EARC,                  // 0x88000001u
299 };
300 
301 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_BLE_ARRAY[] = {
302     AUDIO_DEVICE_IN_BLE_HEADSET,                // 0xA0000000u
303 };
304 
305 
306 static CONST_ARRAY audio_encapsulation_type_t AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY[] = {
307     AUDIO_ENCAPSULATION_TYPE_IEC61937,
308 };
309 
310 
311 #ifndef AUDIO_ARRAY_SIZE
312 // std::size()
313 #define AUDIO_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
314 #endif
315 
316 // inline constexpr
317 static const uint32_t AUDIO_DEVICE_OUT_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_ARRAY);
318 static const uint32_t AUDIO_DEVICE_OUT_A2DP_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY);
319 static const uint32_t AUDIO_DEVICE_OUT_SCO_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY);
320 static const uint32_t AUDIO_DEVICE_OUT_USB_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_USB_ARRAY);
321 static const uint32_t AUDIO_DEVICE_OUT_DIGITAL_CNT = AUDIO_ARRAY_SIZE(
322                                                      AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY);
323 static const uint32_t AUDIO_DEVICE_OUT_BLE_CNT = AUDIO_ARRAY_SIZE(
324                                                      AUDIO_DEVICE_OUT_ALL_BLE_ARRAY);
325 static const uint32_t AUDIO_DEVICE_OUT_BLE_UNICAST_CNT = AUDIO_ARRAY_SIZE(
326                                                      AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY);
327 static const uint32_t AUDIO_DEVICE_OUT_BLE_BROADCAST_CNT = AUDIO_ARRAY_SIZE(
328                                                      AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY);
329 
330 static const uint32_t AUDIO_DEVICE_IN_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_ARRAY);
331 static const uint32_t AUDIO_DEVICE_IN_SCO_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_SCO_ARRAY);
332 static const uint32_t AUDIO_DEVICE_IN_USB_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_USB_ARRAY);
333 static const uint32_t AUDIO_DEVICE_IN_DIGITAL_CNT = AUDIO_ARRAY_SIZE(
334                                                     AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY);
335 static const uint32_t AUDIO_DEVICE_IN_BLE_CNT = AUDIO_ARRAY_SIZE(
336                                                     AUDIO_DEVICE_IN_ALL_BLE_ARRAY);
337 
338 static const uint32_t AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS =
339         (1 << AUDIO_ENCAPSULATION_MODE_NONE) |
340         (1 << AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM) |
341         (1 << AUDIO_ENCAPSULATION_MODE_HANDLE);
342 static const uint32_t AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS =
343         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE) |
344         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER) |
345         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR);
346 
347 static const uint32_t AUDIO_ENCAPSULATION_TYPE_CNT = AUDIO_ARRAY_SIZE(
348                                                      AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY);
349 
350 #if AUDIO_ARRAYS_STATIC_CHECK
351 
352 template<typename T, size_t N>
isSorted(const T (& a)[N])353 constexpr bool isSorted(const T(&a)[N]) {
354     for (size_t i = 1; i < N; ++i) {
355         if (a[i - 1] > a[i]) {
356             return false;
357         }
358     }
359     return true;
360 }
361 
362 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_ARRAY),
363               "AUDIO_DEVICE_OUT_ALL_ARRAY must be sorted");
364 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY),
365               "AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY must be sorted");
366 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY),
367               "AUDIO_DEVICE_OUT_ALL_SCO_ARRAY must be sorted");
368 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_USB_ARRAY),
369               "AUDIO_DEVICE_OUT_ALL_USB_ARRAY must be sorted");
370 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
371               "AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY must be sorted");
372 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY),
373               "AUDIO_DEVICE_OUT_ALL_BLE_ARRAY must be sorted");
374 static_assert(isSorted(AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY),
375               "AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY must be sorted");
376 static_assert(isSorted(AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY),
377               "AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY must be sorted");
378 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_ARRAY),
379               "AUDIO_DEVICE_IN_ALL_ARRAY must be sorted");
380 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_SCO_ARRAY),
381               "AUDIO_DEVICE_IN_ALL_SCO_ARRAY must be sorted");
382 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_USB_ARRAY),
383               "AUDIO_DEVICE_IN_ALL_USB_ARRAY must be sorted");
384 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
385               "AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY must be sorted");
386 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_BLE_ARRAY),
387               "AUDIO_DEVICE_IN_ALL_BLE_ARRAY must be sorted");
388 
389 static_assert(AUDIO_DEVICE_OUT_CNT == std::size(AUDIO_DEVICE_OUT_ALL_ARRAY));
390 static_assert(AUDIO_DEVICE_OUT_A2DP_CNT == std::size(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY));
391 static_assert(AUDIO_DEVICE_OUT_SCO_CNT == std::size(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY));
392 static_assert(AUDIO_DEVICE_OUT_USB_CNT == std::size(AUDIO_DEVICE_OUT_ALL_USB_ARRAY));
393 static_assert(AUDIO_DEVICE_OUT_DIGITAL_CNT == std::size(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY));
394 static_assert(AUDIO_DEVICE_OUT_BLE_CNT == std::size(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY));
395 static_assert(AUDIO_DEVICE_OUT_BLE_UNICAST_CNT == std::size(AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY));
396 static_assert(AUDIO_DEVICE_OUT_BLE_BROADCAST_CNT == std::size(AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY));
397 static_assert(AUDIO_DEVICE_IN_CNT == std::size(AUDIO_DEVICE_IN_ALL_ARRAY));
398 static_assert(AUDIO_DEVICE_IN_SCO_CNT == std::size(AUDIO_DEVICE_IN_ALL_SCO_ARRAY));
399 static_assert(AUDIO_DEVICE_IN_USB_CNT == std::size(AUDIO_DEVICE_IN_ALL_USB_ARRAY));
400 static_assert(AUDIO_DEVICE_IN_DIGITAL_CNT == std::size(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY));
401 static_assert(AUDIO_DEVICE_IN_BLE_CNT == std::size(AUDIO_DEVICE_IN_ALL_BLE_ARRAY));
402 static_assert(AUDIO_ENCAPSULATION_TYPE_CNT == std::size(AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY));
403 
404 #if __has_builtin(__builtin_popcount)
405 // Replace with constexpr std::popcount with C++20
406 
407 // Check common channel masks have counts we expect.
408 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_MONO) == 1);
409 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_STEREO) == 2);
410 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT1) == 3);
411 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_TRI) == 3);
412 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_TRI_BACK) == 3);
413 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD) == 4);
414 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT1) == 4);
415 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT0POINT2) == 4);
416 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD) == 4);
417 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD_SIDE) == 4);
418 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_SURROUND) == 4);
419 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_PENTA) == 5);
420 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT1POINT2) == 5);
421 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT0POINT2) == 5);
422 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1) == 6);
423 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT1POINT2) == 6);
424 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_6POINT1) == 7);
425 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1) == 8);
426 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1POINT2) == 8);
427 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1POINT4) == 10);
428 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1POINT2) == 10);
429 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1POINT4) == 12);
430 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_13POINT_360RA) == 13);
431 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_22POINT2) == 24);
432 
433 // Check common channel masks which are a subset of another.
434 // (by subset, all the channel positions are contained in the other mask).
435 
436 // Validate that channel positions in (a) are a subset of (b).
437 #define CHANNEL_CHECK_SUBSET_OF(a, b) \
438     static_assert(__builtin_popcount((a)^(b)) == __builtin_popcount(b) - __builtin_popcount(a))
439 
440 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO);
441 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_STEREO, AUDIO_CHANNEL_OUT_2POINT1);
442 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_STEREO, AUDIO_CHANNEL_OUT_QUAD);
443 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_QUAD, AUDIO_CHANNEL_OUT_PENTA);
444 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_2POINT1, AUDIO_CHANNEL_OUT_5POINT1);
445 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_5POINT1POINT2);
446 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_5POINT1POINT4);
447 // Note AUDIO_CHANNEL_OUT_5POINT1POINT2 is not subset of AUDIO_CHANNEL_OUT_5POINT1POINT4
448 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1POINT2, AUDIO_CHANNEL_OUT_7POINT1POINT2);
449 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1, AUDIO_CHANNEL_OUT_7POINT1POINT2);
450 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1, AUDIO_CHANNEL_OUT_7POINT1POINT4);
451 // Note AUDIO_CHANNEL_OUT_7POINT1POINT2 is not subset of AUDIO_CHANNEL_OUT_7POINT1POINT4
452 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1POINT4, AUDIO_CHANNEL_OUT_7POINT1POINT4);
453 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_13POINT_360RA, AUDIO_CHANNEL_OUT_22POINT2);
454 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1POINT4, AUDIO_CHANNEL_OUT_22POINT2);
455 
456 #undef CHANNEL_CHECK_SUBSET_OF
457 
458 // Extra channel mask check
459 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_13POINT_360RA
460         ^ AUDIO_CHANNEL_OUT_7POINT1POINT4) == 7); // bfl, bfr, bfc + tfc replace lfe + bl + br
461 
462 #endif // __has_builtin(__builtin_popcount)
463 
464 #endif  // AUDIO_ARRAYS_STATIC_CHECK
465 
466 #endif  // ANDROID_AUDIO_BASE_UTILS_H
467