• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VOLUME_MANAGER_H
16 #define ST_AUDIO_VOLUME_MANAGER_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_shared_memory.h"
29 #include "audio_system_manager.h"
30 #include "audio_ec_info.h"
31 #include "datashare_helper.h"
32 #include "audio_errors.h"
33 #include "audio_policy_manager_factory.h"
34 #include "audio_stream_collector.h"
35 #include "common_event_manager.h"
36 
37 #ifdef BLUETOOTH_ENABLE
38 #include "audio_server_death_recipient.h"
39 #include "audio_bluetooth_manager.h"
40 #include "bluetooth_device_manager.h"
41 #endif
42 
43 #include "audio_a2dp_device.h"
44 #include "audio_active_device.h"
45 #include "audio_scene_manager.h"
46 #include "audio_connected_device.h"
47 #include "audio_offload_stream.h"
48 #include "audio_policy_state_monitor.h"
49 
50 namespace OHOS {
51 namespace AudioStandard {
52 
53 class ForceControlVolumeTypeMonitor;
54 
55 using InternalDeviceType = DeviceType;
56 
57 struct AdjustVolumeInfo {
58     DeviceType deviceType;
59     AudioStreamType streamType;
60     int32_t volumeLevel;
61     int32_t appUid;
62     std::string invocationTime;
63 };
64 
65 struct VolumeKeyEventRegistration {
66     std::string keyType;  // Volume up or down
67     int32_t subscriptionId;
68     std::string registrationTime;
69     bool registrationResult;
70 };
71 
72 class AudioVolumeManager {
73 public:
GetInstance()74     static AudioVolumeManager& GetInstance()
75     {
76         static AudioVolumeManager instance;
77         return instance;
78     }
79     bool Init(std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler);
80     void DeInit(void);
81     void InitKVStore();
82     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType, DeviceType deviceType = DEVICE_TYPE_NONE) const;
83     int32_t GetMinVolumeLevel(AudioVolumeType volumeType, DeviceType deviceType = DEVICE_TYPE_NONE) const;
84     bool SetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume vol);
85     int32_t InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer);
86     void SetSharedAbsVolumeScene(const bool support);
87     int32_t GetSystemVolumeLevel(AudioStreamType streamType, int32_t zoneId = 0);
88     int32_t GetAppVolumeLevel(int32_t appUid, int32_t &volumeLevel);
89     int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType);
90     int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel, int32_t zoneId = 0);
91     int32_t SetAppVolumeMuted(int32_t appUid, bool muted);
92     int32_t IsAppVolumeMute(int32_t appUid, bool owned, bool &isMute);
93     int32_t SetAppRingMuted(int32_t appUid, bool muted);
94     bool IsAppRingMuted(int32_t appUid);
95     int32_t SetAppVolumeLevel(int32_t appUid, int32_t volumeLevel);
96     int32_t SetAdjustVolumeForZone(int32_t zoneId);
97     int32_t GetVolumeAdjustZoneId();
98     int32_t DisableSafeMediaVolume();
99     int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support);
100     int32_t SetStreamMute(AudioStreamType streamType, bool mute,
101         const StreamUsage &streamUsage = STREAM_USAGE_UNKNOWN,
102         const DeviceType &deviceType = DEVICE_TYPE_NONE,
103         int32_t zoneId = 0);
104     bool GetStreamMute(AudioStreamType streamType, int32_t zoneId = 0) const;
105 
106     int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, bool internalCall = false);
107 
108     int32_t SetNearlinkDeviceVolume(const std::string &macAddress, AudioVolumeType volumeType,
109         const int32_t volume, bool internalCall = false);
110 
111     void UpdateGroupInfo(GroupType type, std::string groupName, int32_t& groupId, std::string networkId,
112         bool connected, int32_t mappingId);
113     void GetVolumeGroupInfo(std::vector<sptr<VolumeGroupInfo>>& volumeGroupInfos);
114     int32_t SetVolumeForSwitchDevice(AudioDeviceDescriptor deviceDescriptor,
115         const std::string &newSinkName = PORT_NONE, bool enableSetVoiceCallVolume = true);
116 
117     bool IsRingerModeMute();
118     void SetRingerModeMute(bool flag);
119     int32_t ResetRingerModeMute();
120     void OnReceiveEvent(const EventFwk::CommonEventData &eventData);
121     int32_t SetVoiceRingtoneMute(bool isMute);
122     void SetVoiceCallVolume(int32_t volume);
123     bool GetVolumeGroupInfosNotWait(std::vector<sptr<VolumeGroupInfo>> &infos);
124     void SetDefaultDeviceLoadFlag(bool isLoad);
125     void NotifyVolumeGroup();
126     bool GetLoadFlag();
127     void UpdateSafeVolumeByS4();
128     void SaveSystemVolumeLevelInfo(AudioStreamType streamType, int32_t volumeLevel, int32_t appUid,
129         std::string invocationTime);
130     void SaveVolumeKeyRegistrationInfo(std::string keyType, std::string registrationTime, int32_t subscriptionId,
131         bool registrationResult);
132     void GetSystemVolumeLevelInfo(std::vector<AdjustVolumeInfo> &systemVolumeLevelInfo);
133     std::vector<std::shared_ptr<AllDeviceVolumeInfo>> GetAllDeviceVolumeInfo();
134     void GetVolumeKeyRegistrationInfo(std::vector<VolumeKeyEventRegistration> &keyRegistrationInfo);
135     int32_t SaveSpecifiedDeviceVolume(AudioStreamType streamType, int32_t volumeLevel, DeviceType deviceType);
136     int32_t ForceVolumeKeyControlType(AudioVolumeType volumeType, int32_t duration);
137     void OnTimerExpired();
138     bool IsNeedForceControlVolumeType();
139     AudioVolumeType GetForceControlVolumeType();
140     int32_t SetSystemVolumeDegree(AudioStreamType streamType, int32_t volumeDegree,
141         int32_t zoneId);
142     int32_t GetSystemVolumeDegree(AudioStreamType streamType);
143     int32_t GetMinVolumeDegree(AudioVolumeType volumeType) const;
144 
145 private:
AudioVolumeManager()146     AudioVolumeManager() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
147         audioA2dpDevice_(AudioA2dpDevice::GetInstance()),
148         audioSceneManager_(AudioSceneManager::GetInstance()),
149         audioActiveDevice_(AudioActiveDevice::GetInstance()),
150         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
151         audioOffloadStream_(AudioOffloadStream::GetInstance()) {}
~AudioVolumeManager()152     ~AudioVolumeManager() {}
153 
154     int32_t HandleAbsBluetoothVolume(const std::string &macAddress, const int32_t volumeLevel,
155         bool isNearlinkDevice = false, AudioStreamType streamType = STREAM_DEFAULT);
156     int32_t DealWithSafeVolume(const int32_t volumeLevel, bool isBtDevice);
157     void CreateCheckMusicActiveThread();
158     bool IsBlueTooth(const DeviceType &deviceType);
159     int32_t CheckActiveMusicTime();
160     void CheckBlueToothActiveMusicTime(int32_t safeVolume);
161     void CheckWiredActiveMusicTime(int32_t safeVolume);
162     void RestoreSafeVolume(AudioStreamType streamType, int32_t safeVolume);
163     void SetSafeVolumeCallback(AudioStreamType streamType);
164     void SetDeviceSafeVolumeStatus();
165     void SetAbsVolumeSceneAsync(const std::string &macAddress, const bool support);
166     int32_t SelectDealSafeVolume(AudioStreamType streamType, int32_t volumeLevel,
167         DeviceType deviceType = DEVICE_TYPE_NONE);
168     void PublishSafeVolumeNotification(int32_t notificationId);
169     void CancelSafeVolumeNotification(int32_t notificationId);
170     bool IsWiredHeadSet(const DeviceType &deviceType);
171     void CheckToCloseNotification(AudioStreamType streamType, int32_t volumeLevel);
172     bool DeviceIsSupportSafeVolume();
173     void SetRestoreVolumeLevel(DeviceType deviceType, int32_t curDeviceVolume);
174     void CheckLowerDeviceVolume(DeviceType deviceType);
175     int32_t CheckRestoreDeviceVolume(DeviceType deviceType);
176     int32_t DealWithEventVolume(const int32_t notificationId);
177     void ChangeDeviceSafeStatus(SafeStatus safeStatus);
178     bool CheckMixActiveMusicTime(int32_t safeVolume);
179 
180     int32_t HandleA2dpAbsVolume(AudioStreamType streamType, int32_t volumeLevel, DeviceType curDeviceType);
181     int32_t HandleNearlinkDeviceAbsVolume(AudioStreamType streamType, int32_t volumeLevel,
182         DeviceType curDeviceType);
183 private:
184     std::shared_ptr<AudioSharedMemory> policyVolumeMap_ = nullptr;
185     volatile Volume *volumeVector_ = nullptr;
186     volatile bool *sharedAbsVolumeScene_ = nullptr;
187 
188     int64_t activeSafeTimeBt_ = 0;
189     int64_t activeSafeTime_ = 0;
190     std::time_t startSafeTimeBt_ = 0;
191     std::time_t startSafeTime_ = 0;
192 
193     std::mutex dialogMutex_;
194     std::atomic<bool> isDialogSelectDestroy_ = false;
195     std::condition_variable dialogSelectCondition_;
196 
197     std::unique_ptr<std::thread> calculateLoopSafeTime_ = nullptr;
198     std::mutex checkMusicActiveThreadMutex_; // lock calculateLoopSafeTime_
199 
200     std::unique_ptr<std::thread> safeVolumeDialogThrd_ = nullptr;
201     std::atomic<bool> isSafeVolumeDialogShowing_ = false;
202     std::mutex safeVolumeMutex_;
203 
204     bool userSelect_ = false;
205     bool safeVolumeExit_ = false;
206     SafeStatus safeStatusBt_ = SAFE_UNKNOWN;
207     SafeStatus safeStatus_ = SAFE_UNKNOWN;
208 
209     bool isBtFirstBoot_ = true;
210 
211     std::vector<sptr<VolumeGroupInfo>> volumeGroups_;
212     std::vector<sptr<InterruptGroupInfo>> interruptGroups_;
213     std::mutex volumeGroupsMutex_;
214 
215     std::mutex ringerModeMuteMutex_;
216     std::atomic<bool> ringerModeMute_ = true;
217     std::condition_variable ringerModeMuteCondition_;
218     bool isVoiceRingtoneMute_ = false;
219 
220     std::mutex notifyMutex_;
221     int32_t btRestoreVol_ = 0;
222     int32_t wiredRestoreVol_ = 0;
223     bool restoreNIsShowing_ = false;
224     bool increaseNIsShowing_ = false;
225 
226     std::mutex defaultDeviceLoadMutex_;
227     std::atomic<bool> isPrimaryMicModuleInfoLoaded_ = false;
228     DeviceType curOutputDeviceType_ = DEVICE_TYPE_NONE;
229 
230     std::shared_ptr<FixedSizeList<AdjustVolumeInfo>> systemVolumeLevelInfo_ =
231         std::make_shared<FixedSizeList<AdjustVolumeInfo>>(MAX_CACHE_AMOUNT);
232     std::shared_ptr<FixedSizeList<VolumeKeyEventRegistration>> volumeKeyRegistrations_ =
233     std::make_shared<FixedSizeList<VolumeKeyEventRegistration>>(MAX_CACHE_AMOUNT);
234 
235     IAudioPolicyInterface& audioPolicyManager_;
236     AudioA2dpDevice& audioA2dpDevice_;
237     AudioSceneManager& audioSceneManager_;
238     AudioActiveDevice& audioActiveDevice_;
239     AudioConnectedDevice& audioConnectedDevice_;
240     AudioOffloadStream& audioOffloadStream_;
241     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_;
242     std::mutex forceControlVolumeTypeMutex_;
243     bool needForceControlVolumeType_ = false;
244     AudioVolumeType forceControlVolumeType_ = STREAM_DEFAULT;
245     std::shared_ptr<ForceControlVolumeTypeMonitor> forceControlVolumeTypeMonitor_;
246 };
247 
248 class ForceControlVolumeTypeMonitor : public AudioPolicyStateMonitorCallback {
249 public:
ForceControlVolumeTypeMonitor()250     ForceControlVolumeTypeMonitor() : audioVolumeManager_(AudioVolumeManager::GetInstance()) {};
251     ~ForceControlVolumeTypeMonitor();
252     void OnTimeOut() override;
253     void SetTimer(int32_t duration, std::shared_ptr<ForceControlVolumeTypeMonitor> cb);
254 
255 private:
256     static constexpr int32_t MAX_DURATION_TIME_S = 10;
257 
258     void StartMonitor(int32_t duration, std::shared_ptr<ForceControlVolumeTypeMonitor> cb);
259     void StopMonitor();
260 
261     AudioVolumeManager &audioVolumeManager_;
262     int32_t duration_ = 0;
263     int32_t cbId_ = INVALID_CB_ID;
264     std::mutex monitorMtx_;
265 };
266 }
267 }
268 
269 #endif