• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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_info.h"
28 #include "audio_manager_base.h"
29 #include "audio_policy_client_proxy.h"
30 #include "audio_policy_manager_factory.h"
31 #include "audio_stream_collector.h"
32 #include "audio_router_center.h"
33 #include "datashare_helper.h"
34 #include "ipc_skeleton.h"
35 #include "power_mgr_client.h"
36 #ifdef FEATURE_DTMF_TONE
37 #include "audio_tone_parser.h"
38 #endif
39 
40 #ifdef ACCESSIBILITY_ENABLE
41 #include "accessibility_config_listener.h"
42 #else
43 #include "iaudio_accessibility_config_observer.h"
44 #endif
45 #include "device_status_listener.h"
46 #include "iaudio_policy_interface.h"
47 #include "iport_observer.h"
48 #include "audio_policy_parser_factory.h"
49 #include "audio_effect_manager.h"
50 #include "audio_volume_config.h"
51 #include "policy_provider_stub.h"
52 #include "audio_device_manager.h"
53 #include "audio_device_parser.h"
54 #include "audio_state_manager.h"
55 #include "audio_pnp_server.h"
56 #include "audio_policy_server_handler.h"
57 
58 #ifdef BLUETOOTH_ENABLE
59 #include "audio_server_death_recipient.h"
60 #include "audio_bluetooth_manager.h"
61 #include "bluetooth_device_manager.h"
62 #endif
63 
64 namespace OHOS {
65 namespace AudioStandard {
66 enum A2dpOffloadConnectionState : int32_t {
67     CONNECTION_STATUS_DISCONNECTED = 0,
68     CONNECTION_STATUS_CONNECTING = 1,
69     CONNECTION_STATUS_CONNECTED = 2,
70     CONNECTION_STATUS_TIMEOUT = 3,
71 };
72 
73 class AudioA2dpOffloadManager;
74 
75 class AudioPolicyService : public IPortObserver, public IDeviceStatusObserver,
76     public IAudioAccessibilityConfigObserver, public IPolicyProvider {
77 public:
GetAudioPolicyService()78     static AudioPolicyService& GetAudioPolicyService()
79     {
80         static AudioPolicyService audioPolicyService;
81         return audioPolicyService;
82     }
83 
84     bool Init(void);
85     void Deinit(void);
86     void InitKVStore();
87     bool ConnectServiceAdapter();
88 
89     void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status);
90 
91     void OnBlockedStatusUpdated(DeviceType devType, DeviceBlockStatus status);
92 
93     void TriggerMicrophoneBlockedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &desc,
94         DeviceBlockStatus status);
95 
96     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType) const;
97 
98     int32_t GetMinVolumeLevel(AudioVolumeType volumeType) const;
99 
100     int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel);
101 
102     int32_t SelectDealSafeVolume(AudioStreamType streamType, int32_t volumeLevel);
103 
104     int32_t GetSystemVolumeLevel(AudioStreamType streamType);
105 
106     float GetSystemVolumeDb(AudioStreamType streamType) const;
107 
108     int32_t SetLowPowerVolume(int32_t streamId, float volume) const;
109 
110     float GetLowPowerVolume(int32_t streamId) const;
111 
112     void HandlePowerStateChanged(PowerMgr::PowerState state);
113 
114     float GetSingleStreamVolume(int32_t streamId) const;
115 
116     int32_t SetStreamMute(AudioStreamType streamType, bool mute,
117         const StreamUsage &streamUsage = STREAM_USAGE_UNKNOWN);
118 
119     int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute) const;
120 
121     bool GetStreamMute(AudioStreamType streamType);
122 
123     bool IsStreamActive(AudioStreamType streamType) const;
124 
125     void NotifyRemoteRenderState(std::string networkId, std::string condition, std::string value);
126 
127     void NotifyUserSelectionEventToBt(sptr<AudioDeviceDescriptor> audioDeviceDescriptor);
128 
129     bool IsArmUsbDevice(const AudioDeviceDescriptor &desc);
130 
131     int32_t SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
132         std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
133     int32_t SelectFastOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
134         sptr<AudioDeviceDescriptor> deviceDescriptor);
135 
136     std::string GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType);
137 
138     int32_t SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
139         std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors);
140 
141     std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag);
142 
143     std::vector<sptr<AudioDeviceDescriptor>> GetDevicesInner(DeviceFlag deviceFlag);
144 
145     int32_t SetWakeUpAudioCapturer(InternalAudioCapturerOptions options);
146 
147     int32_t SetWakeUpAudioCapturerFromAudioServer(const AudioProcessConfig &config);
148 
149     int32_t NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo, uint32_t sessionId);
150 
151     int32_t CloseWakeUpAudioCapturer();
152 
153     int32_t NotifyWakeUpCapturerRemoved();
154 
155     bool IsAbsVolumeSupported();
156 
157     int32_t SetDeviceActive(InternalDeviceType deviceType, bool active);
158 
159     bool IsDeviceActive(InternalDeviceType deviceType);
160 
161     DeviceType GetActiveOutputDevice();
162 
163     unique_ptr<AudioDeviceDescriptor> GetActiveOutputDeviceDescriptor();
164 
165     DeviceType GetActiveInputDevice();
166 
167     int32_t SetRingerMode(AudioRingerMode ringMode);
168 
169     AudioRingerMode GetRingerMode() const;
170 
171     int32_t SetMicrophoneMute(bool isMute);
172 
173     int32_t SetMicrophoneMutePersistent(const bool isMute);
174 
175     int32_t InitPersistentMicrophoneMuteState(bool &isMute);
176 
177     bool GetPersistentMicMuteState();
178 
179     bool IsMicrophoneMute();
180 
181     int32_t SetAudioScene(AudioScene audioScene);
182 
183     AudioScene GetAudioScene(bool hasSystemPermission = true) const;
184 
185     int32_t GetAudioLatencyFromXml() const;
186 
187     uint32_t GetSinkLatencyFromXml() const;
188 
189     int32_t GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo, const std::string &bundleName);
190 
191     int32_t GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo);
192 
193     int32_t SetSystemSoundUri(const std::string &key, const std::string &uri);
194 
195     std::string GetSystemSoundUri(const std::string &key);
196 
197     void SetNormalVoipFlag(const bool &normalVoipFlag);
198 
199     int32_t GetVoipRendererFlag(const std::string &sinkPortName, const std::string &networkId,
200         const AudioSamplingRate &samplingRate);
201 
202     bool GetVoipConfig();
203 
204     // Audio Policy Parser callbacks
205     void OnAudioPolicyXmlParsingCompleted(const std::unordered_map<AdaptersType, AudioAdapterInfo> adapterInfoMap);
206 
207     // Parser callbacks
208     void OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmldata);
209 
210     void OnVolumeGroupParsed(std::unordered_map<std::string, std::string>& volumeGroupData);
211 
212     void OnInterruptGroupParsed(std::unordered_map<std::string, std::string>& interruptGroupData);
213 
214     void OnGlobalConfigsParsed(GlobalConfigs &globalConfigs);
215 
216     void OnVoipConfigParsed(bool enableFastVoip);
217 
218     void OnUpdateRouteSupport(bool isSupported);
219 
220     int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName);
221 
222     void SetDisplayName(const std::string &deviceName, bool isLocalDevice);
223 
224     bool IsDataShareReady();
225 #ifdef FEATURE_DTMF_TONE
226     std::vector<int32_t> GetSupportedTones();
227 
228     std::shared_ptr<ToneInfo> GetToneConfig(int32_t ltonetype);
229 #endif
230 
231     void OnDeviceStatusUpdated(DeviceType devType, bool isConnected,
232         const std::string &macAddress, const std::string &deviceName,
233         const AudioStreamInfo &streamInfo);
234     void OnDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
235 
236     void OnPnpDeviceStatusUpdated(DeviceType devType, bool isConnected);
237 
238     void OnPnpDeviceStatusUpdated(DeviceType devType, bool isConnected,
239         const std::string &name, const std::string &adderess);
240 
241     void OnDeviceConfigurationChanged(DeviceType deviceType,
242         const std::string &macAddress, const std::string &deviceName,
243         const AudioStreamInfo &streamInfo);
244 
245     void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false);
246 
247     void checkOffloadAvailable(AudioModuleInfo& moduleInfo);
248 
249     void OnServiceDisconnected(AudioServiceIndex serviceIndex);
250 
251     void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress);
252 
253     void OnMonoAudioConfigChanged(bool audioMono);
254 
255     void OnAudioBalanceChanged(float audioBalance);
256 
257     void LoadEffectLibrary();
258 
259     int32_t SetAudioStreamRemovedCallback(AudioStreamRemovedCallback *callback);
260 
261     void AddAudioPolicyClientProxyMap(int32_t clientPid, const sptr<IAudioPolicyClient>& cb);
262 
263     void ReduceAudioPolicyClientProxyMap(pid_t clientPid);
264 
265     int32_t SetPreferredOutputDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object,
266         bool hasBTPermission);
267 
268     int32_t SetPreferredInputDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object,
269         bool hasBTPermission);
270 
271     int32_t UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId);
272 
273     int32_t UnsetPreferredInputDeviceChangeCallback(const int32_t clientId);
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 RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
293         const sptr<IRemoteObject> &object, const int32_t apiVersion);
294 
295     int32_t UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
296 
297     int32_t GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos,
298         bool hasBTPermission, bool hasSystemPermission);
299 
300     int32_t GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos,
301         bool hasBTPermission, bool hasSystemPermission);
302 
303     void RegisteredTrackerClientDied(pid_t uid);
304 
305     int32_t ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType);
306 
307     void OnAudioLatencyParsed(uint64_t latency);
308 
309     void OnSinkLatencyParsed(uint32_t latency);
310 
311     int32_t UpdateStreamState(int32_t clientUid, StreamSetStateEventInternal &streamSetStateEventInternal);
312 
313     DeviceType GetDeviceTypeFromPin(AudioPin pin);
314 
315     std::vector<sptr<VolumeGroupInfo>> GetVolumeGroupInfos();
316 
317     void SetParameterCallback(const std::shared_ptr<AudioParameterCallback>& callback);
318 
319     void RegiestPolicy();
320 
321     // override for IPolicyProvider
322     int32_t GetProcessDeviceInfo(const AudioProcessConfig &config, bool lockFlag, DeviceInfo &deviceInfo);
323 
324     int32_t InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer);
325 
326     bool GetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume &vol);
327 
328 #ifdef BLUETOOTH_ENABLE
329     static void BluetoothServiceCrashedCallback(pid_t pid);
330 #endif
331 
332     void RegisterBluetoothListener();
333 
334     void SubscribeAccessibilityConfigObserver();
335 
336     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescriptors(AudioRendererInfo &rendererInfo,
337         std::string networkId = LOCAL_NETWORK_ID);
338 
339     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescriptors(AudioCapturerInfo &captureInfo,
340         std::string networkId = LOCAL_NETWORK_ID);
341 
342     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescInner(AudioRendererInfo &rendererInfo,
343         std::string networkId = LOCAL_NETWORK_ID);
344 
345     std::vector<sptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescInner(AudioCapturerInfo &captureInfo,
346         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     int32_t CheckMaxRendererInstances();
357 
358     void RegisterDataObserver();
359 
360     bool IsVolumeUnadjustable();
361 
362     void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos);
363 
364     float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType) const;
365 
366     int32_t QueryEffectManagerSceneMode(SupportedEffectConfig &supportedEffectConfig);
367 
368     void UpdateDescWhenNoBTPermission(vector<sptr<AudioDeviceDescriptor>> &desc);
369 
370     int32_t SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config);
371 
372     int32_t SetCaptureSilentState(bool state);
373 
374     int32_t GetHardwareOutputSamplingRate(const sptr<AudioDeviceDescriptor> &desc);
375 
376     vector<sptr<MicrophoneDescriptor>> GetAudioCapturerMicrophoneDescriptors(int32_t sessionId);
377 
378     vector<sptr<MicrophoneDescriptor>> GetAvailableMicrophones();
379 
380     int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support);
381 
382     bool IsAbsVolumeScene() const;
383 
384     bool IsVgsVolumeSupported() const;
385 
386     int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, bool internalCall = false);
387 
388     int32_t OnCapturerSessionAdded(uint64_t sessionID, SessionInfo sessionInfo, AudioStreamInfo streamInfo);
389 
390     void OnCapturerSessionRemoved(uint64_t sessionID);
391 
392     std::vector<unique_ptr<AudioDeviceDescriptor>> GetAvailableDevices(AudioDeviceUsage usage);
393 
394     void TriggerAvailableDeviceChangedCallback(const vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected);
395 
396     void OffloadStreamSetCheck(uint32_t sessionId);
397 
398     void OffloadStreamReleaseCheck(uint32_t sessionId);
399 
400     void RemoteOffloadStreamRelease(uint32_t sessionId);
401 
402     int32_t OffloadStopPlaying(const std::vector<int32_t> &sessionIds);
403 
404     int32_t OffloadGetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp);
405 
406     int32_t GetAndSaveClientType(uint32_t uid, const std::string &bundleName);
407 
408     void GetA2dpOffloadCodecAndSendToDsp();
409 
410     int32_t HandleA2dpDeviceInOffload(BluetoothOffloadState a2dpOffloadFlag);
411 
412     int32_t HandleA2dpDeviceOutOffload(BluetoothOffloadState a2dpOffloadFlag);
413 
414     void ConfigDistributedRoutingRole(const sptr<AudioDeviceDescriptor> descriptor, CastType type);
415 
416     DistributedRoutingInfo GetDistributedRoutingRoleInfo();
417 
418     void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command);
419 
420     void DeviceUpdateClearRecongnitionStatus(AudioDeviceDescriptor &desc);
421 
422     void UpdateA2dpOffloadFlagBySpatialService(
423         const std::string& macAddress, std::unordered_map<uint32_t, bool> &sessionIDToSpatializationEnableMap);
424 
425     std::vector<sptr<AudioDeviceDescriptor>> DeviceFilterByUsageInner(AudioDeviceUsage usage,
426         const std::vector<sptr<AudioDeviceDescriptor>>& descs);
427 
428     int32_t SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address);
429 
430     std::unique_ptr<AudioDeviceDescriptor> GetActiveBluetoothDevice();
431 
432     ConverterConfig GetConverterConfig();
433 
434     void FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo,
435         const AudioStreamDeviceChangeReasonExt reason);
436 
437     void FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo);
438 
439     float GetMaxAmplitude(const int32_t deviceId);
440 
441     int32_t TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
442 
443     int32_t DisableSafeMediaVolume();
444 
445     int32_t SafeVolumeDialogDisapper();
446 
447     void NotifyAccountsChanged(const int &id);
448 
449     int32_t ActivateConcurrencyFromServer(AudioPipeType incomingPipe);
450 
451     // for hidump
452     void DevicesInfoDump(std::string &dumpString);
453     void AudioModeDump(std::string &dumpString);
454     void AudioPolicyParserDump(std::string &dumpString);
455     void XmlParsedDataMapDump(std::string &dumpString);
456     void StreamVolumesDump(std::string &dumpString);
457     void DeviceVolumeInfosDump(std::string &dumpString, DeviceVolumeInfoMap &deviceVolumeInfos);
458     void AudioStreamDump(std::string &dumpString);
459     void GetVolumeConfigDump(std::string &dumpString);
460     void GetGroupInfoDump(std::string &dumpString);
461     void GetCallStatusDump(std::string &dumpString);
462     void GetRingerModeDump(std::string &dumpString);
463     void GetMicrophoneDescriptorsDump(std::string &dumpString);
464     void GetCapturerStreamDump(std::string &dumpString);
465     void GetSafeVolumeDump(std::string &dumpString);
466     void GetOffloadStatusDump(std::string &dumpString);
467     void EffectManagerInfoDump(std::string &dumpString);
468     void MicrophoneMuteInfoDump(std::string &dumpString);
469 
470     int32_t GetCurActivateCount();
471     void CheckStreamMode(const int64_t activateSessionId);
472 
473     int32_t MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType);
474     int32_t DynamicUnloadModule(const AudioPipeType pipeType);
475 
476     int32_t SetAudioConcurrencyCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object);
477 
478     int32_t UnsetAudioConcurrencyCallback(const uint32_t sessionID);
479 
480     int32_t ActivateAudioConcurrency(const AudioPipeType &pipeType);
481 
482     void OnReceiveBluetoothEvent(const std::string macAddress, const std::string deviceName);
483 
484     int32_t ResetRingerModeMute();
485 
486     bool IsRingerModeMute();
487 
488     AudioScene GetLastAudioScene() const;
489 
490     void SetRotationToEffect(const uint32_t rotate);
491     void FetchStreamForA2dpOffload(const bool &requireReset);
492     void UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state);
493     bool getFastControlParam();
494 
495     int32_t SetVoiceRingtoneMute(bool isMute);
496 
497     int32_t SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID,
498         const StreamUsage streamUsage, bool isRunning);
499 
500     int32_t LoadSplitModule(const std::string &splitArgs, const std::string &networkId);
501 private:
AudioPolicyService()502     AudioPolicyService()
503         :audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
504         audioPolicyConfigParser_(AudioPolicyParserFactory::GetInstance().CreateParser(*this)),
505         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
506         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
507         audioEffectManager_(AudioEffectManager::GetAudioEffectManager()),
508         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
509         audioStateManager_(AudioStateManager::GetAudioStateManager()),
510         audioPolicyServerHandler_(DelayedSingleton<AudioPolicyServerHandler>::GetInstance()),
511         audioPnpServer_(AudioPnpServer::GetAudioPnpServer())
512     {
513 #ifdef ACCESSIBILITY_ENABLE
514         accessibilityConfigListener_ = std::make_shared<AccessibilityConfigListener>(*this);
515 #endif
516         deviceStatusListener_ = std::make_unique<DeviceStatusListener>(*this);
517     }
518 
519     ~AudioPolicyService();
520 
521     void UpdateDeviceInfo(DeviceInfo &deviceInfo, const sptr<AudioDeviceDescriptor> &desc, bool hasBTPermission,
522         bool hasSystemPermission);
523 
524     std::string GetSinkPortName(InternalDeviceType deviceType, AudioPipeType pipeType = PIPE_TYPE_UNKNOWN);
525 
526     std::string GetSourcePortName(InternalDeviceType deviceType);
527 
528     int32_t RememberRoutingInfo(sptr<AudioRendererFilter> audioRendererFilter,
529         sptr<AudioDeviceDescriptor> deviceDescriptor);
530 
531     int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds,
532         sptr<AudioDeviceDescriptor> localDeviceDescriptor);
533 
534     std::vector<SinkInput> FilterSinkInputs(sptr<AudioRendererFilter> audioRendererFilter, bool moveAll);
535 
536     std::vector<SinkInput> FilterSinkInputs(int32_t sessionId);
537 
538     std::vector<SourceOutput> FilterSourceOutputs(int32_t sessionId);
539 
540     int32_t MoveToRemoteOutputDevice(std::vector<SinkInput> sinkInputIds,
541         sptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
542 
543     int32_t MoveToLocalInputDevice(std::vector<SourceOutput> sourceOutputIds,
544         sptr<AudioDeviceDescriptor> localDeviceDescriptor);
545 
546     int32_t MoveToRemoteInputDevice(std::vector<SourceOutput> sourceOutputIds,
547         sptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
548 
549     AudioModuleInfo ConstructRemoteAudioModuleInfo(std::string networkId,
550         DeviceRole deviceRole, DeviceType deviceType);
551 
552     bool FillWakeupStreamPropInfo(const AudioStreamInfo &streamInfo, PipeInfo *pipeInfo,
553         AudioModuleInfo &audioModuleInfo);
554     bool ConstructWakeupAudioModuleInfo(const AudioStreamInfo &streamInfo, AudioModuleInfo &audioModuleInfo);
555 
556     AudioIOHandle GetSinkIOHandle(InternalDeviceType deviceType);
557 
558     AudioIOHandle GetSourceIOHandle(InternalDeviceType deviceType);
559 
560     int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType,
561         sptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
562 
563     InternalDeviceType GetDeviceType(const std::string &deviceName);
564 
565     std::string GetGroupName(const std::string& deviceName, const GroupType type);
566 
567     bool IsDeviceConnected(sptr<AudioDeviceDescriptor> &audioDeviceDescriptors) const;
568 
569     int32_t DeviceParamsCheck(DeviceRole targetRole,
570         std::vector<sptr<AudioDeviceDescriptor>> &audioDeviceDescriptors) const;
571 
572     DeviceRole GetDeviceRole(DeviceType deviceType) const;
573 
574     DeviceRole GetDeviceRole(const std::string &role);
575 
576     int32_t SwitchActiveA2dpDevice(const sptr<AudioDeviceDescriptor> &deviceDescriptor);
577 
578     int32_t HandleActiveDevice(DeviceType deviceType);
579 
580     int32_t LoadA2dpModule();
581 
582     int32_t LoadUsbModule(string deviceInfo, DeviceRole deviceRole);
583 
584     int32_t LoadDpModule(string deviceInfo);
585 
586     int32_t LoadDefaultUsbModule(DeviceRole deviceRole);
587 
588     int32_t RehandlePnpDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address);
589 
590     int32_t HandleArmUsbDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address);
591 
592     int32_t HandleDpDevice(DeviceType deviceType, const std::string &address);
593 
594     int32_t GetModuleInfo(ClassType classType, std::string &moduleInfoStr);
595 
596     void MoveToNewOutputDevice(unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
597         vector<std::unique_ptr<AudioDeviceDescriptor>> &outputDevices,
598         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
599 
600     void MoveToNewInputDevice(unique_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
601         unique_ptr<AudioDeviceDescriptor> &inputDevice);
602 
603     int32_t SetRenderDeviceForUsage(StreamUsage streamUsage, sptr<AudioDeviceDescriptor> desc);
604 
605     void SetCaptureDeviceForUsage(AudioScene scene, SourceType srcType, sptr<AudioDeviceDescriptor> desc);
606 
607     DeviceRole GetDeviceRole(AudioPin pin) const;
608 
609     void UnmutePortAfterMuteDuration(int32_t muteDuration, std::string portName, DeviceType deviceType);
610 
611     int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote);
612 
613     int32_t HandleScoOutputDeviceFetched(unique_ptr<AudioDeviceDescriptor> &desc,
614         vector<unique_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
615         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
616 
617     void FetchOutputDevice(vector<unique_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
618         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
619 
620     bool IsFastFromA2dpToA2dp(const std::unique_ptr<AudioDeviceDescriptor> &desc,
621         const std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
622         const AudioStreamDeviceChangeReasonExt reason);
623 
624     void FetchStreamForA2dpMchStream(std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
625         vector<std::unique_ptr<AudioDeviceDescriptor>> &descs);
626 
627     int32_t HandleScoInputDeviceFetched(unique_ptr<AudioDeviceDescriptor> &desc,
628         vector<unique_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos);
629 
630     void FetchInputDevice(vector<unique_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos,
631         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
632 
633     int32_t HandleDeviceChangeForFetchInputDevice(unique_ptr<AudioDeviceDescriptor> &desc,
634         unique_ptr<AudioCapturerChangeInfo> &capturerChangeInfo);
635 
636     void BluetoothScoFetch(unique_ptr<AudioDeviceDescriptor> &desc,
637         vector<unique_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos, SourceType sourceType);
638 
639     void BluetoothScoDisconectForRecongnition();
640 
641     void FetchDevice(bool isOutputDevice = true,
642         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
643 
644     void UpdateConnectedDevicesWhenConnecting(const AudioDeviceDescriptor& updatedDesc,
645         std::vector<sptr<AudioDeviceDescriptor>>& descForCb);
646 
647     void UpdateConnectedDevicesWhenDisconnecting(const AudioDeviceDescriptor& updatedDesc,
648         std::vector<sptr<AudioDeviceDescriptor>> &descForCb);
649 
650     void TriggerDeviceChangedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection);
651 
652     void GetAllRunningStreamSession(std::vector<int32_t> &allSessions, bool doStop = false);
653 
654     void WriteDeviceChangedSysEvents(const std::vector<sptr<AudioDeviceDescriptor>> &desc, bool isConnected);
655 
656     void WriteOutDeviceChangedSysEvents(const sptr<AudioDeviceDescriptor> &deviceDescriptor,
657         const SinkInput &sinkInput);
658 
659     void WriteInDeviceChangedSysEvents(const sptr<AudioDeviceDescriptor> &deviceDescriptor,
660         const SourceOutput &sourceOutput);
661 
662     void WriteSelectOutputSysEvents(const std::vector<sptr<AudioDeviceDescriptor>> &selectedDesc,
663     StreamUsage strUsage);
664 
665     void WriteSelectInputSysEvents(const std::vector<sptr<AudioDeviceDescriptor>> &selectedDesc,
666         SourceType srcType, AudioScene scene);
667 
668     bool GetActiveDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo);
669 
670     bool IsConfigurationUpdated(DeviceType deviceType, const AudioStreamInfo &streamInfo);
671 
672     void UpdateInputDeviceInfo(DeviceType deviceType);
673 
674     void UpdateTrackerDeviceChange(const vector<sptr<AudioDeviceDescriptor>> &desc);
675 
676     void UpdateGroupInfo(GroupType type, std::string groupName, int32_t& groupId, std::string networkId,
677         bool connected, int32_t mappingId);
678 
679     void AddAudioDevice(AudioModuleInfo& moduleInfo, InternalDeviceType devType);
680 
681     void OnPreferredOutputDeviceUpdated(const AudioDeviceDescriptor& deviceDescriptor);
682 
683     void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId);
684 
685     void OnPreferredDeviceUpdated(const AudioDeviceDescriptor& deviceDescriptor, DeviceType activeInputDevice);
686 
687     std::vector<sptr<AudioDeviceDescriptor>> GetDevicesForGroup(GroupType type, int32_t groupId);
688 
689     void SetVolumeForSwitchDevice(DeviceType deviceType, const std::string &newSinkName = PORT_NONE);
690 
691     void UpdateVolumeForLowLatency();
692 
693     void SetVoiceCallVolume(int32_t volume);
694 
695     std::string GetVolumeGroupType(DeviceType deviceType);
696 
697     int32_t ReloadA2dpAudioPort(AudioModuleInfo &moduleInfo, const AudioStreamInfo& audioStreamInfo);
698 
699     void RemoveDeviceInRouterMap(std::string networkId);
700 
701     void RemoveDeviceInFastRouterMap(std::string networkId);
702 
703     void UpdateDisplayName(sptr<AudioDeviceDescriptor> deviceDescriptor);
704 
705     void RegisterRemoteDevStatusCallback();
706 
707     void UpdateLocalGroupInfo(bool isConnected, const std::string& macAddress,
708         const std::string& deviceName, const DeviceStreamInfo& streamInfo, AudioDeviceDescriptor& deviceDesc);
709 
710     int32_t HandleLocalDeviceConnected(AudioDeviceDescriptor &updatedDesc);
711 
712     int32_t HandleLocalDeviceDisconnected(const AudioDeviceDescriptor &updatedDesc);
713 
714     void UpdateActiveA2dpDeviceWhenDisconnecting(const std::string& macAddress);
715 
716     void UpdateEffectDefaultSink(DeviceType deviceType);
717 
718     void LoadSinksForCapturer();
719 
720     void LoadInnerCapturerSink(string moduleName, AudioStreamInfo streamInfo);
721 
722     DeviceType FindConnectedHeadset();
723 
724     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperInstance();
725 
726     void RegisterNameMonitorHelper();
727 
728     bool IsConnectedOutputDevice(const sptr<AudioDeviceDescriptor> &desc);
729 
730     void AddMicrophoneDescriptor(sptr<AudioDeviceDescriptor> &deviceDescriptor);
731 
732     void RemoveMicrophoneDescriptor(sptr<AudioDeviceDescriptor> &deviceDescriptor);
733 
734     void AddAudioCapturerMicrophoneDescriptor(int32_t sessionId, DeviceType devType);
735 
736     void UpdateAudioCapturerMicrophoneDescriptor(DeviceType devType);
737 
738     void RemoveAudioCapturerMicrophoneDescriptor(int32_t uid);
739 
740     void SetOffloadMode();
741 
742     void ResetOffloadMode(int32_t sessionId);
743 
744     bool GetOffloadAvailableFromXml() const;
745 
746     void SetOffloadAvailableFromXML(AudioModuleInfo &moduleInfo);
747 
748     bool CheckActiveOutputDeviceSupportOffload();
749 
750     bool OpenPortAndAddDeviceOnServiceConnected(AudioModuleInfo &moduleInfo);
751 
752     int32_t FetchTargetInfoForSessionAdd(const SessionInfo sessionInfo, StreamPropInfo &targetInfo,
753         SourceType &targetSourceType);
754 
755     void StoreDistributedRoutingRoleInfo(const sptr<AudioDeviceDescriptor> descriptor, CastType type);
756 
757     void AddEarpiece();
758 
759     void FetchOutputDeviceWhenNoRunningStream();
760 
761     void FetchInputDeviceWhenNoRunningStream();
762 
763     void UpdateActiveDeviceRoute(InternalDeviceType deviceType, DeviceFlag deviceFlag);
764 
765     void UpdateActiveDevicesRoute(std::vector<std::pair<InternalDeviceType, DeviceFlag>> &activeDevices);
766 
767     void UpdateDualToneState(const bool &enable, const int32_t &sessionId);
768 
769     int32_t ActivateA2dpDevice(unique_ptr<AudioDeviceDescriptor> &desc,
770         vector<unique_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
771         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
772 
773     int32_t ActivateA2dpDeviceWhenDescEnabled(unique_ptr<AudioDeviceDescriptor> &desc,
774         vector<unique_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
775         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
776 
777     void ResetToSpeaker(DeviceType devType);
778 
779     void UpdateConnectedDevicesWhenConnectingForOutputDevice(const AudioDeviceDescriptor &updatedDesc,
780         std::vector<sptr<AudioDeviceDescriptor>> &descForCb);
781 
782     void UpdateConnectedDevicesWhenConnectingForInputDevice(const AudioDeviceDescriptor &updatedDesc,
783         std::vector<sptr<AudioDeviceDescriptor>> &descForCb);
784 
785     bool IsSameDevice(unique_ptr<AudioDeviceDescriptor> &desc, DeviceInfo &deviceInfo);
786 
787     bool IsSameDevice(unique_ptr<AudioDeviceDescriptor> &desc, const AudioDeviceDescriptor &deviceDesc);
788 
789     void UpdateOffloadWhenActiveDeviceSwitchFromA2dp();
790 
791     bool IsRendererStreamRunning(unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
792 
793     bool NeedRehandleA2DPDevice(unique_ptr<AudioDeviceDescriptor> &desc);
794 
795     void MuteSinkPort(const std::string &portName, int32_t duration, bool isSync);
796 
797     void MuteSinkPort(const std::string &oldSinkname, const std::string &newSinkName,
798         AudioStreamDeviceChangeReasonExt reason);
799 
800     void MuteDefaultSinkPort();
801 
802     void SetVoiceCallMuteForSwitchDevice();
803 
804     void MuteSinkPortForSwtichDevice(unique_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
805         vector<std::unique_ptr<AudioDeviceDescriptor>>& outputDevices, const AudioStreamDeviceChangeReasonExt reason);
806 
807     std::string GetSinkName(const DeviceInfo& desc, int32_t sessionId);
808 
809     std::string GetSinkName(const AudioDeviceDescriptor& desc, int32_t sessionId);
810 
811     void RectifyModuleInfo(AudioModuleInfo &moduleInfo, std::list<AudioModuleInfo> &moduleInfoList,
812         SourceInfo &targetInfo);
813 
814     void ClearScoDeviceSuspendState(string macAddress = "");
815 
816     PipeInfo& GetPipeInfoByPipeName(std::string &supportPipe, AudioAdapterInfo &adapterInfo);
817 
818     int32_t CheckDeviceCapability(AudioAdapterInfo &adapterInfo, int32_t flag, DeviceType deviceType);
819 
820     bool IsConfigInfoHasAttribute(std::list<ConfigInfo> &configInfos, std::string value);
821 
822     int32_t OpenPortAndInsertIOHandle(const std::string &moduleName, const AudioModuleInfo &moduleInfo);
823 
824     int32_t ClosePortAndEraseIOHandle(const std::string &moduleName);
825 
826     DeviceUsage GetDeviceUsage(const AudioDeviceDescriptor &desc);
827 
828     void UnloadInnerCapturerSink(string moduleName);
829 
830     void HandleRemoteCastDevice(bool isConnected, AudioStreamInfo streamInfo = {});
831 
832     int32_t HandleDeviceChangeForFetchOutputDevice(unique_ptr<AudioDeviceDescriptor> &desc,
833         unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
834 
835     bool IsWiredHeadSet(const DeviceType &deviceType);
836 
837     bool IsBlueTooth(const DeviceType &deviceType);
838 
839     int32_t DealWithSafeVolume(const int32_t volumeLevel, bool isA2dpDevice);
840 
841     void CreateCheckMusicActiveThread();
842 
843     void CreateSafeVolumeDialogThread();
844 
845     void SetDeviceSafeVolumeStatus();
846 
847     void CheckBlueToothActiveMusicTime(int32_t safeVolume);
848 
849     void CheckWiredActiveMusicTime(int32_t safeVolume);
850 
851     void RestoreSafeVolume(AudioStreamType streamType, int32_t safeVolume);
852 
853     int32_t CheckActiveMusicTime();
854 
855     int32_t ShowDialog();
856 
857     int32_t GetVoipDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo, int32_t type,
858         std::vector<sptr<AudioDeviceDescriptor>> &preferredDeviceList);
859 
860     int32_t GetPreferredOutputStreamTypeInner(StreamUsage streamUsage, DeviceType deviceType, int32_t flags,
861         std::string &networkId, AudioSamplingRate &samplingRate);
862 
863     int32_t GetPreferredInputStreamTypeInner(SourceType sourceType, DeviceType deviceType, int32_t flags,
864         const std::string &networkId, const AudioSamplingRate &samplingRate);
865 
866     int32_t GetPreferredInputStreamTypeFromDeviceInfo(AudioAdapterInfo &adapterInfo,
867         DeviceType deviceType, int32_t flags);
868 
869     bool NotifyRecreateRendererStream(std::unique_ptr<AudioDeviceDescriptor> &desc,
870         const std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
871         const AudioStreamDeviceChangeReasonExt reason);
872 
873     void TriggerRecreateRendererStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag,
874         const AudioStreamDeviceChangeReasonExt reason);
875 
876     bool NotifyRecreateDirectStream(std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
877         const AudioStreamDeviceChangeReasonExt reason);
878 
879     bool IsDirectSupportedDevice(DeviceType deviceType);
880 
881     bool UpdateDevice(unique_ptr<AudioDeviceDescriptor> &desc, const AudioStreamDeviceChangeReasonExt reason,
882         const std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
883 
884     bool NotifyRecreateCapturerStream(bool isUpdateActiveDevice,
885         const std::unique_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
886         const AudioStreamDeviceChangeReasonExt reason);
887 
888     void TriggerRecreateCapturerStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag,
889         const AudioStreamDeviceChangeReasonExt reason);
890 
891     bool HasLowLatencyCapability(DeviceType deviceType, bool isRemote);
892 
893     int32_t HandleAbsBluetoothVolume(const std::string &macAddress, const int32_t volumeLevel);
894 
895     void WriteServiceStartupError(string reason);
896 
897     bool LoadToneDtmfConfig();
898 
899     void CreateRecoveryThread();
900     void RecoveryPreferredDevices();
901 
902     int32_t HandleRecoveryPreferredDevices(int32_t preferredType, int32_t deviceType,
903         int32_t usageOrSourceType);
904 
905     void WriteOutputRouteChangeEvent(unique_ptr<AudioDeviceDescriptor> &desc,
906         const AudioStreamDeviceChangeReason reason);
907     void WriteInputRouteChangeEvent(unique_ptr<AudioDeviceDescriptor> &desc,
908         const AudioStreamDeviceChangeReason reason);
909 
910     bool CheckStreamOffloadMode(int64_t activateSessionId, AudioStreamType streamType);
911     AudioModuleInfo ConstructOffloadAudioModuleInfo(DeviceType deviceType);
912     int32_t LoadOffloadModule();
913     int32_t UnloadOffloadModule();
914     int32_t MoveToOutputDevice(uint32_t sessionId, std::string portName);
915 
916     bool CheckStreamMultichannelMode(const int64_t activateSessionId);
917     AudioModuleInfo ConstructMchAudioModuleInfo(DeviceType deviceType);
918     int32_t LoadMchModule();
919     int32_t UnloadMchModule();
920 
921     int32_t MoveToNewPipeInner(const uint32_t sessionId, const AudioPipeType pipeType);
922 
923     void UpdateRoute(unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
924         vector<std::unique_ptr<AudioDeviceDescriptor>> &outputDevices);
925 
926     bool IsRingerOrAlarmerDualDevicesRange(const InternalDeviceType &deviceType);
927 
928     bool SelectRingerOrAlarmDevices(const vector<std::unique_ptr<AudioDeviceDescriptor>> &descs,
929         const unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
930 
931     void DealAudioSceneOutputDevices(const AudioScene &audioScene, std::vector<DeviceType> &activeOutputDevices,
932         bool &haveArmUsbDevice);
933 
934     bool IsA2dpOrArmUsbDevice(const InternalDeviceType &deviceType);
935 
936     void UpdateAllUserSelectDevice(vector<unique_ptr<AudioDeviceDescriptor>> &userSelectDeviceMap,
937         AudioDeviceDescriptor &desc, const sptr<AudioDeviceDescriptor> &selectDesc);
938 
939     void SetAbsVolumeSceneAsync(const std::string &macAddress, const bool support);
940 
941     void SetSharedAbsVolumeScene(const bool support);
942 
943     void LoadHdiEffectModel();
944 
945     void UpdateEffectBtOffloadSupported(const bool &isSupported);
946 
947     int32_t ScoInputDeviceFetchedForRecongnition(bool handleFlag, const std::string &address,
948         ConnectState connectState);
949 
950     bool IsA2dpOffloadConnected();
951 
952     void SetCurrenInputDevice(const AudioDeviceDescriptor &desc);
953 
954     AudioDeviceDescriptor GetCurrentInputDevice();
955 
956     DeviceType GetCurrentInputDeviceType();
957 
958     void SetCurrentInputDeviceType(DeviceType deviceType);
959 
960     void SetCurrentOutputDevice(const AudioDeviceDescriptor &desc);
961 
962     void SetCurrentOutputDeviceType(DeviceType deviceType);
963 
964     AudioDeviceDescriptor GetCurrentOutputDevice();
965 
966     DeviceType GetCurrentOutputDeviceType();
967 
968     DeviceCategory GetCurrentOutputDeviceCategory();
969 
970     std::string GetCurrentOutputDeviceNetworkId();
971 
972     std::string GetCurrentOutputDeviceMacAddr();
973 
974     void SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId);
975 
976     int32_t ConnectVirtualDevice(sptr<AudioDeviceDescriptor> &desc);
977     void UpdateDeviceList(AudioDeviceDescriptor &updatedDesc, bool isConnected,
978         std::vector<sptr<AudioDeviceDescriptor>> &descForCb,
979         AudioStreamDeviceChangeReasonExt &reason);
980     void UpdateDefaultOutputDeviceWhenStopping(int32_t uid);
981 
982     void SetDefaultDeviceLoadFlag(bool isLoad);
983 
984     int32_t SetPreferredDevice(const PreferredType preferredType, const sptr<AudioDeviceDescriptor> &desc);
985     int32_t ErasePreferredDeviceByType(const PreferredType preferredType);
986 
987     void CheckAndNotifyUserSelectedDevice(const sptr<AudioDeviceDescriptor> &deviceDescriptor);
988 
989     bool GetAudioEffectOffloadFlag();
990     void ResetOffloadModeOnSpatializationChanged(std::vector<int32_t> &allSessions);
991 
992     bool CheckSpatializationAndEffectState();
993 
994     void FetchStreamForSpkMchStream(std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
995     vector<std::unique_ptr<AudioDeviceDescriptor>> &descs);
996 
997     void ResetOffloadAndMchMode(std::unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
998         vector<std::unique_ptr<AudioDeviceDescriptor>> &outputDevices);
999 
1000     void JudgeIfLoadMchModule();
1001 
1002     const sptr<IStandardAudioService> GetAudioServerProxy();
1003 
1004     int32_t SelectFastInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
1005         sptr<AudioDeviceDescriptor> deviceDescriptor);
1006 
1007     void GetAudioAdapterInfos(std::unordered_map<AdaptersType, AudioAdapterInfo> &adapterInfoMap);
1008 
1009     void GetVolumeGroupData(std::unordered_map<std::string, std::string>& volumeGroupData);
1010 
1011     void GetInterruptGroupData(std::unordered_map<std::string, std::string>& interruptGroupData);
1012 
1013     void GetDeviceClassInfo(std::unordered_map<ClassType, std::list<AudioModuleInfo>> &deviceClassInfo);
1014 
1015     void GetGlobalConfigs(GlobalConfigs &globalConfigs);
1016 
1017     int32_t HandleSpecialDeviceType(DeviceType &devType, bool &isConnected, const std::string &address);
1018 
1019     void ReloadA2dpOffloadOnDeviceChanged(DeviceType deviceType, const std::string &macAddress,
1020         const std::string &deviceName, const AudioStreamInfo &streamInfo);
1021 
1022     void HandleOfflineDistributedDevice();
1023 
1024     int32_t HandleDistributedDeviceUpdate(DStatusInfo &statusInfo,
1025         std::vector<sptr<AudioDeviceDescriptor>> &descForCb);
1026 
1027     void OnServiceConnected(AudioServiceIndex serviceIndex);
1028 
1029     void LoadModernInnerCapSink();
1030 
1031     void HandleAudioCaptureState(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo);
1032 
1033     AudioStreamType GetStreamType(int32_t sessionId);
1034 
1035     int32_t GetChannelCount(uint32_t sessionId);
1036 
1037     int32_t GetUid(int32_t sessionId);
1038 
1039     bool SetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume vol);
1040 
1041     void UnregisterBluetoothListener();
1042 
1043     void GetEffectManagerInfo();
1044 
1045     std::vector<unique_ptr<AudioDeviceDescriptor>> GetAvailableDevicesInner(AudioDeviceUsage usage);
1046 
1047     void UpdateA2dpOffloadFlagForAllStream(std::unordered_map<uint32_t, bool> &sessionIDToSpatializationEnableMap,
1048         DeviceType deviceType = DEVICE_TYPE_NONE);
1049 
1050     int32_t UpdateA2dpOffloadFlagForAllStream(DeviceType deviceType = DEVICE_TYPE_NONE);
1051 
1052     int32_t OffloadStartPlaying(const std::vector<int32_t> &sessionIds);
1053 
1054 #ifdef BLUETOOTH_ENABLE
1055     void UpdateA2dpOffloadFlag(const std::vector<Bluetooth::A2dpStreamInfo> &allActiveSessions,
1056         DeviceType deviceType = DEVICE_TYPE_NONE);
1057 
1058     void CheckAndActiveHfpDevice(AudioDeviceDescriptor &desc);
1059 #endif
1060 
1061     void OnPreferredStateUpdated(AudioDeviceDescriptor &desc,
1062         const DeviceInfoUpdateCommand updateCommand, AudioStreamDeviceChangeReasonExt &reason);
1063 
1064     void CheckForA2dpSuspend(AudioDeviceDescriptor &desc);
1065 
1066     void UnloadA2dpModule();
1067 
1068     std::vector<sptr<AudioDeviceDescriptor>> GetDumpDevices(DeviceFlag deviceFlag);
1069     std::vector<sptr<AudioDeviceDescriptor>> GetDumpDeviceInfo(std::string &dumpString, DeviceFlag deviceFlag);
1070     bool IsStreamSupported(AudioStreamType streamType);
1071 private:
1072     bool isUpdateRouteSupported_ = true;
1073     bool isCurrentRemoteRenderer = false;
1074     bool remoteCapturerSwitch_ = false;
1075     bool isOpenRemoteDevice = false;
1076     static bool isBtListenerRegistered;
1077     bool isPnpDeviceConnected = false;
1078     bool hasModulesLoaded = false;
1079     bool hasEarpiece_ = false;
1080     const int32_t G_UNKNOWN_PID = -1;
1081     int32_t dAudioClientUid = 3055;
1082     int32_t maxRendererInstances_ = 128;
1083     uint64_t audioLatencyInMsec_ = 50;
1084     uint32_t sinkLatencyInMsec_ {0};
1085     bool isOffloadAvailable_ = false;
1086     bool enableFastVoip_ = false;
1087     bool enableDualHalToneState_ = false;
1088     int32_t enableDualHalToneSessionId_ = -1;
1089     int32_t shouldUpdateDeviceDueToDualTone_ = false;
1090     bool isFastControlled_ = false;
1091     bool isVoiceRingtoneMute_ = false;
1092 
1093     std::unordered_map<std::string, DeviceType> spatialDeviceMap_;
1094 
1095     BluetoothOffloadState a2dpOffloadFlag_ = NO_A2DP_DEVICE;
1096     std::mutex switchA2dpOffloadMutex_;
1097 
1098     std::bitset<MIN_SERVICE_COUNT> serviceFlag_;
1099     std::mutex serviceFlagMutex_;
1100     DeviceType effectActiveDevice_ = DEVICE_TYPE_NONE;
1101     std::mutex curOutputDevice_; // lock this mutex to operate currentActiveDevice_
1102     AudioDeviceDescriptor currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE);
1103     std::mutex curInputDevice_; // lock this mutex to operate currentActiveInputDevice_
1104     AudioDeviceDescriptor currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE);
1105     std::vector<std::pair<DeviceType, bool>> pnpDeviceList_;
1106 
1107     std::mutex routerMapMutex_; // unordered_map is not concurrently-secure
1108     mutable std::mutex a2dpDeviceMapMutex_;
1109     std::unordered_map<int32_t, std::pair<std::string, int32_t>> routerMap_;
1110     std::unordered_map<int32_t, std::pair<std::string, DeviceRole>> fastRouterMap_; // key:uid value:<netWorkId, Role>
1111     IAudioPolicyInterface& audioPolicyManager_;
1112     Parser& audioPolicyConfigParser_;
1113 #ifdef FEATURE_DTMF_TONE
1114     std::unordered_map<int32_t, std::shared_ptr<ToneInfo>> toneDescriptorMap;
1115 #endif
1116     AudioStreamCollector& streamCollector_;
1117     AudioRouterCenter& audioRouterCenter_;
1118 #ifdef ACCESSIBILITY_ENABLE
1119     std::shared_ptr<AccessibilityConfigListener> accessibilityConfigListener_;
1120 #endif
1121     std::unique_ptr<DeviceStatusListener> deviceStatusListener_;
1122     std::vector<sptr<AudioDeviceDescriptor>> connectedDevices_;
1123     std::vector<sptr<MicrophoneDescriptor>> connectedMicrophones_;
1124     std::unordered_map<int32_t, sptr<MicrophoneDescriptor>> audioCaptureMicrophoneDescriptor_;
1125     std::unordered_map<std::string, A2dpDeviceConfigInfo> connectedA2dpDeviceMap_;
1126     std::string activeBTDevice_;
1127 
1128     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
1129     AudioScene lastAudioScene_ = AUDIO_SCENE_DEFAULT;
1130     std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo_ = {};
1131     std::unordered_map<AdaptersType, AudioAdapterInfo> adapterInfoMap_ {};
1132 
1133     std::mutex ioHandlesMutex_;
1134     std::unordered_map<std::string, AudioIOHandle> IOHandles_ = {};
1135 
1136     std::shared_ptr<AudioSharedMemory> policyVolumeMap_ = nullptr;
1137     volatile Volume *volumeVector_ = nullptr;
1138     volatile bool *sharedAbsVolumeScene_ = nullptr;
1139 
1140     std::vector<DeviceType> outputPriorityList_ = {
1141         DEVICE_TYPE_BLUETOOTH_SCO,
1142         DEVICE_TYPE_BLUETOOTH_A2DP,
1143         DEVICE_TYPE_DP,
1144         DEVICE_TYPE_USB_HEADSET,
1145         DEVICE_TYPE_WIRED_HEADSET,
1146         DEVICE_TYPE_SPEAKER
1147     };
1148     std::vector<DeviceType> inputPriorityList_ = {
1149         DEVICE_TYPE_BLUETOOTH_SCO,
1150         DEVICE_TYPE_BLUETOOTH_A2DP,
1151         DEVICE_TYPE_USB_HEADSET,
1152         DEVICE_TYPE_WIRED_HEADSET,
1153         DEVICE_TYPE_WAKEUP,
1154         DEVICE_TYPE_MIC
1155     };
1156 
1157     std::vector<sptr<VolumeGroupInfo>> volumeGroups_;
1158     std::vector<sptr<InterruptGroupInfo>> interruptGroups_;
1159     std::unordered_map<std::string, std::string> volumeGroupData_;
1160     std::unordered_map<std::string, std::string> interruptGroupData_;
1161     GlobalConfigs globalConfigs_;
1162     AudioEffectManager& audioEffectManager_;
1163 
1164     bool isMicrophoneMuteTemporary_ = false;
1165 
1166     bool isMicrophoneMutePersistent_ = false;
1167 
1168     mutable std::shared_mutex deviceStatusUpdateSharedMutex_;
1169 
1170     bool hasArmUsbDevice_ = false;
1171     bool hasHifiUsbDevice_ = false; // Only the first usb device is supported now, hifi or arm.
1172     bool hasDpDevice_ = false; // Only the first dp device is supported.
1173 
1174     AudioDeviceManager &audioDeviceManager_;
1175     AudioStateManager &audioStateManager_;
1176     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_;
1177     AudioPnpServer &audioPnpServer_;
1178 
1179     std::optional<uint32_t> offloadSessionID_;
1180     PowerMgr::PowerState currentPowerState_ = PowerMgr::PowerState::AWAKE;
1181     bool currentOffloadSessionIsBackground_ = false;
1182     std::mutex offloadMutex_;
1183 
1184     AudioModuleInfo primaryMicModuleInfo_ = {};
1185     std::mutex defaultDeviceLoadMutex_;
1186     std::condition_variable loadDefaultDeviceCV_;
1187     std::atomic<bool> isPrimaryMicModuleInfoLoaded_ = false;
1188     std::atomic<bool> isAdapterInfoMap_ = false;
1189 
1190     std::mutex moveDeviceMutex_;
1191     std::condition_variable moveDeviceCV_;
1192     std::atomic<bool> moveDeviceFinished_ = false;
1193 
1194     std::unordered_map<uint32_t, SessionInfo> sessionWithNormalSourceType_;
1195 
1196     DistributedRoutingInfo distributedRoutingInfo_ = {
1197         .descriptor = nullptr,
1198         .type = CAST_TYPE_NULL
1199     };
1200 
1201     // sourceType is SOURCE_TYPE_PLAYBACK_CAPTURE, SOURCE_TYPE_WAKEUP or SOURCE_TYPE_VIRTUAL_CAPTURE
1202     std::unordered_map<uint32_t, SessionInfo> sessionWithSpecialSourceType_;
1203 
1204     static std::map<std::string, std::string> sinkPortStrToClassStrMap_;
1205     static std::map<std::string, uint32_t> formatStrToEnum;
1206     static std::map<std::string, ClassType> classStrToEnum;
1207 
1208     std::unordered_set<uint32_t> sessionIdisRemovedSet_;
1209 
1210     SourceType currentSourceType = SOURCE_TYPE_MIC;
1211     uint32_t currentRate = 0;
1212 
1213     bool updateA2dpOffloadLogFlag = false;
1214     std::mutex checkSpatializedMutex_;
1215     SafeStatus safeStatusBt_ = SAFE_UNKNOWN;
1216     SafeStatus safeStatus_ = SAFE_UNKNOWN;
1217     int64_t activeSafeTimeBt_ = 0;
1218     int64_t activeSafeTime_ = 0;
1219     std::time_t startSafeTimeBt_ = 0;
1220     std::time_t startSafeTime_ = 0;
1221     bool userSelect_ = false;
1222     std::unique_ptr<std::thread> calculateLoopSafeTime_ = nullptr;
1223     std::mutex checkMusicActiveThreadMutex_; // lock calculateLoopSafeTime_
1224     bool safeVolumeExit_ = false;
1225     bool isAbsBtFirstBoot_ = true;
1226     bool normalVoipFlag_ = false;
1227 
1228     std::mutex dialogMutex_;
1229     std::atomic<bool> isDialogSelectDestroy_ = false;
1230     std::condition_variable dialogSelectCondition_;
1231 
1232     std::unique_ptr<std::thread> safeVolumeDialogThrd_ = nullptr;
1233     std::atomic<bool> isSafeVolumeDialogShowing_ = false;
1234     std::mutex safeVolumeMutex_;
1235 
1236     DeviceType priorityOutputDevice_ = DEVICE_TYPE_INVALID;
1237     DeviceType priorityInputDevice_ = DEVICE_TYPE_INVALID;
1238     ConnectType conneceType_ = CONNECT_TYPE_LOCAL;
1239 
1240     SupportedEffectConfig supportedEffectConfig_;
1241     ConverterConfig converterConfig_;
1242 
1243     std::unique_ptr<std::thread> RecoveryDevicesThread_ = nullptr;
1244 
1245     std::mutex offloadCloseMutex_;
1246     std::mutex offloadOpenMutex_;
1247     std::atomic<bool> isOffloadOpened_ = false;
1248     std::condition_variable offloadCloseCondition_;
1249 
1250     std::mutex ringerModeMuteMutex_;
1251     std::atomic<bool> ringerModeMute_ = true;
1252     std::condition_variable ringerModeMuteCondition_;
1253     std::atomic<bool> isPolicyConfigParsered_ = false;
1254     std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr;
1255 
1256     bool isBTReconnecting_ = false;
1257 };
1258 
1259 class AudioA2dpOffloadManager final : public Bluetooth::AudioA2dpPlayingStateChangedListener,
1260     public enable_shared_from_this<AudioA2dpOffloadManager> {
1261 public:
AudioA2dpOffloadManager(AudioPolicyService * audioPolicyService)1262     AudioA2dpOffloadManager(AudioPolicyService *audioPolicyService) : audioPolicyService_(audioPolicyService) {};
Init()1263     void Init() {Bluetooth::AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(shared_from_this());};
GetA2dOffloadConnectionState()1264     A2dpOffloadConnectionState GetA2dOffloadConnectionState() {return currentOffloadConnectionState_;};
1265 
1266     void ConnectA2dpOffload(const std::string &deviceAddress, const vector<int32_t> &sessionIds);
1267     void OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState) override;
1268 
1269     void WaitForConnectionCompleted();
1270     bool IsA2dpOffloadConnecting(int32_t sessionId);
1271 private:
1272     A2dpOffloadConnectionState currentOffloadConnectionState_ = CONNECTION_STATUS_DISCONNECTED;
1273     std::vector<int32_t> connectionTriggerSessionIds_;
1274     std::string a2dpOffloadDeviceAddress_ = "";
1275     AudioPolicyService *audioPolicyService_ = nullptr;
1276     std::mutex connectionMutex_;
1277     std::condition_variable connectionCV_;
1278     static const int32_t CONNECTION_TIMEOUT_IN_MS = 1000; // 1000ms
1279 };
1280 } // namespace AudioStandard
1281 } // namespace OHOS
1282 
1283 #endif // ST_AUDIO_POLICY_SERVICE_H
1284