• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 #ifndef ST_AUDIO_DEVICE_STATUS_H
16 #define ST_AUDIO_DEVICE_STATUS_H
17 
18 #include <bitset>
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <mutex>
24 #include "singleton.h"
25 #include "audio_group_handle.h"
26 #include "audio_manager_base.h"
27 #include "audio_module_info.h"
28 #include "audio_errors.h"
29 
30 #include "audio_stream_collector.h"
31 #include "audio_device_manager.h"
32 #include "audio_policy_manager_factory.h"
33 #include "audio_effect_service.h"
34 
35 #include "audio_active_device.h"
36 #include "audio_scene_manager.h"
37 #include "audio_volume_manager.h"
38 #include "audio_connected_device.h"
39 #include "audio_microphone_descriptor.h"
40 #include "audio_offload_stream.h"
41 #include "audio_device_common.h"
42 #include "audio_capturer_session.h"
43 #include "audio_iohandle_map.h"
44 #include "audio_a2dp_device.h"
45 #include "audio_ec_manager.h"
46 #include "audio_a2dp_offload_flag.h"
47 #include "audio_config_manager.h"
48 #include "audio_router_map.h"
49 #include "audio_a2dp_offload_manager.h"
50 #include "audio_spatialization_service.h"
51 
52 namespace OHOS {
53 namespace AudioStandard {
54 
55 class AudioDeviceStatus {
56 public:
GetInstance()57     static AudioDeviceStatus& GetInstance()
58     {
59         static AudioDeviceStatus instance;
60         return instance;
61     }
62     void Init(std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager,
63         std::shared_ptr<AudioPolicyServerHandler> handler);
64     void DeInit();
65     void OnDeviceStatusUpdated(DeviceType devType, bool isConnected,
66         const std::string &macAddress, const std::string &deviceName,
67         const AudioStreamInfo &streamInfo, DeviceRole role = DEVICE_ROLE_NONE, bool hasPair = false);
68     void OnBlockedStatusUpdated(DeviceType devType, DeviceBlockStatus status);
69     void OnPnpDeviceStatusUpdated(AudioDeviceDescriptor &desc, bool isConnected);
70     void OnMicrophoneBlockedUpdate(DeviceType devType, DeviceBlockStatus status);
71     void OnDeviceConfigurationChanged(DeviceType deviceType,
72         const std::string &macAddress, const std::string &deviceName,
73         const AudioStreamInfo &streamInfo);
74     void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false);
75     int32_t OnServiceConnected(AudioServiceIndex serviceIndex);
76     void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress);
77     void OnDeviceStatusUpdated(AudioDeviceDescriptor &updatedDesc, DeviceType devType,
78         std::string macAddress, std::string deviceName, bool isActualConnection, AudioStreamInfo streamInfo,
79         bool isConnected);
80     void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command);
81 private:
AudioDeviceStatus()82     AudioDeviceStatus() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
83         streamCollector_(AudioStreamCollector::GetAudioStreamCollector()),
84         audioEffectService_(AudioEffectService::GetAudioEffectService()),
85         audioStateManager_(AudioStateManager::GetAudioStateManager()),
86         audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()),
87         audioActiveDevice_(AudioActiveDevice::GetInstance()),
88         audioSceneManager_(AudioSceneManager::GetInstance()),
89         audioVolumeManager_(AudioVolumeManager::GetInstance()),
90         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
91         audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()),
92         audioOffloadStream_(AudioOffloadStream::GetInstance()),
93         audioDeviceCommon_(AudioDeviceCommon::GetInstance()),
94         audioCapturerSession_(AudioCapturerSession::GetInstance()),
95         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
96         audioA2dpDevice_(AudioA2dpDevice::GetInstance()),
97         audioEcManager_(AudioEcManager::GetInstance()),
98         audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()),
99         audioConfigManager_(AudioConfigManager::GetInstance()),
100         audioRouteMap_(AudioRouteMap::GetInstance()) {}
~AudioDeviceStatus()101     ~AudioDeviceStatus() {}
102 
103     void UpdateLocalGroupInfo(bool isConnected, const std::string& macAddress,
104         const std::string& deviceName, const DeviceStreamInfo& streamInfo, AudioDeviceDescriptor& deviceDesc);
105     int32_t HandleLocalDeviceConnected(AudioDeviceDescriptor &updatedDesc);
106     int32_t HandleLocalDeviceDisconnected(const AudioDeviceDescriptor &updatedDesc);
107     void UpdateActiveA2dpDeviceWhenDisconnecting(const std::string& macAddress);
108     int32_t RehandlePnpDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address);
109     int32_t HandleArmUsbDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address);
110     int32_t HandleDpDevice(DeviceType deviceType, const std::string &address);
111     int32_t HandleSpecialDeviceType(DeviceType &devType, bool &isConnected,
112         const std::string &address, DeviceRole role);
113     void TriggerAvailableDeviceChangedCallback(
114         const vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected);
115     void TriggerDeviceChangedCallback(
116         const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection);
117     void TriggerMicrophoneBlockedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc,
118         DeviceBlockStatus status);
119     int32_t HandleDistributedDeviceUpdate(DStatusInfo &statusInfo,
120         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb);
121     void OnPreferredDeviceUpdated(const AudioDeviceDescriptor& deviceDescriptor, DeviceType activeInputDevice);
122     void UpdateDeviceList(AudioDeviceDescriptor &updatedDesc, bool isConnected,
123         std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb,
124         AudioStreamDeviceChangeReasonExt &reason);
125 #ifdef BLUETOOTH_ENABLE
126     void CheckAndActiveHfpDevice(AudioDeviceDescriptor &desc);
127 #endif
128     void CheckForA2dpSuspend(AudioDeviceDescriptor &desc);
129     void UpdateAllUserSelectDevice(vector<shared_ptr<AudioDeviceDescriptor>> &userSelectDeviceMap,
130         AudioDeviceDescriptor &desc, const std::shared_ptr<AudioDeviceDescriptor> &selectDesc);
131     bool IsConfigurationUpdated(DeviceType deviceType, const AudioStreamInfo &streamInfo);
132     std::vector<std::shared_ptr<AudioDeviceDescriptor>> UserSelectDeviceMapInit();
133     void OnPreferredStateUpdated(AudioDeviceDescriptor &desc,
134         const DeviceInfoUpdateCommand updateCommand, AudioStreamDeviceChangeReasonExt &reason);
135     void AddEarpiece();
136     void ReloadA2dpOffloadOnDeviceChanged(DeviceType deviceType, const std::string &macAddress,
137         const std::string &deviceName, const AudioStreamInfo &streamInfo);
138     void AddAudioDevice(AudioModuleInfo& moduleInfo, DeviceType devType);
139     bool OpenPortAndAddDeviceOnServiceConnected(AudioModuleInfo &moduleInfo);
140     int32_t GetModuleInfo(ClassType classType, std::string &moduleInfoStr);
141     int32_t LoadDpModule(std::string deviceInfo);
142     int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote);
143 
144     void DeviceUpdateClearRecongnitionStatus(AudioDeviceDescriptor &desc);
145     void HandleOfflineDistributedDevice();
146     DeviceType GetDeviceTypeFromPin(AudioPin pin);
147     bool NoNeedChangeUsbDevice(const string &address);
148     void WriteAllDeviceSysEvents(
149         const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected);
150     void WriteHeadsetSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &desc, bool isConnected);
151     void WriteDeviceChangeSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &desc);
152     void WriteOutputDeviceChangedSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor,
153         const SinkInput &sinkInput);
154     void WriteInputDeviceChangedSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor,
155         const SourceOutput &sourceOutput);
156 private:
157     IAudioPolicyInterface& audioPolicyManager_;
158     AudioStreamCollector& streamCollector_;
159     AudioEffectService& audioEffectService_;
160     AudioStateManager &audioStateManager_;
161     AudioDeviceManager &audioDeviceManager_;
162     AudioActiveDevice& audioActiveDevice_;
163     AudioSceneManager& audioSceneManager_;
164     AudioVolumeManager& audioVolumeManager_;
165     AudioConnectedDevice& audioConnectedDevice_;
166     AudioMicrophoneDescriptor& audioMicrophoneDescriptor_;
167     AudioOffloadStream& audioOffloadStream_;
168     AudioDeviceCommon& audioDeviceCommon_;
169     AudioCapturerSession& audioCapturerSession_;
170     AudioIOHandleMap& audioIOHandleMap_;
171     AudioA2dpDevice& audioA2dpDevice_;
172     AudioEcManager& audioEcManager_;
173     AudioA2dpOffloadFlag& audioA2dpOffloadFlag_;
174     AudioConfigManager& audioConfigManager_;
175     AudioRouteMap& audioRouteMap_;
176 
177     bool remoteCapturerSwitch_ = false;
178     std::vector<std::pair<AudioDeviceDescriptor, bool>> pnpDeviceList_;
179 
180     static std::map<std::string, AudioSampleFormat> formatStrToEnum;
181     std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr;
182     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_ = nullptr;
183     bool hasModulesLoaded = false;
184 };
185 
186 }
187 }
188 
189 #endif
190