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 #include "audio_manager_base.h"
17 #include "audio_system_manager.h"
18 #include "audio_log.h"
19 #include "i_audio_process.h"
20 #include "audio_effect_server.h"
21
22 using namespace std;
23
24 namespace OHOS {
25 namespace AudioStandard {
LoadEffectLibrariesReadData(vector<Library> & libList,vector<Effect> & effectList,MessageParcel & data,int32_t countLib,int32_t countEff)26 static void LoadEffectLibrariesReadData(vector<Library>& libList, vector<Effect>& effectList, MessageParcel &data,
27 int32_t countLib, int32_t countEff)
28 {
29 int32_t i;
30 for (i = 0; i < countLib; i++) {
31 string libName = data.ReadString();
32 string libPath = data.ReadString();
33 libList.push_back({libName, libPath});
34 }
35 for (i = 0; i < countEff; i++) {
36 string effectName = data.ReadString();
37 string libName = data.ReadString();
38 effectList.push_back({effectName, libName});
39 }
40 }
41
LoadEffectLibrariesWriteReply(const vector<Effect> & successEffectList,MessageParcel & reply)42 static void LoadEffectLibrariesWriteReply(const vector<Effect>& successEffectList, MessageParcel &reply)
43 {
44 reply.WriteInt32(successEffectList.size());
45 for (Effect effect: successEffectList) {
46 reply.WriteString(effect.name);
47 reply.WriteString(effect.libraryName);
48 }
49 }
50
HandleGetAudioParameter(MessageParcel & data,MessageParcel & reply)51 int AudioManagerStub::HandleGetAudioParameter(MessageParcel &data, MessageParcel &reply)
52 {
53 const std::string key = data.ReadString();
54 const std::string value = GetAudioParameter(key);
55 reply.WriteString(value);
56 return AUDIO_OK;
57 }
58
HandleSetAudioParameter(MessageParcel & data,MessageParcel & reply)59 int AudioManagerStub::HandleSetAudioParameter(MessageParcel &data, MessageParcel &reply)
60 {
61 const std::string key = data.ReadString();
62 const std::string value = data.ReadString();
63 SetAudioParameter(key, value);
64 return AUDIO_OK;
65 }
66
HandleGetExtraAudioParameters(MessageParcel & data,MessageParcel & reply)67 int AudioManagerStub::HandleGetExtraAudioParameters(MessageParcel &data, MessageParcel &reply)
68 {
69 const std::string mainKey = data.ReadString();
70 int32_t num = data.ReadInt32();
71 std::vector<std::string> subKeys = {};
72 for (int32_t i = 0; i < num; i++) {
73 std::string subKey = data.ReadString();
74 subKeys.push_back(subKey);
75 }
76
77 const std::vector<std::pair<std::string, std::string>> value = GetExtraParameters(mainKey, subKeys);
78 reply.WriteInt32(static_cast<int32_t>(value.size()));
79 for (auto it = value.begin(); it != value.end(); it++) {
80 reply.WriteString(static_cast<std::string>(it->first));
81 reply.WriteString(static_cast<std::string>(it->second));
82 }
83 return AUDIO_OK;
84 }
85
HandleSetExtraAudioParameters(MessageParcel & data,MessageParcel & reply)86 int AudioManagerStub::HandleSetExtraAudioParameters(MessageParcel &data, MessageParcel &reply)
87 {
88 const std::string mainKey = data.ReadString();
89 std::vector<std::pair<std::string, std::string>> audioParametersSubKVPairs;
90 int32_t mapSize = data.ReadInt32();
91 for (int32_t i = 0; i < mapSize; i++) {
92 std::string subKey = data.ReadString();
93 std::string value = data.ReadString();
94 audioParametersSubKVPairs.push_back(std::make_pair(subKey, value));
95 }
96 SetExtraParameters(mainKey, audioParametersSubKVPairs);
97 return AUDIO_OK;
98 }
99
HandleSetMicrophoneMute(MessageParcel & data,MessageParcel & reply)100 int AudioManagerStub::HandleSetMicrophoneMute(MessageParcel &data, MessageParcel &reply)
101 {
102 bool isMute = data.ReadBool();
103 int32_t result = SetMicrophoneMute(isMute);
104 reply.WriteInt32(result);
105 return AUDIO_OK;
106 }
107
HandleSetAudioScene(MessageParcel & data,MessageParcel & reply)108 int AudioManagerStub::HandleSetAudioScene(MessageParcel &data, MessageParcel &reply)
109 {
110 AudioScene audioScene = (static_cast<AudioScene>(data.ReadInt32()));
111 DeviceType activeDevice = (static_cast<DeviceType>(data.ReadInt32()));
112 int32_t result = SetAudioScene(audioScene, activeDevice);
113 reply.WriteInt32(result);
114 return AUDIO_OK;
115 }
116
HandleUpdateActiveDeviceRoute(MessageParcel & data,MessageParcel & reply)117 int AudioManagerStub::HandleUpdateActiveDeviceRoute(MessageParcel &data, MessageParcel &reply)
118 {
119 DeviceType type = static_cast<DeviceType>(data.ReadInt32());
120 DeviceFlag flag = static_cast<DeviceFlag>(data.ReadInt32());
121 int32_t ret = UpdateActiveDeviceRoute(type, flag);
122 reply.WriteInt32(ret);
123 return AUDIO_OK;
124 }
125
HandleGetTransactionId(MessageParcel & data,MessageParcel & reply)126 int AudioManagerStub::HandleGetTransactionId(MessageParcel &data, MessageParcel &reply)
127 {
128 DeviceType deviceType = (static_cast<DeviceType>(data.ReadInt32()));
129 DeviceRole deviceRole = (static_cast<DeviceRole>(data.ReadInt32()));
130 uint64_t transactionId = GetTransactionId(deviceType, deviceRole);
131 reply.WriteUint64(transactionId);
132 return AUDIO_OK;
133 }
134
HandleSetParameterCallback(MessageParcel & data,MessageParcel & reply)135 int AudioManagerStub::HandleSetParameterCallback(MessageParcel &data, MessageParcel &reply)
136 {
137 sptr<IRemoteObject> object = data.ReadRemoteObject();
138 CHECK_AND_RETURN_RET_LOG(object != nullptr, AUDIO_ERR, "SET_PARAMETER_CALLBACK obj is null");
139 int32_t result = SetParameterCallback(object);
140 reply.WriteInt32(result);
141 return AUDIO_OK;
142 }
143
HandleGetRemoteAudioParameter(MessageParcel & data,MessageParcel & reply)144 int AudioManagerStub::HandleGetRemoteAudioParameter(MessageParcel &data, MessageParcel &reply)
145 {
146 const std::string networkId = data.ReadString();
147 AudioParamKey key = static_cast<AudioParamKey>(data.ReadInt32());
148 const std::string condition = data.ReadString();
149 const std::string value = GetAudioParameter(networkId, key, condition);
150 reply.WriteString(value);
151 return AUDIO_OK;
152 }
153
HandleSetRemoteAudioParameter(MessageParcel & data,MessageParcel & reply)154 int AudioManagerStub::HandleSetRemoteAudioParameter(MessageParcel &data, MessageParcel &reply)
155 {
156 const std::string networkId = data.ReadString();
157 AudioParamKey key = static_cast<AudioParamKey>(data.ReadInt32());
158 const std::string condtion = data.ReadString();
159 const std::string value = data.ReadString();
160 SetAudioParameter(networkId, key, condtion, value);
161 return AUDIO_OK;
162 }
163
HandleNotifyDeviceInfo(MessageParcel & data,MessageParcel & reply)164 int AudioManagerStub::HandleNotifyDeviceInfo(MessageParcel &data, MessageParcel &reply)
165 {
166 const std::string networkId = data.ReadString();
167 const bool connected = data.ReadBool();
168 NotifyDeviceInfo(networkId, connected);
169 return AUDIO_OK;
170 }
171
HandleCheckRemoteDeviceState(MessageParcel & data,MessageParcel & reply)172 int AudioManagerStub::HandleCheckRemoteDeviceState(MessageParcel &data, MessageParcel &reply)
173 {
174 std::string networkId = data.ReadString();
175 DeviceRole deviceRole = static_cast<DeviceRole>(data.ReadInt32());
176 bool isStartDevice = data.ReadBool();
177 int32_t result = CheckRemoteDeviceState(networkId, deviceRole, isStartDevice);
178 reply.WriteInt32(result);
179 return AUDIO_OK;
180 }
181
HandleSetVoiceVolume(MessageParcel & data,MessageParcel & reply)182 int AudioManagerStub::HandleSetVoiceVolume(MessageParcel &data, MessageParcel &reply)
183 {
184 const float volume = data.ReadFloat();
185 int32_t result = SetVoiceVolume(volume);
186 reply.WriteInt32(result);
187 return AUDIO_OK;
188 }
189
HandleSetAudioMonoState(MessageParcel & data,MessageParcel & reply)190 int AudioManagerStub::HandleSetAudioMonoState(MessageParcel &data, MessageParcel &reply)
191 {
192 bool audioMonoState = data.ReadBool();
193 SetAudioMonoState(audioMonoState);
194 return AUDIO_OK;
195 }
196
HandleSetAudioBalanceValue(MessageParcel & data,MessageParcel & reply)197 int AudioManagerStub::HandleSetAudioBalanceValue(MessageParcel &data, MessageParcel &reply)
198 {
199 float audioBalanceValue = data.ReadFloat();
200 SetAudioBalanceValue(audioBalanceValue);
201 return AUDIO_OK;
202 }
203
HandleCreateAudioProcess(MessageParcel & data,MessageParcel & reply)204 int AudioManagerStub::HandleCreateAudioProcess(MessageParcel &data, MessageParcel &reply)
205 {
206 AudioProcessConfig config;
207 ProcessConfig::ReadConfigFromParcel(config, data);
208 sptr<IRemoteObject> process = CreateAudioProcess(config);
209 CHECK_AND_RETURN_RET_LOG(process != nullptr, AUDIO_ERR,
210 "CREATE_AUDIOPROCESS AudioManagerStub CreateAudioProcess failed");
211 reply.WriteRemoteObject(process);
212 return AUDIO_OK;
213 }
214
HandleLoadAudioEffectLibraries(MessageParcel & data,MessageParcel & reply)215 int AudioManagerStub::HandleLoadAudioEffectLibraries(MessageParcel &data, MessageParcel &reply)
216 {
217 vector<Library> libList = {};
218 vector<Effect> effectList = {};
219 int32_t countLib = data.ReadInt32();
220 int32_t countEff = data.ReadInt32();
221 CHECK_AND_RETURN_RET_LOG((countLib >= 0) && (countLib <= AUDIO_EFFECT_COUNT_UPPER_LIMIT) &&
222 (countEff >= 0) && (countEff <= AUDIO_EFFECT_COUNT_UPPER_LIMIT), AUDIO_ERR,
223 "LOAD_AUDIO_EFFECT_LIBRARIES read data failed");
224 LoadEffectLibrariesReadData(libList, effectList, data, countLib, countEff);
225 if (countLib > 0) {
226 //load lib and reply success list
227 vector<Effect> successEffectList = {};
228 bool loadSuccess = LoadAudioEffectLibraries(libList, effectList, successEffectList);
229 CHECK_AND_RETURN_RET_LOG(loadSuccess, AUDIO_ERR, "Load audio effect libraries failed, please check log");
230 LoadEffectLibrariesWriteReply(successEffectList, reply);
231 }
232 return AUDIO_OK;
233 }
234
HandleRequestThreadPriority(MessageParcel & data,MessageParcel & reply)235 int AudioManagerStub::HandleRequestThreadPriority(MessageParcel &data, MessageParcel &reply)
236 {
237 uint32_t tid = data.ReadUint32();
238 string bundleName = data.ReadString();
239 RequestThreadPriority(tid, bundleName);
240 return AUDIO_OK;
241 }
242
HandleCreateAudioEffectChainManager(MessageParcel & data,MessageParcel & reply)243 int AudioManagerStub::HandleCreateAudioEffectChainManager(MessageParcel &data, MessageParcel &reply)
244 {
245 int32_t i;
246 vector<EffectChain> effectChains = {};
247 vector<int32_t> countEffect = {};
248 int32_t countChains = data.ReadInt32();
249 CHECK_AND_RETURN_RET_LOG(countChains >= 0 && countChains <= AUDIO_EFFECT_CHAIN_COUNT_UPPER_LIMIT,
250 AUDIO_ERR, "Create audio effect chain manager failed, please check log");
251 for (i = 0; i < countChains; i++) {
252 int32_t count = data.ReadInt32();
253 CHECK_AND_RETURN_RET_LOG(count >= 0 && count <= AUDIO_EFFECT_COUNT_PER_CHAIN_UPPER_LIMIT,
254 AUDIO_ERR, "Create audio effect chain manager failed, please check log");
255 countEffect.emplace_back(count);
256 }
257
258 for (int32_t count: countEffect) {
259 EffectChain effectChain;
260 effectChain.name = data.ReadString();
261 for (i = 0; i < count; i++) {
262 effectChain.apply.emplace_back(data.ReadString());
263 }
264 effectChains.emplace_back(effectChain);
265 }
266
267 unordered_map<string, string> sceneTypeToEffectChainNameMap;
268 int32_t mapSize = data.ReadInt32();
269 CHECK_AND_RETURN_RET_LOG(mapSize >= 0 && mapSize <= AUDIO_EFFECT_CHAIN_CONFIG_UPPER_LIMIT,
270 AUDIO_ERR, "Create audio effect chain manager failed, please check log");
271 for (i = 0; i < mapSize; i++) {
272 string key = data.ReadString();
273 string value = data.ReadString();
274 sceneTypeToEffectChainNameMap[key] = value;
275 }
276
277 bool createSuccess = CreateEffectChainManager(effectChains, sceneTypeToEffectChainNameMap);
278 CHECK_AND_RETURN_RET_LOG(createSuccess, AUDIO_ERR,
279 "Create audio effect chain manager failed, please check log");
280 return AUDIO_OK;
281 }
282
HandleSetOutputDeviceSink(MessageParcel & data,MessageParcel & reply)283 int AudioManagerStub::HandleSetOutputDeviceSink(MessageParcel &data, MessageParcel &reply)
284 {
285 int32_t deviceType = data.ReadInt32();
286 CHECK_AND_RETURN_RET_LOG(deviceType >= DEVICE_TYPE_NONE && deviceType <= DEVICE_TYPE_MAX, AUDIO_ERR,
287 "Set output device sink failed, please check log");
288 std::string sinkName = data.ReadString();
289 bool ret = SetOutputDeviceSink(deviceType, sinkName);
290 CHECK_AND_RETURN_RET_LOG(ret, AUDIO_ERR, "Set output device sink failed, please check log");
291 return AUDIO_OK;
292 }
293
HandleCreatePlaybackCapturerManager(MessageParcel & data,MessageParcel & reply)294 int AudioManagerStub::HandleCreatePlaybackCapturerManager(MessageParcel &data, MessageParcel &reply)
295 {
296 bool ret = CreatePlaybackCapturerManager();
297 reply.WriteBool(ret);
298 return AUDIO_OK;
299 }
300
HandleSetSupportStreamUsage(MessageParcel & data,MessageParcel & reply)301 int AudioManagerStub::HandleSetSupportStreamUsage(MessageParcel &data, MessageParcel &reply)
302 {
303 vector<int32_t> usage;
304 size_t cnt = static_cast<size_t>(data.ReadInt32());
305 CHECK_AND_RETURN_RET_LOG(cnt <= AUDIO_SUPPORTED_STREAM_USAGES.size(), AUDIO_ERR,
306 "Set support stream usage failed, please check");
307 for (size_t i = 0; i < cnt; i++) {
308 int32_t tmp_usage = data.ReadInt32();
309 if (find(AUDIO_SUPPORTED_STREAM_USAGES.begin(), AUDIO_SUPPORTED_STREAM_USAGES.end(), tmp_usage) ==
310 AUDIO_SUPPORTED_STREAM_USAGES.end()) {
311 continue;
312 }
313 usage.emplace_back(tmp_usage);
314 }
315 int32_t ret = SetSupportStreamUsage(usage);
316 reply.WriteInt32(ret);
317 return AUDIO_OK;
318 }
319
HandleRegiestPolicyProvider(MessageParcel & data,MessageParcel & reply)320 int AudioManagerStub::HandleRegiestPolicyProvider(MessageParcel &data, MessageParcel &reply)
321 {
322 sptr<IRemoteObject> object = data.ReadRemoteObject();
323 CHECK_AND_RETURN_RET_LOG(object != nullptr, AUDIO_ERR, "obj is null");
324 int32_t result = RegiestPolicyProvider(object);
325 reply.WriteInt32(result);
326 return AUDIO_OK;
327 }
328
HandleSetWakeupSourceCallback(MessageParcel & data,MessageParcel & reply)329 int AudioManagerStub::HandleSetWakeupSourceCallback(MessageParcel &data, MessageParcel &reply)
330 {
331 sptr<IRemoteObject> object = data.ReadRemoteObject();
332 CHECK_AND_RETURN_RET_LOG(object != nullptr, AUDIO_ERR,
333 "SET_WAKEUP_CLOSE_CALLBACK obj is null");
334 int32_t result = SetWakeupSourceCallback(object);
335 reply.WriteInt32(result);
336 return AUDIO_OK;
337 }
338
HandleSetCaptureSilentState(MessageParcel & data,MessageParcel & reply)339 int AudioManagerStub::HandleSetCaptureSilentState(MessageParcel &data, MessageParcel &reply)
340 {
341 bool state = false;
342 int32_t flag = data.ReadInt32();
343 if (flag == 1) {
344 state = true;
345 }
346 int32_t ret = SetCaptureSilentState(state);
347 reply.WriteInt32(ret);
348 return AUDIO_OK;
349 }
350
HandleUpdateSpatializationState(MessageParcel & data,MessageParcel & reply)351 int AudioManagerStub::HandleUpdateSpatializationState(MessageParcel &data, MessageParcel &reply)
352 {
353 AudioSpatializationState spatializationState;
354 spatializationState.spatializationEnabled = data.ReadBool();
355 spatializationState.headTrackingEnabled = data.ReadBool();
356 int32_t ret = UpdateSpatializationState(spatializationState);
357 reply.WriteInt32(ret);
358 return AUDIO_OK;
359 }
360
HandleGetCapturePresentationPosition(MessageParcel & data,MessageParcel & reply)361 int AudioManagerStub::HandleGetCapturePresentationPosition(MessageParcel &data, MessageParcel &reply)
362 {
363 const std::string deviceClass = data.ReadString();
364 uint64_t frames;
365 int64_t timeSec;
366 int64_t timeNanoSec;
367 int32_t result = GetCapturePresentationPosition(deviceClass, frames, timeSec, timeNanoSec);
368 reply.WriteInt32(result);
369 reply.WriteUint64(frames);
370 reply.WriteInt64(timeSec);
371 reply.WriteInt64(timeNanoSec);
372
373 return AUDIO_OK;
374 }
375
HandleGetRenderPresentationPosition(MessageParcel & data,MessageParcel & reply)376 int AudioManagerStub::HandleGetRenderPresentationPosition(MessageParcel &data, MessageParcel &reply)
377 {
378 const std::string deviceClass = data.ReadString();
379 uint64_t frames;
380 int64_t timeSec;
381 int64_t timeNanoSec;
382 int32_t result = GetRenderPresentationPosition(deviceClass, frames, timeSec, timeNanoSec);
383 reply.WriteInt32(result);
384 reply.WriteUint64(frames);
385 reply.WriteInt64(timeSec);
386 reply.WriteInt64(timeNanoSec);
387
388 return AUDIO_OK;
389 }
390
HandleOffloadSetVolume(MessageParcel & data,MessageParcel & reply)391 int AudioManagerStub::HandleOffloadSetVolume(MessageParcel &data, MessageParcel &reply)
392 {
393 const float volume = data.ReadFloat();
394 int32_t result = OffloadSetVolume(volume);
395 reply.WriteInt32(result);
396 return AUDIO_OK;
397 }
398
HandleOffloadDrain(MessageParcel & data,MessageParcel & reply)399 int AudioManagerStub::HandleOffloadDrain(MessageParcel &data, MessageParcel &reply)
400 {
401 int32_t result = OffloadDrain();
402 reply.WriteInt32(result);
403 return AUDIO_OK;
404 }
405
HandleOffloadGetPresentationPosition(MessageParcel & data,MessageParcel & reply)406 int AudioManagerStub::HandleOffloadGetPresentationPosition(MessageParcel &data, MessageParcel &reply)
407 {
408 uint64_t frames;
409 int64_t timeSec;
410 int64_t timeNanoSec;
411 int32_t result = OffloadGetPresentationPosition(frames, timeSec, timeNanoSec);
412 reply.WriteInt32(result);
413 reply.WriteUint64(frames);
414 reply.WriteInt64(timeSec);
415 reply.WriteInt64(timeNanoSec);
416
417 return AUDIO_OK;
418 }
419
HandleOffloadSetBufferSize(MessageParcel & data,MessageParcel & reply)420 int AudioManagerStub::HandleOffloadSetBufferSize(MessageParcel &data, MessageParcel &reply)
421 {
422 uint32_t sizeMs = data.ReadUint32();
423 int32_t result = OffloadSetBufferSize(sizeMs);
424 reply.WriteInt32(result);
425 return AUDIO_OK;
426 }
427
HandleNotifyStreamVolumeChanged(MessageParcel & data,MessageParcel & reply)428 int AudioManagerStub::HandleNotifyStreamVolumeChanged(MessageParcel &data, MessageParcel &reply)
429 {
430 AudioStreamType type = static_cast<AudioStreamType>(data.ReadInt32());
431 float volume = data.ReadFloat();
432 int32_t ret = NotifyStreamVolumeChanged(type, volume);
433 reply.WriteInt32(ret);
434
435 return AUDIO_OK;
436 }
437
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)438 int AudioManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
439 {
440 CHECK_AND_RETURN_RET_LOG(data.ReadInterfaceToken() == GetDescriptor(),
441 -1, "ReadInterfaceToken failed");
442
443 CHECK_AND_RETURN_RET(code > static_cast<uint32_t>(AudioServerInterfaceCode::AUDIO_SERVER_CODE_MAX),
444 (this->*handlers[code])(data, reply));
445 AUDIO_ERR_LOG("default case, need check AudioManagerStub");
446 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
447 }
448 } // namespace AudioStandard
449 } // namespace OHOS
450