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) = 0; 38 virtual vector<std::shared_ptr<AudioDeviceDescriptor>> GetRingRenderDevices(StreamUsage streamUsage, 39 int32_t clientUID) = 0; 40 virtual std::shared_ptr<AudioDeviceDescriptor> GetRecordCaptureDevice(SourceType sourceType, int32_t clientUID) = 0; 41 virtual std::shared_ptr<AudioDeviceDescriptor> GetToneRenderDevice(StreamUsage streamUsage, int32_t clientUID) = 0; 42 virtual RouterType GetRouterType() = 0; 43 GetClassName()44 virtual std::string GetClassName() 45 { 46 return name_; 47 } GetLatestNonExcludedConnectDevice(AudioDeviceUsage audioDevUsage,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & descs)48 std::shared_ptr<AudioDeviceDescriptor> GetLatestNonExcludedConnectDevice(AudioDeviceUsage audioDevUsage, 49 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs) 50 { 51 std::vector<std::shared_ptr<AudioDeviceDescriptor>> filteredDescs; 52 // remove abnormal device or excluded device 53 for (const auto &desc : descs) { 54 if (desc->exceptionFlag_ || !desc->isEnable_ || 55 (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO && 56 (desc->connectState_ == SUSPEND_CONNECTED || AudioPolicyUtils::GetInstance().GetScoExcluded())) || 57 AudioStateManager::GetAudioStateManager().IsExcludedDevice(audioDevUsage, desc)) { 58 continue; 59 } 60 filteredDescs.push_back(desc); 61 } 62 if (filteredDescs.size() > 0) { 63 auto compare = [&] (std::shared_ptr<AudioDeviceDescriptor> &desc1, 64 std::shared_ptr<AudioDeviceDescriptor> &desc2) { 65 return desc1->connectTimeStamp_ < desc2->connectTimeStamp_; 66 }; 67 sort(filteredDescs.begin(), filteredDescs.end(), compare); 68 return std::move(filteredDescs.back()); 69 } 70 return std::make_shared<AudioDeviceDescriptor>(); 71 } 72 GetPairDevice(std::shared_ptr<AudioDeviceDescriptor> & targetDevice,std::vector<std::shared_ptr<AudioDeviceDescriptor>> & deviceList)73 std::shared_ptr<AudioDeviceDescriptor> GetPairDevice(std::shared_ptr<AudioDeviceDescriptor> &targetDevice, 74 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &deviceList) 75 { 76 for (auto &device : deviceList) { 77 if (device->deviceRole_ != targetDevice->deviceRole_ || 78 device->deviceType_ != targetDevice->deviceType_ || 79 device->networkId_ != targetDevice->networkId_ || 80 device->macAddress_ != targetDevice->macAddress_) { 81 continue; 82 } 83 if (!device->exceptionFlag_ && device->isEnable_ && 84 (device->deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO || 85 device->connectState_ != SUSPEND_CONNECTED) && 86 device->connectState_ != VIRTUAL_CONNECTED) { 87 return std::move(device); 88 } 89 AUDIO_WARNING_LOG("unavailable device state, type[%{public}d] connectState[%{public}d] " \ 90 "isEnable[%{public}d] exceptionFlag[%{public}d]", device->deviceType_, device->connectState_, 91 device->isEnable_, device->exceptionFlag_); 92 } 93 return std::make_shared<AudioDeviceDescriptor>(); 94 } 95 NeedLatestConnectWithDefaultDevices(DeviceType type)96 bool NeedLatestConnectWithDefaultDevices(DeviceType type) 97 { 98 if (type == DEVICE_TYPE_WIRED_HEADSET || 99 type == DEVICE_TYPE_WIRED_HEADPHONES || 100 type == DEVICE_TYPE_BLUETOOTH_SCO || 101 type == DEVICE_TYPE_USB_HEADSET || 102 type == DEVICE_TYPE_BLUETOOTH_A2DP || 103 type == DEVICE_TYPE_USB_ARM_HEADSET) { 104 return true; 105 } 106 return false; 107 } 108 }; 109 } // namespace AudioStandard 110 } // namespace OHOS 111 112 #endif // ST_ROUTER_BASE_H