• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "audio_stream_info.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 class StreamVolume;
27 class SystemVolume;
28 class AppVolume;
29 enum FadePauseState {
30     NO_FADE,
31     DO_FADE,
32     DONE_FADE,
33     INVALID_STATE
34 };
35 
36 class AudioVolume {
37 public:
38     static AudioVolume *GetInstance();
39     ~AudioVolume();
40 
41     float GetVolume(uint32_t sessionId, int32_t volumeType, const std::string &deviceClass); // all volume
42     float GetStreamVolume(uint32_t sessionId); // only stream volume
43     float GetAppVolume(int32_t appUid, AudioVolumeMode mode);
44     // history volume
45     float GetHistoryVolume(uint32_t sessionId);
46     void SetHistoryVolume(uint32_t sessionId, float volume);
47 
48     // stream volume
49     void AddStreamVolume(uint32_t sessionId, int32_t streamType, int32_t streamUsage, int32_t uid, int32_t pid,
50         bool isSystemApp, int32_t mode);
51     void RemoveStreamVolume(uint32_t sessionId);
52     void SetStreamVolume(uint32_t sessionId, float volume);
53     void SetStreamVolumeDuckFactor(uint32_t sessionId, float duckFactor);
54     void SetStreamVolumeLowPowerFactor(uint32_t sessionId, float lowPowerFactor);
55     void SetStreamVolumeMute(uint32_t sessionId, bool isMuted);
56     void SetStreamVolumeFade(uint32_t sessionId, float fadeBegin, float fadeEnd);
57     std::pair<float, float> GetStreamVolumeFade(uint32_t sessionId);
58 
59     // system volume
60     void SetSystemVolume(SystemVolume &systemVolume);
61     void SetAppVolume(AppVolume &appVolume);
62     void SetAppVolumeMute(int32_t appUid, bool muted);
63     void SetSystemVolume(int32_t volumeType, const std::string &deviceClass, float volume, int32_t volumeLevel);
64     void SetSystemVolumeMute(int32_t volumeType, const std::string &deviceClass, bool isMuted);
65 
66     // stream type convert
67     int32_t ConvertStreamTypeStrToInt(const std::string &streamType);
68     bool IsSameVolume(float x, float y);
69     void Dump(std::string &dumpString);
70     void Monitor(uint32_t sessionId, bool isOutput);
71 
72     void SetFadeoutState(uint32_t streamIndex, uint32_t fadeoutState);
73     uint32_t GetFadeoutState(uint32_t streamIndex);
74     void RemoveFadeoutState(uint32_t streamIndex);
75 
76     void SetStopFadeoutState(uint32_t streamIndex, uint32_t fadeoutState);
77     uint32_t GetStopFadeoutState(uint32_t streamIndex);
78     void RemoveStopFadeoutState(uint32_t streamIndex);
79 
80     void SetDefaultAppVolume(int32_t level);
81     void SetVgsVolumeSupported(bool isVgsSupported);
82     bool IsVgsVolumeSupported() const;
83 private:
84     AudioVolume();
85     float GetStreamVolumeInternal(uint32_t sessionId, int32_t& volumeType,
86         int32_t& appUid, AudioVolumeMode& volumeMode);
87     float GetSystemVolumeInternal(int32_t volumeType, const std::string &deviceClass, int32_t &volumeLevel);
88     bool IsChangeVolume(uint32_t sessionId, float volumeFloat, int32_t volumeLevel);
89 private:
90     std::unordered_map<uint32_t, StreamVolume> streamVolume_ {};
91     std::unordered_map<std::string, SystemVolume> systemVolume_ {};
92     std::unordered_map<int32_t, AppVolume> appVolume_ {};
93     std::unordered_map<uint32_t, float> historyVolume_ {};
94     std::unordered_map<uint32_t, std::pair<float, int32_t>> monitorVolume_ {};
95     std::shared_mutex volumeMutex_ {};
96     std::shared_mutex systemMutex_ {};
97     std::shared_mutex appMutex_ {};
98     bool isVgsVolumeSupported_ = false;
99     std::shared_mutex fadoutMutex_ {};
100     std::unordered_map<uint32_t, uint32_t> fadeoutState_{};
101     std::unordered_map<uint32_t, uint32_t> stopFadeoutState_{};
102     int32_t defaultAppVolume_ = 0;
103 };
104 
105 class StreamVolume {
106 public:
StreamVolume(uint32_t sessionId,int32_t streamType,int32_t streamUsage,int32_t uid,int32_t pid,bool isSystemApp,int32_t mode)107     StreamVolume(uint32_t sessionId, int32_t streamType, int32_t streamUsage, int32_t uid, int32_t pid,
108         bool isSystemApp, int32_t mode) : sessionId_(sessionId), streamType_(streamType), streamUsage_(streamUsage),
109         appUid_(uid), appPid_(pid), isSystemApp_(isSystemApp), volumeMode_(mode) {};
110     ~StreamVolume() = default;
GetSessionId()111     uint32_t GetSessionId() {return sessionId_;};
GetStreamType()112     int32_t GetStreamType() {return streamType_;};
GetStreamUsage()113     int32_t GetStreamUsage() {return streamUsage_;};
GetAppUid()114     int32_t GetAppUid() {return appUid_;};
GetAppPid()115     int32_t GetAppPid() {return appPid_;};
isSystemApp()116     bool isSystemApp() {return isSystemApp_;};
GetvolumeMode()117     int32_t GetvolumeMode() {return volumeMode_;};
118 public:
119     float volume_ = 1.0f;
120     float duckFactor_ = 1.0f;
121     float lowPowerFactor_ = 1.0f;
122     bool isMuted_ = false;
123     float fadeBegin_ = 1.0f;
124     float fadeEnd_ = 1.0f;
125 
126 private:
127     uint32_t sessionId_ = 0;
128     int32_t streamType_ = 0;
129     int32_t streamUsage_ = 0;
130     int32_t appUid_ = 0;
131     int32_t appPid_ = 0;
132     bool isSystemApp_ = false;
133     int32_t volumeMode_ = 0;
134 };
135 
136 class SystemVolume {
137 public:
SystemVolume(int32_t volumeType,std::string deviceClass,float volume,int32_t volumeLevel,bool isMuted)138     SystemVolume(int32_t volumeType, std::string deviceClass, float volume, int32_t volumeLevel, bool isMuted)
139         : volumeType_(volumeType), deviceClass_(deviceClass), volume_(volume),
140         volumeLevel_(volumeLevel), isMuted_(isMuted) {};
141     ~SystemVolume() = default;
GetVolumeType()142     int32_t GetVolumeType() {return volumeType_;};
GetDeviceClass()143     std::string GetDeviceClass() {return deviceClass_;};
144 
145 private:
146     int32_t volumeType_ = 0;
147     std::string deviceClass_ = "";
148 
149 public:
150     float volume_ = 0.0f;
151     int32_t volumeLevel_ = 0;
152     bool isMuted_ = false;
153 };
154 
155 class AppVolume {
156 public:
AppVolume(int32_t appUid,float volume,int32_t volumeLevel,bool isMuted)157     AppVolume(int32_t appUid, float volume, int32_t volumeLevel, bool isMuted)
158         : appUid_(appUid), volume_(volume),
159         volumeLevel_(volumeLevel), isMuted_(isMuted) {};
160     ~AppVolume() = default;
GetAppUid()161     int32_t GetAppUid() {return appUid_;};
162 
163 private:
164     int32_t appUid_ = 0;
165 
166 public:
167     float volume_ = 0.0f;
168     int32_t volumeLevel_ = 0;
169     bool isMuted_ = false;
170 };
171 } // namespace AudioStandard
172 } // namespace OHOS
173 #endif
174