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