1 /* 2 * Copyright (c) 2024 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 AUDIO_VOLUME_H 17 #define AUDIO_VOLUME_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <shared_mutex> 22 23 namespace OHOS { 24 namespace AudioStandard { 25 class StreamVolume; 26 class SystemVolume; 27 class AudioVolume { 28 public: 29 static AudioVolume *GetInstance(); 30 ~AudioVolume(); 31 32 float GetVolume(uint32_t sessionId, int32_t volumeType, const std::string &deviceClass); 33 34 // history volume 35 float GetHistoryVolume(uint32_t sessionId); 36 void SetHistoryVolume(uint32_t sessionId, float volume); 37 38 // stream volume 39 void AddStreamVolume(uint32_t sessionId, int32_t streamType, int32_t streamUsage, int32_t uid, int32_t pid); 40 void RemoveStreamVolume(uint32_t sessionId); 41 void SetStreamVolume(uint32_t sessionId, float volume); 42 void SetStreamVolumeDuckFactor(uint32_t sessionId, float duckFactor); 43 void SetStreamVolumeLowPowerFactor(uint32_t sessionId, float lowPowerFactor); 44 void SetStreamVolumeMute(uint32_t sessionId, bool isMuted); 45 void SetStreamVolumeFade(uint32_t sessionId, float fadeBegin, float fadeEnd); 46 std::pair<float, float> GetStreamVolumeFade(uint32_t sessionId); 47 48 // system volume 49 void SetSystemVolume(SystemVolume &systemVolume); 50 void SetSystemVolume(int32_t volumeType, const std::string &deviceClass, float volume, int32_t volumeLevel); 51 void SetSystemVolumeMute(int32_t volumeType, const std::string &deviceClass, bool isMuted); 52 53 // stream type convert 54 int32_t ConvertStreamTypeStrToInt(const std::string &streamType); 55 bool IsSameVolume(float x, float y); 56 void Dump(std::string &dumpString); 57 void Monitor(uint32_t sessionId, bool isOutput); 58 59 private: 60 AudioVolume(); 61 62 private: 63 std::unordered_map<uint32_t, StreamVolume> streamVolume_ {}; 64 std::unordered_map<std::string, SystemVolume> systemVolume_ {}; 65 std::unordered_map<uint32_t, float> historyVolume_ {}; 66 std::unordered_map<uint32_t, std::pair<float, int32_t>> monitorVolume_ {}; 67 std::shared_mutex volumeMutex_ {}; 68 }; 69 70 class StreamVolume { 71 public: StreamVolume(uint32_t sessionId, int32_t streamType, int32_t streamUsage, int32_t uid, int32_t pid)72 StreamVolume(uint32_t sessionId, int32_t streamType, int32_t streamUsage, int32_t uid, int32_t pid) 73 : sessionId_(sessionId), streamType_(streamType), streamUsage_(streamUsage), appUid_(uid), appPid_(pid) {}; 74 ~StreamVolume() = default; GetSessionId()75 uint32_t GetSessionId() {return sessionId_;}; GetStreamType()76 int32_t GetStreamType() {return streamType_;}; GetStreamUsage()77 int32_t GetStreamUsage() {return streamUsage_;}; GetAppUid()78 int32_t GetAppUid() {return appUid_;}; GetAppPid()79 int32_t GetAppPid() {return appPid_;}; 80 81 public: 82 float volume_ = 1.0f; 83 float duckFactor_ = 1.0f; 84 float lowPowerFactor_ = 1.0f; 85 bool isMuted_ = false; 86 float fadeBegin_ = 1.0f; 87 float fadeEnd_ = 1.0f; 88 89 private: 90 uint32_t sessionId_ = 0; 91 int32_t streamType_ = 0; 92 int32_t streamUsage_ = 0; 93 int32_t appUid_ = 0; 94 int32_t appPid_ = 0; 95 }; 96 97 class SystemVolume { 98 public: SystemVolume(int32_t volumeType, std::string deviceClass)99 SystemVolume(int32_t volumeType, std::string deviceClass) : volumeType_(volumeType), deviceClass_(deviceClass) {}; SystemVolume(int32_t volumeType, std::string deviceClass, float volume, int32_t volumeLevel, bool isMuted)100 SystemVolume(int32_t volumeType, std::string deviceClass, float volume, int32_t volumeLevel, bool isMuted) 101 : volumeType_(volumeType), deviceClass_(deviceClass), volume_(volume), 102 volumeLevel_(volumeLevel), isMuted_(isMuted) {}; 103 ~SystemVolume() = default; GetVolumeType()104 int32_t GetVolumeType() {return volumeType_;}; GetDeviceClass()105 std::string GetDeviceClass() {return deviceClass_;}; 106 107 private: 108 int32_t volumeType_ = 0; 109 std::string deviceClass_ = ""; 110 111 public: 112 float volume_ = 0.0f; 113 int32_t volumeLevel_ = 0; 114 bool isMuted_ = false; 115 }; 116 } // namespace AudioStandard 117 } // namespace OHOS 118 #endif 119