• 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_DEVICE_COMMON_H
17 #define ST_AUDIO_DEVICE_COMMON_H
18 
19 #include <bitset>
20 #include <list>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <mutex>
25 
26 #include "singleton.h"
27 #include "audio_group_handle.h"
28 #include "audio_ec_info.h"
29 #include "audio_manager_base.h"
30 #include "audio_module_info.h"
31 #include "audio_router_center.h"
32 #include "audio_policy_manager_factory.h"
33 #include "audio_device_manager.h"
34 #include "audio_stream_collector.h"
35 #include "audio_state_manager.h"
36 #include "audio_affinity_manager.h"
37 #include "audio_policy_server_handler.h"
38 
39 #include "audio_a2dp_device.h"
40 #include "audio_a2dp_offload_flag.h"
41 #include "audio_config_manager.h"
42 #include "audio_active_device.h"
43 #include "audio_iohandle_map.h"
44 #include "audio_router_map.h"
45 #include "audio_connected_device.h"
46 #include "audio_microphone_descriptor.h"
47 #include "audio_scene_manager.h"
48 #include "audio_offload_stream.h"
49 #include "audio_volume_manager.h"
50 #include "audio_ec_manager.h"
51 
52 namespace OHOS {
53 namespace AudioStandard {
54 
55 class AudioDeviceCommon {
56 public:
GetInstance()57     static AudioDeviceCommon& GetInstance()
58     {
59         static AudioDeviceCommon instance;
60         return instance;
61     }
62     void Init(std::shared_ptr<AudioPolicyServerHandler> handler);
63     void DeInit();
64     void OnPreferredOutputDeviceUpdated(const AudioDeviceDescriptor& deviceDescriptor);
65     void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId);
66     void OnAudioSceneChange(const AudioScene& audioScene);
67     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredOutputDeviceDescInner(
68         AudioRendererInfo &rendererInfo, std::string networkId = LOCAL_NETWORK_ID);
69     std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetPreferredInputDeviceDescInner(
70         AudioCapturerInfo &captureInfo, std::string networkId = LOCAL_NETWORK_ID);
71     int32_t GetPreferredOutputStreamTypeInner(StreamUsage streamUsage, DeviceType deviceType, int32_t flags,
72         std::string &networkId, AudioSamplingRate &samplingRate);
73     int32_t GetPreferredInputStreamTypeInner(SourceType sourceType, DeviceType deviceType, int32_t flags,
74         const std::string &networkId, const AudioSamplingRate &samplingRate);
75     void UpdateDeviceInfo(AudioDeviceDescriptor &deviceInfo,
76         const std::shared_ptr<AudioDeviceDescriptor> &desc,
77         bool hasBTPermission, bool hasSystemPermission);
78     int32_t DeviceParamsCheck(DeviceRole targetRole,
79         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors) const;
80     void UpdateConnectedDevicesWhenConnecting(const AudioDeviceDescriptor& updatedDesc,
81         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& descForCb);
82     void UpdateConnectedDevicesWhenDisconnecting(const AudioDeviceDescriptor& updatedDesc,
83         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
84     void UpdateDualToneState(const bool &enable, const int32_t &sessionId);
85     void FetchDevice(bool isOutputDevice = true,
86         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
87     void FetchOutputDevice(std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
88         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
89     void FetchInputDevice(std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos,
90         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
91     void MoveToNewOutputDevice(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
92         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices,
93         std::vector<SinkInput> sinkInputs,
94         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
95     void MoveToNewInputDevice(std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
96         std::shared_ptr<AudioDeviceDescriptor> &inputDevice);
97     DeviceType GetSpatialDeviceType(const std::string& macAddress);
98 
99     bool GetHasDpFlag();
100     void SetHasDpFlag(bool flag);
101     void UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo, RendererState rendererState);
102     int32_t ActivateA2dpDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
103         std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
104         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
105     bool IsRendererStreamRunning(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
106     void FetchStreamForA2dpMchStream(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
107         vector<std::shared_ptr<AudioDeviceDescriptor>> &descs);
108 
109     int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds,
110         std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor);
111     void TriggerRecreateRendererStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag,
112         const AudioStreamDeviceChangeReasonExt reason);
113     int32_t ScoInputDeviceFetchedForRecongnition(bool handleFlag, const std::string &address,
114         ConnectState connectState);
115     std::vector<SourceOutput> GetSourceOutputs();
116     void BluetoothScoDisconectForRecongnition();
117     void SetFirstScreenOn();
118     void ClientDiedDisconnectScoNormal();
119     void ClientDiedDisconnectScoRecognition();
120     int32_t SetVirtualCall(const bool isVirtual);
121 private:
AudioDeviceCommon()122     AudioDeviceCommon() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
123         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
124         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
125         audioStateManager_(AudioStateManager::GetAudioStateManager()),
126         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
127         audioAffinityManager_(AudioAffinityManager::GetAudioAffinityManager()),
128         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
129         audioActiveDevice_(AudioActiveDevice::GetInstance()),
130         audioConfigManager_(AudioConfigManager::GetInstance()),
131         audioSceneManager_(AudioSceneManager::GetInstance()),
132         audioVolumeManager_(AudioVolumeManager::GetInstance()),
133         audioRouteMap_(AudioRouteMap::GetInstance()),
134         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
135         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
136         audioEcManager_(AudioEcManager::GetInstance()),
137         audioOffloadStream_(AudioOffloadStream::GetInstance()),
138         audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()),
139         audioA2dpDevice_(AudioA2dpDevice::GetInstance()) {}
~AudioDeviceCommon()140     ~AudioDeviceCommon() {}
141 
142     void UpdateConnectedDevicesWhenConnectingForOutputDevice(const AudioDeviceDescriptor &updatedDesc,
143         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
144     void UpdateConnectedDevicesWhenConnectingForInputDevice(const AudioDeviceDescriptor &updatedDesc,
145         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
146 
147     void MutePrimaryOrOffloadSink(const std::string &sinkName, int64_t muteTime);
148     void MuteSinkPort(const std::string &oldSinkName, const std::string &newSinkName,
149         AudioStreamDeviceChangeReasonExt reason);
150     void MuteSinkPortLogic(const std::string &oldSinkName, const std::string &newSinkName,
151         AudioStreamDeviceChangeReasonExt reason);
152 
153     void UpdateRoute(shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
154         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices);
155     void FetchStreamForSpkMchStream(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
156         vector<std::shared_ptr<AudioDeviceDescriptor>> &descs);
157     void ResetOffloadAndMchMode(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
158         vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices);
159     bool SelectRingerOrAlarmDevices(const vector<std::shared_ptr<AudioDeviceDescriptor>> &descs,
160         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
161     bool IsSameDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, AudioDeviceDescriptor &deviceInfo);
162     bool IsSameDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, const AudioDeviceDescriptor &deviceDesc);
163     void RemoveOfflineDevice(const AudioDeviceDescriptor& updatedDesc);
164     bool IsDeviceConnected(std::shared_ptr<AudioDeviceDescriptor> &audioDeviceDescriptors) const;
165     int32_t HandleDeviceChangeForFetchInputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
166         std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo);
167     void JudgeIfLoadMchModule();
168     bool IsFastFromA2dpToA2dp(const std::shared_ptr<AudioDeviceDescriptor> &desc,
169         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
170         const AudioStreamDeviceChangeReasonExt reason);
171     void WriteInputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc,
172         const AudioStreamDeviceChangeReason reason);
173     std::vector<SourceOutput> FilterSourceOutputs(int32_t sessionId);
174     bool IsRingerOrAlarmerDualDevicesRange(const InternalDeviceType &deviceType);
175 
176     int32_t MoveToLocalInputDevice(std::vector<SourceOutput> sourceOutputIds,
177         std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor);
178     int32_t MoveToRemoteInputDevice(std::vector<SourceOutput> sourceOutputIds,
179         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
180     int32_t MoveToRemoteOutputDevice(std::vector<SinkInput> sinkInputIds,
181         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
182 
183     void CheckAndNotifyUserSelectedDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
184     int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType,
185         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
186 
187     int32_t LoadA2dpModule(DeviceType deviceType, const AudioStreamInfo &audioStreamInfo, std::string networkID,
188         std::string sinkName, SourceType sourceType);
189     void GetA2dpModuleInfo(AudioModuleInfo &moduleInfo, const AudioStreamInfo& audioStreamInfo,
190         SourceType sourceType);
191     int32_t ReloadA2dpAudioPort(AudioModuleInfo &moduleInfo, DeviceType deviceType,
192         const AudioStreamInfo& audioStreamInfo, std::string networkID, std::string sinkName,
193         SourceType sourceType);
194     int32_t SwitchActiveA2dpDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
195 
196     // fetchOutput
197     void FetchOutputEnd(const bool isUpdateActiveDevice, const int32_t runningStreamCount);
198     void FetchOutputDeviceWhenNoRunningStream();
199     void SetDeviceConnectedFlagWhenFetchOutputDevice();
200     int32_t HandleDeviceChangeForFetchOutputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
201         std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
202     void MuteSinkPortForSwitchDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
203         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
204         const AudioStreamDeviceChangeReasonExt reason);
205     void MuteSinkForSwitchGeneralDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
206         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
207         const AudioStreamDeviceChangeReasonExt reason);
208     void MuteSinkForSwitchBluetoothDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
209         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
210         const AudioStreamDeviceChangeReasonExt reason);
211     void MuteSinkForSwitchDistributedDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
212         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
213         const AudioStreamDeviceChangeReasonExt reason);
214     int32_t ActivateA2dpDeviceWhenDescEnabled(shared_ptr<AudioDeviceDescriptor> &desc,
215         vector<shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
216         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
217     int32_t HandleScoOutputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
218         std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
219         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
220     bool NotifyRecreateRendererStream(std::shared_ptr<AudioDeviceDescriptor> &desc,
221         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
222         const AudioStreamDeviceChangeReasonExt reason);
223     bool NeedRehandleA2DPDevice(std::shared_ptr<AudioDeviceDescriptor> &desc);
224     void SetVoiceCallMuteForSwitchDevice();
225     bool HasLowLatencyCapability(DeviceType deviceType, bool isRemote);
226     bool NotifyRecreateDirectStream(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
227         const AudioStreamDeviceChangeReasonExt reason);
228     vector<std::shared_ptr<AudioDeviceDescriptor>> GetDeviceDescriptorInner(
229         std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
230     bool IsRingDualToneOnPrimarySpeaker(const vector<std::shared_ptr<AudioDeviceDescriptor>> &descs,
231         const int32_t sessionId);
232     bool IsStopOrReleasePlayback(AudioMode &mode, RendererState rendererState);
233     bool IsDualStreamWhenRingDual(AudioStreamType streamType);
234 
235     // fetchInput
236     void FetchInputDeviceInner(std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos,
237         const AudioStreamDeviceChangeReasonExt reason, bool& needUpdateActiveDevice, bool& isUpdateActiveDevice,
238         int32_t& runningStreamCount);
239     void FetchInputEnd(const bool isUpdateActiveDevice, const int32_t runningStreamCount);
240     void FetchInputDeviceWhenNoRunningStream();
241     void HandleBluetoothInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
242         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos, SourceType sourceType);
243     bool NotifyRecreateCapturerStream(bool isUpdateActiveDevice,
244         const std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
245         const AudioStreamDeviceChangeReasonExt reason);
246     void BluetoothScoFetch(std::shared_ptr<AudioDeviceDescriptor> &desc,
247         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos, SourceType sourceType);
248     void HandleA2dpInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc, SourceType sourceType);
249     void TriggerRecreateCapturerStreamCallback(const std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
250         int32_t streamFlag, const AudioStreamDeviceChangeReasonExt reason);
251     int32_t HandleScoInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
252         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos);
253 private:
254     std::unordered_map<std::string, DeviceType> spatialDeviceMap_;
255     bool isCurrentRemoteRenderer = false;
256     bool hasDpDevice_ = false; // Only the first dp device is supported.
257     bool enableDualHalToneState_ = false;
258     int32_t enableDualHalToneSessionId_ = -1;
259     bool isOpenRemoteDevice = false;
260     int32_t shouldUpdateDeviceDueToDualTone_ = false;
261     bool isFirstScreenOn_ = false;
262     bool isRingDualToneOnPrimarySpeaker_ = false;
263     std::vector<std::pair<AudioStreamType, StreamUsage>> streamsWhenRingDualOnPrimarySpeaker_;
264 
265     IAudioPolicyInterface& audioPolicyManager_;
266     AudioStreamCollector& streamCollector_;
267     AudioRouterCenter& audioRouterCenter_;
268     AudioStateManager &audioStateManager_;
269     AudioDeviceManager &audioDeviceManager_;
270     AudioAffinityManager &audioAffinityManager_;
271     AudioIOHandleMap& audioIOHandleMap_;
272     AudioActiveDevice& audioActiveDevice_;
273     AudioConfigManager& audioConfigManager_;
274     AudioSceneManager& audioSceneManager_;
275     AudioVolumeManager& audioVolumeManager_;
276     AudioRouteMap& audioRouteMap_;
277     AudioConnectedDevice& audioConnectedDevice_;
278     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
279     AudioEcManager& audioEcManager_;
280     AudioOffloadStream& audioOffloadStream_;
281     AudioA2dpOffloadFlag& audioA2dpOffloadFlag_;
282     AudioA2dpDevice& audioA2dpDevice_;
283 
284     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_ = nullptr;
285 };
286 
287 }
288 }
289 
290 #endif
291