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