1 /*
2  * Copyright (C) 2015 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 "DeviceDescriptor.h"
20 #include "HwModule.h"
21 #include "audio_policy_conf.h"
22 #include <system/audio.h>
23 #include <utils/Log.h>
24 #include <utils/Vector.h>
25 #include <utils/SortedVector.h>
26 #include <cutils/config_utils.h>
27 #include <utils/RefBase.h>
28 #include <system/audio_policy.h>
29 
30 namespace android {
31 
32 // ----------------------------------------------------------------------------
33 // Definitions for audio_policy.conf file parsing
34 // ----------------------------------------------------------------------------
35 
36 struct StringToEnum {
37     const char *name;
38     uint32_t value;
39 };
40 
41 // TODO: move to a separate file. Should be in sync with audio.h.
42 #define STRING_TO_ENUM(string) { #string, (uint32_t)string } // uint32_t cast removes warning
43 #define NAME_TO_ENUM(name, value) { name, value }
44 #ifndef ARRAY_SIZE
45 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
46 #endif
47 
48 const StringToEnum sDeviceTypeToEnumTable[] = {
49     STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
50     STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
51     STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER_SAFE),
52     STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
53     STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
54     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO),
55     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET),
56     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
57     STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
58     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP),
59     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES),
60     STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
61     STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
62     STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
63     STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI),
64     STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
65     STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
66     STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
67     STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
68     STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
69     STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
70     STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX),
71     STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE),
72     STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC),
73     STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF),
74     STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM),
75     STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_LINE),
76     STRING_TO_ENUM(AUDIO_DEVICE_OUT_IP),
77     STRING_TO_ENUM(AUDIO_DEVICE_IN_AMBIENT),
78     STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
79     STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
80     STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO),
81     STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
82     STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
83     STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI),
84     STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
85     STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
86     STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
87     STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
88     STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
89     STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
90     STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
91     STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
92     STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
93     STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
94     STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE),
95     STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF),
96     STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
97     STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
98     STRING_TO_ENUM(AUDIO_DEVICE_IN_IP),
99 };
100 
101 const StringToEnum sDeviceNameToEnumTable[] = {
102     NAME_TO_ENUM("Earpiece", AUDIO_DEVICE_OUT_EARPIECE),
103     NAME_TO_ENUM("Speaker", AUDIO_DEVICE_OUT_SPEAKER),
104     NAME_TO_ENUM("Speaker Protected", AUDIO_DEVICE_OUT_SPEAKER_SAFE),
105     NAME_TO_ENUM("Wired Headset", AUDIO_DEVICE_OUT_WIRED_HEADSET),
106     NAME_TO_ENUM("Wired Headphones", AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
107     NAME_TO_ENUM("BT SCO", AUDIO_DEVICE_OUT_BLUETOOTH_SCO),
108     NAME_TO_ENUM("BT SCO Headset", AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET),
109     NAME_TO_ENUM("BT SCO Car Kit", AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
110     NAME_TO_ENUM("", AUDIO_DEVICE_OUT_ALL_SCO),
111     NAME_TO_ENUM("BT A2DP Out", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP),
112     NAME_TO_ENUM("BT A2DP Headphones", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES),
113     NAME_TO_ENUM("BT A2DP Speaker", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
114     NAME_TO_ENUM("", AUDIO_DEVICE_OUT_ALL_A2DP),
115     NAME_TO_ENUM("HDMI Out", AUDIO_DEVICE_OUT_AUX_DIGITAL),
116     NAME_TO_ENUM("HDMI Out", AUDIO_DEVICE_OUT_HDMI),
117     NAME_TO_ENUM("Analog Dock Out", AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
118     NAME_TO_ENUM("Digital Dock Out", AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
119     NAME_TO_ENUM("USB Host Out", AUDIO_DEVICE_OUT_USB_ACCESSORY),
120     NAME_TO_ENUM("USB Device Out", AUDIO_DEVICE_OUT_USB_DEVICE),
121     NAME_TO_ENUM("", AUDIO_DEVICE_OUT_ALL_USB),
122     NAME_TO_ENUM("Reroute Submix Out", AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
123     NAME_TO_ENUM("Telephony Tx", AUDIO_DEVICE_OUT_TELEPHONY_TX),
124     NAME_TO_ENUM("Line Out", AUDIO_DEVICE_OUT_LINE),
125     NAME_TO_ENUM("HDMI ARC Out", AUDIO_DEVICE_OUT_HDMI_ARC),
126     NAME_TO_ENUM("S/PDIF Out", AUDIO_DEVICE_OUT_SPDIF),
127     NAME_TO_ENUM("FM transceiver Out", AUDIO_DEVICE_OUT_FM),
128     NAME_TO_ENUM("Aux Line Out", AUDIO_DEVICE_OUT_AUX_LINE),
129     NAME_TO_ENUM("IP Out", AUDIO_DEVICE_OUT_IP),
130     NAME_TO_ENUM("Ambient Mic", AUDIO_DEVICE_IN_AMBIENT),
131     NAME_TO_ENUM("Built-In Mic", AUDIO_DEVICE_IN_BUILTIN_MIC),
132     NAME_TO_ENUM("BT SCO Headset Mic", AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
133     NAME_TO_ENUM("", AUDIO_DEVICE_IN_ALL_SCO),
134     NAME_TO_ENUM("Wired Headset Mic", AUDIO_DEVICE_IN_WIRED_HEADSET),
135     NAME_TO_ENUM("HDMI In", AUDIO_DEVICE_IN_AUX_DIGITAL),
136     NAME_TO_ENUM("HDMI In", AUDIO_DEVICE_IN_HDMI),
137     NAME_TO_ENUM("Telephony Rx", AUDIO_DEVICE_IN_TELEPHONY_RX),
138     NAME_TO_ENUM("Telephony Rx", AUDIO_DEVICE_IN_VOICE_CALL),
139     NAME_TO_ENUM("Built-In Back Mic", AUDIO_DEVICE_IN_BACK_MIC),
140     NAME_TO_ENUM("Reroute Submix In", AUDIO_DEVICE_IN_REMOTE_SUBMIX),
141     NAME_TO_ENUM("Analog Dock In", AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
142     NAME_TO_ENUM("Digital Dock In", AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
143     NAME_TO_ENUM("USB Host In", AUDIO_DEVICE_IN_USB_ACCESSORY),
144     NAME_TO_ENUM("USB Device In", AUDIO_DEVICE_IN_USB_DEVICE),
145     NAME_TO_ENUM("FM Tuner In", AUDIO_DEVICE_IN_FM_TUNER),
146     NAME_TO_ENUM("TV Tuner In", AUDIO_DEVICE_IN_TV_TUNER),
147     NAME_TO_ENUM("Line In", AUDIO_DEVICE_IN_LINE),
148     NAME_TO_ENUM("S/PDIF In", AUDIO_DEVICE_IN_SPDIF),
149     NAME_TO_ENUM("BT A2DP In", AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
150     NAME_TO_ENUM("Loopback In", AUDIO_DEVICE_IN_LOOPBACK),
151     NAME_TO_ENUM("IP In", AUDIO_DEVICE_IN_IP),
152 };
153 
154 const StringToEnum sOutputFlagNameToEnumTable[] = {
155     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
156     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
157     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
158     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
159     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
160     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
161     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
162     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_TTS),
163     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_RAW),
164     STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_SYNC),
165 };
166 
167 const StringToEnum sInputFlagNameToEnumTable[] = {
168     STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
169     STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
170     STRING_TO_ENUM(AUDIO_INPUT_FLAG_RAW),
171     STRING_TO_ENUM(AUDIO_INPUT_FLAG_SYNC),
172 };
173 
174 const StringToEnum sFormatNameToEnumTable[] = {
175     STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
176     STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
177     STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
178     STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
179     STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
180     STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
181     STRING_TO_ENUM(AUDIO_FORMAT_MP3),
182     STRING_TO_ENUM(AUDIO_FORMAT_AAC),
183     STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN),
184     STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
185     STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR),
186     STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP),
187     STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
188     STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE),
189     STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC),
190     STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD),
191     STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
192     STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD),
193     STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
194     STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1),
195     STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2),
196     STRING_TO_ENUM(AUDIO_FORMAT_OPUS),
197     STRING_TO_ENUM(AUDIO_FORMAT_AC3),
198     STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
199     STRING_TO_ENUM(AUDIO_FORMAT_DTS),
200     STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
201 };
202 
203 const StringToEnum sOutChannelsNameToEnumTable[] = {
204     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
205     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
206     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
207     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
208     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
209 };
210 
211 const StringToEnum sInChannelsNameToEnumTable[] = {
212     STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
213     STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
214     STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
215 };
216 
217 const StringToEnum sIndexChannelsNameToEnumTable[] = {
218     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
219     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
220     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
221     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
222     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
223     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
224     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
225     STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
226 };
227 
228 const StringToEnum sGainModeNameToEnumTable[] = {
229     STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT),
230     STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS),
231     STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP),
232 };
233 
234 class ConfigParsingUtils
235 {
236 public:
237     static uint32_t stringToEnum(const struct StringToEnum *table,
238             size_t size,
239             const char *name);
240     static const char *enumToString(const struct StringToEnum *table,
241             size_t size,
242             uint32_t value);
243     static bool stringToBool(const char *value);
244     static uint32_t parseOutputFlagNames(char *name);
245     static uint32_t parseInputFlagNames(char *name);
246     static audio_devices_t parseDeviceNames(char *name);
247 
248     static void loadHwModules(cnode *root, HwModuleCollection &hwModules,
249                               DeviceVector &availableInputDevices,
250                               DeviceVector &availableOutputDevices,
251                               sp<DeviceDescriptor> &defaultOutputDevices,
252                               bool &isSpeakerDrcEnabled);
253 
254     static void loadGlobalConfig(cnode *root, const sp<HwModule>& module,
255                                  DeviceVector &availableInputDevices,
256                                  DeviceVector &availableOutputDevices,
257                                  sp<DeviceDescriptor> &defaultOutputDevices,
258                                  bool &isSpeakerDrcEnabled);
259 
260     static status_t loadAudioPolicyConfig(const char *path,
261                                           HwModuleCollection &hwModules,
262                                           DeviceVector &availableInputDevices,
263                                           DeviceVector &availableOutputDevices,
264                                           sp<DeviceDescriptor> &defaultOutputDevices,
265                                           bool &isSpeakerDrcEnabled);
266 
267 private:
268     static void loadHwModule(cnode *root, HwModuleCollection &hwModules,
269                              DeviceVector &availableInputDevices,
270                              DeviceVector &availableOutputDevices,
271                              sp<DeviceDescriptor> &defaultOutputDevices,
272                              bool &isSpeakerDrcEnabled);
273 };
274 
275 }; // namespace android
276