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 "AudioPolicyProxy"
17 #endif
18 
19 
20 #include "audio_policy_log.h"
21 #include "audio_policy_proxy.h"
22 
23 
24 namespace {
25 constexpr int MAX_PID_COUNT = 1000;
26 }
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 
AudioPolicyProxy(const sptr<IRemoteObject> & impl)32 AudioPolicyProxy::AudioPolicyProxy(const sptr<IRemoteObject> &impl)
33     : IRemoteProxy<IAudioPolicy>(impl)
34 {
35 }
36 
WriteStreamChangeInfo(MessageParcel & data,const AudioMode & mode,const AudioStreamChangeInfo & streamChangeInfo)37 void AudioPolicyProxy::WriteStreamChangeInfo(MessageParcel &data,
38     const AudioMode &mode, const AudioStreamChangeInfo &streamChangeInfo)
39 {
40     if (mode == AUDIO_MODE_PLAYBACK) {
41         streamChangeInfo.audioRendererChangeInfo.Marshalling(data);
42     } else {
43         streamChangeInfo.audioCapturerChangeInfo.Marshalling(data);
44     }
45 }
46 
SetRingerModeLegacy(AudioRingerMode ringMode)47 int32_t AudioPolicyProxy::SetRingerModeLegacy(AudioRingerMode ringMode)
48 {
49     MessageParcel data;
50     MessageParcel reply;
51     MessageOption option;
52 
53     bool ret = data.WriteInterfaceToken(GetDescriptor());
54     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
55     data.WriteInt32(static_cast<int>(ringMode));
56     int32_t error = Remote()->SendRequest(
57         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_RINGER_MODE_LEGACY), data, reply, option);
58     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set SetRingerModeLegacy failed, error: %d", error);
59 
60     return reply.ReadInt32();
61 }
62 
SetRingerMode(AudioRingerMode ringMode)63 int32_t AudioPolicyProxy::SetRingerMode(AudioRingerMode ringMode)
64 {
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option;
68 
69     bool ret = data.WriteInterfaceToken(GetDescriptor());
70     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
71     data.WriteInt32(static_cast<int>(ringMode));
72     int32_t error = Remote()->SendRequest(
73         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_RINGER_MODE), data, reply, option);
74     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set ringermode failed, error: %d", error);
75 
76     return reply.ReadInt32();
77 }
78 
79 #ifdef FEATURE_DTMF_TONE
GetSupportedTones()80 std::vector<int32_t> AudioPolicyProxy::GetSupportedTones()
81 {
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85     int32_t lListSize = 0;
86     AUDIO_DEBUG_LOG("get GetSupportedTones,");
87     std::vector<int> lSupportedToneList = {};
88     bool ret = data.WriteInterfaceToken(GetDescriptor());
89     CHECK_AND_RETURN_RET_LOG(ret, lSupportedToneList, "WriteInterfaceToken failed");
90     int32_t error = Remote()->SendRequest(
91         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SUPPORTED_TONES), data, reply, option);
92     if (error != ERR_NONE) {
93         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
94     }
95     lListSize = reply.ReadInt32();
96     for (int i = 0; i < lListSize; i++) {
97         lSupportedToneList.push_back(reply.ReadInt32());
98     }
99     AUDIO_DEBUG_LOG("get GetSupportedTones, %{public}d", lListSize);
100     return lSupportedToneList;
101 }
102 
GetToneConfig(int32_t ltonetype)103 std::shared_ptr<ToneInfo> AudioPolicyProxy::GetToneConfig(int32_t ltonetype)
104 {
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option;
108     std::shared_ptr<ToneInfo> spToneInfo =  std::make_shared<ToneInfo>();
109     if (spToneInfo == nullptr) {
110         return nullptr;
111     }
112     bool ret = data.WriteInterfaceToken(GetDescriptor());
113     CHECK_AND_RETURN_RET_LOG(ret, spToneInfo, "WriteInterfaceToken failed");
114     data.WriteInt32(ltonetype);
115     int32_t error = Remote()->SendRequest(
116         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_TONEINFO), data, reply, option);
117     if (error != ERR_NONE) {
118         AUDIO_ERR_LOG("get toneinfo failed, error: %d", error);
119     }
120 
121     spToneInfo->Unmarshalling(reply);
122     AUDIO_DEBUG_LOG("get rGetToneConfig returned,");
123     return spToneInfo;
124 }
125 #endif
126 
GetPersistentMicMuteState()127 bool AudioPolicyProxy::GetPersistentMicMuteState()
128 {
129     MessageParcel data;
130     MessageParcel reply;
131     MessageOption option;
132     bool ret = data.WriteInterfaceToken(GetDescriptor());
133     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
134     int32_t error = Remote()->SendRequest(
135         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MICROPHONE_MUTE_PERSISTENT), data, reply, option);
136     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get persistent microphoneMute state failed, error: %d", error);
137 
138     return reply.ReadBool();
139 }
140 
GetRingerMode()141 AudioRingerMode AudioPolicyProxy::GetRingerMode()
142 {
143     MessageParcel data;
144     MessageParcel reply;
145     MessageOption option;
146 
147     bool ret = data.WriteInterfaceToken(GetDescriptor());
148     CHECK_AND_RETURN_RET_LOG(ret, RINGER_MODE_NORMAL, "WriteInterfaceToken failed");
149     int32_t error = Remote()->SendRequest(
150         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_RINGER_MODE), data, reply, option);
151     if (error != ERR_NONE) {
152         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
153     }
154     return static_cast<AudioRingerMode>(reply.ReadInt32());
155 }
156 
SetAudioScene(AudioScene scene)157 int32_t AudioPolicyProxy::SetAudioScene(AudioScene scene)
158 {
159     MessageParcel data;
160     MessageParcel reply;
161     MessageOption option;
162 
163     bool ret = data.WriteInterfaceToken(GetDescriptor());
164     CHECK_AND_RETURN_RET_LOG(ret, RINGER_MODE_NORMAL, "WriteInterfaceToken failed");
165     data.WriteInt32(static_cast<int>(scene));
166     int32_t error = Remote()->SendRequest(
167         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_AUDIO_SCENE), data, reply, option);
168     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set audio scene failed, error: %d", error);
169 
170     return reply.ReadInt32();
171 }
172 
GetAudioScene()173 AudioScene AudioPolicyProxy::GetAudioScene()
174 {
175     MessageParcel data;
176     MessageParcel reply;
177     MessageOption option;
178 
179     bool ret = data.WriteInterfaceToken(GetDescriptor());
180     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_SCENE_DEFAULT, "WriteInterfaceToken failed");
181     int32_t error = Remote()->SendRequest(
182         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_SCENE), data, reply, option);
183     if (error != ERR_NONE) {
184         AUDIO_ERR_LOG("get audio scene failed, error: %d", error);
185     }
186     return static_cast<AudioScene>(reply.ReadInt32());
187 }
188 
SetStreamMuteLegacy(AudioVolumeType volumeType,bool mute)189 int32_t AudioPolicyProxy::SetStreamMuteLegacy(AudioVolumeType volumeType, bool mute)
190 {
191     MessageParcel data;
192     MessageParcel reply;
193     MessageOption option;
194 
195     bool ret = data.WriteInterfaceToken(GetDescriptor());
196     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
197     data.WriteInt32(static_cast<int32_t>(volumeType));
198     data.WriteBool(mute);
199     int32_t error = Remote()->SendRequest(
200         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_STREAM_MUTE_LEGACY), data, reply, option);
201     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set mute failed, error: %d", error);
202     return reply.ReadInt32();
203 }
204 
SetStreamMute(AudioVolumeType volumeType,bool mute)205 int32_t AudioPolicyProxy::SetStreamMute(AudioVolumeType volumeType, bool mute)
206 {
207     MessageParcel data;
208     MessageParcel reply;
209     MessageOption option;
210 
211     bool ret = data.WriteInterfaceToken(GetDescriptor());
212     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
213     data.WriteInt32(static_cast<int32_t>(volumeType));
214     data.WriteBool(mute);
215     int32_t error = Remote()->SendRequest(
216         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_STREAM_MUTE), data, reply, option);
217     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set mute failed, error: %d", error);
218     return reply.ReadInt32();
219 }
220 
GetStreamMute(AudioVolumeType volumeType)221 bool AudioPolicyProxy::GetStreamMute(AudioVolumeType volumeType)
222 {
223     MessageParcel data;
224     MessageParcel reply;
225     MessageOption option;
226 
227     bool ret = data.WriteInterfaceToken(GetDescriptor());
228     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
229     data.WriteInt32(static_cast<int32_t>(volumeType));
230     int32_t error = Remote()->SendRequest(
231         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_STREAM_MUTE), data, reply, option);
232     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "get mute failed, error: %d", error);
233     return reply.ReadBool();
234 }
235 
IsStreamActive(AudioVolumeType volumeType)236 bool AudioPolicyProxy::IsStreamActive(AudioVolumeType volumeType)
237 {
238     MessageParcel data;
239     MessageParcel reply;
240     MessageOption option;
241 
242     bool ret = data.WriteInterfaceToken(GetDescriptor());
243     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
244     data.WriteInt32(static_cast<int32_t>(volumeType));
245     int32_t error = Remote()->SendRequest(
246         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_STREAM_ACTIVE), data, reply, option);
247     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "isStreamActive failed, error: %d", error);
248     return reply.ReadBool();
249 }
250 
GetDevices(DeviceFlag deviceFlag)251 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetDevices(DeviceFlag deviceFlag)
252 {
253     MessageParcel data;
254     MessageParcel reply;
255     MessageOption option;
256     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
257 
258     bool ret = data.WriteInterfaceToken(GetDescriptor());
259     CHECK_AND_RETURN_RET_LOG(ret, deviceInfo, "WriteInterfaceToken failed");
260     data.WriteInt32(static_cast<int32_t>(deviceFlag));
261     int32_t error = Remote()->SendRequest(
262         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_DEVICES), data, reply, option);
263     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, deviceInfo, "Get devices failed, error: %d", error);
264 
265     int32_t size = reply.ReadInt32();
266     for (int32_t i = 0; i < size; i++) {
267         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
268     }
269 
270     return deviceInfo;
271 }
272 
GetDevicesInner(DeviceFlag deviceFlag)273 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetDevicesInner(DeviceFlag deviceFlag)
274 {
275     MessageParcel data;
276     MessageParcel reply;
277     MessageOption option;
278     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
279 
280     bool ret = data.WriteInterfaceToken(GetDescriptor());
281     CHECK_AND_RETURN_RET_LOG(ret, deviceInfo, "WriteInterfaceToken failed");
282     data.WriteInt32(static_cast<int32_t>(deviceFlag));
283     int32_t error = Remote()->SendRequest(
284         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_DEVICES_INNER), data, reply, option);
285     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, deviceInfo, "Get devices failed, error: %d", error);
286 
287     int32_t size = reply.ReadInt32();
288     for (int32_t i = 0; i < size; i++) {
289         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
290     }
291 
292     return deviceInfo;
293 }
294 
GetPreferredOutputDeviceDescriptors(AudioRendererInfo & rendererInfo)295 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetPreferredOutputDeviceDescriptors(
296     AudioRendererInfo &rendererInfo)
297 {
298     MessageParcel data;
299     MessageParcel reply;
300     MessageOption option;
301     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
302 
303     bool ret = data.WriteInterfaceToken(GetDescriptor());
304     CHECK_AND_RETURN_RET_LOG(ret, deviceInfo, "WriteInterfaceToken failed");
305 
306     bool res = rendererInfo.Marshalling(data);
307     CHECK_AND_RETURN_RET_LOG(res, deviceInfo, "AudioRendererInfo Marshalling() failed");
308 
309     int32_t error = Remote()->SendRequest(
310         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_OUTPUT_DEVICE_DESCRIPTORS), data, reply, option);
311     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, deviceInfo, "Get out devices failed, error: %d", error);
312 
313     int32_t size = reply.ReadInt32();
314     for (int32_t i = 0; i < size; i++) {
315         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
316     }
317 
318     return deviceInfo;
319 }
320 
GetPreferredInputDeviceDescriptors(AudioCapturerInfo & captureInfo)321 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetPreferredInputDeviceDescriptors(
322     AudioCapturerInfo &captureInfo)
323 {
324     MessageParcel data;
325     MessageParcel reply;
326     MessageOption option;
327     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
328 
329     bool ret = data.WriteInterfaceToken(GetDescriptor());
330     CHECK_AND_RETURN_RET_LOG(ret, deviceInfo, "WriteInterfaceToken failed");
331 
332     bool res = captureInfo.Marshalling(data);
333     CHECK_AND_RETURN_RET_LOG(res, deviceInfo, "AudioCapturerInfo Marshalling() failed");
334 
335     int32_t error = Remote()->SendRequest(
336         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_PREFERRED_INTPUT_DEVICE_DESCRIPTORS), data, reply, option);
337     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, deviceInfo, "Get preferred input devices failed, error: %d", error);
338 
339     int32_t size = reply.ReadInt32();
340     for (int32_t i = 0; i < size; i++) {
341         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
342     }
343 
344     return deviceInfo;
345 }
346 
SetDeviceActive(InternalDeviceType deviceType,bool active)347 int32_t AudioPolicyProxy::SetDeviceActive(InternalDeviceType deviceType, bool active)
348 {
349     MessageParcel data;
350     MessageParcel reply;
351     MessageOption option;
352 
353     bool ret = data.WriteInterfaceToken(GetDescriptor());
354     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
355     data.WriteInt32(static_cast<int32_t>(deviceType));
356     data.WriteBool(active);
357     int32_t error = Remote()->SendRequest(
358         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DEVICE_ACTIVE), data, reply, option);
359     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set device active failed, error: %d", error);
360     return reply.ReadInt32();
361 }
362 
LoadSplitModule(const std::string & splitArgs,const std::string & networkId)363 int32_t AudioPolicyProxy::LoadSplitModule(const std::string &splitArgs, const std::string &networkId)
364 {
365     MessageParcel data;
366     MessageParcel reply;
367     MessageOption option;
368 
369     bool ret = data.WriteInterfaceToken(GetDescriptor());
370     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
371     data.WriteString(splitArgs);
372     data.WriteString(networkId);
373     int32_t error = Remote()->SendRequest(
374         static_cast<uint32_t>(AudioPolicyInterfaceCode::LOAD_SPLIT_MODULE), data, reply, option);
375     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "load split module failed, error: %{public}d", error);
376     return reply.ReadInt32();
377 }
378 
SetVoiceRingtoneMute(bool isMute)379 int32_t AudioPolicyProxy::SetVoiceRingtoneMute(bool isMute)
380 {
381     MessageParcel data;
382     MessageParcel reply;
383     MessageOption option;
384 
385     bool ret = data.WriteInterfaceToken(GetDescriptor());
386     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
387     data.WriteBool(isMute);
388     int32_t error = Remote()->SendRequest(
389         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_VOICE_RINGTONE_MUTE), data, reply, option);
390     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SetVoiceRingtoneMute failed, error: %{public}d", error);
391     return reply.ReadInt32();
392 }
393 
IsDeviceActive(InternalDeviceType deviceType)394 bool AudioPolicyProxy::IsDeviceActive(InternalDeviceType deviceType)
395 {
396     MessageParcel data;
397     MessageParcel reply;
398     MessageOption option;
399 
400     bool ret = data.WriteInterfaceToken(GetDescriptor());
401     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
402     data.WriteInt32(static_cast<int32_t>(deviceType));
403     int32_t error = Remote()->SendRequest(
404         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_DEVICE_ACTIVE), data, reply, option);
405     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "is device active failed, error: %d", error);
406     return reply.ReadBool();
407 }
408 
GetActiveOutputDevice()409 DeviceType AudioPolicyProxy::GetActiveOutputDevice()
410 {
411     MessageParcel data;
412     MessageParcel reply;
413     MessageOption option;
414 
415     bool ret = data.WriteInterfaceToken(GetDescriptor());
416     CHECK_AND_RETURN_RET_LOG(ret, DEVICE_TYPE_INVALID, "WriteInterfaceToken failed");
417 
418     int32_t error = Remote()->SendRequest(
419         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_OUTPUT_DEVICE), data, reply, option);
420     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, DEVICE_TYPE_INVALID,
421         "get active output device failed, error: %d", error);
422 
423     return static_cast<DeviceType>(reply.ReadInt32());
424 }
425 
GetActiveInputDevice()426 DeviceType AudioPolicyProxy::GetActiveInputDevice()
427 {
428     MessageParcel data;
429     MessageParcel reply;
430     MessageOption option;
431 
432     bool ret = data.WriteInterfaceToken(GetDescriptor());
433     CHECK_AND_RETURN_RET_LOG(ret, DEVICE_TYPE_INVALID, "WriteInterfaceToken failed");
434 
435     int32_t error = Remote()->SendRequest(
436         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_INPUT_DEVICE), data, reply, option);
437     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, DEVICE_TYPE_INVALID,
438         "get active input device failed, error: %d", error);
439 
440     return static_cast<DeviceType>(reply.ReadInt32());
441 }
442 
SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)443 int32_t AudioPolicyProxy::SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
444     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
445 {
446     MessageParcel data;
447     MessageParcel reply;
448     MessageOption option;
449 
450     bool ret = data.WriteInterfaceToken(GetDescriptor());
451     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
452     bool tmp = audioRendererFilter->Marshalling(data);
453     CHECK_AND_RETURN_RET_LOG(tmp, -1, "AudioRendererFilter Marshalling() failed");
454 
455     uint32_t size = audioDeviceDescriptors.size();
456     uint32_t validSize = 20; // Use 20 as limit.
457     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= validSize, -1, "SelectOutputDevice get invalid device size.");
458     data.WriteInt32(size);
459     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
460         bool audioDeviceTmp = audioDeviceDescriptor->Marshalling(data);
461         CHECK_AND_RETURN_RET_LOG(audioDeviceTmp, -1, "AudioDeviceDescriptor Marshalling() failed");
462     }
463     int error = Remote()->SendRequest(
464         static_cast<uint32_t>(AudioPolicyInterfaceCode::SELECT_OUTPUT_DEVICE), data, reply, option);
465     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SelectOutputDevice failed, error: %{public}d", error);
466 
467     return reply.ReadInt32();
468 }
GetSelectedDeviceInfo(int32_t uid,int32_t pid,AudioStreamType streamType)469 std::string AudioPolicyProxy::GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType)
470 {
471     MessageParcel data;
472     MessageParcel reply;
473     MessageOption option;
474 
475     bool ret = data.WriteInterfaceToken(GetDescriptor());
476     CHECK_AND_RETURN_RET_LOG(ret, "", "WriteInterfaceToken failed");
477     data.WriteInt32(uid);
478     data.WriteInt32(pid);
479     data.WriteInt32(static_cast<int32_t>(streamType));
480     int error = Remote()->SendRequest(
481         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SELECTED_DEVICE_INFO), data, reply, option);
482     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, "", "GetSelectedDeviceInfo failed, error: %{public}d", error);
483 
484     return reply.ReadString();
485 }
486 
SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)487 int32_t AudioPolicyProxy::SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
488     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
489 {
490     MessageParcel data;
491     MessageParcel reply;
492     MessageOption option;
493 
494     bool ret = data.WriteInterfaceToken(GetDescriptor());
495     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
496     bool tmp = audioCapturerFilter->Marshalling(data);
497     CHECK_AND_RETURN_RET_LOG(tmp, -1, "AudioCapturerFilter Marshalling() failed");
498 
499     uint32_t size = audioDeviceDescriptors.size();
500     uint32_t validSize = 20; // Use 20 as limit.
501     CHECK_AND_RETURN_RET_LOG(size > 0 && size <= validSize, -1, "SelectOutputDevice get invalid device size.");
502     data.WriteInt32(size);
503     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
504         bool audioDeviceTmp = audioDeviceDescriptor->Marshalling(data);
505         CHECK_AND_RETURN_RET_LOG(audioDeviceTmp, -1, "AudioDeviceDescriptor Marshalling() failed");
506     }
507     int error = Remote()->SendRequest(
508         static_cast<uint32_t>(AudioPolicyInterfaceCode::SELECT_INPUT_DEVICE), data, reply, option);
509     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SelectInputDevice failed, error: %{public}d", error);
510 
511     return reply.ReadInt32();
512 }
513 
ConfigDistributedRoutingRole(const sptr<AudioDeviceDescriptor> descriptor,CastType type)514 int32_t AudioPolicyProxy::ConfigDistributedRoutingRole(const sptr<AudioDeviceDescriptor> descriptor, CastType type)
515 {
516     MessageParcel data;
517     MessageParcel reply;
518     MessageOption option;
519 
520     bool ret = data.WriteInterfaceToken(GetDescriptor());
521     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
522     CHECK_AND_RETURN_RET_LOG(descriptor != nullptr, -1, "descriptor is null");
523     bool res = descriptor->Marshalling(data);
524     CHECK_AND_RETURN_RET_LOG(res, -1, "AudioDeviceDescriptor marshalling failed");
525     data.WriteInt32(static_cast<int32_t>(type));
526     int error = Remote()->SendRequest(
527         static_cast<uint32_t>(AudioPolicyInterfaceCode::CONFIG_DISTRIBUTED_ROUTING_ROLE), data, reply, option);
528     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "failed error : %{public}d", error);
529     return reply.ReadInt32();
530 }
531 
ActivateAudioSession(const AudioSessionStrategy & strategy)532 int32_t AudioPolicyProxy::ActivateAudioSession(const AudioSessionStrategy &strategy)
533 {
534     MessageParcel data;
535     MessageParcel reply;
536     MessageOption option;
537 
538     bool ret = data.WriteInterfaceToken(GetDescriptor());
539     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
540     data.WriteInt32(static_cast<int32_t>(strategy.concurrencyMode));
541     int32_t error = Remote()->SendRequest(
542         static_cast<uint32_t>(AudioPolicyInterfaceCode::ACTIVATE_AUDIO_SESSION), data, reply, option);
543     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "Failed to activate audio session. Error: %{public}d", error);
544 
545     return reply.ReadInt32();
546 }
547 
DeactivateAudioSession()548 int32_t AudioPolicyProxy::DeactivateAudioSession()
549 {
550     MessageParcel data;
551     MessageParcel reply;
552     MessageOption option;
553 
554     bool ret = data.WriteInterfaceToken(GetDescriptor());
555     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
556     int error = Remote()->SendRequest(
557         static_cast<uint32_t>(AudioPolicyInterfaceCode::DEACTIVATE_AUDIO_SESSION), data, reply, option);
558     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "Failed to deactivate audio session. Error: %{public}d", error);
559 
560     return reply.ReadInt32();
561 }
562 
IsAudioSessionActivated()563 bool AudioPolicyProxy::IsAudioSessionActivated()
564 {
565     MessageParcel data;
566     MessageParcel reply;
567     MessageOption option;
568 
569     bool ret = data.WriteInterfaceToken(GetDescriptor());
570     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
571     int error = Remote()->SendRequest(
572         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_AUDIO_SESSION_ACTIVATED), data, reply, option);
573     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false,
574         "Failed to query IsAudioSessionActivated, error: %{public}d", error);
575 
576     return reply.ReadBool();
577 }
578 
ReadAudioFocusInfo(MessageParcel & reply,std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)579 void AudioPolicyProxy::ReadAudioFocusInfo(MessageParcel &reply,
580     std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
581 {
582     std::pair<AudioInterrupt, AudioFocuState> focusInfo;
583     AudioInterrupt::Unmarshalling(reply, focusInfo.first);
584     focusInfo.second = static_cast<AudioFocuState>(reply.ReadInt32());
585     focusInfoList.push_back(focusInfo);
586 }
587 
GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList,const int32_t zoneID)588 int32_t AudioPolicyProxy::GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList,
589     const int32_t zoneID)
590 {
591     MessageParcel data;
592     MessageParcel reply;
593     MessageOption option;
594 
595     bool res = data.WriteInterfaceToken(GetDescriptor());
596     CHECK_AND_RETURN_RET_LOG(res, ERROR, "WriteInterfaceToken failed");
597     data.WriteInt32(zoneID);
598     int32_t error = Remote()->SendRequest(
599         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_FOCUS_INFO_LIST), data, reply, option);
600     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "GetAudioFocusInfoList, error: %d", error);
601     int32_t ret = reply.ReadInt32();
602     int32_t size = reply.ReadInt32();
603     focusInfoList = {};
604     if (ret < 0) {
605         return ret;
606     } else {
607         for (int32_t i = 0; i < size; i++) {
608             ReadAudioFocusInfo(reply, focusInfoList);
609         }
610         return SUCCESS;
611     }
612 }
613 
ActivateAudioInterrupt(const AudioInterrupt & audioInterrupt,const int32_t zoneID,const bool isUpdatedAudioStrategy)614 int32_t AudioPolicyProxy::ActivateAudioInterrupt(
615     const AudioInterrupt &audioInterrupt, const int32_t zoneID, const bool isUpdatedAudioStrategy)
616 {
617     MessageParcel data;
618     MessageParcel reply;
619     MessageOption option;
620 
621     bool ret = data.WriteInterfaceToken(GetDescriptor());
622     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
623     data.WriteInt32(zoneID);
624     data.WriteBool(isUpdatedAudioStrategy);
625     AudioInterrupt::Marshalling(data, audioInterrupt);
626     int error = Remote()->SendRequest(
627         static_cast<uint32_t>(AudioPolicyInterfaceCode::ACTIVATE_INTERRUPT), data, reply, option);
628     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "activate interrupt failed, error: %{public}d", error);
629 
630     return reply.ReadInt32();
631 }
632 
DeactivateAudioInterrupt(const AudioInterrupt & audioInterrupt,const int32_t zoneID)633 int32_t AudioPolicyProxy::DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt, const int32_t zoneID)
634 {
635     MessageParcel data;
636     MessageParcel reply;
637     MessageOption option;
638 
639     bool ret = data.WriteInterfaceToken(GetDescriptor());
640     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
641     data.WriteInt32(zoneID);
642     AudioInterrupt::Marshalling(data, audioInterrupt);
643     int error = Remote()->SendRequest(
644         static_cast<uint32_t>(AudioPolicyInterfaceCode::DEACTIVATE_INTERRUPT), data, reply, option);
645     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "deactivate interrupt failed, error: %{public}d", error);
646 
647     return reply.ReadInt32();
648 }
649 
RequestAudioFocus(const int32_t clientId,const AudioInterrupt & audioInterrupt)650 int32_t AudioPolicyProxy::RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt)
651 {
652     MessageParcel data;
653     MessageParcel reply;
654     MessageOption option;
655 
656     bool ret = data.WriteInterfaceToken(GetDescriptor());
657     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
658 
659     data.WriteInt32(clientId);
660     AudioInterrupt::Marshalling(data, audioInterrupt);
661 
662     int error = Remote()->SendRequest(
663         static_cast<uint32_t>(AudioPolicyInterfaceCode::REQUEST_AUDIO_FOCUS), data, reply, option);
664     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "activate interrupt failed, error: %{public}d", error);
665 
666     return reply.ReadInt32();
667 }
668 
AbandonAudioFocus(const int32_t clientId,const AudioInterrupt & audioInterrupt)669 int32_t AudioPolicyProxy::AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt)
670 {
671     MessageParcel data;
672     MessageParcel reply;
673     MessageOption option;
674 
675     bool ret = data.WriteInterfaceToken(GetDescriptor());
676     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
677     data.WriteInt32(clientId);
678     AudioInterrupt::Marshalling(data, audioInterrupt);
679 
680     int error = Remote()->SendRequest(
681         static_cast<uint32_t>(AudioPolicyInterfaceCode::ABANDON_AUDIO_FOCUS), data, reply, option);
682     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "deactivate interrupt failed, error: %{public}d", error);
683 
684     return reply.ReadInt32();
685 }
686 
GetStreamInFocus(const int32_t zoneID)687 AudioStreamType AudioPolicyProxy::GetStreamInFocus(const int32_t zoneID)
688 {
689     MessageParcel data;
690     MessageParcel reply;
691     MessageOption option;
692 
693     bool ret = data.WriteInterfaceToken(GetDescriptor());
694     CHECK_AND_RETURN_RET_LOG(ret, STREAM_DEFAULT, "WriteInterfaceToken failed");
695     data.WriteInt32(zoneID);
696     int32_t error = Remote()->SendRequest(
697         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_STREAM_IN_FOCUS), data, reply, option);
698     if (error != ERR_NONE) {
699         AUDIO_ERR_LOG("get stream in focus failed, error: %d", error);
700     }
701     return static_cast<AudioStreamType>(reply.ReadInt32());
702 }
703 
GetSessionInfoInFocus(AudioInterrupt & audioInterrupt,const int32_t zoneID)704 int32_t AudioPolicyProxy::GetSessionInfoInFocus(AudioInterrupt &audioInterrupt, const int32_t zoneID)
705 {
706     MessageParcel data;
707     MessageParcel reply;
708     MessageOption option;
709 
710     bool ret = data.WriteInterfaceToken(GetDescriptor());
711     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
712     data.WriteInt32(zoneID);
713     int32_t error = Remote()->SendRequest(
714         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SESSION_INFO_IN_FOCUS), data, reply, option);
715     if (error != ERR_NONE) {
716         AUDIO_ERR_LOG("AudioPolicyProxy::GetSessionInfoInFocus failed, error: %d", error);
717     }
718     AudioInterrupt::Unmarshalling(reply, audioInterrupt);
719 
720     return reply.ReadInt32();
721 }
722 
CheckRecordingCreate(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid,SourceType sourceType)723 bool AudioPolicyProxy::CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
724     SourceType sourceType)
725 {
726     AUDIO_DEBUG_LOG("CheckRecordingCreate: [uid : %{public}d]", appUid);
727     MessageParcel data;
728     MessageParcel reply;
729     MessageOption option;
730 
731     bool ret = data.WriteInterfaceToken(GetDescriptor());
732     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
733     data.WriteUint32(appTokenId);
734     data.WriteUint64(appFullTokenId);
735     data.WriteInt32(appUid);
736     data.WriteInt32(sourceType);
737 
738     int result = Remote()->SendRequest(
739         static_cast<uint32_t>(AudioPolicyInterfaceCode::QUERY_MICROPHONE_PERMISSION), data, reply, option);
740     CHECK_AND_RETURN_RET_LOG(result == ERR_NONE, false, "CheckRecordingCreate failed, result: %{public}d", result);
741 
742     return reply.ReadBool();
743 }
744 
CheckRecordingStateChange(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid,AudioPermissionState state)745 bool AudioPolicyProxy::CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
746     AudioPermissionState state)
747 {
748     MessageParcel data;
749     MessageParcel reply;
750     MessageOption option;
751 
752     bool ret = data.WriteInterfaceToken(GetDescriptor());
753     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
754 
755     data.WriteUint32(appTokenId);
756     data.WriteUint64(appFullTokenId);
757     data.WriteInt32(appUid);
758     data.WriteInt32(state);
759 
760     int result = Remote()->SendRequest(
761         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_USING_PEMISSION_FROM_PRIVACY), data, reply, option);
762     CHECK_AND_RETURN_RET_LOG(result == ERR_NONE, false, "CheckRecordingStateChange failed, result: %{public}d", result);
763 
764     return reply.ReadBool();
765 }
766 
ReconfigureAudioChannel(const uint32_t & count,DeviceType deviceType)767 int32_t AudioPolicyProxy::ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType)
768 {
769     MessageParcel data;
770     MessageParcel reply;
771     MessageOption option;
772 
773     bool ret = data.WriteInterfaceToken(GetDescriptor());
774     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
775 
776     data.WriteUint32(count);
777     data.WriteInt32(deviceType);
778 
779     int result = Remote()->SendRequest(
780         static_cast<uint32_t>(AudioPolicyInterfaceCode::RECONFIGURE_CHANNEL), data, reply, option);
781     CHECK_AND_RETURN_RET_LOG(result == ERR_NONE, ERR_TRANSACTION_FAILED,
782         "ReconfigureAudioChannel failed, result: %{public}d", result);
783 
784     return reply.ReadInt32();
785 }
786 
GetAudioLatencyFromXml()787 int32_t AudioPolicyProxy::GetAudioLatencyFromXml()
788 {
789     MessageParcel data;
790     MessageParcel reply;
791     MessageOption option;
792 
793     bool ret = data.WriteInterfaceToken(GetDescriptor());
794     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
795 
796     int32_t error = Remote()->SendRequest(
797         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_LATENCY), data, reply, option);
798     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERR_TRANSACTION_FAILED,
799         "GetAudioLatencyFromXml, error: %d", error);
800 
801     return reply.ReadInt32();
802 }
803 
GetSinkLatencyFromXml()804 uint32_t AudioPolicyProxy::GetSinkLatencyFromXml()
805 {
806     MessageParcel data;
807     MessageParcel reply;
808     MessageOption option;
809 
810     bool ret = data.WriteInterfaceToken(GetDescriptor());
811     CHECK_AND_RETURN_RET_LOG(ret, 0, "WriteInterfaceToken failed");
812 
813     int32_t error = Remote()->SendRequest(
814         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SINK_LATENCY), data, reply, option);
815     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, 0, "GetSinkLatencyFromXml, error: %d", error);
816 
817     return reply.ReadUint32();
818 }
819 
GetPreferredOutputStreamType(AudioRendererInfo & rendererInfo)820 int32_t AudioPolicyProxy::GetPreferredOutputStreamType(AudioRendererInfo &rendererInfo)
821 {
822     MessageParcel data;
823     MessageParcel reply;
824     MessageOption option;
825 
826     bool ret = data.WriteInterfaceToken(GetDescriptor());
827     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_FLAG_INVALID, "WriteInterfaceToken failed");
828 
829     ret = rendererInfo.Marshalling(data);
830     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_FLAG_INVALID, "Marshalling rendererInfo failed");
831 
832     int32_t error = Remote()->SendRequest(
833         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_PREFERRED_OUTPUT_STREAM_TYPE), data, reply, option);
834     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, AUDIO_FLAG_INVALID, "Failed to send request, error: %{public}d", error);
835 
836     return reply.ReadInt32();
837 }
838 
GetPreferredInputStreamType(AudioCapturerInfo & capturerInfo)839 int32_t AudioPolicyProxy::GetPreferredInputStreamType(AudioCapturerInfo &capturerInfo)
840 {
841     MessageParcel data;
842     MessageParcel reply;
843     MessageOption option;
844 
845     bool ret = data.WriteInterfaceToken(GetDescriptor());
846     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_FLAG_INVALID, "WriteInterfaceToken failed");
847 
848     ret = capturerInfo.Marshalling(data);
849     CHECK_AND_RETURN_RET_LOG(ret, AUDIO_FLAG_INVALID, "Marshalling capturerInfo failed");
850 
851     int32_t error = Remote()->SendRequest(
852         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_PREFERRED_INPUT_STREAM_TYPE), data, reply, option);
853     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, AUDIO_FLAG_INVALID, "Failed to send request, error: %{public}d", error);
854 
855     return reply.ReadInt32();
856 }
857 
RegisterTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo,const sptr<IRemoteObject> & object)858 int32_t AudioPolicyProxy::RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
859     const sptr<IRemoteObject> &object)
860 {
861     MessageParcel data;
862     MessageParcel reply;
863     MessageOption option;
864 
865     bool ret = data.WriteInterfaceToken(GetDescriptor());
866     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
867 
868     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "Register Tracker Event object is null");
869 
870     data.WriteUint32(mode);
871     WriteStreamChangeInfo(data, mode, streamChangeInfo);
872     data.WriteRemoteObject(object);
873 
874     int32_t error = Remote()->SendRequest(
875         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_TRACKER), data, reply, option);
876     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "event failed , error: %d", error);
877 
878     return reply.ReadInt32();
879 }
880 
UpdateTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo)881 int32_t AudioPolicyProxy::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo)
882 {
883     MessageParcel data;
884     MessageParcel reply;
885     MessageOption option;
886 
887     bool ret = data.WriteInterfaceToken(GetDescriptor());
888     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
889 
890     data.WriteUint32(mode);
891     WriteStreamChangeInfo(data, mode, streamChangeInfo);
892 
893     int32_t error = Remote()->SendRequest(
894         static_cast<uint32_t>(AudioPolicyInterfaceCode::UPDATE_TRACKER), data, reply, option);
895     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "UpdateTracker event failed , error: %d", error);
896 
897     return reply.ReadInt32();
898 }
899 
FetchOutputDeviceForTrack(AudioStreamChangeInfo & streamChangeInfo,const AudioStreamDeviceChangeReasonExt reason)900 void AudioPolicyProxy::FetchOutputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo,
901     const AudioStreamDeviceChangeReasonExt reason)
902 {
903     MessageParcel data;
904     MessageParcel reply;
905     MessageOption option;
906 
907     bool ret = data.WriteInterfaceToken(GetDescriptor());
908     if (!ret) {
909         AUDIO_ERR_LOG("WriteInterfaceToken failed");
910     }
911 
912     streamChangeInfo.audioRendererChangeInfo.Marshalling(data);
913 
914     data.WriteInt32(static_cast<int32_t>(reason));
915 
916     int32_t error = Remote()->SendRequest(
917         static_cast<uint32_t>(AudioPolicyInterfaceCode::FETCH_OUTPUT_DEVICE_FOR_TRACK), data, reply, option);
918     if (error != ERR_NONE) {
919         AUDIO_ERR_LOG("event failed , error: %d", error);
920     }
921     return;
922 }
923 
FetchInputDeviceForTrack(AudioStreamChangeInfo & streamChangeInfo)924 void AudioPolicyProxy::FetchInputDeviceForTrack(AudioStreamChangeInfo &streamChangeInfo)
925 {
926     MessageParcel data;
927     MessageParcel reply;
928     MessageOption option;
929 
930     bool ret = data.WriteInterfaceToken(GetDescriptor());
931     if (!ret) {
932         AUDIO_ERR_LOG("WriteInterfaceToken failed");
933     }
934 
935     streamChangeInfo.audioCapturerChangeInfo.Marshalling(data);
936 
937     int32_t error = Remote()->SendRequest(
938         static_cast<uint32_t>(AudioPolicyInterfaceCode::FETCH_INPUT_DEVICE_FOR_TRACK), data, reply, option);
939     if (error != ERR_NONE) {
940         AUDIO_ERR_LOG("event failed , error: %d", error);
941     }
942     return;
943 }
944 
GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)945 int32_t AudioPolicyProxy::GetCurrentRendererChangeInfos(
946     vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
947 {
948     MessageParcel data;
949     MessageParcel reply;
950     MessageOption option;
951 
952     bool ret = data.WriteInterfaceToken(GetDescriptor());
953     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
954 
955     int32_t error = Remote()->SendRequest(
956         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_RENDERER_CHANGE_INFOS), data, reply, option);
957     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "Get Renderer change info event failed , error: %d", error);
958 
959     int32_t size = reply.ReadInt32();
960     while (size > 0) {
961         unique_ptr<AudioRendererChangeInfo> rendererChangeInfo = make_unique<AudioRendererChangeInfo>();
962         CHECK_AND_RETURN_RET_LOG(rendererChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
963         rendererChangeInfo->Unmarshalling(reply);
964         audioRendererChangeInfos.push_back(move(rendererChangeInfo));
965         size--;
966     }
967 
968     return SUCCESS;
969 }
970 
GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)971 int32_t AudioPolicyProxy::GetCurrentCapturerChangeInfos(
972     vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
973 {
974     MessageParcel data;
975     MessageParcel reply;
976     MessageOption option;
977 
978     AUDIO_DEBUG_LOG("AudioPolicyProxy::GetCurrentCapturerChangeInfos");
979 
980     bool ret = data.WriteInterfaceToken(GetDescriptor());
981     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
982 
983     int32_t error = Remote()->SendRequest(
984         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_CAPTURER_CHANGE_INFOS), data, reply, option);
985     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "Get capturer change info event failed , error: %d", error);
986 
987     int32_t size = reply.ReadInt32();
988     while (size > 0) {
989         unique_ptr<AudioCapturerChangeInfo> capturerChangeInfo = make_unique<AudioCapturerChangeInfo>();
990         CHECK_AND_RETURN_RET_LOG(capturerChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
991         capturerChangeInfo->Unmarshalling(reply);
992         audioCapturerChangeInfos.push_back(move(capturerChangeInfo));
993         size--;
994     }
995 
996     return SUCCESS;
997 }
998 
UpdateStreamState(const int32_t clientUid,StreamSetState streamSetState,StreamUsage streamUsage)999 int32_t AudioPolicyProxy::UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState,
1000     StreamUsage streamUsage)
1001 {
1002     MessageParcel data;
1003     MessageParcel reply;
1004     MessageOption option;
1005 
1006     bool ret = data.WriteInterfaceToken(GetDescriptor());
1007     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1008 
1009     data.WriteInt32(static_cast<int32_t>(clientUid));
1010     data.WriteInt32(static_cast<int32_t>(streamSetState));
1011     data.WriteInt32(static_cast<int32_t>(streamUsage));
1012 
1013     int32_t error = Remote()->SendRequest(
1014         static_cast<uint32_t>(AudioPolicyInterfaceCode::UPDATE_STREAM_STATE), data, reply, option);
1015     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR,
1016         "UPDATE_STREAM_STATE stream changed info event failed , error: %d", error);
1017 
1018     return SUCCESS;
1019 }
1020 
GetNetworkIdByGroupId(int32_t groupId,std::string & networkId)1021 int32_t AudioPolicyProxy::GetNetworkIdByGroupId(int32_t groupId, std::string &networkId)
1022 {
1023     MessageParcel data;
1024     MessageParcel reply;
1025     MessageOption option;
1026 
1027     bool res = data.WriteInterfaceToken(GetDescriptor());
1028     CHECK_AND_RETURN_RET_LOG(res, ERROR, "WriteInterfaceToken failed");
1029     data.WriteInt32(groupId);
1030     int32_t error = Remote()->SendRequest(
1031         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_NETWORKID_BY_GROUP_ID), data, reply, option);
1032     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "GetNetworkIdByGroupId, error: %d", error);
1033 
1034     networkId = reply.ReadString();
1035     int32_t ret = reply.ReadInt32();
1036     return ret;
1037 }
1038 
IsAudioRendererLowLatencySupported(const AudioStreamInfo & audioStreamInfo)1039 bool AudioPolicyProxy::IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo)
1040 {
1041     MessageParcel data;
1042     MessageParcel reply;
1043     MessageOption option;
1044 
1045     bool ret = data.WriteInterfaceToken(GetDescriptor());
1046     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
1047     audioStreamInfo.Marshalling(data);
1048     int32_t error = Remote()->SendRequest(
1049         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_AUDIO_RENDER_LOW_LATENCY_SUPPORTED), data, reply, option);
1050     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERR_TRANSACTION_FAILED,
1051         "IsAudioRendererLowLatencySupported, error: %d", error);
1052 
1053     return reply.ReadBool();
1054 }
1055 
SetSystemSoundUri(const std::string & key,const std::string & uri)1056 int32_t AudioPolicyProxy::SetSystemSoundUri(const std::string &key, const std::string &uri)
1057 {
1058     MessageParcel data;
1059     MessageParcel reply;
1060     MessageOption option;
1061 
1062     bool ret = data.WriteInterfaceToken(GetDescriptor());
1063     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
1064     data.WriteString(key);
1065     data.WriteString(uri);
1066     int32_t error = Remote()->SendRequest(
1067         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SYSTEM_SOUND_URI), data, reply, option);
1068     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SetSystemSoundUri failed, error: %d", error);
1069     return reply.ReadInt32();
1070 }
1071 
GetSystemSoundUri(const std::string & key)1072 std::string AudioPolicyProxy::GetSystemSoundUri(const std::string &key)
1073 {
1074     MessageParcel data;
1075     MessageParcel reply;
1076     MessageOption option;
1077 
1078     bool ret = data.WriteInterfaceToken(GetDescriptor());
1079     CHECK_AND_RETURN_RET_LOG(ret, "", "WriteInterfaceToken failed");
1080     data.WriteString(key);
1081     int32_t error = Remote()->SendRequest(
1082         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_SOUND_URI), data, reply, option);
1083     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, "", "GetSystemSoundUri failed, error: %d", error);
1084     return reply.ReadString();
1085 }
1086 
CheckMaxRendererInstances()1087 int32_t AudioPolicyProxy::CheckMaxRendererInstances()
1088 {
1089     MessageParcel data;
1090     MessageParcel reply;
1091     MessageOption option;
1092 
1093     bool ret = data.WriteInterfaceToken(GetDescriptor());
1094     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1095     int32_t error = Remote()->SendRequest(
1096         static_cast<uint32_t>(AudioPolicyInterfaceCode::CHECK_MAX_RENDERER_INSTANCES), data, reply, option);
1097     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "CheckMaxRendererInstances failed, error: %d", error);
1098     return reply.ReadInt32();
1099 }
1100 
PreprocessMode(Stream & stream,MessageParcel & reply,int countMode)1101 static void PreprocessMode(Stream &stream, MessageParcel &reply, int countMode)
1102 {
1103     for (int j = 0; j < countMode; j++) {
1104         StreamEffectMode streamEffectMode;
1105         streamEffectMode.mode = reply.ReadString();
1106         int countDev = reply.ReadInt32();
1107         if (countDev > 0) {
1108             for (int k = 0; k < countDev; k++) {
1109                 string type = reply.ReadString();
1110                 string chain = reply.ReadString();
1111                 streamEffectMode.devicePort.push_back({type, chain});
1112             }
1113         }
1114         stream.streamEffectMode.push_back(streamEffectMode);
1115     }
1116 }
1117 
PreprocessProcess(MessageParcel & reply)1118 static Stream PreprocessProcess(MessageParcel &reply)
1119 {
1120     Stream stream;
1121     stream.scene = reply.ReadString();
1122     int countMode = reply.ReadInt32();
1123     if (countMode > 0) {
1124         PreprocessMode(stream, reply, countMode);
1125     }
1126     return stream;
1127 }
1128 
PostprocessMode(Stream & stream,MessageParcel & reply,int countMode)1129 static void PostprocessMode(Stream &stream, MessageParcel &reply, int countMode)
1130 {
1131     for (int j = 0; j < countMode; j++) {
1132         StreamEffectMode streamEffectMode;
1133         streamEffectMode.mode = reply.ReadString();
1134         int countDev = reply.ReadInt32();
1135         if (countDev > 0) {
1136             for (int k = 0; k < countDev; k++) {
1137                 string type = reply.ReadString();
1138                 string chain = reply.ReadString();
1139                 streamEffectMode.devicePort.push_back({type, chain});
1140             }
1141         }
1142         stream.streamEffectMode.push_back(streamEffectMode);
1143     }
1144 }
1145 
PostprocessProcess(MessageParcel & reply)1146 static Stream PostprocessProcess(MessageParcel &reply)
1147 {
1148     Stream stream;
1149     stream.scene = reply.ReadString();
1150     int countMode = reply.ReadInt32();
1151     if (countMode > 0) {
1152         PostprocessMode(stream, reply, countMode);
1153     }
1154     return stream;
1155 }
1156 
QueryEffectSceneModeChkReply(uint32_t countPre,uint32_t countPost)1157 static int32_t QueryEffectSceneModeChkReply(uint32_t countPre, uint32_t countPost)
1158 {
1159     CHECK_AND_RETURN_RET_LOG((countPre >= 0) && (countPre <= AUDIO_EFFECT_COUNT_UPPER_LIMIT), -1,
1160         "QUERY_EFFECT_SCENEMODE read replyParcel failed");
1161     CHECK_AND_RETURN_RET_LOG((countPost >= 0) && (countPost <= AUDIO_EFFECT_COUNT_UPPER_LIMIT), -1,
1162         "QUERY_EFFECT_SCENEMODE read replyParcel failed");
1163     return 0;
1164 }
1165 
QueryEffectSceneMode(SupportedEffectConfig & supportedEffectConfig)1166 int32_t AudioPolicyProxy::QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig)
1167 {
1168     uint32_t i;
1169     int32_t error;
1170     MessageParcel data;
1171     MessageParcel reply;
1172     MessageOption option;
1173     bool ret = data.WriteInterfaceToken(GetDescriptor());
1174     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1175     error = Remote()->SendRequest(
1176         static_cast<uint32_t>(AudioPolicyInterfaceCode::QUERY_EFFECT_SCENEMODE), data, reply, option);
1177     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get scene & mode failed, error: %d", error);
1178     uint32_t countPre = reply.ReadUint32();
1179     uint32_t countPost = reply.ReadUint32();
1180     uint32_t countPostMap = reply.ReadUint32();
1181     error = QueryEffectSceneModeChkReply(countPre, countPost);
1182     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get scene & mode failed, error: %d", error);
1183     // preprocess
1184     Stream stream;
1185     if (countPre > 0) {
1186         ProcessNew preProcessNew;
1187         for (i = 0; i < countPre; i++) {
1188             stream = PreprocessProcess(reply);
1189             preProcessNew.stream.push_back(stream);
1190         }
1191         supportedEffectConfig.preProcessNew = preProcessNew;
1192     }
1193     // postprocess
1194     if (countPost > 0) {
1195         ProcessNew postProcessNew;
1196         for (i = 0; i < countPost; i++) {
1197             stream = PostprocessProcess(reply);
1198             postProcessNew.stream.push_back(stream);
1199         }
1200         supportedEffectConfig.postProcessNew = postProcessNew;
1201     }
1202     if (countPostMap > 0) {
1203         SceneMappingItem item;
1204         for (i = 0; i < countPostMap; i++) {
1205             item.name = reply.ReadString();
1206             item.sceneType = reply.ReadString();
1207             supportedEffectConfig.postProcessSceneMap.push_back(item);
1208         }
1209     }
1210     return 0;
1211 }
1212 
SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig & config,uint32_t appTokenId)1213 int32_t AudioPolicyProxy::SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config, uint32_t appTokenId)
1214 {
1215     MessageParcel data;
1216     MessageParcel reply;
1217     MessageOption option;
1218 
1219     bool ret = data.WriteInterfaceToken(GetDescriptor());
1220     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1221     data.WriteInt32(static_cast<int32_t>(config.silentCapture));
1222     size_t ss = config.filterOptions.usages.size();
1223     data.WriteUint32(ss);
1224     for (size_t i = 0; i < ss; i++) {
1225         data.WriteInt32(static_cast<int32_t>(config.filterOptions.usages[i]));
1226     }
1227     data.WriteUint32(appTokenId);
1228 
1229     int32_t error = Remote()->SendRequest(
1230         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_PLAYBACK_CAPTURER_FILTER_INFO), data, reply, option);
1231     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetPlaybackCapturerFilterInfos failed, error: %d", error);
1232     return reply.ReadInt32();
1233 }
1234 
SetCaptureSilentState(bool state)1235 int32_t AudioPolicyProxy::SetCaptureSilentState(bool state)
1236 {
1237     MessageParcel data;
1238     MessageParcel reply;
1239     MessageOption option;
1240 
1241     if (!data.WriteInterfaceToken(GetDescriptor())) {
1242         AUDIO_ERR_LOG(" SetCaptureSilentState WriteInterfaceToken failed");
1243         return ERROR;
1244     }
1245     data.WriteBool(state);
1246 
1247     int32_t error = Remote()->SendRequest(
1248         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_CAPTURER_SILENT_STATE), data, reply, option);
1249     if (error != ERR_NONE) {
1250         AUDIO_ERR_LOG("SetCaptureSilentState failed, error: %d", error);
1251         return ERROR;
1252     }
1253     return reply.ReadInt32();
1254 }
1255 
GetHardwareOutputSamplingRate(const sptr<AudioDeviceDescriptor> & desc)1256 int32_t AudioPolicyProxy::GetHardwareOutputSamplingRate(const sptr<AudioDeviceDescriptor> &desc)
1257 {
1258     MessageParcel data;
1259     MessageParcel reply;
1260     MessageOption option;
1261 
1262     bool ret = data.WriteInterfaceToken(GetDescriptor());
1263     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1264 
1265     bool result = desc->Marshalling(data);
1266     CHECK_AND_RETURN_RET_LOG(result, -1, "AudioDeviceDescriptor Marshalling() failed");
1267 
1268     int32_t error = Remote()->SendRequest(
1269         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_HARDWARE_OUTPUT_SAMPLING_RATE), data, reply, option);
1270     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "event failed , error: %d", error);
1271 
1272     return reply.ReadInt32();
1273 }
1274 
IsVgsVolumeSupported()1275 bool AudioPolicyProxy::IsVgsVolumeSupported()
1276 {
1277     MessageParcel data;
1278     MessageParcel reply;
1279     MessageOption option;
1280 
1281     bool ret = data.WriteInterfaceToken(GetDescriptor());
1282     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1283 
1284     int32_t error = Remote()->SendRequest(
1285         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_VGS_VOLUME_SUPPORTED), data, reply, option);
1286     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "IsVgsVolumeSupported failed, error: %d", error);
1287     return reply.ReadBool();
1288 }
1289 
GetAvailableDevices(AudioDeviceUsage usage)1290 std::vector<std::unique_ptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetAvailableDevices(AudioDeviceUsage usage)
1291 {
1292     MessageParcel data;
1293     MessageParcel reply;
1294     MessageOption option;
1295     std::vector<std::unique_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors;
1296 
1297     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
1298         audioDeviceDescriptors, "WriteInterfaceToken failed");
1299 
1300     bool token = data.WriteInt32(static_cast<int32_t>(usage));
1301     CHECK_AND_RETURN_RET_LOG(token, audioDeviceDescriptors, "WriteInt32 usage failed");
1302 
1303     int32_t error = Remote()->SendRequest(
1304         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AVAILABLE_DESCRIPTORS), data, reply, option);
1305     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, audioDeviceDescriptors, "GetAvailableDevices failed, error: %d", error);
1306 
1307     int32_t size = reply.ReadInt32();
1308     for (int32_t i = 0; i < size; i++) {
1309         std::unique_ptr<AudioDeviceDescriptor> desc =
1310             std::make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::Unmarshalling(reply));
1311         audioDeviceDescriptors.push_back(move(desc));
1312     }
1313     return audioDeviceDescriptors;
1314 }
1315 
IsSpatializationEnabled()1316 bool AudioPolicyProxy::IsSpatializationEnabled()
1317 {
1318     MessageParcel data;
1319     MessageParcel reply;
1320     MessageOption option;
1321 
1322     bool ret = data.WriteInterfaceToken(GetDescriptor());
1323     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1324 
1325     int32_t error = Remote()->SendRequest(
1326         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_SPATIALIZATION_ENABLED), data, reply, option);
1327     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsSpatializationEnabled failed, error: %{public}d", error);
1328     return reply.ReadBool();
1329 }
1330 
IsSpatializationEnabled(const std::string address)1331 bool AudioPolicyProxy::IsSpatializationEnabled(const std::string address)
1332 {
1333     MessageParcel data;
1334     MessageParcel reply;
1335     MessageOption option;
1336 
1337     bool ret = data.WriteInterfaceToken(GetDescriptor());
1338     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1339     data.WriteString(address);
1340 
1341     int32_t error = Remote()->SendRequest(
1342         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_SPATIALIZATION_ENABLED_FOR_DEVICE), data, reply, option);
1343     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsSpatializationEnabled failed, error: %{public}d", error);
1344     return reply.ReadBool();
1345 }
1346 
1347 
SetSpatializationEnabled(const bool enable)1348 int32_t AudioPolicyProxy::SetSpatializationEnabled(const bool enable)
1349 {
1350     MessageParcel data;
1351     MessageParcel reply;
1352     MessageOption option;
1353 
1354     bool ret = data.WriteInterfaceToken(GetDescriptor());
1355     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1356     data.WriteBool(enable);
1357 
1358     int32_t error = Remote()->SendRequest(
1359         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SPATIALIZATION_ENABLED), data, reply, option);
1360     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetSpatializationEnabled failed, error: %{public}d", error);
1361     return reply.ReadInt32();
1362 }
1363 
SetSpatializationEnabled(const sptr<AudioDeviceDescriptor> & selectedAudioDevice,const bool enable)1364 int32_t AudioPolicyProxy::SetSpatializationEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice,
1365     const bool enable)
1366 {
1367     MessageParcel data;
1368     MessageParcel reply;
1369     MessageOption option;
1370 
1371     bool ret = data.WriteInterfaceToken(GetDescriptor());
1372     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1373 
1374     bool result = selectedAudioDevice->Marshalling(data);
1375     CHECK_AND_RETURN_RET_LOG(result, -1, "SelectedAudioDevice Marshalling() failed");
1376 
1377     data.WriteBool(enable);
1378 
1379     int32_t error = Remote()->SendRequest(
1380         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SPATIALIZATION_ENABLED_FOR_DEVICE), data, reply, option);
1381     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetSpatializationEnabled failed, error: %{public}d", error);
1382     return reply.ReadInt32();
1383 }
1384 
IsHeadTrackingEnabled()1385 bool AudioPolicyProxy::IsHeadTrackingEnabled()
1386 {
1387     MessageParcel data;
1388     MessageParcel reply;
1389     MessageOption option;
1390 
1391     bool ret = data.WriteInterfaceToken(GetDescriptor());
1392     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1393 
1394     int32_t error = Remote()->SendRequest(
1395         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HEAD_TRACKING_ENABLED), data, reply, option);
1396 
1397     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsHeadTrackingEnabled failed, error: %{public}d", error);
1398     return reply.ReadBool();
1399 }
1400 
IsHeadTrackingEnabled(const std::string address)1401 bool AudioPolicyProxy::IsHeadTrackingEnabled(const std::string address)
1402 {
1403     MessageParcel data;
1404     MessageParcel reply;
1405     MessageOption option;
1406 
1407     bool ret = data.WriteInterfaceToken(GetDescriptor());
1408     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1409     data.WriteString(address);
1410 
1411     int32_t error = Remote()->SendRequest(
1412         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HEAD_TRACKING_ENABLED_FOR_DEVICE), data, reply, option);
1413 
1414     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsHeadTrackingEnabled failed, error: %{public}d", error);
1415     return reply.ReadBool();
1416 }
1417 
SetHeadTrackingEnabled(const bool enable)1418 int32_t AudioPolicyProxy::SetHeadTrackingEnabled(const bool enable)
1419 {
1420     MessageParcel data;
1421     MessageParcel reply;
1422     MessageOption option;
1423 
1424     bool ret = data.WriteInterfaceToken(GetDescriptor());
1425     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1426     data.WriteBool(enable);
1427 
1428     int32_t error = Remote()->SendRequest(
1429         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_HEAD_TRACKING_ENABLED), data, reply, option);
1430     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetHeadTrackingEnabled failed, error: %{public}d", error);
1431     return reply.ReadInt32();
1432 }
1433 
SetHeadTrackingEnabled(const sptr<AudioDeviceDescriptor> & selectedAudioDevice,const bool enable)1434 int32_t AudioPolicyProxy::SetHeadTrackingEnabled(const sptr<AudioDeviceDescriptor> &selectedAudioDevice,
1435     const bool enable)
1436 {
1437     MessageParcel data;
1438     MessageParcel reply;
1439     MessageOption option;
1440 
1441     bool ret = data.WriteInterfaceToken(GetDescriptor());
1442     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1443 
1444     bool result = selectedAudioDevice->Marshalling(data);
1445     CHECK_AND_RETURN_RET_LOG(result, -1, "SelectedAudioDevice Marshalling() failed");
1446     data.WriteBool(enable);
1447 
1448     int32_t error = Remote()->SendRequest(
1449         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_HEAD_TRACKING_ENABLED_FOR_DEVICE), data, reply, option);
1450     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetHeadTrackingEnabled failed, error: %{public}d", error);
1451     return reply.ReadInt32();
1452 }
1453 
GetSpatializationState(const StreamUsage streamUsage)1454 AudioSpatializationState AudioPolicyProxy::GetSpatializationState(const StreamUsage streamUsage)
1455 {
1456     MessageParcel data;
1457     MessageParcel reply;
1458     MessageOption option;
1459     AudioSpatializationState spatializationState = {false, false};
1460 
1461     bool ret = data.WriteInterfaceToken(GetDescriptor());
1462     CHECK_AND_RETURN_RET_LOG(ret, spatializationState, "WriteInterfaceToken failed");
1463 
1464     data.WriteInt32(static_cast<int32_t>(streamUsage));
1465     int32_t error = Remote() ->SendRequest(
1466         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SPATIALIZATION_STATE), data, reply, option);
1467     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, spatializationState, "SendRequest failed, error: %{public}d", error);
1468 
1469     spatializationState.spatializationEnabled = reply.ReadBool();
1470     spatializationState.headTrackingEnabled = reply.ReadBool();
1471 
1472     return spatializationState;
1473 }
1474 
IsSpatializationSupported()1475 bool AudioPolicyProxy::IsSpatializationSupported()
1476 {
1477     MessageParcel data;
1478     MessageParcel reply;
1479     MessageOption option;
1480 
1481     bool ret = data.WriteInterfaceToken(GetDescriptor());
1482     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1483 
1484     int32_t error = Remote()->SendRequest(
1485         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_SPATIALIZATION_SUPPORTED), data, reply, option);
1486     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsSpatializationSupported failed, error: %d", error);
1487     return reply.ReadBool();
1488 }
1489 
IsSpatializationSupportedForDevice(const std::string address)1490 bool AudioPolicyProxy::IsSpatializationSupportedForDevice(const std::string address)
1491 {
1492     MessageParcel data;
1493     MessageParcel reply;
1494     MessageOption option;
1495 
1496     bool ret = data.WriteInterfaceToken(GetDescriptor());
1497     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1498     data.WriteString(address);
1499 
1500     int32_t error = Remote()->SendRequest(
1501         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_SPATIALIZATION_SUPPORTED_FOR_DEVICE), data, reply, option);
1502     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "IsSpatializationSupportedForDevice failed, error: %d", error);
1503     return reply.ReadBool();
1504 }
1505 
IsHeadTrackingSupported()1506 bool AudioPolicyProxy::IsHeadTrackingSupported()
1507 {
1508     MessageParcel data;
1509     MessageParcel reply;
1510     MessageOption option;
1511 
1512     bool ret = data.WriteInterfaceToken(GetDescriptor());
1513     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1514 
1515     int32_t error = Remote()->SendRequest(
1516         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HEAD_TRACKING_SUPPORTED), data, reply, option);
1517     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "IsHeadTrackingSupported failed, error: %d", error);
1518     return reply.ReadBool();
1519 }
1520 
IsHeadTrackingSupportedForDevice(const std::string address)1521 bool AudioPolicyProxy::IsHeadTrackingSupportedForDevice(const std::string address)
1522 {
1523     MessageParcel data;
1524     MessageParcel reply;
1525     MessageOption option;
1526 
1527     bool ret = data.WriteInterfaceToken(GetDescriptor());
1528     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1529     data.WriteString(address);
1530 
1531     int32_t error = Remote()->SendRequest(
1532         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HEAD_TRACKING_SUPPORTED_FOR_DEVICE), data, reply, option);
1533     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "IsHeadTrackingSupportedForDevice failed, error: %d", error);
1534     return reply.ReadBool();
1535 }
1536 
UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState)1537 int32_t AudioPolicyProxy::UpdateSpatialDeviceState(const AudioSpatialDeviceState audioSpatialDeviceState)
1538 {
1539     MessageParcel data;
1540     MessageParcel reply;
1541     MessageOption option;
1542 
1543     bool ret = data.WriteInterfaceToken(GetDescriptor());
1544     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1545     data.WriteString(audioSpatialDeviceState.address);
1546     data.WriteBool(audioSpatialDeviceState.isSpatializationSupported);
1547     data.WriteBool(audioSpatialDeviceState.isHeadTrackingSupported);
1548     data.WriteInt32(static_cast<int32_t>(audioSpatialDeviceState.spatialDeviceType));
1549 
1550     int32_t error = Remote()->SendRequest(
1551         static_cast<uint32_t>(AudioPolicyInterfaceCode::UPDATE_SPATIAL_DEVICE_STATE), data, reply, option);
1552     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "UpdateSpatialDeviceState failed, error: %d", error);
1553     return reply.ReadInt32();
1554 }
1555 
CreateAudioInterruptZone(const std::set<int32_t> & pids,const int32_t zoneID)1556 int32_t AudioPolicyProxy::CreateAudioInterruptZone(const std::set<int32_t> &pids, const int32_t zoneID)
1557 {
1558     MessageParcel data;
1559     MessageParcel reply;
1560     MessageOption option;
1561     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), -1, "WriteInterfaceToken failed");
1562 
1563     data.WriteInt32(zoneID);
1564     data.WriteInt32(pids.size());
1565     int32_t count = 0;
1566     for (int32_t pid : pids) {
1567         data.WriteInt32(pid);
1568         count++;
1569         if (count >= MAX_PID_COUNT) {
1570             break;
1571         }
1572     }
1573 
1574     int error = Remote()->SendRequest(
1575         static_cast<uint32_t>(AudioPolicyInterfaceCode::CREATE_AUDIO_INTERRUPT_ZONE), data, reply, option);
1576 
1577     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "CreateAudioInterruptZone failed, error: %d", error);
1578     return reply.ReadInt32();
1579 }
1580 
AddAudioInterruptZonePids(const std::set<int32_t> & pids,const int32_t zoneID)1581 int32_t AudioPolicyProxy::AddAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID)
1582 {
1583     MessageParcel data;
1584     MessageParcel reply;
1585     MessageOption option;
1586     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), -1, "WriteInterfaceToken failed");
1587     data.WriteInt32(zoneID);
1588     data.WriteInt32(pids.size());
1589     int32_t count = 0;
1590     for (int32_t pid : pids) {
1591         data.WriteInt32(pid);
1592         count++;
1593         if (count >= MAX_PID_COUNT) {
1594             break;
1595         }
1596     }
1597 
1598     int error = Remote()->SendRequest(
1599         static_cast<uint32_t>(AudioPolicyInterfaceCode::ADD_AUDIO_INTERRUPT_ZONE_PIDS), data, reply, option);
1600     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "AddAudioInterruptZonePids failed, error: %d", error);
1601     return reply.ReadInt32();
1602 }
1603 
RemoveAudioInterruptZonePids(const std::set<int32_t> & pids,const int32_t zoneID)1604 int32_t AudioPolicyProxy::RemoveAudioInterruptZonePids(const std::set<int32_t> &pids, const int32_t zoneID)
1605 {
1606     MessageParcel data;
1607     MessageParcel reply;
1608     MessageOption option;
1609     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), -1, "WriteInterfaceToken failed");
1610     data.WriteInt32(zoneID);
1611     data.WriteInt32(pids.size());
1612     int32_t count = 0;
1613     for (int32_t pid : pids) {
1614         data.WriteInt32(pid);
1615         count++;
1616         if (count >= MAX_PID_COUNT) {
1617             break;
1618         }
1619     }
1620     int error = Remote()->SendRequest(
1621         static_cast<uint32_t>(AudioPolicyInterfaceCode::REMOVE_AUDIO_INTERRUPT_ZONE_PIDS), data, reply, option);
1622     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "RemoveAudioInterruptZonePids failed, error: %d", error);
1623 
1624     return reply.ReadInt32();
1625 }
1626 
ReleaseAudioInterruptZone(const int32_t zoneID)1627 int32_t AudioPolicyProxy::ReleaseAudioInterruptZone(const int32_t zoneID)
1628 {
1629     MessageParcel data;
1630     MessageParcel reply;
1631     MessageOption option;
1632     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), -1, "WriteInterfaceToken failed");
1633     data.WriteInt32(zoneID);
1634     int error = Remote()->SendRequest(
1635         static_cast<uint32_t>(AudioPolicyInterfaceCode::RELEASE_AUDIO_INTERRUPT_ZONE), data, reply, option);
1636     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "ReleaseAudioInterruptZone failed, error: %d", error);
1637     return reply.ReadInt32();
1638 }
1639 
SetCallDeviceActive(InternalDeviceType deviceType,bool active,std::string address)1640 int32_t AudioPolicyProxy::SetCallDeviceActive(InternalDeviceType deviceType, bool active, std::string address)
1641 {
1642     MessageParcel data;
1643     MessageParcel reply;
1644     MessageOption option;
1645 
1646     bool ret = data.WriteInterfaceToken(GetDescriptor());
1647     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1648     data.WriteInt32(static_cast<int32_t>(deviceType));
1649     data.WriteBool(active);
1650     data.WriteString(address);
1651     int32_t error = Remote()->SendRequest(
1652         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_CALL_DEVICE_ACTIVE), data, reply, option);
1653     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %d", error);
1654     return reply.ReadInt32();
1655 }
1656 
GetActiveBluetoothDevice()1657 std::unique_ptr<AudioDeviceDescriptor> AudioPolicyProxy::GetActiveBluetoothDevice()
1658 {
1659     MessageParcel data;
1660     MessageParcel reply;
1661     MessageOption option;
1662     std::unique_ptr<AudioDeviceDescriptor> audioDeviceDescriptor;
1663 
1664     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
1665         audioDeviceDescriptor, "WriteInterfaceToken failed");
1666 
1667     int32_t error = Remote()->SendRequest(
1668         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_BLUETOOTH_DESCRIPTOR), data, reply, option);
1669     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, audioDeviceDescriptor,
1670         "GetActiveBluetoothDevice failed, error: %d", error);
1671 
1672     std::unique_ptr<AudioDeviceDescriptor> desc =
1673         std::make_unique<AudioDeviceDescriptor>(AudioDeviceDescriptor::Unmarshalling(reply));
1674     return desc;
1675 }
1676 
NotifyCapturerAdded(AudioCapturerInfo capturerInfo,AudioStreamInfo streamInfo,uint32_t sessionId)1677 int32_t AudioPolicyProxy::NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo,
1678     uint32_t sessionId)
1679 {
1680     MessageParcel data;
1681     MessageParcel reply;
1682     MessageOption option;
1683 
1684     bool ret = data.WriteInterfaceToken(GetDescriptor());
1685     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1686     capturerInfo.Marshalling(data);
1687     streamInfo.Marshalling(data);
1688     data.WriteUint32(sessionId);
1689     int32_t error = Remote()->SendRequest(
1690         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_WAKEUP_AUDIOCAPTURER), data, reply, option);
1691     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, -1, "failed, error: %d", error);
1692     return reply.ReadInt32();
1693 }
1694 
GetConverterConfig()1695 ConverterConfig AudioPolicyProxy::GetConverterConfig()
1696 {
1697     MessageParcel data;
1698     MessageParcel reply;
1699     MessageOption option;
1700     ConverterConfig result;
1701 
1702     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), result, "WriteInterfaceToken failed");
1703     int32_t error = Remote()->SendRequest(static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_CONVERTER_CONFIG),
1704         data, reply, option);
1705     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, result, "failed, error: %d", error);
1706 
1707     result.library = {reply.ReadString(), reply.ReadString()};
1708     result.outChannelLayout = reply.ReadUint64();
1709     return result;
1710 }
1711 
IsHighResolutionExist()1712 bool AudioPolicyProxy::IsHighResolutionExist()
1713 {
1714     MessageParcel data;
1715     MessageParcel reply;
1716     MessageOption option;
1717 
1718     bool ret = data.WriteInterfaceToken(GetDescriptor());
1719     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1720     int32_t error = Remote()->SendRequest(
1721         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HIGH_RESOLUTION_EXIST), data, reply, option);
1722     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERR_TRANSACTION_FAILED, "SendRequest failed, error: %d", error);
1723     bool replyReadBool = reply.ReadBool();
1724     return replyReadBool;
1725 }
1726 
SetHighResolutionExist(bool highResExist)1727 int32_t AudioPolicyProxy::SetHighResolutionExist(bool highResExist)
1728 {
1729     MessageParcel data;
1730     MessageParcel reply;
1731     MessageOption option;
1732 
1733     bool ret = data.WriteInterfaceToken(GetDescriptor());
1734     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1735 
1736     data.WriteBool(highResExist);
1737     int32_t error = Remote()->SendRequest(
1738         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_HIGH_RESOLUTION_EXIST), data, reply, option);
1739     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %d", error);
1740     return SUCCESS;
1741 }
1742 
GetSpatializationSceneType()1743 AudioSpatializationSceneType AudioPolicyProxy::GetSpatializationSceneType()
1744 {
1745     MessageParcel data;
1746     MessageParcel reply;
1747     MessageOption option;
1748 
1749     bool ret = data.WriteInterfaceToken(GetDescriptor());
1750     CHECK_AND_RETURN_RET_LOG(ret, SPATIALIZATION_SCENE_TYPE_DEFAULT, "WriteInterfaceToken failed");
1751 
1752     int32_t error = Remote()->SendRequest(
1753         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SPATIALIZATION_SCENE_TYPE), data, reply, option);
1754     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, SPATIALIZATION_SCENE_TYPE_DEFAULT,
1755         "SendRequest failed, error: %{public}d", error);
1756     return static_cast<AudioSpatializationSceneType>(reply.ReadInt32());
1757 }
1758 
SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType)1759 int32_t AudioPolicyProxy::SetSpatializationSceneType(const AudioSpatializationSceneType spatializationSceneType)
1760 {
1761     MessageParcel data;
1762     MessageParcel reply;
1763     MessageOption option;
1764 
1765     bool ret = data.WriteInterfaceToken(GetDescriptor());
1766     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1767     data.WriteInt32(static_cast<int32_t>(spatializationSceneType));
1768 
1769     int32_t error = Remote()->SendRequest(
1770         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SPATIALIZATION_SCENE_TYPE), data, reply, option);
1771     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SendRequest failed, error: %{public}d", error);
1772     return reply.ReadInt32();
1773 }
1774 
GetMaxAmplitude(const int32_t deviceId)1775 float AudioPolicyProxy::GetMaxAmplitude(const int32_t deviceId)
1776 {
1777     MessageParcel data;
1778     MessageParcel reply;
1779     MessageOption option;
1780 
1781     bool ret = data.WriteInterfaceToken(GetDescriptor());
1782     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1783 
1784     data.WriteInt32(deviceId);
1785     int32_t error = Remote()->SendRequest(
1786         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_AMPLITUDE), data, reply, option);
1787     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1788     return reply.ReadFloat();
1789 }
1790 
DisableSafeMediaVolume()1791 int32_t AudioPolicyProxy::DisableSafeMediaVolume()
1792 {
1793     MessageParcel data;
1794     MessageParcel reply;
1795     MessageOption option;
1796 
1797     bool ret = data.WriteInterfaceToken(GetDescriptor());
1798     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
1799 
1800     int32_t error = Remote()->SendRequest(
1801         static_cast<uint32_t>(AudioPolicyInterfaceCode::DISABLE_SAFE_MEDIA_VOLUME), data, reply, option);
1802     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SendRequest failed, error: %{public}d", error);
1803     return reply.ReadInt32();
1804 }
1805 
IsHeadTrackingDataRequested(const std::string & macAddress)1806 bool AudioPolicyProxy::IsHeadTrackingDataRequested(const std::string &macAddress)
1807 {
1808     MessageParcel data;
1809     MessageParcel reply;
1810     MessageOption option;
1811 
1812     bool ret = data.WriteInterfaceToken(GetDescriptor());
1813     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1814     data.WriteString(macAddress);
1815 
1816     int32_t error = Remote()->SendRequest(
1817         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_HEAD_TRACKING_DATA_REQUESTED), data, reply, option);
1818     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "SendRequest failed, error: %d", error);
1819     return reply.ReadBool();
1820 }
1821 
TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason)1822 int32_t AudioPolicyProxy::TriggerFetchDevice(AudioStreamDeviceChangeReasonExt reason)
1823 {
1824     MessageParcel data;
1825     MessageParcel reply;
1826     MessageOption option;
1827 
1828     bool ret = data.WriteInterfaceToken(GetDescriptor());
1829     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1830 
1831     data.WriteInt32(static_cast<int>(reason));
1832     int32_t error = Remote()->SendRequest(
1833         static_cast<uint32_t>(AudioPolicyInterfaceCode::TRIGGER_FETCH_DEVICE), data, reply, option);
1834     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1835 
1836     return reply.ReadInt32();
1837 }
1838 
SetPreferredDevice(const PreferredType preferredType,const sptr<AudioDeviceDescriptor> & desc)1839 int32_t AudioPolicyProxy::SetPreferredDevice(const PreferredType preferredType,
1840     const sptr<AudioDeviceDescriptor> &desc)
1841 {
1842     MessageParcel data;
1843     MessageParcel reply;
1844     MessageOption option;
1845 
1846     bool ret = data.WriteInterfaceToken(GetDescriptor());
1847     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1848 
1849     data.WriteInt32(static_cast<int32_t>(preferredType));
1850     bool result = desc->Marshalling(data);
1851     CHECK_AND_RETURN_RET_LOG(result, -1, "Desc Marshalling() faild");
1852 
1853     int32_t error = Remote()->SendRequest(
1854         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_PREFERRED_DEVICE), data, reply, option);
1855     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "SendRequest failed, error: %d", error);
1856     return reply.ReadInt32();
1857 }
1858 
SaveRemoteInfo(const std::string & networkId,DeviceType deviceType)1859 void AudioPolicyProxy::SaveRemoteInfo(const std::string &networkId, DeviceType deviceType)
1860 {
1861     MessageParcel data;
1862     MessageParcel reply;
1863     MessageOption option;
1864 
1865     bool ret = data.WriteInterfaceToken(GetDescriptor());
1866     if (!ret) {
1867         AUDIO_ERR_LOG("WriteInterfaceToken failed");
1868     }
1869 
1870     data.WriteString(networkId);
1871     data.WriteInt32(static_cast<int32_t>(deviceType));
1872     int32_t error = Remote()->SendRequest(
1873         static_cast<uint32_t>(AudioPolicyInterfaceCode::SAVE_REMOTE_INFO), data, reply, option);
1874     if (error != ERR_NONE) {
1875         AUDIO_ERR_LOG("SendRequest failed, error: %{public}d", error);
1876     }
1877 }
1878 
MoveToNewPipe(const uint32_t sessionId,const AudioPipeType pipeType)1879 int32_t AudioPolicyProxy::MoveToNewPipe(const uint32_t sessionId, const AudioPipeType pipeType)
1880 {
1881     MessageParcel data;
1882     MessageParcel reply;
1883     MessageOption option;
1884 
1885     bool ret = data.WriteInterfaceToken(GetDescriptor());
1886     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
1887     data.WriteUint32(sessionId);
1888     data.WriteInt32(pipeType);
1889 
1890     int32_t error = Remote()->SendRequest(
1891         static_cast<uint32_t>(AudioPolicyInterfaceCode::MOVE_TO_NEW_PIPE), data, reply, option);
1892     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "SendRequest failed, error: %d", error);
1893     return reply.ReadInt32();
1894 }
1895 
ActivateAudioConcurrency(const AudioPipeType & pipeType)1896 int32_t AudioPolicyProxy::ActivateAudioConcurrency(const AudioPipeType &pipeType)
1897 {
1898     MessageParcel data;
1899     MessageParcel reply;
1900     MessageOption option;
1901 
1902     bool ret = data.WriteInterfaceToken(GetDescriptor());
1903     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1904     data.WriteInt32(pipeType);
1905     int error = Remote()->SendRequest(
1906         static_cast<uint32_t>(AudioPolicyInterfaceCode::ACTIVATE_AUDIO_CONCURRENCY), data, reply, option);
1907     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "activate concurrency failed, error: %{public}d", error);
1908     return reply.ReadInt32();
1909 }
1910 
InjectInterruption(const std::string networkId,InterruptEvent & event)1911 int32_t AudioPolicyProxy::InjectInterruption(const std::string networkId, InterruptEvent &event)
1912 {
1913     MessageParcel data;
1914     MessageParcel reply;
1915     MessageOption option;
1916 
1917     bool ret = data.WriteInterfaceToken(GetDescriptor());
1918     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1919     data.WriteString(networkId);
1920     data.WriteInt32(event.eventType);
1921     data.WriteInt32(event.forceType);
1922     data.WriteInt32(event.hintType);
1923 
1924     int error = Remote()->SendRequest(
1925         static_cast<uint32_t>(AudioPolicyInterfaceCode::INJECT_INTERRUPTION), data, reply, option);
1926     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1927     return reply.ReadInt32();
1928 }
1929 
SetDefaultOutputDevice(const DeviceType deviceType,const uint32_t sessionID,const StreamUsage streamUsage,bool isRunning)1930 int32_t AudioPolicyProxy::SetDefaultOutputDevice(const DeviceType deviceType, const uint32_t sessionID,
1931     const StreamUsage streamUsage, bool isRunning)
1932 {
1933     MessageParcel data;
1934     MessageParcel reply;
1935     MessageOption option;
1936 
1937     bool ret = data.WriteInterfaceToken(GetDescriptor());
1938     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
1939     data.WriteInt32(static_cast<int32_t>(deviceType));
1940     data.WriteUint32(sessionID);
1941     data.WriteInt32(static_cast<int32_t>(streamUsage));
1942     data.WriteBool(isRunning);
1943 
1944     int error = Remote()->SendRequest(
1945         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DEFAULT_OUTPUT_DEVICE), data, reply, option);
1946     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
1947     return reply.ReadInt32();
1948 }
1949 } // namespace AudioStandard
1950 } // namespace OHOS
1951