AudioPolicyService.h revision 58e5aa34f01d663654d8bafad65db1dda42161ff
1/* 2 * Copyright (C) 2009 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_AUDIOPOLICYSERVICE_H 18#define ANDROID_AUDIOPOLICYSERVICE_H 19 20#include <cutils/misc.h> 21#include <cutils/config_utils.h> 22#include <utils/String8.h> 23#include <utils/Vector.h> 24#include <utils/SortedVector.h> 25#include <binder/BinderService.h> 26#include <system/audio.h> 27#include <system/audio_policy.h> 28#include <hardware/audio_policy.h> 29#include <media/IAudioPolicyService.h> 30#include <media/ToneGenerator.h> 31#include <media/AudioEffect.h> 32 33namespace android { 34 35// ---------------------------------------------------------------------------- 36 37class AudioPolicyService : 38 public BinderService<AudioPolicyService>, 39 public BnAudioPolicyService, 40// public AudioPolicyClientInterface, 41 public IBinder::DeathRecipient 42{ 43 friend class BinderService<AudioPolicyService>; 44 45public: 46 // for BinderService 47 static const char *getServiceName() { return "media.audio_policy"; } 48 49 virtual status_t dump(int fd, const Vector<String16>& args); 50 51 // 52 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) 53 // 54 55 virtual status_t setDeviceConnectionState(audio_devices_t device, 56 audio_policy_dev_state_t state, 57 const char *device_address); 58 virtual audio_policy_dev_state_t getDeviceConnectionState( 59 audio_devices_t device, 60 const char *device_address); 61 virtual status_t setPhoneState(audio_mode_t state); 62 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 63 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); 64 virtual audio_io_handle_t getOutput(audio_stream_type_t stream, 65 uint32_t samplingRate = 0, 66 audio_format_t format = AUDIO_FORMAT_DEFAULT, 67 audio_channel_mask_t channelMask = 0, 68 audio_output_flags_t flags = 69 AUDIO_OUTPUT_FLAG_NONE); 70 virtual status_t startOutput(audio_io_handle_t output, 71 audio_stream_type_t stream, 72 int session = 0); 73 virtual status_t stopOutput(audio_io_handle_t output, 74 audio_stream_type_t stream, 75 int session = 0); 76 virtual void releaseOutput(audio_io_handle_t output); 77 virtual audio_io_handle_t getInput(audio_source_t inputSource, 78 uint32_t samplingRate = 0, 79 audio_format_t format = AUDIO_FORMAT_DEFAULT, 80 audio_channel_mask_t channelMask = 0, 81 int audioSession = 0); 82 virtual status_t startInput(audio_io_handle_t input); 83 virtual status_t stopInput(audio_io_handle_t input); 84 virtual void releaseInput(audio_io_handle_t input); 85 virtual status_t initStreamVolume(audio_stream_type_t stream, 86 int indexMin, 87 int indexMax); 88 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, 89 int index, 90 audio_devices_t device); 91 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, 92 int *index, 93 audio_devices_t device); 94 95 virtual uint32_t getStrategyForStream(audio_stream_type_t stream); 96 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); 97 98 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); 99 virtual status_t registerEffect(const effect_descriptor_t *desc, 100 audio_io_handle_t io, 101 uint32_t strategy, 102 int session, 103 int id); 104 virtual status_t unregisterEffect(int id); 105 virtual status_t setEffectEnabled(int id, bool enabled); 106 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 107 108 virtual status_t queryDefaultPreProcessing(int audioSession, 109 effect_descriptor_t *descriptors, 110 uint32_t *count); 111 virtual status_t onTransact( 112 uint32_t code, 113 const Parcel& data, 114 Parcel* reply, 115 uint32_t flags); 116 117 // IBinder::DeathRecipient 118 virtual void binderDied(const wp<IBinder>& who); 119 120 // 121 // Helpers for the struct audio_policy_service_ops implementation. 122 // This is used by the audio policy manager for certain operations that 123 // are implemented by the policy service. 124 // 125 virtual void setParameters(audio_io_handle_t ioHandle, 126 const char *keyValuePairs, 127 int delayMs); 128 129 virtual status_t setStreamVolume(audio_stream_type_t stream, 130 float volume, 131 audio_io_handle_t output, 132 int delayMs = 0); 133 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); 134 virtual status_t stopTone(); 135 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 136 137private: 138 AudioPolicyService(); 139 virtual ~AudioPolicyService(); 140 141 status_t dumpInternals(int fd); 142 143 // Thread used for tone playback and to send audio config commands to audio flinger 144 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone() 145 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause 146 // calls to AudioPolicyService and an attempt to lock mLock. 147 // For audio config commands, it is necessary because audio flinger requires that the calling process (user) 148 // has permission to modify audio settings. 149 class AudioCommandThread : public Thread { 150 class AudioCommand; 151 public: 152 153 // commands for tone AudioCommand 154 enum { 155 START_TONE, 156 STOP_TONE, 157 SET_VOLUME, 158 SET_PARAMETERS, 159 SET_VOICE_VOLUME 160 }; 161 162 AudioCommandThread (String8 name); 163 virtual ~AudioCommandThread(); 164 165 status_t dump(int fd); 166 167 // Thread virtuals 168 virtual void onFirstRef(); 169 virtual bool threadLoop(); 170 171 void exit(); 172 void startToneCommand(ToneGenerator::tone_type type, 173 audio_stream_type_t stream); 174 void stopToneCommand(); 175 status_t volumeCommand(audio_stream_type_t stream, float volume, 176 audio_io_handle_t output, int delayMs = 0); 177 status_t parametersCommand(audio_io_handle_t ioHandle, 178 const char *keyValuePairs, int delayMs = 0); 179 status_t voiceVolumeCommand(float volume, int delayMs = 0); 180 void insertCommand_l(AudioCommand *command, int delayMs = 0); 181 182 private: 183 // descriptor for requested tone playback event 184 class AudioCommand { 185 186 public: 187 AudioCommand() 188 : mCommand(-1) {} 189 190 void dump(char* buffer, size_t size); 191 192 int mCommand; // START_TONE, STOP_TONE ... 193 nsecs_t mTime; // time stamp 194 Condition mCond; // condition for status return 195 status_t mStatus; // command status 196 bool mWaitStatus; // true if caller is waiting for status 197 void *mParam; // command parameter (ToneData, VolumeData, ParametersData) 198 }; 199 200 class ToneData { 201 public: 202 ToneGenerator::tone_type mType; // tone type (START_TONE only) 203 audio_stream_type_t mStream; // stream type (START_TONE only) 204 }; 205 206 class VolumeData { 207 public: 208 audio_stream_type_t mStream; 209 float mVolume; 210 audio_io_handle_t mIO; 211 }; 212 213 class ParametersData { 214 public: 215 audio_io_handle_t mIO; 216 String8 mKeyValuePairs; 217 }; 218 219 class VoiceVolumeData { 220 public: 221 float mVolume; 222 }; 223 224 Mutex mLock; 225 Condition mWaitWorkCV; 226 Vector <AudioCommand *> mAudioCommands; // list of pending commands 227 ToneGenerator *mpToneGenerator; // the tone generator 228 AudioCommand mLastCommand; // last processed command (used by dump) 229 String8 mName; // string used by wake lock fo delayed commands 230 }; 231 232 class EffectDesc { 233 public: 234 EffectDesc(const char *name, const effect_uuid_t& uuid) : 235 mName(strdup(name)), 236 mUuid(uuid) { } 237 EffectDesc(const EffectDesc& orig) : 238 mName(strdup(orig.mName)), 239 mUuid(orig.mUuid) { 240 // deep copy mParams 241 for (size_t k = 0; k < orig.mParams.size(); k++) { 242 effect_param_t *origParam = orig.mParams[k]; 243 // psize and vsize are rounded up to an int boundary for allocation 244 size_t origSize = sizeof(effect_param_t) + 245 ((origParam->psize + 3) & ~3) + 246 ((origParam->vsize + 3) & ~3); 247 effect_param_t *dupParam = (effect_param_t *) malloc(origSize); 248 memcpy(dupParam, origParam, origSize); 249 // This works because the param buffer allocation is also done by 250 // multiples of 4 bytes originally. In theory we should memcpy only 251 // the actual param size, that is without rounding vsize. 252 mParams.add(dupParam); 253 } 254 } 255 /*virtual*/ ~EffectDesc() { 256 free(mName); 257 for (size_t k = 0; k < mParams.size(); k++) { 258 free(mParams[k]); 259 } 260 } 261 char *mName; 262 effect_uuid_t mUuid; 263 Vector <effect_param_t *> mParams; 264 }; 265 266 class InputSourceDesc { 267 public: 268 InputSourceDesc() {} 269 /*virtual*/ ~InputSourceDesc() { 270 for (size_t j = 0; j < mEffects.size(); j++) { 271 delete mEffects[j]; 272 } 273 } 274 Vector <EffectDesc *> mEffects; 275 }; 276 277 278 class InputDesc { 279 public: 280 InputDesc(int session) : mSessionId(session) {} 281 /*virtual*/ ~InputDesc() {} 282 const int mSessionId; 283 Vector< sp<AudioEffect> >mEffects; 284 }; 285 286 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1]; 287 288 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled); 289 status_t loadPreProcessorConfig(const char *path); 290 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects); 291 EffectDesc *loadEffect(cnode *root); 292 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects); 293 audio_source_t inputSourceNameToEnum(const char *name); 294 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects); 295 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params); 296 effect_param_t *loadEffectParameter(cnode *root); 297 size_t readParamValue(cnode *node, 298 char *param, 299 size_t *curSize, 300 size_t *totSize); 301 size_t growParamSize(char *param, 302 size_t size, 303 size_t *curSize, 304 size_t *totSize); 305 306 // Internal dump utilities. 307 status_t dumpPermissionDenial(int fd); 308 309 310 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing 311 // device connection state or routing 312 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread 313 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread 314 struct audio_policy_device *mpAudioPolicyDev; 315 struct audio_policy *mpAudioPolicy; 316 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources; 317 KeyedVector< audio_io_handle_t, InputDesc* > mInputs; 318}; 319 320}; // namespace android 321 322#endif // ANDROID_AUDIOPOLICYSERVICE_H 323