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
20#include <AudioPolicyManagerInterface.h>
21#include <AudioPolicyPluginInterface.h>
22#include "Collection.h"
23
24namespace android
25{
26class AudioPolicyManagerObserver;
27
28namespace audio_policy
29{
30
31class ParameterManagerWrapper;
32class VolumeProfile;
33
34class Engine
35{
36public:
37    Engine();
38    virtual ~Engine();
39
40    template <class RequestedInterface>
41    RequestedInterface *queryInterface();
42
43private:
44    /// Interface members
45    class ManagerInterfaceImpl : public AudioPolicyManagerInterface
46    {
47    public:
48        ManagerInterfaceImpl(Engine *policyEngine)
49            : mPolicyEngine(policyEngine) {}
50
51        virtual android::status_t initCheck()
52        {
53            return mPolicyEngine->initCheck();
54        }
55        virtual void setObserver(AudioPolicyManagerObserver *observer)
56        {
57            mPolicyEngine->setObserver(observer);
58        }
59        virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource) const
60        {
61            return mPolicyEngine->getPropertyForKey<audio_devices_t, audio_source_t>(inputSource);
62        }
63        virtual audio_devices_t getDeviceForStrategy(routing_strategy stategy) const;
64        virtual routing_strategy getStrategyForStream(audio_stream_type_t stream)
65        {
66            return mPolicyEngine->getPropertyForKey<routing_strategy, audio_stream_type_t>(stream);
67        }
68        virtual routing_strategy getStrategyForUsage(audio_usage_t usage);
69        virtual status_t setPhoneState(audio_mode_t mode)
70        {
71            return mPolicyEngine->setPhoneState(mode);
72        }
73        virtual audio_mode_t getPhoneState() const
74        {
75            return mPolicyEngine->getPhoneState();
76        }
77        virtual status_t setForceUse(audio_policy_force_use_t usage,
78                                              audio_policy_forced_cfg_t config)
79        {
80            return mPolicyEngine->setForceUse(usage, config);
81        }
82        virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const
83        {
84            return mPolicyEngine->getForceUse(usage);
85        }
86        virtual android::status_t setDeviceConnectionState(const sp<DeviceDescriptor> devDesc,
87                                                           audio_policy_dev_state_t state)
88        {
89            return mPolicyEngine->setDeviceConnectionState(devDesc, state);
90        }
91
92    private:
93        Engine *mPolicyEngine;
94    } mManagerInterface;
95
96    class PluginInterfaceImpl : public AudioPolicyPluginInterface
97    {
98    public:
99        PluginInterfaceImpl(Engine *policyEngine)
100            : mPolicyEngine(policyEngine) {}
101
102        virtual status_t addStrategy(const std::string &name, routing_strategy strategy)
103        {
104            return mPolicyEngine->add<routing_strategy>(name, strategy);
105        }
106        virtual status_t addStream(const std::string &name, audio_stream_type_t stream)
107        {
108            return mPolicyEngine->add<audio_stream_type_t>(name, stream);
109        }
110        virtual status_t addUsage(const std::string &name, audio_usage_t usage)
111        {
112            return mPolicyEngine->add<audio_usage_t>(name, usage);
113        }
114        virtual status_t addInputSource(const std::string &name, audio_source_t source)
115        {
116            return mPolicyEngine->add<audio_source_t>(name, source);
117        }
118        virtual bool setDeviceForStrategy(const routing_strategy &strategy, audio_devices_t devices)
119        {
120            return mPolicyEngine->setPropertyForKey<audio_devices_t, routing_strategy>(devices,
121                                                                                       strategy);
122        }
123        virtual bool setStrategyForStream(const audio_stream_type_t &stream,
124                                          routing_strategy strategy)
125        {
126            return mPolicyEngine->setPropertyForKey<routing_strategy, audio_stream_type_t>(strategy,
127                                                                                           stream);
128        }
129        virtual bool setVolumeProfileForStream(const audio_stream_type_t &stream,
130                                               const audio_stream_type_t &volumeProfile);
131
132        virtual bool setStrategyForUsage(const audio_usage_t &usage, routing_strategy strategy)
133        {
134            return mPolicyEngine->setPropertyForKey<routing_strategy, audio_usage_t>(strategy,
135                                                                                     usage);
136        }
137        virtual bool setDeviceForInputSource(const audio_source_t &inputSource,
138                                             audio_devices_t device)
139        {
140            return mPolicyEngine->setPropertyForKey<audio_devices_t, audio_source_t>(device,
141                                                                                     inputSource);
142        }
143
144    private:
145        Engine *mPolicyEngine;
146    } mPluginInterface;
147
148private:
149    /* Copy facilities are put private to disable copy. */
150    Engine(const Engine &object);
151    Engine &operator=(const Engine &object);
152
153    void setObserver(AudioPolicyManagerObserver *observer);
154
155    bool setVolumeProfileForStream(const audio_stream_type_t &stream,
156                                   device_category deviceCategory,
157                                   const VolumeCurvePoints &points);
158
159    status_t initCheck();
160    status_t setPhoneState(audio_mode_t mode);
161    audio_mode_t getPhoneState() const;
162    status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
163    audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) const;
164    status_t setDeviceConnectionState(const sp<DeviceDescriptor> devDesc,
165                                      audio_policy_dev_state_t state);
166    StrategyCollection mStrategyCollection; /**< Strategies indexed by their enum id. */
167    StreamCollection mStreamCollection; /**< Streams indexed by their enum id.  */
168    UsageCollection mUsageCollection; /**< Usages indexed by their enum id. */
169    InputSourceCollection mInputSourceCollection; /**< Input sources indexed by their enum id. */
170
171    template <typename Key>
172    status_t add(const std::string &name, const Key &key);
173
174    template <typename Key>
175    Element<Key> *getFromCollection(const Key &key) const;
176
177    template <typename Key>
178    const Collection<Key> &getCollection() const;
179
180    template <typename Key>
181    Collection<Key> &getCollection();
182
183    template <typename Property, typename Key>
184    Property getPropertyForKey(Key key) const;
185
186    template <typename Property, typename Key>
187    bool setPropertyForKey(const Property &property, const Key &key);
188
189    /**
190     * Policy Parameter Manager hidden through a wrapper.
191     */
192    ParameterManagerWrapper *mPolicyParameterMgr;
193
194    AudioPolicyManagerObserver *mApmObserver;
195};
196
197}; // namespace audio_policy
198
199}; // namespace android
200
201