• 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 
49 namespace OHOS {
50 namespace AudioStandard {
51 
52 using InternalDeviceType = DeviceType;
53 
54 class AudioVolumeManager {
55 public:
GetInstance()56     static AudioVolumeManager& GetInstance()
57     {
58         static AudioVolumeManager instance;
59         return instance;
60     }
61     bool Init(std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler);
62     void DeInit(void);
63     void InitKVStore();
64     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType) const;
65     int32_t GetMinVolumeLevel(AudioVolumeType volumeType) const;
66     bool GetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume &vol);
67     bool SetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume vol);
68     int32_t InitSharedVolume(std::shared_ptr<AudioSharedMemory> &buffer);
69     void SetSharedAbsVolumeScene(const bool support);
70     int32_t GetSystemVolumeLevel(AudioStreamType streamType);
71     int32_t GetAppVolumeLevel(int32_t appUid);
72     int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType);
73     int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel);
74     int32_t SetSystemVolumeLevelWithDevice(AudioStreamType streamType, int32_t volumeLevel, DeviceType deviceType);
75     int32_t SetAppVolumeMuted(int32_t appUid, bool muted);
76     bool IsAppVolumeMute(int32_t appUid, bool owned);
77     int32_t SetAppVolumeLevel(int32_t appUid, int32_t volumeLevel);
78     int32_t DisableSafeMediaVolume();
79     int32_t SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support);
80     int32_t SetStreamMute(AudioStreamType streamType, bool mute,
81         const StreamUsage &streamUsage = STREAM_USAGE_UNKNOWN,
82         const DeviceType &deviceType = DEVICE_TYPE_NONE);
83     bool GetStreamMute(AudioStreamType streamType) const;
84 
85     int32_t SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume, bool internalCall = false);
86 
87     void UpdateGroupInfo(GroupType type, std::string groupName, int32_t& groupId, std::string networkId,
88         bool connected, int32_t mappingId);
89     void GetVolumeGroupInfo(std::vector<sptr<VolumeGroupInfo>>& volumeGroupInfos);
90     void SetVolumeForSwitchDevice(DeviceType deviceType, const std::string &newSinkName = PORT_NONE);
91 
92     bool IsRingerModeMute();
93     void SetRingerModeMute(bool flag);
94     int32_t ResetRingerModeMute();
95     void OnReceiveEvent(const EventFwk::CommonEventData &eventData);
96     int32_t SetVoiceRingtoneMute(bool isMute);
97     void SetVoiceCallVolume(int32_t volume);
98     bool GetVolumeGroupInfosNotWait(std::vector<sptr<VolumeGroupInfo>> &infos);
99     void SetDefaultDeviceLoadFlag(bool isLoad);
100     void NotifyVolumeGroup();
101     bool GetLoadFlag();
102     void UpdateSafeVolumeByS4();
103 private:
AudioVolumeManager()104     AudioVolumeManager() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
105         audioA2dpDevice_(AudioA2dpDevice::GetInstance()),
106         audioSceneManager_(AudioSceneManager::GetInstance()),
107         audioActiveDevice_(AudioActiveDevice::GetInstance()),
108         audioConnectedDevice_(AudioConnectedDevice::GetInstance()),
109         audioOffloadStream_(AudioOffloadStream::GetInstance()) {}
~AudioVolumeManager()110     ~AudioVolumeManager() {}
111 
112     int32_t HandleAbsBluetoothVolume(const std::string &macAddress, const int32_t volumeLevel);
113     int32_t DealWithSafeVolume(const int32_t volumeLevel, bool isA2dpDevice);
114     void CreateCheckMusicActiveThread();
115     bool IsBlueTooth(const DeviceType &deviceType);
116     int32_t CheckActiveMusicTime();
117     void CheckBlueToothActiveMusicTime(int32_t safeVolume);
118     void CheckWiredActiveMusicTime(int32_t safeVolume);
119     void RestoreSafeVolume(AudioStreamType streamType, int32_t safeVolume);
120     void SetSafeVolumeCallback(AudioStreamType streamType);
121     void SetDeviceSafeVolumeStatus();
122     void SetAbsVolumeSceneAsync(const std::string &macAddress, const bool support);
123     int32_t SelectDealSafeVolume(AudioStreamType streamType, int32_t volumeLevel);
124     void PublishSafeVolumeNotification(int32_t notificationId);
125     void CancelSafeVolumeNotification(int32_t notificationId);
126     void UpdateVolumeForLowLatency();
127     bool IsWiredHeadSet(const DeviceType &deviceType);
128     void CheckToCloseNotification(AudioStreamType streamType, int32_t volumeLevel);
129     bool DeviceIsSupportSafeVolume();
130     int32_t DealWithEventVolume(const int32_t notificationId);
131     void ChangeDeviceSafeStatus(SafeStatus safeStatus);
132     bool CheckMixActiveMusicTime(int32_t safeVolume);
133 private:
134     std::shared_ptr<AudioSharedMemory> policyVolumeMap_ = nullptr;
135     volatile Volume *volumeVector_ = nullptr;
136     volatile bool *sharedAbsVolumeScene_ = nullptr;
137 
138     int64_t activeSafeTimeBt_ = 0;
139     int64_t activeSafeTime_ = 0;
140     std::time_t startSafeTimeBt_ = 0;
141     std::time_t startSafeTime_ = 0;
142 
143     std::mutex dialogMutex_;
144     std::atomic<bool> isDialogSelectDestroy_ = false;
145     std::condition_variable dialogSelectCondition_;
146 
147     std::unique_ptr<std::thread> calculateLoopSafeTime_ = nullptr;
148     std::mutex checkMusicActiveThreadMutex_; // lock calculateLoopSafeTime_
149 
150     std::unique_ptr<std::thread> safeVolumeDialogThrd_ = nullptr;
151     std::atomic<bool> isSafeVolumeDialogShowing_ = false;
152     std::mutex safeVolumeMutex_;
153 
154     bool userSelect_ = false;
155     bool safeVolumeExit_ = false;
156     SafeStatus safeStatusBt_ = SAFE_UNKNOWN;
157     SafeStatus safeStatus_ = SAFE_UNKNOWN;
158 
159     bool isBtFirstBoot_ = true;
160 
161     std::vector<sptr<VolumeGroupInfo>> volumeGroups_;
162     std::vector<sptr<InterruptGroupInfo>> interruptGroups_;
163 
164     std::mutex ringerModeMuteMutex_;
165     std::atomic<bool> ringerModeMute_ = true;
166     std::condition_variable ringerModeMuteCondition_;
167     bool isVoiceRingtoneMute_ = false;
168 
169     std::mutex notifyMutex_;
170     int32_t streamMusicVol_ = 0;
171     bool restoreNIsShowing_ = false;
172     bool increaseNIsShowing_ = false;
173 
174     std::mutex defaultDeviceLoadMutex_;
175     std::atomic<bool> isPrimaryMicModuleInfoLoaded_ = false;
176 
177     IAudioPolicyInterface& audioPolicyManager_;
178     AudioA2dpDevice& audioA2dpDevice_;
179     AudioSceneManager& audioSceneManager_;
180     AudioActiveDevice& audioActiveDevice_;
181     AudioConnectedDevice& audioConnectedDevice_;
182     AudioOffloadStream& audioOffloadStream_;
183     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_;
184 };
185 
186 }
187 }
188 
189 #endif