1 /*
2  * Copyright (C) 2009 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 <unordered_map>
20 #include <unordered_set>
21 
22 #include <AudioPatch.h>
23 #include <DeviceDescriptor.h>
24 #include <IOProfile.h>
25 #include <HwModule.h>
26 #include <PolicyAudioPort.h>
27 #include <AudioInputDescriptor.h>
28 #include <AudioOutputDescriptor.h>
29 #include <AudioPolicyMix.h>
30 #include <EffectDescriptor.h>
31 #include <SoundTriggerSession.h>
32 #include <media/AudioProfile.h>
33 
34 namespace android {
35 
36 class AudioPolicyConfig
37 {
38 public:
AudioPolicyConfig(HwModuleCollection & hwModules,DeviceVector & outputDevices,DeviceVector & inputDevices,sp<DeviceDescriptor> & defaultOutputDevice)39     AudioPolicyConfig(HwModuleCollection &hwModules,
40                       DeviceVector &outputDevices,
41                       DeviceVector &inputDevices,
42                       sp<DeviceDescriptor> &defaultOutputDevice)
43         : mEngineLibraryNameSuffix(kDefaultEngineLibraryNameSuffix),
44           mHwModules(hwModules),
45           mOutputDevices(outputDevices),
46           mInputDevices(inputDevices),
47           mDefaultOutputDevice(defaultOutputDevice),
48           mIsSpeakerDrcEnabled(false),
49           mIsCallScreenModeSupported(false)
50     {}
51 
getSource()52     const std::string& getSource() const {
53         return mSource;
54     }
55 
setSource(const std::string & file)56     void setSource(const std::string& file) {
57         mSource = file;
58     }
59 
getEngineLibraryNameSuffix()60     const std::string& getEngineLibraryNameSuffix() const {
61         return mEngineLibraryNameSuffix;
62     }
63 
setEngineLibraryNameSuffix(const std::string & suffix)64     void setEngineLibraryNameSuffix(const std::string& suffix) {
65         mEngineLibraryNameSuffix = suffix;
66     }
67 
setHwModules(const HwModuleCollection & hwModules)68     void setHwModules(const HwModuleCollection &hwModules)
69     {
70         mHwModules = hwModules;
71     }
72 
addDevice(const sp<DeviceDescriptor> & device)73     void addDevice(const sp<DeviceDescriptor> &device)
74     {
75         if (audio_is_output_device(device->type())) {
76             mOutputDevices.add(device);
77         } else if (audio_is_input_device(device->type())) {
78             mInputDevices.add(device);
79         }
80     }
81 
addInputDevices(const DeviceVector & inputDevices)82     void addInputDevices(const DeviceVector &inputDevices)
83     {
84         mInputDevices.add(inputDevices);
85     }
86 
addOutputDevices(const DeviceVector & outputDevices)87     void addOutputDevices(const DeviceVector &outputDevices)
88     {
89         mOutputDevices.add(outputDevices);
90     }
91 
isSpeakerDrcEnabled()92     bool isSpeakerDrcEnabled() const { return mIsSpeakerDrcEnabled; }
93 
setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)94     void setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)
95     {
96         mIsSpeakerDrcEnabled = isSpeakerDrcEnabled;
97     }
98 
isCallScreenModeSupported()99     bool isCallScreenModeSupported() const { return mIsCallScreenModeSupported; }
100 
setCallScreenModeSupported(bool isCallScreenModeSupported)101     void setCallScreenModeSupported(bool isCallScreenModeSupported)
102     {
103         mIsCallScreenModeSupported = isCallScreenModeSupported;
104     }
105 
106 
getHwModules()107     const HwModuleCollection getHwModules() const { return mHwModules; }
108 
getInputDevices()109     const DeviceVector &getInputDevices() const
110     {
111         return mInputDevices;
112     }
113 
getOutputDevices()114     const DeviceVector &getOutputDevices() const
115     {
116         return mOutputDevices;
117     }
118 
setDefaultOutputDevice(const sp<DeviceDescriptor> & defaultDevice)119     void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice)
120     {
121         mDefaultOutputDevice = defaultDevice;
122     }
123 
getDefaultOutputDevice()124     const sp<DeviceDescriptor> &getDefaultOutputDevice() const { return mDefaultOutputDevice; }
125 
setDefault(void)126     void setDefault(void)
127     {
128         mSource = "AudioPolicyConfig::setDefault";
129         mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix;
130         mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
131         mDefaultOutputDevice->addAudioProfile(AudioProfile::createFullDynamic(gDynamicFormat));
132         sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
133         defaultInputDevice->addAudioProfile(AudioProfile::createFullDynamic(gDynamicFormat));
134         sp<AudioProfile> micProfile = new AudioProfile(
135                 AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO, 8000);
136         defaultInputDevice->addAudioProfile(micProfile);
137         mOutputDevices.add(mDefaultOutputDevice);
138         mInputDevices.add(defaultInputDevice);
139 
140         sp<HwModule> module = new HwModule(AUDIO_HARDWARE_MODULE_ID_PRIMARY, 2 /*halVersionMajor*/);
141         mHwModules.add(module);
142 
143         sp<OutputProfile> outProfile = new OutputProfile("primary");
144         outProfile->addAudioProfile(
145                 new AudioProfile(AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 44100));
146         outProfile->addSupportedDevice(mDefaultOutputDevice);
147         outProfile->setFlags(AUDIO_OUTPUT_FLAG_PRIMARY);
148         module->addOutputProfile(outProfile);
149 
150         sp<InputProfile> inProfile = new InputProfile("primary");
151         inProfile->addAudioProfile(micProfile);
152         inProfile->addSupportedDevice(defaultInputDevice);
153         module->addInputProfile(inProfile);
154 
155         setDefaultSurroundFormats();
156     }
157 
158     // Surround formats, with an optional list of subformats that are equivalent from users' POV.
159     using SurroundFormats = std::unordered_map<audio_format_t, std::unordered_set<audio_format_t>>;
160 
getSurroundFormats()161     const SurroundFormats &getSurroundFormats() const
162     {
163         return mSurroundFormats;
164     }
165 
setSurroundFormats(const SurroundFormats & surroundFormats)166     void setSurroundFormats(const SurroundFormats &surroundFormats)
167     {
168         mSurroundFormats = surroundFormats;
169     }
170 
setDefaultSurroundFormats()171     void setDefaultSurroundFormats()
172     {
173         mSurroundFormats = {
174             {AUDIO_FORMAT_AC3, {}},
175             {AUDIO_FORMAT_E_AC3, {}},
176             {AUDIO_FORMAT_DTS, {}},
177             {AUDIO_FORMAT_DTS_HD, {}},
178             {AUDIO_FORMAT_AAC_LC, {
179                     AUDIO_FORMAT_AAC_HE_V1, AUDIO_FORMAT_AAC_HE_V2, AUDIO_FORMAT_AAC_ELD,
180                     AUDIO_FORMAT_AAC_XHE}},
181             {AUDIO_FORMAT_DOLBY_TRUEHD, {}},
182             {AUDIO_FORMAT_E_AC3_JOC, {}},
183             {AUDIO_FORMAT_AC4, {}}};
184     }
185 
186 private:
187     static const constexpr char* const kDefaultEngineLibraryNameSuffix = "default";
188 
189     std::string mSource;
190     std::string mEngineLibraryNameSuffix;
191     HwModuleCollection &mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
192     DeviceVector &mOutputDevices;
193     DeviceVector &mInputDevices;
194     sp<DeviceDescriptor> &mDefaultOutputDevice;
195     // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
196     // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
197     // Note: remove also speaker_drc_enabled from global configuration of XML config file.
198     bool mIsSpeakerDrcEnabled;
199     bool mIsCallScreenModeSupported;
200     SurroundFormats mSurroundFormats;
201 };
202 
203 } // namespace android
204