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 <media/IAudioPolicyService.h> 30 #include <media/ToneGenerator.h> 31 #include <media/AudioEffect.h> 32 #include <media/AudioPolicy.h> 33 #include "AudioPolicyEffects.h" 34 #include "managerdefault/AudioPolicyManager.h" 35 36 37 namespace android { 38 39 // ---------------------------------------------------------------------------- 40 41 class AudioPolicyService : 42 public BinderService<AudioPolicyService>, 43 public BnAudioPolicyService, 44 public IBinder::DeathRecipient 45 { 46 friend class BinderService<AudioPolicyService>; 47 48 public: 49 // for BinderService getServiceName()50 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; } 51 52 virtual status_t dump(int fd, const Vector<String16>& args); 53 54 // 55 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) 56 // 57 58 virtual status_t setDeviceConnectionState(audio_devices_t device, 59 audio_policy_dev_state_t state, 60 const char *device_address, 61 const char *device_name); 62 virtual audio_policy_dev_state_t getDeviceConnectionState( 63 audio_devices_t device, 64 const char *device_address); 65 virtual status_t handleDeviceConfigChange(audio_devices_t device, 66 const char *device_address, 67 const char *device_name); 68 virtual status_t setPhoneState(audio_mode_t state); 69 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 70 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); 71 virtual audio_io_handle_t getOutput(audio_stream_type_t stream, 72 uint32_t samplingRate = 0, 73 audio_format_t format = AUDIO_FORMAT_DEFAULT, 74 audio_channel_mask_t channelMask = 0, 75 audio_output_flags_t flags = 76 AUDIO_OUTPUT_FLAG_NONE, 77 const audio_offload_info_t *offloadInfo = NULL); 78 virtual status_t getOutputForAttr(const audio_attributes_t *attr, 79 audio_io_handle_t *output, 80 audio_session_t session, 81 audio_stream_type_t *stream, 82 uid_t uid, 83 const audio_config_t *config, 84 audio_output_flags_t flags, 85 audio_port_handle_t selectedDeviceId, 86 audio_port_handle_t *portId); 87 virtual status_t startOutput(audio_io_handle_t output, 88 audio_stream_type_t stream, 89 audio_session_t session); 90 virtual status_t stopOutput(audio_io_handle_t output, 91 audio_stream_type_t stream, 92 audio_session_t session); 93 virtual void releaseOutput(audio_io_handle_t output, 94 audio_stream_type_t stream, 95 audio_session_t session); 96 virtual status_t getInputForAttr(const audio_attributes_t *attr, 97 audio_io_handle_t *input, 98 audio_session_t session, 99 pid_t pid, 100 uid_t uid, 101 const audio_config_base_t *config, 102 audio_input_flags_t flags, 103 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE, 104 audio_port_handle_t *portId = NULL); 105 virtual status_t startInput(audio_io_handle_t input, 106 audio_session_t session); 107 virtual status_t stopInput(audio_io_handle_t input, 108 audio_session_t session); 109 virtual void releaseInput(audio_io_handle_t input, 110 audio_session_t session); 111 virtual status_t initStreamVolume(audio_stream_type_t stream, 112 int indexMin, 113 int indexMax); 114 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, 115 int index, 116 audio_devices_t device); 117 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, 118 int *index, 119 audio_devices_t device); 120 121 virtual uint32_t getStrategyForStream(audio_stream_type_t stream); 122 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); 123 124 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); 125 virtual status_t registerEffect(const effect_descriptor_t *desc, 126 audio_io_handle_t io, 127 uint32_t strategy, 128 audio_session_t session, 129 int id); 130 virtual status_t unregisterEffect(int id); 131 virtual status_t setEffectEnabled(int id, bool enabled); 132 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 133 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 134 virtual bool isSourceActive(audio_source_t source) const; 135 136 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession, 137 effect_descriptor_t *descriptors, 138 uint32_t *count); 139 virtual status_t onTransact( 140 uint32_t code, 141 const Parcel& data, 142 Parcel* reply, 143 uint32_t flags); 144 145 // IBinder::DeathRecipient 146 virtual void binderDied(const wp<IBinder>& who); 147 148 // RefBase 149 virtual void onFirstRef(); 150 151 // 152 // Helpers for the struct audio_policy_service_ops implementation. 153 // This is used by the audio policy manager for certain operations that 154 // are implemented by the policy service. 155 // 156 virtual void setParameters(audio_io_handle_t ioHandle, 157 const char *keyValuePairs, 158 int delayMs); 159 160 virtual status_t setStreamVolume(audio_stream_type_t stream, 161 float volume, 162 audio_io_handle_t output, 163 int delayMs = 0); 164 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); 165 virtual status_t stopTone(); 166 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 167 virtual bool isOffloadSupported(const audio_offload_info_t &config); 168 169 virtual status_t listAudioPorts(audio_port_role_t role, 170 audio_port_type_t type, 171 unsigned int *num_ports, 172 struct audio_port *ports, 173 unsigned int *generation); 174 virtual status_t getAudioPort(struct audio_port *port); 175 virtual status_t createAudioPatch(const struct audio_patch *patch, 176 audio_patch_handle_t *handle); 177 virtual status_t releaseAudioPatch(audio_patch_handle_t handle); 178 virtual status_t listAudioPatches(unsigned int *num_patches, 179 struct audio_patch *patches, 180 unsigned int *generation); 181 virtual status_t setAudioPortConfig(const struct audio_port_config *config); 182 183 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client); 184 185 virtual void setAudioPortCallbacksEnabled(bool enabled); 186 187 virtual status_t acquireSoundTriggerSession(audio_session_t *session, 188 audio_io_handle_t *ioHandle, 189 audio_devices_t *device); 190 191 virtual status_t releaseSoundTriggerSession(audio_session_t session); 192 193 virtual audio_mode_t getPhoneState(); 194 195 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration); 196 197 virtual status_t startAudioSource(const struct audio_port_config *source, 198 const audio_attributes_t *attributes, 199 audio_patch_handle_t *handle); 200 virtual status_t stopAudioSource(audio_patch_handle_t handle); 201 202 virtual status_t setMasterMono(bool mono); 203 virtual status_t getMasterMono(bool *mono); 204 205 status_t doStopOutput(audio_io_handle_t output, 206 audio_stream_type_t stream, 207 audio_session_t session); 208 void doReleaseOutput(audio_io_handle_t output, 209 audio_stream_type_t stream, 210 audio_session_t session); 211 212 status_t clientCreateAudioPatch(const struct audio_patch *patch, 213 audio_patch_handle_t *handle, 214 int delayMs); 215 status_t clientReleaseAudioPatch(audio_patch_handle_t handle, 216 int delayMs); 217 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config, 218 int delayMs); 219 220 void removeNotificationClient(uid_t uid); 221 void onAudioPortListUpdate(); 222 void doOnAudioPortListUpdate(); 223 void onAudioPatchListUpdate(); 224 void doOnAudioPatchListUpdate(); 225 226 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); 227 void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); 228 void onRecordingConfigurationUpdate(int event, audio_session_t session, 229 audio_source_t source, const audio_config_base_t *clientConfig, 230 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle); 231 void doOnRecordingConfigurationUpdate(int event, audio_session_t session, 232 audio_source_t source, const audio_config_base_t *clientConfig, 233 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle); 234 235 private: 236 AudioPolicyService() ANDROID_API; 237 virtual ~AudioPolicyService(); 238 239 status_t dumpInternals(int fd); 240 241 // Thread used for tone playback and to send audio config commands to audio flinger 242 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because 243 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start 244 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock. 245 // For audio config commands, it is necessary because audio flinger requires that the calling 246 // process (user) has permission to modify audio settings. 247 class AudioCommandThread : public Thread { 248 class AudioCommand; 249 public: 250 251 // commands for tone AudioCommand 252 enum { 253 START_TONE, 254 STOP_TONE, 255 SET_VOLUME, 256 SET_PARAMETERS, 257 SET_VOICE_VOLUME, 258 STOP_OUTPUT, 259 RELEASE_OUTPUT, 260 CREATE_AUDIO_PATCH, 261 RELEASE_AUDIO_PATCH, 262 UPDATE_AUDIOPORT_LIST, 263 UPDATE_AUDIOPATCH_LIST, 264 SET_AUDIOPORT_CONFIG, 265 DYN_POLICY_MIX_STATE_UPDATE, 266 RECORDING_CONFIGURATION_UPDATE 267 }; 268 269 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service); 270 virtual ~AudioCommandThread(); 271 272 status_t dump(int fd); 273 274 // Thread virtuals 275 virtual void onFirstRef(); 276 virtual bool threadLoop(); 277 278 void exit(); 279 void startToneCommand(ToneGenerator::tone_type type, 280 audio_stream_type_t stream); 281 void stopToneCommand(); 282 status_t volumeCommand(audio_stream_type_t stream, float volume, 283 audio_io_handle_t output, int delayMs = 0); 284 status_t parametersCommand(audio_io_handle_t ioHandle, 285 const char *keyValuePairs, int delayMs = 0); 286 status_t voiceVolumeCommand(float volume, int delayMs = 0); 287 void stopOutputCommand(audio_io_handle_t output, 288 audio_stream_type_t stream, 289 audio_session_t session); 290 void releaseOutputCommand(audio_io_handle_t output, 291 audio_stream_type_t stream, 292 audio_session_t session); 293 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0); 294 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0); 295 status_t createAudioPatchCommand(const struct audio_patch *patch, 296 audio_patch_handle_t *handle, 297 int delayMs); 298 status_t releaseAudioPatchCommand(audio_patch_handle_t handle, 299 int delayMs); 300 void updateAudioPortListCommand(); 301 void updateAudioPatchListCommand(); 302 status_t setAudioPortConfigCommand(const struct audio_port_config *config, 303 int delayMs); 304 void dynamicPolicyMixStateUpdateCommand(const String8& regId, int32_t state); 305 void recordingConfigurationUpdateCommand( 306 int event, audio_session_t session, 307 audio_source_t source, 308 const audio_config_base_t *clientConfig, 309 const audio_config_base_t *deviceConfig, 310 audio_patch_handle_t patchHandle); 311 void insertCommand_l(AudioCommand *command, int delayMs = 0); 312 313 private: 314 class AudioCommandData; 315 316 // descriptor for requested tone playback event 317 class AudioCommand: public RefBase { 318 319 public: AudioCommand()320 AudioCommand() 321 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {} 322 323 void dump(char* buffer, size_t size); 324 325 int mCommand; // START_TONE, STOP_TONE ... 326 nsecs_t mTime; // time stamp 327 Mutex mLock; // mutex associated to mCond 328 Condition mCond; // condition for status return 329 status_t mStatus; // command status 330 bool mWaitStatus; // true if caller is waiting for status 331 sp<AudioCommandData> mParam; // command specific parameter data 332 }; 333 334 class AudioCommandData: public RefBase { 335 public: ~AudioCommandData()336 virtual ~AudioCommandData() {} 337 protected: AudioCommandData()338 AudioCommandData() {} 339 }; 340 341 class ToneData : public AudioCommandData { 342 public: 343 ToneGenerator::tone_type mType; // tone type (START_TONE only) 344 audio_stream_type_t mStream; // stream type (START_TONE only) 345 }; 346 347 class VolumeData : public AudioCommandData { 348 public: 349 audio_stream_type_t mStream; 350 float mVolume; 351 audio_io_handle_t mIO; 352 }; 353 354 class ParametersData : public AudioCommandData { 355 public: 356 audio_io_handle_t mIO; 357 String8 mKeyValuePairs; 358 }; 359 360 class VoiceVolumeData : public AudioCommandData { 361 public: 362 float mVolume; 363 }; 364 365 class StopOutputData : public AudioCommandData { 366 public: 367 audio_io_handle_t mIO; 368 audio_stream_type_t mStream; 369 audio_session_t mSession; 370 }; 371 372 class ReleaseOutputData : public AudioCommandData { 373 public: 374 audio_io_handle_t mIO; 375 audio_stream_type_t mStream; 376 audio_session_t mSession; 377 }; 378 379 class CreateAudioPatchData : public AudioCommandData { 380 public: 381 struct audio_patch mPatch; 382 audio_patch_handle_t mHandle; 383 }; 384 385 class ReleaseAudioPatchData : public AudioCommandData { 386 public: 387 audio_patch_handle_t mHandle; 388 }; 389 390 class SetAudioPortConfigData : public AudioCommandData { 391 public: 392 struct audio_port_config mConfig; 393 }; 394 395 class DynPolicyMixStateUpdateData : public AudioCommandData { 396 public: 397 String8 mRegId; 398 int32_t mState; 399 }; 400 401 class RecordingConfigurationUpdateData : public AudioCommandData { 402 public: 403 int mEvent; 404 audio_session_t mSession; 405 audio_source_t mSource; 406 struct audio_config_base mClientConfig; 407 struct audio_config_base mDeviceConfig; 408 audio_patch_handle_t mPatchHandle; 409 }; 410 411 Mutex mLock; 412 Condition mWaitWorkCV; 413 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands 414 ToneGenerator *mpToneGenerator; // the tone generator 415 sp<AudioCommand> mLastCommand; // last processed command (used by dump) 416 String8 mName; // string used by wake lock fo delayed commands 417 wp<AudioPolicyService> mService; 418 }; 419 420 class AudioPolicyClient : public AudioPolicyClientInterface 421 { 422 public: AudioPolicyClient(AudioPolicyService * service)423 explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {} ~AudioPolicyClient()424 virtual ~AudioPolicyClient() {} 425 426 // 427 // Audio HW module functions 428 // 429 430 // loads a HW module. 431 virtual audio_module_handle_t loadHwModule(const char *name); 432 433 // 434 // Audio output Control functions 435 // 436 437 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values 438 // in case the audio policy manager has no specific requirements for the output being opened. 439 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. 440 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. 441 virtual status_t openOutput(audio_module_handle_t module, 442 audio_io_handle_t *output, 443 audio_config_t *config, 444 audio_devices_t *devices, 445 const String8& address, 446 uint32_t *latencyMs, 447 audio_output_flags_t flags); 448 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by 449 // a special mixer thread in the AudioFlinger. 450 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2); 451 // closes the output stream 452 virtual status_t closeOutput(audio_io_handle_t output); 453 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in 454 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. 455 virtual status_t suspendOutput(audio_io_handle_t output); 456 // restores a suspended output. 457 virtual status_t restoreOutput(audio_io_handle_t output); 458 459 // 460 // Audio input Control functions 461 // 462 463 // opens an audio input 464 virtual audio_io_handle_t openInput(audio_module_handle_t module, 465 audio_io_handle_t *input, 466 audio_config_t *config, 467 audio_devices_t *devices, 468 const String8& address, 469 audio_source_t source, 470 audio_input_flags_t flags); 471 // closes an audio input 472 virtual status_t closeInput(audio_io_handle_t input); 473 // 474 // misc control functions 475 // 476 477 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes 478 // for each output (destination device) it is attached to. 479 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0); 480 481 // invalidate a stream type, causing a reroute to an unspecified new output 482 virtual status_t invalidateStream(audio_stream_type_t stream); 483 484 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. 485 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); 486 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. 487 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); 488 489 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing 490 // over a telephony device during a phone call. 491 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); 492 virtual status_t stopTone(); 493 494 // set down link audio volume. 495 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 496 497 // move effect to the specified output 498 virtual status_t moveEffects(audio_session_t session, 499 audio_io_handle_t srcOutput, 500 audio_io_handle_t dstOutput); 501 502 /* Create a patch between several source and sink ports */ 503 virtual status_t createAudioPatch(const struct audio_patch *patch, 504 audio_patch_handle_t *handle, 505 int delayMs); 506 507 /* Release a patch */ 508 virtual status_t releaseAudioPatch(audio_patch_handle_t handle, 509 int delayMs); 510 511 /* Set audio port configuration */ 512 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs); 513 514 virtual void onAudioPortListUpdate(); 515 virtual void onAudioPatchListUpdate(); 516 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state); 517 virtual void onRecordingConfigurationUpdate(int event, 518 audio_session_t session, audio_source_t source, 519 const audio_config_base_t *clientConfig, 520 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle); 521 522 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use); 523 524 private: 525 AudioPolicyService *mAudioPolicyService; 526 }; 527 528 // --- Notification Client --- 529 class NotificationClient : public IBinder::DeathRecipient { 530 public: 531 NotificationClient(const sp<AudioPolicyService>& service, 532 const sp<IAudioPolicyServiceClient>& client, 533 uid_t uid); 534 virtual ~NotificationClient(); 535 536 void onAudioPortListUpdate(); 537 void onAudioPatchListUpdate(); 538 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); 539 void onRecordingConfigurationUpdate( 540 int event, audio_session_t session, 541 audio_source_t source, 542 const audio_config_base_t *clientConfig, 543 const audio_config_base_t *deviceConfig, 544 audio_patch_handle_t patchHandle); 545 void setAudioPortCallbacksEnabled(bool enabled); 546 547 // IBinder::DeathRecipient 548 virtual void binderDied(const wp<IBinder>& who); 549 550 private: 551 NotificationClient(const NotificationClient&); 552 NotificationClient& operator = (const NotificationClient&); 553 554 const wp<AudioPolicyService> mService; 555 const uid_t mUid; 556 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient; 557 bool mAudioPortCallbacksEnabled; 558 }; 559 560 // Internal dump utilities. 561 status_t dumpPermissionDenial(int fd); 562 563 564 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing 565 // device connection state or routing 566 mutable Mutex mEffectsLock; // serialize access to Effect state within APM. 567 // Note: lock acquisition order is always mLock > mEffectsLock: 568 // mLock protects AudioPolicyManager methods that can call into audio flinger 569 // and possibly back in to audio policy service and acquire mEffectsLock. 570 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread 571 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread 572 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output 573 struct audio_policy_device *mpAudioPolicyDev; 574 struct audio_policy *mpAudioPolicy; 575 AudioPolicyInterface *mAudioPolicyManager; 576 AudioPolicyClient *mAudioPolicyClient; 577 578 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients; 579 Mutex mNotificationClientsLock; // protects mNotificationClients 580 // Manage all effects configured in audio_effects.conf 581 sp<AudioPolicyEffects> mAudioPolicyEffects; 582 audio_mode_t mPhoneState; 583 }; 584 585 }; // namespace android 586 587 #endif // ANDROID_AUDIOPOLICYSERVICE_H 588