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_policy_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 std::shared_ptr<AudioDeviceDescriptor> GetDeviceByStatusInfo(const DStatusInfo &statusInfo); 75 void OnDeviceStatusUpdated(DStatusInfo statusInfo, bool isStop = false); 76 int32_t OnServiceConnected(AudioServiceIndex serviceIndex); 77 void OnForcedDeviceSelected(DeviceType devType, const std::string &macAddress); 78 void OnDeviceStatusUpdated(AudioDeviceDescriptor &updatedDesc, DeviceType devType, 79 std::string macAddress, std::string deviceName, bool isActualConnection, AudioStreamInfo streamInfo, 80 bool isConnected); 81 void OnDeviceInfoUpdated(AudioDeviceDescriptor &desc, const DeviceInfoUpdateCommand command); 82 uint16_t GetDmDeviceType(); 83 void RemoveDeviceFromGlobalOnly(std::shared_ptr<AudioDeviceDescriptor> desc); 84 void AddDeviceBackToGlobalOnly(std::shared_ptr<AudioDeviceDescriptor> desc); 85 private: AudioDeviceStatus()86 AudioDeviceStatus() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), 87 streamCollector_(AudioStreamCollector::GetAudioStreamCollector()), 88 audioEffectService_(AudioEffectService::GetAudioEffectService()), 89 audioStateManager_(AudioStateManager::GetAudioStateManager()), 90 audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 91 audioActiveDevice_(AudioActiveDevice::GetInstance()), 92 audioSceneManager_(AudioSceneManager::GetInstance()), 93 audioVolumeManager_(AudioVolumeManager::GetInstance()), 94 audioConnectedDevice_(AudioConnectedDevice::GetInstance()), 95 audioMicrophoneDescriptor_(AudioMicrophoneDescriptor::GetInstance()), 96 audioOffloadStream_(AudioOffloadStream::GetInstance()), 97 audioDeviceCommon_(AudioDeviceCommon::GetInstance()), 98 audioCapturerSession_(AudioCapturerSession::GetInstance()), 99 audioIOHandleMap_(AudioIOHandleMap::GetInstance()), 100 audioA2dpDevice_(AudioA2dpDevice::GetInstance()), 101 audioEcManager_(AudioEcManager::GetInstance()), 102 audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()), 103 audioConfigManager_(AudioPolicyConfigManager::GetInstance()), 104 audioRouteMap_(AudioRouteMap::GetInstance()) {} ~AudioDeviceStatus()105 ~AudioDeviceStatus() {} 106 107 void UpdateLocalGroupInfo(bool isConnected, const std::string& macAddress, 108 const std::string& deviceName, const DeviceStreamInfo& streamInfo, AudioDeviceDescriptor& deviceDesc); 109 int32_t HandleLocalDeviceConnected(AudioDeviceDescriptor &updatedDesc); 110 int32_t HandleLocalDeviceDisconnected(const AudioDeviceDescriptor &updatedDesc); 111 void UpdateActiveA2dpDeviceWhenDisconnecting(const std::string& macAddress); 112 int32_t RehandlePnpDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address); 113 int32_t HandleArmUsbDevice(DeviceType deviceType, DeviceRole deviceRole, const std::string &address); 114 int32_t HandleDpDevice(DeviceType deviceType, const std::string &address); 115 int32_t HandleAccessoryDevice(DeviceType deviceType, const std::string &address); 116 int32_t LoadAccessoryModule(std::string deviceInfo); 117 int32_t HandleSpecialDeviceType(DeviceType &devType, bool &isConnected, 118 const std::string &address, DeviceRole role); 119 void TriggerAvailableDeviceChangedCallback( 120 const vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 121 void TriggerDeviceChangedCallback( 122 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection); 123 void TriggerMicrophoneBlockedCallback(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, 124 DeviceBlockStatus status); 125 int32_t HandleDistributedDeviceUpdate(DStatusInfo &statusInfo, 126 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb, AudioStreamDeviceChangeReasonExt &reason); 127 void OnPreferredDeviceUpdated(const AudioDeviceDescriptor& deviceDescriptor, DeviceType activeInputDevice); 128 void UpdateDeviceList(AudioDeviceDescriptor &updatedDesc, bool isConnected, 129 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descForCb, 130 AudioStreamDeviceChangeReasonExt &reason); 131 #ifdef BLUETOOTH_ENABLE 132 void CheckAndActiveHfpDevice(AudioDeviceDescriptor &desc); 133 #endif 134 void CheckForA2dpSuspend(AudioDeviceDescriptor &desc); 135 void UpdateAllUserSelectDevice(vector<shared_ptr<AudioDeviceDescriptor>> &userSelectDeviceMap, 136 AudioDeviceDescriptor &desc, const std::shared_ptr<AudioDeviceDescriptor> &selectDesc); 137 bool IsConfigurationUpdated(DeviceType deviceType, const AudioStreamInfo &streamInfo); 138 std::vector<std::shared_ptr<AudioDeviceDescriptor>> UserSelectDeviceMapInit(); 139 void OnPreferredStateUpdated(AudioDeviceDescriptor &desc, 140 const DeviceInfoUpdateCommand updateCommand, AudioStreamDeviceChangeReasonExt &reason); 141 void AddEarpiece(); 142 void ReloadA2dpOffloadOnDeviceChanged(DeviceType deviceType, const std::string &macAddress, 143 const std::string &deviceName, const AudioStreamInfo &streamInfo); 144 void AddAudioDevice(AudioModuleInfo& moduleInfo, DeviceType devType); 145 bool OpenPortAndAddDeviceOnServiceConnected(AudioModuleInfo &moduleInfo); 146 int32_t GetModuleInfo(ClassType classType, std::string &moduleInfoStr); 147 int32_t LoadDpModule(std::string deviceInfo); 148 int32_t ActivateNewDevice(std::string networkId, DeviceType deviceType, bool isRemote); 149 int32_t RestoreNewA2dpPort(std::vector<std::shared_ptr<AudioStreamDescriptor>> &streamDescs, 150 AudioModuleInfo &moduleInfo, std::string ¤tActivePort); 151 uint32_t GetPaIndexByPortName(std::string &portName); 152 153 void DeactivateNearlinkDevice(AudioDeviceDescriptor &desc); 154 155 void HandleOfflineDistributedDevice(); 156 DeviceType GetDeviceTypeFromPin(AudioPin pin); 157 string GetModuleNameByType(ClassType type); 158 bool NoNeedChangeUsbDevice(const string &address); 159 void WriteAllDeviceSysEvents( 160 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc, bool isConnected); 161 void WriteHeadsetSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &desc, bool isConnected); 162 void WriteDeviceChangeSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &desc); 163 void WriteOutputDeviceChangedSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 164 const SinkInput &sinkInput); 165 void WriteInputDeviceChangedSysEvents(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, 166 const SourceOutput &sourceOutput); 167 private: 168 IAudioPolicyInterface& audioPolicyManager_; 169 AudioStreamCollector& streamCollector_; 170 AudioEffectService& audioEffectService_; 171 AudioStateManager &audioStateManager_; 172 AudioDeviceManager &audioDeviceManager_; 173 AudioActiveDevice& audioActiveDevice_; 174 AudioSceneManager& audioSceneManager_; 175 AudioVolumeManager& audioVolumeManager_; 176 AudioConnectedDevice& audioConnectedDevice_; 177 AudioMicrophoneDescriptor& audioMicrophoneDescriptor_; 178 AudioOffloadStream& audioOffloadStream_; 179 AudioDeviceCommon& audioDeviceCommon_; 180 AudioCapturerSession& audioCapturerSession_; 181 AudioIOHandleMap& audioIOHandleMap_; 182 AudioA2dpDevice& audioA2dpDevice_; 183 AudioEcManager& audioEcManager_; 184 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 185 AudioPolicyConfigManager& audioConfigManager_; 186 AudioRouteMap& audioRouteMap_; 187 188 bool remoteCapturerSwitch_ = false; 189 std::vector<std::pair<AudioDeviceDescriptor, bool>> pnpDeviceList_; 190 191 static std::map<std::string, AudioSampleFormat> formatStrToEnum; 192 std::shared_ptr<AudioA2dpOffloadManager> audioA2dpOffloadManager_ = nullptr; 193 std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_ = nullptr; 194 bool hasModulesLoaded = false; 195 uint16_t dmDeviceType_ = 0; 196 }; 197 198 } 199 } 200 201 #endif 202