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 struct audio_port_config &config, 187 const sp<DeviceDescriptor>& srcDevice, 188 audio_stream_type_t stream, product_strategy_t strategy, 189 VolumeSource volumeSource); 190 191 ~SourceClientDescriptor() override = default; 192 getPatchHandle()193 audio_patch_handle_t getPatchHandle() const { return mPatchHandle; } setPatchHandle(audio_patch_handle_t patchHandle)194 void setPatchHandle(audio_patch_handle_t patchHandle) { mPatchHandle = patchHandle; } 195 srcDevice()196 sp<DeviceDescriptor> srcDevice() const { return mSrcDevice; } swOutput()197 wp<SwAudioOutputDescriptor> swOutput() const { return mSwOutput; } 198 void setSwOutput(const sp<SwAudioOutputDescriptor>& swOutput); hwOutput()199 wp<HwAudioOutputDescriptor> hwOutput() const { return mHwOutput; } 200 void setHwOutput(const sp<HwAudioOutputDescriptor>& hwOutput); 201 202 using ClientDescriptor::dump; 203 void dump(String8 *dst, int spaces, int index) const override; 204 205 private: 206 audio_patch_handle_t mPatchHandle = AUDIO_PATCH_HANDLE_NONE; 207 const sp<DeviceDescriptor> mSrcDevice; 208 wp<SwAudioOutputDescriptor> mSwOutput; 209 wp<HwAudioOutputDescriptor> mHwOutput; 210 }; 211 212 class SourceClientCollection : 213 public DefaultKeyedVector< audio_port_handle_t, sp<SourceClientDescriptor> > 214 { 215 public: 216 void dump(String8 *dst) const; 217 }; 218 219 typedef std::vector< sp<TrackClientDescriptor> > TrackClientVector; 220 typedef std::vector< sp<RecordClientDescriptor> > RecordClientVector; 221 222 // A Map that associates a portId with a client (type T) 223 // which is either TrackClientDescriptor or RecordClientDescriptor. 224 225 template<typename T> 226 class ClientMapHandler { 227 public: 228 virtual ~ClientMapHandler() = default; 229 230 // Track client management addClient(const sp<T> & client)231 virtual void addClient(const sp<T> &client) { 232 const audio_port_handle_t portId = client->portId(); 233 LOG_ALWAYS_FATAL_IF(!mClients.emplace(portId, client).second, 234 "%s(%d): attempting to add client that already exists", __func__, portId); 235 } getClient(audio_port_handle_t portId)236 sp<T> getClient(audio_port_handle_t portId) const { 237 auto it = mClients.find(portId); 238 if (it == mClients.end()) return nullptr; 239 return it->second; 240 } removeClient(audio_port_handle_t portId)241 virtual void removeClient(audio_port_handle_t portId) { 242 auto it = mClients.find(portId); 243 LOG_ALWAYS_FATAL_IF(it == mClients.end(), 244 "%s(%d): client does not exist", __func__, portId); 245 LOG_ALWAYS_FATAL_IF(it->second->active(), 246 "%s(%d): removing client still active!", __func__, portId); 247 (void)mClients.erase(it); 248 } getClientCount()249 size_t getClientCount() const { 250 return mClients.size(); 251 } dump(String8 * dst)252 virtual void dump(String8 *dst) const { 253 size_t index = 0; 254 for (const auto& client: getClientIterable()) { 255 client->dump(dst, 2, index++); 256 } 257 } 258 259 // helper types 260 using ClientMap = std::map<audio_port_handle_t, sp<T>>; 261 using ClientMapIterator = typename ClientMap::const_iterator; // ClientMap is const qualified 262 class ClientIterable { 263 public: ClientIterable(const ClientMapHandler<T> & ref)264 explicit ClientIterable(const ClientMapHandler<T> &ref) : mClientMapHandler(ref) { } 265 266 class iterator { 267 public: 268 // traits 269 using iterator_category = std::forward_iterator_tag; 270 using value_type = sp<T>; 271 using difference_type = ptrdiff_t; 272 using pointer = const sp<T>*; // Note: const 273 using reference = const sp<T>&; // Note: const 274 275 // implementation iterator(const ClientMapIterator & it)276 explicit iterator(const ClientMapIterator &it) : mIt(it) { } 277 iterator& operator++() /* prefix */ { ++mIt; return *this; } 278 reference operator* () const { return mIt->second; } 279 reference operator->() const { return mIt->second; } // as if sp<> 280 difference_type operator-(const iterator& rhs) {return mIt - rhs.mIt; } 281 bool operator==(const iterator& rhs) const { return mIt == rhs.mIt; } 282 bool operator!=(const iterator& rhs) const { return mIt != rhs.mIt; } 283 private: 284 ClientMapIterator mIt; 285 }; 286 begin()287 iterator begin() const { return iterator{mClientMapHandler.mClients.begin()}; } end()288 iterator end() const { return iterator{mClientMapHandler.mClients.end()}; } 289 290 private: 291 const ClientMapHandler<T>& mClientMapHandler; // iterating does not modify map. 292 }; 293 294 // return an iterable object that can be used in a range-based-for to enumerate clients. 295 // this iterable does not allow modification, it should be used as a temporary. getClientIterable()296 ClientIterable getClientIterable() const { 297 return ClientIterable{*this}; 298 } 299 300 private: 301 // ClientMap maps a portId to a client descriptor (both uniquely identify each other). 302 ClientMap mClients; 303 }; 304 305 } // namespace android 306