AudioPolicyEffects.h revision 890a5637b3e3eb197fd2c7f5deec816c7feb13fc
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 30namespace 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. 37class AudioPolicyEffects : public RefBase 38{ 39 40public: 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 48 // Return a list of effect descriptors for default input effects 49 // associated with audioSession 50 status_t queryDefaultInputEffects(int audioSession, 51 effect_descriptor_t *descriptors, 52 uint32_t *count); 53 54 // Add all input effects associated with this input 55 // Effects are attached depending on the audio_source_t 56 status_t addInputEffects(audio_io_handle_t input, 57 audio_source_t inputSource, 58 int audioSession); 59 60 // Add all input effects associated to this input 61 status_t releaseInputEffects(audio_io_handle_t input); 62 63 64 // Return a list of effect descriptors for default output effects 65 // associated with audioSession 66 status_t queryDefaultOutputSessionEffects(int audioSession, 67 effect_descriptor_t *descriptors, 68 uint32_t *count); 69 70 // Add all output effects associated to this output 71 // Effects are attached depending on the audio_stream_type_t 72 status_t addOutputSessionEffects(audio_io_handle_t output, 73 audio_stream_type_t stream, 74 int audioSession); 75 76 // release all output effects associated with this output stream and audiosession 77 status_t releaseOutputSessionEffects(audio_io_handle_t output, 78 audio_stream_type_t stream, 79 int audioSession); 80 81private: 82 83 // class to store the description of an effects and its parameters 84 // as defined in audio_effects.conf 85 class EffectDesc { 86 public: 87 EffectDesc(const char *name, const effect_uuid_t& uuid) : 88 mName(strdup(name)), 89 mUuid(uuid) { } 90 EffectDesc(const EffectDesc& orig) : 91 mName(strdup(orig.mName)), 92 mUuid(orig.mUuid) { 93 // deep copy mParams 94 for (size_t k = 0; k < orig.mParams.size(); k++) { 95 effect_param_t *origParam = orig.mParams[k]; 96 // psize and vsize are rounded up to an int boundary for allocation 97 size_t origSize = sizeof(effect_param_t) + 98 ((origParam->psize + 3) & ~3) + 99 ((origParam->vsize + 3) & ~3); 100 effect_param_t *dupParam = (effect_param_t *) malloc(origSize); 101 memcpy(dupParam, origParam, origSize); 102 // This works because the param buffer allocation is also done by 103 // multiples of 4 bytes originally. In theory we should memcpy only 104 // the actual param size, that is without rounding vsize. 105 mParams.add(dupParam); 106 } 107 } 108 /*virtual*/ ~EffectDesc() { 109 free(mName); 110 for (size_t k = 0; k < mParams.size(); k++) { 111 free(mParams[k]); 112 } 113 } 114 char *mName; 115 effect_uuid_t mUuid; 116 Vector <effect_param_t *> mParams; 117 }; 118 119 // class to store voctor of EffectDesc 120 class EffectDescVector { 121 public: 122 EffectDescVector() {} 123 /*virtual*/ ~EffectDescVector() { 124 for (size_t j = 0; j < mEffects.size(); j++) { 125 delete mEffects[j]; 126 } 127 } 128 Vector <EffectDesc *> mEffects; 129 }; 130 131 // class to store voctor of AudioEffects 132 class EffectVector { 133 public: 134 EffectVector(int session) : mSessionId(session), mRefCount(0) {} 135 /*virtual*/ ~EffectVector() {} 136 const int mSessionId; 137 // AudioPolicyManager keeps mLock, no need for lock on reference count here 138 int mRefCount; 139 Vector< sp<AudioEffect> >mEffects; 140 }; 141 142 143 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1]; 144 audio_source_t inputSourceNameToEnum(const char *name); 145 146 static const char *kStreamNames[AUDIO_STREAM_CNT+1]; //+1 required as streams start from -1 147 audio_stream_type_t streamNameToEnum(const char *name); 148 149 // Enable or disable all effects in effect vector 150 void setProcessorEnabled(const EffectVector *effectVector, bool enabled); 151 152 // Parse audio_effects.conf 153 status_t loadAudioEffectConfig(const char *path); 154 155 // Load all effects descriptors in configuration file 156 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects); 157 EffectDesc *loadEffect(cnode *root); 158 159 // Load all automatic effect configurations 160 status_t loadInputEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects); 161 status_t loadStreamEffectConfigurations(cnode *root, const Vector <EffectDesc *>& effects); 162 EffectDescVector *loadEffectConfig(cnode *root, const Vector <EffectDesc *>& effects); 163 164 // Load all automatic effect parameters 165 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params); 166 effect_param_t *loadEffectParameter(cnode *root); 167 size_t readParamValue(cnode *node, 168 char *param, 169 size_t *curSize, 170 size_t *totSize); 171 size_t growParamSize(char *param, 172 size_t size, 173 size_t *curSize, 174 size_t *totSize); 175 176 // Automatic input effects are configured per audio_source_t 177 KeyedVector< audio_source_t, EffectDescVector* > mInputSources; 178 // Automatic input effects are unique for audio_io_handle_t 179 KeyedVector< audio_io_handle_t, EffectVector* > mInputs; 180 181 // Automatic output effects are organized per audio_stream_type_t 182 KeyedVector< audio_stream_type_t, EffectDescVector* > mOutputStreams; 183 // Automatic output effects are unique for audiosession ID 184 KeyedVector< int32_t, EffectVector* > mOutputSessions; 185}; 186 187}; // namespace android 188 189#endif // ANDROID_AUDIOPOLICYEFFECTS_H 190