• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 <unordered_set>
24 #include <mutex>
25 #include "singleton.h"
26 #include "audio_group_handle.h"
27 #include "audio_manager_base.h"
28 #include "audio_policy_client_proxy.h"
29 #include "audio_policy_manager_factory.h"
30 #include "audio_stream_collector.h"
31 #include "audio_router_center.h"
32 #include "datashare_helper.h"
33 #include "ipc_skeleton.h"
34 #include "power_mgr_client.h"
35 #include "common_event_manager.h"
36 #ifdef FEATURE_DTMF_TONE
37 #include "audio_tone_parser.h"
38 #endif
39 
40 #include "device_status_listener.h"
41 #include "iaudio_policy_interface.h"
42 #include "iport_observer.h"
43 #include "audio_policy_parser_factory.h"
44 #include "audio_effect_service.h"
45 #include "audio_volume_config.h"
46 #include "policy_provider_stub.h"
47 #include "audio_device_manager.h"
48 #include "audio_device_parser.h"
49 #include "audio_state_manager.h"
50 #include "audio_pnp_server.h"
51 #include "audio_policy_server_handler.h"
52 #include "audio_affinity_manager.h"
53 #include "audio_ec_info.h"
54 
55 #ifdef BLUETOOTH_ENABLE
56 #include "audio_server_death_recipient.h"
57 #include "audio_bluetooth_manager.h"
58 #include "bluetooth_device_manager.h"
59 #endif
60 
61 #include "audio_a2dp_offload_manager.h"
62 #include "audio_iohandle_map.h"
63 #include "audio_router_map.h"
64 #include "audio_config_manager.h"
65 #include "audio_connected_device.h"
66 #include "audio_tone_manager.h"
67 #include "audio_microphone_descriptor.h"
68 #include "audio_active_device.h"
69 #include "audio_a2dp_device.h"
70 #include "audio_scene_manager.h"
71 #include "audio_offload_stream.h"
72 #include "audio_volume_manager.h"
73 #include "audio_ec_manager.h"
74 #include "audio_device_common.h"
75 #include "audio_recovery_device.h"
76 #include "audio_device_lock.h"
77 #include "audio_capturer_session.h"
78 #include "audio_device_status.h"
79 #include "audio_global_config_manager.h"
80 
81 namespace OHOS {
82 namespace AudioStandard {
83 
84 class AudioA2dpOffloadManager;
85 
86 class AudioPolicyService : public IDeviceStatusObserver, public IPolicyProvider {
87 public:
GetAudioPolicyService()88     static AudioPolicyService& GetAudioPolicyService()
89     {
90         static AudioPolicyService audioPolicyService;
91         return audioPolicyService;
92     }
93 
94     bool Init(void);
95     void Deinit(void);
96     void InitKVStore();
97     void SettingsDataReady();
98     bool ConnectServiceAdapter();
99 
100     void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status);
101 
102     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType) const;
103 
104     int32_t GetMinVolumeLevel(AudioVolumeType volumeType) const;
105 
106     int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel);
107 
108     int32_t SetSystemVolumeLevelWithDevice(AudioStreamType streamType, int32_t volumeLevel, DeviceType deviceType);
109 
110     int32_t SetAppVolumeLevel(int32_t appUid, int32_t volumeLevel);
111 
112     int32_t SetAppVolumeMuted(int32_t appUid, bool muted);
113 
114     bool IsAppVolumeMute(int32_t appUid, bool owned);
115 
116     int32_t GetSystemVolumeLevel(AudioStreamType streamType);
117 
118     int32_t GetAppVolumeLevel(int32_t appUid);
119 
120     int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType);
121 
122     float GetSystemVolumeDb(AudioStreamType streamType) const;
123 
124     int32_t SetLowPowerVolume(int32_t streamId, float volume) const;
125 
126     float GetLowPowerVolume(int32_t streamId) const;
127 
128     void HandlePowerStateChanged(PowerMgr::PowerState state);
129 
130     float GetSingleStreamVolume(int32_t streamId) const;
131 
132     int32_t SetStreamMute(AudioStreamType streamType, bool mute,
133         const StreamUsage &streamUsage = STREAM_USAGE_UNKNOWN,
134         const DeviceType &deviceType = DEVICE_TYPE_NONE);
135 
136     int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute) const;
137 
138     bool GetStreamMute(AudioStreamType streamType);
139 
140     bool IsStreamActive(AudioStreamType streamType) const;
141 
142     void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value);
143 
144     bool IsArmUsbDevice(const AudioDeviceDescriptor &desc);
145 
146     int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
147         std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
148 
149     std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType);
150 
151     int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
152         std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
153 
154     int32_t ExcludeOutputDevices(AudioDeviceUsage audioDevUsage,
155         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
156 
157     int32_t UnexcludeOutputDevices(AudioDeviceUsage audioDevUsage,
158         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors);
159 
160     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetExcludedDevices(
161         AudioDeviceUsage audioDevUsage);
162 
163     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag);
164 
165     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetDevicesInner(DeviceFlag deviceFlag);
166 
167     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetOutputDevice(sptr<AudioRendererFilter> audioRendererFilter);
168 
169     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter);
170     int32_t SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config);
171 
172     int32_t NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo, uint32_t sessionId);
173 
174     int32_t CloseWakeUpAudioCapturer();
175 
176     int32_t NotifyWakeUpCapturerRemoved();
177 
178     bool IsAbsVolumeSupported();
179 
180     int32_t SetDeviceActive(InternalDeviceType deviceType, bool active, const int32_t uid = INVALID_UID);
181 
182     bool IsDeviceActive(InternalDeviceType deviceType);
183 
184     DeviceType GetActiveOutputDevice();
185 
186     shared_ptr<AudioDeviceDescriptor> GetActiveOutputDeviceDescriptor();
187 
188     DeviceType GetActiveInputDevice();
189 
190     int32_t SetRingerMode(AudioRingerMode ringMode);
191 
192     AudioRingerMode GetRingerMode() const;
193 
194     int32_t SetMicrophoneMute(bool isMute);
195 
196     int32_t SetMicrophoneMutePersistent(const bool isMute);
197 
198     int32_t InitPersistentMicrophoneMuteState(bool &isMute);
199 
200     bool GetPersistentMicMuteState();
201 
202     bool IsMicrophoneMute();
203 
204     int32_t SetAudioScene(AudioScene audioScene, const int32_t uid = INVALID_UID, const int32_t pid = INVALID_PID);
205 
206     AudioScene GetAudioScene(bool hasSystemPermission = true) const;
207 
208     int32_t GetAudioLatencyFromXml() const;
209 
210     uint32_t GetSinkLatencyFromXml() const;
211 
212     int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo, const std::string &bundleName);
213 
214     int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo);
215 
216     int32_t SetSystemSoundUri(const std::string &key, const std::string &uri);
217 
218     std::string GetSystemSoundUri(const std::string &key);
219 
220     void SetNormalVoipFlag(const bool &normalVoipFlag);
221 
222     void OnUpdateAnahsSupport(std::string anahsShowType);
223 
224     int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName);
225 
226     void SetDisplayName(const std::string &deviceName, bool isLocalDevice);
227 
228     void SetDmDeviceType(const uint16_t dmDeviceType);
229 
230     bool IsDataShareReady();
231 
232     void SetDataShareReady(std::atomic<bool> isDataShareReady);
233 
234     void SetFirstScreenOn();
235 
236     int32_t ResumeStreamState();
237 
238     int32_t SetVirtualCall(const bool isVirtual);
239 
240     void GetAllSinkInputs(std::vector<SinkInput> &sinkInputs);
241 #ifdef FEATURE_DTMF_TONE
242     std::vector<int32_t> GetSupportedTones(const std::string &countryCode);
243 
244     std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype, const std::string &countryCode);
245 #endif
246     void OnDeviceStatusUpdated(DeviceType devType, bool isConnected,
247         const std::string &macAddress, const std::string &deviceName,
248         const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false);
249     void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
250 
251     void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
252 
253     void OnDeviceConfigurationChanged(DeviceType deviceType,
254         const std::string &macAddress, const std::string &deviceName,
255         const AudioStreamInfo &streamInfo);
256 
257     void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false);
258 
259     void OnServiceDisconnected(AudioServiceIndex serviceIndex);
260 
261     void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress);
262 
263     void OnMonoAudioConfigChanged(bool audioMono);
264 
265     void OnAudioBalanceChanged(float audioBalance);
266 
267     void LoadEffectLibrary();
268 
269     int32_t SetAudioStreamRemovedCallback(AudioStreamRemovedCallback *callback);
270 
271     void AddAudioPolicyClientProxyMap(int32_t clientPid, const sptr<IAudioPolicyClient>& cb);
272 
273     void ReduceAudioPolicyClientProxyMap(pid_t clientPid);
274 
275     int32_t RegisterAudioRendererEventListener(int32_t clientPid, const sptr<IRemoteObject> &object,
276         bool hasBTPermission, bool hasSysPermission);
277 
278     int32_t UnregisterAudioRendererEventListener(int32_t clientPid);
279 
280     int32_t RegisterAudioCapturerEventListener(int32_t clientPid, const sptr<IRemoteObject> &object,
281         bool hasBTPermission, bool hasSysPermission);
282 
283     int32_t UnregisterAudioCapturerEventListener(int32_t clientPid);
284 
285     int32_t SetAvailableDeviceChangeCallback(const int32_t clientId, const AudioDeviceUsage usage,
286         const sptr<IRemoteObject> &object, bool hasBTPermission);
287 
288     int32_t UnsetAvailableDeviceChangeCallback(const int32_t clientId, AudioDeviceUsage usage);
289 
290     int32_t SetQueryClientTypeCallback(const sptr<IRemoteObject> &object);
291 
292     int32_t SetAudioClientInfoMgrCallback(const sptr<IRemoteObject> &object);
293 
294     int32_t RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
295         const sptr<IRemoteObject> &object, const int32_t apiVersion);
296 
297     int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
298 
299     int32_t GetCurrentRendererChangeInfos(vector<shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos,
300         bool hasBTPermission, bool hasSystemPermission);
301 
302     int32_t GetCurrentCapturerChangeInfos(vector<shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos,
303         bool hasBTPermission, bool hasSystemPermission);
304 
305     void RegisteredTrackerClientDied(pid_t uid);
306 
307     int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType);
308 
309     int32_t UpdateStreamState(int32_t clientUid, StreamSetStateEventInternal &streamSetStateEventInternal);
310 
311     void RemoveDeviceForUid(int32_t uid);
312 
313     std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos();
314 
315     void SetParameterCallback(const std::shared_ptr<AudioParameterCallback>& callback);
316 
317     void RegiestPolicy();
318 
319     // override for IPolicyProvider
320     int32_t GetProcessDeviceInfo(const AudioProcessConfig &config, bool lockFlag, AudioDeviceDescriptor &deviceInfo);
321 
322     int32_t InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer);
323 
324     bool GetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume &vol);
325 
326 #ifdef BLUETOOTH_ENABLE
327     static void BluetoothServiceCrashedCallback(pid_t pid, pid_t uid);
328 #endif
329 
330     void RegisterBluetoothListener();
331 
332     void SubscribeAccessibilityConfigObserver();
333 
334     void RegisterRemoteDevStatusCallback();
335 
336     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(
337         AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID);
338 
339     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(
340         AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID);
341 
342     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescInner(
343         AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID);
344 
345     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescInner(
346         AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID);
347 
348     int32_t SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable);
349 
350     float GetMinStreamVolume(void);
351 
352     float GetMaxStreamVolume(void);
353 
354     int32_t GetMaxRendererInstances();
355 
356     void RegisterDataObserver();
357 
358     bool IsVolumeUnadjustable();
359 
360     void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos);
361 
362     float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType) const;
363 
364     int32_t QueryEffectManagerSceneMode(SupportedEffectConfig &supportedEffectConfig);
365 
366     void UpdateDescWhenNoBTPermission(vector<std::shared_ptr<AudioDeviceDescriptor>> &desc);
367 
368     int32_t GetHardwareOutputSamplingRate(const std::shared_ptr<AudioDeviceDescriptor> &desc);
369 
370     vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId);
371 
372     vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones();
373 
374     int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support);
375 
376     bool IsAbsVolumeScene() const;
377 
378     int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, bool internalCall = false);
379 
380     int32_t OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo, AudioStreamInfo streamInfo);
381 
382     void OnCapturerSessionRemoved(uint64_t sessionID);
383 
384     std::vector<shared_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage);
385 
386     void OffloadStreamSetCheck(uint32_t sessionId);
387 
388     void OffloadStreamReleaseCheck(uint32_t sessionId);
389 
390     int32_t OffloadStopPlaying(const std::vector<int32_t> &sessionIds);
391 
392     int32_t OffloadGetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp);
393 
394     int32_t GetAndSaveClientType(uint32_t uid, const std::string &bundleName);
395 
396     void ConfigDistributedRoutingRole(const std::shared_ptr<AudioDeviceDescriptor> descriptor, CastType type);
397 
398     DistributedRoutingInfo GetDistributedRoutingRoleInfo();
399 
400     void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command);
401 
402     void UpdateA2dpOffloadFlagBySpatialService(
403         const std::string& macAddress, std::unordered_map<uint32_t, bool> &sessionIDToSpatializationEnableMap);
404 
405     std::vector<std::shared_ptr<AudioDeviceDescriptor>> DeviceFilterByUsageInner(AudioDeviceUsage usage,
406         const std::vector<std::shared_ptr<AudioDeviceDescriptor>>& descs);
407 
408     int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address,
409         const int32_t uid = INVALID_UID);
410 
411     std::shared_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice();
412 
413     ConverterConfig GetConverterConfig();
414 
415     void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo,
416         const AudioStreamDeviceChangeReasonExt reason);
417 
418     void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo);
419 
420     float GetMaxAmplitude(const int32_t deviceId, const AudioInterrupt audioInterrupt);
421 
422     int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
423 
424     int32_t DisableSafeMediaVolume();
425 
426     void NotifyAccountsChanged(const int &id);
427 
428     int32_t ActivateConcurrencyFromServer(AudioPipeType incomingPipe);
429 
430     void CheckHibernateState(bool onHibernate);
431 
432     void UpdateSafeVolumeByS4();
433 
434     // for hidump
435     int32_t GetCurActivateCount();
436     void CheckStreamMode(const int64_t activateSessionId);
437 
438     int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType);
439     int32_t DynamicUnloadModule(const AudioPipeType pipeType);
440 
441     int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object);
442 
443     int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID);
444 
445     int32_t ActivateAudioConcurrency(const AudioPipeType &pipeType);
446 
447     int32_t ResetRingerModeMute();
448 
449     bool IsRingerModeMute();
450 
451     void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName);
452     // for effect V3
453     int32_t GetSupportedAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray);
454     int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray);
455     int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray);
456     // for effect
457     int32_t GetSupportedAudioEffectProperty(AudioEffectPropertyArray &propertyArray);
458     int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray);
459     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray);
460     // for enhance
461     int32_t GetSupportedAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray);
462     int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray);
463     int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray);
464     int32_t GetAudioEnhancePropertyByDevice(DeviceType deviceType, AudioEnhancePropertyArray &propertyArray);
465 
466     AudioScene GetLastAudioScene() const;
467     void SetRotationToEffect(const uint32_t rotate);
468     bool getFastControlParam();
469 
470     int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId);
471 
472     bool IsAllowedPlayback(const int32_t &uid, const int32_t &pid);
473 
474     bool IsCurrentActiveDeviceA2dp();
475 
476     int32_t SetVoiceRingtoneMute(bool isMute);
477 
478     int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID,
479         const StreamUsage streamUsage, bool isRunning);
480 
481     int32_t SetAudioDeviceAnahsCallback(const sptr<IRemoteObject> &object);
482 
483     int32_t UnsetAudioDeviceAnahsCallback();
484     void OnReceiveEvent(const EventFwk::CommonEventData &eventData);
485     void SubscribeSafeVolumeEvent();
486     int32_t NotifyCapturerRemoved(uint64_t sessionId);
487     void UpdateSpatializationSupported(const std::string macAddress, const bool support);
488 #ifdef HAS_FEATURE_INNERCAPTURER
489     int32_t LoadModernInnerCapSink(int32_t innerCapId);
490     int32_t UnloadModernInnerCapSink(int32_t innerCapId);
491 #endif
492     int32_t SetQueryAllowedPlaybackCallback(const sptr<IRemoteObject> &object);
493     void RestoreSession(const uint32_t &sessionID, RestoreInfo restoreInfo);
494     void CheckConnectedDevice();
495     void SetDeviceConnectedFlagFalseAfterDuration();
496 private:
AudioPolicyService()497     AudioPolicyService()
498         :audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
499         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
500         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
501         audioEffectService_(AudioEffectService::GetAudioEffectService()),
502         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
503         audioAffinityManager_(AudioAffinityManager::GetAudioAffinityManager()),
504         audioStateManager_(AudioStateManager::GetAudioStateManager()),
505         audioPolicyServerHandler_(DelayedSingleton<AudioPolicyServerHandler>::GetInstance()),
506 #ifdef AUDIO_WIRED_DETECT
507         audioPnpServer_(AudioPnpServer::GetAudioPnpServer()),
508 #endif
509         audioGlobalConfigManager_(AudioGlobalConfigManager::GetAudioGlobalConfigManager()),
510         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
511         audioRouteMap_(AudioRouteMap::GetInstance()),
512         audioConfigManager_(AudioConfigManager::GetInstance()),
513         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
514         audioToneManager_(AudioToneManager::GetInstance()),
515         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
516         audioActiveDevice_(AudioActiveDevice::GetInstance()),
517         audioA2dpDevice_(AudioA2dpDevice::GetInstance()),
518         audioSceneManager_(AudioSceneManager::GetInstance()),
519         audioOffloadStream_(AudioOffloadStream::GetInstance()),
520         audioVolumeManager_(AudioVolumeManager::GetInstance()),
521         audioEcManager_(AudioEcManager::GetInstance()),
522         audioDeviceCommon_(AudioDeviceCommon::GetInstance()),
523         audioRecoveryDevice_(AudioRecoveryDevice::GetInstance()),
524         audioCapturerSession_(AudioCapturerSession::GetInstance()),
525         audioDeviceLock_(AudioDeviceLock::GetInstance()),
526         audioDeviceStatus_(AudioDeviceStatus::GetInstance())
527 
528     {
529         deviceStatusListener_ = std::make_unique<DeviceStatusListener>(*this);
530     }
531 
532     ~AudioPolicyService();
533 
534     void GetSupportedEffectProperty(AudioEffectPropertyArrayV3 &propertyArray);
535     void GetSupportedEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray);
536     int32_t CheckSupportedAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray, const EffectFlag& flag);
537     int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray);
538 
539     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperInstance();
540 
541     void RegisterNameMonitorHelper();
542 
543     void RegisterAccessibilityMonitorHelper();
544 
545     void RegisterAccessiblilityBalance();
546 
547     void RegisterAccessiblilityMono();
548 
549     void StoreDistributedRoutingRoleInfo(const std::shared_ptr<AudioDeviceDescriptor> descriptor, CastType type);
550 
551     void ResetToSpeaker(DeviceType devType);
552 
553     PipeInfo& GetPipeInfoByPipeName(std::string &supportPipe, AudioAdapterInfo &adapterInfo);
554 
555     int32_t CheckDeviceCapability(AudioAdapterInfo &adapterInfo, int32_t flag, DeviceType deviceType);
556 
557     bool IsConfigInfoHasAttribute(std::list<ConfigInfo> &configInfos, std::string value);
558 
559     int32_t GetVoipDeviceInfo(const AudioProcessConfig &config, AudioDeviceDescriptor &deviceInfo, int32_t type,
560         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &preferredDeviceList);
561 
562     bool LoadAudioPolicyConfig();
563     void CreateRecoveryThread();
564     void RecoverExcludedOutputDevices();
565     void RecoveryPreferredDevices();
566 
567     void LoadHdiEffectModel();
568 
569     void UpdateEffectBtOffloadSupported(const bool &isSupported);
570 
571     bool IsA2dpOffloadConnected();
572 
573     void SetDefaultDeviceLoadFlag(bool isLoad);
574 
575     bool GetAudioEffectOffloadFlag();
576 
577     void OnServiceConnected(AudioServiceIndex serviceIndex);
578     int32_t GetUid(int32_t sessionId);
579 
580     void UnregisterBluetoothListener();
581 
582     int32_t OffloadStartPlaying(const std::vector<int32_t> &sessionIds);
583 
584     void SetA2dpOffloadFlag(BluetoothOffloadState state);
585     BluetoothOffloadState GetA2dpOffloadFlag();
586     void SetDefaultAdapterEnable(bool isEnable);
587 private:
588 
589     static bool isBtListenerRegistered;
590     static bool isBtCrashed;
591     bool isPnpDeviceConnected = false;
592     const int32_t G_UNKNOWN_PID = -1;
593     int32_t dAudioClientUid = 3055;
594     int32_t maxRendererInstances_ = 128;
595     bool isFastControlled_ = false;
596     static constexpr int32_t MIN_SERVICE_COUNT = 2;
597     std::bitset<MIN_SERVICE_COUNT> serviceFlag_;
598     std::mutex serviceFlagMutex_;
599 
600     IAudioPolicyInterface& audioPolicyManager_;
601 
602     AudioStreamCollector& streamCollector_;
603     AudioRouterCenter& audioRouterCenter_;
604     std::unique_ptr<DeviceStatusListener> deviceStatusListener_;
605 
606     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
607     AudioScene lastAudioScene_ = AUDIO_SCENE_DEFAULT;
608 
609     std::vector<DeviceType> outputPriorityList_ = {
610         DEVICE_TYPE_BLUETOOTH_SCO,
611         DEVICE_TYPE_BLUETOOTH_A2DP,
612         DEVICE_TYPE_DP,
613         DEVICE_TYPE_USB_HEADSET,
614         DEVICE_TYPE_WIRED_HEADSET,
615         DEVICE_TYPE_SPEAKER,
616         DEVICE_TYPE_HDMI
617     };
618     std::vector<DeviceType> inputPriorityList_ = {
619         DEVICE_TYPE_BLUETOOTH_SCO,
620         DEVICE_TYPE_BLUETOOTH_A2DP,
621         DEVICE_TYPE_USB_HEADSET,
622         DEVICE_TYPE_WIRED_HEADSET,
623         DEVICE_TYPE_WAKEUP,
624         DEVICE_TYPE_MIC
625     };
626 
627     AudioEffectService& audioEffectService_;
628 
629     bool isMicrophoneMuteTemporary_ = false;
630 
631     bool isMicrophoneMutePersistent_ = false;
632 
633     AudioDeviceManager &audioDeviceManager_;
634     AudioAffinityManager &audioAffinityManager_;
635     AudioStateManager &audioStateManager_;
636     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_;
637 #ifdef AUDIO_WIRED_DETECT
638     AudioPnpServer &audioPnpServer_;
639 #endif
640     AudioGlobalConfigManager &audioGlobalConfigManager_;
641     DistributedRoutingInfo distributedRoutingInfo_ = {
642         .descriptor = nullptr,
643         .type = CAST_TYPE_NULL
644     };
645 
646     static std::map<std::string, ClassType> classStrToEnum;
647 
648     SourceType currentSourceType = SOURCE_TYPE_MIC;
649     uint32_t currentRate = 0;
650     bool updateA2dpOffloadLogFlag = false;
651     std::mutex checkSpatializedMutex_;
652 
653     std::unique_ptr<std::thread> RecoveryDevicesThread_ = nullptr;
654 
655     std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr;
656 
657     AudioIOHandleMap& audioIOHandleMap_;
658     AudioRouteMap& audioRouteMap_;
659     AudioConfigManager& audioConfigManager_;
660     AudioConnectedDevice& audioConnectedDevice_;
661     AudioToneManager& audioToneManager_;
662     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
663     AudioActiveDevice& audioActiveDevice_;
664     AudioA2dpDevice& audioA2dpDevice_;
665     AudioSceneManager& audioSceneManager_;
666     AudioOffloadStream& audioOffloadStream_;
667     AudioVolumeManager& audioVolumeManager_;
668     AudioEcManager& audioEcManager_;
669     AudioDeviceCommon& audioDeviceCommon_;
670     AudioRecoveryDevice& audioRecoveryDevice_;
671 
672     AudioCapturerSession& audioCapturerSession_;
673     AudioDeviceLock& audioDeviceLock_;
674     AudioDeviceStatus& audioDeviceStatus_;
675 
676     sptr<IStandardAudioPolicyManagerListener> policyManagerListener_;
677 };
678 
679 class SafeVolumeEventSubscriber : public EventFwk::CommonEventSubscriber {
680 public:
SafeVolumeEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,std::function<void (const EventFwk::CommonEventData &)> receiver)681     explicit SafeVolumeEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
682         std::function<void(const EventFwk::CommonEventData&)> receiver)
683         : EventFwk::CommonEventSubscriber(subscribeInfo), eventReceiver_(receiver) {}
~SafeVolumeEventSubscriber()684     ~SafeVolumeEventSubscriber() {}
685     void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
686 private:
687     SafeVolumeEventSubscriber() = default;
688     std::function<void(const EventFwk::CommonEventData&)> eventReceiver_;
689 };
690 } // namespace AudioStandard
691 } // namespace OHOS
692 
693 #endif // ST_AUDIO_POLICY_SERVICE_H
694