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/ActivityManager.h> 27 #include <binder/BinderService.h> 28 #include <binder/IUidObserver.h> 29 #include <system/audio.h> 30 #include <system/audio_policy.h> 31 #include <media/IAudioPolicyService.h> 32 #include <media/ToneGenerator.h> 33 #include <media/AudioEffect.h> 34 #include <media/AudioPolicy.h> 35 #include <mediautils/ServiceUtilities.h> 36 #include "AudioPolicyEffects.h" 37 #include <AudioPolicyInterface.h> 38 #include <android/hardware/BnSensorPrivacyListener.h> 39 40 #include <unordered_map> 41 42 namespace android { 43 44 // ---------------------------------------------------------------------------- 45 46 class AudioPolicyService : 47 public BinderService<AudioPolicyService>, 48 public BnAudioPolicyService, 49 public IBinder::DeathRecipient 50 { 51 friend class BinderService<AudioPolicyService>; 52 53 public: 54 // for BinderService getServiceName()55 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; } 56 57 virtual status_t dump(int fd, const Vector<String16>& args); 58 59 // 60 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) 61 // 62 63 void onNewAudioModulesAvailable() override; 64 virtual status_t setDeviceConnectionState(audio_devices_t device, 65 audio_policy_dev_state_t state, 66 const char *device_address, 67 const char *device_name, 68 audio_format_t encodedFormat); 69 virtual audio_policy_dev_state_t getDeviceConnectionState( 70 audio_devices_t device, 71 const char *device_address); 72 virtual status_t handleDeviceConfigChange(audio_devices_t device, 73 const char *device_address, 74 const char *device_name, 75 audio_format_t encodedFormat); 76 virtual status_t setPhoneState(audio_mode_t state); 77 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); 78 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); 79 virtual audio_io_handle_t getOutput(audio_stream_type_t stream); 80 status_t getOutputForAttr(audio_attributes_t *attr, 81 audio_io_handle_t *output, 82 audio_session_t session, 83 audio_stream_type_t *stream, 84 pid_t pid, 85 uid_t uid, 86 const audio_config_t *config, 87 audio_output_flags_t flags, 88 audio_port_handle_t *selectedDeviceId, 89 audio_port_handle_t *portId, 90 std::vector<audio_io_handle_t> *secondaryOutputs) override; 91 virtual status_t startOutput(audio_port_handle_t portId); 92 virtual status_t stopOutput(audio_port_handle_t portId); 93 virtual void releaseOutput(audio_port_handle_t portId); 94 virtual status_t getInputForAttr(const audio_attributes_t *attr, 95 audio_io_handle_t *input, 96 audio_unique_id_t riid, 97 audio_session_t session, 98 pid_t pid, 99 uid_t uid, 100 const String16& opPackageName, 101 const audio_config_base_t *config, 102 audio_input_flags_t flags, 103 audio_port_handle_t *selectedDeviceId = NULL, 104 audio_port_handle_t *portId = NULL); 105 virtual status_t startInput(audio_port_handle_t portId); 106 virtual status_t stopInput(audio_port_handle_t portId); 107 virtual void releaseInput(audio_port_handle_t portId); 108 virtual status_t initStreamVolume(audio_stream_type_t stream, 109 int indexMin, 110 int indexMax); 111 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, 112 int index, 113 audio_devices_t device); 114 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, 115 int *index, 116 audio_devices_t device); 117 118 virtual status_t setVolumeIndexForAttributes(const audio_attributes_t &attr, 119 int index, 120 audio_devices_t device); 121 virtual status_t getVolumeIndexForAttributes(const audio_attributes_t &attr, 122 int &index, 123 audio_devices_t device); 124 virtual status_t getMinVolumeIndexForAttributes(const audio_attributes_t &attr, 125 int &index); 126 virtual status_t getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, 127 int &index); 128 129 virtual uint32_t getStrategyForStream(audio_stream_type_t stream); 130 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); 131 132 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); 133 virtual status_t registerEffect(const effect_descriptor_t *desc, 134 audio_io_handle_t io, 135 uint32_t strategy, 136 audio_session_t session, 137 int id); 138 virtual status_t unregisterEffect(int id); 139 virtual status_t setEffectEnabled(int id, bool enabled); 140 status_t moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) override; 141 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 142 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; 143 virtual bool isSourceActive(audio_source_t source) const; 144 145 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession, 146 effect_descriptor_t *descriptors, 147 uint32_t *count); 148 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type, 149 const String16& opPackageName, 150 const effect_uuid_t *uuid, 151 int32_t priority, 152 audio_source_t source, 153 audio_unique_id_t* id); 154 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type, 155 const String16& opPackageName, 156 const effect_uuid_t *uuid, 157 int32_t priority, 158 audio_usage_t usage, 159 audio_unique_id_t* id); 160 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id); 161 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id); 162 163 virtual status_t onTransact( 164 uint32_t code, 165 const Parcel& data, 166 Parcel* reply, 167 uint32_t flags); 168 169 // IBinder::DeathRecipient 170 virtual void binderDied(const wp<IBinder>& who); 171 172 // RefBase 173 virtual void onFirstRef(); 174 175 // 176 // Helpers for the struct audio_policy_service_ops implementation. 177 // This is used by the audio policy manager for certain operations that 178 // are implemented by the policy service. 179 // 180 virtual void setParameters(audio_io_handle_t ioHandle, 181 const char *keyValuePairs, 182 int delayMs); 183 184 virtual status_t setStreamVolume(audio_stream_type_t stream, 185 float volume, 186 audio_io_handle_t output, 187 int delayMs = 0); 188 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 189 status_t setAllowedCapturePolicy(uint_t uid, audio_flags_mask_t capturePolicy) override; 190 virtual bool isOffloadSupported(const audio_offload_info_t &config); 191 virtual bool isDirectOutputSupported(const audio_config_base_t& config, 192 const audio_attributes_t& attributes); 193 194 virtual status_t listAudioPorts(audio_port_role_t role, 195 audio_port_type_t type, 196 unsigned int *num_ports, 197 struct audio_port *ports, 198 unsigned int *generation); 199 virtual status_t getAudioPort(struct audio_port *port); 200 virtual status_t createAudioPatch(const struct audio_patch *patch, 201 audio_patch_handle_t *handle); 202 virtual status_t releaseAudioPatch(audio_patch_handle_t handle); 203 virtual status_t listAudioPatches(unsigned int *num_patches, 204 struct audio_patch *patches, 205 unsigned int *generation); 206 virtual status_t setAudioPortConfig(const struct audio_port_config *config); 207 208 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client); 209 210 virtual void setAudioPortCallbacksEnabled(bool enabled); 211 212 virtual void setAudioVolumeGroupCallbacksEnabled(bool enabled); 213 214 virtual status_t acquireSoundTriggerSession(audio_session_t *session, 215 audio_io_handle_t *ioHandle, 216 audio_devices_t *device); 217 218 virtual status_t releaseSoundTriggerSession(audio_session_t session); 219 220 virtual audio_mode_t getPhoneState(); 221 222 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration); 223 224 virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices); 225 226 virtual status_t removeUidDeviceAffinities(uid_t uid); 227 228 virtual status_t setPreferredDeviceForStrategy(product_strategy_t strategy, 229 const AudioDeviceTypeAddr &device); 230 231 virtual status_t removePreferredDeviceForStrategy(product_strategy_t strategy); 232 233 234 virtual status_t getPreferredDeviceForStrategy(product_strategy_t strategy, 235 AudioDeviceTypeAddr &device); 236 237 virtual status_t startAudioSource(const struct audio_port_config *source, 238 const audio_attributes_t *attributes, 239 audio_port_handle_t *portId); 240 virtual status_t stopAudioSource(audio_port_handle_t portId); 241 242 virtual status_t setMasterMono(bool mono); 243 virtual status_t getMasterMono(bool *mono); 244 245 virtual float getStreamVolumeDB( 246 audio_stream_type_t stream, int index, audio_devices_t device); 247 248 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats, 249 audio_format_t *surroundFormats, 250 bool *surroundFormatsEnabled, 251 bool reported); 252 virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP( 253 std::vector<audio_format_t> *formats); 254 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled); 255 256 virtual status_t setAssistantUid(uid_t uid); 257 virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids); 258 259 virtual bool isHapticPlaybackSupported(); 260 261 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies); 262 virtual status_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa, 263 product_strategy_t &productStrategy); 264 265 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups); 266 267 virtual status_t getVolumeGroupFromAudioAttributes(const AudioAttributes &aa, 268 volume_group_t &volumeGroup); 269 270 virtual status_t setRttEnabled(bool enabled); 271 272 void doOnNewAudioModulesAvailable(); 273 status_t doStopOutput(audio_port_handle_t portId); 274 void doReleaseOutput(audio_port_handle_t portId); 275 276 status_t clientCreateAudioPatch(const struct audio_patch *patch, 277 audio_patch_handle_t *handle, 278 int delayMs); 279 status_t clientReleaseAudioPatch(audio_patch_handle_t handle, 280 int delayMs); 281 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config, 282 int delayMs); 283 284 void removeNotificationClient(uid_t uid, pid_t pid); 285 void onAudioPortListUpdate(); 286 void doOnAudioPortListUpdate(); 287 void onAudioPatchListUpdate(); 288 void doOnAudioPatchListUpdate(); 289 290 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); 291 void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); 292 void onRecordingConfigurationUpdate(int event, 293 const record_client_info_t *clientInfo, 294 const audio_config_base_t *clientConfig, 295 std::vector<effect_descriptor_t> clientEffects, 296 const audio_config_base_t *deviceConfig, 297 std::vector<effect_descriptor_t> effects, 298 audio_patch_handle_t patchHandle, 299 audio_source_t source); 300 void doOnRecordingConfigurationUpdate(int event, 301 const record_client_info_t *clientInfo, 302 const audio_config_base_t *clientConfig, 303 std::vector<effect_descriptor_t> clientEffects, 304 const audio_config_base_t *deviceConfig, 305 std::vector<effect_descriptor_t> effects, 306 audio_patch_handle_t patchHandle, 307 audio_source_t source); 308 309 void onAudioVolumeGroupChanged(volume_group_t group, int flags); 310 void doOnAudioVolumeGroupChanged(volume_group_t group, int flags); 311 void setEffectSuspended(int effectId, 312 audio_session_t sessionId, 313 bool suspended); 314 315 private: 316 AudioPolicyService() ANDROID_API; 317 virtual ~AudioPolicyService(); 318 319 status_t dumpInternals(int fd); 320 321 // Handles binder shell commands 322 virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args); 323 324 // Sets whether the given UID records only silence 325 virtual void setAppState_l(uid_t uid, app_state_t state); 326 327 // Overrides the UID state as if it is idle 328 status_t handleSetUidState(Vector<String16>& args, int err); 329 330 // Clears the override for the UID state 331 status_t handleResetUidState(Vector<String16>& args, int err); 332 333 // Gets the UID state 334 status_t handleGetUidState(Vector<String16>& args, int out, int err); 335 336 // Prints the shell command help 337 status_t printHelp(int out); 338 339 std::string getDeviceTypeStrForPortId(audio_port_handle_t portId); 340 341 status_t getAudioPolicyEffects(sp<AudioPolicyEffects>& audioPolicyEffects); 342 343 app_state_t apmStatFromAmState(int amState); 344 345 void updateUidStates(); 346 void updateUidStates_l(); 347 348 void silenceAllRecordings_l(); 349 350 static bool isPrivacySensitiveSource(audio_source_t source); 351 static bool isVirtualSource(audio_source_t source); 352 353 // If recording we need to make sure the UID is allowed to do that. If the UID is idle 354 // then it cannot record and gets buffers with zeros - silence. As soon as the UID 355 // transitions to an active state we will start reporting buffers with data. This approach 356 // transparently handles recording while the UID transitions between idle/active state 357 // avoiding to get stuck in a state receiving non-empty buffers while idle or in a state 358 // receiving empty buffers while active. 359 class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient { 360 public: UidPolicy(wp<AudioPolicyService> service)361 explicit UidPolicy(wp<AudioPolicyService> service) 362 : mService(service), mObserverRegistered(false), 363 mAssistantUid(0), mRttEnabled(false) {} 364 365 void registerSelf(); 366 void unregisterSelf(); 367 368 // IBinder::DeathRecipient implementation 369 void binderDied(const wp<IBinder> &who) override; 370 371 bool isUidActive(uid_t uid); 372 int getUidState(uid_t uid); setAssistantUid(uid_t uid)373 void setAssistantUid(uid_t uid) { mAssistantUid = uid; } isAssistantUid(uid_t uid)374 bool isAssistantUid(uid_t uid) { return uid == mAssistantUid; } setA11yUids(const std::vector<uid_t> & uids)375 void setA11yUids(const std::vector<uid_t>& uids) { mA11yUids.clear(); mA11yUids = uids; } 376 bool isA11yUid(uid_t uid); 377 bool isA11yOnTop(); setRttEnabled(bool enabled)378 void setRttEnabled(bool enabled) { mRttEnabled = enabled; } isRttEnabled()379 bool isRttEnabled() { return mRttEnabled; } 380 381 // BnUidObserver implementation 382 void onUidActive(uid_t uid) override; 383 void onUidGone(uid_t uid, bool disabled) override; 384 void onUidIdle(uid_t uid, bool disabled) override; 385 void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq); 386 addOverrideUid(uid_t uid,bool active)387 void addOverrideUid(uid_t uid, bool active) { updateOverrideUid(uid, active, true); } removeOverrideUid(uid_t uid)388 void removeOverrideUid(uid_t uid) { updateOverrideUid(uid, false, false); } 389 390 void updateUid(std::unordered_map<uid_t, std::pair<bool, int>> *uids, 391 uid_t uid, bool active, int state, bool insert); 392 393 private: 394 void notifyService(); 395 void updateOverrideUid(uid_t uid, bool active, bool insert); 396 void updateUidLocked(std::unordered_map<uid_t, std::pair<bool, int>> *uids, 397 uid_t uid, bool active, int state, bool insert); 398 void checkRegistered(); 399 400 wp<AudioPolicyService> mService; 401 Mutex mLock; 402 ActivityManager mAm; 403 bool mObserverRegistered; 404 std::unordered_map<uid_t, std::pair<bool, int>> mOverrideUids; 405 std::unordered_map<uid_t, std::pair<bool, int>> mCachedUids; 406 uid_t mAssistantUid; 407 std::vector<uid_t> mA11yUids; 408 bool mRttEnabled; 409 }; 410 411 // If sensor privacy is enabled then all apps, including those that are active, should be 412 // prevented from recording. This is handled similar to idle UIDs, any app that attempts 413 // to record while sensor privacy is enabled will receive buffers with zeros. As soon as 414 // sensor privacy is disabled active apps will receive the expected data when recording. 415 class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener { 416 public: SensorPrivacyPolicy(wp<AudioPolicyService> service)417 explicit SensorPrivacyPolicy(wp<AudioPolicyService> service) 418 : mService(service) {} 419 420 void registerSelf(); 421 void unregisterSelf(); 422 423 bool isSensorPrivacyEnabled(); 424 425 binder::Status onSensorPrivacyChanged(bool enabled); 426 427 private: 428 wp<AudioPolicyService> mService; 429 std::atomic_bool mSensorPrivacyEnabled; 430 }; 431 432 // Thread used to send audio config commands to audio flinger 433 // For audio config commands, it is necessary because audio flinger requires that the calling 434 // process (user) has permission to modify audio settings. 435 class AudioCommandThread : public Thread { 436 class AudioCommand; 437 public: 438 439 // commands for tone AudioCommand 440 enum { 441 SET_VOLUME, 442 SET_PARAMETERS, 443 SET_VOICE_VOLUME, 444 STOP_OUTPUT, 445 RELEASE_OUTPUT, 446 CREATE_AUDIO_PATCH, 447 RELEASE_AUDIO_PATCH, 448 UPDATE_AUDIOPORT_LIST, 449 UPDATE_AUDIOPATCH_LIST, 450 CHANGED_AUDIOVOLUMEGROUP, 451 SET_AUDIOPORT_CONFIG, 452 DYN_POLICY_MIX_STATE_UPDATE, 453 RECORDING_CONFIGURATION_UPDATE, 454 SET_EFFECT_SUSPENDED, 455 AUDIO_MODULES_UPDATE, 456 }; 457 458 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service); 459 virtual ~AudioCommandThread(); 460 461 status_t dump(int fd); 462 463 // Thread virtuals 464 virtual void onFirstRef(); 465 virtual bool threadLoop(); 466 467 void exit(); 468 status_t volumeCommand(audio_stream_type_t stream, float volume, 469 audio_io_handle_t output, int delayMs = 0); 470 status_t parametersCommand(audio_io_handle_t ioHandle, 471 const char *keyValuePairs, int delayMs = 0); 472 status_t voiceVolumeCommand(float volume, int delayMs = 0); 473 void stopOutputCommand(audio_port_handle_t portId); 474 void releaseOutputCommand(audio_port_handle_t portId); 475 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0); 476 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0); 477 status_t createAudioPatchCommand(const struct audio_patch *patch, 478 audio_patch_handle_t *handle, 479 int delayMs); 480 status_t releaseAudioPatchCommand(audio_patch_handle_t handle, 481 int delayMs); 482 void updateAudioPortListCommand(); 483 void updateAudioPatchListCommand(); 484 void changeAudioVolumeGroupCommand(volume_group_t group, int flags); 485 status_t setAudioPortConfigCommand(const struct audio_port_config *config, 486 int delayMs); 487 void dynamicPolicyMixStateUpdateCommand(const String8& regId, 488 int32_t state); 489 void recordingConfigurationUpdateCommand( 490 int event, 491 const record_client_info_t *clientInfo, 492 const audio_config_base_t *clientConfig, 493 std::vector<effect_descriptor_t> clientEffects, 494 const audio_config_base_t *deviceConfig, 495 std::vector<effect_descriptor_t> effects, 496 audio_patch_handle_t patchHandle, 497 audio_source_t source); 498 void setEffectSuspendedCommand(int effectId, 499 audio_session_t sessionId, 500 bool suspended); 501 void audioModulesUpdateCommand(); 502 void insertCommand_l(AudioCommand *command, int delayMs = 0); 503 private: 504 class AudioCommandData; 505 506 // descriptor for requested tone playback event 507 class AudioCommand: public RefBase { 508 509 public: AudioCommand()510 AudioCommand() 511 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {} 512 513 void dump(char* buffer, size_t size); 514 515 int mCommand; // SET_VOLUME, SET_PARAMETERS... 516 nsecs_t mTime; // time stamp 517 Mutex mLock; // mutex associated to mCond 518 Condition mCond; // condition for status return 519 status_t mStatus; // command status 520 bool mWaitStatus; // true if caller is waiting for status 521 sp<AudioCommandData> mParam; // command specific parameter data 522 }; 523 524 class AudioCommandData: public RefBase { 525 public: ~AudioCommandData()526 virtual ~AudioCommandData() {} 527 protected: AudioCommandData()528 AudioCommandData() {} 529 }; 530 531 class VolumeData : public AudioCommandData { 532 public: 533 audio_stream_type_t mStream; 534 float mVolume; 535 audio_io_handle_t mIO; 536 }; 537 538 class ParametersData : public AudioCommandData { 539 public: 540 audio_io_handle_t mIO; 541 String8 mKeyValuePairs; 542 }; 543 544 class VoiceVolumeData : public AudioCommandData { 545 public: 546 float mVolume; 547 }; 548 549 class StopOutputData : public AudioCommandData { 550 public: 551 audio_port_handle_t mPortId; 552 }; 553 554 class ReleaseOutputData : public AudioCommandData { 555 public: 556 audio_port_handle_t mPortId; 557 }; 558 559 class CreateAudioPatchData : public AudioCommandData { 560 public: 561 struct audio_patch mPatch; 562 audio_patch_handle_t mHandle; 563 }; 564 565 class ReleaseAudioPatchData : public AudioCommandData { 566 public: 567 audio_patch_handle_t mHandle; 568 }; 569 570 class AudioVolumeGroupData : public AudioCommandData { 571 public: 572 volume_group_t mGroup; 573 int mFlags; 574 }; 575 576 class SetAudioPortConfigData : public AudioCommandData { 577 public: 578 struct audio_port_config mConfig; 579 }; 580 581 class DynPolicyMixStateUpdateData : public AudioCommandData { 582 public: 583 String8 mRegId; 584 int32_t mState; 585 }; 586 587 class RecordingConfigurationUpdateData : public AudioCommandData { 588 public: 589 int mEvent; 590 record_client_info_t mClientInfo; 591 struct audio_config_base mClientConfig; 592 std::vector<effect_descriptor_t> mClientEffects; 593 struct audio_config_base mDeviceConfig; 594 std::vector<effect_descriptor_t> mEffects; 595 audio_patch_handle_t mPatchHandle; 596 audio_source_t mSource; 597 }; 598 599 class SetEffectSuspendedData : public AudioCommandData { 600 public: 601 int mEffectId; 602 audio_session_t mSessionId; 603 bool mSuspended; 604 }; 605 606 Mutex mLock; 607 Condition mWaitWorkCV; 608 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands 609 sp<AudioCommand> mLastCommand; // last processed command (used by dump) 610 String8 mName; // string used by wake lock fo delayed commands 611 wp<AudioPolicyService> mService; 612 }; 613 614 class AudioPolicyClient : public AudioPolicyClientInterface 615 { 616 public: AudioPolicyClient(AudioPolicyService * service)617 explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {} ~AudioPolicyClient()618 virtual ~AudioPolicyClient() {} 619 620 // 621 // Audio HW module functions 622 // 623 624 // loads a HW module. 625 virtual audio_module_handle_t loadHwModule(const char *name); 626 627 // 628 // Audio output Control functions 629 // 630 631 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values 632 // in case the audio policy manager has no specific requirements for the output being opened. 633 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. 634 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. 635 virtual status_t openOutput(audio_module_handle_t module, 636 audio_io_handle_t *output, 637 audio_config_t *config, 638 const sp<DeviceDescriptorBase>& device, 639 uint32_t *latencyMs, 640 audio_output_flags_t flags); 641 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by 642 // a special mixer thread in the AudioFlinger. 643 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2); 644 // closes the output stream 645 virtual status_t closeOutput(audio_io_handle_t output); 646 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in 647 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. 648 virtual status_t suspendOutput(audio_io_handle_t output); 649 // restores a suspended output. 650 virtual status_t restoreOutput(audio_io_handle_t output); 651 652 // 653 // Audio input Control functions 654 // 655 656 // opens an audio input 657 virtual audio_io_handle_t openInput(audio_module_handle_t module, 658 audio_io_handle_t *input, 659 audio_config_t *config, 660 audio_devices_t *devices, 661 const String8& address, 662 audio_source_t source, 663 audio_input_flags_t flags); 664 // closes an audio input 665 virtual status_t closeInput(audio_io_handle_t input); 666 // 667 // misc control functions 668 // 669 670 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes 671 // for each output (destination device) it is attached to. 672 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0); 673 674 // invalidate a stream type, causing a reroute to an unspecified new output 675 virtual status_t invalidateStream(audio_stream_type_t stream); 676 677 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. 678 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); 679 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. 680 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); 681 682 // set down link audio volume. 683 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 684 685 // move effect to the specified output 686 virtual status_t moveEffects(audio_session_t session, 687 audio_io_handle_t srcOutput, 688 audio_io_handle_t dstOutput); 689 690 void setEffectSuspended(int effectId, 691 audio_session_t sessionId, 692 bool suspended) override; 693 694 /* Create a patch between several source and sink ports */ 695 virtual status_t createAudioPatch(const struct audio_patch *patch, 696 audio_patch_handle_t *handle, 697 int delayMs); 698 699 /* Release a patch */ 700 virtual status_t releaseAudioPatch(audio_patch_handle_t handle, 701 int delayMs); 702 703 /* Set audio port configuration */ 704 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs); 705 706 virtual void onAudioPortListUpdate(); 707 virtual void onAudioPatchListUpdate(); 708 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state); 709 virtual void onRecordingConfigurationUpdate(int event, 710 const record_client_info_t *clientInfo, 711 const audio_config_base_t *clientConfig, 712 std::vector<effect_descriptor_t> clientEffects, 713 const audio_config_base_t *deviceConfig, 714 std::vector<effect_descriptor_t> effects, 715 audio_patch_handle_t patchHandle, 716 audio_source_t source); 717 718 virtual void onAudioVolumeGroupChanged(volume_group_t group, int flags); 719 720 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use); 721 722 private: 723 AudioPolicyService *mAudioPolicyService; 724 }; 725 726 // --- Notification Client --- 727 class NotificationClient : public IBinder::DeathRecipient { 728 public: 729 NotificationClient(const sp<AudioPolicyService>& service, 730 const sp<IAudioPolicyServiceClient>& client, 731 uid_t uid, pid_t pid); 732 virtual ~NotificationClient(); 733 734 void onAudioPortListUpdate(); 735 void onAudioPatchListUpdate(); 736 void onDynamicPolicyMixStateUpdate(const String8& regId, 737 int32_t state); 738 void onAudioVolumeGroupChanged(volume_group_t group, int flags); 739 void onRecordingConfigurationUpdate( 740 int event, 741 const record_client_info_t *clientInfo, 742 const audio_config_base_t *clientConfig, 743 std::vector<effect_descriptor_t> clientEffects, 744 const audio_config_base_t *deviceConfig, 745 std::vector<effect_descriptor_t> effects, 746 audio_patch_handle_t patchHandle, 747 audio_source_t source); 748 void setAudioPortCallbacksEnabled(bool enabled); 749 void setAudioVolumeGroupCallbacksEnabled(bool enabled); 750 uid()751 uid_t uid() { 752 return mUid; 753 } 754 755 // IBinder::DeathRecipient 756 virtual void binderDied(const wp<IBinder>& who); 757 758 private: 759 NotificationClient(const NotificationClient&); 760 NotificationClient& operator = (const NotificationClient&); 761 762 const wp<AudioPolicyService> mService; 763 const uid_t mUid; 764 const pid_t mPid; 765 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient; 766 bool mAudioPortCallbacksEnabled; 767 bool mAudioVolumeGroupCallbacksEnabled; 768 }; 769 770 class AudioClient : public virtual RefBase { 771 public: AudioClient(const audio_attributes_t attributes,const audio_io_handle_t io,uid_t uid,pid_t pid,const audio_session_t session,const audio_port_handle_t deviceId)772 AudioClient(const audio_attributes_t attributes, 773 const audio_io_handle_t io, uid_t uid, pid_t pid, 774 const audio_session_t session, const audio_port_handle_t deviceId) : 775 attributes(attributes), io(io), uid(uid), pid(pid), 776 session(session), deviceId(deviceId), active(false) {} 777 ~AudioClient() override = default; 778 779 780 const audio_attributes_t attributes; // source, flags ... 781 const audio_io_handle_t io; // audio HAL stream IO handle 782 const uid_t uid; // client UID 783 const pid_t pid; // client PID 784 const audio_session_t session; // audio session ID 785 const audio_port_handle_t deviceId; // selected input device port ID 786 bool active; // Playback/Capture is active or inactive 787 }; 788 789 // --- AudioRecordClient --- 790 // Information about each registered AudioRecord client 791 // (between calls to getInputForAttr() and releaseInput()) 792 class AudioRecordClient : public AudioClient { 793 public: AudioRecordClient(const audio_attributes_t attributes,const audio_io_handle_t io,uid_t uid,pid_t pid,const audio_session_t session,const audio_port_handle_t deviceId,const String16 & opPackageName,bool canCaptureOutput,bool canCaptureHotword)794 AudioRecordClient(const audio_attributes_t attributes, 795 const audio_io_handle_t io, uid_t uid, pid_t pid, 796 const audio_session_t session, const audio_port_handle_t deviceId, 797 const String16& opPackageName, 798 bool canCaptureOutput, bool canCaptureHotword) : 799 AudioClient(attributes, io, uid, pid, session, deviceId), 800 opPackageName(opPackageName), startTimeNs(0), 801 canCaptureOutput(canCaptureOutput), canCaptureHotword(canCaptureHotword) {} 802 ~AudioRecordClient() override = default; 803 804 const String16 opPackageName; // client package name 805 nsecs_t startTimeNs; 806 const bool canCaptureOutput; 807 const bool canCaptureHotword; 808 }; 809 810 // --- AudioPlaybackClient --- 811 // Information about each registered AudioTrack client 812 // (between calls to getOutputForAttr() and releaseOutput()) 813 class AudioPlaybackClient : public AudioClient { 814 public: AudioPlaybackClient(const audio_attributes_t attributes,const audio_io_handle_t io,uid_t uid,pid_t pid,const audio_session_t session,audio_port_handle_t deviceId,audio_stream_type_t stream)815 AudioPlaybackClient(const audio_attributes_t attributes, 816 const audio_io_handle_t io, uid_t uid, pid_t pid, 817 const audio_session_t session, audio_port_handle_t deviceId, 818 audio_stream_type_t stream) : 819 AudioClient(attributes, io, uid, pid, session, deviceId), stream(stream) {} 820 ~AudioPlaybackClient() override = default; 821 822 const audio_stream_type_t stream; 823 }; 824 825 void getPlaybackClientAndEffects(audio_port_handle_t portId, 826 sp<AudioPlaybackClient>& client, 827 sp<AudioPolicyEffects>& effects, 828 const char *context); 829 830 831 // A class automatically clearing and restoring binder caller identity inside 832 // a code block (scoped variable) 833 // Declare one systematically before calling AudioPolicyManager methods so that they are 834 // executed with the same level of privilege as audioserver process. 835 class AutoCallerClear { 836 public: AutoCallerClear()837 AutoCallerClear() : 838 mToken(IPCThreadState::self()->clearCallingIdentity()) {} ~AutoCallerClear()839 ~AutoCallerClear() { 840 IPCThreadState::self()->restoreCallingIdentity(mToken); 841 } 842 843 private: 844 const int64_t mToken; 845 }; 846 847 // Internal dump utilities. 848 status_t dumpPermissionDenial(int fd); 849 850 851 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing 852 // device connection state or routing 853 // Note: lock acquisition order is always mLock > mEffectsLock: 854 // mLock protects AudioPolicyManager methods that can call into audio flinger 855 // and possibly back in to audio policy service and acquire mEffectsLock. 856 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread 857 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output 858 struct audio_policy_device *mpAudioPolicyDev; 859 struct audio_policy *mpAudioPolicy; 860 AudioPolicyInterface *mAudioPolicyManager; 861 AudioPolicyClient *mAudioPolicyClient; 862 863 DefaultKeyedVector< int64_t, sp<NotificationClient> > mNotificationClients; 864 Mutex mNotificationClientsLock; // protects mNotificationClients 865 // Manage all effects configured in audio_effects.conf 866 // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as 867 // those can call back into AudioPolicyService methods and try to acquire the mutex 868 sp<AudioPolicyEffects> mAudioPolicyEffects; 869 audio_mode_t mPhoneState; 870 871 sp<UidPolicy> mUidPolicy; 872 sp<SensorPrivacyPolicy> mSensorPrivacyPolicy; 873 874 DefaultKeyedVector< audio_port_handle_t, sp<AudioRecordClient> > mAudioRecordClients; 875 DefaultKeyedVector< audio_port_handle_t, sp<AudioPlaybackClient> > mAudioPlaybackClients; 876 877 MediaPackageManager mPackageManager; // To check allowPlaybackCapture 878 }; 879 880 } // namespace android 881 882 #endif // ANDROID_AUDIOPOLICYSERVICE_H 883