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 #pragma once
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 namespace android::mediametrics::types {
24 
25 // Helper methods that map mediametrics logged strings to integer codes.
26 // In R we do not use the integer codes, but rather we can use these maps
27 // to validate correct strings.
28 const std::unordered_map<std::string, int32_t>& getAudioCallerNameMap();
29 const std::unordered_map<std::string, int64_t>& getAudioDeviceInMap();
30 const std::unordered_map<std::string, int64_t>& getAudioDeviceOutMap();
31 const std::unordered_map<std::string, int32_t>& getAudioThreadTypeMap();
32 const std::unordered_map<std::string, int32_t>& getAudioTrackTraitsMap();
33 const std::unordered_map<std::string, int32_t>& getHeadTrackingModeMap();
34 const std::unordered_map<std::string, int32_t>& getSpatializerLevelMap();
35 const std::unordered_map<std::string, int32_t>& getSpatializerModeMap();
36 
37 std::vector<int32_t> vectorFromMap(
38         const std::string &str, const std::unordered_map<std::string, int32_t>& map);
39 
40 std::vector<int64_t> channelMaskVectorFromString(const std::string &s);
41 
42 // Enumeration for the device connection results.
43 enum DeviceConnectionResult : int32_t {
44     DEVICE_CONNECTION_RESULT_SUCCESS = 0,              // Audio delivered
45     DEVICE_CONNECTION_RESULT_UNKNOWN = 1,              // Success is unknown.
46     DEVICE_CONNECTION_RESULT_JAVA_SERVICE_CANCEL = 2,  // Canceled in Java service
47     // Do not modify the constants above after R.  Adding new constants is fine.
48 };
49 
50 // Enumeration for all the string translations to integers (generally int32_t) unless noted.
51 // This is used to index the template method below:
52 // template <AudioEnumCategory C, typename T, typename S>  T lookup(const S &str);
53 //
54 // Okay to keep AudioEnumCategory alphabetical and add new translations in the middle.
55 enum AudioEnumCategory {
56     AAUDIO_DIRECTION,
57     AAUDIO_PERFORMANCE_MODE,
58     AAUDIO_SHARING_MODE,
59     AUDIO_DEVICE_INFO_TYPE,
60     CALLER_NAME,
61     CONTENT_TYPE,
62     ENCODING,
63     HEAD_TRACKING_MODE,
64     INPUT_DEVICE,  // int64_t
65     INPUT_FLAG,
66     OUTPUT_DEVICE, // int64_t
67     OUTPUT_FLAG,
68     SOURCE_TYPE,
69     SPATIALIZER_LEVEL,
70     SPATIALIZER_MODE,
71     STATUS,
72     STREAM_TYPE,
73     THREAD_TYPE,
74     TRACK_TRAITS,
75     USAGE,
76 };
77 
78 // Convert a string (or arbitrary S) from an AudioEnumCategory to a particular type.
79 // This is used to convert log std::strings back to the original type (int32_t or int64_t).
80 //
81 // For a string, generally there is a prefix "AUDIO_INPUT_FLAG" or some such that could
82 // actually indicate the category so the AudioEnumCategory could be superfluous, but
83 // we use it to find the proper default value in case of an unknown string.
84 //
85 // lookup<ENCODING, int32_t>("AUDIO_FORMAT_PCM_16_BIT") -> 1
86 //
87 template <AudioEnumCategory C, typename T, typename S>
88 T lookup(const S &str);
89 
90 // Helper: Allow using a const char * in lieu of std::string.
91 template <AudioEnumCategory C, typename T>
lookup(const char * str)92 T lookup(const char *str) {
93     return lookup<C, T, std::string>(str);
94 }
95 
96 bool isInputThreadType(const std::string &threadType);
97 
98 } // namespace android::mediametrics::types
99