1 /*
2  * Copyright (C) 2022 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 <memory>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include <aidl/android/hardware/audio/core/SurroundSoundConfig.h>
27 #include <aidl/android/media/audio/common/AudioHalEngineConfig.h>
28 #include <android_audio_policy_configuration.h>
29 #include <android_audio_policy_configuration_enums.h>
30 #include <media/AidlConversionUtil.h>
31 
32 #include "core-impl/Module.h"
33 #include "core-impl/XmlConverter.h"
34 
35 namespace aidl::android::hardware::audio::core::internal {
36 
37 class AudioPolicyConfigXmlConverter {
38   public:
39     using ModuleConfiguration = std::pair<std::string, std::unique_ptr<Module::Configuration>>;
40     using ModuleConfigs = std::vector<ModuleConfiguration>;
41 
AudioPolicyConfigXmlConverter(const std::string & configFilePath)42     explicit AudioPolicyConfigXmlConverter(const std::string& configFilePath)
43         : mConverter(configFilePath, &::android::audio::policy::configuration::read) {
44         if (mConverter.getXsdcConfig()) {
45             init();
46         }
47     }
48 
getError()49     std::string getError() const { return mConverter.getError(); }
getStatus()50     ::android::status_t getStatus() const { return mConverter.getStatus(); }
51 
52     const ::aidl::android::media::audio::common::AudioHalEngineConfig& getAidlEngineConfig();
53     const SurroundSoundConfig& getSurroundSoundConfig();
54     std::unique_ptr<ModuleConfigs> releaseModuleConfigs();
55 
56     // Public for testing purposes.
57     static const SurroundSoundConfig& getDefaultSurroundSoundConfig();
58 
59   private:
60     const std::optional<::android::audio::policy::configuration::AudioPolicyConfiguration>&
getXsdcConfig()61     getXsdcConfig() const {
62         return mConverter.getXsdcConfig();
63     }
64     void addVolumeGroupstoEngineConfig();
65     void init();
66     void mapStreamToVolumeCurve(
67             const ::android::audio::policy::configuration::Volume& xsdcVolumeCurve);
68     void mapStreamsToVolumeCurves();
69     void parseVolumes();
70     ConversionResult<::aidl::android::media::audio::common::AudioHalVolumeCurve>
71     convertVolumeCurveToAidl(
72             const ::android::audio::policy::configuration::Volume& xsdcVolumeCurve);
73 
74     ::aidl::android::media::audio::common::AudioHalEngineConfig mAidlEngineConfig;
75     XmlConverter<::android::audio::policy::configuration::AudioPolicyConfiguration> mConverter;
76     std::unordered_map<std::string, ::android::audio::policy::configuration::Reference>
77             mVolumesReferenceMap;
78     std::unordered_map<::android::audio::policy::configuration::AudioStreamType,
79                        std::vector<::aidl::android::media::audio::common::AudioHalVolumeCurve>>
80             mStreamToVolumeCurvesMap;
81     std::unique_ptr<ModuleConfigs> mModuleConfigurations = std::make_unique<ModuleConfigs>();
82 };
83 
84 }  // namespace aidl::android::hardware::audio::core::internal
85