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_POLICY_UTILS_H 16 #define ST_AUDIO_POLICY_UTILS_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_ec_info.h" 29 #include "datashare_helper.h" 30 #include "audio_errors.h" 31 #include "audio_state_manager.h" 32 #include "audio_device_manager.h" 33 #include "audio_stream_collector.h" 34 35 #include "audio_a2dp_offload_flag.h" 36 #include "audio_config_manager.h" 37 38 namespace OHOS { 39 namespace AudioStandard { 40 41 const int64_t SET_BT_ABS_SCENE_DELAY_MS = 120000; // 120ms 42 const int64_t CALL_IPC_COST_TIME_MS = 20000000; // 20ms 43 44 class AudioPolicyUtils { 45 public: GetInstance()46 static AudioPolicyUtils& GetInstance() 47 { 48 static AudioPolicyUtils instance; 49 return instance; 50 } 51 void WriteServiceStartupError(std::string reason); 52 std::string GetRemoteModuleName(std::string networkId, DeviceRole role); 53 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetAvailableDevicesInner(AudioDeviceUsage usage); 54 void SetBtConnecting(bool flag); 55 int32_t SetPreferredDevice(const PreferredType preferredType, const std::shared_ptr<AudioDeviceDescriptor> &desc, 56 const int32_t uid = INVALID_UID, const std::string caller = ""); 57 void ClearScoDeviceSuspendState(std::string macAddress = ""); 58 int64_t GetCurrentTimeMS(); 59 std::string GetNewSinkPortName(DeviceType deviceType); 60 std::string GetSinkPortName(DeviceType deviceType, AudioPipeType pipeType = PIPE_TYPE_UNKNOWN); 61 string ConvertToHDIAudioFormat(AudioSampleFormat sampleFormat); 62 std::string GetSinkName(const AudioDeviceDescriptor& desc, int32_t sessionId); 63 uint32_t PcmFormatToBytes(AudioSampleFormat format); 64 std::string GetSourcePortName(DeviceType deviceType); 65 void UpdateDisplayName(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor); 66 void UpdateDisplayNameForRemote(std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor); 67 int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName); 68 void UpdateEffectDefaultSink(DeviceType deviceType); 69 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperInstance(); 70 AudioModuleInfo ConstructRemoteAudioModuleInfo(std::string networkId, 71 DeviceRole deviceRole, DeviceType deviceType); 72 DeviceRole GetDeviceRole(DeviceType deviceType) const; 73 DeviceRole GetDeviceRole(const std::string &role); 74 DeviceRole GetDeviceRole(AudioPin pin) const; 75 DeviceType GetDeviceType(const std::string &deviceName); 76 std::string GetDevicesStr(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 77 78 AudioDeviceUsage GetAudioDeviceUsageByStreamUsage(StreamUsage streamUsage); 79 PreferredType GetPreferredTypeByStreamUsage(StreamUsage streamUsage); 80 81 int32_t UnexcludeOutputDevices(std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs); 82 std::string GetOutputDeviceClassBySinkPortName(std::string sinkPortName); 83 std::string GetInputDeviceClassBySourcePortName(std::string sourcePortName); 84 void SetScoExcluded(bool scoExcluded); 85 bool GetScoExcluded(); 86 private: AudioPolicyUtils()87 AudioPolicyUtils() : streamCollector_(AudioStreamCollector::GetAudioStreamCollector()), 88 audioStateManager_(AudioStateManager::GetAudioStateManager()), 89 audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 90 audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()), 91 audioConfigManager_(AudioConfigManager::GetInstance()) {} ~AudioPolicyUtils()92 ~AudioPolicyUtils() {} 93 int32_t ErasePreferredDeviceByType(const PreferredType preferredType); 94 public: 95 static int32_t startDeviceId; 96 static std::map<std::string, ClassType> portStrToEnum; 97 private: 98 bool isBTReconnecting_ = false; 99 bool isScoExcluded_ = false; 100 DeviceType effectActiveDevice_ = DEVICE_TYPE_NONE; 101 AudioStreamCollector& streamCollector_; 102 AudioStateManager &audioStateManager_; 103 AudioDeviceManager &audioDeviceManager_; 104 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 105 AudioConfigManager& audioConfigManager_; 106 }; 107 108 } 109 } 110 111 #endif