1 /* 2 * Copyright (c) 2021-2023 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_SERVICE_H 17 #define ST_AUDIO_POLICY_SERVICE_H 18 19 #include <bitset> 20 #include <list> 21 #include <string> 22 #include <unordered_map> 23 #include <mutex> 24 #include <shared_mutex> 25 #include "audio_group_handle.h" 26 #include "audio_info.h" 27 #include "audio_manager_base.h" 28 #include "audio_policy_manager_factory.h" 29 #include "audio_stream_collector.h" 30 #include "ipc_skeleton.h" 31 #ifdef FEATURE_DTMF_TONE 32 #include "audio_tone_parser.h" 33 #endif 34 35 #ifdef ACCESSIBILITY_ENABLE 36 #include "accessibility_config_listener.h" 37 #else 38 #include "iaudio_accessibility_config_observer.h" 39 #endif 40 #include "device_status_listener.h" 41 #include "iaudio_policy_interface.h" 42 #include "iport_observer.h" 43 #include "parser_factory.h" 44 #include "audio_effect_manager.h" 45 #include "audio_volume_config.h" 46 #include "policy_provider_stub.h" 47 48 namespace OHOS { 49 namespace AudioStandard { 50 class AudioPolicyService : public IPortObserver, public IDeviceStatusObserver, 51 public IAudioAccessibilityConfigObserver, public IPolicyProvider { 52 public: GetAudioPolicyService()53 static AudioPolicyService& GetAudioPolicyService() 54 { 55 static AudioPolicyService audioPolicyService; 56 return audioPolicyService; 57 } 58 59 bool Init(void); 60 void Deinit(void); 61 void InitKVStore(); 62 bool ConnectServiceAdapter(); 63 64 const sptr<IStandardAudioService> GetAudioServerProxy(); 65 66 int32_t GetMaxVolumeLevel(AudioVolumeType volumeType) const; 67 68 int32_t GetMinVolumeLevel(AudioVolumeType volumeType) const; 69 70 int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel, bool isFromVolumeKey = false); 71 72 int32_t GetSystemVolumeLevel(AudioStreamType streamType, bool isFromVolumeKey = false) const; 73 74 float GetSystemVolumeDb(AudioStreamType streamType) const; 75 76 int32_t SetLowPowerVolume(int32_t streamId, float volume) const; 77 78 float GetLowPowerVolume(int32_t streamId) const; 79 80 float GetSingleStreamVolume(int32_t streamId) const; 81 82 int32_t SetStreamMute(AudioStreamType streamType, bool mute); 83 84 int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute) const; 85 86 bool GetStreamMute(AudioStreamType streamType) const; 87 88 bool IsStreamActive(AudioStreamType streamType) const; 89 90 void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value); 91 92 int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter, 93 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 94 95 std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType); 96 97 int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter, 98 std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors); 99 100 std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag); 101 102 int32_t SetWakeUpAudioCapturer(InternalAudioCapturerOptions options); 103 104 int32_t CloseWakeUpAudioCapturer(); 105 106 int32_t SetDeviceActive(InternalDeviceType deviceType, bool active); 107 108 bool IsDeviceActive(InternalDeviceType deviceType) const; 109 110 DeviceType GetActiveOutputDevice() const; 111 112 DeviceType GetActiveInputDevice() const; 113 114 int32_t SetRingerMode(AudioRingerMode ringMode); 115 116 bool IsAudioInterruptEnabled() const; 117 GetAudioFocusMap()118 auto& GetAudioFocusMap() const 119 { 120 return focusMap_; 121 } 122 123 AudioRingerMode GetRingerMode() const; 124 125 int32_t SetMicrophoneMute(bool isMute); 126 127 bool IsMicrophoneMute(); 128 129 int32_t SetAudioScene(AudioScene audioScene); 130 131 AudioScene GetAudioScene(bool hasSystemPermission = true) const; 132 133 int32_t GetAudioLatencyFromXml() const; 134 135 uint32_t GetSinkLatencyFromXml() const; 136 137 int32_t SetSystemSoundUri(const std::string &key, const std::string &uri); 138 139 std::string GetSystemSoundUri(const std::string &key); 140 141 bool IsSessionIdValid(int32_t callerUid, int32_t sessionId); 142 143 // Parser callbacks 144 void OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmldata); 145 146 void OnVolumeGroupParsed(std::unordered_map<std::string, std::string>& volumeGroupData); 147 148 void OnInterruptGroupParsed(std::unordered_map<std::string, std::string>& interruptGroupData); 149 150 void OnAudioInterruptEnable(bool enable); 151 152 void OnUpdateRouteSupport(bool isSupported); 153 154 int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName); 155 156 void SetDisplayName(const std::string &deviceName, bool isLocalDevice); 157 158 #ifdef FEATURE_DTMF_TONE 159 std::vector<int32_t> GetSupportedTones(); 160 161 std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype); 162 #endif 163 164 void OnDeviceStatusUpdated(DeviceType devType, bool isConnected, 165 const std::string &macAddress, const std::string &deviceName, 166 const AudioStreamInfo &streamInfo); 167 168 void OnPnpDeviceStatusUpdated(DeviceType devType, bool isConnected); 169 170 void OnDeviceConfigurationChanged(DeviceType deviceType, 171 const std::string &macAddress, const std::string &deviceName, 172 const AudioStreamInfo &streamInfo); 173 174 void OnDeviceStatusUpdated(DStatusInfo statusInfo); 175 176 void OnServiceConnected(AudioServiceIndex serviceIndex); 177 178 void OnServiceDisconnected(AudioServiceIndex serviceIndex); 179 180 void OnMonoAudioConfigChanged(bool audioMono); 181 182 void OnAudioBalanceChanged(float audioBalance); 183 184 void LoadEffectLibrary(); 185 186 int32_t SetAudioSessionCallback(AudioSessionCallback *callback); 187 188 int32_t SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag, const sptr<IRemoteObject> &object, 189 bool hasBTPermission); 190 191 int32_t UnsetDeviceChangeCallback(const int32_t clientId, DeviceFlag flag); 192 193 int32_t SetPreferredOutputDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object, 194 bool hasBTPermission); 195 196 int32_t SetPreferredInputDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object, 197 bool hasBTPermission); 198 199 int32_t UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId); 200 201 int32_t UnsetPreferredInputDeviceChangeCallback(const int32_t clientId); 202 203 int32_t RegisterAudioRendererEventListener(int32_t clientPid, const sptr<IRemoteObject> &object, 204 bool hasBTPermission, bool hasSysPermission); 205 206 int32_t UnregisterAudioRendererEventListener(int32_t clientPid); 207 208 int32_t RegisterAudioCapturerEventListener(int32_t clientPid, const sptr<IRemoteObject> &object, 209 bool hasBTPermission, bool hasSysPermission); 210 211 int32_t UnregisterAudioCapturerEventListener(int32_t clientPid); 212 213 int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, 214 const sptr<IRemoteObject> &object); 215 216 int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 217 218 int32_t GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos, 219 bool hasBTPermission, bool hasSystemPermission); 220 221 int32_t GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos, 222 bool hasBTPermission, bool hasSystemPermission); 223 224 void RegisteredTrackerClientDied(pid_t pid); 225 226 void RegisteredStreamListenerClientDied(pid_t pid); 227 228 int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType); 229 230 void OnAudioLatencyParsed(uint64_t latency); 231 232 void OnSinkLatencyParsed(uint32_t latency); 233 234 int32_t UpdateStreamState(int32_t clientUid, StreamSetStateEventInternal &streamSetStateEventInternal); 235 236 DeviceType GetDeviceTypeFromPin(AudioPin pin); 237 238 std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos(); 239 240 void SetParameterCallback(const std::shared_ptr<AudioParameterCallback>& callback); 241 242 void RegiestPolicy(); 243 244 // override for IPolicyProvider 245 int32_t GetProcessDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo); 246 247 int32_t InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer); 248 249 bool GetSharedVolume(AudioStreamType streamType, DeviceType deviceType, Volume &vol); 250 251 bool SetSharedVolume(AudioStreamType streamType, DeviceType deviceType, Volume vol); 252 253 #ifdef BLUETOOTH_ENABLE 254 static void BluetoothServiceCrashedCallback(pid_t pid); 255 #endif 256 257 void RegisterBluetoothListener(); 258 259 void UnregisterBluetoothListener(); 260 261 void SubscribeAccessibilityConfigObserver(); 262 263 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(AudioRendererInfo &rendererInfo, 264 std::string networkId = LOCAL_NETWORK_ID); 265 266 std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(AudioCapturerInfo &captureInfo, 267 std::string networkId = LOCAL_NETWORK_ID); 268 269 void GetEffectManagerInfo(OriginalEffectConfig& oriEffectConfig, std::vector<Effect>& availableEffects); 270 271 float GetMinStreamVolume(void); 272 273 float GetMaxStreamVolume(void); 274 275 int32_t GetMaxRendererInstances(); 276 277 void RegisterDataObserver(); 278 279 bool IsVolumeUnadjustable(); 280 281 void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos); 282 283 float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType) const; 284 285 std::string GetLocalDevicesType(); 286 287 int32_t QueryEffectManagerSceneMode(SupportedEffectConfig &supportedEffectConfig); 288 289 void UpdateDescWhenNoBTPermission(vector<sptr<AudioDeviceDescriptor>> &desc); 290 291 int32_t SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config); 292 293 void UnloadLoopback(); 294 295 void UpdateOutputDeviceSelectedByCalling(DeviceType deviceType); 296 297 private: AudioPolicyService()298 AudioPolicyService() 299 :audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), 300 configParser_(ParserFactory::GetInstance().CreateParser(*this)), 301 streamCollector_(AudioStreamCollector::GetAudioStreamCollector()), 302 audioEffectManager_(AudioEffectManager::GetAudioEffectManager()) 303 { 304 #ifdef ACCESSIBILITY_ENABLE 305 accessibilityConfigListener_ = std::make_shared<AccessibilityConfigListener>(*this); 306 #endif 307 deviceStatusListener_ = std::make_unique<DeviceStatusListener>(*this); 308 } 309 310 ~AudioPolicyService(); 311 312 std::string GetPortName(InternalDeviceType deviceType); 313 314 int32_t RememberRoutingInfo(sptr<AudioRendererFilter> audioRendererFilter, 315 sptr<AudioDeviceDescriptor> deviceDescriptor); 316 317 int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds, 318 sptr<AudioDeviceDescriptor> localDeviceDescriptor); 319 320 int32_t MoveToRemoteOutputDevice(std::vector<SinkInput> sinkInputIds, 321 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 322 323 int32_t MoveToLocalInputDevice(std::vector<uint32_t> sourceOutputIds, 324 sptr<AudioDeviceDescriptor> localDeviceDescriptor); 325 326 int32_t MoveToRemoteInputDevice(std::vector<uint32_t> sourceOutputIds, 327 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 328 329 AudioModuleInfo ConstructRemoteAudioModuleInfo(std::string networkId, 330 DeviceRole deviceRole, DeviceType deviceType); 331 332 AudioModuleInfo ConstructWakeUpAudioModuleInfo(int32_t wakeupNo); 333 334 AudioIOHandle GetAudioIOHandle(InternalDeviceType deviceType); 335 336 int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType, 337 sptr<AudioDeviceDescriptor> remoteDeviceDescriptor); 338 339 InternalDeviceType GetDeviceType(const std::string &deviceName); 340 341 std::string GetGroupName(const std::string& deviceName, const GroupType type); 342 343 bool IsDeviceConnected(sptr<AudioDeviceDescriptor> &audioDeviceDescriptors) const; 344 345 int32_t DeviceParamsCheck(DeviceRole targetRole, 346 std::vector<sptr<AudioDeviceDescriptor>> &audioDeviceDescriptors) const; 347 348 bool IsInputDevice(DeviceType deviceType) const; 349 350 bool IsOutputDevice(DeviceType deviceType) const; 351 352 DeviceRole GetDeviceRole(DeviceType deviceType) const; 353 354 DeviceRole GetDeviceRole(const std::string &role); 355 356 int32_t SelectNewDevice(DeviceRole deviceRole, DeviceType deviceType); 357 358 int32_t HandleA2dpDevice(DeviceType deviceType); 359 360 int32_t HandleFileDevice(DeviceType deviceType); 361 362 int32_t ActivateNewDevice(DeviceType deviceType, bool isSceneActivation); 363 364 DeviceRole GetDeviceRole(AudioPin pin) const; 365 366 void KeepPortMute(int32_t muteDuration, std::string portName, DeviceType deviceType); 367 368 int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote); 369 370 DeviceType FetchHighPriorityDevice(bool isOutputDevice); 371 372 void UpdateConnectedDevicesWhenConnecting(const AudioDeviceDescriptor& deviceDescriptor, 373 std::vector<sptr<AudioDeviceDescriptor>>& desc); 374 375 void UpdateConnectedDevicesWhenDisconnecting(const AudioDeviceDescriptor& deviceDescriptor, 376 std::vector<sptr<AudioDeviceDescriptor>> &desc); 377 378 void TriggerDeviceChangedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection); 379 380 std::vector<sptr<AudioDeviceDescriptor>> DeviceFilterByFlag(DeviceFlag flag, 381 const std::vector<sptr<AudioDeviceDescriptor>>& desc); 382 383 void WriteDeviceChangedSysEvents(const std::vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected); 384 385 bool GetActiveDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo); 386 387 bool IsConfigurationUpdated(DeviceType deviceType, const AudioStreamInfo &streamInfo); 388 389 void UpdateInputDeviceInfo(DeviceType deviceType); 390 391 void UpdateStreamChangeDeviceInfo(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo); 392 393 void UpdateTrackerDeviceChange(const vector<sptr<AudioDeviceDescriptor>> &desc); 394 395 void UpdateGroupInfo(GroupType type, std::string groupName, int32_t& groupId, std::string networkId, 396 bool connected, int32_t mappingId); 397 398 void AddAudioDevice(AudioModuleInfo& moduleInfo, InternalDeviceType devType); 399 400 void OnPreferredOutputDeviceUpdated(DeviceType devType, std::string networkId); 401 402 void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId); 403 404 void OnPreferredDeviceUpdated(DeviceType activeOutputDevice, DeviceType activeInputDevice); 405 406 std::vector<sptr<AudioDeviceDescriptor>> GetDevicesForGroup(GroupType type, int32_t groupId); 407 408 void SetEarpieceState(); 409 410 void SetVolumeForSwitchDevice(DeviceType deviceType); 411 412 void SetVoiceCallVolume(int32_t volume); 413 414 void RemoveDeviceInRouterMap(std::string networkId); 415 416 void UpdateDisplayName(sptr<AudioDeviceDescriptor> deviceDescriptor); 417 418 void RegisterRemoteDevStatusCallback(); 419 420 void UpdateLocalGroupInfo(bool isConnected, const std::string& macAddress, 421 const std::string& deviceName, const AudioStreamInfo& streamInfo, AudioDeviceDescriptor& deviceDesc); 422 423 int32_t HandleLocalDeviceConnected(DeviceType devType, const std::string& macAddress, const std::string& deviceName, 424 const AudioStreamInfo& streamInfo); 425 426 int32_t HandleLocalDeviceDisconnected(DeviceType devType, const std::string& macAddress); 427 428 void UpdateEffectDefaultSink(DeviceType deviceType); 429 430 void LoadEffectSinks(); 431 432 void LoadSinksForCapturer(); 433 434 void LoadInnerCapturerSink(); 435 436 void LoadReceiverSink(); 437 438 void LoadLoopback(); 439 440 DeviceType FindConnectedHeadset(); 441 442 bool CreateDataShareHelperInstance(); 443 444 void RegisterNameMonitorHelper(); 445 446 bool interruptEnabled_ = true; 447 bool isUpdateRouteSupported_ = true; 448 bool isCurrentRemoteRenderer = false; 449 bool remoteCapturerSwitch_ = false; 450 bool isOpenRemoteDevice = false; 451 bool isBtListenerRegistered = false; 452 bool isPnpDeviceConnected = false; 453 bool hasModulesLoaded = false; 454 const int32_t G_UNKNOWN_PID = -1; 455 int32_t dAudioClientUid = 3055; 456 int32_t maxRendererInstances_ = 16; 457 uint64_t audioLatencyInMsec_ = 50; 458 uint32_t sinkLatencyInMsec_ {0}; 459 460 std::bitset<MIN_SERVICE_COUNT> serviceFlag_; 461 std::mutex serviceFlagMutex_; 462 DeviceType effectActiveDevice_ = DEVICE_TYPE_NONE; 463 DeviceType currentActiveDevice_ = DEVICE_TYPE_NONE; 464 DeviceType activeInputDevice_ = DEVICE_TYPE_NONE; 465 DeviceType pnpDevice_ = DEVICE_TYPE_NONE; 466 std::string localDevicesType_ = ""; 467 468 std::mutex routerMapMutex_; // unordered_map is not concurrently-secure 469 std::mutex preferredInputMapMutex_; 470 std::unordered_map<int32_t, std::pair<std::string, int32_t>> routerMap_; 471 IAudioPolicyInterface& audioPolicyManager_; 472 Parser& configParser_; 473 #ifdef FEATURE_DTMF_TONE 474 std::unordered_map<int32_t, std::shared_ptr<ToneInfo>> toneDescriptorMap; 475 #endif 476 AudioStreamCollector& streamCollector_; 477 #ifdef ACCESSIBILITY_ENABLE 478 std::shared_ptr<AccessibilityConfigListener> accessibilityConfigListener_; 479 #endif 480 std::unique_ptr<DeviceStatusListener> deviceStatusListener_; 481 std::vector<sptr<AudioDeviceDescriptor>> connectedDevices_; 482 std::unordered_map<std::string, AudioStreamInfo> connectedA2dpDeviceMap_; 483 std::string activeBTDevice_; 484 485 std::map<std::pair<int32_t, DeviceFlag>, sptr<IStandardAudioPolicyManagerListener>> deviceChangeCbsMap_; 486 std::unordered_map<int32_t, sptr<IStandardAudioRoutingManagerListener>> preferredOutputDeviceCbsMap_; 487 std::unordered_map<int32_t, sptr<IStandardAudioRoutingManagerListener>> preferredInputDeviceCbsMap_; 488 489 AudioScene audioScene_ = AUDIO_SCENE_DEFAULT; 490 std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> focusMap_ = {}; 491 std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo_ = {}; 492 493 std::mutex ioHandlesMutex_; 494 std::unordered_map<std::string, AudioIOHandle> IOHandles_ = {}; 495 496 std::shared_ptr<AudioSharedMemory> policyVolumeMap_ = nullptr; 497 volatile Volume *volumeVector_ = nullptr; 498 499 std::vector<DeviceType> outputPriorityList_ = { 500 DEVICE_TYPE_BLUETOOTH_SCO, 501 DEVICE_TYPE_BLUETOOTH_A2DP, 502 DEVICE_TYPE_USB_HEADSET, 503 DEVICE_TYPE_WIRED_HEADSET, 504 DEVICE_TYPE_SPEAKER 505 }; 506 std::vector<DeviceType> inputPriorityList_ = { 507 DEVICE_TYPE_BLUETOOTH_SCO, 508 DEVICE_TYPE_BLUETOOTH_A2DP, 509 DEVICE_TYPE_USB_HEADSET, 510 DEVICE_TYPE_WIRED_HEADSET, 511 DEVICE_TYPE_WAKEUP, 512 DEVICE_TYPE_MIC 513 }; 514 515 std::vector<sptr<VolumeGroupInfo>> volumeGroups_; 516 std::vector<sptr<InterruptGroupInfo>> interruptGroups_; 517 std::unordered_map<std::string, std::string> volumeGroupData_; 518 std::unordered_map<std::string, std::string> interruptGroupData_; 519 AudioEffectManager& audioEffectManager_; 520 521 std::mutex outputDeviceSelectedByCallingMutex_; 522 std::unordered_map<decltype(IPCSkeleton::GetCallingUid()), DeviceType> outputDeviceSelectedByCalling_; 523 524 bool isMicrophoneMute_ = false; 525 526 int wakeupCount_ = 0; 527 std::mutex wakeupCountMutex_; 528 529 std::mutex deviceClassInfoMutex_; 530 531 std::shared_mutex deviceStatusUpdateSharedMutex_; 532 }; 533 } // namespace AudioStandard 534 } // namespace OHOS 535 536 #endif // ST_AUDIO_POLICY_SERVICE_H 537