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_policy_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 void WriteDeviceChangeExceptionEvent(const AudioStreamDeviceChangeReason reason, 53 DeviceType deviceType, DeviceRole deviceRole, int32_t errorMsg, const std::string &errorDesc); 54 std::string GetRemoteModuleName(std::string networkId, DeviceRole role); 55 std::vector<std::shared_ptr<AudioDeviceDescriptor>> GetAvailableDevicesInner(AudioDeviceUsage usage); 56 void SetBtConnecting(bool flag); 57 int32_t SetPreferredDevice(const PreferredType preferredType, const std::shared_ptr<AudioDeviceDescriptor> &desc, 58 const int32_t uid = INVALID_UID, const std::string caller = ""); 59 void ClearScoDeviceSuspendState(std::string macAddress = ""); 60 int64_t GetCurrentTimeMS(); 61 std::string GetNewSinkPortName(DeviceType deviceType); 62 std::string GetSinkPortName(DeviceType deviceType, AudioPipeType pipeType = PIPE_TYPE_UNKNOWN); 63 string ConvertToHDIAudioFormat(AudioSampleFormat sampleFormat); 64 std::string GetSinkName(const AudioDeviceDescriptor &desc, int32_t sessionId); 65 std::string GetSinkName(std::shared_ptr<AudioDeviceDescriptor> desc, int32_t sessionId); 66 uint32_t PcmFormatToBytes(AudioSampleFormat format); 67 std::string GetSourcePortName(DeviceType deviceType); 68 void UpdateDisplayName(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor); 69 void UpdateDisplayNameForRemote(std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor); 70 int32_t GetDeviceNameFromDataShareHelper(std::string &deviceName); 71 void UpdateEffectDefaultSink(DeviceType deviceType); 72 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperInstance(); 73 AudioModuleInfo ConstructRemoteAudioModuleInfo(std::string networkId, 74 DeviceRole deviceRole, DeviceType deviceType); 75 DeviceRole GetDeviceRole(DeviceType deviceType) const; 76 DeviceRole GetDeviceRole(const std::string &role); 77 DeviceRole GetDeviceRole(AudioPin pin) const; 78 DeviceType GetDeviceType(const std::string &deviceName); 79 std::string GetEncryptAddr(const std::string &addr); 80 std::string GetDevicesStr(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &audioDeviceDescriptors); 81 82 AudioDeviceUsage GetAudioDeviceUsageByStreamUsage(StreamUsage streamUsage); 83 PreferredType GetPreferredTypeByStreamUsage(StreamUsage streamUsage); 84 85 int32_t UnexcludeOutputDevices(std::vector<std::shared_ptr<AudioDeviceDescriptor>> &descs); 86 std::string GetOutputDeviceClassBySinkPortName(std::string sinkPortName); 87 std::string GetInputDeviceClassBySourcePortName(std::string sourcePortName); 88 void SetScoExcluded(bool scoExcluded); 89 bool GetScoExcluded(); 90 bool IsDataShareReady(); 91 private: AudioPolicyUtils()92 AudioPolicyUtils() : streamCollector_(AudioStreamCollector::GetAudioStreamCollector()), 93 audioStateManager_(AudioStateManager::GetAudioStateManager()), 94 audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 95 audioA2dpOffloadFlag_(AudioA2dpOffloadFlag::GetInstance()), 96 audioConfigManager_(AudioPolicyConfigManager::GetInstance()) {} ~AudioPolicyUtils()97 ~AudioPolicyUtils() {} 98 int32_t ErasePreferredDeviceByType(const PreferredType preferredType); 99 public: 100 static int32_t startDeviceId; 101 static std::map<std::string, ClassType> portStrToEnum; 102 private: 103 bool isBTReconnecting_ = false; 104 bool isScoExcluded_ = false; 105 DeviceType effectActiveDevice_ = DEVICE_TYPE_NONE; 106 AudioStreamCollector& streamCollector_; 107 AudioStateManager &audioStateManager_; 108 AudioDeviceManager &audioDeviceManager_; 109 AudioA2dpOffloadFlag& audioA2dpOffloadFlag_; 110 AudioPolicyConfigManager& audioConfigManager_; 111 }; 112 113 } 114 } 115 116 #endif