1 /*
2 * Copyright (C) 2020 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 #include "AudioTypes.h"
18 #include "MediaMetricsConstants.h"
19 #include "StringUtils.h"
20 #include <media/TypeConverter.h> // requires libmedia_helper to get the Audio code.
21 #include <statslog.h> // statsd
22
23 namespace android::mediametrics::types {
24
getAudioCallerNameMap()25 const std::unordered_map<std::string, int32_t>& getAudioCallerNameMap() {
26 // DO NOT MODIFY VALUES (OK to add new ones).
27 // This may be found in frameworks/av/media/libmediametrics/include/MediaMetricsConstants.h
28 static std::unordered_map<std::string, int32_t> map{
29 {"unknown", 0}, // callerName not set
30 {"aaudio", 1}, // Native AAudio
31 {"java", 2}, // Java API layer
32 {"media", 3}, // libmedia (mediaplayer)
33 {"opensles", 4}, // Open SLES
34 {"rtp", 5}, // RTP communication
35 {"soundpool", 6}, // SoundPool
36 {"tonegenerator", 7}, // dial tones
37 // R values above.
38 };
39 return map;
40 }
41
42 // A map in case we need to return a flag for input devices.
43 // This is 64 bits (and hence not the same as audio_device_t) because we need extra
44 // bits to represent new devices.
45 // NOT USED FOR R. We do not use int64 flags.
46 // This can be out of date for now, as it is unused even for string validation
47 // (instead TypeConverter<InputDeviceTraits> is used).
getAudioDeviceInMap()48 const std::unordered_map<std::string, int64_t>& getAudioDeviceInMap() {
49 // DO NOT MODIFY VALUES (OK to add new ones). This does NOT match audio_device_t.
50 static std::unordered_map<std::string, int64_t> map{
51 {"AUDIO_DEVICE_IN_COMMUNICATION", 1LL << 0},
52 {"AUDIO_DEVICE_IN_AMBIENT", 1LL << 1},
53 {"AUDIO_DEVICE_IN_BUILTIN_MIC", 1LL << 2},
54 {"AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET", 1LL << 3},
55 {"AUDIO_DEVICE_IN_WIRED_HEADSET", 1LL << 4},
56 {"AUDIO_DEVICE_IN_AUX_DIGITAL", 1LL << 5},
57 {"AUDIO_DEVICE_IN_HDMI", 1LL << 5}, // HDMI == AUX_DIGITAL (6 reserved)
58 {"AUDIO_DEVICE_IN_VOICE_CALL", 1LL << 7},
59 {"AUDIO_DEVICE_IN_TELEPHONY_RX", 1LL << 7}, // TELEPHONY_RX == VOICE_CALL (8 reserved)
60 {"AUDIO_DEVICE_IN_BACK_MIC", 1LL << 9},
61 {"AUDIO_DEVICE_IN_REMOTE_SUBMIX", 1LL << 10},
62 {"AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET", 1LL << 11},
63 {"AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET", 1LL << 12},
64 {"AUDIO_DEVICE_IN_USB_ACCESSORY", 1LL << 13},
65 {"AUDIO_DEVICE_IN_USB_DEVICE", 1LL << 14},
66 {"AUDIO_DEVICE_IN_FM_TUNER", 1LL << 15},
67 {"AUDIO_DEVICE_IN_TV_TUNER", 1LL << 16},
68 {"AUDIO_DEVICE_IN_LINE", 1LL << 17},
69 {"AUDIO_DEVICE_IN_SPDIF", 1LL << 18},
70 {"AUDIO_DEVICE_IN_BLUETOOTH_A2DP", 1LL << 19},
71 {"AUDIO_DEVICE_IN_LOOPBACK", 1LL << 20},
72 {"AUDIO_DEVICE_IN_IP", 1LL << 21},
73 {"AUDIO_DEVICE_IN_BUS", 1LL << 22},
74 {"AUDIO_DEVICE_IN_PROXY", 1LL << 23},
75 {"AUDIO_DEVICE_IN_USB_HEADSET", 1LL << 24},
76 {"AUDIO_DEVICE_IN_BLUETOOTH_BLE", 1LL << 25},
77 {"AUDIO_DEVICE_IN_HDMI_ARC", 1LL << 26},
78 {"AUDIO_DEVICE_IN_ECHO_REFERENCE", 1LL << 27},
79 {"AUDIO_DEVICE_IN_DEFAULT", 1LL << 28},
80 // R values above.
81 {"AUDIO_DEVICE_IN_BLE_HEADSET", 1LL << 29},
82 {"AUDIO_DEVICE_IN_HDMI_EARC", 1LL << 30},
83 };
84 return map;
85 }
86
87 // A map in case we need to return a flag for output devices.
88 // This is 64 bits (and hence not the same as audio_device_t) because we need extra
89 // bits to represent new devices.
90 // NOT USED FOR R. We do not use int64 flags.
91 // This can be out of date for now, as it is unused even for string validation
92 // (instead TypeConverter<OutputDeviceTraits> is used).
getAudioDeviceOutMap()93 const std::unordered_map<std::string, int64_t>& getAudioDeviceOutMap() {
94 // DO NOT MODIFY VALUES (OK to add new ones). This does NOT match audio_device_t.
95 static std::unordered_map<std::string, int64_t> map{
96 {"AUDIO_DEVICE_OUT_EARPIECE", 1LL << 0},
97 {"AUDIO_DEVICE_OUT_SPEAKER", 1LL << 1},
98 {"AUDIO_DEVICE_OUT_WIRED_HEADSET", 1LL << 2},
99 {"AUDIO_DEVICE_OUT_WIRED_HEADPHONE", 1LL << 3},
100 {"AUDIO_DEVICE_OUT_BLUETOOTH_SCO", 1LL << 4},
101 {"AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET", 1LL << 5},
102 {"AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT", 1LL << 6},
103 {"AUDIO_DEVICE_OUT_BLUETOOTH_A2DP", 1LL << 7},
104 {"AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES", 1LL << 8},
105 {"AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER", 1LL << 9},
106 {"AUDIO_DEVICE_OUT_AUX_DIGITAL", 1LL << 10},
107 {"AUDIO_DEVICE_OUT_HDMI", 1LL << 10}, // HDMI == AUX_DIGITAL (11 reserved)
108 {"AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET", 1LL << 12},
109 {"AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET", 1LL << 13},
110 {"AUDIO_DEVICE_OUT_USB_ACCESSORY", 1LL << 14},
111 {"AUDIO_DEVICE_OUT_USB_DEVICE", 1LL << 15},
112 {"AUDIO_DEVICE_OUT_REMOTE_SUBMIX", 1LL << 16},
113 {"AUDIO_DEVICE_OUT_TELEPHONY_TX", 1LL << 17},
114 {"AUDIO_DEVICE_OUT_LINE", 1LL << 18},
115 {"AUDIO_DEVICE_OUT_HDMI_ARC", 1LL << 19},
116 {"AUDIO_DEVICE_OUT_SPDIF", 1LL << 20},
117 {"AUDIO_DEVICE_OUT_FM", 1LL << 21},
118 {"AUDIO_DEVICE_OUT_AUX_LINE", 1LL << 22},
119 {"AUDIO_DEVICE_OUT_SPEAKER_SAFE", 1LL << 23},
120 {"AUDIO_DEVICE_OUT_IP", 1LL << 24},
121 {"AUDIO_DEVICE_OUT_BUS", 1LL << 25},
122 {"AUDIO_DEVICE_OUT_PROXY", 1LL << 26},
123 {"AUDIO_DEVICE_OUT_USB_HEADSET", 1LL << 27},
124 {"AUDIO_DEVICE_OUT_HEARING_AID", 1LL << 28},
125 {"AUDIO_DEVICE_OUT_ECHO_CANCELLER", 1LL << 29},
126 {"AUDIO_DEVICE_OUT_DEFAULT", 1LL << 30},
127 // R values above.
128 {"AUDIO_DEVICE_OUT_BLE_HEADSET", 1LL << 31},
129 {"AUDIO_DEVICE_OUT_BLE_SPEAKER", 1LL << 32},
130 {"AUDIO_DEVICE_OUT_HDMI_EARC", 1LL << 33},
131 // S values above
132 {"AUDIO_DEVICE_OUT_BLE_BROADCAST", 1LL << 34},
133 // T values above
134 };
135 return map;
136 }
137
138 // A map for the Java AudioDeviceInfo types to internal (native) output devices.
getAudioDeviceOutCompactMap()139 const std::unordered_map<std::string, int32_t>& getAudioDeviceOutCompactMap() {
140 // DO NOT MODIFY VALUES (OK to add new ones).
141 static std::unordered_map<std::string, int32_t> map{
142 // should "unknown" go to AUDIO_DEVICE_NONE?
143 {"earpiece", AUDIO_DEVICE_OUT_EARPIECE},
144 {"speaker", AUDIO_DEVICE_OUT_SPEAKER},
145 {"headset", AUDIO_DEVICE_OUT_WIRED_HEADSET},
146 {"headphone", AUDIO_DEVICE_OUT_WIRED_HEADPHONE},
147 {"bt_sco", AUDIO_DEVICE_OUT_BLUETOOTH_SCO},
148 {"bt_sco_hs", AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET},
149 {"bt_sco_carkit", AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT},
150 {"bt_a2dp", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP},
151 {"bt_a2dp_hp", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES},
152 {"bt_a2dp_spk", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER},
153 {"aux_digital", AUDIO_DEVICE_OUT_AUX_DIGITAL},
154 {"hdmi", AUDIO_DEVICE_OUT_HDMI},
155 {"analog_dock", AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET},
156 {"digital_dock", AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET},
157 {"usb_accessory", AUDIO_DEVICE_OUT_USB_ACCESSORY},
158 {"usb_device", AUDIO_DEVICE_OUT_USB_DEVICE},
159 {"remote_submix", AUDIO_DEVICE_OUT_REMOTE_SUBMIX},
160 {"telephony_tx", AUDIO_DEVICE_OUT_TELEPHONY_TX},
161 {"line", AUDIO_DEVICE_OUT_LINE},
162 {"hdmi_arc", AUDIO_DEVICE_OUT_HDMI_ARC},
163 {"hdmi_earc", AUDIO_DEVICE_OUT_HDMI_EARC},
164 {"spdif", AUDIO_DEVICE_OUT_SPDIF},
165 {"fm_transmitter", AUDIO_DEVICE_OUT_FM},
166 {"aux_line", AUDIO_DEVICE_OUT_AUX_LINE},
167 {"speaker_safe", AUDIO_DEVICE_OUT_SPEAKER_SAFE},
168 {"ip", AUDIO_DEVICE_OUT_IP},
169 {"bus", AUDIO_DEVICE_OUT_BUS},
170 {"proxy", AUDIO_DEVICE_OUT_PROXY},
171 {"usb_headset", AUDIO_DEVICE_OUT_USB_HEADSET},
172 {"hearing_aid_out", AUDIO_DEVICE_OUT_HEARING_AID},
173 {"echo_canceller", AUDIO_DEVICE_OUT_ECHO_CANCELLER},
174 // default does not exist
175 {"ble_headset", AUDIO_DEVICE_OUT_BLE_HEADSET},
176 {"ble_speaker", AUDIO_DEVICE_OUT_BLE_SPEAKER},
177 {"ble_broadcast", AUDIO_DEVICE_OUT_BLE_BROADCAST},
178 };
179 return map;
180 }
181
182 // A map for the Java AudioDeviceInfo types.
183 // This uses generated statsd enums.proto constants.
getAudioDeviceInfoTypeMap()184 const std::unordered_map<std::string, int32_t>& getAudioDeviceInfoTypeMap() {
185 // DO NOT MODIFY VALUES (OK to add new ones).
186 static std::unordered_map<std::string, int32_t> map{
187 {"unknown", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_UNKNOWN},
188 {"earpiece", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BUILTIN_EARPIECE},
189 {"speaker", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BUILTIN_SPEAKER},
190 {"headset", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_WIRED_HEADSET},
191 {"headphone", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_WIRED_HEADPHONES}, // sic
192 {"bt_sco", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_SCO},
193 {"bt_sco_hs", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_SCO},
194 {"bt_sco_carkit", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_SCO},
195 {"bt_a2dp", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_A2DP},
196 {"bt_a2dp_hp", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_A2DP},
197 {"bt_a2dp_spk", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLUETOOTH_A2DP},
198 {"aux_digital", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_HDMI},
199 {"hdmi", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_HDMI},
200 {"analog_dock", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_DOCK},
201 {"digital_dock", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_DOCK},
202 {"usb_accessory", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_USB_ACCESSORY},
203 {"usb_device", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_USB_DEVICE},
204 {"usb_headset", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_USB_HEADSET},
205 {"remote_submix", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_REMOTE_SUBMIX},
206 {"telephony_tx", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_TELEPHONY},
207 {"line", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_LINE_ANALOG},
208 {"hdmi_arc", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_HDMI_ARC},
209 {"hdmi_earc", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_HDMI_EARC},
210 {"spdif", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_LINE_DIGITAL},
211 {"fm_transmitter", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_FM},
212 {"aux_line", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_AUX_LINE},
213 {"speaker_safe", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BUILTIN_SPEAKER_SAFE},
214 {"ip", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_IP},
215 {"bus", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BUS},
216 {"proxy", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_UNKNOWN /* AUDIO_DEVICE_INFO_TYPE_PROXY */},
217 {"hearing_aid_out", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_HEARING_AID},
218 {"echo_canceller", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_ECHO_REFERENCE}, // sic
219 {"ble_headset", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLE_HEADSET},
220 {"ble_speaker", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLE_SPEAKER},
221 {"ble_broadcast", util::MEDIAMETRICS_SPATIALIZER_DEVICE_ENABLED_REPORTED__TYPE__AUDIO_DEVICE_INFO_TYPE_BLE_BROADCAST},
222 };
223 return map;
224 }
225
getAudioThreadTypeMap()226 const std::unordered_map<std::string, int32_t>& getAudioThreadTypeMap() {
227 // DO NOT MODIFY VALUES (OK to add new ones).
228 // This may be found in frameworks/av/services/audioflinger/Threads.h
229 static std::unordered_map<std::string, int32_t> map{
230 // UNKNOWN is -1
231 {"MIXER", 0}, // Thread class is MixerThread
232 {"DIRECT", 1}, // Thread class is DirectOutputThread
233 {"DUPLICATING", 2}, // Thread class is DuplicatingThread
234 {"RECORD", 3}, // Thread class is RecordThread
235 {"OFFLOAD", 4}, // Thread class is OffloadThread
236 {"MMAP_PLAYBACK", 5}, // Thread class for MMAP playback stream
237 {"MMAP_CAPTURE", 6}, // Thread class for MMAP capture stream
238 // R values above.
239 {"SPATIALIZER", 7}, // Thread class for SpatializerThread
240 // S values above.
241 };
242 return map;
243 }
244
getAudioTrackTraitsMap()245 const std::unordered_map<std::string, int32_t>& getAudioTrackTraitsMap() {
246 // DO NOT MODIFY VALUES (OK to add new ones).
247 static std::unordered_map<std::string, int32_t> map{
248 {"static", (1 << 0)}, // A static track
249 // R values above.
250 };
251 return map;
252 }
253
getAAudioDirection()254 const std::unordered_map<std::string, int32_t>& getAAudioDirection() {
255 // DO NOT MODIFY VALUES(OK to add new ones).
256 // This may be found in frameworks/av/media/libaaudio/include/aaudio/AAudio.h
257 static std::unordered_map<std::string, int32_t> map {
258 // UNKNOWN is 0
259 {"AAUDIO_DIRECTION_OUTPUT", 1 /* AAUDIO_DIRECTION_OUTPUT + 1 */},
260 {"AAUDIO_DIRECTION_INPUT", 2 /* AAUDIO_DIRECTION_INPUT + 1*/},
261 };
262 return map;
263 }
264
getAAudioPerformanceMode()265 const std::unordered_map<std::string, int32_t>& getAAudioPerformanceMode() {
266 // DO NOT MODIFY VALUES(OK to add new ones).
267 // This may be found in frameworks/av/media/libaaudio/include/aaudio/AAudio.h
268 static std::unordered_map<std::string, int32_t> map {
269 // UNKNOWN is 0
270 {"AAUDIO_PERFORMANCE_MODE_NONE", 10},
271 {"AAUDIO_PERFORMANCE_MODE_POWER_SAVING", 11},
272 {"AAUDIO_PERFORMANCE_MODE_LOW_LATENCY", 12},
273 };
274 return map;
275 }
276
getAAudioSharingMode()277 const std::unordered_map<std::string, int32_t>& getAAudioSharingMode() {
278 // DO NOT MODIFY VALUES(OK to add new ones).
279 // This may be found in frameworks/av/media/libaaudio/include/aaudio/AAudio.h
280 static std::unordered_map<std::string, int32_t> map {
281 // UNKNOWN is 0
282 {"AAUDIO_SHARING_MODE_EXCLUSIVE", 1 /* AAUDIO_SHARING_MODE_EXCLUSIVE + 1 */},
283 {"AAUDIO_SHARING_MODE_SHARED", 2 /* AAUDIO_SHARING_MODE_SHARED + 1 */},
284 };
285 return map;
286 }
287
getHeadTrackingModeMap()288 const std::unordered_map<std::string, int32_t>& getHeadTrackingModeMap() {
289 // DO NOT MODIFY VALUES(OK to add new ones).
290 // frameworks/base/media/java/android/media/Spatializer.java
291 // frameworks/av/media/libaudioclient/aidl/android/media/SpatializerHeadTrackingMode.aidl
292 static std::unordered_map<std::string, int32_t> map {
293 {"OTHER", 0},
294 {"DISABLED", -1},
295 {"RELATIVE_WORLD", 1},
296 {"RELATIVE_SCREEN", 2},
297 };
298 return map;
299 }
300
getSpatializerLevelMap()301 const std::unordered_map<std::string, int32_t>& getSpatializerLevelMap() {
302 // DO NOT MODIFY VALUES(OK to add new ones).
303 // frameworks/base/media/java/android/media/Spatializer.java
304 // frameworks/av/media/libaudioclient/aidl/android/media/SpatializerHeadTrackingMode.aidl
305 static std::unordered_map<std::string, int32_t> map {
306 {"NONE", 0},
307 {"SPATIALIZER_MULTICHANNEL", 1},
308 {"SPATIALIZER_MCHAN_BED_PLUS_OBJECTS", 2},
309 };
310 return map;
311 }
312
getSpatializerModeMap()313 const std::unordered_map<std::string, int32_t>& getSpatializerModeMap() {
314 // DO NOT MODIFY VALUES(OK to add new ones).
315 // frameworks/av/media/libaudioclient/aidl/android/media/SpatializationMode.aidl
316 static std::unordered_map<std::string, int32_t> map {
317 {"SPATIALIZER_BINAURAL", 0},
318 {"SPATIALIZER_TRANSAURAL", 1},
319 };
320 return map;
321 }
322
getStatusMap()323 const std::unordered_map<std::string, int32_t>& getStatusMap() {
324 // DO NOT MODIFY VALUES(OK to add new ones).
325 static std::unordered_map<std::string, int32_t> map {
326 {"",
327 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__NO_ERROR},
328 {AMEDIAMETRICS_PROP_STATUS_VALUE_OK,
329 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__NO_ERROR},
330 {AMEDIAMETRICS_PROP_STATUS_VALUE_ARGUMENT,
331 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_ARGUMENT},
332 {AMEDIAMETRICS_PROP_STATUS_VALUE_IO,
333 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_IO},
334 {AMEDIAMETRICS_PROP_STATUS_VALUE_MEMORY,
335 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_MEMORY},
336 {AMEDIAMETRICS_PROP_STATUS_VALUE_SECURITY,
337 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_SECURITY},
338 {AMEDIAMETRICS_PROP_STATUS_VALUE_STATE,
339 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_STATE},
340 {AMEDIAMETRICS_PROP_STATUS_VALUE_TIMEOUT,
341 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_TIMEOUT},
342 {AMEDIAMETRICS_PROP_STATUS_VALUE_UNKNOWN,
343 util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_UNKNOWN},
344 };
345 return map;
346 }
347
348 // Helper: Create the corresponding int32 from string flags split with '|'.
349 template <typename Traits>
int32FromFlags(const std::string & flags)350 int32_t int32FromFlags(const std::string &flags)
351 {
352 const auto result = stringutils::split(flags, "|");
353 int32_t intFlags = 0;
354 for (const auto& flag : result) {
355 typename Traits::Type value;
356 if (!TypeConverter<Traits>::fromString(flag, value)) {
357 break;
358 }
359 intFlags |= value;
360 }
361 return intFlags;
362 }
363
364 template <typename Traits>
stringFromFlags(const std::string & flags,size_t len)365 std::string stringFromFlags(const std::string &flags, size_t len)
366 {
367 const auto result = stringutils::split(flags, "|");
368 std::string sFlags;
369 for (const auto& flag : result) {
370 typename Traits::Type value;
371 if (!TypeConverter<Traits>::fromString(flag, value)) {
372 break;
373 }
374 if (len >= flag.size()) continue;
375 if (!sFlags.empty()) sFlags += "|";
376 sFlags += flag.c_str() + len;
377 }
378 return sFlags;
379 }
380
381 template <typename M>
validateStringFromMap(const std::string & str,const M & map)382 std::string validateStringFromMap(const std::string &str, const M& map)
383 {
384 if (str.empty()) return {};
385
386 const auto result = stringutils::split(str, "|");
387 std::stringstream ss;
388 for (const auto &s : result) {
389 if (map.count(s) > 0) {
390 if (ss.tellp() > 0) ss << "|";
391 ss << s;
392 }
393 }
394 return ss.str();
395 }
396
397 template <typename M>
flagsFromMap(const std::string & str,const M & map)398 typename M::mapped_type flagsFromMap(const std::string &str, const M& map)
399 {
400 if (str.empty()) return {};
401
402 const auto result = stringutils::split(str, "|");
403 typename M::mapped_type value{};
404 for (const auto &s : result) {
405 auto it = map.find(s);
406 if (it == map.end()) continue;
407 value |= it->second;
408 }
409 return value;
410 }
411
vectorFromMap(const std::string & str,const std::unordered_map<std::string,int32_t> & map)412 std::vector<int32_t> vectorFromMap(
413 const std::string &str, const std::unordered_map<std::string, int32_t>& map)
414 {
415 std::vector<int32_t> v;
416
417 if (str.empty()) return v;
418
419 const auto result = stringutils::split(str, "|");
420 for (const auto &s : result) {
421 auto it = map.find(s);
422 if (it == map.end()) continue;
423 v.push_back(it->second);
424 }
425 return v;
426 }
427
channelMaskVectorFromString(const std::string & s)428 std::vector<int64_t> channelMaskVectorFromString(const std::string &s)
429 {
430 std::vector<int64_t> v;
431
432 const auto result = stringutils::split(s, "|");
433 for (const auto &mask : result) {
434 // 0 if undetected or if actually 0.
435 int64_t int64Mask = strtoll(mask.c_str(), nullptr, 0);
436 v.push_back(int64Mask);
437 }
438 return v;
439 }
440
441 template <>
lookup(const std::string & contentType)442 int32_t lookup<CONTENT_TYPE>(const std::string &contentType)
443 {
444 AudioContentTraits::Type value;
445 if (!TypeConverter<AudioContentTraits>::fromString(contentType, value)) {
446 value = AUDIO_CONTENT_TYPE_UNKNOWN;
447 }
448 return (int32_t)value;
449 }
450
451 template <>
lookup(const std::string & contentType)452 std::string lookup<CONTENT_TYPE>(const std::string &contentType)
453 {
454 AudioContentTraits::Type value;
455 if (!TypeConverter<AudioContentTraits>::fromString(contentType, value)) {
456 return "";
457 }
458 return contentType.c_str() + sizeof("AUDIO_CONTENT_TYPE");
459 }
460
461 template <>
lookup(const std::string & encoding)462 int32_t lookup<ENCODING>(const std::string &encoding)
463 {
464 FormatTraits::Type value;
465 if (!TypeConverter<FormatTraits>::fromString(encoding, value)) {
466 value = AUDIO_FORMAT_INVALID;
467 }
468 return (int32_t)value;
469 }
470
471 template <>
lookup(const std::string & encoding)472 std::string lookup<ENCODING>(const std::string &encoding)
473 {
474 FormatTraits::Type value;
475 if (!TypeConverter<FormatTraits>::fromString(encoding, value)) {
476 return "";
477 }
478 return encoding.c_str() + sizeof("AUDIO_FORMAT");
479 }
480
481 template <>
lookup(const std::string & inputFlag)482 int32_t lookup<INPUT_FLAG>(const std::string &inputFlag)
483 {
484 return int32FromFlags<InputFlagTraits>(inputFlag);
485 }
486
487 template <>
lookup(const std::string & inputFlag)488 std::string lookup<INPUT_FLAG>(const std::string &inputFlag)
489 {
490 return stringFromFlags<InputFlagTraits>(inputFlag, sizeof("AUDIO_INPUT_FLAG"));
491 }
492
493 template <>
lookup(const std::string & outputFlag)494 int32_t lookup<OUTPUT_FLAG>(const std::string &outputFlag)
495 {
496 return int32FromFlags<OutputFlagTraits>(outputFlag);
497 }
498
499 template <>
lookup(const std::string & outputFlag)500 std::string lookup<OUTPUT_FLAG>(const std::string &outputFlag)
501 {
502 return stringFromFlags<OutputFlagTraits>(outputFlag, sizeof("AUDIO_OUTPUT_FLAG"));
503 }
504
505 template <>
lookup(const std::string & sourceType)506 int32_t lookup<SOURCE_TYPE>(const std::string &sourceType)
507 {
508 SourceTraits::Type value;
509 if (!TypeConverter<SourceTraits>::fromString(sourceType, value)) {
510 value = AUDIO_SOURCE_DEFAULT;
511 }
512 return (int32_t)value;
513 }
514
515 template <>
lookup(const std::string & sourceType)516 std::string lookup<SOURCE_TYPE>(const std::string &sourceType)
517 {
518 SourceTraits::Type value;
519 if (!TypeConverter<SourceTraits>::fromString(sourceType, value)) {
520 return "";
521 }
522 return sourceType.c_str() + sizeof("AUDIO_SOURCE");
523 }
524
525 template <>
lookup(const std::string & streamType)526 int32_t lookup<STREAM_TYPE>(const std::string &streamType)
527 {
528 StreamTraits::Type value;
529 if (!TypeConverter<StreamTraits>::fromString(streamType, value)) {
530 value = AUDIO_STREAM_DEFAULT;
531 }
532 return (int32_t)value;
533 }
534
535 template <>
lookup(const std::string & streamType)536 std::string lookup<STREAM_TYPE>(const std::string &streamType)
537 {
538 StreamTraits::Type value;
539 if (!TypeConverter<StreamTraits>::fromString(streamType, value)) {
540 return "";
541 }
542 return streamType.c_str() + sizeof("AUDIO_STREAM");
543 }
544
545 template <>
lookup(const std::string & usage)546 int32_t lookup<USAGE>(const std::string &usage)
547 {
548 UsageTraits::Type value;
549 if (!TypeConverter<UsageTraits>::fromString(usage, value)) {
550 value = AUDIO_USAGE_UNKNOWN;
551 }
552 return (int32_t)value;
553 }
554
555 template <>
lookup(const std::string & usage)556 std::string lookup<USAGE>(const std::string &usage)
557 {
558 UsageTraits::Type value;
559 if (!TypeConverter<UsageTraits>::fromString(usage, value)) {
560 return "";
561 }
562 return usage.c_str() + sizeof("AUDIO_USAGE");
563 }
564
565 template <>
lookup(const std::string & inputDevice)566 int64_t lookup<INPUT_DEVICE>(const std::string &inputDevice)
567 {
568 // NOT USED FOR R.
569 // Returns a set of bits, each one representing a device in inputDevice.
570 // This is a 64 bit integer, not the same as audio_device_t.
571 return flagsFromMap(inputDevice, getAudioDeviceInMap());
572 }
573
574 template <>
lookup(const std::string & inputDevice)575 std::string lookup<INPUT_DEVICE>(const std::string &inputDevice)
576 {
577 return stringFromFlags<InputDeviceTraits>(inputDevice, sizeof("AUDIO_DEVICE_IN"));
578 }
579
580 template <>
lookup(const std::string & outputDevice)581 int64_t lookup<OUTPUT_DEVICE>(const std::string &outputDevice)
582 {
583 // NOT USED FOR R.
584 // Returns a set of bits, each one representing a device in outputDevice.
585 // This is a 64 bit integer, not the same as audio_device_t.
586 return flagsFromMap(outputDevice, getAudioDeviceOutMap());
587 }
588
589 template <>
lookup(const std::string & outputDevice)590 std::string lookup<OUTPUT_DEVICE>(const std::string &outputDevice)
591 {
592 return stringFromFlags<OutputDeviceTraits>(outputDevice, sizeof("AUDIO_DEVICE_OUT"));
593 }
594
595 template <>
lookup(const std::string & audioDeviceInfoType)596 int32_t lookup<AUDIO_DEVICE_INFO_TYPE>(const std::string& audioDeviceInfoType)
597 {
598 auto& map = getAudioDeviceInfoTypeMap();
599 auto it = map.find(audioDeviceInfoType);
600 if (it == map.end()) {
601 return 0;
602 }
603 return it->second;
604 }
605
606 template <>
lookup(const std::string & callerName)607 int32_t lookup<CALLER_NAME>(const std::string &callerName)
608 {
609 auto& map = getAudioCallerNameMap();
610 auto it = map.find(callerName);
611 if (it == map.end()) {
612 return 0; // return unknown
613 }
614 return it->second;
615 }
616
617 template <>
lookup(const std::string & callerName)618 std::string lookup<CALLER_NAME>(const std::string &callerName)
619 {
620 auto& map = getAudioCallerNameMap();
621 auto it = map.find(callerName);
622 if (it == map.end()) {
623 return "";
624 }
625 return callerName;
626 }
627
628 template <>
lookup(const std::string & headTrackingMode)629 int32_t lookup<HEAD_TRACKING_MODE>(const std::string& headTrackingMode)
630 {
631 auto& map = getHeadTrackingModeMap();
632 auto it = map.find(headTrackingMode);
633 if (it == map.end()) {
634 return 0;
635 }
636 return it->second;
637 }
638
639 template <>
lookup(const std::string & spatializerLevel)640 int32_t lookup<SPATIALIZER_LEVEL>(const std::string& spatializerLevel)
641 {
642 auto& map = getSpatializerLevelMap();
643 auto it = map.find(spatializerLevel);
644 if (it == map.end()) {
645 return 0;
646 }
647 return it->second;
648 }
649
650 template <>
lookup(const std::string & spatializerMode)651 int32_t lookup<SPATIALIZER_MODE>(const std::string& spatializerMode)
652 {
653 auto& map = getSpatializerModeMap();
654 auto it = map.find(spatializerMode);
655 if (it == map.end()) {
656 return 0;
657 }
658 return it->second;
659 }
660
661 template <>
lookup(const std::string & status)662 int32_t lookup<STATUS>(const std::string &status)
663 {
664 auto& map = getStatusMap();
665 auto it = map.find(status);
666 if (it == map.end()) {
667 return util::MEDIAMETRICS_AUDIO_TRACK_STATUS_REPORTED__STATUS__ERROR_UNKNOWN;
668 }
669 return it->second;
670 }
671
672 template <>
lookup(const std::string & threadType)673 int32_t lookup<THREAD_TYPE>(const std::string &threadType)
674 {
675 auto& map = getAudioThreadTypeMap();
676 auto it = map.find(threadType);
677 if (it == map.end()) {
678 return -1; // note this as an illegal thread value as we don't have unknown here.
679 }
680 return it->second;
681 }
682
683 template <>
lookup(const std::string & threadType)684 std::string lookup<THREAD_TYPE>(const std::string &threadType)
685 {
686 auto& map = getAudioThreadTypeMap();
687 auto it = map.find(threadType);
688 if (it == map.end()) {
689 return "";
690 }
691 return threadType;
692 }
693
isInputThreadType(const std::string & threadType)694 bool isInputThreadType(const std::string &threadType)
695 {
696 return threadType == "RECORD" || threadType == "MMAP_CAPTURE";
697 }
698
699 template <>
lookup(const std::string & traits)700 std::string lookup<TRACK_TRAITS>(const std::string &traits)
701 {
702 return validateStringFromMap(traits, getAudioTrackTraitsMap());
703 }
704
705 template <>
lookup(const std::string & traits)706 int32_t lookup<TRACK_TRAITS>(const std::string &traits)
707 {
708 return flagsFromMap(traits, getAudioTrackTraitsMap());
709 }
710
711 template <>
lookup(const std::string & direction)712 std::string lookup<AAUDIO_DIRECTION>(const std::string &direction)
713 {
714 auto& map = getAAudioDirection();
715 auto it = map.find(direction);
716 if (it == map.end()) {
717 return "";
718 }
719 return direction;
720 }
721
722 template <>
lookup(const std::string & direction)723 int32_t lookup<AAUDIO_DIRECTION>(const std::string &direction)
724 {
725 auto& map = getAAudioDirection();
726 auto it = map.find(direction);
727 if (it == map.end()) {
728 return 0; // return unknown
729 }
730 return it->second;
731 }
732
733 template <>
lookup(const std::string & performanceMode)734 std::string lookup<AAUDIO_PERFORMANCE_MODE>(const std::string &performanceMode)
735 {
736 auto& map = getAAudioPerformanceMode();
737 auto it = map.find(performanceMode);
738 if (it == map.end()) {
739 return "";
740 }
741 return performanceMode;
742 }
743
744 template <>
lookup(const std::string & performanceMode)745 int32_t lookup<AAUDIO_PERFORMANCE_MODE>(const std::string &performanceMode)
746 {
747 auto& map = getAAudioPerformanceMode();
748 auto it = map.find(performanceMode);
749 if (it == map.end()) {
750 return 0; // return unknown
751 }
752 return it->second;
753 }
754
755 template <>
lookup(const std::string & sharingMode)756 std::string lookup<AAUDIO_SHARING_MODE>(const std::string &sharingMode)
757 {
758 auto& map = getAAudioSharingMode();
759 auto it = map.find(sharingMode);
760 if (it == map.end()) {
761 return "";
762 }
763 return sharingMode;
764 }
765
766 template <>
lookup(const std::string & sharingMode)767 int32_t lookup<AAUDIO_SHARING_MODE>(const std::string &sharingMode)
768 {
769 auto& map = getAAudioSharingMode();
770 auto it = map.find(sharingMode);
771 if (it == map.end()) {
772 return 0; // return unknown
773 }
774 return it->second;
775 }
776
777 } // namespace android::mediametrics::types
778