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 <policy.h> 20 #include <system/audio_effect.h> 21 #include <utils/KeyedVector.h> 22 #include <utils/RefBase.h> 23 #include <utils/Errors.h> 24 #include <utils/String8.h> 25 26 namespace android { 27 28 class AudioInputCollection; 29 class AudioInputDescriptor; 30 class AudioPolicyClientInterface; 31 32 class EffectDescriptor : public RefBase 33 { 34 public: EffectDescriptor(const effect_descriptor_t * desc,bool isMusicEffect,int id,audio_io_handle_t io,audio_session_t session)35 EffectDescriptor(const effect_descriptor_t* desc, bool isMusicEffect, int id, 36 audio_io_handle_t io, audio_session_t session) 37 : mId(id), 38 mIo(io), 39 mIsOrphan(io == AUDIO_IO_HANDLE_NONE), 40 mSession(session), 41 mEnabled(false), 42 mSuspended(false), 43 mIsMusicEffect(isMusicEffect) { 44 memcpy (&mDesc, desc, sizeof(effect_descriptor_t)); 45 } 46 47 void dump(String8 *dst, int spaces = 0) const; 48 49 int mId; // effect unique ID 50 audio_io_handle_t mIo; // io the effect is attached to 51 bool mIsOrphan = false; // on creation, effect is not yet attached but not yet orphan 52 bool mEnabledWhenMoved = false; // Backup enabled state before being moved 53 audio_session_t mSession; // audio session the effect is on 54 effect_descriptor_t mDesc; // effect descriptor 55 bool mEnabled; // enabled state: CPU load being used or not 56 bool mSuspended; // enabled but suspended by concurent capture policy 57 isMusicEffect()58 bool isMusicEffect() const { return mIsMusicEffect; } 59 60 private: 61 bool mIsMusicEffect; 62 }; 63 64 class EffectDescriptorCollection : public KeyedVector<int, sp<EffectDescriptor> > 65 { 66 public: 67 EffectDescriptorCollection(); 68 69 status_t registerEffect(const effect_descriptor_t *desc, audio_io_handle_t io, 70 int session, int id, bool isMusicEffect); 71 status_t unregisterEffect(int id); 72 sp<EffectDescriptor> getEffect(int id) const; 73 EffectDescriptorCollection getEffectsForIo(audio_io_handle_t io) const; 74 status_t setEffectEnabled(int id, bool enabled); 75 bool isEffectEnabled(int id) const; 76 uint32_t getMaxEffectsCpuLoad() const; 77 uint32_t getMaxEffectsMemory() const; 78 bool isNonOffloadableEffectEnabled() const; 79 80 void moveEffects(audio_session_t session, 81 audio_io_handle_t srcOutput, 82 audio_io_handle_t dstOutput, 83 AudioPolicyClientInterface *clientInterface); 84 void moveEffects(const std::vector<int>& ids, audio_io_handle_t dstOutput); 85 void moveEffects(audio_session_t sessionId, audio_io_handle_t srcIo, audio_io_handle_t dstIo, 86 const AudioInputCollection *inputs, AudioPolicyClientInterface *clientInterface); 87 void moveEffectsForIo(audio_session_t sessionId, audio_io_handle_t dstIo, 88 const AudioInputCollection *inputs, AudioPolicyClientInterface *mClientInterface); 89 void putOrphanEffects(audio_session_t sessionId, audio_io_handle_t srcIo, 90 const AudioInputCollection *inputs, AudioPolicyClientInterface *clientInterface); 91 void putOrphanEffectsForIo(audio_io_handle_t srcIo); 92 93 /** 94 * @brief Checks if an effect session was already attached to an io handle and return it if 95 * found. Check only for a given effect type if effectType is not null or for any effect 96 * otherwise. 97 * @param sessionId to consider. 98 * @param effectType to consider. 99 * @return ioHandle if found, AUDIO_IO_HANDLE_NONE otherwise. 100 */ 101 audio_io_handle_t getIoForSession(audio_session_t sessionId, 102 const effect_uuid_t *effectType = nullptr) const; 103 bool hasOrphansForSession(audio_session_t sessionId) const; 104 EffectDescriptorCollection getOrphanEffectsForSession(audio_session_t sessionId) const; 105 void dump(String8 *dst, int spaces = 0, bool verbose = true) const; 106 107 /** 108 * @brief Checks if there is at least one orphan effect with given sessionId and effect type 109 * uuid. 110 * @param sessionId Session ID. 111 * @param effectType Effect type UUID, the implementation will be same as hasOrphansForSession 112 * if null. 113 * @return True if there is an orphan effect for given sessionId and type UUID, false otherwise. 114 */ 115 bool hasOrphanEffectsForSessionAndType(audio_session_t sessionId, 116 const effect_uuid_t* effectType) const; 117 118 private: 119 status_t setEffectEnabled(const sp<EffectDescriptor> &effectDesc, bool enabled); 120 121 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects (in MIPS) 122 uint32_t mTotalEffectsMemory; // current memory used by effects (in KB) 123 uint32_t mTotalEffectsMemoryMaxUsed; // maximum memory used by effects (in KB) 124 125 /** 126 * Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units 127 */ 128 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000; 129 /** 130 * Maximum memory allocated to audio effects in KB 131 */ 132 static const uint32_t MAX_EFFECTS_MEMORY = 512; 133 }; 134 135 } // namespace android 136