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