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