AudioPolicyService.h revision 951f455566775e5f01e67c5ee26863d7d19209d7
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#include <hardware_legacy/AudioPolicyInterface.h> 34#include "AudioPolicyManager.h" 35 36namespace android { 37 38// ---------------------------------------------------------------------------- 39 40class AudioPolicyService : 41 public BinderService<AudioPolicyService>, 42 public BnAudioPolicyService, 43 public IBinder::DeathRecipient 44{ 45 friend class BinderService<AudioPolicyService>; 46 47public: 48 // for BinderService 49 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; } 50 51 virtual status_t dump(int fd, const Vector<String16>& args); 52 53 // 54 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) 55 // 56 57 virtual status_t setDeviceConnectionState(audio_devices_t device, 58 audio_policy_dev_state_t state, 59 const char *device_address); 60 virtual audio_policy_dev_state_t getDeviceConnectionState( 61 audio_devices_t device, 62 const char *device_address); 63 virtual status_t setPhoneState(audio_mode_t state); 64 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 65 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); 66 virtual audio_io_handle_t getOutput(audio_stream_type_t stream, 67 uint32_t samplingRate = 0, 68 audio_format_t format = AUDIO_FORMAT_DEFAULT, 69 audio_channel_mask_t channelMask = 0, 70 audio_output_flags_t flags = 71 AUDIO_OUTPUT_FLAG_NONE, 72 const audio_offload_info_t *offloadInfo = NULL); 73 virtual status_t startOutput(audio_io_handle_t output, 74 audio_stream_type_t stream, 75 int session = 0); 76 virtual status_t stopOutput(audio_io_handle_t output, 77 audio_stream_type_t stream, 78 int session = 0); 79 virtual void releaseOutput(audio_io_handle_t output); 80 virtual audio_io_handle_t getInput(audio_source_t inputSource, 81 uint32_t samplingRate = 0, 82 audio_format_t format = AUDIO_FORMAT_DEFAULT, 83 audio_channel_mask_t channelMask = 0, 84 int audioSession = 0); 85 virtual status_t startInput(audio_io_handle_t input); 86 virtual status_t stopInput(audio_io_handle_t input); 87 virtual void releaseInput(audio_io_handle_t input); 88 virtual status_t initStreamVolume(audio_stream_type_t stream, 89 int indexMin, 90 int indexMax); 91 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, 92 int index, 93 audio_devices_t device); 94 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, 95 int *index, 96 audio_devices_t device); 97 98 virtual uint32_t getStrategyForStream(audio_stream_type_t stream); 99 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); 100 101 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); 102 virtual status_t registerEffect(const effect_descriptor_t *desc, 103 audio_io_handle_t io, 104 uint32_t strategy, 105 int session, 106 int id); 107 virtual status_t unregisterEffect(int id); 108 virtual status_t setEffectEnabled(int id, bool enabled); 109 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 110 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 111 virtual bool isSourceActive(audio_source_t source) const; 112 113 virtual status_t queryDefaultPreProcessing(int audioSession, 114 effect_descriptor_t *descriptors, 115 uint32_t *count); 116 virtual status_t onTransact( 117 uint32_t code, 118 const Parcel& data, 119 Parcel* reply, 120 uint32_t flags); 121 122 // IBinder::DeathRecipient 123 virtual void binderDied(const wp<IBinder>& who); 124 125 // 126 // Helpers for the struct audio_policy_service_ops implementation. 127 // This is used by the audio policy manager for certain operations that 128 // are implemented by the policy service. 129 // 130 virtual void setParameters(audio_io_handle_t ioHandle, 131 const char *keyValuePairs, 132 int delayMs); 133 134 virtual status_t setStreamVolume(audio_stream_type_t stream, 135 float volume, 136 audio_io_handle_t output, 137 int delayMs = 0); 138 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); 139 virtual status_t stopTone(); 140 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 141 virtual bool isOffloadSupported(const audio_offload_info_t &config); 142 143 status_t doStopOutput(audio_io_handle_t output, 144 audio_stream_type_t stream, 145 int session = 0); 146 void doReleaseOutput(audio_io_handle_t output); 147 148 status_t clientCreateAudioPatch(const struct audio_patch *patch, 149 audio_patch_handle_t *handle, 150 int delayMs); 151 status_t clientReleaseAudioPatch(audio_patch_handle_t handle, 152 int delayMs); 153 154private: 155 AudioPolicyService() ANDROID_API; 156 virtual ~AudioPolicyService(); 157 158 status_t dumpInternals(int fd); 159 160 // Thread used for tone playback and to send audio config commands to audio flinger 161 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because 162 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start 163 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock. 164 // For audio config commands, it is necessary because audio flinger requires that the calling 165 // process (user) has permission to modify audio settings. 166 class AudioCommandThread : public Thread { 167 class AudioCommand; 168 public: 169 170 // commands for tone AudioCommand 171 enum { 172 START_TONE, 173 STOP_TONE, 174 SET_VOLUME, 175 SET_PARAMETERS, 176 SET_VOICE_VOLUME, 177 STOP_OUTPUT, 178 RELEASE_OUTPUT, 179 CREATE_AUDIO_PATCH, 180 RELEASE_AUDIO_PATCH, 181 }; 182 183 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service); 184 virtual ~AudioCommandThread(); 185 186 status_t dump(int fd); 187 188 // Thread virtuals 189 virtual void onFirstRef(); 190 virtual bool threadLoop(); 191 192 void exit(); 193 void startToneCommand(ToneGenerator::tone_type type, 194 audio_stream_type_t stream); 195 void stopToneCommand(); 196 status_t volumeCommand(audio_stream_type_t stream, float volume, 197 audio_io_handle_t output, int delayMs = 0); 198 status_t parametersCommand(audio_io_handle_t ioHandle, 199 const char *keyValuePairs, int delayMs = 0); 200 status_t voiceVolumeCommand(float volume, int delayMs = 0); 201 void stopOutputCommand(audio_io_handle_t output, 202 audio_stream_type_t stream, 203 int session); 204 void releaseOutputCommand(audio_io_handle_t output); 205 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0); 206 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0); 207 status_t createAudioPatchCommand(const struct audio_patch *patch, 208 audio_patch_handle_t *handle, 209 int delayMs); 210 status_t releaseAudioPatchCommand(audio_patch_handle_t handle, 211 int delayMs); 212 213 void insertCommand_l(AudioCommand *command, int delayMs = 0); 214 215 private: 216 class AudioCommandData; 217 218 // descriptor for requested tone playback event 219 class AudioCommand: public RefBase { 220 221 public: 222 AudioCommand() 223 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {} 224 225 void dump(char* buffer, size_t size); 226 227 int mCommand; // START_TONE, STOP_TONE ... 228 nsecs_t mTime; // time stamp 229 Mutex mLock; // mutex associated to mCond 230 Condition mCond; // condition for status return 231 status_t mStatus; // command status 232 bool mWaitStatus; // true if caller is waiting for status 233 sp<AudioCommandData> mParam; // command specific parameter data 234 }; 235 236 class AudioCommandData: public RefBase { 237 public: 238 virtual ~AudioCommandData() {} 239 protected: 240 AudioCommandData() {} 241 }; 242 243 class ToneData : public AudioCommandData { 244 public: 245 ToneGenerator::tone_type mType; // tone type (START_TONE only) 246 audio_stream_type_t mStream; // stream type (START_TONE only) 247 }; 248 249 class VolumeData : public AudioCommandData { 250 public: 251 audio_stream_type_t mStream; 252 float mVolume; 253 audio_io_handle_t mIO; 254 }; 255 256 class ParametersData : public AudioCommandData { 257 public: 258 audio_io_handle_t mIO; 259 String8 mKeyValuePairs; 260 }; 261 262 class VoiceVolumeData : public AudioCommandData { 263 public: 264 float mVolume; 265 }; 266 267 class StopOutputData : public AudioCommandData { 268 public: 269 audio_io_handle_t mIO; 270 audio_stream_type_t mStream; 271 int mSession; 272 }; 273 274 class ReleaseOutputData : public AudioCommandData { 275 public: 276 audio_io_handle_t mIO; 277 }; 278 279 class CreateAudioPatchData : public AudioCommandData { 280 public: 281 struct audio_patch mPatch; 282 audio_patch_handle_t mHandle; 283 }; 284 285 class ReleaseAudioPatchData : public AudioCommandData { 286 public: 287 audio_patch_handle_t mHandle; 288 }; 289 290 Mutex mLock; 291 Condition mWaitWorkCV; 292 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands 293 ToneGenerator *mpToneGenerator; // the tone generator 294 sp<AudioCommand> mLastCommand; // last processed command (used by dump) 295 String8 mName; // string used by wake lock fo delayed commands 296 wp<AudioPolicyService> mService; 297 }; 298 299 class EffectDesc { 300 public: 301 EffectDesc(const char *name, const effect_uuid_t& uuid) : 302 mName(strdup(name)), 303 mUuid(uuid) { } 304 EffectDesc(const EffectDesc& orig) : 305 mName(strdup(orig.mName)), 306 mUuid(orig.mUuid) { 307 // deep copy mParams 308 for (size_t k = 0; k < orig.mParams.size(); k++) { 309 effect_param_t *origParam = orig.mParams[k]; 310 // psize and vsize are rounded up to an int boundary for allocation 311 size_t origSize = sizeof(effect_param_t) + 312 ((origParam->psize + 3) & ~3) + 313 ((origParam->vsize + 3) & ~3); 314 effect_param_t *dupParam = (effect_param_t *) malloc(origSize); 315 memcpy(dupParam, origParam, origSize); 316 // This works because the param buffer allocation is also done by 317 // multiples of 4 bytes originally. In theory we should memcpy only 318 // the actual param size, that is without rounding vsize. 319 mParams.add(dupParam); 320 } 321 } 322 /*virtual*/ ~EffectDesc() { 323 free(mName); 324 for (size_t k = 0; k < mParams.size(); k++) { 325 free(mParams[k]); 326 } 327 } 328 char *mName; 329 effect_uuid_t mUuid; 330 Vector <effect_param_t *> mParams; 331 }; 332 333 class InputSourceDesc { 334 public: 335 InputSourceDesc() {} 336 /*virtual*/ ~InputSourceDesc() { 337 for (size_t j = 0; j < mEffects.size(); j++) { 338 delete mEffects[j]; 339 } 340 } 341 Vector <EffectDesc *> mEffects; 342 }; 343 344 345 class InputDesc { 346 public: 347 InputDesc(int session) : mSessionId(session) {} 348 /*virtual*/ ~InputDesc() {} 349 const int mSessionId; 350 Vector< sp<AudioEffect> >mEffects; 351 }; 352 353 class AudioPolicyClient : public AudioPolicyClientInterface 354 { 355 public: 356 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {} 357 virtual ~AudioPolicyClient() {} 358 359 // 360 // Audio HW module functions 361 // 362 363 // loads a HW module. 364 virtual audio_module_handle_t loadHwModule(const char *name); 365 366 // 367 // Audio output Control functions 368 // 369 370 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values 371 // in case the audio policy manager has no specific requirements for the output being opened. 372 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. 373 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. 374 virtual audio_io_handle_t openOutput(audio_module_handle_t module, 375 audio_devices_t *pDevices, 376 uint32_t *pSamplingRate, 377 audio_format_t *pFormat, 378 audio_channel_mask_t *pChannelMask, 379 uint32_t *pLatencyMs, 380 audio_output_flags_t flags, 381 const audio_offload_info_t *offloadInfo = NULL); 382 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by 383 // a special mixer thread in the AudioFlinger. 384 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2); 385 // closes the output stream 386 virtual status_t closeOutput(audio_io_handle_t output); 387 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in 388 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. 389 virtual status_t suspendOutput(audio_io_handle_t output); 390 // restores a suspended output. 391 virtual status_t restoreOutput(audio_io_handle_t output); 392 393 // 394 // Audio input Control functions 395 // 396 397 // opens an audio input 398 virtual audio_io_handle_t openInput(audio_module_handle_t module, 399 audio_devices_t *pDevices, 400 uint32_t *pSamplingRate, 401 audio_format_t *pFormat, 402 audio_channel_mask_t *pChannelMask); 403 // closes an audio input 404 virtual status_t closeInput(audio_io_handle_t input); 405 // 406 // misc control functions 407 // 408 409 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes 410 // for each output (destination device) it is attached to. 411 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0); 412 413 // invalidate a stream type, causing a reroute to an unspecified new output 414 virtual status_t invalidateStream(audio_stream_type_t stream); 415 416 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. 417 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); 418 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. 419 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); 420 421 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing 422 // over a telephony device during a phone call. 423 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); 424 virtual status_t stopTone(); 425 426 // set down link audio volume. 427 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 428 429 // move effect to the specified output 430 virtual status_t moveEffects(int session, 431 audio_io_handle_t srcOutput, 432 audio_io_handle_t dstOutput); 433 434 /* Create a patch between several source and sink ports */ 435 virtual status_t createAudioPatch(const struct audio_patch *patch, 436 audio_patch_handle_t *handle, 437 int delayMs); 438 439 /* Release a patch */ 440 virtual status_t releaseAudioPatch(audio_patch_handle_t handle, 441 int delayMs); 442 443 private: 444 AudioPolicyService *mAudioPolicyService; 445 }; 446 447 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1]; 448 449 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled); 450 status_t loadPreProcessorConfig(const char *path); 451 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects); 452 EffectDesc *loadEffect(cnode *root); 453 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects); 454 audio_source_t inputSourceNameToEnum(const char *name); 455 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects); 456 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params); 457 effect_param_t *loadEffectParameter(cnode *root); 458 size_t readParamValue(cnode *node, 459 char *param, 460 size_t *curSize, 461 size_t *totSize); 462 size_t growParamSize(char *param, 463 size_t size, 464 size_t *curSize, 465 size_t *totSize); 466 467 // Internal dump utilities. 468 status_t dumpPermissionDenial(int fd); 469 470 471 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing 472 // device connection state or routing 473 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread 474 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread 475 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output 476 struct audio_policy_device *mpAudioPolicyDev; 477 struct audio_policy *mpAudioPolicy; 478 AudioPolicyManager *mAudioPolicyManager; 479 AudioPolicyClient *mAudioPolicyClient; 480 481 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources; 482 KeyedVector< audio_io_handle_t, InputDesc* > mInputs; 483}; 484 485}; // namespace android 486 487#endif // ANDROID_AUDIOPOLICYSERVICE_H 488