1 /*
2  * Copyright (C) 2018 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 <EngineConfig.h>
20 #include <EngineInterface.h>
21 #include <ProductStrategy.h>
22 #include <VolumeGroup.h>
23 #include <LastRemovableMediaDevices.h>
24 
25 namespace android {
26 namespace audio_policy {
27 
28 class EngineBase : public EngineInterface
29 {
30 public:
31     ///
32     /// from EngineInterface
33     ///
34     android::status_t initCheck() override;
35 
36     void setObserver(AudioPolicyManagerObserver *observer) override;
37 
38     status_t setPhoneState(audio_mode_t mode) override;
39 
getPhoneState()40     audio_mode_t getPhoneState() const override { return mPhoneState; }
41 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)42     status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) override
43     {
44         mForceUse[usage] = config;
45         return NO_ERROR;
46     }
47 
getForceUse(audio_policy_force_use_t usage)48     audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const override
49     {
50         return mForceUse[usage];
51     }
52     android::status_t setDeviceConnectionState(const sp<DeviceDescriptor> /*devDesc*/,
53                                                audio_policy_dev_state_t /*state*/) override;
54 
55     product_strategy_t getProductStrategyForAttributes(
56             const audio_attributes_t &attr) const override;
57 
58     audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const override;
59 
60     audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const override;
61 
62     StreamTypeVector getStreamTypesForProductStrategy(product_strategy_t ps) const override;
63 
64     AttributesVector getAllAttributesForProductStrategy(product_strategy_t ps) const override;
65 
66     StrategyVector getOrderedProductStrategies() const override;
67 
68     status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) const override;
69 
70     VolumeCurves *getVolumeCurvesForAttributes(const audio_attributes_t &attr) const override;
71 
72     VolumeCurves *getVolumeCurvesForStreamType(audio_stream_type_t stream) const override;
73 
getVolumeCurvesForVolumeGroup(volume_group_t group)74     IVolumeCurves *getVolumeCurvesForVolumeGroup(volume_group_t group) const override
75     {
76        return mVolumeGroups.find(group) != end(mVolumeGroups) ?
77                    mVolumeGroups.at(group)->getVolumeCurves() : nullptr;
78     }
79 
80     VolumeGroupVector getVolumeGroups() const override;
81 
82     volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const override;
83 
84     volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const override;
85 
86     status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const override;
87 
88     std::vector<audio_devices_t> getLastRemovableMediaDevices(
89             device_out_group_t group = GROUP_NONE) const
90     {
91         return mLastRemovableMediaDevices.getLastRemovableMediaDevices(group);
92     }
93 
94     void dump(String8 *dst) const override;
95 
96     status_t setPreferredDeviceForStrategy(product_strategy_t strategy,
97             const AudioDeviceTypeAddr &device) override;
98 
99     status_t removePreferredDeviceForStrategy(product_strategy_t strategy) override;
100 
101     status_t getPreferredDeviceForStrategy(product_strategy_t strategy,
102             AudioDeviceTypeAddr &device) const override;
103 
104     engineConfig::ParsingResult loadAudioPolicyEngineConfig();
105 
getProductStrategies()106     const ProductStrategyMap &getProductStrategies() const { return mProductStrategies; }
107 
getProductStrategies()108     ProductStrategyMap &getProductStrategies() { return mProductStrategies; }
109 
110     product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const;
111 
112     product_strategy_t getProductStrategyByName(const std::string &name) const;
113 
getApmObserver()114     AudioPolicyManagerObserver *getApmObserver() const { return mApmObserver; }
115 
isInCall()116     inline bool isInCall() const
117     {
118         return is_state_in_call(getPhoneState());
119     }
120 
toVolumeSource(audio_stream_type_t stream)121     VolumeSource toVolumeSource(audio_stream_type_t stream) const
122     {
123         return static_cast<VolumeSource>(getVolumeGroupForStreamType(stream));
124     }
125 
126     status_t switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst);
127 
128     status_t restoreOriginVolumeCurve(audio_stream_type_t stream);
129 
130 private:
131     AudioPolicyManagerObserver *mApmObserver = nullptr;
132 
133     ProductStrategyMap mProductStrategies;
134     ProductStrategyPreferredRoutingMap mProductStrategyPreferredDevices;
135     VolumeGroupMap mVolumeGroups;
136     LastRemovableMediaDevices mLastRemovableMediaDevices;
137     audio_mode_t mPhoneState = AUDIO_MODE_NORMAL;  /**< current phone state. */
138 
139     /** current forced use configuration. */
140     audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT] = {};
141 };
142 
143 } // namespace audio_policy
144 } // namespace android
145