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