xref: /aosp_15_r20/frameworks/av/services/audiopolicy/common/managerdefinitions/include/AudioPolicyConfig.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 <optional>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 
24 #include <DeviceDescriptor.h>
25 #include <HwModule.h>
26 #include <android/media/AudioPolicyConfig.h>
27 #include <error/Result.h>
28 #include <utils/StrongPointer.h>
29 #include <utils/RefBase.h>
30 
31 namespace android {
32 
33 // This class gathers together various bits of AudioPolicyManager configuration. It can be filled
34 // out either as a result of parsing the audio_policy_configuration.xml file, from the HAL data, or
35 // to default fallback data.
36 //
37 // The data in this class is immutable once loaded, this is why a pointer to a const is returned
38 // from the factory methods. However, this does not prevent modifications of data bits that
39 // are held inside collections, for example, individual modules, devices, etc.
40 class AudioPolicyConfig : public RefBase
41 {
42 public:
43     // Surround formats, with an optional list of subformats that are equivalent from users' POV.
44     using SurroundFormats = std::unordered_map<audio_format_t, std::unordered_set<audio_format_t>>;
45 
46     // The source used to indicate the configuration from the AIDL HAL.
47     static const constexpr char* const kAidlConfigSource = "AIDL HAL";
48     // The source used to indicate the default fallback configuration.
49     static const constexpr char* const kDefaultConfigSource = "AudioPolicyConfig::setDefault";
50     // The suffix of the "engine default" implementation shared library name.
51     static const constexpr char* const kDefaultEngineLibraryNameSuffix = "default";
52     static const constexpr char* const kCapEngineLibraryNameSuffix = "configurable";
53 
54     // Creates the default (fallback) configuration.
55     static sp<const AudioPolicyConfig> createDefault();
56     // Attempts to load the configuration from the AIDL config falls back to default on failure.
57     static sp<const AudioPolicyConfig> loadFromApmAidlConfigWithFallback(
58             const media::AudioPolicyConfig& aidl);
59     // Attempts to load the configuration from the XML file, falls back to default on failure.
60     // If the XML file path is not provided, uses `audio_get_audio_policy_config_file` function.
61     static sp<const AudioPolicyConfig> loadFromApmXmlConfigWithFallback(
62             const std::string& xmlFilePath = "");
63     // The factory method to use in APM tests which craft the configuration manually.
64     static sp<AudioPolicyConfig> createWritableForTests();
65     // The factory method to use in APM tests which use a custom XML file.
66     static error::Result<sp<AudioPolicyConfig>> loadFromCustomXmlConfigForTests(
67             const std::string& xmlFilePath);
68     // The factory method to use in VTS tests. If the 'configPath' is empty,
69     // it is determined automatically from the list of known config paths.
70     static error::Result<sp<AudioPolicyConfig>> loadFromCustomXmlConfigForVtsTests(
71             const std::string& configPath, const std::string& xmlFileName);
72 
73     ~AudioPolicyConfig() = default;
74 
getSource()75     const std::string& getSource() const {
76         return mSource;
77     }
setSource(const std::string & file)78     void setSource(const std::string& file) {
79         mSource = file;
80     }
81 
getEngineLibraryNameSuffix()82     const std::string& getEngineLibraryNameSuffix() const {
83         return mEngineLibraryNameSuffix;
84     }
setEngineLibraryNameSuffix(const std::string & suffix)85     void setEngineLibraryNameSuffix(const std::string& suffix) {
86         mEngineLibraryNameSuffix = suffix;
87     }
88 
getHwModules()89     const HwModuleCollection& getHwModules() const { return mHwModules; }
setHwModules(const HwModuleCollection & hwModules)90     void setHwModules(const HwModuleCollection &hwModules)
91     {
92         mHwModules = hwModules;
93     }
94 
getInputDevices()95     const DeviceVector& getInputDevices() const
96     {
97         return mInputDevices;
98     }
getOutputDevices()99     const DeviceVector& getOutputDevices() const
100     {
101         return mOutputDevices;
102     }
addDevice(const sp<DeviceDescriptor> & device)103     void addDevice(const sp<DeviceDescriptor> &device)
104     {
105         if (audio_is_output_device(device->type())) {
106             mOutputDevices.add(device);
107         } else if (audio_is_input_device(device->type())) {
108             mInputDevices.add(device);
109         }
110     }
addInputDevices(const DeviceVector & inputDevices)111     void addInputDevices(const DeviceVector &inputDevices)
112     {
113         mInputDevices.add(inputDevices);
114     }
addOutputDevices(const DeviceVector & outputDevices)115     void addOutputDevices(const DeviceVector &outputDevices)
116     {
117         mOutputDevices.add(outputDevices);
118     }
119 
getDefaultOutputDevice()120     const sp<DeviceDescriptor>& getDefaultOutputDevice() const { return mDefaultOutputDevice; }
setDefaultOutputDevice(const sp<DeviceDescriptor> & defaultDevice)121     void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice)
122     {
123         mDefaultOutputDevice = defaultDevice;
124     }
125 
isCallScreenModeSupported()126     bool isCallScreenModeSupported() const { return mIsCallScreenModeSupported; }
setCallScreenModeSupported(bool isCallScreenModeSupported)127     void setCallScreenModeSupported(bool isCallScreenModeSupported)
128     {
129         mIsCallScreenModeSupported = isCallScreenModeSupported;
130     }
131 
getSurroundFormats()132     const SurroundFormats &getSurroundFormats() const
133     {
134         return mSurroundFormats;
135     }
136     void setDefaultSurroundFormats();
setSurroundFormats(const SurroundFormats & surroundFormats)137     void setSurroundFormats(const SurroundFormats &surroundFormats)
138     {
139         mSurroundFormats = surroundFormats;
140     }
141 
142     void setDefault();
143 
setUseDeepBufferForMediaOverrideForTests(bool useDeepBufferForMedia)144     void setUseDeepBufferForMediaOverrideForTests(bool useDeepBufferForMedia)
145     {
146         mUseDeepBufferForMediaOverride = useDeepBufferForMedia;
147     }
148     bool useDeepBufferForMedia() const;
149 
150 private:
151     friend class sp<AudioPolicyConfig>;
152 
153     AudioPolicyConfig() = default;
154 
155     void augmentData();
156     status_t loadFromAidl(const media::AudioPolicyConfig& aidl);
157     status_t loadFromXml(const std::string& xmlFilePath, bool forVts);
158 
159     std::string mSource;  // Not kDefaultConfigSource. Empty source means an empty config.
160     std::string mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix;
161     HwModuleCollection mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */
162     DeviceVector mOutputDevices;  // Attached output devices.
163     DeviceVector mInputDevices;   // Attached input devices.
164     sp<DeviceDescriptor> mDefaultOutputDevice;
165     bool mIsCallScreenModeSupported = false;
166     SurroundFormats mSurroundFormats;
167     std::optional<bool> mUseDeepBufferForMediaOverride;
168 };
169 
170 } // namespace android
171