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 "AudioManagerProxy"
17 #endif
18 
19 #include "audio_manager_proxy.h"
20 
21 #include <cinttypes>
22 
23 #include "audio_system_manager.h"
24 #include "audio_service_log.h"
25 #include "audio_utils.h"
26 #include "i_audio_process.h"
27 
28 using namespace std;
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 namespace {
33 constexpr int32_t MAX_OFFLINE_EFFECT_CHAIN_NUM = 10;
34 }
AudioManagerProxy(const sptr<IRemoteObject> & impl)35 AudioManagerProxy::AudioManagerProxy(const sptr<IRemoteObject> &impl)
36     : IRemoteProxy<IStandardAudioService>(impl)
37 {
38 }
39 
SetMicrophoneMute(bool isMute)40 int32_t AudioManagerProxy::SetMicrophoneMute(bool isMute)
41 {
42     MessageParcel data;
43     MessageParcel reply;
44     MessageOption option;
45 
46     bool ret = data.WriteInterfaceToken(GetDescriptor());
47     CHECK_AND_RETURN_RET_LOG(ret, -1, "AudioManagerProxy: WriteInterfaceToken failed");
48     data.WriteBool(isMute);
49     int32_t error = Remote()->SendRequest(
50         static_cast<uint32_t>(AudioServerInterfaceCode::SET_MICROPHONE_MUTE), data, reply, option);
51     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
52         "SetMicrophoneMute failed, error: %d", error);
53 
54     int32_t result = reply.ReadInt32();
55     return result;
56 }
57 
SetVoiceVolume(float volume)58 int32_t AudioManagerProxy::SetVoiceVolume(float volume)
59 {
60     MessageParcel data;
61     MessageParcel reply;
62     MessageOption option;
63 
64     bool ret = data.WriteInterfaceToken(GetDescriptor());
65     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
66 
67     data.WriteFloat(volume);
68 
69     int32_t error = Remote()->SendRequest(
70         static_cast<uint32_t>(AudioServerInterfaceCode::SET_VOICE_VOLUME), data, reply, option);
71     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false,
72         "SetVoiceVolume failed, error: %d", error);
73 
74     int32_t result = reply.ReadInt32();
75     return result;
76 }
77 
OffloadSetVolume(float volume)78 int32_t AudioManagerProxy::OffloadSetVolume(float volume)
79 {
80     MessageParcel data;
81     MessageParcel reply;
82     MessageOption option;
83 
84     bool ret = data.WriteInterfaceToken(GetDescriptor());
85     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
86 
87     data.WriteFloat(volume);
88 
89     int32_t error = Remote()->SendRequest(
90         static_cast<uint32_t>(AudioServerInterfaceCode::OFFLOAD_SET_VOLUME), data, reply, option);
91     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "OffloadSetVolume failed, error: %d", error);
92 
93     int32_t result = reply.ReadInt32();
94     return result;
95 }
96 
SetAudioScene(AudioScene audioScene,std::vector<DeviceType> & activeOutputDevices,DeviceType activeInputDevice,BluetoothOffloadState a2dpOffloadFlag)97 int32_t AudioManagerProxy::SetAudioScene(AudioScene audioScene, std::vector<DeviceType> &activeOutputDevices,
98     DeviceType activeInputDevice, BluetoothOffloadState a2dpOffloadFlag)
99 {
100     CHECK_AND_RETURN_RET_LOG(!activeOutputDevices.empty() &&
101         activeOutputDevices.size() <= AUDIO_CONCURRENT_ACTIVE_DEVICES_LIMIT,
102         ERR_NONE, "Invalid active output devices.");
103     MessageParcel data;
104     MessageParcel reply;
105     MessageOption option;
106 
107     bool ret = data.WriteInterfaceToken(GetDescriptor());
108     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
109 
110     data.WriteInt32(static_cast<int32_t>(audioScene));
111 
112     data.WriteInt32(static_cast<int32_t>(activeOutputDevices.size()));
113     for (auto activeOutputDevice : activeOutputDevices) {
114         data.WriteInt32(static_cast<int32_t>(activeOutputDevice));
115     }
116     data.WriteInt32(static_cast<int32_t>(activeInputDevice));
117     data.WriteInt32(static_cast<int32_t>(a2dpOffloadFlag));
118 
119     int32_t error = Remote()->SendRequest(
120         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_SCENE), data, reply, option);
121     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "SetAudioScene failed, error: %d", error);
122 
123     int32_t result = reply.ReadInt32();
124     return result;
125 }
126 
GetAudioParameter(const std::string & key)127 const std::string AudioManagerProxy::GetAudioParameter(const std::string &key)
128 {
129     MessageParcel data;
130     MessageParcel reply;
131     MessageOption option;
132 
133     bool ret = data.WriteInterfaceToken(GetDescriptor());
134     CHECK_AND_RETURN_RET_LOG(ret, "", "WriteInterfaceToken failed");
135     data.WriteString(static_cast<std::string>(key));
136     int32_t error = Remote()->SendRequest(
137         static_cast<uint32_t>(AudioServerInterfaceCode::GET_AUDIO_PARAMETER), data, reply, option);
138     if (error != ERR_NONE) {
139         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
140         const std::string value = "";
141         return value;
142     }
143 
144     const std::string value = reply.ReadString();
145     return value;
146 }
147 
GetAudioParameter(const std::string & networkId,const AudioParamKey key,const std::string & condition)148 const std::string AudioManagerProxy::GetAudioParameter(const std::string& networkId, const AudioParamKey key,
149     const std::string& condition)
150 {
151     MessageParcel data;
152     MessageParcel reply;
153     MessageOption option;
154 
155     bool ret = data.WriteInterfaceToken(GetDescriptor());
156     CHECK_AND_RETURN_RET_LOG(ret, "", "WriteInterfaceToken failed");
157     data.WriteString(static_cast<std::string>(networkId));
158     data.WriteInt32(static_cast<int32_t>(key));
159     data.WriteString(static_cast<std::string>(condition));
160     int32_t error = Remote()->SendRequest(
161         static_cast<uint32_t>(AudioServerInterfaceCode::GET_REMOTE_AUDIO_PARAMETER), data, reply, option);
162     if (error != ERR_NONE) {
163         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
164         const std::string value = "";
165         return value;
166     }
167 
168     const std::string value = reply.ReadString();
169     return value;
170 }
171 
GetExtraParameters(const std::string & mainKey,const std::vector<std::string> & subKeys,std::vector<std::pair<std::string,std::string>> & result)172 int32_t AudioManagerProxy::GetExtraParameters(const std::string &mainKey, const std::vector<std::string> &subKeys,
173     std::vector<std::pair<std::string, std::string>> &result)
174 {
175     MessageParcel data;
176     MessageParcel reply;
177     MessageOption option;
178 
179     bool ret = data.WriteInterfaceToken(GetDescriptor());
180     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
181     data.WriteString(static_cast<std::string>(mainKey));
182     data.WriteInt32(static_cast<int32_t>(subKeys.size()));
183     for (std::string subKey : subKeys) {
184         data.WriteString(static_cast<std::string>(subKey));
185     }
186     int32_t error = Remote()->SendRequest(
187         static_cast<uint32_t>(AudioServerInterfaceCode::GET_EXTRA_AUDIO_PARAMETERS), data, reply, option);
188     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "Get extra audio parameters failed, error: %d", error);
189 
190     int32_t num = reply.ReadInt32();
191     for (auto i = 0; i < num; i++) {
192         const std::string key = reply.ReadString();
193         const std::string value = reply.ReadString();
194         result.push_back(std::make_pair(key, value));
195     }
196     return reply.ReadInt32();
197 }
198 
SetAudioParameter(const std::string & key,const std::string & value)199 void AudioManagerProxy::SetAudioParameter(const std::string &key, const std::string &value)
200 {
201     MessageParcel data;
202     MessageParcel reply;
203     MessageOption option;
204 
205     bool ret = data.WriteInterfaceToken(GetDescriptor());
206     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
207     data.WriteString(static_cast<std::string>(key));
208     data.WriteString(static_cast<std::string>(value));
209     int32_t error = Remote()->SendRequest(
210         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_PARAMETER), data, reply, option);
211     CHECK_AND_RETURN_LOG(error == ERR_NONE, "Get audio parameter failed, error: %d", error);
212 }
213 
SetAudioParameter(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)214 void AudioManagerProxy::SetAudioParameter(const std::string& networkId, const AudioParamKey key,
215     const std::string& condition, const std::string& value)
216 {
217     MessageParcel data;
218     MessageParcel reply;
219     MessageOption option;
220 
221     bool ret = data.WriteInterfaceToken(GetDescriptor());
222     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
223     data.WriteString(static_cast<std::string>(networkId));
224     data.WriteInt32(static_cast<int32_t>(key));
225     data.WriteString(static_cast<std::string>(condition));
226     data.WriteString(static_cast<std::string>(value));
227     int32_t error = Remote()->SendRequest(
228         static_cast<uint32_t>(AudioServerInterfaceCode::SET_REMOTE_AUDIO_PARAMETER), data, reply, option);
229     CHECK_AND_RETURN_LOG(error == ERR_NONE, "Get audio parameter failed, error: %d", error);
230 }
231 
SetExtraParameters(const std::string & key,const std::vector<std::pair<std::string,std::string>> & kvpairs)232 int32_t AudioManagerProxy::SetExtraParameters(const std::string &key,
233     const std::vector<std::pair<std::string, std::string>> &kvpairs)
234 {
235     MessageParcel data;
236     MessageParcel reply;
237     MessageOption option;
238 
239     bool ret = data.WriteInterfaceToken(GetDescriptor());
240     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
241     data.WriteString(static_cast<std::string>(key));
242     data.WriteInt32(static_cast<int32_t>(kvpairs.size()));
243     for (auto it = kvpairs.begin(); it != kvpairs.end(); it++) {
244         data.WriteString(static_cast<std::string>(it->first));
245         data.WriteString(static_cast<std::string>(it->second));
246     }
247 
248     int32_t error = Remote()->SendRequest(
249         static_cast<uint32_t>(AudioServerInterfaceCode::SET_EXTRA_AUDIO_PARAMETERS), data, reply, option);
250     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "Set extra audio parameters failed, error: %d", error);
251     return reply.ReadInt32();
252 }
253 
SetAsrAecMode(AsrAecMode asrAecMode)254 int32_t AudioManagerProxy::SetAsrAecMode(AsrAecMode asrAecMode)
255 {
256     MessageParcel data;
257     MessageParcel reply;
258     MessageOption option;
259 
260     bool ret = data.WriteInterfaceToken(GetDescriptor());
261     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
262     data.WriteInt32(static_cast<int32_t>(asrAecMode));
263 
264     int32_t error = Remote()->SendRequest(
265         static_cast<uint32_t>(AudioServerInterfaceCode::SET_ASR_AEC_MODE), data, reply, option);
266     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
267     int32_t result = reply.ReadInt32();
268     return result;
269 }
270 
GetAsrAecMode(AsrAecMode & asrAecMode)271 int32_t AudioManagerProxy::GetAsrAecMode(AsrAecMode &asrAecMode)
272 {
273     MessageParcel data;
274     MessageParcel reply;
275     MessageOption option;
276 
277     bool ret = data.WriteInterfaceToken(GetDescriptor());
278     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
279     data.WriteInt32(static_cast<int32_t>(asrAecMode));
280 
281     int32_t error = Remote()->SendRequest(
282         static_cast<uint32_t>(AudioServerInterfaceCode::GET_ASR_AEC_MODE), data, reply, option);
283     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
284     int32_t result = reply.ReadInt32();
285     asrAecMode = static_cast<AsrAecMode>(result);
286     return 0;
287 }
288 
SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode)289 int32_t AudioManagerProxy::SetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode asrNoiseSuppressionMode)
290 {
291     MessageParcel data;
292     MessageParcel reply;
293     MessageOption option;
294 
295     bool ret = data.WriteInterfaceToken(GetDescriptor());
296     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
297     data.WriteInt32(static_cast<int32_t>(asrNoiseSuppressionMode));
298 
299     int32_t error = Remote()->SendRequest(
300         static_cast<uint32_t>(AudioServerInterfaceCode::SET_ASR_NOISE_SUPPRESSION_MODE), data, reply, option);
301     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
302     int32_t result = reply.ReadInt32();
303     return result;
304 }
305 
GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode & asrNoiseSuppressionMode)306 int32_t AudioManagerProxy::GetAsrNoiseSuppressionMode(AsrNoiseSuppressionMode &asrNoiseSuppressionMode)
307 {
308     MessageParcel data;
309     MessageParcel reply;
310     MessageOption option;
311 
312     bool ret = data.WriteInterfaceToken(GetDescriptor());
313     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
314     data.WriteInt32(static_cast<int32_t>(asrNoiseSuppressionMode));
315 
316     int32_t error = Remote()->SendRequest(
317         static_cast<uint32_t>(AudioServerInterfaceCode::GET_ASR_NOISE_SUPPRESSION_MODE), data, reply, option);
318     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
319     int32_t result = reply.ReadInt32();
320     asrNoiseSuppressionMode = static_cast<AsrNoiseSuppressionMode>(result);
321     return 0;
322 }
323 
SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode)324 int32_t AudioManagerProxy::SetAsrWhisperDetectionMode(AsrWhisperDetectionMode asrWhisperDetectionMode)
325 {
326     MessageParcel data;
327     MessageParcel reply;
328     MessageOption option;
329 
330     bool ret = data.WriteInterfaceToken(GetDescriptor());
331     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
332     data.WriteInt32(static_cast<int32_t>(asrWhisperDetectionMode));
333 
334     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(
335         AudioServerInterfaceCode::SET_ASR_WHISPER_DETECTION_MODE), data, reply, option);
336     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
337     int32_t result = reply.ReadInt32();
338     return result;
339 }
340 
GetAsrWhisperDetectionMode(AsrWhisperDetectionMode & asrWhisperDetectionMode)341 int32_t AudioManagerProxy::GetAsrWhisperDetectionMode(AsrWhisperDetectionMode &asrWhisperDetectionMode)
342 {
343     MessageParcel data;
344     MessageParcel reply;
345     MessageOption option;
346 
347     bool ret = data.WriteInterfaceToken(GetDescriptor());
348     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
349     data.WriteInt32(static_cast<int32_t>(asrWhisperDetectionMode));
350 
351     int32_t error = Remote()->SendRequest(
352         static_cast<uint32_t>(AudioServerInterfaceCode::GET_ASR_WHISPER_DETECTION_MODE), data, reply, option);
353     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
354     int32_t result = reply.ReadInt32();
355     asrWhisperDetectionMode = static_cast<AsrWhisperDetectionMode>(result);
356     return 0;
357 }
358 
SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode,bool on)359 int32_t AudioManagerProxy::SetAsrVoiceControlMode(AsrVoiceControlMode asrVoiceControlMode, bool on)
360 {
361     MessageParcel data;
362     MessageParcel reply;
363     MessageOption option;
364 
365     bool ret = data.WriteInterfaceToken(GetDescriptor());
366     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
367     data.WriteInt32(static_cast<int32_t>(asrVoiceControlMode));
368     data.WriteBool(on);
369 
370     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(AudioServerInterfaceCode::SET_ASR_VOICE_CONTROL_MODE),
371         data, reply, option);
372     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
373     int32_t result = reply.ReadInt32();
374     return result;
375 }
376 
SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode,bool on)377 int32_t AudioManagerProxy::SetAsrVoiceMuteMode(AsrVoiceMuteMode asrVoiceMuteMode, bool on)
378 {
379     MessageParcel data;
380     MessageParcel reply;
381     MessageOption option;
382 
383     bool ret = data.WriteInterfaceToken(GetDescriptor());
384     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
385     data.WriteInt32(static_cast<int32_t>(asrVoiceMuteMode));
386     data.WriteBool(on);
387 
388     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(AudioServerInterfaceCode::SET_ASR_VOICE_MUTE_MODE),
389         data, reply, option);
390     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed,error:%d", error);
391     int32_t result = reply.ReadInt32();
392     return result;
393 }
394 
IsWhispering()395 int32_t AudioManagerProxy::IsWhispering()
396 {
397     MessageParcel data;
398     MessageParcel reply;
399     MessageOption option;
400 
401     bool ret = data.WriteInterfaceToken(GetDescriptor());
402     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
403 
404     int32_t result = Remote()->SendRequest(
405         static_cast<uint32_t>(AudioServerInterfaceCode::IS_WHISPERING), data, reply, option);
406     return result;
407 }
408 
GetEffectOffloadEnabled()409 bool AudioManagerProxy::GetEffectOffloadEnabled()
410 {
411     MessageParcel data;
412     MessageParcel reply;
413     MessageOption option;
414 
415     bool ret = data.WriteInterfaceToken(GetDescriptor());
416     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
417 
418     int32_t error = Remote()->SendRequest(
419         static_cast<uint32_t>(AudioServerInterfaceCode::GET_EFFECT_OFFLOAD_ENABLED), data, reply, option);
420     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "failed,error:%d", error);
421     bool result = reply.ReadBool();
422     return result;
423 }
424 
GetTransactionId(DeviceType deviceType,DeviceRole deviceRole)425 uint64_t AudioManagerProxy::GetTransactionId(DeviceType deviceType, DeviceRole deviceRole)
426 {
427     MessageParcel data;
428     MessageParcel reply;
429     MessageOption option;
430     uint32_t transactionId = 0;
431 
432     bool ret = data.WriteInterfaceToken(GetDescriptor());
433     CHECK_AND_RETURN_RET_LOG(ret, transactionId, "WriteInterfaceToken failed");
434 
435     data.WriteInt32(static_cast<int32_t>(deviceType));
436     data.WriteInt32(static_cast<int32_t>(deviceRole));
437 
438     int32_t error = Remote()->SendRequest(
439         static_cast<uint32_t>(AudioServerInterfaceCode::GET_TRANSACTION_ID), data, reply, option);
440     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, transactionId, "get transaction id failed, error: %d", error);
441 
442     transactionId = reply.ReadUint64();
443 
444     return transactionId;
445 }
446 
NotifyDeviceInfo(std::string networkId,bool connected)447 void AudioManagerProxy::NotifyDeviceInfo(std::string networkId, bool connected)
448 {
449     MessageParcel data;
450     MessageParcel reply;
451     MessageOption option;
452 
453     bool ret = data.WriteInterfaceToken(GetDescriptor());
454     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
455     data.WriteString(networkId);
456     data.WriteBool(connected);
457     int32_t error = Remote()->SendRequest(
458         static_cast<uint32_t>(AudioServerInterfaceCode::NOTIFY_DEVICE_INFO), data, reply, option);
459     CHECK_AND_RETURN_LOG(error == ERR_NONE, "Get audio parameter failed, error: %d", error);
460 }
461 
CheckRemoteDeviceState(std::string networkId,DeviceRole deviceRole,bool isStartDevice)462 int32_t AudioManagerProxy::CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice)
463 {
464     MessageParcel data;
465     MessageParcel reply;
466     MessageOption option;
467 
468     bool ret = data.WriteInterfaceToken(GetDescriptor());
469     CHECK_AND_RETURN_RET_LOG(ret, ERR_TRANSACTION_FAILED, "WriteInterfaceToken failed");
470     data.WriteString(networkId);
471     data.WriteInt32(static_cast<int32_t>(deviceRole));
472     data.WriteBool(isStartDevice);
473     int32_t error = Remote()->SendRequest(
474         static_cast<uint32_t>(AudioServerInterfaceCode::CHECK_REMOTE_DEVICE_STATE), data, reply, option);
475     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "CheckRemoteDeviceState failed in proxy, error: %d", error);
476     return reply.ReadInt32();
477 }
478 
UpdateActiveDeviceRoute(DeviceType type,DeviceFlag flag,BluetoothOffloadState a2dpOffloadFlag)479 int32_t AudioManagerProxy::UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag,
480     BluetoothOffloadState a2dpOffloadFlag)
481 {
482     MessageParcel data;
483     MessageParcel reply;
484     MessageOption option;
485 
486     bool ret = data.WriteInterfaceToken(GetDescriptor());
487     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
488 
489     data.WriteInt32(type);
490     data.WriteInt32(flag);
491     data.WriteInt32(static_cast<int32_t>(a2dpOffloadFlag));
492 
493     auto error = Remote()->SendRequest(
494         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_ROUTE_REQ), data, reply, option);
495     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "UpdateActiveDeviceRoute failed, error: %{public}d", error);
496 
497     auto result = reply.ReadInt32();
498     AUDIO_DEBUG_LOG("[UPDATE_ROUTE_REQ] result %{public}d", result);
499     return result;
500 }
501 
UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType,DeviceFlag>> & activeDevices,BluetoothOffloadState a2dpOffloadFlag)502 int32_t AudioManagerProxy::UpdateActiveDevicesRoute(std::vector<std::pair<DeviceType, DeviceFlag>> &activeDevices,
503     BluetoothOffloadState a2dpOffloadFlag)
504 {
505     CHECK_AND_RETURN_RET_LOG(!activeDevices.empty() && activeDevices.size() <= AUDIO_CONCURRENT_ACTIVE_DEVICES_LIMIT,
506         ERR_NONE, "Invalid active output devices.");
507     MessageParcel data;
508     MessageParcel reply;
509     MessageOption option;
510 
511     bool ret = data.WriteInterfaceToken(GetDescriptor());
512     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
513     data.WriteInt32(static_cast<int32_t>(activeDevices.size()));
514     for (auto it = activeDevices.begin(); it != activeDevices.end(); it++) {
515         data.WriteInt32(static_cast<int32_t>(it->first));
516         data.WriteInt32(static_cast<int32_t>(it->second));
517     }
518     data.WriteInt32(static_cast<int32_t>(a2dpOffloadFlag));
519 
520     auto error = Remote()->SendRequest(
521         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_ROUTES_REQ), data, reply, option);
522     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "UpdateActiveDevicesRoute failed, error: %{public}d", error);
523 
524     auto result = reply.ReadInt32();
525     AUDIO_DEBUG_LOG("[UPDATE_ROUTES_REQ] result %{public}d", result);
526     return result;
527 }
528 
UpdateDualToneState(bool enable,int32_t sessionId)529 int32_t AudioManagerProxy::UpdateDualToneState(bool enable, int32_t sessionId)
530 {
531     MessageParcel data;
532     MessageParcel reply;
533     MessageOption option;
534 
535     bool ret = data.WriteInterfaceToken(GetDescriptor());
536     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
537 
538     data.WriteBool(enable);
539     data.WriteInt32(sessionId);
540     auto error = Remote()->SendRequest(
541         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_DUAL_TONE_REQ), data, reply, option);
542     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "UpdateDualToneState failed, error: %{public}d", error);
543 
544     auto result = reply.ReadInt32();
545     AUDIO_DEBUG_LOG("[UPDATE_DUAL_TONE_REQ] result %{public}d", result);
546     return result;
547 }
548 
SetParameterCallback(const sptr<IRemoteObject> & object)549 int32_t AudioManagerProxy::SetParameterCallback(const sptr<IRemoteObject>& object)
550 {
551     MessageParcel data;
552     MessageParcel reply;
553     MessageOption option;
554 
555     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null");
556 
557     bool ret = data.WriteInterfaceToken(GetDescriptor());
558     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
559 
560     (void)data.WriteRemoteObject(object);
561     int error = Remote()->SendRequest(
562         static_cast<uint32_t>(AudioServerInterfaceCode::SET_PARAMETER_CALLBACK), data, reply, option);
563     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
564         "SetParameterCallback failed, error: %{public}d", error);
565 
566     return reply.ReadInt32();
567 }
568 
RegiestPolicyProvider(const sptr<IRemoteObject> & object)569 int32_t AudioManagerProxy::RegiestPolicyProvider(const sptr<IRemoteObject> &object)
570 {
571     MessageParcel data;
572     MessageParcel reply;
573     MessageOption option(MessageOption::TF_ASYNC);
574 
575     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null");
576     bool ret = data.WriteInterfaceToken(GetDescriptor());
577     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
578 
579     (void)data.WriteRemoteObject(object);
580     int error = Remote()->SendRequest(static_cast<uint32_t>(AudioServerInterfaceCode::REGISET_POLICY_PROVIDER), data,
581         reply, option);
582     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
583         "RegiestPolicyProvider failed, error: %{public}d", error);
584 
585     return reply.ReadInt32();
586 }
587 
SetWakeupSourceCallback(const sptr<IRemoteObject> & object)588 int32_t AudioManagerProxy::SetWakeupSourceCallback(const sptr<IRemoteObject>& object)
589 {
590     MessageParcel data;
591     MessageParcel reply;
592     MessageOption option;
593 
594     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null");
595     bool ret = data.WriteInterfaceToken(GetDescriptor());
596     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
597 
598     (void)data.WriteRemoteObject(object);
599     int error = Remote()->SendRequest(
600         static_cast<uint32_t>(AudioServerInterfaceCode::SET_WAKEUP_CLOSE_CALLBACK), data, reply, option);
601     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
602         "SetWakeupCloseCallback failed, error: %{public}d", error);
603 
604     return reply.ReadInt32();
605 }
606 
SetAudioMonoState(bool audioMono)607 void AudioManagerProxy::SetAudioMonoState(bool audioMono)
608 {
609     MessageParcel data;
610     MessageParcel reply;
611     MessageOption option;
612 
613     bool ret = data.WriteInterfaceToken(GetDescriptor());
614     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
615     (void)data.WriteBool(audioMono);
616     int error = Remote()->SendRequest(
617         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_MONO_STATE), data, reply, option);
618     CHECK_AND_RETURN_LOG(error == ERR_NONE, "SetAudioMonoState failed, error: %{public}d", error);
619 }
620 
SetAudioBalanceValue(float audioBalance)621 void AudioManagerProxy::SetAudioBalanceValue(float audioBalance)
622 {
623     MessageParcel data;
624     MessageParcel reply;
625     MessageOption option;
626 
627     bool ret = data.WriteInterfaceToken(GetDescriptor());
628     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
629     (void)data.WriteFloat(audioBalance);
630     int error = Remote()->SendRequest(
631         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_BALANCE_VALUE), data, reply, option);
632     CHECK_AND_RETURN_LOG(error == ERR_NONE, "SetAudioBalanceValue failed, error: %{public}d", error);
633 }
634 
CreateAudioProcess(const AudioProcessConfig & config,int32_t & errorCode)635 sptr<IRemoteObject> AudioManagerProxy::CreateAudioProcess(const AudioProcessConfig &config, int32_t &errorCode)
636 {
637     MessageParcel data;
638     MessageParcel reply;
639     MessageOption option;
640 
641     bool ret = data.WriteInterfaceToken(GetDescriptor());
642     CHECK_AND_RETURN_RET_LOG(ret, nullptr, "WriteInterfaceToken failed");
643     ProcessConfig::WriteConfigToParcel(config, data);
644     int error = Remote()->SendRequest(
645         static_cast<uint32_t>(AudioServerInterfaceCode::CREATE_AUDIOPROCESS), data, reply, option);
646     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, nullptr, "CreateAudioProcess failed, error: %{public}d", error);
647     sptr<IRemoteObject> process = reply.ReadRemoteObject();
648     errorCode = reply.ReadInt32();
649     return process;
650 }
651 
LoadAudioEffectLibraries(const vector<Library> libraries,const vector<Effect> effects,vector<Effect> & successEffects)652 bool AudioManagerProxy::LoadAudioEffectLibraries(const vector<Library> libraries, const vector<Effect> effects,
653     vector<Effect> &successEffects)
654 {
655     int32_t error;
656     int32_t i;
657 
658     MessageParcel dataParcel;
659     MessageParcel replyParcel;
660     MessageOption option;
661     bool ret = dataParcel.WriteInterfaceToken(GetDescriptor());
662     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
663 
664     uint32_t countLib = libraries.size();
665     uint32_t countEff = effects.size();
666 
667     dataParcel.WriteInt32(countLib);
668     dataParcel.WriteInt32(countEff);
669 
670     for (Library x : libraries) {
671         dataParcel.WriteString(x.name);
672         dataParcel.WriteString(x.path);
673     }
674 
675     for (Effect x : effects) {
676         dataParcel.WriteString(x.name);
677         dataParcel.WriteString(x.libraryName);
678     }
679 
680     error = Remote()->SendRequest(
681         static_cast<uint32_t>(AudioServerInterfaceCode::LOAD_AUDIO_EFFECT_LIBRARIES), dataParcel, replyParcel, option);
682     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "LoadAudioEffectLibraries failed, error: %{public}d", error);
683 
684     int32_t successEffSize = replyParcel.ReadInt32();
685     CHECK_AND_RETURN_RET_LOG((successEffSize >= 0) && (successEffSize <= AUDIO_EFFECT_COUNT_UPPER_LIMIT),
686         false, "LOAD_AUDIO_EFFECT_LIBRARIES read replyParcel failed");
687 
688     for (i = 0; i < successEffSize; i++) {
689         string effectName = replyParcel.ReadString();
690         string libName = replyParcel.ReadString();
691         successEffects.push_back({effectName, libName});
692     }
693 
694     return true;
695 }
696 
RequestThreadPriority(uint32_t tid,string bundleName)697 void AudioManagerProxy::RequestThreadPriority(uint32_t tid, string bundleName)
698 {
699     MessageParcel data;
700     MessageParcel reply;
701     MessageOption option;
702 
703     bool ret = data.WriteInterfaceToken(GetDescriptor());
704     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
705     (void)data.WriteUint32(tid);
706     (void)data.WriteString(bundleName);
707     int error = Remote()->SendRequest(
708         static_cast<uint32_t>(AudioServerInterfaceCode::REQUEST_THREAD_PRIORITY), data, reply, option);
709     CHECK_AND_RETURN_LOG(error == ERR_NONE, "RequestThreadPriority failed, error: %{public}d", error);
710 }
711 
MarshellEffectChainMgrParam(const EffectChainManagerParam & effectChainMgrParam,MessageParcel & data)712 static void MarshellEffectChainMgrParam(const EffectChainManagerParam &effectChainMgrParam, MessageParcel &data)
713 {
714     data.WriteInt32(effectChainMgrParam.maxExtraNum);
715     data.WriteString(effectChainMgrParam.defaultSceneName);
716     data.WriteInt32(effectChainMgrParam.priorSceneList.size());
717     for (const auto &prioScene : effectChainMgrParam.priorSceneList) {
718         data.WriteString(prioScene);
719     }
720 
721     data.WriteInt32(effectChainMgrParam.sceneTypeToChainNameMap.size());
722     for (const auto &[scene, chain] : effectChainMgrParam.sceneTypeToChainNameMap) {
723         data.WriteString(scene);
724         data.WriteString(chain);
725     }
726 
727     data.WriteInt32(effectChainMgrParam.effectDefaultProperty.size());
728     for (const auto &[effect, prop] : effectChainMgrParam.effectDefaultProperty) {
729         data.WriteString(effect);
730         data.WriteString(prop);
731     }
732 }
733 
CreateEffectChainManager(std::vector<EffectChain> & effectChains,const EffectChainManagerParam & effectParam,const EffectChainManagerParam & enhanceParam)734 bool AudioManagerProxy::CreateEffectChainManager(std::vector<EffectChain> &effectChains,
735     const EffectChainManagerParam &effectParam, const EffectChainManagerParam &enhanceParam)
736 {
737     int32_t error;
738 
739     MessageParcel dataParcel;
740     MessageParcel replyParcel;
741     MessageOption option;
742 
743     bool ret = dataParcel.WriteInterfaceToken(GetDescriptor());
744     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
745 
746     uint32_t countEffectChains = effectChains.size();
747     std::vector<int32_t> listCountEffects;
748 
749     for (EffectChain &effectChain: effectChains) {
750         listCountEffects.emplace_back(effectChain.apply.size());
751     }
752 
753     dataParcel.WriteInt32(countEffectChains);
754     for (int32_t countEffects: listCountEffects) {
755         dataParcel.WriteInt32(countEffects);
756     }
757 
758     for (EffectChain &effectChain: effectChains) {
759         dataParcel.WriteString(effectChain.name);
760         for (std::string applyName: effectChain.apply) {
761             dataParcel.WriteString(applyName);
762         }
763     }
764 
765     MarshellEffectChainMgrParam(effectParam, dataParcel);
766     MarshellEffectChainMgrParam(enhanceParam, dataParcel);
767 
768     error = Remote()->SendRequest(
769         static_cast<uint32_t>(AudioServerInterfaceCode::CREATE_AUDIO_EFFECT_CHAIN_MANAGER),
770         dataParcel, replyParcel, option);
771     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false,
772         "CreateAudioEffectChainManager failed, error: %{public}d", error);
773     return true;
774 }
775 
SetOutputDeviceSink(int32_t deviceType,std::string & sinkName)776 void AudioManagerProxy::SetOutputDeviceSink(int32_t deviceType, std::string &sinkName)
777 {
778     int32_t error;
779 
780     MessageParcel dataParcel;
781     MessageParcel replyParcel;
782     MessageOption option;
783     bool ret = dataParcel.WriteInterfaceToken(GetDescriptor());
784     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
785     dataParcel.WriteInt32(deviceType);
786     dataParcel.WriteString(sinkName);
787 
788     error = Remote()->SendRequest(
789         static_cast<uint32_t>(AudioServerInterfaceCode::SET_OUTPUT_DEVICE_SINK), dataParcel, replyParcel, option);
790         CHECK_AND_RETURN_LOG(error == ERR_NONE, "SetOutputDeviceSink failed, error: %{public}d", error);
791     return;
792 }
793 
CreatePlaybackCapturerManager()794 bool AudioManagerProxy::CreatePlaybackCapturerManager()
795 {
796     int32_t error;
797     MessageParcel data;
798     MessageParcel reply;
799     MessageOption option;
800     bool ret = data.WriteInterfaceToken(GetDescriptor());
801     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
802 
803     error = Remote()->SendRequest(
804         static_cast<uint32_t>(AudioServerInterfaceCode::CREATE_PLAYBACK_CAPTURER_MANAGER), data, reply, option);
805     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false,
806         "CreatePlaybackCapturerManager failed, error: %{public}d", error);
807 
808     return reply.ReadBool();
809 }
810 
SetSupportStreamUsage(std::vector<int32_t> usage)811 int32_t AudioManagerProxy::SetSupportStreamUsage(std::vector<int32_t> usage)
812 {
813     int32_t error;
814     MessageParcel data;
815     MessageParcel reply;
816     MessageOption option;
817 
818     bool ret = data.WriteInterfaceToken(GetDescriptor());
819     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
820 
821     int32_t cnt = (int32_t)usage.size();
822     data.WriteInt32(cnt);
823     for (int32_t i = 0; i < cnt; i++) {
824         data.WriteInt32(usage[i]);
825     }
826 
827     error = Remote()->SendRequest(
828         static_cast<uint32_t>(AudioServerInterfaceCode::SET_SUPPORT_STREAM_USAGE), data, reply, option);
829     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
830         "SetSupportStreamUsage failed, error: %{public}d", error);
831 
832     return reply.ReadInt32();
833 }
834 
SetCaptureSilentState(bool state)835 int32_t AudioManagerProxy::SetCaptureSilentState(bool state)
836 {
837     int32_t error;
838     MessageParcel data;
839     MessageParcel reply;
840     MessageOption option;
841 
842     bool ret = data.WriteInterfaceToken(GetDescriptor());
843     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
844 
845     data.WriteInt32(static_cast<int32_t>(state));
846     error = Remote()->SendRequest(static_cast<uint32_t>(AudioServerInterfaceCode::SET_CAPTURE_SILENT_STATE),
847         data, reply, option);
848     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
849         "SetCaptureSilentState failed, error: %{public}d", error);
850     return reply.ReadInt32();
851 }
852 
NotifyStreamVolumeChanged(AudioStreamType streamType,float volume)853 int32_t AudioManagerProxy::NotifyStreamVolumeChanged(AudioStreamType streamType, float volume)
854 {
855     int32_t error;
856     MessageParcel data;
857     MessageParcel reply;
858     MessageOption option;
859 
860     if (!data.WriteInterfaceToken(GetDescriptor())) {
861         AUDIO_ERR_LOG("NotifyStreamVolumeChanged: WriteInterfaceToken failed");
862         return -1;
863     }
864 
865     data.WriteInt32(static_cast<int32_t>(streamType));
866     data.WriteFloat(volume);
867     error = Remote()->SendRequest(static_cast<uint32_t>(AudioServerInterfaceCode::NOTIFY_STREAM_VOLUME_CHANGED),
868         data, reply, option);
869     if (error != ERR_NONE) {
870         AUDIO_ERR_LOG("NotifyStreamVolumeChanged failed, error: %{public}d", error);
871         return error;
872     }
873     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "NotifyStreamVolumeChanged failed, error: %{public}d", error);
874     return reply.ReadInt32();
875 }
876 
UpdateSpatializationState(AudioSpatializationState spatializationState)877 int32_t AudioManagerProxy::UpdateSpatializationState(AudioSpatializationState spatializationState)
878 {
879     int32_t error;
880     MessageParcel data;
881     MessageParcel reply;
882     MessageOption option;
883 
884     bool ret = data.WriteInterfaceToken(GetDescriptor());
885     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
886     data.WriteBool(spatializationState.spatializationEnabled);
887     data.WriteBool(spatializationState.headTrackingEnabled);
888 
889     error = Remote()->SendRequest(
890         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_SPATIALIZATION_STATE), data, reply, option);
891     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
892         "UpdateSpatializationState failed, error: %{public}d", error);
893 
894     return reply.ReadInt32();
895 }
896 
UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)897 int32_t AudioManagerProxy::UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType)
898 {
899     int32_t error;
900     MessageParcel data;
901     MessageParcel reply;
902     MessageOption option;
903 
904     bool ret = data.WriteInterfaceToken(GetDescriptor());
905     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
906     data.WriteInt32(spatialDeviceType);
907 
908     error = Remote()->SendRequest(
909         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_SPATIAL_DEVICE_TYPE), data, reply, option);
910     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "UpdateSpatialDeviceType failed, error: %{public}d", error);
911 
912     return reply.ReadInt32();
913 }
914 
SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)915 int32_t AudioManagerProxy::SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType)
916 {
917     int32_t error;
918     MessageParcel data;
919     MessageParcel reply;
920     MessageOption option;
921 
922     bool ret = data.WriteInterfaceToken(GetDescriptor());
923     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
924     data.WriteInt32(static_cast<int32_t>(spatializationSceneType));
925 
926     error = Remote()->SendRequest(
927         static_cast<uint32_t>(AudioServerInterfaceCode::SET_SPATIALIZATION_SCENE_TYPE), data, reply, option);
928     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
929 
930     return reply.ReadInt32();
931 }
932 
ResetRouteForDisconnect(DeviceType type)933 int32_t AudioManagerProxy::ResetRouteForDisconnect(DeviceType type)
934 {
935     int32_t error;
936     MessageParcel data;
937     MessageParcel reply;
938     MessageOption option;
939 
940     bool ret = data.WriteInterfaceToken(GetDescriptor());
941     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
942     data.WriteInt32(static_cast<int32_t>(type));
943 
944     error = Remote()->SendRequest(
945         static_cast<uint32_t>(AudioServerInterfaceCode::RESET_ROUTE_FOR_DISCONNECT), data, reply, option);
946     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "error: %{public}d", error);
947 
948     return reply.ReadInt32();
949 }
950 
GetEffectLatency(const std::string & sessionId)951 uint32_t AudioManagerProxy::GetEffectLatency(const std::string &sessionId)
952 {
953     int32_t error;
954     MessageParcel data;
955     MessageParcel reply;
956     MessageOption option;
957 
958     bool ret = data.WriteInterfaceToken(GetDescriptor());
959     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
960     data.WriteString(sessionId);
961 
962     error = Remote()->SendRequest(
963         static_cast<uint32_t>(AudioServerInterfaceCode::GET_EFFECT_LATENCY), data, reply, option);
964     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "error: %{public}d", error);
965 
966     return reply.ReadUint32();
967 }
968 
UpdateLatencyTimestamp(std::string & timestamp,bool isRenderer)969 void AudioManagerProxy::UpdateLatencyTimestamp(std::string &timestamp, bool isRenderer)
970 {
971     int32_t error;
972     MessageParcel data;
973     MessageParcel reply;
974     MessageOption option;
975 
976     bool ret = data.WriteInterfaceToken(GetDescriptor());
977     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
978     data.WriteString(timestamp);
979     data.WriteBool(isRenderer);
980 
981     error = Remote()->SendRequest(
982         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_LATENCY_TIMESTAMP), data, reply, option);
983     CHECK_AND_RETURN_LOG(error == ERR_NONE,
984         "LatencyMeas UpdateLatencyTimestamp failed, error:%{public}d", error);
985 }
986 
GetMaxAmplitude(bool isOutputDevice,int32_t deviceType)987 float AudioManagerProxy::GetMaxAmplitude(bool isOutputDevice, int32_t deviceType)
988 {
989     int32_t error;
990     MessageParcel data;
991     MessageParcel reply;
992     MessageOption option;
993 
994     bool ret = data.WriteInterfaceToken(GetDescriptor());
995     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
996     data.WriteBool(isOutputDevice);
997     data.WriteInt32(deviceType);
998 
999     error = Remote()->SendRequest(
1000         static_cast<uint32_t>(AudioServerInterfaceCode::GET_MAX_AMPLITUDE), data, reply, option);
1001     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1002 
1003     return reply.ReadFloat();
1004 }
1005 
ResetAudioEndpoint()1006 void AudioManagerProxy::ResetAudioEndpoint()
1007 {
1008     int32_t error;
1009     MessageParcel data;
1010     MessageParcel reply;
1011     MessageOption option;
1012 
1013     bool ret = data.WriteInterfaceToken(GetDescriptor());
1014     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1015     error = Remote()->SendRequest(
1016         static_cast<uint32_t>(AudioServerInterfaceCode::RESET_AUDIO_ENDPOINT), data, reply, option);
1017     CHECK_AND_RETURN_LOG(error == ERR_NONE, "Send request failed, error:%{public}d", error);
1018 }
1019 
SuspendRenderSink(const std::string & sinkName)1020 int32_t AudioManagerProxy::SuspendRenderSink(const std::string &sinkName)
1021 {
1022     int32_t error;
1023     MessageParcel data;
1024     MessageParcel reply;
1025     MessageOption option;
1026 
1027     bool ret = data.WriteInterfaceToken(GetDescriptor());
1028     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1029     data.WriteString(sinkName);
1030 
1031     error = Remote()->SendRequest(
1032         static_cast<uint32_t>(AudioServerInterfaceCode::SUSPEND_RENDERSINK), data, reply, option);
1033     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1034 
1035     return reply.ReadInt32();
1036 }
1037 
RestoreRenderSink(const std::string & sinkName)1038 int32_t AudioManagerProxy::RestoreRenderSink(const std::string &sinkName)
1039 {
1040     int32_t error;
1041     MessageParcel data;
1042     MessageParcel reply;
1043     MessageOption option;
1044 
1045     bool ret = data.WriteInterfaceToken(GetDescriptor());
1046     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1047     data.WriteString(sinkName);
1048 
1049     error = Remote()->SendRequest(
1050         static_cast<uint32_t>(AudioServerInterfaceCode::RESTORE_RENDERSINK), data, reply, option);
1051     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1052 
1053     return reply.ReadInt32();
1054 }
1055 
LoadHdiEffectModel()1056 void AudioManagerProxy::LoadHdiEffectModel()
1057 {
1058     MessageParcel data;
1059     MessageParcel reply;
1060     MessageOption option;
1061 
1062     bool ret = data.WriteInterfaceToken(GetDescriptor());
1063     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1064 
1065     int32_t error = Remote()->SendRequest(
1066         static_cast<uint32_t>(AudioServerInterfaceCode::LOAD_HDI_EFFECT_MODEL), data, reply, option);
1067     CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed,error:%d", error);
1068 }
1069 
UpdateEffectBtOffloadSupported(const bool & isSupported)1070 void AudioManagerProxy::UpdateEffectBtOffloadSupported(const bool &isSupported)
1071 {
1072     MessageParcel data;
1073     MessageParcel reply;
1074     MessageOption option;
1075 
1076     bool ret = data.WriteInterfaceToken(GetDescriptor());
1077     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1078     data.WriteBool(isSupported);
1079 
1080     int32_t error = Remote()->SendRequest(
1081         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_EFFECT_BT_OFFLOAD_SUPPORTED), data, reply, option);
1082     CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error);
1083 }
1084 
SetSinkMuteForSwitchDevice(const std::string & devceClass,int32_t durationUs,bool mute)1085 int32_t AudioManagerProxy::SetSinkMuteForSwitchDevice(const std::string &devceClass, int32_t durationUs, bool mute)
1086 {
1087     MessageParcel data;
1088     MessageParcel reply;
1089     MessageOption option;
1090 
1091     bool ret = data.WriteInterfaceToken(GetDescriptor());
1092     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1093     data.WriteString(devceClass);
1094     data.WriteInt32(durationUs);
1095     data.WriteInt32(mute);
1096 
1097     int32_t error = Remote()->SendRequest(
1098         static_cast<uint32_t>(AudioServerInterfaceCode::SET_SINK_MUTE_FOR_SWITCH_DEVICE), data, reply, option);
1099     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "failed, error:%{public}d", error);
1100     return reply.ReadInt32();
1101 }
1102 
SetRotationToEffect(const uint32_t rotate)1103 void AudioManagerProxy::SetRotationToEffect(const uint32_t rotate)
1104 {
1105     MessageParcel data;
1106     MessageParcel reply;
1107     MessageOption option;
1108 
1109     bool ret = data.WriteInterfaceToken(GetDescriptor());
1110     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1111     data.WriteUint32(rotate);
1112 
1113     int32_t error = Remote()->SendRequest(
1114         static_cast<uint32_t>(AudioServerInterfaceCode::SET_ROTATION_TO_EFFECT), data, reply, option);
1115     CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error);
1116 }
1117 
UpdateSessionConnectionState(const int32_t & sessionID,const int32_t & state)1118 void AudioManagerProxy::UpdateSessionConnectionState(const int32_t &sessionID, const int32_t &state)
1119 {
1120     MessageParcel data;
1121     MessageParcel reply;
1122     MessageOption option;
1123 
1124     bool ret = data.WriteInterfaceToken(GetDescriptor());
1125     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1126     data.WriteInt32(sessionID);
1127     data.WriteInt32(state);
1128 
1129     int32_t error = Remote()->SendRequest(
1130         static_cast<uint32_t>(AudioServerInterfaceCode::UPDATE_SESSION_CONNECTION_STATE), data, reply, option);
1131     CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error);
1132 }
1133 
SetNonInterruptMute(const uint32_t sessionId,const bool muteFlag)1134 void AudioManagerProxy::SetNonInterruptMute(const uint32_t sessionId, const bool muteFlag)
1135 {
1136     MessageParcel data;
1137     MessageParcel reply;
1138     MessageOption option;
1139 
1140     bool ret = data.WriteInterfaceToken(GetDescriptor());
1141     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
1142     data.WriteUint32(sessionId);
1143     data.WriteBool(muteFlag);
1144 
1145     int32_t error = Remote()->SendRequest(
1146         static_cast<uint32_t>(AudioServerInterfaceCode::SET_SINGLE_STREAM_MUTE), data, reply, option);
1147     CHECK_AND_RETURN_LOG(error == ERR_NONE, "failed, error:%{public}d", error);
1148 }
1149 
CreateIpcOfflineStream(int32_t & errorCode)1150 sptr<IRemoteObject> AudioManagerProxy::CreateIpcOfflineStream(int32_t &errorCode)
1151 {
1152     MessageParcel data;
1153     MessageParcel reply;
1154     MessageOption option;
1155 
1156     bool ret = data.WriteInterfaceToken(GetDescriptor());
1157     CHECK_AND_RETURN_RET_LOG(ret, nullptr, "WriteInterfaceToken failed");
1158     int error = Remote()->SendRequest(
1159         static_cast<uint32_t>(AudioServerInterfaceCode::CREATE_IPC_OFFLINE_STREAM), data, reply, option);
1160     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, nullptr, "CreateIpcOfflineStream failed, error: %{public}d", error);
1161     sptr<IRemoteObject> process = reply.ReadRemoteObject();
1162     errorCode = reply.ReadInt32();
1163     return process;
1164 }
1165 
GetOfflineAudioEffectChains(vector<string> & effectChains)1166 int32_t AudioManagerProxy::GetOfflineAudioEffectChains(vector<string> &effectChains)
1167 {
1168     MessageParcel data;
1169     MessageParcel reply;
1170     MessageOption option;
1171 
1172     bool ret = data.WriteInterfaceToken(GetDescriptor());
1173     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_ERR, "WriteInterfaceToken failed");
1174     int error = Remote()->SendRequest(
1175         static_cast<uint32_t>(AudioServerInterfaceCode::GET_OFFLINE_AUDIO_EFFECT_CHAINS), data, reply, option);
1176     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "GetOfflineAudioEffectChains failed, error: %{public}d", error);
1177     int32_t vecSize = reply.ReadInt32();
1178     CHECK_AND_RETURN_RET_LOG(vecSize >= 0 && vecSize <= MAX_OFFLINE_EFFECT_CHAIN_NUM, AUDIO_ERR,
1179         "invalid offline effect chain num:%{public}d", vecSize);
1180     for (int i = 0; i < vecSize; i++) {
1181         effectChains.emplace_back(reply.ReadString());
1182     }
1183     return reply.ReadInt32();
1184 }
1185 } // namespace AudioStandard
1186 } // namespace OHOS
1187