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 #ifndef ST_AUDIO_ACTIVE_DEVICE_H 16 #define ST_AUDIO_ACTIVE_DEVICE_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_volume_config.h" 29 #include "audio_system_manager.h" 30 #include "audio_errors.h" 31 #include "audio_device_manager.h" 32 #include "audio_affinity_manager.h" 33 #include "audio_policy_manager_factory.h" 34 35 36 #include "audio_a2dp_offload_flag.h" 37 #include "audio_a2dp_device.h" 38 #include "audio_iohandle_map.h" 39 40 namespace OHOS { 41 namespace AudioStandard { 42 43 class AudioActiveDevice { 44 public: GetInstance()45 static AudioActiveDevice& GetInstance() 46 { 47 static AudioActiveDevice instance; 48 return instance; 49 } 50 bool CheckActiveOutputDeviceSupportOffload(); 51 bool IsDirectSupportedDevice(); 52 void NotifyUserSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor); 53 void DisconnectScoWhenUserSelectInput(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor); 54 55 bool UpdateDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, const AudioStreamDeviceChangeReasonExt reason, 56 const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo); 57 bool IsDeviceActive(DeviceType deviceType); 58 float GetMaxAmplitude(const int32_t deviceId, const AudioInterrupt audioInterrupt); 59 void UpdateInputDeviceInfo(DeviceType deviceType); 60 std::string GetActiveBtDeviceMac(); 61 void SetActiveBtDeviceMac(const std::string macAddress); 62 void SetActiveBtInDeviceMac(const std::string macAddress); 63 int32_t SetDeviceActive(DeviceType deviceType, bool active, const int32_t uid = INVALID_UID); 64 int32_t SetCallDeviceActive(DeviceType deviceType, bool active, std::string address, 65 const int32_t uid = INVALID_UID); 66 bool GetActiveA2dpDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo); 67 68 void SetCurrentInputDevice(const AudioDeviceDescriptor &desc); 69 const AudioDeviceDescriptor& GetCurrentInputDevice(); 70 DeviceType GetCurrentInputDeviceType(); 71 void SetCurrentInputDeviceType(DeviceType deviceType); 72 std::string GetCurrentInputDeviceMacAddr(); 73 void SetCurrentOutputDevice(const AudioDeviceDescriptor &desc); 74 void SetCurrentOutputDeviceType(DeviceType deviceType); 75 const AudioDeviceDescriptor& GetCurrentOutputDevice(); 76 DeviceType GetCurrentOutputDeviceType(); 77 DeviceCategory GetCurrentOutputDeviceCategory(); 78 std::string GetCurrentOutputDeviceNetworkId(); 79 std::string GetCurrentOutputDeviceMacAddr(); 80 void UpdateActiveDeviceRoute(InternalDeviceType deviceType, DeviceFlag deviceFlag, 81 const std::string &deviceName = ""); 82 void UpdateActiveDevicesRoute(std::vector<std::pair<InternalDeviceType, DeviceFlag>> &activeDevices, 83 const std::string &deviceName = ""); 84 private: AudioActiveDevice()85 AudioActiveDevice() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), 86 audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 87 audioAffinityManager_(AudioAffinityManager::GetAudioAffinityManager()), 88 audioA2dpDevice_(AudioA2dpDevice::GetInstance()), 89 audioIOHandleMap_(AudioIOHandleMap::GetInstance()), 90 audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()) {} ~AudioActiveDevice()91 ~AudioActiveDevice() {} 92 void WriteOutputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc, 93 const AudioStreamDeviceChangeReason reason); 94 void HandleActiveBt(DeviceType deviceType, std::string macAddress); 95 void HandleNegtiveBt(DeviceType deviceType); 96 private: 97 std::mutex curOutputDevice_; // lock this mutex to operate currentActiveDevice_ 98 AudioDeviceDescriptor currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); 99 std::mutex curInputDevice_; // lock this mutex to operate currentActiveInputDevice_ 100 AudioDeviceDescriptor currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); 101 102 std::string activeBTDevice_; 103 std::string activeBTInDevice_; 104 105 IAudioPolicyInterface& audioPolicyManager_; 106 AudioDeviceManager &audioDeviceManager_; 107 AudioAffinityManager &audioAffinityManager_; 108 AudioA2dpDevice& audioA2dpDevice_; 109 AudioIOHandleMap& audioIOHandleMap_; 110 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 111 }; 112 113 } 114 } 115 116 #endif 117