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
26 namespace OHOS {
27 namespace AudioStandard {
28 using namespace std;
29
RecognizeAudioEffectType(const std::string & mainkey,const std::string & subkey,const std::string & extraSceneType)30 void AudioServer::RecognizeAudioEffectType(const std::string &mainkey, const std::string &subkey,
31 const std::string &extraSceneType)
32 {
33 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
34 if (audioEffectChainManager == nullptr) {
35 AUDIO_ERR_LOG("audioEffectChainManager is nullptr");
36 return;
37 }
38 audioEffectChainManager->UpdateParamExtra(mainkey, subkey, extraSceneType);
39
40 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
41 if (audioEnhanceChainManager == nullptr) {
42 AUDIO_ERR_LOG("audioEnhanceChainManager is nullptr");
43 return;
44 }
45 audioEnhanceChainManager->UpdateExtraSceneType(mainkey, subkey, extraSceneType);
46 }
47
CreateEffectChainManager(std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectParam,const EffectChainManagerParam & enhanceParam)48 bool AudioServer::CreateEffectChainManager(std::vector<EffectChain> &effectChains,
49 const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam)
50 {
51 if (!PermissionUtil::VerifyIsAudio()) {
52 AUDIO_ERR_LOG("not audio calling!");
53 return false;
54 }
55 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
56 audioEffectChainManager->InitAudioEffectChainManager(effectChains, effectParam,
57 audioEffectServer_->GetEffectEntries());
58 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
59 audioEnhanceChainManager->InitAudioEnhanceChainManager(effectChains, enhanceParam,
60 audioEffectServer_->GetEffectEntries());
61 return true;
62 }
63
SetOutputDeviceSink(int32_t deviceType,std::string & sinkName)64 void AudioServer::SetOutputDeviceSink(int32_t deviceType, std::string &sinkName)
65 {
66 Trace trace("AudioServer::SetOutputDeviceSink:" + std::to_string(deviceType) + " sink:" + sinkName);
67 if (!PermissionUtil::VerifyIsAudio()) {
68 AUDIO_ERR_LOG("not audio calling!");
69 return;
70 }
71 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
72 audioEffectChainManager->SetOutputDeviceSink(deviceType, sinkName);
73 return;
74 }
75
UpdateSpatializationState(AudioSpatializationState spatializationState)76 int32_t AudioServer::UpdateSpatializationState(AudioSpatializationState spatializationState)
77 {
78 int32_t callingUid = IPCSkeleton::GetCallingUid();
79 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
80 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
81 if (audioEffectChainManager == nullptr) {
82 AUDIO_ERR_LOG("audioEffectChainManager is nullptr");
83 return ERROR;
84 }
85 return audioEffectChainManager->UpdateSpatializationState(spatializationState);
86 }
87
UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)88 int32_t AudioServer::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)
89 {
90 int32_t callingUid = IPCSkeleton::GetCallingUid();
91 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
92
93 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
94 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
95
96 return audioEffectChainManager->UpdateSpatialDeviceType(spatialDeviceType);
97 }
98
SetSystemVolumeToEffect(const AudioStreamType streamType,float volume)99 int32_t AudioServer::SetSystemVolumeToEffect(const AudioStreamType streamType, float volume)
100 {
101 AudioVolumeType systemVolumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
102
103 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
104 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
105 AUDIO_INFO_LOG("streamType: %{public}d, systemVolume: %{public}f", streamType, volume);
106 audioEffectChainManager->SetEffectSystemVolume(systemVolumeType, volume);
107
108 std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
109 CHECK_AND_RETURN_RET_LOG(audioEffectVolume != nullptr, ERROR, "null audioEffectVolume");
110 audioEffectChainManager->EffectVolumeUpdate(audioEffectVolume);
111
112 return SUCCESS;
113 }
114
SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)115 int32_t AudioServer::SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)
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 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
121 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
122 return audioEffectChainManager->SetSpatializationSceneType(spatializationSceneType);
123 }
124
GetEffectLatency(const std::string & sessionId)125 uint32_t AudioServer::GetEffectLatency(const std::string &sessionId)
126 {
127 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
128 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
129 return audioEffectChainManager->GetLatency(sessionId);
130 }
131
GetEffectOffloadEnabled()132 bool AudioServer::GetEffectOffloadEnabled()
133 {
134 int32_t callingUid = IPCSkeleton::GetCallingUid();
135 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_NOT_SUPPORTED, "refused for %{public}d", callingUid);
136
137 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
138 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
139 return audioEffectChainManager->GetOffloadEnabled();
140 }
141
LoadHdiEffectModel()142 void AudioServer::LoadHdiEffectModel()
143 {
144 int32_t callingUid = IPCSkeleton::GetCallingUid();
145 CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "load hdi effect model refused for %{public}d", callingUid);
146
147 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
148 CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr");
149 audioEffectChainManager->InitHdiState();
150 }
151
SetAudioEffectProperty(const AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)152 int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray,
153 const DeviceType &deviceType)
154 {
155 int32_t callingUid = IPCSkeleton::GetCallingUid();
156 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
157 "SetA udio Effect Property refused for %{public}d", callingUid);
158 AudioEffectPropertyArrayV3 effectPropertyArray = {};
159 AudioEffectPropertyArrayV3 enhancePropertyArray = {};
160 for (auto &item : propertyArray.property) {
161 if (item.flag == CAPTURE_EFFECT_FLAG) {
162 enhancePropertyArray.property.push_back(item);
163 } else {
164 effectPropertyArray.property.push_back(item);
165 }
166 }
167 if (enhancePropertyArray.property.size() > 0) {
168 CHECK_AND_RETURN_RET_LOG(SetAudioEnhanceChainProperty(enhancePropertyArray, deviceType) == AUDIO_OK,
169 ERR_OPERATION_FAILED, "set audio enhancce property failed");
170 }
171 if (effectPropertyArray.property.size() > 0) {
172 CHECK_AND_RETURN_RET_LOG(SetAudioEffectChainProperty(effectPropertyArray) == AUDIO_OK,
173 ERR_OPERATION_FAILED, "set audio effect property failed");
174 }
175 return AUDIO_OK;
176 }
177
GetAudioEffectProperty(AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)178 int32_t AudioServer::GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray, const DeviceType& deviceType)
179 {
180 int32_t callingUid = IPCSkeleton::GetCallingUid();
181 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
182 "get audio effect property refused for %{public}d", callingUid);
183 AudioEffectPropertyArrayV3 effectPropertyArray = {};
184 (void)GetAudioEffectPropertyArray(effectPropertyArray);
185 propertyArray.property.insert(propertyArray.property.end(),
186 effectPropertyArray.property.begin(), effectPropertyArray.property.end());
187
188 AudioEffectPropertyArrayV3 enhancePropertyArray = {};
189 (void)GetAudioEnhancePropertyArray(enhancePropertyArray, deviceType);
190 propertyArray.property.insert(propertyArray.property.end(),
191 enhancePropertyArray.property.begin(), enhancePropertyArray.property.end());
192 return AUDIO_OK;
193 }
194
SetAudioEffectProperty(const AudioEffectPropertyArray & propertyArray)195 int32_t AudioServer::SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray)
196 {
197 int32_t callingUid = IPCSkeleton::GetCallingUid();
198 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
199 "SetA udio Effect Property refused for %{public}d", callingUid);
200 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
201 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
202 return audioEffectChainManager->SetAudioEffectProperty(propertyArray);
203 }
204
GetAudioEffectProperty(AudioEffectPropertyArray & propertyArray)205 int32_t AudioServer::GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray)
206 {
207 int32_t callingUid = IPCSkeleton::GetCallingUid();
208 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
209 "Get Audio Effect Property refused for %{public}d", callingUid);
210 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
211 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
212 return audioEffectChainManager->GetAudioEffectProperty(propertyArray);
213 }
214
SetAudioEnhanceProperty(const AudioEnhancePropertyArray & propertyArray,DeviceType deviceType)215 int32_t AudioServer::SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray,
216 DeviceType deviceType)
217 {
218 int32_t callingUid = IPCSkeleton::GetCallingUid();
219 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
220 "Set Audio Enhance Property refused for %{public}d", callingUid);
221 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
222 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
223 return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType);
224 }
225
GetAudioEnhanceProperty(AudioEnhancePropertyArray & propertyArray,DeviceType deviceType)226 int32_t AudioServer::GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray,
227 DeviceType deviceType)
228 {
229 int32_t callingUid = IPCSkeleton::GetCallingUid();
230 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), ERR_PERMISSION_DENIED,
231 "Get Audio Enhance Property refused for %{public}d", callingUid);
232 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
233 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
234 return audioEnhanceChainManager->GetAudioEnhanceProperty(propertyArray, deviceType);
235 }
236
SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 & propertyArray)237 int32_t AudioServer::SetAudioEffectChainProperty(const AudioEffectPropertyArrayV3 &propertyArray)
238 {
239 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
240 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
241 return audioEffectChainManager->SetAudioEffectProperty(propertyArray);
242 }
243
SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)244 int32_t AudioServer::SetAudioEnhanceChainProperty(const AudioEffectPropertyArrayV3 &propertyArray,
245 const DeviceType& deviceType)
246 {
247 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
248 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
249 return audioEnhanceChainManager->SetAudioEnhanceProperty(propertyArray, deviceType);
250 }
251
GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 & propertyArray)252 int32_t AudioServer::GetAudioEffectPropertyArray(AudioEffectPropertyArrayV3 &propertyArray)
253 {
254 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
255 CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERROR, "audioEffectChainManager is nullptr");
256 return audioEffectChainManager->GetAudioEffectProperty(propertyArray);
257 }
258
GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 & propertyArray,const DeviceType & deviceType)259 int32_t AudioServer::GetAudioEnhancePropertyArray(AudioEffectPropertyArrayV3 &propertyArray,
260 const DeviceType& deviceType)
261 {
262 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
263 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
264 return audioEnhanceChainManager->GetAudioEnhanceProperty(propertyArray, deviceType);
265 }
266
UpdateEffectBtOffloadSupported(const bool & isSupported)267 void AudioServer::UpdateEffectBtOffloadSupported(const bool &isSupported)
268 {
269 int32_t callingUid = IPCSkeleton::GetCallingUid();
270 CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "refused for %{public}d", callingUid);
271
272 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
273 CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr");
274 audioEffectChainManager->UpdateEffectBtOffloadSupported(isSupported);
275 }
276
SetRotationToEffect(const uint32_t rotate)277 void AudioServer::SetRotationToEffect(const uint32_t rotate)
278 {
279 int32_t callingUid = IPCSkeleton::GetCallingUid();
280 CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "set rotation to effect refused for %{public}d", callingUid);
281
282 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
283 CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr");
284 audioEffectChainManager->EffectRotationUpdate(rotate);
285
286 std::string value = "rotation=" + std::to_string(rotate);
287 HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
288 std::shared_ptr<IDeviceManager> deviceManager = manager.GetDeviceManager(HDI_DEVICE_MANAGER_TYPE_LOCAL);
289 CHECK_AND_RETURN_LOG(deviceManager != nullptr, "local device manager is nullptr");
290 deviceManager->SetAudioParameter("primary", AudioParamKey::NONE, "", value);
291 }
292
SetVolumeInfoForEnhanceChain(const AudioStreamType & streamType)293 int32_t AudioServer::SetVolumeInfoForEnhanceChain(const AudioStreamType &streamType)
294 {
295 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
296 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
297 AudioVolumeType volumeType = VolumeUtils::GetVolumeTypeFromStreamType(streamType);
298 DeviceType deviceType = PolicyHandler::GetInstance().GetActiveOutPutDevice();
299 Volume vol = {false, 0.0f, 0};
300 PolicyHandler::GetInstance().GetSharedVolume(volumeType, deviceType, vol);
301 float systemVol = vol.isMute ? 0.0f : vol.volumeFloat;
302 if (PolicyHandler::GetInstance().IsAbsVolumeSupported() &&
303 PolicyHandler::GetInstance().GetActiveOutPutDevice() == DEVICE_TYPE_BLUETOOTH_A2DP) {
304 systemVol = 1.0f; // 1.0f for a2dp abs volume
305 }
306 return audioEnhanceChainManager->SetVolumeInfo(volumeType, systemVol);
307 }
308
SetMicrophoneMuteForEnhanceChain(const bool & isMute)309 int32_t AudioServer::SetMicrophoneMuteForEnhanceChain(const bool &isMute)
310 {
311 AudioEnhanceChainManager *audioEnhanceChainManager = AudioEnhanceChainManager::GetInstance();
312 CHECK_AND_RETURN_RET_LOG(audioEnhanceChainManager != nullptr, ERROR, "audioEnhanceChainManager is nullptr");
313 return audioEnhanceChainManager->SetMicrophoneMuteInfo(isMute);
314 }
315
LoadAudioEffectLibraries(const std::vector<Library> libraries,const std::vector<Effect> effects,std::vector<Effect> & successEffectList)316 bool AudioServer::LoadAudioEffectLibraries(const std::vector<Library> libraries, const std::vector<Effect> effects,
317 std::vector<Effect>& successEffectList)
318 {
319 int32_t callingUid = IPCSkeleton::GetCallingUid();
320 CHECK_AND_RETURN_RET_LOG(PermissionUtil::VerifyIsAudio(), false, "LoadAudioEffectLibraries refused for %{public}d",
321 callingUid);
322 bool loadSuccess = audioEffectServer_->LoadAudioEffects(libraries, effects, successEffectList);
323 if (!loadSuccess) {
324 AUDIO_WARNING_LOG("Load audio effect failed, please check log");
325 }
326 return loadSuccess;
327 }
328
NotifyAccountsChanged()329 void AudioServer::NotifyAccountsChanged()
330 {
331 int32_t callingUid = IPCSkeleton::GetCallingUid();
332 CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "refused for %{public}d", callingUid);
333
334 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
335 CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr");
336 audioEffectChainManager->LoadEffectProperties();
337 }
338
NotifySettingsDataReady()339 void AudioServer::NotifySettingsDataReady()
340 {
341 int32_t callingUid = IPCSkeleton::GetCallingUid();
342 CHECK_AND_RETURN_LOG(PermissionUtil::VerifyIsAudio(), "refused for %{public}d", callingUid);
343
344 AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
345 CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "audioEffectChainManager is nullptr");
346 audioEffectChainManager->LoadEffectProperties();
347 }
348 } // namespace AudioStandard
349 } // namespace OHOS
350