• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bool fallbackOnDefault = true) 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(
83             const audio_attributes_t &attr, bool fallbackOnDefault = true) const override;
84 
85     volume_group_t getVolumeGroupForStreamType(
86             audio_stream_type_t stream, bool fallbackOnDefault = true) const override;
87 
88     status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) const override;
89 
90     /**
91      * Get the list of currently connected removable device types ordered from most recently
92      * connected to least recently connected.
93      * @param group the device group to consider: wired, a2dp... If none, consider all groups.
94      * @param excludedDevices list of device types to ignore
95      * @return a potentially empty ordered list of connected removable devices.
96      */
97     std::vector<audio_devices_t> getLastRemovableMediaDevices(
98             device_out_group_t group = GROUP_NONE,
99             std::vector<audio_devices_t> excludedDevices = {}) const {
100         return mLastRemovableMediaDevices.getLastRemovableMediaDevices(group, excludedDevices);
101     }
102 
103     void dump(String8 *dst) const override;
104 
105     status_t setDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role,
106             const AudioDeviceTypeAddrVector &devices) override;
107 
108     status_t removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) override;
109 
110     status_t getDevicesForRoleAndStrategy(product_strategy_t strategy, device_role_t role,
111             AudioDeviceTypeAddrVector &devices) const override;
112 
113     engineConfig::ParsingResult loadAudioPolicyEngineConfig();
114 
getProductStrategies()115     const ProductStrategyMap &getProductStrategies() const { return mProductStrategies; }
116 
getProductStrategies()117     ProductStrategyMap &getProductStrategies() { return mProductStrategies; }
118 
119     product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const;
120 
121     product_strategy_t getProductStrategyByName(const std::string &name) const;
122 
getApmObserver()123     AudioPolicyManagerObserver *getApmObserver() const { return mApmObserver; }
124 
isInCall()125     inline bool isInCall() const
126     {
127         return is_state_in_call(getPhoneState());
128     }
129 
toVolumeSource(audio_stream_type_t stream)130     VolumeSource toVolumeSource(audio_stream_type_t stream) const
131     {
132         return static_cast<VolumeSource>(getVolumeGroupForStreamType(stream));
133     }
134 
135     status_t switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst);
136 
137     status_t restoreOriginVolumeCurve(audio_stream_type_t stream);
138 
139     status_t setDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
140             const AudioDeviceTypeAddrVector &devices) override;
141 
142     status_t addDevicesRoleForCapturePreset(audio_source_t audioSource, device_role_t role,
143             const AudioDeviceTypeAddrVector &devices) override;
144 
145     /**
146      * Remove devices role for capture preset. When `forceMatched` is true, the devices to be
147      * removed must all show as role for the capture preset. Otherwise, only devices that has shown
148      * as role for the capture preset will be remove.
149      */
150     status_t doRemoveDevicesRoleForCapturePreset(audio_source_t audioSource,
151             device_role_t role, const AudioDeviceTypeAddrVector& devices,
152             bool forceMatched=true);
153 
154     status_t removeDevicesRoleForCapturePreset(audio_source_t audioSource,
155             device_role_t role, const AudioDeviceTypeAddrVector& devices) override;
156 
157     status_t clearDevicesRoleForCapturePreset(audio_source_t audioSource,
158             device_role_t role) override;
159 
160     status_t getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
161             device_role_t role, AudioDeviceTypeAddrVector &devices) const override;
162 
163     DeviceVector getActiveMediaDevices(const DeviceVector& availableDevices) const override;
164 
165 private:
166     /**
167      * Get media devices as the given role
168      *
169      * @param role the audio devices role
170      * @param availableDevices all available devices
171      * @param devices the DeviceVector to store devices as the given role
172      * @return NO_ERROR if all devices associated to the given role are present in available devices
173      *         NAME_NO_FOUND if there is no strategy for media or there are no devices associate to
174      *         the given role
175      *         NOT_ENOUGH_DATA if not all devices as given role are present in available devices
176      */
177     status_t getMediaDevicesForRole(device_role_t role, const DeviceVector& availableDevices,
178             DeviceVector& devices) const;
179 
180     void dumpCapturePresetDevicesRoleMap(String8 *dst, int spaces) const;
181 
182     AudioPolicyManagerObserver *mApmObserver = nullptr;
183 
184     ProductStrategyMap mProductStrategies;
185     ProductStrategyDevicesRoleMap mProductStrategyDeviceRoleMap;
186     CapturePresetDevicesRoleMap mCapturePresetDevicesRoleMap;
187     VolumeGroupMap mVolumeGroups;
188     LastRemovableMediaDevices mLastRemovableMediaDevices;
189     audio_mode_t mPhoneState = AUDIO_MODE_NORMAL;  /**< current phone state. */
190 
191     /** current forced use configuration. */
192     audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT] = {};
193 };
194 
195 } // namespace audio_policy
196 } // namespace android
197