• 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 <vector>
20 #include <map>
21 #include <unistd.h>
22 #include <sys/types.h>
23 
24 #include <system/audio.h>
25 #include <audiomanager/AudioManager.h>
26 #include <media/AudioProductStrategy.h>
27 #include <utils/Errors.h>
28 #include <utils/KeyedVector.h>
29 #include <utils/RefBase.h>
30 #include <utils/String8.h>
31 #include <policy.h>
32 #include <Volume.h>
33 #include "AudioPatch.h"
34 #include "EffectDescriptor.h"
35 
36 namespace android {
37 
38 class DeviceDescriptor;
39 class HwAudioOutputDescriptor;
40 class SwAudioOutputDescriptor;
41 
42 class ClientDescriptor: public RefBase
43 {
44 public:
45     ClientDescriptor(audio_port_handle_t portId, uid_t uid, audio_session_t sessionId,
46                      audio_attributes_t attributes, audio_config_base_t config,
47                      audio_port_handle_t preferredDeviceId,
48                      bool isPreferredDeviceForExclusiveUse = false) :
mPortId(portId)49         mPortId(portId), mUid(uid), mSessionId(sessionId), mAttributes(attributes),
50         mConfig(config), mPreferredDeviceId(preferredDeviceId), mActive(false),
51         mPreferredDeviceForExclusiveUse(isPreferredDeviceForExclusiveUse){}
52     ~ClientDescriptor() override = default;
53 
54     virtual void dump(String8 *dst, int spaces, int index) const;
55     virtual std::string toShortString() const;
56 
portId()57     audio_port_handle_t portId() const { return mPortId; }
uid()58     uid_t uid() const { return mUid; }
session()59     audio_session_t session() const { return mSessionId; };
attributes()60     audio_attributes_t attributes() const { return mAttributes; }
config()61     audio_config_base_t config() const { return mConfig; }
preferredDeviceId()62     audio_port_handle_t preferredDeviceId() const { return mPreferredDeviceId; };
setPreferredDeviceId(audio_port_handle_t preferredDeviceId)63     void setPreferredDeviceId(audio_port_handle_t preferredDeviceId) {
64         mPreferredDeviceId = preferredDeviceId;
65     }
isPreferredDeviceForExclusiveUse()66     bool isPreferredDeviceForExclusiveUse() const { return mPreferredDeviceForExclusiveUse; }
setActive(bool active)67     virtual void setActive(bool active) { mActive = active; }
active()68     bool active() const { return mActive; }
69     bool hasPreferredDevice(bool activeOnly = false) const {
70         return mPreferredDeviceId != AUDIO_PORT_HANDLE_NONE && (!activeOnly || mActive);
71     }
72 
73 private:
74     const audio_port_handle_t mPortId;  // unique Id for this client
75     const uid_t mUid;                     // client UID
76     const audio_session_t mSessionId;       // audio session ID
77     const audio_attributes_t mAttributes; // usage...
78     const audio_config_base_t mConfig;
79           audio_port_handle_t mPreferredDeviceId;  // selected input device port ID
80           bool mActive;
81           bool mPreferredDeviceForExclusiveUse = false;
82 };
83 
84 class TrackClientDescriptor: public ClientDescriptor
85 {
86 public:
TrackClientDescriptor(audio_port_handle_t portId,uid_t uid,audio_session_t sessionId,audio_attributes_t attributes,audio_config_base_t config,audio_port_handle_t preferredDeviceId,audio_stream_type_t stream,product_strategy_t strategy,VolumeSource volumeSource,audio_output_flags_t flags,bool isPreferredDeviceForExclusiveUse,std::vector<wp<SwAudioOutputDescriptor>> secondaryOutputs)87     TrackClientDescriptor(audio_port_handle_t portId, uid_t uid, audio_session_t sessionId,
88                           audio_attributes_t attributes, audio_config_base_t config,
89                           audio_port_handle_t preferredDeviceId, audio_stream_type_t stream,
90                           product_strategy_t strategy, VolumeSource volumeSource,
91                           audio_output_flags_t flags,
92                           bool isPreferredDeviceForExclusiveUse,
93                           std::vector<wp<SwAudioOutputDescriptor>> secondaryOutputs) :
94         ClientDescriptor(portId, uid, sessionId, attributes, config, preferredDeviceId,
95                          isPreferredDeviceForExclusiveUse),
96         mStream(stream), mStrategy(strategy), mVolumeSource(volumeSource), mFlags(flags),
97         mSecondaryOutputs(std::move(secondaryOutputs)) {}
98     ~TrackClientDescriptor() override = default;
99 
100     using ClientDescriptor::dump;
101     void dump(String8 *dst, int spaces, int index) const override;
102     std::string toShortString() const override;
103 
flags()104     audio_output_flags_t flags() const { return mFlags; }
stream()105     audio_stream_type_t stream() const { return mStream; }
strategy()106     product_strategy_t strategy() const { return mStrategy; }
getSecondaryOutputs()107     const std::vector<wp<SwAudioOutputDescriptor>>& getSecondaryOutputs() const {
108         return mSecondaryOutputs;
109     };
volumeSource()110     VolumeSource volumeSource() const { return mVolumeSource; }
111 
setActive(bool active)112     void setActive(bool active) override
113     {
114         int delta = active ? 1 : -1;
115         changeActivityCount(delta);
116     }
changeActivityCount(int delta)117     void changeActivityCount(int delta)
118     {
119         if (delta > 0) {
120             mActivityCount += delta;
121         } else {
122             LOG_ALWAYS_FATAL_IF(!mActivityCount, "%s(%s) invalid delta %d, inactive client",
123                                  __func__, toShortString().c_str(), delta);
124             LOG_ALWAYS_FATAL_IF(static_cast<int>(mActivityCount) < -delta,
125                                 "%s(%s) invalid delta %d, active client count %d",
126                                  __func__, toShortString().c_str(), delta, mActivityCount);
127             mActivityCount += delta;
128         }
129         ClientDescriptor::setActive(mActivityCount > 0);
130     }
getActivityCount()131     uint32_t getActivityCount() const { return mActivityCount; }
132 
133 private:
134     const audio_stream_type_t mStream;
135     const product_strategy_t mStrategy;
136     const VolumeSource mVolumeSource;
137     const audio_output_flags_t mFlags;
138     const std::vector<wp<SwAudioOutputDescriptor>> mSecondaryOutputs;
139 
140     /**
141      * required for duplicating thread, prevent from removing active client from an output
142      * involved in a duplication.
143      */
144     uint32_t mActivityCount = 0;
145 };
146 
147 class RecordClientDescriptor: public ClientDescriptor
148 {
149 public:
RecordClientDescriptor(audio_port_handle_t portId,audio_unique_id_t riid,uid_t uid,audio_session_t sessionId,audio_attributes_t attributes,audio_config_base_t config,audio_port_handle_t preferredDeviceId,audio_source_t source,audio_input_flags_t flags,bool isSoundTrigger)150     RecordClientDescriptor(audio_port_handle_t portId, audio_unique_id_t riid, uid_t uid,
151                         audio_session_t sessionId, audio_attributes_t attributes,
152                         audio_config_base_t config, audio_port_handle_t preferredDeviceId,
153                         audio_source_t source, audio_input_flags_t flags, bool isSoundTrigger) :
154         ClientDescriptor(portId, uid, sessionId, attributes, config, preferredDeviceId),
155         mRIId(riid), mSource(source), mFlags(flags), mIsSoundTrigger(isSoundTrigger),
156         mAppState(APP_STATE_IDLE) {}
157     ~RecordClientDescriptor() override = default;
158 
159     using ClientDescriptor::dump;
160     void dump(String8 *dst, int spaces, int index) const override;
161 
riid()162     audio_unique_id_t riid() const { return mRIId; }
source()163     audio_source_t source() const { return mSource; }
flags()164     audio_input_flags_t flags() const { return mFlags; }
isSoundTrigger()165     bool isSoundTrigger() const { return mIsSoundTrigger; }
isLowLevel()166     bool isLowLevel() const { return mRIId == RECORD_RIID_INVALID; }
setAppState(app_state_t appState)167     void setAppState(app_state_t appState) { mAppState = appState; }
appState()168     app_state_t appState() { return mAppState; }
isSilenced()169     bool isSilenced() const { return mAppState == APP_STATE_IDLE; }
170     void trackEffectEnabled(const sp<EffectDescriptor> &effect, bool enabled);
getEnabledEffects()171     EffectDescriptorCollection getEnabledEffects() const { return mEnabledEffects; }
172 
173 private:
174     const audio_unique_id_t mRIId;
175     const audio_source_t mSource;
176     const audio_input_flags_t mFlags;
177     const bool mIsSoundTrigger;
178           app_state_t mAppState;
179     EffectDescriptorCollection mEnabledEffects;
180 };
181 
182 class SourceClientDescriptor: public TrackClientDescriptor
183 {
184 public:
185     SourceClientDescriptor(audio_port_handle_t portId, uid_t uid, audio_attributes_t attributes,
186                            const sp<AudioPatch>& patchDesc, const sp<DeviceDescriptor>& srcDevice,
187                            audio_stream_type_t stream, product_strategy_t strategy,
188                            VolumeSource volumeSource);
189     ~SourceClientDescriptor() override = default;
190 
patchDesc()191     sp<AudioPatch> patchDesc() const { return mPatchDesc; }
srcDevice()192     sp<DeviceDescriptor> srcDevice() const { return mSrcDevice; };
swOutput()193     wp<SwAudioOutputDescriptor> swOutput() const { return mSwOutput; }
194     void setSwOutput(const sp<SwAudioOutputDescriptor>& swOutput);
hwOutput()195     wp<HwAudioOutputDescriptor> hwOutput() const { return mHwOutput; }
196     void setHwOutput(const sp<HwAudioOutputDescriptor>& hwOutput);
197 
198     using ClientDescriptor::dump;
199     void dump(String8 *dst, int spaces, int index) const override;
200 
201  private:
202     const sp<AudioPatch> mPatchDesc;
203     const sp<DeviceDescriptor> mSrcDevice;
204     wp<SwAudioOutputDescriptor> mSwOutput;
205     wp<HwAudioOutputDescriptor> mHwOutput;
206 };
207 
208 class SourceClientCollection :
209     public DefaultKeyedVector< audio_port_handle_t, sp<SourceClientDescriptor> >
210 {
211 public:
212     void dump(String8 *dst) const;
213 };
214 
215 typedef std::vector< sp<TrackClientDescriptor> > TrackClientVector;
216 typedef std::vector< sp<RecordClientDescriptor> > RecordClientVector;
217 
218 // A Map that associates a portId with a client (type T)
219 // which is either TrackClientDescriptor or RecordClientDescriptor.
220 
221 template<typename T>
222 class ClientMapHandler {
223 public:
224     virtual ~ClientMapHandler() = default;
225 
226     // Track client management
addClient(const sp<T> & client)227     virtual void addClient(const sp<T> &client) {
228         const audio_port_handle_t portId = client->portId();
229         LOG_ALWAYS_FATAL_IF(!mClients.emplace(portId, client).second,
230                 "%s(%d): attempting to add client that already exists", __func__, portId);
231     }
getClient(audio_port_handle_t portId)232     sp<T> getClient(audio_port_handle_t portId) const {
233         auto it = mClients.find(portId);
234         if (it == mClients.end()) return nullptr;
235         return it->second;
236     }
removeClient(audio_port_handle_t portId)237     virtual void removeClient(audio_port_handle_t portId) {
238         auto it = mClients.find(portId);
239         LOG_ALWAYS_FATAL_IF(it == mClients.end(),
240                 "%s(%d): client does not exist", __func__, portId);
241         LOG_ALWAYS_FATAL_IF(it->second->active(),
242                 "%s(%d): removing client still active!", __func__, portId);
243         (void)mClients.erase(it);
244     }
getClientCount()245     size_t getClientCount() const {
246         return mClients.size();
247     }
dump(String8 * dst)248     virtual void dump(String8 *dst) const {
249         size_t index = 0;
250         for (const auto& client: getClientIterable()) {
251             client->dump(dst, 2, index++);
252         }
253     }
254 
255     // helper types
256     using ClientMap = std::map<audio_port_handle_t, sp<T>>;
257     using ClientMapIterator = typename ClientMap::const_iterator;  // ClientMap is const qualified
258     class ClientIterable {
259     public:
ClientIterable(const ClientMapHandler<T> & ref)260         explicit ClientIterable(const ClientMapHandler<T> &ref) : mClientMapHandler(ref) { }
261 
262         class iterator {
263         public:
264             // traits
265             using iterator_category = std::forward_iterator_tag;
266             using value_type = sp<T>;
267             using difference_type = ptrdiff_t;
268             using pointer = const sp<T>*;    // Note: const
269             using reference = const sp<T>&;  // Note: const
270 
271             // implementation
iterator(const ClientMapIterator & it)272             explicit iterator(const ClientMapIterator &it) : mIt(it) { }
273             iterator& operator++()    /* prefix */     { ++mIt; return *this; }
274             reference operator* () const               { return mIt->second; }
275             reference operator->() const               { return mIt->second; } // as if sp<>
276             difference_type operator-(const iterator& rhs) {return mIt - rhs.mIt; }
277             bool operator==(const iterator& rhs) const { return mIt == rhs.mIt; }
278             bool operator!=(const iterator& rhs) const { return mIt != rhs.mIt; }
279         private:
280             ClientMapIterator mIt;
281         };
282 
begin()283         iterator begin() const { return iterator{mClientMapHandler.mClients.begin()}; }
end()284         iterator end() const { return iterator{mClientMapHandler.mClients.end()}; }
285 
286     private:
287         const ClientMapHandler<T>& mClientMapHandler; // iterating does not modify map.
288     };
289 
290     // return an iterable object that can be used in a range-based-for to enumerate clients.
291     // this iterable does not allow modification, it should be used as a temporary.
getClientIterable()292     ClientIterable getClientIterable() const {
293         return ClientIterable{*this};
294     }
295 
296 private:
297     // ClientMap maps a portId to a client descriptor (both uniquely identify each other).
298     ClientMap mClients;
299 };
300 
301 } // namespace android
302