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