• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This class gathers together various bits of AudioPolicyManager
37 // configuration, which are usually filled out as a result of parsing
38 // the audio_policy_configuration.xml file.
39 //
40 // Note that AudioPolicyConfig doesn't own some of the data,
41 // it simply proxies access to the fields of AudioPolicyManager
42 // class. Be careful about the fields that are references,
43 // e.g. 'mOutputDevices'. This also means that it's impossible
44 // to implement "deep copying" of this class without re-designing it.
45 class AudioPolicyConfig
46 {
47 public:
AudioPolicyConfig(HwModuleCollection & hwModules,DeviceVector & outputDevices,DeviceVector & inputDevices,sp<DeviceDescriptor> & defaultOutputDevice)48     AudioPolicyConfig(HwModuleCollection &hwModules,
49                       DeviceVector &outputDevices,
50                       DeviceVector &inputDevices,
51                       sp<DeviceDescriptor> &defaultOutputDevice)
52         : mHwModules(hwModules),
53           mOutputDevices(outputDevices),
54           mInputDevices(inputDevices),
55           mDefaultOutputDevice(defaultOutputDevice) {
56         clear();
57     }
58 
clear()59     void clear() {
60         mSource = {};
61         mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix;
62         mHwModules.clear();
63         mOutputDevices.clear();
64         mInputDevices.clear();
65         mDefaultOutputDevice.clear();
66         mIsSpeakerDrcEnabled = false;
67         mIsCallScreenModeSupported = false;
68         mSurroundFormats.clear();
69     }
70 
getSource()71     const std::string& getSource() const {
72         return mSource;
73     }
74 
setSource(const std::string & file)75     void setSource(const std::string& file) {
76         mSource = file;
77     }
78 
getEngineLibraryNameSuffix()79     const std::string& getEngineLibraryNameSuffix() const {
80         return mEngineLibraryNameSuffix;
81     }
82 
setEngineLibraryNameSuffix(const std::string & suffix)83     void setEngineLibraryNameSuffix(const std::string& suffix) {
84         mEngineLibraryNameSuffix = suffix;
85     }
86 
setHwModules(const HwModuleCollection & hwModules)87     void setHwModules(const HwModuleCollection &hwModules)
88     {
89         mHwModules = hwModules;
90     }
91 
addDevice(const sp<DeviceDescriptor> & device)92     void addDevice(const sp<DeviceDescriptor> &device)
93     {
94         if (audio_is_output_device(device->type())) {
95             mOutputDevices.add(device);
96         } else if (audio_is_input_device(device->type())) {
97             mInputDevices.add(device);
98         }
99     }
100 
addInputDevices(const DeviceVector & inputDevices)101     void addInputDevices(const DeviceVector &inputDevices)
102     {
103         mInputDevices.add(inputDevices);
104     }
105 
addOutputDevices(const DeviceVector & outputDevices)106     void addOutputDevices(const DeviceVector &outputDevices)
107     {
108         mOutputDevices.add(outputDevices);
109     }
110 
isSpeakerDrcEnabled()111     bool isSpeakerDrcEnabled() const { return mIsSpeakerDrcEnabled; }
112 
setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)113     void setSpeakerDrcEnabled(bool isSpeakerDrcEnabled)
114     {
115         mIsSpeakerDrcEnabled = isSpeakerDrcEnabled;
116     }
117 
isCallScreenModeSupported()118     bool isCallScreenModeSupported() const { return mIsCallScreenModeSupported; }
119 
setCallScreenModeSupported(bool isCallScreenModeSupported)120     void setCallScreenModeSupported(bool isCallScreenModeSupported)
121     {
122         mIsCallScreenModeSupported = isCallScreenModeSupported;
123     }
124 
125 
getHwModules()126     const HwModuleCollection getHwModules() const { return mHwModules; }
127 
getInputDevices()128     const DeviceVector &getInputDevices() const
129     {
130         return mInputDevices;
131     }
132 
getOutputDevices()133     const DeviceVector &getOutputDevices() const
134     {
135         return mOutputDevices;
136     }
137 
setDefaultOutputDevice(const sp<DeviceDescriptor> & defaultDevice)138     void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice)
139     {
140         mDefaultOutputDevice = defaultDevice;
141     }
142 
getDefaultOutputDevice()143     const sp<DeviceDescriptor> &getDefaultOutputDevice() const { return mDefaultOutputDevice; }
144 
setDefault(void)145     void setDefault(void)
146     {
147         mSource = "AudioPolicyConfig::setDefault";
148         mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix;
149         mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
150         mDefaultOutputDevice->addAudioProfile(AudioProfile::createFullDynamic(gDynamicFormat));
151         sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
152         defaultInputDevice->addAudioProfile(AudioProfile::createFullDynamic(gDynamicFormat));
153         sp<AudioProfile> micProfile = new AudioProfile(
154                 AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO, 8000);
155         defaultInputDevice->addAudioProfile(micProfile);
156         mOutputDevices.add(mDefaultOutputDevice);
157         mInputDevices.add(defaultInputDevice);
158 
159         sp<HwModule> module = new HwModule(AUDIO_HARDWARE_MODULE_ID_PRIMARY, 2 /*halVersionMajor*/);
160         mHwModules.add(module);
161 
162         sp<OutputProfile> outProfile = new OutputProfile("primary");
163         outProfile->addAudioProfile(
164                 new AudioProfile(AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 44100));
165         outProfile->addSupportedDevice(mDefaultOutputDevice);
166         outProfile->setFlags(AUDIO_OUTPUT_FLAG_PRIMARY);
167         module->addOutputProfile(outProfile);
168 
169         sp<InputProfile> inProfile = new InputProfile("primary");
170         inProfile->addAudioProfile(micProfile);
171         inProfile->addSupportedDevice(defaultInputDevice);
172         module->addInputProfile(inProfile);
173 
174         setDefaultSurroundFormats();
175     }
176 
177     // Surround formats, with an optional list of subformats that are equivalent from users' POV.
178     using SurroundFormats = std::unordered_map<audio_format_t, std::unordered_set<audio_format_t>>;
179 
getSurroundFormats()180     const SurroundFormats &getSurroundFormats() const
181     {
182         return mSurroundFormats;
183     }
184 
setSurroundFormats(const SurroundFormats & surroundFormats)185     void setSurroundFormats(const SurroundFormats &surroundFormats)
186     {
187         mSurroundFormats = surroundFormats;
188     }
189 
setDefaultSurroundFormats()190     void setDefaultSurroundFormats()
191     {
192         mSurroundFormats = {
193             {AUDIO_FORMAT_AC3, {}},
194             {AUDIO_FORMAT_E_AC3, {}},
195             {AUDIO_FORMAT_DTS, {}},
196             {AUDIO_FORMAT_DTS_HD, {}},
197             {AUDIO_FORMAT_AAC_LC, {
198                     AUDIO_FORMAT_AAC_HE_V1, AUDIO_FORMAT_AAC_HE_V2, AUDIO_FORMAT_AAC_ELD,
199                     AUDIO_FORMAT_AAC_XHE}},
200             {AUDIO_FORMAT_DOLBY_TRUEHD, {}},
201             {AUDIO_FORMAT_E_AC3_JOC, {}},
202             {AUDIO_FORMAT_AC4, {}}};
203     }
204 
205 private:
206     static const constexpr char* const kDefaultEngineLibraryNameSuffix = "default";
207 
208     std::string mSource;
209     std::string mEngineLibraryNameSuffix;
210     HwModuleCollection &mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
211     DeviceVector &mOutputDevices;
212     DeviceVector &mInputDevices;
213     sp<DeviceDescriptor> &mDefaultOutputDevice;
214     // TODO: remove when legacy conf file is removed. true on devices that use DRC on the
215     // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly.
216     // Note: remove also speaker_drc_enabled from global configuration of XML config file.
217     bool mIsSpeakerDrcEnabled;
218     bool mIsCallScreenModeSupported;
219     SurroundFormats mSurroundFormats;
220 };
221 
222 } // namespace android
223