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