1 /* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ST_AUDIO_POLICY_SERVER_H 17 #define ST_AUDIO_POLICY_SERVER_H 18 19 #include <mutex> 20 #include <pthread.h> 21 22 #include "singleton.h" 23 #include "system_ability_definition.h" 24 #include "ipc_skeleton.h" 25 #include "system_ability.h" 26 #include "iservice_registry.h" 27 28 #include "accesstoken_kit.h" 29 #include "perm_state_change_callback_customize.h" 30 #include "power_state_callback_stub.h" 31 #include "power_state_listener.h" 32 #include "common_event_subscriber.h" 33 #include "common_event_support.h" 34 35 #include "bundle_mgr_interface.h" 36 #include "bundle_mgr_proxy.h" 37 38 #include "audio_policy_service.h" 39 #include "audio_policy_utils.h" 40 #include "audio_stream_removed_callback.h" 41 #include "audio_interrupt_callback.h" 42 #include "audio_policy_manager_stub.h" 43 #include "audio_policy_client_proxy.h" 44 #include "audio_server_death_recipient.h" 45 #include "session_processor.h" 46 #include "audio_spatialization_service.h" 47 #include "audio_policy_server_handler.h" 48 #include "audio_interrupt_service.h" 49 #include "audio_device_manager.h" 50 #include "audio_policy_dump.h" 51 #include "app_state_listener.h" 52 53 namespace OHOS { 54 namespace AudioStandard { 55 56 class AudioPolicyService; 57 class AudioInterruptService; 58 class AudioPolicyServerHandler; 59 class AudioSessionService; 60 class BluetoothEventSubscriber; 61 62 class AudioPolicyServer : public SystemAbility, 63 public AudioPolicyManagerStub, 64 public AudioStreamRemovedCallback { 65 DECLARE_SYSTEM_ABILITY(AudioPolicyServer); 66 67 public: 68 DISALLOW_COPY_AND_MOVE(AudioPolicyServer); 69 70 enum DeathRecipientId { 71 TRACKER_CLIENT = 0, 72 LISTENER_CLIENT 73 }; 74 75 explicit AudioPolicyServer(int32_t systemAbilityId, bool runOnCreate = true); 76 ~AudioPolicyServer()77 virtual ~AudioPolicyServer() 78 { 79 AUDIO_WARNING_LOG("dtor should not happen"); 80 }; 81 82 void OnDump() override; 83 void OnStart() override; 84 void OnStop() override; 85 86 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType) override; 87 88 int32_t GetMinVolumeLevel(AudioVolumeType volumeType) override; 89 90 int32_t SetSystemVolumeLevelLegacy(AudioVolumeType volumeType, int32_t volumeLevel) override; 91 92 int32_t SetSystemVolumeLevel(AudioVolumeType volumeType, int32_t volumeLevel, int32_t volumeFlag = 0) override; 93 94 int32_t SetSystemVolumeLevelWithDevice(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType, 95 int32_t volumeFlag = 0) override; 96 97 int32_t SetAppVolumeLevel(int32_t appUid, int32_t volumeLevel, int32_t volumeFlag = 0) override; 98 99 bool IsAppVolumeMute(int32_t appUid, bool owned) override; 100 101 int32_t SetAppVolumeMuted(int32_t appUid, bool muted, int32_t volumeFlag = 0) override; 102 103 int32_t SetSelfAppVolumeLevel(int32_t volumeLevel, int32_t volumeFlag = 0) override; 104 105 AudioStreamType GetSystemActiveVolumeType(const int32_t clientUid) override; 106 107 int32_t GetSystemVolumeLevel(AudioStreamType streamType) override; 108 109 int32_t GetAppVolumeLevel(int32_t appUid) override; 110 111 int32_t GetSelfAppVolumeLevel() override; 112 113 int32_t SetLowPowerVolume(int32_t streamId, float volume) override; 114 115 float GetLowPowerVolume(int32_t streamId) override; 116 117 float GetSingleStreamVolume(int32_t streamId) override; 118 119 int32_t SetStreamMuteLegacy(AudioStreamType streamType, bool mute, 120 const DeviceType &deviceType = DEVICE_TYPE_NONE) override; 121 122 int32_t SetStreamMute(AudioStreamType streamType, bool mute, 123 const DeviceType &deviceType = DEVICE_TYPE_NONE) override; 124 125 bool GetStreamMute(AudioStreamType streamType) override; 126 127 bool IsStreamActive(AudioStreamType streamType) override; 128 129 bool IsVolumeUnadjustable() override; 130 131 int32_t AdjustVolumeByStep(VolumeAdjustType adjustType) override; 132 133 int32_t AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType) override; 134 135 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType) override; 136 137 bool IsArmUsbDevice(const AudioDeviceDescriptor &desc) override; 138 139 void MapExternalToInternalDeviceType(AudioDeviceDescriptor &desc) override; 140 141 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 142 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors) override; 143 144 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType) override; 145 146 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 147 std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors) override; 148 149 int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage, 150 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors) override; 151 152 int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage, 153 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors) override; 154 155 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(AudioDeviceUsage audioDevUsage) override; 156 157 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag) override; 158 159 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevicesInner(DeviceFlag deviceFlag) override; 160 161 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active, const int32_t uid = INVALID_UID) override; 162 163 bool IsDeviceActive(InternalDeviceType deviceType) override; 164 165 InternalDeviceType GetActiveOutputDevice() override; 166 167 InternalDeviceType GetActiveInputDevice() override; 168 169 int32_t SetRingerModeLegacy(AudioRingerMode ringMode) override; 170 171 int32_t SetRingerMode(AudioRingerMode ringMode) override; 172 173 #ifdef FEATURE_DTMF_TONE 174 std::vector<int32_t> GetSupportedTones(const std::string &countryCode) override; 175 176 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype, const std::string &countryCode) override; 177 #endif 178 179 AudioRingerMode GetRingerMode() override; 180 181 int32_t SetAudioScene(AudioScene audioScene) override; 182 183 int32_t SetMicrophoneMuteCommon(bool isMute, bool isLegacy); 184 185 int32_t SetMicrophoneMute(bool isMute) override; 186 187 int32_t SetMicrophoneMuteAudioConfig(bool isMute) override; 188 189 int32_t SetMicrophoneMutePersistent(const bool isMute, const PolicyType type) override; 190 191 bool GetPersistentMicMuteState() override; 192 193 bool IsMicrophoneMuteLegacy() override; 194 195 bool IsMicrophoneMute() override; 196 197 AudioScene GetAudioScene() override; 198 199 int32_t ActivateAudioSession(const AudioSessionStrategy &strategy) override; 200 201 int32_t DeactivateAudioSession() override; 202 203 bool IsAudioSessionActivated() override; 204 205 int32_t SetAudioInterruptCallback(const uint32_t sessionID, 206 const sptr<IRemoteObject> &object, uint32_t clientUid, const int32_t zoneId = 0) override; 207 208 int32_t UnsetAudioInterruptCallback(const uint32_t sessionID, const int32_t zoneId = 0) override; 209 210 int32_t ActivateAudioInterrupt(AudioInterrupt &audioInterrupt, const int32_t zoneId = 0, 211 const bool isUpdatedAudioStrategy = false) override; 212 213 int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt, const int32_t zoneId = 0) override; 214 215 int32_t SetAudioManagerInterruptCallback(const int32_t clientId, const sptr<IRemoteObject> &object) override; 216 217 int32_t UnsetAudioManagerInterruptCallback(const int32_t clientId) override; 218 219 int32_t SetQueryClientTypeCallback(const sptr<IRemoteObject> &object) override; 220 221 int32_t SetAudioClientInfoMgrCallback(const sptr<IRemoteObject> &object) override; 222 223 int32_t RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt) override; 224 225 int32_t AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt) override; 226 227 AudioStreamType GetStreamInFocus(const int32_t zoneId = 0) override; 228 229 AudioStreamType GetStreamInFocusByUid(const int32_t uid, const int32_t zoneId = 0) override; 230 231 int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt, const int32_t zoneId = 0) override; 232 233 void OnAudioStreamRemoved(const uint64_t sessionID) override; 234 235 void ProcessSessionRemoved(const uint64_t sessionID, const int32_t zoneId = 0); 236 237 void ProcessSessionAdded(SessionEvent sessionEvent); 238 239 void ProcessorCloseWakeupSource(const uint64_t sessionID); 240 241 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 242 243 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType) override; 244 245 int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo) override; 246 247 int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo) override; 248 249 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 250 const sptr<IRemoteObject> &object) override; 251 252 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo) override; 253 254 int32_t GetCurrentRendererChangeInfos( 255 std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override; 256 257 int32_t GetCurrentCapturerChangeInfos( 258 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos) override; 259 260 void RegisterClientDeathRecipient(const sptr<IRemoteObject> &object, DeathRecipientId id); 261 262 void RegisteredTrackerClientDied(int pid, int uid); 263 264 void RegisteredStreamListenerClientDied(int pid, int uid); 265 266 int32_t ResumeStreamState(); 267 268 int32_t UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState, 269 StreamUsage streamUsage) override; 270 271 int32_t GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos) override; 272 273 int32_t GetSupportedAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override; 274 int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray) override; 275 int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override; 276 277 int32_t GetSupportedAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override; 278 int32_t GetSupportedAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray) override; 279 int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray) override; 280 int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override; 281 int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray) override; 282 int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray) override; 283 284 int32_t GetNetworkIdByGroupId(int32_t groupId, std::string &networkId) override; 285 286 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors( 287 AudioRendererInfo &rendererInfo, bool forceNoBTPermission) override; 288 289 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors( 290 AudioCapturerInfo &captureInfo) override; 291 292 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetOutputDevice( 293 sptr<AudioRendererFilter> audioRendererFilter) override; 294 295 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetInputDevice( 296 sptr<AudioCapturerFilter> audioCapturerFilter) override; 297 298 int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable) override; 299 300 int32_t SetCallbackRendererInfo(const AudioRendererInfo &rendererInfo) override; 301 302 int32_t SetCallbackCapturerInfo(const AudioCapturerInfo &capturerInfo) override; 303 304 int32_t GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList, 305 const int32_t zoneId = 0) override; 306 307 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri) override; 308 309 std::string GetSystemSoundUri(const std::string &key) override; 310 311 float GetMinStreamVolume(void) override; 312 313 float GetMaxStreamVolume(void) override; 314 315 int32_t GetMaxRendererInstances() override; 316 317 void GetStreamVolumeInfoMap(StreamVolumeInfoMap& streamVolumeInfos); 318 319 int32_t QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig) override; 320 321 int32_t GetHardwareOutputSamplingRate(const std::shared_ptr<AudioDeviceDescriptor> &desc) override; 322 323 std::vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId) override; 324 325 std::vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones() override; 326 327 int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support) override; 328 329 bool IsAbsVolumeScene() override; 330 331 int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, const bool updateUi) override; 332 333 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage) override; 334 335 int32_t SetAvailableDeviceChangeCallback(const int32_t /*clientId*/, const AudioDeviceUsage usage, 336 const sptr<IRemoteObject> &object) override; 337 338 int32_t UnsetAvailableDeviceChangeCallback(const int32_t clientId, AudioDeviceUsage usage) override; 339 340 bool IsSpatializationEnabled() override; 341 342 bool IsSpatializationEnabled(const std::string address) override; 343 344 bool IsSpatializationEnabledForCurrentDevice() override; 345 346 int32_t SetSpatializationEnabled(const bool enable) override; 347 348 int32_t SetSpatializationEnabled(const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, 349 const bool enable) override; 350 351 bool IsHeadTrackingEnabled() override; 352 353 bool IsHeadTrackingEnabled(const std::string address) override; 354 355 int32_t SetHeadTrackingEnabled(const bool enable) override; 356 357 int32_t SetHeadTrackingEnabled( 358 const std::shared_ptr<AudioDeviceDescriptor> &selectedAudioDevice, const bool enable) override; 359 360 AudioSpatializationState GetSpatializationState(const StreamUsage streamUsage) override; 361 362 bool IsSpatializationSupported() override; 363 364 bool IsSpatializationSupportedForDevice(const std::string address) override; 365 366 bool IsHeadTrackingSupported() override; 367 368 bool IsHeadTrackingSupportedForDevice(const std::string address) override; 369 370 int32_t UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState) override; 371 372 int32_t RegisterSpatializationStateEventListener(const uint32_t sessionID, const StreamUsage streamUsage, 373 const sptr<IRemoteObject> &object) override; 374 375 int32_t ConfigDistributedRoutingRole( 376 const std::shared_ptr<AudioDeviceDescriptor> descriptor, CastType type) override; 377 378 int32_t SetDistributedRoutingRoleCallback(const sptr<IRemoteObject> &object) override; 379 380 int32_t UnsetDistributedRoutingRoleCallback() override; 381 382 int32_t UnregisterSpatializationStateEventListener(const uint32_t sessionID) override; 383 384 int32_t RegisterPolicyCallbackClient(const sptr<IRemoteObject> &object, const int32_t zoneId = 0) override; 385 386 int32_t CreateAudioInterruptZone(const std::set<int32_t> &pids, const int32_t zoneId) override; 387 388 int32_t AddAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneId) override; 389 390 int32_t RemoveAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneId) override; 391 392 int32_t ReleaseAudioInterruptZone(const int32_t zoneId) override; 393 394 int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address, 395 const int32_t uid = INVALID_UID) override; 396 397 std::shared_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice() override; 398 399 ConverterConfig GetConverterConfig() override; 400 401 void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo, 402 const AudioStreamDeviceChangeReasonExt reason) override; 403 404 void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo) override; 405 406 AudioSpatializationSceneType GetSpatializationSceneType() override; 407 408 int32_t SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType) override; 409 410 float GetMaxAmplitude(const int32_t deviceId) override; 411 412 int32_t DisableSafeMediaVolume() override; 413 414 bool IsHeadTrackingDataRequested(const std::string &macAddress) override; 415 416 int32_t SetAudioDeviceRefinerCallback(const sptr<IRemoteObject> &object) override; 417 418 int32_t UnsetAudioDeviceRefinerCallback() override; 419 420 int32_t TriggerFetchDevice( 421 AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN) override; 422 423 int32_t SetPreferredDevice(const PreferredType preferredType, 424 const std::shared_ptr<AudioDeviceDescriptor> &desc, const int32_t uid = INVALID_UID) override; 425 426 void SaveRemoteInfo(const std::string &networkId, DeviceType deviceType) override; 427 428 int32_t SetAudioDeviceAnahsCallback(const sptr<IRemoteObject> &object) override; 429 430 int32_t UnsetAudioDeviceAnahsCallback() override; 431 432 int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType) override; 433 434 int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object) override; 435 436 int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID) override; 437 438 int32_t ActivateAudioConcurrency(const AudioPipeType &pipeType) override; 439 440 int32_t InjectInterruption(const std::string networkId, InterruptEvent &event) override; 441 442 int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId) override; 443 444 bool IsAllowedPlayback(const int32_t &uid, const int32_t &pid) override; 445 446 int32_t SetVoiceRingtoneMute(bool isMute) override; 447 448 int32_t SetVirtualCall(const bool isVirtual) override; 449 450 int32_t SetDeviceConnectionStatus(const std::shared_ptr<AudioDeviceDescriptor> &desc, 451 const bool isConnected) override; 452 453 int32_t SetQueryAllowedPlaybackCallback(const sptr<IRemoteObject> &object) override; 454 455 void ProcessRemoteInterrupt(std::set<int32_t> sessionIds, InterruptEventInternal interruptEvent); 456 457 void SendVolumeKeyEventCbWithUpdateUiOrNot(AudioStreamType streamType, const bool& isUpdateUi = false); 458 void SendMuteKeyEventCbWithUpdateUiOrNot(AudioStreamType streamType, const bool& isUpdateUi = false); 459 void UpdateMuteStateAccordingToVolLevel(AudioStreamType streamType, int32_t volumeLevel, 460 bool mute, const bool& isUpdateUi = false); 461 462 void ProcUpdateRingerMode(); 463 uint32_t TranslateErrorCode(int32_t result); 464 465 class RemoteParameterCallback : public AudioParameterCallback { 466 public: 467 RemoteParameterCallback(sptr<AudioPolicyServer> server); 468 // AudioParameterCallback 469 void OnAudioParameterChange(const std::string networkId, const AudioParamKey key, const std::string& condition, 470 const std::string& value) override; 471 private: 472 sptr<AudioPolicyServer> server_; 473 void VolumeOnChange(const std::string networkId, const std::string& condition); 474 void InterruptOnChange(const std::string networkId, const std::string& condition); 475 void StateOnChange(const std::string networkId, const std::string& condition, const std::string& value); 476 }; 477 478 std::shared_ptr<RemoteParameterCallback> remoteParameterCallback_; 479 480 class PerStateChangeCbCustomizeCallback : public Security::AccessToken::PermStateChangeCallbackCustomize { 481 public: PerStateChangeCbCustomizeCallback(const Security::AccessToken::PermStateChangeScope & scopeInfo,sptr<AudioPolicyServer> server)482 explicit PerStateChangeCbCustomizeCallback(const Security::AccessToken::PermStateChangeScope &scopeInfo, 483 sptr<AudioPolicyServer> server) : PermStateChangeCallbackCustomize(scopeInfo), 484 ready_(false), server_(server) {} ~PerStateChangeCbCustomizeCallback()485 ~PerStateChangeCbCustomizeCallback() {} 486 487 void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result); 488 int32_t getUidByBundleName(std::string bundle_name, int user_id); 489 void UpdateMicPrivacyByCapturerState(bool targetMuteState, uint32_t targetTokenId, int32_t appUid); 490 491 bool ready_; 492 private: 493 sptr<AudioPolicyServer> server_; 494 }; 495 496 bool IsHighResolutionExist() override; 497 498 int32_t SetHighResolutionExist(bool highResExist) override; 499 500 void NotifyAccountsChanged(const int &id); 501 502 // for hidump 503 void AudioDevicesDump(std::string &dumpString); 504 void AudioModeDump(std::string &dumpString); 505 void AudioInterruptZoneDump(std::string &dumpString); 506 void AudioPolicyParserDump(std::string &dumpString); 507 void AudioVolumeDump(std::string &dumpString); 508 void AudioStreamDump(std::string &dumpString); 509 void OffloadStatusDump(std::string &dumpString); 510 void XmlParsedDataMapDump(std::string &dumpString); 511 void EffectManagerInfoDump(std::string &dumpString); 512 void MicrophoneMuteInfoDump(std::string &dumpString); 513 void AudioSessionInfoDump(std::string &dumpString); 514 515 // for hibernate callback 516 void CheckHibernateState(bool hibernate); 517 // for S4 reboot update safevolume 518 void UpdateSafeVolumeByS4(); 519 AppExecFwk::BundleInfo GetBundleInfoFromUid(int32_t callingUid); 520 521 void CheckConnectedDevice(); 522 void SetDeviceConnectedFlagFalseAfterDuration(); 523 524 protected: 525 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 526 void RegisterParamCallback(); 527 528 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 529 530 private: 531 friend class AudioInterruptService; 532 533 static constexpr int32_t MAX_VOLUME_LEVEL = 15; 534 static constexpr int32_t MIN_VOLUME_LEVEL = 0; 535 static constexpr int32_t VOLUME_CHANGE_FACTOR = 1; 536 static constexpr int32_t VOLUME_KEY_DURATION = 0; 537 static constexpr int32_t VOLUME_MUTE_KEY_DURATION = 0; 538 static constexpr int32_t MEDIA_SERVICE_UID = 1013; 539 static constexpr int32_t EDM_SERVICE_UID = 3057; 540 static constexpr char DAUDIO_DEV_TYPE_SPK = '1'; 541 static constexpr char DAUDIO_DEV_TYPE_MIC = '2'; 542 static constexpr int32_t AUDIO_UID = 1041; 543 static constexpr uint32_t MICPHONE_CALLER = 0; 544 static constexpr int32_t ROOT_UID = 0; 545 546 static const std::list<uid_t> RECORD_ALLOW_BACKGROUND_LIST; 547 static const std::list<uid_t> RECORD_PASS_APPINFO_LIST; 548 static constexpr const char* MICROPHONE_CONTROL_PERMISSION = "ohos.permission.MICROPHONE_CONTROL"; 549 550 class AudioPolicyServerPowerStateCallback : public PowerMgr::PowerStateCallbackStub { 551 public: 552 AudioPolicyServerPowerStateCallback(AudioPolicyServer *policyServer); 553 void OnAsyncPowerStateChanged(PowerMgr::PowerState state) override; 554 555 private: 556 AudioPolicyServer *policyServer_; 557 }; 558 559 int32_t VerifyVoiceCallPermission(uint64_t fullTokenId, Security::AccessToken::AccessTokenID tokenId); 560 561 // offload session 562 void OffloadStreamCheck(int64_t activateSessionId, int64_t deactivateSessionId); 563 void CheckSubscribePowerStateChange(); 564 void CheckStreamMode(const int64_t activateSessionId); 565 bool CheckAudioSessionStrategy(const AudioSessionStrategy &sessionStrategy); 566 567 // for audio volume and mute status 568 int32_t SetRingerModeInternal(AudioRingerMode inputRingerMode, bool hasUpdatedVolume = false); 569 int32_t SetSystemVolumeLevelInternal(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi); 570 int32_t SetAppVolumeLevelInternal(int32_t appUid, int32_t volumeLevel, bool isUpdateUi); 571 int32_t SetAppVolumeMutedInternal(int32_t appUid, bool muted, bool isUpdateUi); 572 int32_t SetSystemVolumeLevelWithDeviceInternal(AudioStreamType streamType, int32_t volumeLevel, 573 bool isUpdateUi, DeviceType deviceType); 574 int32_t SetSingleStreamVolume(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi, bool mute); 575 int32_t SetAppSingleStreamVolume(int32_t streamType, int32_t volumeLevel, bool isUpdateUi); 576 int32_t SetSingleStreamVolumeWithDevice(AudioStreamType streamType, int32_t volumeLevel, bool isUpdateUi, 577 bool mute, DeviceType deviceType); 578 AudioStreamType GetSystemActiveVolumeTypeInternal(const int32_t clientUid); 579 int32_t GetSystemVolumeLevelInternal(AudioStreamType streamType); 580 int32_t GetAppVolumeLevelInternal(int32_t appUid); 581 int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType); 582 float GetSystemVolumeDb(AudioStreamType streamType); 583 int32_t SetStreamMuteInternal(AudioStreamType streamType, bool mute, bool isUpdateUi, 584 const DeviceType &deviceType = DEVICE_TYPE_NONE); 585 void UpdateSystemMuteStateAccordingMusicState(AudioStreamType streamType, bool mute, bool isUpdateUi); 586 void ProcUpdateRingerModeForMute(bool updateRingerMode, bool mute); 587 int32_t SetSingleStreamMute(AudioStreamType streamType, bool mute, bool isUpdateUi, 588 const DeviceType &deviceType = DEVICE_TYPE_NONE); 589 bool GetStreamMuteInternal(AudioStreamType streamType); 590 bool IsVolumeTypeValid(AudioStreamType streamType); 591 bool IsVolumeLevelValid(AudioStreamType streamType, int32_t volumeLevel); 592 bool CheckCanMuteVolumeTypeByStep(AudioVolumeType volumeType, int32_t volumeLevel); 593 594 // Permission and privacy 595 bool VerifyPermission(const std::string &permission, uint32_t tokenId = 0, bool isRecording = false); 596 bool VerifyBluetoothPermission(); 597 int32_t OffloadStopPlaying(const AudioInterrupt &audioInterrupt); 598 int32_t SetAudioSceneInternal(AudioScene audioScene, const int32_t uid = INVALID_UID, 599 const int32_t pid = INVALID_PID); 600 601 int32_t GetApiTargerVersion(); 602 603 // externel function call 604 #ifdef FEATURE_MULTIMODALINPUT_INPUT 605 bool MaxOrMinVolumeOption(const int32_t &volLevel, const int32_t keyType, const AudioStreamType &streamInFocus); 606 int32_t RegisterVolumeKeyEvents(const int32_t keyType); 607 int32_t RegisterVolumeKeyMuteEvents(); 608 void SubscribeVolumeKeyEvents(); 609 int32_t ProcessVolumeKeyMuteEvents(const int32_t keyType); 610 #endif 611 void AddAudioServiceOnStart(); 612 void SubscribeOsAccountChangeEvents(); 613 void SubscribePowerStateChangeEvents(); 614 void SubscribeCommonEvent(const std::string event); 615 void OnReceiveEvent(const EventFwk::CommonEventData &eventData); 616 void HandleKvDataShareEvent(); 617 void InitMicrophoneMute(); 618 void InitKVStore(); 619 void SettingsDataReady(); 620 void ConnectServiceAdapter(); 621 void LoadEffectLibrary(); 622 void RegisterBluetoothListener(); 623 void SubscribeAccessibilityConfigObserver(); 624 void RegisterDataObserver(); 625 void RegisterPowerStateListener(); 626 void UnRegisterPowerStateListener(); 627 void RegisterSyncHibernateListener(); 628 void UnRegisterSyncHibernateListener(); 629 void RegisterAppStateListener(); 630 void AddRemoteDevstatusCallback(); 631 void OnDistributedRoutingRoleChange(const std::shared_ptr<AudioDeviceDescriptor> descriptor, const CastType type); 632 void SubscribeSafeVolumeEvent(); 633 void SubscribeCommonEventExecute(); 634 void SendMonitrtEvent(const int32_t keyType, int32_t resultOfVolumeKey); 635 636 void InitPolicyDumpMap(); 637 void PolicyDataDump(std::string &dumpString); 638 void ArgInfoDump(std::string &dumpString, std::queue<std::u16string> &argQue); 639 void InfoDumpHelp(std::string &dumpString); 640 641 int32_t SetRingerModeInner(AudioRingerMode ringMode); 642 void AddSystemAbilityListeners(); 643 void OnAddSystemAbilityExtract(int32_t systemAbilityId, const std::string& deviceId); 644 645 // for updating default device selection state when game audio stream is muted 646 void UpdateDefaultOutputDeviceWhenStarting(const uint32_t sessionID); 647 void UpdateDefaultOutputDeviceWhenStopping(const uint32_t sessionID); 648 void ChangeVolumeOnVoiceAssistant(AudioStreamType &streamInFocus); 649 650 AudioPolicyService& audioPolicyService_; 651 AudioPolicyUtils &audioPolicyUtils_; 652 AudioDeviceManager &audioDeviceManager_; 653 std::shared_ptr<AudioInterruptService> interruptService_; 654 655 int32_t volumeStep_; 656 std::atomic<bool> isFirstAudioServiceStart_ = false; 657 std::atomic<bool> isInitMuteState_ = false; 658 std::atomic<bool> isInitSettingsData_ = false; 659 std::atomic<bool> isScreenOffOrLock_ = false; 660 #ifdef FEATURE_MULTIMODALINPUT_INPUT 661 std::atomic<bool> hasSubscribedVolumeKeyEvents_ = false; 662 #endif 663 std::vector<pid_t> clientDiedListenerState_; 664 sptr<PowerStateListener> powerStateListener_; 665 sptr<SyncHibernateListener> syncHibernateListener_; 666 bool powerStateCallbackRegister_; 667 AppExecFwk::AppMgrClient appManager_; 668 sptr<AppStateListener> appStateListener_; 669 670 std::mutex systemVolumeMutex_; 671 std::mutex micStateChangeMutex_; 672 std::mutex clientDiedListenerStateMutex_; 673 std::mutex subscribeVolumeKey_; 674 675 SessionProcessor sessionProcessor_{ 676 [this] (const uint64_t sessionID, const int32_t zoneID) { this->ProcessSessionRemoved(sessionID, zoneID); }, 677 [this] (SessionEvent sessionEvent) { this->ProcessSessionAdded(sessionEvent); }, 678 [this] (const uint64_t sessionID) {this->ProcessorCloseWakeupSource(sessionID); }}; 679 680 AudioSpatializationService& audioSpatializationService_; 681 std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_; 682 bool volumeApplyToAll_ = false; 683 bool supportVibrator_ = false; 684 685 bool isHighResolutionExist_ = false; 686 std::mutex descLock_; 687 AudioRouterCenter &audioRouterCenter_; 688 using DumpFunc = void(AudioPolicyServer::*)(std::string &dumpString); 689 std::map<std::u16string, DumpFunc> dumpFuncMap; 690 pid_t lastMicMuteSettingPid_ = 0; 691 std::string GetBundleName(); 692 std::shared_ptr<AudioOsAccountInfo> accountObserver_ = nullptr; 693 AudioPolicyDump &audioPolicyDump_; 694 int32_t sessionIdByRemote_ = -1; 695 AudioActiveDevice& audioActiveDevice_; 696 }; 697 698 class AudioOsAccountInfo : public AccountSA::OsAccountSubscriber { 699 public: AudioOsAccountInfo(const AccountSA::OsAccountSubscribeInfo & subscribeInfo,AudioPolicyServer * audioPolicyServer)700 explicit AudioOsAccountInfo(const AccountSA::OsAccountSubscribeInfo &subscribeInfo, 701 AudioPolicyServer *audioPolicyServer) : AccountSA::OsAccountSubscriber(subscribeInfo), 702 audioPolicyServer_(audioPolicyServer) {} 703 ~AudioOsAccountInfo()704 ~AudioOsAccountInfo() 705 { 706 AUDIO_WARNING_LOG("Destructor AudioOsAccountInfo"); 707 } 708 OnAccountsChanged(const int & id)709 void OnAccountsChanged(const int &id) override 710 { 711 AUDIO_INFO_LOG("OnAccountsChanged received, id: %{public}d", id); 712 } 713 OnAccountsSwitch(const int & newId,const int & oldId)714 void OnAccountsSwitch(const int &newId, const int &oldId) override 715 { 716 CHECK_AND_RETURN_LOG(oldId >= LOCAL_USER_ID, "invalid id"); 717 AUDIO_INFO_LOG("OnAccountsSwitch received, newid: %{public}d, oldid: %{public}d", newId, oldId); 718 if (audioPolicyServer_ != nullptr) { 719 audioPolicyServer_->NotifyAccountsChanged(newId); 720 } 721 } 722 private: 723 static constexpr int32_t LOCAL_USER_ID = 100; 724 AudioPolicyServer *audioPolicyServer_; 725 }; 726 727 class AudioCommonEventSubscriber : public EventFwk::CommonEventSubscriber { 728 public: AudioCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,std::function<void (const EventFwk::CommonEventData &)> receiver)729 explicit AudioCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, 730 std::function<void(const EventFwk::CommonEventData&)> receiver) 731 : EventFwk::CommonEventSubscriber(subscribeInfo), eventReceiver_(receiver) {} ~AudioCommonEventSubscriber()732 ~AudioCommonEventSubscriber() {} 733 void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 734 private: 735 AudioCommonEventSubscriber() = default; 736 std::function<void(const EventFwk::CommonEventData&)> eventReceiver_; 737 }; 738 } // namespace AudioStandard 739 } // namespace OHOS 740 #endif // ST_AUDIO_POLICY_SERVER_H 741