• 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_policy_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         const AudioStreamDeviceChangeReason reason);
66     void OnPreferredInputDeviceUpdated(DeviceType deviceType, std::string networkId);
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, bool isFirstCreate = true);
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 FetchOutputDevice(std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
86         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
87     void FetchInputDevice(std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos,
88         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
89     void MoveToNewOutputDevice(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
90         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices,
91         std::vector<SinkInput> sinkInputs,
92         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
93     void MoveToNewInputDevice(std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
94         std::shared_ptr<AudioDeviceDescriptor> &inputDevice);
95     DeviceType GetSpatialDeviceType(const std::string& macAddress);
96 
97     int32_t ActivateA2dpDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
98         std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
99         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
100     bool IsRendererStreamRunning(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
101     void FetchStreamForA2dpMchStream(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
102         vector<std::shared_ptr<AudioDeviceDescriptor>> &descs);
103 
104     int32_t MoveToLocalOutputDevice(std::vector<SinkInput> sinkInputIds,
105         std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor);
106     void TriggerRecreateRendererStreamCallback(int32_t callerPid, int32_t sessionId, int32_t streamFlag,
107         const AudioStreamDeviceChangeReasonExt reason);
108     int32_t ScoInputDeviceFetchedForRecongnition(bool handleFlag, const std::string &address,
109         ConnectState connectState);
110     std::vector<SourceOutput> GetSourceOutputs();
111     void SetFirstScreenOn();
112     void ClientDiedDisconnectScoNormal();
113     void ClientDiedDisconnectScoRecognition();
114     int32_t SetVirtualCall(pid_t uid, const bool isVirtual);
115     void NotifyDistributedOutputChange(const AudioDeviceDescriptor &deviceDesc);
116 private:
AudioDeviceCommon()117     AudioDeviceCommon() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
118         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
119         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
120         audioStateManager_(AudioStateManager::GetAudioStateManager()),
121         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
122         audioAffinityManager_(AudioAffinityManager::GetAudioAffinityManager()),
123         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
124         audioActiveDevice_(AudioActiveDevice::GetInstance()),
125         audioConfigManager_(AudioPolicyConfigManager::GetInstance()),
126         audioSceneManager_(AudioSceneManager::GetInstance()),
127         audioVolumeManager_(AudioVolumeManager::GetInstance()),
128         audioRouteMap_(AudioRouteMap::GetInstance()),
129         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
130         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
131         audioEcManager_(AudioEcManager::GetInstance()),
132         audioOffloadStream_(AudioOffloadStream::GetInstance()),
133         audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()),
134         audioA2dpDevice_(AudioA2dpDevice::GetInstance()) {}
~AudioDeviceCommon()135     ~AudioDeviceCommon() {}
136 
137     void UpdateConnectedDevicesWhenConnectingForOutputDevice(const AudioDeviceDescriptor &updatedDesc,
138         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
139     void UpdateConnectedDevicesWhenConnectingForInputDevice(const AudioDeviceDescriptor &updatedDesc,
140         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
141 
142     void MuteOtherSink(const std::string &sinkName, int64_t muteTime);
143     void MuteSinkPort(const std::string &oldSinkName, const std::string &newSinkName,
144         AudioStreamDeviceChangeReasonExt reason);
145     void MuteSinkPortLogic(const std::string &oldSinkName, const std::string &newSinkName,
146         AudioStreamDeviceChangeReasonExt reason);
147 
148     void UpdateRoute(shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
149         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices);
150     void FetchStreamForSpkMchStream(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
151         vector<std::shared_ptr<AudioDeviceDescriptor>> &descs);
152     void ResetOffloadAndMchMode(std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
153         vector<std::shared_ptr<AudioDeviceDescriptor>> &outputDevices);
154     bool SelectRingerOrAlarmDevices(const vector<std::shared_ptr<AudioDeviceDescriptor>> &descs,
155         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
156     bool IsSameDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, AudioDeviceDescriptor &deviceInfo);
157     bool IsSameDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, const AudioDeviceDescriptor &deviceDesc);
158     void RemoveOfflineDevice(const AudioDeviceDescriptor& updatedDesc);
159     bool IsDeviceConnected(std::shared_ptr<AudioDeviceDescriptor> &audioDeviceDescriptors) const;
160     int32_t HandleDeviceChangeForFetchInputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
161         std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo);
162     void JudgeIfLoadMchModule();
163     bool IsFastFromA2dpToA2dp(const std::shared_ptr<AudioDeviceDescriptor> &desc,
164         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
165         const AudioStreamDeviceChangeReasonExt reason);
166     void WriteInputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc,
167         const AudioStreamDeviceChangeReason reason);
168     std::vector<SourceOutput> FilterSourceOutputs(int32_t sessionId);
169     bool IsRingerOrAlarmerDualDevicesRange(const InternalDeviceType &deviceType);
170 
171     int32_t MoveToLocalInputDevice(std::vector<SourceOutput> sourceOutputIds,
172         std::shared_ptr<AudioDeviceDescriptor> localDeviceDescriptor);
173     int32_t MoveToRemoteInputDevice(std::vector<SourceOutput> sourceOutputIds,
174         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
175     int32_t MoveToRemoteOutputDevice(std::vector<SinkInput> sinkInputIds,
176         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
177 
178     void CheckAndNotifyUserSelectedDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
179     int32_t OpenRemoteAudioDevice(std::string networkId, DeviceRole deviceRole, DeviceType deviceType,
180         std::shared_ptr<AudioDeviceDescriptor> remoteDeviceDescriptor);
181 
182     int32_t LoadA2dpModule(DeviceType deviceType, const AudioStreamInfo &audioStreamInfo, std::string networkID,
183         std::string sinkName, SourceType sourceType);
184     void GetA2dpModuleInfo(AudioModuleInfo &moduleInfo, const AudioStreamInfo& audioStreamInfo,
185         SourceType sourceType);
186     int32_t ReloadA2dpAudioPort(AudioModuleInfo &moduleInfo, DeviceType deviceType,
187         const AudioStreamInfo& audioStreamInfo, std::string networkID, std::string sinkName,
188         SourceType sourceType);
189     int32_t SwitchActiveA2dpDevice(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
190     int32_t RingToneVoiceControl(const InternalDeviceType &deviceType);
191     void ClearRingMuteWhenCallStart(bool pre, bool after);
192 
193     // fetchOutput
194     void FetchOutputEnd(const bool isUpdateActiveDevice, const int32_t runningStreamCount,
195         const AudioStreamDeviceChangeReason reason);
196     void FetchOutputDeviceWhenNoRunningStream(const AudioStreamDeviceChangeReason reason);
197     void SetDeviceConnectedFlagWhenFetchOutputDevice();
198     int32_t HandleDeviceChangeForFetchOutputDevice(std::shared_ptr<AudioDeviceDescriptor> &desc,
199         std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo, const AudioStreamDeviceChangeReason reason);
200     void MuteSinkPortForSwitchDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
201         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
202         const AudioStreamDeviceChangeReasonExt reason);
203     void MuteSinkForSwitchGeneralDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
204         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
205         const AudioStreamDeviceChangeReasonExt reason);
206     void MuteSinkForSwitchBluetoothDevice(std::shared_ptr<AudioRendererChangeInfo>& rendererChangeInfo,
207         std::vector<std::shared_ptr<AudioDeviceDescriptor>>& outputDevices,
208         const AudioStreamDeviceChangeReasonExt reason);
209     int32_t ActivateA2dpDeviceWhenDescEnabled(shared_ptr<AudioDeviceDescriptor> &desc,
210         vector<shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
211         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
212     int32_t HandleScoOutputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
213         std::vector<std::shared_ptr<AudioRendererChangeInfo>> &rendererChangeInfos,
214         const AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN);
215     bool NotifyRecreateRendererStream(std::shared_ptr<AudioDeviceDescriptor> &desc,
216         const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo,
217         const AudioStreamDeviceChangeReasonExt reason);
218     bool NeedRehandleA2DPDevice(std::shared_ptr<AudioDeviceDescriptor> &desc);
219     void SetVoiceCallMuteForSwitchDevice();
220     bool HasLowLatencyCapability(DeviceType deviceType, bool isRemote);
221     vector<std::shared_ptr<AudioDeviceDescriptor>> GetDeviceDescriptorInner(
222         std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo);
223     bool IsRingDualToneOnPrimarySpeaker(const vector<std::shared_ptr<AudioDeviceDescriptor>> &descs,
224         const int32_t sessionId);
225     bool IsRingOverPlayback(AudioMode &mode, RendererState rendererState);
226     bool IsDualStreamWhenRingDual(AudioStreamType streamType);
227 
228     // fetchInput
229     void FetchInputDeviceInner(std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos,
230         const AudioStreamDeviceChangeReasonExt reason, bool& needUpdateActiveDevice, bool& isUpdateActiveDevice,
231         int32_t& runningStreamCount);
232     void FetchInputEnd(const bool isUpdateActiveDevice, const int32_t runningStreamCount);
233     void FetchInputDeviceWhenNoRunningStream();
234     void HandleBluetoothInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
235         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos, SourceType sourceType);
236     bool NotifyRecreateCapturerStream(bool isUpdateActiveDevice,
237         const std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
238         const AudioStreamDeviceChangeReasonExt reason);
239     void BluetoothScoFetch(std::shared_ptr<AudioDeviceDescriptor> &desc,
240         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos, SourceType sourceType);
241     void HandleA2dpInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc, SourceType sourceType);
242     void TriggerRecreateCapturerStreamCallback(const std::shared_ptr<AudioCapturerChangeInfo> &capturerChangeInfo,
243         int32_t streamFlag, const AudioStreamDeviceChangeReasonExt reason);
244     int32_t HandleScoInputDeviceFetched(std::shared_ptr<AudioDeviceDescriptor> &desc,
245         std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &capturerChangeInfos);
246     void SetHeadsetUnpluggedToSpkOrEpFlag(DeviceType oldDeviceType, DeviceType newDeviceType);
247 
248 private:
249     std::unordered_map<std::string, DeviceType> spatialDeviceMap_;
250     bool isCurrentRemoteRenderer = false;
251     bool enableDualHalToneState_ = false;
252     int32_t enableDualHalToneSessionId_ = -1;
253     bool isOpenRemoteDevice = false;
254     int32_t shouldUpdateDeviceDueToDualTone_ = false;
255     bool isFirstScreenOn_ = false;
256     bool isRingDualToneOnPrimarySpeaker_ = false;
257     bool isHeadsetUnpluggedToSpkOrEpFlag_ = false;
258     std::vector<std::pair<AudioStreamType, StreamUsage>> streamsWhenRingDualOnPrimarySpeaker_;
259 
260     IAudioPolicyInterface& audioPolicyManager_;
261     AudioStreamCollector& streamCollector_;
262     AudioRouterCenter& audioRouterCenter_;
263     AudioStateManager &audioStateManager_;
264     AudioDeviceManager &audioDeviceManager_;
265     AudioAffinityManager &audioAffinityManager_;
266     AudioIOHandleMap& audioIOHandleMap_;
267     AudioActiveDevice& audioActiveDevice_;
268     AudioPolicyConfigManager& audioConfigManager_;
269     AudioSceneManager& audioSceneManager_;
270     AudioVolumeManager& audioVolumeManager_;
271     AudioRouteMap& audioRouteMap_;
272     AudioConnectedDevice& audioConnectedDevice_;
273     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
274     AudioEcManager& audioEcManager_;
275     AudioOffloadStream& audioOffloadStream_;
276     AudioA2dpOffloadFlag& audioA2dpOffloadFlag_;
277     AudioA2dpDevice& audioA2dpDevice_;
278 
279     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_ = nullptr;
280 };
281 
282 }
283 }
284 
285 #endif
286