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 #ifdef ENABLE_CAP_AIDL_HYBRID_MODE 49 // The source used to indicate the configuration from the AIDL HAL but engine still use XML. 50 static const constexpr char* const kHybridAidlConfigSource = "AIDL HAL Hybrid CAP"; 51 #endif 52 // The source used to indicate the default fallback configuration. 53 static const constexpr char* const kDefaultConfigSource = "AudioPolicyConfig::setDefault"; 54 // The suffix of the "engine default" implementation shared library name. 55 static const constexpr char* const kDefaultEngineLibraryNameSuffix = "default"; 56 static const constexpr char* const kCapEngineLibraryNameSuffix = "configurable"; 57 58 // Creates the default (fallback) configuration. 59 static sp<const AudioPolicyConfig> createDefault(); 60 // Attempts to load the configuration from the AIDL config falls back to default on failure. 61 static sp<const AudioPolicyConfig> loadFromApmAidlConfigWithFallback( 62 const media::AudioPolicyConfig& aidl); 63 // Attempts to load the configuration from the XML file, falls back to default on failure. 64 // If the XML file path is not provided, uses `audio_get_audio_policy_config_file` function. 65 static sp<const AudioPolicyConfig> loadFromApmXmlConfigWithFallback( 66 const std::string& xmlFilePath = ""); 67 // The factory method to use in APM tests which craft the configuration manually. 68 static sp<AudioPolicyConfig> createWritableForTests(); 69 // The factory method to use in APM tests which use a custom XML file. 70 static error::Result<sp<AudioPolicyConfig>> loadFromCustomXmlConfigForTests( 71 const std::string& xmlFilePath); 72 // The factory method to use in VTS tests. If the 'configPath' is empty, 73 // it is determined automatically from the list of known config paths. 74 static error::Result<sp<AudioPolicyConfig>> loadFromCustomXmlConfigForVtsTests( 75 const std::string& configPath, const std::string& xmlFileName); 76 77 ~AudioPolicyConfig() = default; 78 getSource()79 const std::string& getSource() const { 80 return mSource; 81 } setSource(const std::string & file)82 void setSource(const std::string& file) { 83 mSource = file; 84 } 85 getEngineLibraryNameSuffix()86 const std::string& getEngineLibraryNameSuffix() const { 87 return mEngineLibraryNameSuffix; 88 } setEngineLibraryNameSuffix(const std::string & suffix)89 void setEngineLibraryNameSuffix(const std::string& suffix) { 90 mEngineLibraryNameSuffix = suffix; 91 } 92 getHwModules()93 const HwModuleCollection& getHwModules() const { return mHwModules; } setHwModules(const HwModuleCollection & hwModules)94 void setHwModules(const HwModuleCollection &hwModules) 95 { 96 mHwModules = hwModules; 97 } 98 getInputDevices()99 const DeviceVector& getInputDevices() const 100 { 101 return mInputDevices; 102 } getOutputDevices()103 const DeviceVector& getOutputDevices() const 104 { 105 return mOutputDevices; 106 } addDevice(const sp<DeviceDescriptor> & device)107 void addDevice(const sp<DeviceDescriptor> &device) 108 { 109 if (audio_is_output_device(device->type())) { 110 mOutputDevices.add(device); 111 } else if (audio_is_input_device(device->type())) { 112 mInputDevices.add(device); 113 } 114 } addInputDevices(const DeviceVector & inputDevices)115 void addInputDevices(const DeviceVector &inputDevices) 116 { 117 mInputDevices.add(inputDevices); 118 } addOutputDevices(const DeviceVector & outputDevices)119 void addOutputDevices(const DeviceVector &outputDevices) 120 { 121 mOutputDevices.add(outputDevices); 122 } 123 getDefaultOutputDevice()124 const sp<DeviceDescriptor>& getDefaultOutputDevice() const { return mDefaultOutputDevice; } setDefaultOutputDevice(const sp<DeviceDescriptor> & defaultDevice)125 void setDefaultOutputDevice(const sp<DeviceDescriptor> &defaultDevice) 126 { 127 mDefaultOutputDevice = defaultDevice; 128 } 129 isCallScreenModeSupported()130 bool isCallScreenModeSupported() const { return mIsCallScreenModeSupported; } setCallScreenModeSupported(bool isCallScreenModeSupported)131 void setCallScreenModeSupported(bool isCallScreenModeSupported) 132 { 133 mIsCallScreenModeSupported = isCallScreenModeSupported; 134 } 135 getSurroundFormats()136 const SurroundFormats &getSurroundFormats() const 137 { 138 return mSurroundFormats; 139 } 140 void setDefaultSurroundFormats(); setSurroundFormats(const SurroundFormats & surroundFormats)141 void setSurroundFormats(const SurroundFormats &surroundFormats) 142 { 143 mSurroundFormats = surroundFormats; 144 } 145 146 void setDefault(); 147 setUseDeepBufferForMediaOverrideForTests(bool useDeepBufferForMedia)148 void setUseDeepBufferForMediaOverrideForTests(bool useDeepBufferForMedia) 149 { 150 mUseDeepBufferForMediaOverride = useDeepBufferForMedia; 151 } 152 bool useDeepBufferForMedia() const; 153 154 private: 155 friend class sp<AudioPolicyConfig>; 156 157 AudioPolicyConfig() = default; 158 159 void augmentData(); 160 status_t loadFromAidl(const media::AudioPolicyConfig& aidl); 161 status_t loadFromXml(const std::string& xmlFilePath, bool forVts); 162 163 std::string mSource; // Not kDefaultConfigSource. Empty source means an empty config. 164 std::string mEngineLibraryNameSuffix = kDefaultEngineLibraryNameSuffix; 165 HwModuleCollection mHwModules; /**< Collection of Module, with Profiles, i.e. Mix Ports. */ 166 DeviceVector mOutputDevices; // Attached output devices. 167 DeviceVector mInputDevices; // Attached input devices. 168 sp<DeviceDescriptor> mDefaultOutputDevice; 169 bool mIsCallScreenModeSupported = false; 170 SurroundFormats mSurroundFormats; 171 std::optional<bool> mUseDeepBufferForMediaOverride; 172 }; 173 174 } // namespace android 175