• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioServer"
17 #endif
18 
19 #include "audio_server.h"
20 
21 #include "audio_effect_chain_manager.h"
22 #include "audio_enhance_chain_manager.h"
23 #include "common/hdi_adapter_info.h"
24 #include "manager/hdi_adapter_manager.h"
25 #include "i_hpae_manager.h"
26 #include "audio_utils.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 
RecognizeAudioEffectType(const std::string & mainkey,const std::string & subkey,const std::string & extraSceneType)32 void AudioServer::RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey,
33     const std::string &extraSceneType)
34 {
35     int32_t engineFlag = GetEngineFlag();
36     if (engineFlag == 1) {
37         HPAE::IHpaeManager::GetHpaeManager().UpdateParamExtra(mainkey, subkey, extraSceneType);
38     } else {
39         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
40         if (audioEffectChainManager == nullptr) {
41             AUDIO_ERR_LOG("audioEffectChainManager is nullptr!");
42             return;
43         }
44         audioEffectChainManager->UpdateParamExtra(mainkey, subkey, extraSceneType);
45 
46         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
47         CHECK_AND_RETURN_LOG(audioEnhanceChainManager != nullptr, "audioEnhanceChainManager is nullptr!");
48         return audioEnhanceChainManager->UpdateExtraSceneType(mainkey, subkey, extraSceneType);
49     }
50 }
51 
52 // LCOV_EXCL_START
CreateEffectChainManager(const std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectParam,const EffectChainManagerParam & enhanceParam)53 int32_t AudioServer::CreateEffectChainManager(const std::vector<EffectChain> &effectChains,
54     const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam)
55 {
56     CHECK_AND_RETURN_RET_LOG(effectChains.size() >= 0 && effectChains.size() <= AUDIO_EFFECT_CHAIN_COUNT_UPPER_LIMIT,
57         AUDIO_ERR, "Create audio effect chains failed, invalid countChains!");
58     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED, "not audio calling!");
59     int32_t engineFlag = GetEngineFlag();
60     if (engineFlag == 1) {
61         HPAE::IHpaeManager::GetHpaeManager().InitAudioEffectChainManager(effectChains, effectParam,
62             audioEffectServer_->GetEffectEntries());
63         HPAE::IHpaeManager::GetHpaeManager().InitAudioEnhanceChainManager(effectChains, enhanceParam,
64             audioEffectServer_->GetEffectEntries());
65         AUDIO_INFO_LOG("AudioEffectChainManager Init");
66     } else {
67         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
68         audioEffectChainManager->InitAudioEffectChainManager(effectChains, effectParam,
69             audioEffectServer_->GetEffectEntries());
70         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
71         audioEnhanceChainManager->InitAudioEnhanceChainManager(effectChains, enhanceParam,
72             audioEffectServer_->GetEffectEntries());
73     }
74     return SUCCESS;
75 }
76 
SetOutputDeviceSink(int32_t deviceType,const std::string & sinkName)77 int32_t AudioServer::SetOutputDeviceSink(int32_t deviceType, const std::string &sinkName)
78 {
79     CHECK_AND_RETURN_RET_LOG(deviceType >= DEVICE_TYPE_NONE && deviceType <= DEVICE_TYPE_MAX, AUDIO_ERR,
80         "Set output device sink failed, please check log");
81     Trace trace("AudioServer::SetOutputDeviceSink:" + std::to_string(deviceType) + " sink:" + sinkName);
82     if (!PermissionUtil::VerifyIsAudio()) {
83         AUDIO_ERR_LOG("not audio calling!");
84         return SUCCESS;
85     }
86 
87     int32_t engineFlag = GetEngineFlag();
88     if (engineFlag == 1) {
89         HPAE::IHpaeManager::GetHpaeManager().SetOutputDeviceSink(deviceType, sinkName);
90     } else {
91         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
92         audioEffectChainManager->SetOutputDeviceSink(deviceType, sinkName);
93     }
94     return SUCCESS;
95 }
96 
UpdateSpatializationState(const AudioSpatializationState & spatializationState)97 int32_t AudioServer::UpdateSpatializationState(const AudioSpatializationState& spatializationState)
98 {
99     int32_t callingUid = IPCSkeleton::GetCallingUid();
100     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
101 
102     int32_t engineFlag = GetEngineFlag();
103     if (engineFlag == 1) {
104         return HPAE::IHpaeManager::GetHpaeManager().UpdateSpatializationState(spatializationState);
105     } else {
106         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
107         if (audioEffectChainManager == nullptr) {
108             AUDIO_ERR_LOG("audioEffectChainManager is nullptr!");
109             return ERROR;
110         }
111         return audioEffectChainManager->UpdateSpatializationState(spatializationState);
112     }
113 }
114 
UpdateSpatialDeviceType(int32_t spatialDeviceType)115 int32_t AudioServer::UpdateSpatialDeviceType(int32_t spatialDeviceType)
116 {
117     int32_t callingUid = IPCSkeleton::GetCallingUid();
118     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
119 
120     int32_t engineFlag = GetEngineFlag();
121     if (engineFlag == 1) {
122         return HPAE::IHpaeManager::GetHpaeManager().UpdateSpatialDeviceType(
123             static_cast<AudioSpatialDeviceType>(spatialDeviceType));
124     } else {
125         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
126         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr!");
127 
128         return audioEffectChainManager->UpdateSpatialDeviceType(
129             static_cast<AudioSpatialDeviceType>(spatialDeviceType));
130     }
131 }
132 // LCOV_EXCL_STOP
133 
SetSystemVolumeToEffect(const AudioStreamType streamType,float volume)134 int32_t AudioServer::SetSystemVolumeToEffect(const AudioStreamType streamType, float volume)
135 {
136     AudioVolumeType systemVolumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
137 
138     int32_t engineFlag = GetEngineFlag();
139     if (engineFlag == 1) {
140         HPAE::IHpaeManager::GetHpaeManager().SetEffectSystemVolume(systemVolumeType, volume);
141     } else {
142         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
143         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
144         AUDIO_INFO_LOG("streamType: %{public}d, systemVolume: %{public}f", streamType, volume);
145         audioEffectChainManager->SetEffectSystemVolume(systemVolumeType, volume);
146 
147         audioEffectChainManager->EffectVolumeUpdate();
148     }
149     return SUCCESS;
150 }
151 
152 // LCOV_EXCL_START
SetSpatializationSceneType(int32_t spatializationSceneType)153 int32_t AudioServer::SetSpatializationSceneType(int32_t spatializationSceneType)
154 {
155     int32_t callingUid = IPCSkeleton::GetCallingUid();
156     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
157 
158     int32_t engineFlag = GetEngineFlag();
159     if (engineFlag == 1) {
160         return HPAE::IHpaeManager::GetHpaeManager().SetSpatializationSceneType(
161             static_cast<AudioSpatializationSceneType>(spatializationSceneType));
162     } else {
163         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
164         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
165         return audioEffectChainManager->SetSpatializationSceneType(
166             static_cast<AudioSpatializationSceneType>(spatializationSceneType));
167     }
168 }
169 // LCOV_EXCL_STOP
170 
GetEffectLatency(const std::string & sessionId,uint32_t & latency)171 int32_t AudioServer::GetEffectLatency(const std::string &sessionId, uint32_t& latency)
172 {
173     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
174     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
175     latency = audioEffectChainManager->GetLatency(sessionId);
176     return SUCCESS;
177 }
178 
179 // LCOV_EXCL_START
GetEffectOffloadEnabled(bool & isEffectOffloadEnabled)180 int32_t AudioServer::GetEffectOffloadEnabled(bool& isEffectOffloadEnabled)
181 {
182     int32_t callingUid = IPCSkeleton::GetCallingUid();
183     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
184 
185     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
186     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
187     isEffectOffloadEnabled = audioEffectChainManager->GetOffloadEnabled();
188     return SUCCESS;
189 }
190 
LoadHdiEffectModel()191 int32_t AudioServer::LoadHdiEffectModel()
192 {
193     int32_t callingUid = IPCSkeleton::GetCallingUid();
194     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
195         "load hdi effect model refused for %{public}d", callingUid);
196 
197     int32_t engineFlag = GetEngineFlag();
198     if (engineFlag == 1) {
199         HPAE::IHpaeManager::GetHpaeManager().InitHdiState();
200     } else {
201         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
202         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
203         audioEffectChainManager->InitHdiState();
204     }
205     return SUCCESS;
206 }
207 
SetAudioEffectProperty(const AudioEffectPropertyArrayV3 & propertyArray,int32_t deviceType)208 int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray,
209     int32_t deviceType)
210 {
211     size_t size = propertyArray.property.size();
212     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
213         ERROR_INVALID_PARAM, "audio enhance property array size invalid");
214     int32_t callingUid = IPCSkeleton::GetCallingUid();
215     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
216                              "SetA udio Effect Property refused for %{public}d", callingUid);
217     AudioEffectPropertyArrayV3 effectPropertyArray = {};
218     AudioEffectPropertyArrayV3 enhancePropertyArray = {};
219     for (auto &item : propertyArray.property) {
220         if (item.flag == CAPTURE_EFFECT_FLAG) {
221             enhancePropertyArray.property.push_back(item);
222         } else {
223             effectPropertyArray.property.push_back(item);
224         }
225     }
226     if (enhancePropertyArray.property.size() > 0) {
227         CHECK_AND_RETURN_RET_LOG(SetAudioEnhanceChainProperty(
228             enhancePropertyArray, static_cast<DeviceType>(deviceType)) == AUDIO_OK,
229             ERR_OPERATION_FAILED, "set audio enhancce property failed");
230     }
231     if (effectPropertyArray.property.size() > 0) {
232         CHECK_AND_RETURN_RET_LOG(SetAudioEffectChainProperty(effectPropertyArray) == AUDIO_OK,
233             ERR_OPERATION_FAILED, "set audio effect property failed");
234     }
235     return AUDIO_OK;
236 }
237 
GetAudioEffectProperty(AudioEffectPropertyArrayV3 & propertyArray,int32_t deviceType)238 int32_t AudioServer::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray, int32_t deviceType)
239 {
240     int32_t callingUid = IPCSkeleton::GetCallingUid();
241     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
242         "get audio effect property refused for %{public}d", callingUid);
243     AudioEffectPropertyArrayV3 effectPropertyArray = {};
244     (void)GetAudioEffectPropertyArray(effectPropertyArray);
245     propertyArray.property.insert(propertyArray.property.end(),
246         effectPropertyArray.property.begin(), effectPropertyArray.property.end());
247 
248     AudioEffectPropertyArrayV3 enhancePropertyArray = {};
249     (void)GetAudioEnhancePropertyArray(enhancePropertyArray, static_cast<DeviceType>(deviceType));
250     propertyArray.property.insert(propertyArray.property.end(),
251         enhancePropertyArray.property.begin(), enhancePropertyArray.property.end());
252 
253     size_t size = propertyArray.property.size();
254     CHECK_AND_RETURN_RET_LOG(size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
255         ERROR_INVALID_PARAM, "audio enhance property array size invalid");
256     return AUDIO_OK;
257 }
258 
SetAudioEffectProperty(const AudioEffectPropertyArray & propertyArray)259 int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray)
260 {
261     size_t size = propertyArray.property.size();
262     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
263         ERROR_INVALID_PARAM, "audio enhance property array size invalid");
264     int32_t callingUid = IPCSkeleton::GetCallingUid();
265     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
266         "SetA udio Effect Property refused for %{public}d", callingUid);
267 
268     int32_t engineFlag = GetEngineFlag();
269     if (engineFlag == 1) {
270         return HPAE::IHpaeManager::GetHpaeManager().SetAudioEffectProperty(propertyArray);
271     } else {
272         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
273         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
274         return audioEffectChainManager->SetAudioEffectProperty(propertyArray);
275     }
276 }
277 
GetAudioEffectProperty(AudioEffectPropertyArray & propertyArray)278 int32_t AudioServer::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray)
279 {
280     int32_t callingUid = IPCSkeleton::GetCallingUid();
281     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
282         "Get Audio Effect Property refused for %{public}d", callingUid);
283     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
284     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
285     int32_t ret = audioEffectChainManager->GetAudioEffectProperty(propertyArray);
286     size_t size = propertyArray.property.size();
287     CHECK_AND_RETURN_RET_LOG(size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
288         ERROR_INVALID_PARAM, "audio enhance property array size invalid");
289     return ret;
290 }
291 
SetAudioEnhanceProperty(const AudioEnhancePropertyArray & propertyArray,int32_t deviceType)292 int32_t AudioServer::SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray,
293     int32_t deviceType)
294 {
295     size_t size = propertyArray.property.size();
296     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
297         ERROR_INVALID_PARAM, "Audio enhance property array size invalid");
298     int32_t callingUid = IPCSkeleton::GetCallingUid();
299     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
300         "Set Audio Enhance Property refused for %{public}d", callingUid);
301     int32_t engineFlag = GetEngineFlag();
302     if (engineFlag == 1) {
303         return HPAE::IHpaeManager::GetHpaeManager().SetAudioEnhanceProperty(
304             propertyArray, static_cast<DeviceType>(deviceType));
305     } else {
306         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
307         CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
308         return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, static_cast<DeviceType>(deviceType));
309     }
310 }
311 
GetAudioEnhanceProperty(AudioEnhancePropertyArray & propertyArray,int32_t deviceType)312 int32_t AudioServer::GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray,
313     int32_t deviceType)
314 {
315     int32_t callingUid = IPCSkeleton::GetCallingUid();
316     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
317         "Get Audio Enhance Property refused for %{public}d", callingUid);
318     AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
319     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
320     int32_t ret = audioEnhanceChainManager->GetAudioEnhanceProperty(propertyArray, static_cast<DeviceType>(deviceType));
321     size_t size = propertyArray.property.size();
322     CHECK_AND_RETURN_RET_LOG(size <= AUDIO_EFFECT_COUNT_UPPER_LIMIT,
323         ERROR_INVALID_PARAM, "Audio enhance property array size invalid");
324     return ret;
325 }
326 // LCOV_EXCL_STOP
327 
SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 & propertyArray)328 int32_t AudioServer::SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 &propertyArray)
329 {
330     int32_t engineFlag = GetEngineFlag();
331     if (engineFlag == 1) {
332         return HPAE::IHpaeManager::GetHpaeManager().SetAudioEffectProperty(propertyArray);
333         return SUCCESS;
334     } else {
335         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
336         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
337         return audioEffectChainManager->SetAudioEffectProperty(propertyArray);
338     }
339 }
340 
SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)341 int32_t AudioServer::SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 &propertyArray,
342     const DeviceType& deviceType)
343 {
344     int32_t engineFlag = GetEngineFlag();
345     if (engineFlag == 1) {
346         return HPAE::IHpaeManager::GetHpaeManager().SetAudioEnhanceProperty(propertyArray, deviceType);
347     } else {
348         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
349         CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
350         return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType);
351     }
352 }
353 
GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 & propertyArray)354 int32_t AudioServer::GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 &propertyArray)
355 {
356     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
357     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
358     return audioEffectChainManager->GetAudioEffectProperty(propertyArray);
359 }
360 
GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)361 int32_t AudioServer::GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 &propertyArray,
362     const DeviceType& deviceType)
363 {
364     AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
365     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
366     return audioEnhanceChainManager->GetAudioEnhanceProperty(propertyArray, deviceType);
367 }
368 
369 // LCOV_EXCL_START
UpdateEffectBtOffloadSupported(bool isSupported)370 int32_t AudioServer::UpdateEffectBtOffloadSupported(bool isSupported)
371 {
372     int32_t callingUid = IPCSkeleton::GetCallingUid();
373     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
374         "refused for %{public}d", callingUid);
375 
376     int32_t engineFlag = GetEngineFlag();
377     if (engineFlag == 1) {
378         HPAE::IHpaeManager::GetHpaeManager().UpdateEffectBtOffloadSupported(isSupported);
379     } else {
380         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
381         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
382         audioEffectChainManager->UpdateEffectBtOffloadSupported(isSupported);
383     }
384     return SUCCESS;
385 }
386 
SetRotationToEffect(const uint32_t rotate)387 int32_t AudioServer::SetRotationToEffect(const uint32_t rotate)
388 {
389     int32_t callingUid = IPCSkeleton::GetCallingUid();
390     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
391         "set rotation to effect refused for %{public}d", callingUid);
392 
393     int32_t engineFlag = GetEngineFlag();
394     if (engineFlag == 1) {
395         HPAE::IHpaeManager::GetHpaeManager().EffectRotationUpdate(rotate);
396     } else {
397         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
398         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
399         audioEffectChainManager->EffectRotationUpdate(rotate);
400     }
401 
402     std::string value = "rotation=" + std::to_string(rotate);
403     HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
404     std::shared_ptr<IDeviceManager> deviceManager = manager.GetDeviceManager(HDI_DEVICE_MANAGER_TYPE_LOCAL);
405     CHECK_AND_RETURN_RET_LOG(deviceManager != nullptr, ERROR, "local device manager is nullptr");
406     deviceManager->SetAudioParameter("primary", AudioParamKey::NONE, "", value);
407     return SUCCESS;
408 }
409 // LCOV_EXCL_STOP
410 
SetVolumeInfoForEnhanceChain(const AudioStreamType & streamType)411 int32_t AudioServer::SetVolumeInfoForEnhanceChain(const AudioStreamType &streamType)
412 {
413     AudioVolumeType volumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
414     DeviceType deviceType = PolicyHandler::GetInstance().GetActiveOutPutDevice();
415     Volume vol = {false, 0.0f, 0};
416     PolicyHandler::GetInstance().GetSharedVolume(volumeType, deviceType, vol);
417     float systemVol = vol.isMute ? 0.0f : vol.volumeFloat;
418     if (PolicyHandler::GetInstance().IsAbsVolumeSupported() &&
419         PolicyHandler::GetInstance().GetActiveOutPutDevice() == DEVICE_TYPE_BLUETOOTH_A2DP) {
420         systemVol = 1.0f; // 1.0f for a2dp abs volume
421     }
422 
423     int32_t engineFlag = GetEngineFlag();
424     if (engineFlag == 1) {
425         return HPAE::IHpaeManager::GetHpaeManager().SetVolumeInfo(volumeType, systemVol);
426     } else {
427         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
428         CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
429         return audioEnhanceChainManager->SetVolumeInfo(volumeType, systemVol);
430     }
431 }
432 
SetMicrophoneMuteForEnhanceChain(const bool & isMute)433 int32_t AudioServer::SetMicrophoneMuteForEnhanceChain(const bool &isMute)
434 {
435     int32_t engineFlag = GetEngineFlag();
436     if (engineFlag == 1) {
437         return HPAE::IHpaeManager::GetHpaeManager().SetMicrophoneMuteInfo(isMute);
438     } else {
439         AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
440         CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
441         return audioEnhanceChainManager->SetMicrophoneMuteInfo(isMute);
442     }
443 }
444 
445 // LCOV_EXCL_START
LoadAudioEffectLibraries(const std::vector<Library> & libraries,const std::vector<Effect> & effects,std::vector<Effect> & successEffectList,bool & hasEffectsLoaded)446 int32_t AudioServer::LoadAudioEffectLibraries(const std::vector<Library> &libraries,
447     const std::vector<Effect> &effects, std::vector<Effect> &successEffectList, bool &hasEffectsLoaded)
448 {
449     CHECK_AND_RETURN_RET_LOG((libraries.size() >= 0) && (libraries.size() <= AUDIO_EFFECT_COUNT_UPPER_LIMIT) &&
450         (effects.size() >= 0) && (effects.size() <= AUDIO_EFFECT_COUNT_UPPER_LIMIT), AUDIO_ERR,
451         "LOAD_AUDIO_EFFECT_LIBRARIES read data failed");
452     CHECK_AND_RETURN_RET(libraries.size() > 0, SUCCESS);
453     int32_t callingUid = IPCSkeleton::GetCallingUid();
454     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
455         "LoadAudioEffectLibraries refused for %{public}d", callingUid);
456     CHECK_AND_RETURN_RET_LOG(audioEffectServer_ != nullptr, ERROR, "audioEffectServer_ is nullptr");
457     hasEffectsLoaded = audioEffectServer_->LoadAudioEffects(libraries, effects, successEffectList);
458     if (!hasEffectsLoaded) {
459         AUDIO_WARNING_LOG("Load audio effect failed, please check log");
460         successEffectList.clear();
461         return ERR_INVALID_OPERATION;
462     }
463     return SUCCESS;
464 }
465 
NotifyAccountsChanged()466 int32_t AudioServer::NotifyAccountsChanged()
467 {
468     int32_t callingUid = IPCSkeleton::GetCallingUid();
469     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
470         "refused for %{public}d", callingUid);
471 
472     int32_t engineFlag = GetEngineFlag();
473     if (engineFlag == 1) {
474         HPAE::IHpaeManager::GetHpaeManager().NotifyAccountsChanged();
475     } else {
476         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
477         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
478         audioEffectChainManager->LoadEffectProperties();
479     }
480     return SUCCESS;
481 }
482 
NotifySettingsDataReady()483 int32_t AudioServer::NotifySettingsDataReady()
484 {
485     int32_t callingUid = IPCSkeleton::GetCallingUid();
486     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
487         "refused for %{public}d", callingUid);
488     int32_t engineFlag = GetEngineFlag();
489     if (engineFlag == 1) {
490         HPAE::IHpaeManager::GetHpaeManager().NotifySettingsDataReady();
491     } else {
492         AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
493         CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
494         audioEffectChainManager->LoadEffectProperties();
495     }
496     return SUCCESS;
497 }
498 
IsAcousticEchoCancelerSupported(int32_t sourceType,bool & isSupported)499 int32_t AudioServer::IsAcousticEchoCancelerSupported(int32_t sourceType,  bool& isSupported)
500 {
501     int32_t callingUid = IPCSkeleton::GetCallingUid();
502     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
503         "IsAcousticEchoCancelerSupported refused for %{public}d", callingUid);
504     int32_t engineFlag = GetEngineFlag();
505     if (engineFlag == 1) {
506         isSupported = HPAE::IHpaeManager::GetHpaeManager().IsAcousticEchoCancelerSupported(
507             static_cast<SourceType>(sourceType));
508         return SUCCESS;
509     }
510     AUDIO_INFO_LOG("IsAcousticEchoCancelerSupported not support");
511     return SUCCESS;
512 }
513 
SetKaraokeParameters(const std::string & parameters,bool & ret)514 int32_t AudioServer::SetKaraokeParameters(const std::string &parameters, bool &ret)
515 {
516     int32_t callingUid = IPCSkeleton::GetCallingUid();
517     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
518         "SetKaraokeParameters refused for %{public}d", callingUid);
519     HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
520     std::shared_ptr<IDeviceManager> deviceManager = manager.GetDeviceManager(HDI_DEVICE_MANAGER_TYPE_LOCAL);
521     CHECK_AND_RETURN_RET_LOG(deviceManager != nullptr, ERROR, "local device manager is nullptr");
522     deviceManager->SetAudioParameter("primary", AudioParamKey::NONE, "", parameters);
523     ret = true;
524     return SUCCESS;
525 }
526 
IsAudioLoopbackSupported(int32_t mode,bool & isSupported)527 int32_t AudioServer::IsAudioLoopbackSupported(int32_t mode, bool &isSupported)
528 {
529     isSupported = false;
530     int32_t callingUid = IPCSkeleton::GetCallingUid();
531     CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
532         "IsAudioLoopbackSupported refused for %{public}d", callingUid);
533 #ifdef SUPPORT_LOW_LATENCY
534     if (mode == AudioLoopbackMode::LOOPBACK_HARDWARE) {
535         HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
536         std::shared_ptr<IDeviceManager> deviceManager = manager.GetDeviceManager(HDI_DEVICE_MANAGER_TYPE_LOCAL);
537         CHECK_AND_RETURN_RET_LOG(deviceManager != nullptr, ERROR, "local device manager is nullptr");
538         std::string ret = deviceManager->GetAudioParameter("primary", AudioParamKey::PARAM_KEY_STATE,
539             "is_audioloop_support");
540         AUDIO_INFO_LOG("IsAudioLoopbackSupported ret: %{public}s", ret.c_str());
541         isSupported = ret == "true";
542         return SUCCESS;
543     }
544 #endif
545     AUDIO_ERR_LOG("IsAudioLoopbackSupported not support");
546     return SUCCESS;
547 }
548 // LCOV_EXCL_STOP
549 } // namespace AudioStandard
550 } // namespace OHOS
551