• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 ST_AUDIO_ADAPTER_MANAGER_H
17 #define ST_AUDIO_ADAPTER_MANAGER_H
18 
19 #include <list>
20 #include <unordered_map>
21 #include <cinttypes>
22 
23 #include "audio_adapter_manager_handler.h"
24 #include "audio_service_adapter.h"
25 #include "distributed_kv_data_manager.h"
26 #include "iaudio_policy_interface.h"
27 #include "types.h"
28 #include "audio_policy_log.h"
29 #include "audio_policy_server_handler.h"
30 #include "audio_volume_config.h"
31 #include "volume_data_maintainer.h"
32 #include "audio_utils.h"
33 
34 namespace OHOS {
35 namespace AudioStandard {
36 using namespace OHOS::DistributedKv;
37 
38 class AudioOsAccountInfo;
39 
40 struct AppConfigVolume {
41     int32_t defaultVolume;
42     int32_t maxVolume;
43     int32_t minVolume;
44 };
45 
46 class AudioAdapterManager : public IAudioPolicyInterface {
47 public:
48     static constexpr std::string_view SPLIT_STREAM_SINK = "libmodule-split-stream-sink.z.so";
49     static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so";
50     static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so";
51     static constexpr std::string_view PIPE_SINK = "libmodule-pipe-sink.z.so";
52     static constexpr std::string_view PIPE_SOURCE = "libmodule-pipe-source.z.so";
53     static constexpr std::string_view CLUSTER_SINK = "libmodule-cluster-sink.z.so";
54     static constexpr std::string_view EFFECT_SINK = "libmodule-effect-sink.z.so";
55     static constexpr std::string_view INNER_CAPTURER_SINK = "libmodule-inner-capturer-sink.z.so";
56     static constexpr std::string_view RECEIVER_SINK = "libmodule-receiver-sink.z.so";
57     static constexpr uint32_t KVSTORE_CONNECT_RETRY_COUNT = 5;
58     static constexpr uint32_t KVSTORE_CONNECT_RETRY_DELAY_TIME = 200000;
59     static constexpr float MIN_VOLUME = 0.0f;
60     static constexpr uint32_t NUMBER_TWO = 2;
61     bool Init();
62     void Deinit(void);
63     void InitKVStore();
64     bool ConnectServiceAdapter();
65 
GetInstance()66     static IAudioPolicyInterface& GetInstance()
67     {
68         static AudioAdapterManager audioAdapterManager;
69         return audioAdapterManager;
70     }
71 
~AudioAdapterManager()72     virtual ~AudioAdapterManager() {}
73 
74     int32_t GetMaxVolumeLevel(AudioVolumeType volumeType);
75 
76     int32_t GetMinVolumeLevel(AudioVolumeType volumeType);
77 
78     int32_t SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel);
79 
80     int32_t SetSystemVolumeLevelWithDevice(AudioStreamType streamType, int32_t volumeLevel, DeviceType deviceType);
81 
82     int32_t SetAppVolumeLevel(int32_t appUid, int32_t volumeLevel);
83 
84     int32_t SetAppVolumeMuted(int32_t appUid, bool muted);
85 
86     int32_t SetAppVolumeMutedDB(int32_t appUid, bool muted);
87 
88     bool IsAppVolumeMute(int32_t appUid, bool owned);
89 
90     int32_t GetSystemVolumeLevel(AudioStreamType streamType);
91 
92     int32_t GetAppVolumeLevel(int32_t appUid);
93 
94     int32_t GetSystemVolumeLevelNoMuteState(AudioStreamType streamType);
95 
96     float GetSystemVolumeDb(AudioStreamType streamType);
97 
98     int32_t SetStreamMute(AudioStreamType streamType, bool mute, StreamUsage streamUsage = STREAM_USAGE_UNKNOWN,
99         const DeviceType &deviceType = DEVICE_TYPE_NONE);
100 
101     int32_t SetSourceOutputStreamMute(int32_t uid, bool setMute);
102 
103     bool GetStreamMute(AudioStreamType streamType);
104 
105     bool GetAppMute(int32_t appUid);
106 
107     std::vector<SinkInfo> GetAllSinks();
108 
109     void GetAllSinkInputs(std::vector<SinkInput> &sinkInputs);
110 
111     std::vector<SourceOutput> GetAllSourceOutputs();
112 
113     AudioIOHandle OpenAudioPort(const AudioModuleInfo &audioModuleInfo);
114 
115     int32_t CloseAudioPort(AudioIOHandle ioHandle, bool isSync = false);
116 
117     int32_t SelectDevice(DeviceRole deviceRole, InternalDeviceType deviceType, std::string name);
118 
119     int32_t SetDeviceActive(InternalDeviceType deviceType, std::string name, bool active,
120         DeviceFlag flag = ALL_DEVICES_FLAG);
121 
122     void SetVolumeForSwitchDevice(InternalDeviceType deviceType);
123 
124     int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName);
125 
126     int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName);
127 
128     int32_t SetRingerMode(AudioRingerMode ringerMode);
129 
130     AudioRingerMode GetRingerMode(void) const;
131 
132     int32_t SetAudioStreamRemovedCallback(AudioStreamRemovedCallback *callback);
133 
134     int32_t SuspendAudioDevice(std::string &name, bool isSuspend);
135 
136     bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false);
137 
138     float CalculateVolumeDb(int32_t volumeLevel);
139 
140     int32_t SetSystemSoundUri(const std::string &key, const std::string &uri);
141 
142     std::string GetSystemSoundUri(const std::string &key);
143 
144     float GetMinStreamVolume(void) const;
145 
146     float GetMaxStreamVolume(void) const;
147 
148     bool IsVolumeUnadjustable();
149 
150     float CalculateVolumeDbNonlinear(AudioStreamType streamType, DeviceType deviceType, int32_t volumeLevel);
151 
152     void GetStreamVolumeInfoMap(StreamVolumeInfoMap &streamVolumeInfos);
153 
154     DeviceVolumeType GetDeviceCategory(DeviceType deviceType);
155 
156     void SetActiveDevice(DeviceType deviceType);
157 
158     DeviceType GetActiveDevice();
159 
160     float GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType);
161 
IsUseNonlinearAlgo()162     bool IsUseNonlinearAlgo() { return useNonlinearAlgo_; }
163 
164     void SetAbsVolumeScene(bool isAbsVolumeScene);
165 
166     bool IsAbsVolumeScene() const;
167 
168     void SetAbsVolumeMute(bool mute);
169 
170     void SetDataShareReady(std::atomic<bool> isDataShareReady);
171 
172     bool IsAbsVolumeMute() const;
173 
174     std::string GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const;
175 
176     void ResetRemoteCastDeviceVolume();
177 
178     int32_t GetStreamVolume(AudioStreamType streamType);
179 
180     void NotifyAccountsChanged(const int &id);
181 
182     void SafeVolumeDump(std::string &dumpString);
183 
184     int32_t DoRestoreData();
185     SafeStatus GetCurrentDeviceSafeStatus(DeviceType deviceType);
186 
187     int64_t GetCurentDeviceSafeTime(DeviceType deviceType);
188 
189     int32_t SetDeviceSafeStatus(DeviceType deviceType, SafeStatus status);
190 
191     int32_t SetDeviceSafeTime(DeviceType deviceType, int64_t time);
192 
193     int32_t SetRestoreVolumeLevel(DeviceType deviceType, int32_t volume);
194 
195     int32_t GetRestoreVolumeLevel(DeviceType deviceType);
196 
197     int32_t GetSafeVolumeLevel() const;
198 
199     int32_t GetSafeVolumeTimeout() const;
200 
201     int32_t GetCurActivateCount(void) const;
202 
203     void HandleKvData(bool isFirstBoot);
204 
205     int32_t SetPersistMicMuteState(const bool isMute);
206 
207     int32_t GetPersistMicMuteState(bool &isMute) const;
208 
209     void HandleSaveVolume(DeviceType deviceType, AudioStreamType streamType, int32_t volumeLevel);
210 
211     void HandleStreamMuteStatus(AudioStreamType streamType, bool mute, StreamUsage streamUsage = STREAM_USAGE_UNKNOWN,
212         const DeviceType &deviceType = DEVICE_TYPE_NONE);
213 
214     void HandleRingerMode(AudioRingerMode ringerMode);
215 
216     void SetAudioServerProxy(sptr<IStandardAudioService> gsp);
217 
218     void SetOffloadSessionId(uint32_t sessionId);
219 
220     void ResetOffloadSessionId();
221 
222     int32_t SetDoubleRingVolumeDb(const AudioStreamType &streamType, const int32_t &volumeLevel);
223 
224     void SetDeviceSafeVolume(const AudioStreamType streamType, const int32_t volumeLevel);
225 
226     void SetRestoreVolumeFlag(const bool safeVolumeCall);
227 
228     void UpdateSafeVolumeByS4();
229     void SetVgsVolumeSupported(bool isVgsSupported);
230     bool IsVgsVolumeSupported() const;
231 private:
232     friend class PolicyCallbackImpl;
233 
234     static constexpr int32_t MAX_VOLUME_LEVEL = 15;
235     static constexpr int32_t MIN_VOLUME_LEVEL = 0;
236     static constexpr int32_t DEFAULT_VOLUME_LEVEL = 7;
237     static constexpr int32_t CONST_FACTOR = 100;
238     static constexpr int32_t DEFAULT_SAFE_VOLUME_TIMEOUT = 1140;
239     static constexpr int32_t CONVERT_FROM_MS_TO_SECONDS = 1000;
240     static constexpr float MIN_STREAM_VOLUME = 0.0f;
241     static constexpr float MAX_STREAM_VOLUME = 1.0f;
242 
243     struct UserData {
244         AudioAdapterManager *thiz;
245         AudioStreamType streamType;
246         float volume;
247         bool mute;
248         bool isCorked;
249         uint32_t idx;
250     };
251 
AudioAdapterManager()252     AudioAdapterManager()
253         : ringerMode_(RINGER_MODE_NORMAL),
254           audioPolicyKvStore_(nullptr),
255           audioPolicyServerHandler_(DelayedSingleton<AudioPolicyServerHandler>::GetInstance()),
256           volumeDataMaintainer_(VolumeDataMaintainer::GetVolumeDataMaintainer())
257     {
258         InitVolumeMapIndex();
259     }
260 
261     AudioStreamType GetStreamIDByType(std::string streamType);
262     int32_t ReInitKVStore();
263     bool InitAudioPolicyKvStore(bool& isFirstBoot);
264     void InitVolumeMap(bool isFirstBoot);
265     bool LoadVolumeMap(void);
266     std::string GetVolumeKeyForKvStore(DeviceType deviceType, AudioStreamType streamType);
267     void InitRingerMode(bool isFirstBoot);
268     void InitMuteStatusMap(bool isFirstBoot);
269     bool LoadMuteStatusMap(void);
270     std::string GetMuteKeyForKvStore(DeviceType deviceType, AudioStreamType streamType);
271     void InitSystemSoundUriMap();
272     void InitVolumeMapIndex();
273     void InitBootAnimationVolume();
274     void UpdateVolumeMapIndex();
275     void GetVolumePoints(AudioVolumeType streamType, DeviceVolumeType deviceType,
276         std::vector<VolumePoint> &volumePoints);
277     uint32_t GetPositionInVolumePoints(std::vector<VolumePoint> &volumePoints, int32_t idx);
278     void SaveRingtoneVolumeToLocal(AudioVolumeType volumeType, int32_t volumeLevel);
279     int32_t SetVolumeDb(AudioStreamType streamType);
280     int32_t SetAppVolumeDb(int32_t appUid);
281     void SetAudioVolume(AudioStreamType streamType, float volumeDb);
282     void SetAppAudioVolume(int32_t appUid, float volumeDb);
283     void SetOffloadVolume(AudioStreamType streamType, float volumeDb);
284     bool GetStreamMuteInternal(AudioStreamType streamType);
285     int32_t SetRingerModeInternal(AudioRingerMode ringerMode);
286     int32_t SetStreamMuteInternal(AudioStreamType streamType, bool mute, StreamUsage streamUsage,
287         const DeviceType &deviceType = DEVICE_TYPE_NONE);
288     int32_t GetDefaultVolumeLevel(std::unordered_map<AudioStreamType, int32_t> &volumeLevelMapTemp,
289         AudioVolumeType volumeType, DeviceType deviceType) const;
290     void InitKVStoreInternal(void);
291     void DeleteAudioPolicyKvStore();
292     void TransferMuteStatus(void);
293     void CloneMuteStatusMap(void);
294     void CloneVolumeMap(void);
295     void CloneSystemSoundUrl(void);
296     void InitSafeStatus(bool isFirstBoot);
297     void InitSafeTime(bool isFirstBoot);
298     void ConvertSafeTime(void);
299     void UpdateSafeVolume();
300     void CheckAndDealMuteStatus(const DeviceType &deviceType, const AudioStreamType &streamType);
301     void SetVolumeCallbackAfterClone();
302     void SetFirstBoot(bool isFirst);
303     void MaximizeVoiceAssistantVolume(InternalDeviceType deviceType);
304     template<typename T>
TransferTypeToByteArray(const T & t)305     std::vector<uint8_t> TransferTypeToByteArray(const T &t)
306     {
307         return std::vector<uint8_t>(reinterpret_cast<uint8_t *>(const_cast<T *>(&t)),
308             reinterpret_cast<uint8_t *>(const_cast<T *>(&t)) + sizeof(T));
309     }
310 
311     template<typename T>
TransferByteArrayToType(const std::vector<uint8_t> & data)312     T TransferByteArrayToType(const std::vector<uint8_t> &data)
313     {
314         if (data.size() != sizeof(T) || data.size() == 0) {
315             constexpr int tSize = sizeof(T);
316             uint8_t tContent[tSize] = { 0 };
317             return *reinterpret_cast<T *>(tContent);
318         }
319         return *reinterpret_cast<T *>(const_cast<uint8_t *>(&data[0]));
320     }
321 
322     std::unique_ptr<AudioServiceAdapter> audioServiceAdapter_;
323     std::unordered_map<AudioStreamType, int> minVolumeIndexMap_;
324     std::unordered_map<AudioStreamType, int> maxVolumeIndexMap_;
325     std::mutex systemSoundMutex_;
326     std::unordered_map<std::string, std::string> systemSoundUriMap_;
327     StreamVolumeInfoMap streamVolumeInfos_;
328     DeviceType currentActiveDevice_ = DeviceType::DEVICE_TYPE_SPEAKER;
329     AudioRingerMode ringerMode_;
330     int32_t safeVolume_ = 0;
331     SafeStatus safeStatus_ = SAFE_ACTIVE;
332     SafeStatus safeStatusBt_ = SAFE_ACTIVE;
333     int64_t safeActiveTime_ = 0;
334     int64_t safeActiveBtTime_ = 0;
335     int32_t safeVolumeTimeout_ = DEFAULT_SAFE_VOLUME_TIMEOUT;
336     int32_t safeActiveVolume_ = 0;
337     int32_t safeActiveBtVolume_ = 0;
338     bool isWiredBoot_ = true;
339     bool isBtBoot_ = true;
340     int32_t curActiveCount_ = 0;
341     bool safeVolumeCall_ = false;
342     bool isSafeBoot_ = true;
343     bool isVgsVolumeSupported_ = false;
344     std::shared_ptr<AudioAdapterManagerHandler> handler_ = nullptr;
345 
346     std::shared_ptr<SingleKvStore> audioPolicyKvStore_;
347     std::shared_ptr<AudioPolicyServerHandler> audioPolicyServerHandler_;
348     AudioStreamRemovedCallback *sessionCallback_ = nullptr;
349     VolumeDataMaintainer &volumeDataMaintainer_;
350     bool isVolumeUnadjustable_ = false;
351     bool testModeOn_ {false};
352     std::atomic<float> getSystemVolumeInDb_  {0.0f};
353     bool useNonlinearAlgo_ = false;
354     bool isAbsVolumeScene_ = false;
355     bool isAbsVolumeMute_ = false;
356     bool isNeedCopyVolumeData_ = false;
357     bool isNeedCopyMuteData_ = false;
358     bool isNeedCopyRingerModeData_ = false;
359     bool isNeedCopySystemUrlData_ = false;
360     bool isLoaded_ = false;
361     bool isAllCopyDone_ = false;
362     bool isNeedConvertSafeTime_ = false;
363     sptr<IStandardAudioService> audioServerProxy_ = nullptr;
364     std::optional<uint32_t> offloadSessionID_;
365     std::mutex audioVolumeMutex_;
366     std::mutex activeDeviceMutex_;
367     AppConfigVolume appConfigVolume_;
368 };
369 
370 class PolicyCallbackImpl : public AudioServiceAdapterCallback {
371 public:
PolicyCallbackImpl(AudioAdapterManager * audioAdapterManager)372     explicit PolicyCallbackImpl(AudioAdapterManager *audioAdapterManager)
373     {
374         audioAdapterManager_ = audioAdapterManager;
375     }
376 
~PolicyCallbackImpl()377     ~PolicyCallbackImpl()
378     {
379         AUDIO_WARNING_LOG("Destructor PolicyCallbackImpl");
380     }
381 
OnAudioStreamRemoved(const uint64_t sessionID)382     void OnAudioStreamRemoved(const uint64_t sessionID)
383     {
384         AUDIO_DEBUG_LOG("PolicyCallbackImpl OnAudioStreamRemoved: Session ID %{public}" PRIu64"", sessionID);
385         if (audioAdapterManager_->sessionCallback_ == nullptr) {
386             AUDIO_DEBUG_LOG("PolicyCallbackImpl audioAdapterManager_->sessionCallback_ == nullptr"
387                 "not firing OnAudioStreamRemoved");
388         } else {
389             audioAdapterManager_->sessionCallback_->OnAudioStreamRemoved(sessionID);
390         }
391     }
392 
OnSetVolumeDbCb()393     void OnSetVolumeDbCb()
394     {
395         if (!isFirstBoot_) {
396             return;
397         }
398         isFirstBoot_ = false;
399         static const std::vector<AudioVolumeType> VOLUME_TYPE_LIST = {
400             STREAM_VOICE_CALL,
401             STREAM_RING,
402             STREAM_MUSIC,
403             STREAM_VOICE_ASSISTANT,
404             STREAM_ALARM,
405             STREAM_ACCESSIBILITY,
406             STREAM_ULTRASONIC,
407             STREAM_SYSTEM,
408             STREAM_VOICE_CALL_ASSISTANT,
409             STREAM_ALL
410         };
411         for (auto &volumeType : VOLUME_TYPE_LIST) {
412             audioAdapterManager_->SetVolumeDb(volumeType);
413         }
414     }
415 
416 private:
417     AudioAdapterManager *audioAdapterManager_;
418     bool isFirstBoot_ = true;
419 };
420 } // namespace AudioStandard
421 } // namespace OHOS
422 #endif // ST_PULSEAUDIO_ADAPTER_MANAGER_H
423