1 /* 2 * Copyright (C) 2021-2023 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 TELEPHONY_AUDIO_DEVICE_MANAGER_H 17 #define TELEPHONY_AUDIO_DEVICE_MANAGER_H 18 19 #include <map> 20 21 #include "audio_base.h" 22 #include "call_base.h" 23 #include "call_manager_inner_type.h" 24 #include "singleton.h" 25 26 namespace OHOS { 27 namespace Telephony { 28 /** 29 * @class AudioDeviceManager 30 * describes the available devices of a call. 31 */ 32 class AudioDeviceManager : public std::enable_shared_from_this<AudioDeviceManager> { 33 DECLARE_DELAYED_SINGLETON(AudioDeviceManager) 34 public: 35 void Init(); 36 bool InitAudioDevice(); 37 bool ProcessEvent(AudioEvent event); 38 static bool IsEarpieceAvailable(); 39 static bool IsSpeakerAvailable(); 40 static bool IsBtScoConnected(); 41 static bool IsBtActived(); 42 static bool IsNearlinkActived(AudioDevice &device); 43 static bool IsBtHearingAidActived(AudioDevice &device); 44 static bool IsDistributedCallConnected(); 45 static bool IsWiredHeadsetConnected(); 46 static void SetDeviceAvailable(AudioDeviceType deviceType, bool available); 47 static bool SetVirtualCall(bool isVirtual); 48 bool SwitchDevice(AudioDeviceType device); 49 void AddAudioDeviceList(const std::string &address, AudioDeviceType deviceType, const std::string &deviceName); 50 void RemoveAudioDeviceList(const std::string &address, AudioDeviceType deviceType); 51 void ResetBtAudioDevicesList(); 52 void ResetDistributedCallDevicesList(); 53 void ResetNearlinkAudioDevicesList(); 54 void ResetBtHearingAidDeviceList(); 55 int32_t ReportAudioDeviceChange(const AudioDevice &device); 56 bool CheckAndSwitchDistributedAudioDevice(); 57 int32_t ReportAudioDeviceInfo(); 58 int32_t ReportAudioDeviceInfo(sptr<CallBase> call); 59 void SetCurrentAudioDevice(AudioDeviceType deviceType); 60 void SetCurrentAudioDevice(const AudioDevice &device); 61 void OnActivedCallDisconnected(); 62 void SetMuteState(bool isMuted); 63 bool GetMuteState(); 64 std::string ConvertAddress(); 65 66 /** 67 * UpdateEarpieceDevice 68 * 69 * @brief update earpiece device 70 */ 71 void UpdateEarpieceDevice(); 72 73 /** 74 * @brief Update bluetooth device name 75 * 76 * @param macAddress[in], mac address 77 * @param deviceName[in], device name 78 */ 79 void UpdateBluetoothDeviceName(const std::string &macAddress, const std::string &deviceName); 80 81 AudioDeviceType GetCurrentAudioDevice(); 82 83 private: 84 bool SwitchDevice(AudioEvent event); 85 bool EnableBtSco(); 86 bool EnableNearlink(); 87 bool EnableBtHearingAid(); 88 bool EnableWiredHeadset(); 89 bool EnableSpeaker(); 90 bool EnableEarpiece(); 91 bool DisableAll(); 92 bool IsBtScoDevEnable(); 93 bool IsDCallDevEnable(); 94 bool IsSpeakerDevEnable(); 95 bool IsEarpieceDevEnable(); 96 bool IsWiredHeadsetDevEnable(); 97 bool IsDistributedAudioDeviceType(AudioDeviceType deviceType); 98 bool IsSupportEarpiece(); 99 void AddEarpiece(); 100 void UpdateBtDevice(std::string &address, std::string &deviceName); 101 void UpdateNearlinkDevice(std::string &address, std::string &deviceName); 102 void UpdateBtHearingAidDevice(std::string &address, std::string &deviceName); 103 std::mutex mutex_; 104 std::mutex infoMutex_; 105 AudioDeviceType audioDeviceType_; 106 static bool isBtScoDevEnable_; 107 static bool isDCallDevEnable_; 108 bool isWiredHeadsetDevEnable_ = false; 109 bool isSpeakerDevEnable_ = false; 110 bool isEarpieceDevEnable_ = false; 111 std::unique_ptr<AudioBase> currentAudioDevice_; 112 static bool isEarpieceAvailable_; 113 static bool isSpeakerAvailable_; 114 static bool isUpdateEarpieceDevice_; 115 static bool isWiredHeadsetConnected_; 116 static bool isBtScoConnected_; 117 static bool isDCallDevConnected_; 118 bool isAudioActivated_; 119 using AudioDeviceManagerFunc = std::function<bool()>; 120 std::map<uint32_t, AudioDeviceManagerFunc> memberFuncMap_; 121 AudioDeviceInfo info_; 122 }; 123 } // namespace Telephony 124 } // namespace OHOS 125 #endif // TELEPHONY_AUDIO_DEVICE_MANAGER_H