bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ANDROID_AUDIOPOLICYEFFECTS_H |
| 18 | #define ANDROID_AUDIOPOLICYEFFECTS_H |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <cutils/misc.h> |
| 24 | #include <media/AudioEffect.h> |
| 25 | #include <system/audio.h> |
| 26 | #include <hardware/audio_effect.h> |
| 27 | #include <utils/Vector.h> |
| 28 | #include <utils/SortedVector.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | // AudioPolicyEffects class |
| 35 | // This class will manage all effects attached to input and output streams in |
| 36 | // AudioPolicyService as configured in audio_effects.conf. |
| 37 | class AudioPolicyEffects : public RefBase |
| 38 | { |
| 39 | |
| 40 | public: |
| 41 | |
| 42 | // The constructor will parse audio_effects.conf |
| 43 | // First it will look whether vendor specific file exists, |
| 44 | // otherwise it will parse the system default file. |
| 45 | AudioPolicyEffects(); |
| 46 | virtual ~AudioPolicyEffects(); |
| 47 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 48 | // NOTE: methods on AudioPolicyEffects should never be called with the AudioPolicyService |
| 49 | // main mutex (mLock) held as they will indirectly call back into AudioPolicyService when |
| 50 | // managing audio effects. |
| 51 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 52 | // Return a list of effect descriptors for default input effects |
| 53 | // associated with audioSession |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 54 | status_t queryDefaultInputEffects(int audioSession, |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 55 | effect_descriptor_t *descriptors, |
| 56 | uint32_t *count); |
| 57 | |
| 58 | // Add all input effects associated with this input |
| 59 | // Effects are attached depending on the audio_source_t |
| 60 | status_t addInputEffects(audio_io_handle_t input, |
| 61 | audio_source_t inputSource, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 62 | int audioSession); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 63 | |
| 64 | // Add all input effects associated to this input |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 65 | status_t releaseInputEffects(audio_io_handle_t input); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 66 | |
| 67 | |
| 68 | // Return a list of effect descriptors for default output effects |
| 69 | // associated with audioSession |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 70 | status_t queryDefaultOutputSessionEffects(int audioSession, |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 71 | effect_descriptor_t *descriptors, |
| 72 | uint32_t *count); |
| 73 | |
| 74 | // Add all output effects associated to this output |
| 75 | // Effects are attached depending on the audio_stream_type_t |
| 76 | status_t addOutputSessionEffects(audio_io_handle_t output, |
| 77 | audio_stream_type_t stream, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 78 | int audioSession); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 79 | |
| 80 | // release all output effects associated with this output stream and audiosession |
| 81 | status_t releaseOutputSessionEffects(audio_io_handle_t output, |
| 82 | audio_stream_type_t stream, |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 83 | int audioSession); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | |
| 87 | // class to store the description of an effects and its parameters |
| 88 | // as defined in audio_effects.conf |
| 89 | class EffectDesc { |
| 90 | public: |
| 91 | EffectDesc(const char *name, const effect_uuid_t& uuid) : |
| 92 | mName(strdup(name)), |
| 93 | mUuid(uuid) { } |
| 94 | EffectDesc(const EffectDesc& orig) : |
| 95 | mName(strdup(orig.mName)), |
| 96 | mUuid(orig.mUuid) { |
| 97 | // deep copy mParams |
| 98 | for (size_t k = 0; k < orig.mParams.size(); k++) { |
| 99 | effect_param_t *origParam = orig.mParams[k]; |
| 100 | // psize and vsize are rounded up to an int boundary for allocation |
| 101 | size_t origSize = sizeof(effect_param_t) + |
| 102 | ((origParam->psize + 3) & ~3) + |
| 103 | ((origParam->vsize + 3) & ~3); |
| 104 | effect_param_t *dupParam = (effect_param_t *) malloc(origSize); |
| 105 | memcpy(dupParam, origParam, origSize); |
| 106 | // This works because the param buffer allocation is also done by |
| 107 | // multiples of 4 bytes originally. In theory we should memcpy only |
| 108 | // the actual param size, that is without rounding vsize. |
| 109 | mParams.add(dupParam); |
| 110 | } |
| 111 | } |
| 112 | /*virtual*/ ~EffectDesc() { |
| 113 | free(mName); |
| 114 | for (size_t k = 0; k < mParams.size(); k++) { |
| 115 | free(mParams[k]); |
| 116 | } |
| 117 | } |
| 118 | char *mName; |
| 119 | effect_uuid_t mUuid; |
| 120 | Vector <effect_param_t *> mParams; |
| 121 | }; |
| 122 | |
| 123 | // class to store voctor of EffectDesc |
| 124 | class EffectDescVector { |
| 125 | public: |
| 126 | EffectDescVector() {} |
| 127 | /*virtual*/ ~EffectDescVector() { |
| 128 | for (size_t j = 0; j < mEffects.size(); j++) { |
| 129 | delete mEffects[j]; |
| 130 | } |
| 131 | } |
| 132 | Vector <EffectDesc *> mEffects; |
| 133 | }; |
| 134 | |
| 135 | // class to store voctor of AudioEffects |
| 136 | class EffectVector { |
| 137 | public: |
bryant_liu | 890a563 | 2014-08-20 18:06:13 +0800 | [diff] [blame] | 138 | EffectVector(int session) : mSessionId(session), mRefCount(0) {} |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 139 | /*virtual*/ ~EffectVector() {} |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 140 | |
| 141 | // Enable or disable all effects in effect vector |
| 142 | void setProcessorEnabled(bool enabled); |
| 143 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 144 | const int mSessionId; |
bryant_liu | 890a563 | 2014-08-20 18:06:13 +0800 | [diff] [blame] | 145 | // AudioPolicyManager keeps mLock, no need for lock on reference count here |
| 146 | int mRefCount; |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 147 | Vector< sp<AudioEffect> >mEffects; |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1]; |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 152 | static audio_source_t inputSourceNameToEnum(const char *name); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 153 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 154 | static const char *kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1]; //+1 required as streams start from -1 |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 155 | audio_stream_type_t streamNameToEnum(const char *name); |
| 156 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 157 | // Parse audio_effects.conf |
| 158 | status_t loadAudioEffectConfig(const char *path); |
| 159 | |
| 160 | // Load all effects descriptors in configuration file |
| 161 | status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects); |
| 162 | EffectDesc *loadEffect(cnode *root); |
| 163 | |
| 164 | // Load all automatic effect configurations |
| 165 | status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects); |
| 166 | status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects); |
| 167 | EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects); |
| 168 | |
| 169 | // Load all automatic effect parameters |
| 170 | void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params); |
| 171 | effect_param_t *loadEffectParameter(cnode *root); |
| 172 | size_t readParamValue(cnode *node, |
Eric Laurent | 138ed17 | 2016-02-10 10:40:44 -0800 | [diff] [blame] | 173 | char **param, |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 174 | size_t *curSize, |
| 175 | size_t *totSize); |
Eric Laurent | 138ed17 | 2016-02-10 10:40:44 -0800 | [diff] [blame] | 176 | size_t growParamSize(char **param, |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 177 | size_t size, |
| 178 | size_t *curSize, |
| 179 | size_t *totSize); |
| 180 | |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 181 | // protects access to mInputSources, mInputs, mOutputStreams, mOutputSessions |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 182 | Mutex mLock; |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 183 | // Automatic input effects are configured per audio_source_t |
| 184 | KeyedVector< audio_source_t, EffectDescVector* > mInputSources; |
| 185 | // Automatic input effects are unique for audio_io_handle_t |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 186 | KeyedVector< audio_io_handle_t, EffectVector* > mInputs; |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 187 | |
| 188 | // Automatic output effects are organized per audio_stream_type_t |
| 189 | KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams; |
| 190 | // Automatic output effects are unique for audiosession ID |
Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame^] | 191 | KeyedVector< int32_t, EffectVector* > mOutputSessions; |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | }; // namespace android |
| 195 | |
| 196 | #endif // ANDROID_AUDIOPOLICYEFFECTS_H |