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 }; // enum
137 
138 // Microphone Field Dimension Constants
139 #define MIC_FIELD_DIMENSION_WIDE (-1.0f)
140 #define MIC_FIELD_DIMENSION_NORMAL (0.0f)
141 #define MIC_FIELD_DIMENSION_NARROW (1.0f)
142 #define MIC_FIELD_DIMENSION_DEFAULT MIC_FIELD_DIMENSION_NORMAL
143 
144 #ifdef __cplusplus
145 #define CONST_ARRAY inline constexpr
146 #else
147 #define CONST_ARRAY const
148 #endif
149 
150 // Keep the device arrays in order from low to high as they may be needed to do binary search.
151 // inline constexpr
152 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_ARRAY[] = {
153     AUDIO_DEVICE_OUT_EARPIECE,                  // 0x00000001u
154     AUDIO_DEVICE_OUT_SPEAKER,                   // 0x00000002u
155     AUDIO_DEVICE_OUT_WIRED_HEADSET,             // 0x00000004u
156     AUDIO_DEVICE_OUT_WIRED_HEADPHONE,           // 0x00000008u
157     AUDIO_DEVICE_OUT_BLUETOOTH_SCO,             // 0x00000010u
158     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     // 0x00000020u
159     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      // 0x00000040u
160     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            // 0x00000080u
161     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, // 0x00000100u
162     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    // 0x00000200u
163     AUDIO_DEVICE_OUT_HDMI,                      // 0x00000400u, OUT_AUX_DIGITAL
164     AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,         // 0x00000800u
165     AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,         // 0x00001000u
166     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
167     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
168     AUDIO_DEVICE_OUT_REMOTE_SUBMIX,             // 0x00008000u
169     AUDIO_DEVICE_OUT_TELEPHONY_TX,              // 0x00010000u
170     AUDIO_DEVICE_OUT_LINE,                      // 0x00020000u
171     AUDIO_DEVICE_OUT_HDMI_ARC,                  // 0x00040000u
172     AUDIO_DEVICE_OUT_HDMI_EARC,                 // 0x00040001u,
173     AUDIO_DEVICE_OUT_SPDIF,                     // 0x00080000u
174     AUDIO_DEVICE_OUT_FM,                        // 0x00100000u
175     AUDIO_DEVICE_OUT_AUX_LINE,                  // 0x00200000u
176     AUDIO_DEVICE_OUT_SPEAKER_SAFE,              // 0x00400000u
177     AUDIO_DEVICE_OUT_IP,                        // 0x00800000u
178     AUDIO_DEVICE_OUT_BUS,                       // 0x01000000u
179     AUDIO_DEVICE_OUT_PROXY,                     // 0x02000000u
180     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
181     AUDIO_DEVICE_OUT_HEARING_AID,               // 0x08000000u
182     AUDIO_DEVICE_OUT_ECHO_CANCELLER,            // 0x10000000u
183     AUDIO_DEVICE_OUT_BLE_HEADSET,               // 0x20000000u
184     AUDIO_DEVICE_OUT_BLE_SPEAKER,               // 0x20000001u
185     AUDIO_DEVICE_OUT_DEFAULT,                   // 0x40000000u, BIT_DEFAULT
186 };
187 
188 // inline constexpr
189 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY[] = {
190     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,            // 0x00000080u,
191     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, // 0x00000100u,
192     AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,    // 0x00000200u,
193 };
194 
195 // inline constexpr
196 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_SCO_ARRAY[] = {
197     AUDIO_DEVICE_OUT_BLUETOOTH_SCO,             // 0x00000010u,
198     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,     // 0x00000020u,
199     AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,      // 0x00000040u,
200 };
201 
202 // inline constexpr
203 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_USB_ARRAY[] = {
204     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
205     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
206     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
207 };
208 
209 // Digital out device array should contain all usb out devices
210 // inline constexpr
211 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY[] = {
212     AUDIO_DEVICE_OUT_HDMI,                      // 0x00000400u, OUT_AUX_DIGITAL
213     AUDIO_DEVICE_OUT_USB_ACCESSORY,             // 0x00002000u
214     AUDIO_DEVICE_OUT_USB_DEVICE,                // 0x00004000u
215     AUDIO_DEVICE_OUT_HDMI_ARC,                  // 0x00040000u
216     AUDIO_DEVICE_OUT_HDMI_EARC,                 // 0x00040001u
217     AUDIO_DEVICE_OUT_SPDIF,                     // 0x00080000u
218     AUDIO_DEVICE_OUT_IP,                        // 0x00800000u
219     AUDIO_DEVICE_OUT_BUS,                       // 0x01000000u
220     AUDIO_DEVICE_OUT_USB_HEADSET,               // 0x04000000u
221 };
222 
223 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_OUT_ALL_BLE_ARRAY[] = {
224     AUDIO_DEVICE_OUT_BLE_HEADSET,               // 0x20000000u
225     AUDIO_DEVICE_OUT_BLE_SPEAKER,               // 0x20000001u
226 };
227 
228 // inline constexpr
229 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_ARRAY[] = {
230     AUDIO_DEVICE_IN_COMMUNICATION,              // 0x80000001u
231     AUDIO_DEVICE_IN_AMBIENT,                    // 0x80000002u
232     AUDIO_DEVICE_IN_BUILTIN_MIC,                // 0x80000004u
233     AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,      // 0x80000008u
234     AUDIO_DEVICE_IN_WIRED_HEADSET,              // 0x80000010u
235     AUDIO_DEVICE_IN_HDMI,                       // 0x80000020u, IN_AUX_DIGITAL
236     AUDIO_DEVICE_IN_TELEPHONY_RX,               // 0x80000040u, IN_VOICE_CALL
237     AUDIO_DEVICE_IN_BACK_MIC,                   // 0x80000080u
238     AUDIO_DEVICE_IN_REMOTE_SUBMIX,              // 0x80000100u
239     AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET,          // 0x80000200u
240     AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET,          // 0x80000400u
241     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
242     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
243     AUDIO_DEVICE_IN_FM_TUNER,                   // 0x80002000u
244     AUDIO_DEVICE_IN_TV_TUNER,                   // 0x80004000u
245     AUDIO_DEVICE_IN_LINE,                       // 0x80008000u
246     AUDIO_DEVICE_IN_SPDIF,                      // 0x80010000u
247     AUDIO_DEVICE_IN_BLUETOOTH_A2DP,             // 0x80020000u
248     AUDIO_DEVICE_IN_LOOPBACK,                   // 0x80040000u
249     AUDIO_DEVICE_IN_IP,                         // 0x80080000u
250     AUDIO_DEVICE_IN_BUS,                        // 0x80100000u
251     AUDIO_DEVICE_IN_PROXY,                      // 0x81000000u
252     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
253     AUDIO_DEVICE_IN_BLUETOOTH_BLE,              // 0x84000000u
254     AUDIO_DEVICE_IN_HDMI_ARC,                   // 0x88000000u
255     AUDIO_DEVICE_IN_HDMI_EARC,                  // 0x88000001u
256     AUDIO_DEVICE_IN_ECHO_REFERENCE,             // 0x90000000u
257     AUDIO_DEVICE_IN_BLE_HEADSET,                // 0xA0000000u
258     AUDIO_DEVICE_IN_DEFAULT,                    // 0xC0000000u
259 };
260 
261 // inline constexpr
262 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_SCO_ARRAY[] = {
263     AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,      // 0x80000008u
264 };
265 
266 // inline constexpr
267 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_USB_ARRAY[] = {
268     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
269     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
270     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
271 };
272 
273 // Digital in device array should contain all usb in devices
274 // inline constexpr
275 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY[] = {
276     AUDIO_DEVICE_IN_HDMI,                       // 0x80000020u, IN_AUX_DIGITAL
277     AUDIO_DEVICE_IN_USB_ACCESSORY,              // 0x80000800u
278     AUDIO_DEVICE_IN_USB_DEVICE,                 // 0x80001000u
279     AUDIO_DEVICE_IN_SPDIF,                      // 0x80010000u
280     AUDIO_DEVICE_IN_IP,                         // 0x80080000u
281     AUDIO_DEVICE_IN_BUS,                        // 0x80100000u
282     AUDIO_DEVICE_IN_USB_HEADSET,                // 0x82000000u
283     AUDIO_DEVICE_IN_HDMI_ARC,                   // 0x88000000u
284     AUDIO_DEVICE_IN_HDMI_EARC,                  // 0x88000001u
285 };
286 
287 static CONST_ARRAY audio_devices_t AUDIO_DEVICE_IN_ALL_BLE_ARRAY[] = {
288     AUDIO_DEVICE_IN_BLE_HEADSET,                // 0xA0000000u
289 };
290 
291 
292 static CONST_ARRAY audio_encapsulation_type_t AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY[] = {
293     AUDIO_ENCAPSULATION_TYPE_IEC61937,
294 };
295 
296 
297 #ifndef AUDIO_ARRAY_SIZE
298 // std::size()
299 #define AUDIO_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
300 #endif
301 
302 // inline constexpr
303 static const uint32_t AUDIO_DEVICE_OUT_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_ARRAY);
304 static const uint32_t AUDIO_DEVICE_OUT_A2DP_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY);
305 static const uint32_t AUDIO_DEVICE_OUT_SCO_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY);
306 static const uint32_t AUDIO_DEVICE_OUT_USB_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_OUT_ALL_USB_ARRAY);
307 static const uint32_t AUDIO_DEVICE_OUT_DIGITAL_CNT = AUDIO_ARRAY_SIZE(
308                                                      AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY);
309 static const uint32_t AUDIO_DEVICE_OUT_BLE_CNT = AUDIO_ARRAY_SIZE(
310                                                      AUDIO_DEVICE_OUT_ALL_BLE_ARRAY);
311 
312 static const uint32_t AUDIO_DEVICE_IN_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_ARRAY);
313 static const uint32_t AUDIO_DEVICE_IN_SCO_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_SCO_ARRAY);
314 static const uint32_t AUDIO_DEVICE_IN_USB_CNT = AUDIO_ARRAY_SIZE(AUDIO_DEVICE_IN_ALL_USB_ARRAY);
315 static const uint32_t AUDIO_DEVICE_IN_DIGITAL_CNT = AUDIO_ARRAY_SIZE(
316                                                     AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY);
317 static const uint32_t AUDIO_DEVICE_IN_BLE_CNT = AUDIO_ARRAY_SIZE(
318                                                     AUDIO_DEVICE_IN_ALL_BLE_ARRAY);
319 
320 static const uint32_t AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS =
321         (1 << AUDIO_ENCAPSULATION_MODE_NONE) |
322         (1 << AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM) |
323         (1 << AUDIO_ENCAPSULATION_MODE_HANDLE);
324 static const uint32_t AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS =
325         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE) |
326         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER) |
327         (1 << AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR);
328 
329 static const uint32_t AUDIO_ENCAPSULATION_TYPE_CNT = AUDIO_ARRAY_SIZE(
330                                                      AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY);
331 
332 #if AUDIO_ARRAYS_STATIC_CHECK
333 
334 template<typename T, size_t N>
isSorted(const T (& a)[N])335 constexpr bool isSorted(const T(&a)[N]) {
336     for (size_t i = 1; i < N; ++i) {
337         if (a[i - 1] > a[i]) {
338             return false;
339         }
340     }
341     return true;
342 }
343 
344 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_ARRAY),
345               "AUDIO_DEVICE_OUT_ALL_ARRAY must be sorted");
346 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY),
347               "AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY must be sorted");
348 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY),
349               "AUDIO_DEVICE_OUT_ALL_SCO_ARRAY must be sorted");
350 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_USB_ARRAY),
351               "AUDIO_DEVICE_OUT_ALL_USB_ARRAY must be sorted");
352 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
353               "AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY must be sorted");
354 static_assert(isSorted(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY),
355               "AUDIO_DEVICE_OUT_ALL_BLE_ARRAY must be sorted");
356 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_ARRAY),
357               "AUDIO_DEVICE_IN_ALL_ARRAY must be sorted");
358 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_SCO_ARRAY),
359               "AUDIO_DEVICE_IN_ALL_SCO_ARRAY must be sorted");
360 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_USB_ARRAY),
361               "AUDIO_DEVICE_IN_ALL_USB_ARRAY must be sorted");
362 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
363               "AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY must be sorted");
364 static_assert(isSorted(AUDIO_DEVICE_IN_ALL_BLE_ARRAY),
365               "AUDIO_DEVICE_IN_ALL_BLE_ARRAY must be sorted");
366 
367 static_assert(AUDIO_DEVICE_OUT_CNT == std::size(AUDIO_DEVICE_OUT_ALL_ARRAY));
368 static_assert(AUDIO_DEVICE_OUT_A2DP_CNT == std::size(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY));
369 static_assert(AUDIO_DEVICE_OUT_SCO_CNT == std::size(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY));
370 static_assert(AUDIO_DEVICE_OUT_USB_CNT == std::size(AUDIO_DEVICE_OUT_ALL_USB_ARRAY));
371 static_assert(AUDIO_DEVICE_OUT_DIGITAL_CNT == std::size(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY));
372 static_assert(AUDIO_DEVICE_OUT_BLE_CNT == std::size(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY));
373 static_assert(AUDIO_DEVICE_IN_CNT == std::size(AUDIO_DEVICE_IN_ALL_ARRAY));
374 static_assert(AUDIO_DEVICE_IN_SCO_CNT == std::size(AUDIO_DEVICE_IN_ALL_SCO_ARRAY));
375 static_assert(AUDIO_DEVICE_IN_USB_CNT == std::size(AUDIO_DEVICE_IN_ALL_USB_ARRAY));
376 static_assert(AUDIO_DEVICE_IN_DIGITAL_CNT == std::size(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY));
377 static_assert(AUDIO_DEVICE_IN_BLE_CNT == std::size(AUDIO_DEVICE_IN_ALL_BLE_ARRAY));
378 static_assert(AUDIO_ENCAPSULATION_TYPE_CNT == std::size(AUDIO_ENCAPSULATION_TYPE_ALL_ARRAY));
379 
380 #if __has_builtin(__builtin_popcount)
381 // Replace with constexpr std::popcount with C++20
382 
383 // Check common channel masks have counts we expect.
384 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_MONO) == 1);
385 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_STEREO) == 2);
386 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT1) == 3);
387 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_TRI) == 3);
388 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_TRI_BACK) == 3);
389 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD) == 4);
390 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT1) == 4);
391 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT0POINT2) == 4);
392 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD) == 4);
393 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_QUAD_SIDE) == 4);
394 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_SURROUND) == 4);
395 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_PENTA) == 5);
396 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_2POINT1POINT2) == 5);
397 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT0POINT2) == 5);
398 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1) == 6);
399 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_3POINT1POINT2) == 6);
400 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_6POINT1) == 7);
401 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1) == 8);
402 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1POINT2) == 8);
403 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_5POINT1POINT4) == 10);
404 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1POINT2) == 10);
405 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_7POINT1POINT4) == 12);
406 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_13POINT_360RA) == 13);
407 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_22POINT2) == 24);
408 
409 // Check common channel masks which are a subset of another.
410 // (by subset, all the channel positions are contained in the other mask).
411 
412 // Validate that channel positions in (a) are a subset of (b).
413 #define CHANNEL_CHECK_SUBSET_OF(a, b) \
414     static_assert(__builtin_popcount((a)^(b)) == __builtin_popcount(b) - __builtin_popcount(a))
415 
416 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_MONO, AUDIO_CHANNEL_OUT_STEREO);
417 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_STEREO, AUDIO_CHANNEL_OUT_2POINT1);
418 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_STEREO, AUDIO_CHANNEL_OUT_QUAD);
419 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_QUAD, AUDIO_CHANNEL_OUT_PENTA);
420 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_2POINT1, AUDIO_CHANNEL_OUT_5POINT1);
421 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_5POINT1POINT2);
422 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_5POINT1POINT4);
423 // Note AUDIO_CHANNEL_OUT_5POINT1POINT2 is not subset of AUDIO_CHANNEL_OUT_5POINT1POINT4
424 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1POINT2, AUDIO_CHANNEL_OUT_7POINT1POINT2);
425 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1, AUDIO_CHANNEL_OUT_7POINT1POINT2);
426 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1, AUDIO_CHANNEL_OUT_7POINT1POINT4);
427 // Note AUDIO_CHANNEL_OUT_7POINT1POINT2 is not subset of AUDIO_CHANNEL_OUT_7POINT1POINT4
428 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_5POINT1POINT4, AUDIO_CHANNEL_OUT_7POINT1POINT4);
429 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_13POINT_360RA, AUDIO_CHANNEL_OUT_22POINT2);
430 CHANNEL_CHECK_SUBSET_OF(AUDIO_CHANNEL_OUT_7POINT1POINT4, AUDIO_CHANNEL_OUT_22POINT2);
431 
432 #undef CHANNEL_CHECK_SUBSET_OF
433 
434 // Extra channel mask check
435 static_assert(__builtin_popcount(AUDIO_CHANNEL_OUT_13POINT_360RA
436         ^ AUDIO_CHANNEL_OUT_7POINT1POINT4) == 7); // bfl, bfr, bfc + tfc replace lfe + bl + br
437 
438 #endif // __has_builtin(__builtin_popcount)
439 
440 #endif  // AUDIO_ARRAYS_STATIC_CHECK
441 
442 #endif  // ANDROID_AUDIO_BASE_UTILS_H
443