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