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 StreamUsage streamUsage = STREAM_USAGE_UNKNOWN); 54 void NotifyUserDisSelectionEventToBt(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor, 55 bool isSameDevice = false); 56 void NotifyUserSelectionEventForInput(std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor, 57 SourceType sourceType = SOURCE_TYPE_INVALID); 58 59 bool UpdateDevice(std::shared_ptr<AudioDeviceDescriptor> &desc, const AudioStreamDeviceChangeReasonExt reason, 60 const std::shared_ptr<AudioRendererChangeInfo> &rendererChangeInfo); 61 bool IsDeviceActive(DeviceType deviceType); 62 float GetMaxAmplitude(const int32_t deviceId, const AudioInterrupt audioInterrupt); 63 std::string GetActiveBtDeviceMac(); 64 void SetActiveBtDeviceMac(const std::string macAddress); 65 void SetActiveBtInDeviceMac(const std::string macAddress); 66 int32_t SetDeviceActive(DeviceType deviceType, bool active, const int32_t uid = INVALID_UID); 67 int32_t SetCallDeviceActive(DeviceType deviceType, bool active, std::string address, 68 const int32_t uid = INVALID_UID); 69 bool GetActiveA2dpDeviceStreamInfo(DeviceType deviceType, AudioStreamInfo &streamInfo); 70 71 void SetCurrentInputDevice(const AudioDeviceDescriptor &desc); 72 const AudioDeviceDescriptor GetCurrentInputDevice(); 73 DeviceType GetCurrentInputDeviceType(); 74 std::string GetCurrentInputDeviceMacAddr(); 75 void SetCurrentOutputDevice(const AudioDeviceDescriptor &desc); 76 const AudioDeviceDescriptor GetCurrentOutputDevice(); 77 DeviceType GetCurrentOutputDeviceType(); 78 DeviceCategory GetCurrentOutputDeviceCategory(); 79 std::string GetCurrentOutputDeviceNetworkId(); 80 std::string GetCurrentOutputDeviceMacAddr(); 81 void UpdateActiveDeviceRoute(InternalDeviceType deviceType, DeviceFlag deviceFlag, 82 const std::string &deviceName = "", std::string networkId = LOCAL_NETWORK_ID); 83 void UpdateActiveDevicesRoute(std::vector<std::pair<InternalDeviceType, DeviceFlag>> &activeDevices, 84 const std::string &deviceName = ""); 85 bool IsDeviceInVector(std::shared_ptr<AudioDeviceDescriptor> desc, 86 std::vector<std::shared_ptr<AudioDeviceDescriptor>> descs); 87 void UpdateStreamDeviceMap(std::string source); 88 private: AudioActiveDevice()89 AudioActiveDevice() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()), 90 audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 91 audioAffinityManager_(AudioAffinityManager::GetAudioAffinityManager()), 92 audioA2dpDevice_(AudioA2dpDevice::GetInstance()), 93 audioIOHandleMap_(AudioIOHandleMap::GetInstance()), 94 audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()) {} ~AudioActiveDevice()95 ~AudioActiveDevice() {} 96 void WriteOutputRouteChangeEvent(std::shared_ptr<AudioDeviceDescriptor> &desc, 97 const AudioStreamDeviceChangeReason reason); 98 void HandleActiveBt(DeviceType deviceType, std::string macAddress); 99 void HandleNegtiveBt(DeviceType deviceType); 100 private: 101 std::mutex curOutputDevice_; // lock this mutex to operate currentActiveDevice_ 102 AudioDeviceDescriptor currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); 103 std::mutex curInputDevice_; // lock this mutex to operate currentActiveInputDevice_ 104 AudioDeviceDescriptor currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); 105 106 std::string activeBTDevice_; 107 std::string activeBTInDevice_; 108 std::vector<std::shared_ptr<AudioDeviceDescriptor>> activeOutputDevices_; 109 std::unordered_map<AudioStreamType, std::shared_ptr<AudioDeviceDescriptor>> streamTypeDeviceMap_; 110 std::unordered_map<StreamUsage, std::shared_ptr<AudioDeviceDescriptor>> streamUsageDeviceMap_; 111 112 IAudioPolicyInterface& audioPolicyManager_; 113 AudioDeviceManager &audioDeviceManager_; 114 AudioAffinityManager &audioAffinityManager_; 115 AudioA2dpDevice& audioA2dpDevice_; 116 AudioIOHandleMap& audioIOHandleMap_; 117 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 118 }; 119 120 } 121 } 122 123 #endif 124