1 /* 2 * Copyright (c) 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 ST_ROUTER_BASE_H 17 #define ST_ROUTER_BASE_H 18 19 #include "audio_system_manager.h" 20 #include "audio_device_manager.h" 21 #include "audio_policy_manager_factory.h" 22 #include "audio_policy_log.h" 23 #include "audio_state_manager.h" 24 #include "audio_policy_utils.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 class RouterBase { 29 public: 30 std::string name_; 31 IAudioPolicyInterface& audioPolicyManager_; RouterBase()32 RouterBase() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()) {} ~RouterBase()33 virtual ~RouterBase() {}; 34 35 virtual std::shared_ptr<AudioDeviceDescriptor> GetMediaRenderDevice(StreamUsage streamUsage, int32_t clientUID) = 0; 36 virtual std::shared_ptr<AudioDeviceDescriptor> GetCallRenderDevice(StreamUsage streamUsage, int32_t clientUID) = 0; 37 virtual std::shared_ptr<AudioDeviceDescriptor> GetCallCaptureDevice(SourceType sourceType, int32_t clientUID, 38 const uint32_t sessionID = 0) = 0; 39 virtual vector<std::shared_ptr<AudioDeviceDescriptor>> GetRingRenderDevices(StreamUsage streamUsage, 40 int32_t clientUID) = 0; 41 virtual std::shared_ptr<AudioDeviceDescriptor> GetRecordCaptureDevice(SourceType sourceType, int32_t clientUID, 42 const uint32_t sessionID = 0) = 0; 43 virtual std::shared_ptr<AudioDeviceDescriptor> GetToneRenderDevice(StreamUsage streamUsage, int32_t clientUID) = 0; 44 virtual RouterType GetRouterType() = 0; 45 GetClassName()46 virtual std::string GetClassName() 47 { 48 return name_; 49 } 50 IsDeviceUsageSupported(AudioDeviceUsage audioDevUsage,const std::shared_ptr<AudioDeviceDescriptor> & deviceDesc)51 bool IsDeviceUsageSupported(AudioDeviceUsage audioDevUsage, 52 const std::shared_ptr<AudioDeviceDescriptor> &deviceDesc) 53 { 54 CHECK_AND_RETURN_RET_LOG(deviceDesc != nullptr, false, "deviceDesc is nullptr"); 55 uint32_t deviceUsage = static_cast<uint32_t>(deviceDesc->deviceUsage_); 56 switch (audioDevUsage) { 57 case MEDIA_OUTPUT_DEVICES: 58 case MEDIA_INPUT_DEVICES: 59 return (deviceUsage & MEDIA) != 0; 60 case CALL_OUTPUT_DEVICES: 61 case CALL_INPUT_DEVICES: 62 return (deviceUsage & VOICE) != 0; 63 default: 64 return false; 65 } 66 } 67 GetLatestNonExcludedConnectDevice(AudioDeviceUsage audioDevUsage,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & descs)68 std::shared_ptr<AudioDeviceDescriptor> GetLatestNonExcludedConnectDevice(AudioDeviceUsage audioDevUsage, 69 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs) 70 { 71 std::vector<std::shared_ptr<AudioDeviceDescriptor>> filteredDescs; 72 // remove abnormal device or excluded device 73 for (const auto &desc : descs) { 74 CHECK_AND_CONTINUE(desc != nullptr); 75 if (desc->exceptionFlag_ || !desc->isEnable_ || 76 (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && 77 (desc->connectState_ == SUSPEND_CONNECTED || AudioPolicyUtils::GetInstance().GetScoExcluded())) || 78 AudioStateManager::GetAudioStateManager().IsExcludedDevice(audioDevUsage, desc) || 79 !IsDeviceUsageSupported(audioDevUsage, desc)) { 80 continue; 81 } 82 CHECK_AND_CONTINUE(!ExistSameRemoteCarWithA2DP(desc)); 83 filteredDescs.push_back(desc); 84 } 85 if (filteredDescs.size() > 0) { 86 auto compare = [&] (std::shared_ptr<AudioDeviceDescriptor> &desc1, 87 std::shared_ptr<AudioDeviceDescriptor> &desc2) { 88 return desc1->connectTimeStamp_ < desc2->connectTimeStamp_; 89 }; 90 sort(filteredDescs.begin(), filteredDescs.end(), compare); 91 return std::move(filteredDescs.back()); 92 } 93 return std::make_shared<AudioDeviceDescriptor>(); 94 } 95 GetPairDevice(std::shared_ptr<AudioDeviceDescriptor> & targetDevice,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & deviceList)96 std::shared_ptr<AudioDeviceDescriptor> GetPairDevice(std::shared_ptr<AudioDeviceDescriptor> &targetDevice, 97 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &deviceList) 98 { 99 for (auto &device : deviceList) { 100 if (device->deviceRole_ != targetDevice->deviceRole_ || 101 device->deviceType_ != targetDevice->deviceType_ || 102 device->networkId_ != targetDevice->networkId_ || 103 device->macAddress_ != targetDevice->macAddress_) { 104 continue; 105 } 106 if (!device->exceptionFlag_ && device->isEnable_ && 107 (device->deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO || 108 device->connectState_ != SUSPEND_CONNECTED) && 109 device->connectState_ != VIRTUAL_CONNECTED) { 110 return std::move(device); 111 } 112 AUDIO_WARNING_LOG("unavailable device state, type[%{public}d] connectState[%{public}d] " \ 113 "isEnable[%{public}d] exceptionFlag[%{public}d]", device->deviceType_, device->connectState_, 114 device->isEnable_, device->exceptionFlag_); 115 } 116 return std::make_shared<AudioDeviceDescriptor>(); 117 } 118 NeedLatestConnectWithDefaultDevices(DeviceType type)119 bool NeedLatestConnectWithDefaultDevices(DeviceType type) 120 { 121 return type == DEVICE_TYPE_WIRED_HEADSET || 122 type == DEVICE_TYPE_WIRED_HEADPHONES || 123 type == DEVICE_TYPE_BLUETOOTH_SCO || 124 type == DEVICE_TYPE_USB_HEADSET || 125 type == DEVICE_TYPE_BLUETOOTH_A2DP || 126 type == DEVICE_TYPE_USB_ARM_HEADSET || 127 type == DEVICE_TYPE_HEARING_AID || 128 type == DEVICE_TYPE_NEARLINK; 129 } 130 ExistSameRemoteCarWithA2DP(std::shared_ptr<AudioDeviceDescriptor> desc)131 bool ExistSameRemoteCarWithA2DP(std::shared_ptr<AudioDeviceDescriptor> desc) 132 { 133 CHECK_AND_RETURN_RET_LOG(desc != nullptr, false, "desc is nullptr"); 134 if (desc->deviceType_ != DEVICE_TYPE_BLUETOOTH_A2DP) { 135 return false; 136 } 137 if (desc->deviceCategory_ != BT_CAR) { 138 return false; 139 } 140 return AudioDeviceManager::GetAudioDeviceManager().ExistSameRemoteDeviceByMacAddress(desc); 141 } 142 }; 143 } // namespace AudioStandard 144 } // namespace OHOS 145 146 #endif // ST_ROUTER_BASE_H