1 /*
2  * Copyright (C) 2015 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 "EngineBase.h"
20 #include "EngineInterface.h"
21 #include <policy.h>
22 
23 namespace android
24 {
25 
26 class AudioPolicyManagerObserver;
27 
28 namespace audio_policy
29 {
30 
31 enum legacy_strategy {
32     STRATEGY_NONE = -1,
33     STRATEGY_MEDIA,
34     STRATEGY_PHONE,
35     STRATEGY_SONIFICATION,
36     STRATEGY_SONIFICATION_RESPECTFUL,
37     STRATEGY_DTMF,
38     STRATEGY_ENFORCED_AUDIBLE,
39     STRATEGY_TRANSMITTED_THROUGH_SPEAKER,
40     STRATEGY_ACCESSIBILITY,
41     STRATEGY_REROUTING,
42     STRATEGY_CALL_ASSISTANT,
43 };
44 
45 class Engine : public EngineBase
46 {
47 public:
48     Engine() = default;
49     virtual ~Engine() = default;
50     Engine(const Engine &object) = delete;
51     Engine &operator=(const Engine &object) = delete;
52 
53     ///
54     /// from EngineInterface
55     ///
56     status_t loadFromHalConfigWithFallback(
57             const media::audio::common::AudioHalEngineConfig& config) override;
58     status_t loadFromXmlConfigWithFallback(const std::string& xmlFilePath = "") override;
59 
60 private:
61     ///
62     /// from EngineBase, so from EngineInterface
63     ///
64     status_t setForceUse(audio_policy_force_use_t usage,
65                          audio_policy_forced_cfg_t config) override;
66 
67     DeviceVector getOutputDevicesForAttributes(const audio_attributes_t &attr,
68                                                const sp<DeviceDescriptor> &preferedDevice = nullptr,
69                                                bool fromCache = false) const override;
70 
71     DeviceVector getOutputDevicesForStream(audio_stream_type_t stream,
72                                            bool fromCache = false) const override;
73 
74     sp<DeviceDescriptor> getInputDeviceForAttributes(const audio_attributes_t &attr,
75                                                      uid_t uid = 0,
76                                                      audio_session_t session = AUDIO_SESSION_NONE,
77                                                      sp<AudioPolicyMix> *mix = nullptr)
78                                                      const override;
79 
80     void setStrategyDevices(const sp<ProductStrategy>& strategy,
81                             const DeviceVector& devices) override;
82 
83     DeviceVector getDevicesForProductStrategy(product_strategy_t strategy) const override;
84 
85 private:
86     template<typename T>
87     status_t loadWithFallback(const T& configSource);
88 
89     status_t setDefaultDevice(audio_devices_t device);
90 
91     void filterOutputDevicesForStrategy(legacy_strategy strategy,
92                                             DeviceVector& availableOutputDevices,
93                                             const SwAudioOutputCollection &outputs) const;
94 
95     product_strategy_t remapStrategyFromContext(product_strategy_t strategy,
96                                             const SwAudioOutputCollection &outputs) const;
97 
98     DeviceVector getDevicesForStrategyInt(legacy_strategy strategy,
99                                           DeviceVector availableOutputDevices,
100                                           const SwAudioOutputCollection &outputs) const;
101 
102     sp<DeviceDescriptor> getDeviceForInputSource(audio_source_t inputSource) const;
103 
104     product_strategy_t getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const;
105     audio_devices_t getPreferredDeviceTypeForLegacyStrategy(
106         const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const;
107     DeviceVector getPreferredAvailableDevicesForInputSource(
108             const DeviceVector& availableInputDevices, audio_source_t inputSource) const;
109     DeviceVector getDisabledDevicesForInputSource(
110             const DeviceVector& availableInputDevices, audio_source_t inputSource) const;
111 
112     bool isBtScoActive(DeviceVector& availableOutputDevices,
113                        const SwAudioOutputCollection &outputs) const;
114 
115     std::map<product_strategy_t, legacy_strategy> mLegacyStrategyMap;
116 };
117 } // namespace audio_policy
118 } // namespace android
119