• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "audio_policy_manager.h"
17 #include "audio_log.h"
18 #include "audio_policy_proxy.h"
19 
20 namespace OHOS {
21 namespace AudioStandard {
22 using namespace std;
23 
AudioPolicyProxy(const sptr<IRemoteObject> & impl)24 AudioPolicyProxy::AudioPolicyProxy(const sptr<IRemoteObject> &impl)
25     : IRemoteProxy<IAudioPolicy>(impl)
26 {
27 }
28 
WriteAudioInteruptParams(MessageParcel & data,const AudioInterrupt & audioInterrupt)29 void AudioPolicyProxy::WriteAudioInteruptParams(MessageParcel &data, const AudioInterrupt &audioInterrupt)
30 {
31     data.WriteInt32(static_cast<int32_t>(audioInterrupt.streamUsage));
32     data.WriteInt32(static_cast<int32_t>(audioInterrupt.contentType));
33     data.WriteInt32(static_cast<int32_t>(audioInterrupt.audioFocusType.streamType));
34     data.WriteInt32(static_cast<int32_t>(audioInterrupt.audioFocusType.sourceType));
35     data.WriteBool(audioInterrupt.audioFocusType.isPlay);
36     data.WriteUint32(audioInterrupt.sessionID);
37     data.WriteInt32(audioInterrupt.pid);
38     data.WriteInt32(static_cast<int32_t>(audioInterrupt.mode));
39     data.WriteBool(audioInterrupt.parallelPlayFlag);
40 }
41 
WriteAudioManagerInteruptParams(MessageParcel & data,const AudioInterrupt & audioInterrupt)42 void AudioPolicyProxy::WriteAudioManagerInteruptParams(MessageParcel &data, const AudioInterrupt &audioInterrupt)
43 {
44     data.WriteInt32(static_cast<int32_t>(audioInterrupt.streamUsage));
45     data.WriteInt32(static_cast<int32_t>(audioInterrupt.contentType));
46     data.WriteInt32(static_cast<int32_t>(audioInterrupt.audioFocusType.streamType));
47     data.WriteInt32(static_cast<int32_t>(audioInterrupt.audioFocusType.sourceType));
48     data.WriteBool(audioInterrupt.audioFocusType.isPlay);
49     data.WriteBool(audioInterrupt.pauseWhenDucked);
50     data.WriteInt32(audioInterrupt.pid);
51     data.WriteInt32(static_cast<int32_t>(audioInterrupt.mode));
52 }
53 
ReadAudioInterruptParams(MessageParcel & reply,AudioInterrupt & audioInterrupt)54 void AudioPolicyProxy::ReadAudioInterruptParams(MessageParcel &reply, AudioInterrupt &audioInterrupt)
55 {
56     audioInterrupt.streamUsage = static_cast<StreamUsage>(reply.ReadInt32());
57     audioInterrupt.contentType = static_cast<ContentType>(reply.ReadInt32());
58     audioInterrupt.audioFocusType.streamType = static_cast<AudioStreamType>(reply.ReadInt32());
59     audioInterrupt.audioFocusType.sourceType = static_cast<SourceType>(reply.ReadInt32());
60     audioInterrupt.audioFocusType.isPlay = reply.ReadBool();
61     audioInterrupt.sessionID = reply.ReadUint32();
62     audioInterrupt.pid = reply.ReadInt32();
63     audioInterrupt.mode = static_cast<InterruptMode>(reply.ReadInt32());
64     audioInterrupt.parallelPlayFlag = reply.ReadBool();
65 }
66 
WriteStreamChangeInfo(MessageParcel & data,const AudioMode & mode,const AudioStreamChangeInfo & streamChangeInfo)67 void AudioPolicyProxy::WriteStreamChangeInfo(MessageParcel &data,
68     const AudioMode &mode, const AudioStreamChangeInfo &streamChangeInfo)
69 {
70     if (mode == AUDIO_MODE_PLAYBACK) {
71         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.sessionId);
72         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererState);
73         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.clientUID);
74         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.contentType);
75         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.streamUsage);
76         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.rendererFlags);
77     } else {
78         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.sessionId);
79         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerState);
80         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.clientUID);
81         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerInfo.sourceType);
82         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerInfo.capturerFlags);
83     }
84 }
85 
WriteAudioStreamInfoParams(MessageParcel & data,const AudioStreamInfo & audioStreamInfo)86 void AudioPolicyProxy::WriteAudioStreamInfoParams(MessageParcel &data, const AudioStreamInfo &audioStreamInfo)
87 {
88     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.samplingRate));
89     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.channels));
90     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.format));
91     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.encoding));
92 }
93 
GetMaxVolumeLevel(AudioVolumeType volumeType)94 int32_t AudioPolicyProxy::GetMaxVolumeLevel(AudioVolumeType volumeType)
95 {
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option;
99 
100     if (!data.WriteInterfaceToken(GetDescriptor())) {
101         AUDIO_ERR_LOG("GetMaxVolumeLevel: WriteInterfaceToken failed");
102         return -1;
103     }
104     data.WriteInt32(static_cast<int32_t>(volumeType));
105     int32_t error = Remote()->SendRequest(
106         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_VOLUMELEVEL), data, reply, option);
107     if (error != ERR_NONE) {
108         AUDIO_ERR_LOG("get max volume failed, error: %d", error);
109         return error;
110     }
111     return reply.ReadInt32();
112 }
113 
GetMinVolumeLevel(AudioVolumeType volumeType)114 int32_t AudioPolicyProxy::GetMinVolumeLevel(AudioVolumeType volumeType)
115 {
116     MessageParcel data;
117     MessageParcel reply;
118     MessageOption option;
119 
120     if (!data.WriteInterfaceToken(GetDescriptor())) {
121         AUDIO_ERR_LOG("GetMinVolumeLevel: WriteInterfaceToken failed");
122         return -1;
123     }
124     data.WriteInt32(static_cast<int32_t>(volumeType));
125     int32_t error = Remote()->SendRequest(
126         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MIN_VOLUMELEVEL), data, reply, option);
127     if (error != ERR_NONE) {
128         AUDIO_ERR_LOG("get min volume failed, error: %d", error);
129         return error;
130     }
131     return reply.ReadInt32();
132 }
133 
SetSystemVolumeLevel(AudioStreamType streamType,int32_t volumeLevel,API_VERSION api_v)134 int32_t AudioPolicyProxy::SetSystemVolumeLevel(AudioStreamType streamType, int32_t volumeLevel, API_VERSION api_v)
135 {
136     MessageParcel data;
137     MessageParcel reply;
138     MessageOption option;
139     if (!data.WriteInterfaceToken(GetDescriptor())) {
140         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
141         return -1;
142     }
143 
144     data.WriteInt32(static_cast<int32_t>(streamType));
145     data.WriteInt32(volumeLevel);
146     data.WriteInt32(static_cast<int32_t>(api_v));
147     int32_t error = Remote()->SendRequest(
148         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SYSTEM_VOLUMELEVEL), data, reply, option);
149     if (error != ERR_NONE) {
150         AUDIO_ERR_LOG("set volume failed, error: %d", error);
151         return error;
152     }
153     return reply.ReadInt32();
154 }
155 
SetRingerMode(AudioRingerMode ringMode,API_VERSION api_v)156 int32_t AudioPolicyProxy::SetRingerMode(AudioRingerMode ringMode, API_VERSION api_v)
157 {
158     MessageParcel data;
159     MessageParcel reply;
160     MessageOption option;
161 
162     if (!data.WriteInterfaceToken(GetDescriptor())) {
163         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
164         return -1;
165     }
166     data.WriteInt32(static_cast<int>(ringMode));
167     data.WriteInt32(static_cast<int32_t>(api_v));
168     int32_t error = Remote()->SendRequest(
169         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_RINGER_MODE), data, reply, option);
170     if (error != ERR_NONE) {
171         AUDIO_ERR_LOG("set ringermode failed, error: %d", error);
172         return error;
173     }
174 
175     return reply.ReadInt32();
176 }
177 
178 #ifdef FEATURE_DTMF_TONE
GetSupportedTones()179 std::vector<int32_t> AudioPolicyProxy::GetSupportedTones()
180 {
181     MessageParcel data;
182     MessageParcel reply;
183     MessageOption option;
184     int32_t lListSize = 0;
185     AUDIO_DEBUG_LOG("get GetSupportedTones,");
186     std::vector<int> lSupportedToneList = {};
187     if (!data.WriteInterfaceToken(GetDescriptor())) {
188         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
189         return lSupportedToneList;
190     }
191     int32_t error = Remote()->SendRequest(
192         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SUPPORTED_TONES), data, reply, option);
193     if (error != ERR_NONE) {
194         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
195     }
196     lListSize = reply.ReadInt32();
197     for (int i = 0; i < lListSize; i++) {
198         lSupportedToneList.push_back(reply.ReadInt32());
199     }
200     AUDIO_DEBUG_LOG("get GetSupportedTones, %{public}d", lListSize);
201     return lSupportedToneList;
202 }
203 
GetToneConfig(int32_t ltonetype)204 std::shared_ptr<ToneInfo> AudioPolicyProxy::GetToneConfig(int32_t ltonetype)
205 {
206     MessageParcel data;
207     MessageParcel reply;
208     MessageOption option;
209     std::shared_ptr<ToneInfo> spToneInfo =  std::make_shared<ToneInfo>();
210     if (spToneInfo == nullptr) {
211         return nullptr;
212     }
213     if (!data.WriteInterfaceToken(GetDescriptor())) {
214         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
215         return spToneInfo;
216     }
217     data.WriteInt32(ltonetype);
218     int32_t error = Remote()->SendRequest(
219         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_TONEINFO), data, reply, option);
220     if (error != ERR_NONE) {
221         AUDIO_ERR_LOG("get toneinfo failed, error: %d", error);
222     }
223 
224     spToneInfo->segmentCnt = reply.ReadUint32();
225     spToneInfo->repeatCnt = reply.ReadUint32();
226     spToneInfo->repeatSegment = reply.ReadUint32();
227     AUDIO_DEBUG_LOG("segmentCnt: %{public}d, repeatCnt: %{public}d, repeatSegment: %{public}d",
228         spToneInfo->segmentCnt, spToneInfo->repeatCnt, spToneInfo->repeatSegment);
229     for (uint32_t i = 0; i<spToneInfo->segmentCnt; i++) {
230         spToneInfo->segments[i].duration = reply.ReadUint32();
231         spToneInfo->segments[i].loopCnt = reply.ReadUint16();
232         spToneInfo->segments[i].loopIndx = reply.ReadUint16();
233         AUDIO_DEBUG_LOG("seg[%{public}d].duration: %{public}d, seg[%{public}d].loopCnt: %{public}d, \
234             seg[%{public}d].loopIndex: %{public}d", i, spToneInfo->segments[i].duration,
235             i, spToneInfo->segments[i].loopCnt, i, spToneInfo->segments[i].loopIndx);
236         for (uint32_t j = 0; j < TONEINFO_MAX_WAVES+1; j++) {
237             spToneInfo->segments[i].waveFreq[j] = reply.ReadUint16();
238             AUDIO_DEBUG_LOG("wave[%{public}d]: %{public}d", j, spToneInfo->segments[i].waveFreq[j]);
239         }
240     }
241     AUDIO_DEBUG_LOG("get rGetToneConfig returned,");
242     return spToneInfo;
243 }
244 #endif
245 
SetMicrophoneMute(bool isMute)246 int32_t AudioPolicyProxy::SetMicrophoneMute(bool isMute)
247 {
248     MessageParcel data;
249     MessageParcel reply;
250     MessageOption option;
251 
252     if (!data.WriteInterfaceToken(GetDescriptor())) {
253         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
254         return -1;
255     }
256     data.WriteBool(isMute);
257     int32_t error = Remote()->SendRequest(
258         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE), data, reply, option);
259     if (error != ERR_NONE) {
260         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
261         return error;
262     }
263 
264     return reply.ReadInt32();
265 }
266 
SetMicrophoneMuteAudioConfig(bool isMute)267 int32_t AudioPolicyProxy::SetMicrophoneMuteAudioConfig(bool isMute)
268 {
269     MessageParcel data;
270     MessageParcel reply;
271     MessageOption option;
272 
273     if (!data.WriteInterfaceToken(GetDescriptor())) {
274         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
275         return -1;
276     }
277     data.WriteBool(isMute);
278     int32_t error = Remote()->SendRequest(
279         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE_AUDIO_CONFIG), data, reply, option);
280     if (error != ERR_NONE) {
281         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
282         return error;
283     }
284 
285     return reply.ReadInt32();
286 }
287 
IsMicrophoneMute(API_VERSION api_v)288 bool AudioPolicyProxy::IsMicrophoneMute(API_VERSION api_v)
289 {
290     MessageParcel data;
291     MessageParcel reply;
292     MessageOption option;
293 
294     if (!data.WriteInterfaceToken(GetDescriptor())) {
295         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
296         return -1;
297     }
298     data.WriteInt32(static_cast<int32_t>(api_v));
299     int32_t error = Remote()->SendRequest(
300         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_MICROPHONE_MUTE), data, reply, option);
301     if (error != ERR_NONE) {
302         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
303         return error;
304     }
305 
306     return reply.ReadBool();
307 }
308 
GetRingerMode()309 AudioRingerMode AudioPolicyProxy::GetRingerMode()
310 {
311     MessageParcel data;
312     MessageParcel reply;
313     MessageOption option;
314 
315     if (!data.WriteInterfaceToken(GetDescriptor())) {
316         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
317         return RINGER_MODE_NORMAL;
318     }
319     int32_t error = Remote()->SendRequest(
320         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_RINGER_MODE), data, reply, option);
321     if (error != ERR_NONE) {
322         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
323     }
324     return static_cast<AudioRingerMode>(reply.ReadInt32());
325 }
326 
SetAudioScene(AudioScene scene)327 int32_t AudioPolicyProxy::SetAudioScene(AudioScene scene)
328 {
329     MessageParcel data;
330     MessageParcel reply;
331     MessageOption option;
332 
333     if (!data.WriteInterfaceToken(GetDescriptor())) {
334         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
335         return -1;
336     }
337     data.WriteInt32(static_cast<int>(scene));
338     int32_t error = Remote()->SendRequest(
339         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_AUDIO_SCENE), data, reply, option);
340     if (error != ERR_NONE) {
341         AUDIO_ERR_LOG("set audio scene failed, error: %d", error);
342         return error;
343     }
344 
345     return reply.ReadInt32();
346 }
347 
GetAudioScene()348 AudioScene AudioPolicyProxy::GetAudioScene()
349 {
350     MessageParcel data;
351     MessageParcel reply;
352     MessageOption option;
353 
354     if (!data.WriteInterfaceToken(GetDescriptor())) {
355         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
356         return AUDIO_SCENE_DEFAULT;
357     }
358     int32_t error = Remote()->SendRequest(
359         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_SCENE), data, reply, option);
360     if (error != ERR_NONE) {
361         AUDIO_ERR_LOG("get audio scene failed, error: %d", error);
362     }
363     return static_cast<AudioScene>(reply.ReadInt32());
364 }
365 
GetSystemVolumeLevel(AudioStreamType streamType)366 int32_t AudioPolicyProxy::GetSystemVolumeLevel(AudioStreamType streamType)
367 {
368     MessageParcel data;
369     MessageParcel reply;
370     MessageOption option;
371 
372     if (!data.WriteInterfaceToken(GetDescriptor())) {
373         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
374         return -1;
375     }
376     data.WriteInt32(static_cast<int32_t>(streamType));
377     int32_t error = Remote()->SendRequest(
378         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_VOLUMELEVEL), data, reply, option);
379     if (error != ERR_NONE) {
380         AUDIO_ERR_LOG("get volume failed, error: %d", error);
381         return error;
382     }
383     return reply.ReadInt32();
384 }
385 
SetLowPowerVolume(int32_t streamId,float volume)386 int32_t AudioPolicyProxy::SetLowPowerVolume(int32_t streamId, float volume)
387 {
388     MessageParcel data;
389     MessageParcel reply;
390     MessageOption option;
391     if (!data.WriteInterfaceToken(GetDescriptor())) {
392         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
393         return -1;
394     }
395 
396     data.WriteInt32(streamId);
397     data.WriteFloat(volume);
398     int32_t error = Remote()->SendRequest(
399         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_LOW_POWER_STREM_VOLUME), data, reply, option);
400     if (error != ERR_NONE) {
401         AUDIO_ERR_LOG("set low power stream volume failed, error: %d", error);
402         return error;
403     }
404     return reply.ReadInt32();
405 }
406 
GetLowPowerVolume(int32_t streamId)407 float AudioPolicyProxy::GetLowPowerVolume(int32_t streamId)
408 {
409     MessageParcel data;
410     MessageParcel reply;
411     MessageOption option;
412 
413     if (!data.WriteInterfaceToken(GetDescriptor())) {
414         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
415         return -1;
416     }
417     data.WriteInt32(streamId);
418     int32_t error = Remote()->SendRequest(
419         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_LOW_POWRR_STREM_VOLUME), data, reply, option);
420     if (error != ERR_NONE) {
421         AUDIO_ERR_LOG("get low power stream volume failed, error: %d", error);
422         return error;
423     }
424     return reply.ReadFloat();
425 }
426 
GetSingleStreamVolume(int32_t streamId)427 float AudioPolicyProxy::GetSingleStreamVolume(int32_t streamId)
428 {
429     MessageParcel data;
430     MessageParcel reply;
431     MessageOption option;
432 
433     if (!data.WriteInterfaceToken(GetDescriptor())) {
434         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
435         return -1;
436     }
437     data.WriteInt32(streamId);
438     int32_t error = Remote()->SendRequest(
439         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SINGLE_STREAM_VOLUME), data, reply, option);
440     if (error != ERR_NONE) {
441         AUDIO_ERR_LOG("get single stream volume failed, error: %d", error);
442         return error;
443     }
444     return reply.ReadFloat();
445 }
446 
SetStreamMute(AudioStreamType streamType,bool mute,API_VERSION api_v)447 int32_t AudioPolicyProxy::SetStreamMute(AudioStreamType streamType, bool mute, API_VERSION api_v)
448 {
449     MessageParcel data;
450     MessageParcel reply;
451     MessageOption option;
452 
453     if (!data.WriteInterfaceToken(GetDescriptor())) {
454         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
455         return -1;
456     }
457     data.WriteInt32(static_cast<int32_t>(streamType));
458     data.WriteBool(mute);
459     data.WriteInt32(static_cast<int32_t>(api_v));
460     int32_t error = Remote()->SendRequest(
461         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_STREAM_MUTE), data, reply, option);
462     if (error != ERR_NONE) {
463         AUDIO_ERR_LOG("set mute failed, error: %d", error);
464         return error;
465     }
466     return reply.ReadInt32();
467 }
468 
GetStreamMute(AudioStreamType streamType)469 bool AudioPolicyProxy::GetStreamMute(AudioStreamType streamType)
470 {
471     MessageParcel data;
472     MessageParcel reply;
473     MessageOption option;
474 
475     if (!data.WriteInterfaceToken(GetDescriptor())) {
476         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
477         return false;
478     }
479     data.WriteInt32(static_cast<int32_t>(streamType));
480     int32_t error = Remote()->SendRequest(
481         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_STREAM_MUTE), data, reply, option);
482     if (error != ERR_NONE) {
483         AUDIO_ERR_LOG("get mute failed, error: %d", error);
484         return false;
485     }
486     return reply.ReadBool();
487 }
488 
IsStreamActive(AudioStreamType streamType)489 bool AudioPolicyProxy::IsStreamActive(AudioStreamType streamType)
490 {
491     MessageParcel data;
492     MessageParcel reply;
493     MessageOption option;
494 
495     if (!data.WriteInterfaceToken(GetDescriptor())) {
496         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
497         return false;
498     }
499     data.WriteInt32(static_cast<int32_t>(streamType));
500     int32_t error = Remote()->SendRequest(
501         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_STREAM_ACTIVE), data, reply, option);
502     if (error != ERR_NONE) {
503         AUDIO_ERR_LOG("isStreamActive failed, error: %d", error);
504         return false;
505     }
506     return reply.ReadBool();
507 }
508 
GetDevices(DeviceFlag deviceFlag)509 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetDevices(DeviceFlag deviceFlag)
510 {
511     MessageParcel data;
512     MessageParcel reply;
513     MessageOption option;
514     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
515 
516     if (!data.WriteInterfaceToken(GetDescriptor())) {
517         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
518         return deviceInfo;
519     }
520     data.WriteInt32(static_cast<int32_t>(deviceFlag));
521     int32_t error = Remote()->SendRequest(
522         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_DEVICES), data, reply, option);
523     if (error != ERR_NONE) {
524         AUDIO_ERR_LOG("Get devices failed, error: %d", error);
525         return deviceInfo;
526     }
527 
528     int32_t size = reply.ReadInt32();
529     for (int32_t i = 0; i < size; i++) {
530         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
531     }
532 
533     return deviceInfo;
534 }
535 
SetWakeUpAudioCapturer(InternalAudioCapturerOptions options)536 int32_t AudioPolicyProxy::SetWakeUpAudioCapturer(InternalAudioCapturerOptions options)
537 {
538     MessageParcel data;
539     MessageParcel reply;
540     MessageOption option;
541 
542     if (!data.WriteInterfaceToken(GetDescriptor())) {
543         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
544         return -1;
545     }
546     data.WriteInt32(static_cast<int32_t>(options.streamInfo.samplingRate));
547     data.WriteInt32(static_cast<int32_t>(options.streamInfo.encoding));
548     data.WriteInt32(static_cast<int32_t>(options.streamInfo.format));
549     data.WriteInt32(static_cast<int32_t>(options.streamInfo.channels));
550 
551     data.WriteInt32(static_cast<int32_t>(options.capturerInfo.sourceType));
552     data.WriteInt32(options.capturerInfo.capturerFlags);
553     int32_t error = Remote()->SendRequest(
554         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_WAKEUP_AUDIOCAPTURER), data, reply, option);
555     if (error != ERR_NONE) {
556         AUDIO_ERR_LOG("CreateWakeUpAudioCapturer failed, error: %d", error);
557         return -1;
558     }
559     return reply.ReadInt32();
560 }
561 
CloseWakeUpAudioCapturer()562 int32_t AudioPolicyProxy::CloseWakeUpAudioCapturer()
563 {
564     MessageParcel data;
565     MessageParcel reply;
566     MessageOption option;
567 
568     if (!data.WriteInterfaceToken(GetDescriptor())) {
569         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
570         return -1;
571     }
572     int32_t error = Remote()->SendRequest(
573         static_cast<uint32_t>(AudioPolicyInterfaceCode::CLOSE_WAKEUP_AUDIOCAPTURER), data, reply, option);
574     if (error != ERR_NONE) {
575         AUDIO_ERR_LOG("CloseWakeUpAudioCapturer failed, error: %d", error);
576         return -1;
577     }
578     return reply.ReadInt32();
579 }
GetPreferredOutputDeviceDescriptors(AudioRendererInfo & rendererInfo)580 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetPreferredOutputDeviceDescriptors(
581     AudioRendererInfo &rendererInfo)
582 {
583     MessageParcel data;
584     MessageParcel reply;
585     MessageOption option;
586     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
587 
588     if (!data.WriteInterfaceToken(GetDescriptor())) {
589         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
590         return deviceInfo;
591     }
592     sptr<AudioRendererFilter> audioRendererFilter = new(std::nothrow) AudioRendererFilter();
593     audioRendererFilter->uid = -1;
594     int32_t error = Remote()->SendRequest(
595         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_OUTPUT_DEVICE_DESCRIPTORS), data, reply, option);
596     if (error != ERR_NONE) {
597         AUDIO_ERR_LOG("Get out devices failed, error: %d", error);
598         return deviceInfo;
599     }
600 
601     int32_t size = reply.ReadInt32();
602     for (int32_t i = 0; i < size; i++) {
603         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
604     }
605 
606     return deviceInfo;
607 }
608 
GetPreferredInputDeviceDescriptors(AudioCapturerInfo & captureInfo)609 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetPreferredInputDeviceDescriptors(
610     AudioCapturerInfo &captureInfo)
611 {
612     MessageParcel data;
613     MessageParcel reply;
614     MessageOption option;
615     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
616 
617     if (!data.WriteInterfaceToken(GetDescriptor())) {
618         AUDIO_ERR_LOG("GetPreferredInputDeviceDescriptors: WriteInterfaceToken failed");
619         return deviceInfo;
620     }
621 
622     int32_t error = Remote()->SendRequest(
623         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_PREFERRED_INTPUT_DEVICE_DESCRIPTORS), data, reply, option);
624     if (error != ERR_NONE) {
625         AUDIO_ERR_LOG("Get preferred input devices failed, error: %d", error);
626         return deviceInfo;
627     }
628 
629     int32_t size = reply.ReadInt32();
630     for (int32_t i = 0; i < size; i++) {
631         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
632     }
633 
634     return deviceInfo;
635 }
636 
SetDeviceActive(InternalDeviceType deviceType,bool active)637 int32_t AudioPolicyProxy::SetDeviceActive(InternalDeviceType deviceType, bool active)
638 {
639     MessageParcel data;
640     MessageParcel reply;
641     MessageOption option;
642 
643     if (!data.WriteInterfaceToken(GetDescriptor())) {
644         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
645         return -1;
646     }
647     data.WriteInt32(static_cast<int32_t>(deviceType));
648     data.WriteBool(active);
649     int32_t error = Remote()->SendRequest(
650         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DEVICE_ACTIVE), data, reply, option);
651     if (error != ERR_NONE) {
652         AUDIO_ERR_LOG("set device active failed, error: %d", error);
653         return error;
654     }
655     return reply.ReadInt32();
656 }
657 
IsDeviceActive(InternalDeviceType deviceType)658 bool AudioPolicyProxy::IsDeviceActive(InternalDeviceType deviceType)
659 {
660     MessageParcel data;
661     MessageParcel reply;
662     MessageOption option;
663 
664     if (!data.WriteInterfaceToken(GetDescriptor())) {
665         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
666         return false;
667     }
668     data.WriteInt32(static_cast<int32_t>(deviceType));
669     int32_t error = Remote()->SendRequest(
670         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_DEVICE_ACTIVE), data, reply, option);
671     if (error != ERR_NONE) {
672         AUDIO_ERR_LOG("is device active failed, error: %d", error);
673         return false;
674     }
675     return reply.ReadBool();
676 }
677 
GetActiveOutputDevice()678 DeviceType AudioPolicyProxy::GetActiveOutputDevice()
679 {
680     MessageParcel data;
681     MessageParcel reply;
682     MessageOption option;
683 
684     if (!data.WriteInterfaceToken(GetDescriptor())) {
685         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
686         return DEVICE_TYPE_INVALID;
687     }
688 
689     int32_t error = Remote()->SendRequest(
690         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_OUTPUT_DEVICE), data, reply, option);
691     if (error != ERR_NONE) {
692         AUDIO_ERR_LOG("get active output device failed, error: %d", error);
693         return DEVICE_TYPE_INVALID;
694     }
695 
696     return static_cast<DeviceType>(reply.ReadInt32());
697 }
698 
GetActiveInputDevice()699 DeviceType AudioPolicyProxy::GetActiveInputDevice()
700 {
701     MessageParcel data;
702     MessageParcel reply;
703     MessageOption option;
704 
705     if (!data.WriteInterfaceToken(GetDescriptor())) {
706         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
707         return DEVICE_TYPE_INVALID;
708     }
709 
710     int32_t error = Remote()->SendRequest(
711         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ACTIVE_INPUT_DEVICE), data, reply, option);
712     if (error != ERR_NONE) {
713         AUDIO_ERR_LOG("get active input device failed, error: %d", error);
714         return DEVICE_TYPE_INVALID;
715     }
716 
717     return static_cast<DeviceType>(reply.ReadInt32());
718 }
719 
SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)720 int32_t AudioPolicyProxy::SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
721     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
722 {
723     MessageParcel data;
724     MessageParcel reply;
725     MessageOption option;
726 
727     if (!data.WriteInterfaceToken(GetDescriptor())) {
728         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
729         return -1;
730     }
731     if (!audioRendererFilter->Marshalling(data)) {
732         AUDIO_ERR_LOG("AudioRendererFilter Marshalling() failed");
733         return -1;
734     }
735     int size = audioDeviceDescriptors.size();
736     int validSize = 20; // Use 20 as limit.
737     if (size <= 0 || size > validSize) {
738         AUDIO_ERR_LOG("SelectOutputDevice get invalid device size.");
739         return -1;
740     }
741     data.WriteInt32(size);
742     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
743         if (!audioDeviceDescriptor->Marshalling(data)) {
744             AUDIO_ERR_LOG("AudioDeviceDescriptor Marshalling() failed");
745             return -1;
746         }
747     }
748     int error = Remote()->SendRequest(
749         static_cast<uint32_t>(AudioPolicyInterfaceCode::SELECT_OUTPUT_DEVICE), data, reply, option);
750     if (error != ERR_NONE) {
751         AUDIO_ERR_LOG("SelectOutputDevice failed, error: %{public}d", error);
752         return error;
753     }
754 
755     return reply.ReadInt32();
756 }
GetSelectedDeviceInfo(int32_t uid,int32_t pid,AudioStreamType streamType)757 std::string AudioPolicyProxy::GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType)
758 {
759     MessageParcel data;
760     MessageParcel reply;
761     MessageOption option;
762 
763     if (!data.WriteInterfaceToken(GetDescriptor())) {
764         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
765         return "";
766     }
767     data.WriteInt32(uid);
768     data.WriteInt32(pid);
769     data.WriteInt32(static_cast<int32_t>(streamType));
770     int error = Remote()->SendRequest(
771         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SELECTED_DEVICE_INFO), data, reply, option);
772     if (error != ERR_NONE) {
773         AUDIO_ERR_LOG("GetSelectedDeviceInfo failed, error: %{public}d", error);
774         return "";
775     }
776 
777     return reply.ReadString();
778 }
779 
SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)780 int32_t AudioPolicyProxy::SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
781     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
782 {
783     MessageParcel data;
784     MessageParcel reply;
785     MessageOption option;
786 
787     if (!data.WriteInterfaceToken(GetDescriptor())) {
788         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
789         return -1;
790     }
791     if (!audioCapturerFilter->Marshalling(data)) {
792         AUDIO_ERR_LOG("AudioCapturerFilter Marshalling() failed");
793         return -1;
794     }
795     int size = audioDeviceDescriptors.size();
796     int validSize = 20; // Use 20 as limit.
797     if (size <= 0 || size > validSize) {
798         AUDIO_ERR_LOG("SelectInputDevice get invalid device size.");
799         return -1;
800     }
801     data.WriteInt32(size);
802     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
803         if (!audioDeviceDescriptor->Marshalling(data)) {
804             AUDIO_ERR_LOG("AudioDeviceDescriptor Marshalling() failed");
805             return -1;
806         }
807     }
808     int error = Remote()->SendRequest(
809         static_cast<uint32_t>(AudioPolicyInterfaceCode::SELECT_INPUT_DEVICE), data, reply, option);
810     if (error != ERR_NONE) {
811         AUDIO_ERR_LOG("SelectInputDevice failed, error: %{public}d", error);
812         return error;
813     }
814 
815     return reply.ReadInt32();
816 }
817 
SetRingerModeCallback(const int32_t clientId,const sptr<IRemoteObject> & object,API_VERSION api_v)818 int32_t AudioPolicyProxy::SetRingerModeCallback(const int32_t clientId,
819     const sptr<IRemoteObject> &object, API_VERSION api_v)
820 {
821     MessageParcel data;
822     MessageParcel reply;
823     MessageOption option;
824 
825     if (object == nullptr) {
826         AUDIO_ERR_LOG("AudioPolicyProxy: SetRingerModeCallback object is null");
827         return ERR_NULL_OBJECT;
828     }
829     if (!data.WriteInterfaceToken(GetDescriptor())) {
830         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
831         return -1;
832     }
833     data.WriteInt32(clientId);
834     (void)data.WriteRemoteObject(object);
835     data.WriteInt32(static_cast<int32_t>(api_v));
836     int error = Remote()->SendRequest(
837         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_RINGERMODE_CALLBACK), data, reply, option);
838     if (error != ERR_NONE) {
839         AUDIO_ERR_LOG("AudioPolicyProxy: set ringermode callback failed, error: %{public}d", error);
840         return error;
841     }
842 
843     return reply.ReadInt32();
844 }
845 
UnsetRingerModeCallback(const int32_t clientId)846 int32_t AudioPolicyProxy::UnsetRingerModeCallback(const int32_t clientId)
847 {
848     MessageParcel data;
849     MessageParcel reply;
850     MessageOption option;
851 
852     if (!data.WriteInterfaceToken(GetDescriptor())) {
853         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
854         return -1;
855     }
856     data.WriteInt32(clientId);
857     int error = Remote()->SendRequest(
858         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_RINGERMODE_CALLBACK), data, reply, option);
859     if (error != ERR_NONE) {
860         AUDIO_ERR_LOG("AudioPolicyProxy: unset ringermode callback failed, error: %{public}d", error);
861         return error;
862     }
863 
864     return reply.ReadInt32();
865 }
866 
SetMicStateChangeCallback(const int32_t clientId,const sptr<IRemoteObject> & object)867 int32_t AudioPolicyProxy::SetMicStateChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object)
868 {
869     MessageParcel data;
870     MessageParcel reply;
871     MessageOption option;
872 
873     if (object == nullptr) {
874         AUDIO_ERR_LOG("AudioPolicyProxy: SetMicStateChangeCallback object is null");
875         return ERR_NULL_OBJECT;
876     }
877     if (!data.WriteInterfaceToken(GetDescriptor())) {
878         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
879         return -1;
880     }
881 
882     data.WriteInt32(clientId);
883     (void)data.WriteRemoteObject(object);
884     int error = Remote()->SendRequest(
885         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MIC_STATE_CHANGE_CALLBACK), data, reply, option);
886     if (error != ERR_NONE) {
887         AUDIO_ERR_LOG("AudioPolicyProxy: SetMicStateChangeCallback failed, error: %{public}d", error);
888         return error;
889     }
890 
891     return reply.ReadInt32();
892 }
893 
SetDeviceChangeCallback(const int32_t clientId,const DeviceFlag flag,const sptr<IRemoteObject> & object)894 int32_t AudioPolicyProxy::SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag,
895     const sptr<IRemoteObject> &object)
896 {
897     MessageParcel data;
898     MessageParcel reply;
899     MessageOption option;
900 
901     if (object == nullptr) {
902         AUDIO_ERR_LOG("AudioPolicyProxy: SetDeviceChangeCallback object is null");
903         return ERR_NULL_OBJECT;
904     }
905     if (!data.WriteInterfaceToken(GetDescriptor())) {
906         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
907         return -1;
908     }
909 
910     data.WriteInt32(clientId);
911     data.WriteInt32(flag);
912     (void)data.WriteRemoteObject(object);
913     int error = Remote()->SendRequest(
914         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DEVICE_CHANGE_CALLBACK), data, reply, option);
915     if (error != ERR_NONE) {
916         AUDIO_ERR_LOG("AudioPolicyProxy: SetDeviceChangeCallback failed, error: %{public}d", error);
917         return error;
918     }
919 
920     return reply.ReadInt32();
921 }
922 
UnsetDeviceChangeCallback(const int32_t clientId,DeviceFlag flag)923 int32_t AudioPolicyProxy::UnsetDeviceChangeCallback(const int32_t clientId, DeviceFlag flag)
924 {
925     MessageParcel data;
926     MessageParcel reply;
927     MessageOption option;
928 
929     if (!data.WriteInterfaceToken(GetDescriptor())) {
930         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
931         return -1;
932     }
933     data.WriteInt32(clientId);
934     data.WriteInt32(flag);
935     int error = Remote()->SendRequest(
936         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_DEVICE_CHANGE_CALLBACK), data, reply, option);
937     if (error != ERR_NONE) {
938         AUDIO_ERR_LOG("AudioPolicyProxy: unset device change callback failed, error: %{public}d", error);
939         return error;
940     }
941 
942     return reply.ReadInt32();
943 }
944 
SetPreferredOutputDeviceChangeCallback(const int32_t clientId,const sptr<IRemoteObject> & object)945 int32_t AudioPolicyProxy::SetPreferredOutputDeviceChangeCallback(const int32_t clientId,
946     const sptr<IRemoteObject> &object)
947 {
948     MessageParcel data;
949     MessageParcel reply;
950     MessageOption option;
951 
952     if (object == nullptr) {
953         AUDIO_ERR_LOG("SetPreferredOutputDeviceChangeCallback object is null");
954         return ERR_NULL_OBJECT;
955     }
956     if (!data.WriteInterfaceToken(GetDescriptor())) {
957         AUDIO_ERR_LOG("WriteInterfaceToken failed");
958         return -1;
959     }
960 
961     data.WriteInt32(clientId);
962     (void)data.WriteRemoteObject(object);
963     int error = Remote()->SendRequest(
964         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_ACTIVE_OUTPUT_DEVICE_CHANGE_CALLBACK), data, reply, option);
965     if (error != ERR_NONE) {
966         AUDIO_ERR_LOG("SetPreferredOutputDeviceChangeCallback failed, error: %{public}d", error);
967         return error;
968     }
969 
970     return reply.ReadInt32();
971 }
972 
SetPreferredInputDeviceChangeCallback(const sptr<IRemoteObject> & object)973 int32_t AudioPolicyProxy::SetPreferredInputDeviceChangeCallback(const sptr<IRemoteObject> &object)
974 {
975     MessageParcel data;
976     MessageParcel reply;
977     MessageOption option;
978 
979     if (object == nullptr) {
980         AUDIO_ERR_LOG("SetPreferredInputDeviceChangeCallback object is null");
981         return ERR_NULL_OBJECT;
982     }
983     if (!data.WriteInterfaceToken(GetDescriptor())) {
984         AUDIO_ERR_LOG("WriteInterfaceToken failed");
985         return -1;
986     }
987 
988     (void)data.WriteRemoteObject(object);
989     int error = Remote()->SendRequest(
990         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_ACTIVE_INPUT_DEVICE_CHANGE_CALLBACK), data, reply, option);
991     if (error != ERR_NONE) {
992         AUDIO_ERR_LOG("SetPreferredInputDeviceChangeCallback failed, error: %{public}d", error);
993         return error;
994     }
995 
996     return reply.ReadInt32();
997 }
998 
UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId)999 int32_t AudioPolicyProxy::UnsetPreferredOutputDeviceChangeCallback(const int32_t clientId)
1000 {
1001     MessageParcel data;
1002     MessageParcel reply;
1003     MessageOption option;
1004 
1005     if (!data.WriteInterfaceToken(GetDescriptor())) {
1006         AUDIO_ERR_LOG("WriteInterfaceToken failed");
1007         return -1;
1008     }
1009     data.WriteInt32(clientId);
1010     int error = Remote()->SendRequest(
1011         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_ACTIVE_OUTPUT_DEVICE_CHANGE_CALLBACK),
1012         data, reply, option);
1013     if (error != ERR_NONE) {
1014         AUDIO_ERR_LOG("unset active output device change callback failed, error: %{public}d", error);
1015         return error;
1016     }
1017 
1018     return reply.ReadInt32();
1019 }
1020 
UnsetPreferredInputDeviceChangeCallback()1021 int32_t AudioPolicyProxy::UnsetPreferredInputDeviceChangeCallback()
1022 {
1023     MessageParcel data;
1024     MessageParcel reply;
1025     MessageOption option;
1026 
1027     if (!data.WriteInterfaceToken(GetDescriptor())) {
1028         AUDIO_ERR_LOG("WriteInterfaceToken failed");
1029         return -1;
1030     }
1031     int error = Remote()->SendRequest(
1032         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_ACTIVE_INPUT_DEVICE_CHANGE_CALLBACK),
1033         data, reply, option);
1034     if (error != ERR_NONE) {
1035         AUDIO_ERR_LOG("unset active input device change callback failed, error: %{public}d", error);
1036         return error;
1037     }
1038 
1039     return reply.ReadInt32();
1040 }
1041 
ReadAudioFocusInfo(MessageParcel & reply,std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)1042 void AudioPolicyProxy::ReadAudioFocusInfo(MessageParcel &reply,
1043     std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
1044 {
1045     std::pair<AudioInterrupt, AudioFocuState> focusInfo;
1046 
1047     focusInfo.first.streamUsage = static_cast<StreamUsage>(reply.ReadInt32());
1048     focusInfo.first.contentType = static_cast<ContentType>(reply.ReadInt32());
1049     focusInfo.first.audioFocusType.streamType = static_cast<AudioStreamType>(reply.ReadInt32());
1050     focusInfo.first.audioFocusType.sourceType = static_cast<SourceType>(reply.ReadInt32());
1051     focusInfo.first.audioFocusType.isPlay = reply.ReadBool();
1052     focusInfo.first.sessionID = reply.ReadInt32();
1053     focusInfo.first.pauseWhenDucked = reply.ReadBool();
1054     focusInfo.first.mode = static_cast<InterruptMode>(reply.ReadInt32());
1055     focusInfo.second = static_cast<AudioFocuState>(reply.ReadInt32());
1056 
1057     focusInfoList.push_back(focusInfo);
1058 }
1059 
GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)1060 int32_t AudioPolicyProxy::GetAudioFocusInfoList(std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
1061 {
1062     MessageParcel data;
1063     MessageParcel reply;
1064     MessageOption option;
1065 
1066     if (!data.WriteInterfaceToken(GetDescriptor())) {
1067         AUDIO_ERR_LOG(" GetAudioFocusInfoList WriteInterfaceToken failed");
1068         return ERROR;
1069     }
1070     int32_t error = Remote()->SendRequest(
1071         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_FOCUS_INFO_LIST), data, reply, option);
1072     if (error != ERR_NONE) {
1073         AUDIO_ERR_LOG("GetAudioFocusInfoList, error: %d", error);
1074         return error;
1075     }
1076     int32_t ret = reply.ReadInt32();
1077     int32_t size = reply.ReadInt32();
1078     focusInfoList = {};
1079     if (ret < 0) {
1080         return ret;
1081     } else {
1082         for (int32_t i = 0; i < size; i++) {
1083             ReadAudioFocusInfo(reply, focusInfoList);
1084         }
1085         return SUCCESS;
1086     }
1087 }
1088 
RegisterFocusInfoChangeCallback(const int32_t clientId,const sptr<IRemoteObject> & object)1089 int32_t AudioPolicyProxy::RegisterFocusInfoChangeCallback(const int32_t clientId,
1090     const sptr<IRemoteObject> &object)
1091 {
1092     MessageParcel data;
1093     MessageParcel reply;
1094     MessageOption option;
1095 
1096     if (object == nullptr) {
1097         AUDIO_ERR_LOG("RegisterFocusInfoChangeCallback object is null");
1098         return ERR_NULL_OBJECT;
1099     }
1100     if (!data.WriteInterfaceToken(GetDescriptor())) {
1101         AUDIO_ERR_LOG("WriteInterfaceToken failed");
1102         return -1;
1103     }
1104 
1105     data.WriteInt32(clientId);
1106     (void)data.WriteRemoteObject(object);
1107     int error = Remote()->SendRequest(
1108         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_FOCUS_INFO_CHANGE_CALLBACK), data, reply, option);
1109     if (error != ERR_NONE) {
1110         AUDIO_ERR_LOG("RegisterFocusInfoChangeCallback failed, error: %{public}d", error);
1111         return error;
1112     }
1113 
1114     return reply.ReadInt32();
1115 }
1116 
UnregisterFocusInfoChangeCallback(const int32_t clientId)1117 int32_t AudioPolicyProxy::UnregisterFocusInfoChangeCallback(const int32_t clientId)
1118 {
1119     MessageParcel data;
1120     MessageParcel reply;
1121     MessageOption option;
1122 
1123     if (!data.WriteInterfaceToken(GetDescriptor())) {
1124         AUDIO_ERR_LOG("WriteInterfaceToken failed");
1125         return -1;
1126     }
1127     data.WriteInt32(clientId);
1128     int error = Remote()->SendRequest(
1129         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNREGISTER_FOCUS_INFO_CHANGE_CALLBACK),
1130         data, reply, option);
1131     if (error != ERR_NONE) {
1132         AUDIO_ERR_LOG("unset focus info change callback failed, error: %{public}d", error);
1133         return error;
1134     }
1135 
1136     return reply.ReadInt32();
1137 }
1138 
SetAudioInterruptCallback(const uint32_t sessionID,const sptr<IRemoteObject> & object)1139 int32_t AudioPolicyProxy::SetAudioInterruptCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object)
1140 {
1141     MessageParcel data;
1142     MessageParcel reply;
1143     MessageOption option;
1144 
1145     if (object == nullptr) {
1146         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioInterruptCallback object is null");
1147         return ERR_NULL_OBJECT;
1148     }
1149     if (!data.WriteInterfaceToken(GetDescriptor())) {
1150         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1151         return -1;
1152     }
1153     data.WriteUint32(sessionID);
1154     (void)data.WriteRemoteObject(object);
1155     int error = Remote()->SendRequest(
1156         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_CALLBACK), data, reply, option);
1157     if (error != ERR_NONE) {
1158         AUDIO_ERR_LOG("AudioPolicyProxy: set callback failed, error: %{public}d", error);
1159         return error;
1160     }
1161 
1162     return reply.ReadInt32();
1163 }
1164 
UnsetAudioInterruptCallback(const uint32_t sessionID)1165 int32_t AudioPolicyProxy::UnsetAudioInterruptCallback(const uint32_t sessionID)
1166 {
1167     MessageParcel data;
1168     MessageParcel reply;
1169     MessageOption option;
1170 
1171     if (!data.WriteInterfaceToken(GetDescriptor())) {
1172         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1173         return -1;
1174     }
1175     data.WriteUint32(sessionID);
1176     int error = Remote()->SendRequest(
1177         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_CALLBACK), data, reply, option);
1178     if (error != ERR_NONE) {
1179         AUDIO_ERR_LOG("AudioPolicyProxy: unset callback failed, error: %{public}d", error);
1180         return error;
1181     }
1182 
1183     return reply.ReadInt32();
1184 }
1185 
ActivateAudioInterrupt(const AudioInterrupt & audioInterrupt)1186 int32_t AudioPolicyProxy::ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt)
1187 {
1188     MessageParcel data;
1189     MessageParcel reply;
1190     MessageOption option;
1191 
1192     if (!data.WriteInterfaceToken(GetDescriptor())) {
1193         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1194         return -1;
1195     }
1196     WriteAudioInteruptParams(data, audioInterrupt);
1197     int error = Remote()->SendRequest(
1198         static_cast<uint32_t>(AudioPolicyInterfaceCode::ACTIVATE_INTERRUPT), data, reply, option);
1199     if (error != ERR_NONE) {
1200         AUDIO_ERR_LOG("AudioPolicyProxy: activate interrupt failed, error: %{public}d", error);
1201         return error;
1202     }
1203 
1204     return reply.ReadInt32();
1205 }
1206 
DeactivateAudioInterrupt(const AudioInterrupt & audioInterrupt)1207 int32_t AudioPolicyProxy::DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt)
1208 {
1209     MessageParcel data;
1210     MessageParcel reply;
1211     MessageOption option;
1212 
1213     if (!data.WriteInterfaceToken(GetDescriptor())) {
1214         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1215         return -1;
1216     }
1217     WriteAudioInteruptParams(data, audioInterrupt);
1218     int error = Remote()->SendRequest(
1219         static_cast<uint32_t>(AudioPolicyInterfaceCode::DEACTIVATE_INTERRUPT), data, reply, option);
1220     if (error != ERR_NONE) {
1221         AUDIO_ERR_LOG("AudioPolicyProxy: deactivate interrupt failed, error: %{public}d", error);
1222         return error;
1223     }
1224 
1225     return reply.ReadInt32();
1226 }
1227 
SetAudioManagerInterruptCallback(const int32_t clientId,const sptr<IRemoteObject> & object)1228 int32_t AudioPolicyProxy::SetAudioManagerInterruptCallback(const int32_t clientId, const sptr<IRemoteObject> &object)
1229 {
1230     MessageParcel data;
1231     MessageParcel reply;
1232     MessageOption option;
1233 
1234     if (object == nullptr) {
1235         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioManagerInterruptCallback object is null");
1236         return ERR_NULL_OBJECT;
1237     }
1238     if (!data.WriteInterfaceToken(GetDescriptor())) {
1239         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1240         return -1;
1241     }
1242     data.WriteInt32(clientId);
1243     (void)data.WriteRemoteObject(object);
1244     int error = Remote()->SendRequest(
1245         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_INTERRUPT_CALLBACK), data, reply, option);
1246     if (error != ERR_NONE) {
1247         AUDIO_ERR_LOG("AudioPolicyProxy: set callback failed, error: %{public}d", error);
1248         return error;
1249     }
1250 
1251     return reply.ReadInt32();
1252 }
1253 
UnsetAudioManagerInterruptCallback(const int32_t clientId)1254 int32_t AudioPolicyProxy::UnsetAudioManagerInterruptCallback(const int32_t clientId)
1255 {
1256     MessageParcel data;
1257     MessageParcel reply;
1258     MessageOption option;
1259 
1260     if (!data.WriteInterfaceToken(GetDescriptor())) {
1261         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1262         return -1;
1263     }
1264 
1265     data.WriteInt32(clientId);
1266 
1267     int error = Remote()->SendRequest(
1268         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_INTERRUPT_CALLBACK), data, reply, option);
1269     if (error != ERR_NONE) {
1270         AUDIO_ERR_LOG("AudioPolicyProxy: unset callback failed, error: %{public}d", error);
1271         return error;
1272     }
1273 
1274     return reply.ReadInt32();
1275 }
1276 
RequestAudioFocus(const int32_t clientId,const AudioInterrupt & audioInterrupt)1277 int32_t AudioPolicyProxy::RequestAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt)
1278 {
1279     MessageParcel data;
1280     MessageParcel reply;
1281     MessageOption option;
1282 
1283     if (!data.WriteInterfaceToken(GetDescriptor())) {
1284         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1285         return -1;
1286     }
1287 
1288     data.WriteInt32(clientId);
1289     WriteAudioManagerInteruptParams(data, audioInterrupt);
1290 
1291     int error = Remote()->SendRequest(
1292         static_cast<uint32_t>(AudioPolicyInterfaceCode::REQUEST_AUDIO_FOCUS), data, reply, option);
1293     if (error != ERR_NONE) {
1294         AUDIO_ERR_LOG("AudioPolicyProxy: activate interrupt failed, error: %{public}d", error);
1295         return error;
1296     }
1297 
1298     return reply.ReadInt32();
1299 }
1300 
AbandonAudioFocus(const int32_t clientId,const AudioInterrupt & audioInterrupt)1301 int32_t AudioPolicyProxy::AbandonAudioFocus(const int32_t clientId, const AudioInterrupt &audioInterrupt)
1302 {
1303     MessageParcel data;
1304     MessageParcel reply;
1305     MessageOption option;
1306 
1307     if (!data.WriteInterfaceToken(GetDescriptor())) {
1308         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1309         return -1;
1310     }
1311     data.WriteInt32(clientId);
1312     WriteAudioManagerInteruptParams(data, audioInterrupt);
1313 
1314     int error = Remote()->SendRequest(
1315         static_cast<uint32_t>(AudioPolicyInterfaceCode::ABANDON_AUDIO_FOCUS), data, reply, option);
1316     if (error != ERR_NONE) {
1317         AUDIO_ERR_LOG("AudioPolicyProxy: deactivate interrupt failed, error: %{public}d", error);
1318         return error;
1319     }
1320 
1321     return reply.ReadInt32();
1322 }
1323 
GetStreamInFocus()1324 AudioStreamType AudioPolicyProxy::GetStreamInFocus()
1325 {
1326     MessageParcel data;
1327     MessageParcel reply;
1328     MessageOption option;
1329 
1330     if (!data.WriteInterfaceToken(GetDescriptor())) {
1331         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1332         return STREAM_DEFAULT;
1333     }
1334     int32_t error = Remote()->SendRequest(
1335         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_STREAM_IN_FOCUS), data, reply, option);
1336     if (error != ERR_NONE) {
1337         AUDIO_ERR_LOG("get stream in focus failed, error: %d", error);
1338     }
1339     return static_cast<AudioStreamType>(reply.ReadInt32());
1340 }
1341 
GetSessionInfoInFocus(AudioInterrupt & audioInterrupt)1342 int32_t AudioPolicyProxy::GetSessionInfoInFocus(AudioInterrupt &audioInterrupt)
1343 {
1344     MessageParcel data;
1345     MessageParcel reply;
1346     MessageOption option;
1347 
1348     if (!data.WriteInterfaceToken(GetDescriptor())) {
1349         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1350         return -1;
1351     }
1352     int32_t error = Remote()->SendRequest(
1353         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SESSION_INFO_IN_FOCUS), data, reply, option);
1354     if (error != ERR_NONE) {
1355         AUDIO_ERR_LOG("AudioPolicyProxy::GetSessionInfoInFocus failed, error: %d", error);
1356     }
1357     ReadAudioInterruptParams(reply, audioInterrupt);
1358 
1359     return reply.ReadInt32();
1360 }
1361 
SetVolumeKeyEventCallback(const int32_t clientPid,const sptr<IRemoteObject> & object,API_VERSION api_v)1362 int32_t AudioPolicyProxy::SetVolumeKeyEventCallback(const int32_t clientPid,
1363     const sptr<IRemoteObject> &object, API_VERSION api_v)
1364 {
1365     MessageParcel data;
1366     MessageParcel reply;
1367     MessageOption option;
1368 
1369     if (!data.WriteInterfaceToken(GetDescriptor())) {
1370         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1371         return -1;
1372     }
1373     if (object == nullptr) {
1374         AUDIO_ERR_LOG("VolumeKeyEventCallback object is null");
1375         return ERR_NULL_OBJECT;
1376     }
1377 
1378     data.WriteInt32(clientPid);
1379     data.WriteRemoteObject(object);
1380     data.WriteInt32(static_cast<int32_t>(api_v));
1381     int result = Remote()->SendRequest(
1382         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_VOLUME_KEY_EVENT_CALLBACK), data, reply, option);
1383     if (result != ERR_NONE) {
1384         AUDIO_ERR_LOG("SetAudioVolumeKeyEventCallback failed, result: %{public}d", result);
1385         return result;
1386     }
1387 
1388     return reply.ReadInt32();
1389 }
1390 
UnsetVolumeKeyEventCallback(const int32_t clientPid)1391 int32_t AudioPolicyProxy::UnsetVolumeKeyEventCallback(const int32_t clientPid)
1392 {
1393     MessageParcel data;
1394     MessageParcel reply;
1395     MessageOption option;
1396 
1397     if (!data.WriteInterfaceToken(GetDescriptor())) {
1398         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1399         return -1;
1400     }
1401 
1402     data.WriteInt32(clientPid);
1403     int result = Remote()->SendRequest(
1404         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_VOLUME_KEY_EVENT_CALLBACK), data, reply, option);
1405     if (result != ERR_NONE) {
1406         AUDIO_ERR_LOG("UnsetVolumeKeyEventCallback failed, result: %{public}d", result);
1407         return result;
1408     }
1409 
1410     return reply.ReadInt32();
1411 }
1412 
CheckRecordingCreate(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid)1413 bool AudioPolicyProxy::CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid)
1414 {
1415     AUDIO_DEBUG_LOG("CheckRecordingCreate: [tid : %{public}d]", appTokenId);
1416     MessageParcel data;
1417     MessageParcel reply;
1418     MessageOption option;
1419 
1420     if (!data.WriteInterfaceToken(GetDescriptor())) {
1421         AUDIO_ERR_LOG("CheckRecordingCreate: WriteInterfaceToken failed");
1422         return false;
1423     }
1424 
1425     data.WriteUint32(appTokenId);
1426     data.WriteUint64(appFullTokenId);
1427     data.WriteInt32(appUid);
1428 
1429     int result = Remote()->SendRequest(
1430         static_cast<uint32_t>(AudioPolicyInterfaceCode::QUERY_MICROPHONE_PERMISSION), data, reply, option);
1431     if (result != ERR_NONE) {
1432         AUDIO_ERR_LOG("CheckRecordingCreate failed, result: %{public}d", result);
1433         return false;
1434     }
1435 
1436     return reply.ReadBool();
1437 }
1438 
CheckRecordingStateChange(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid,AudioPermissionState state)1439 bool AudioPolicyProxy::CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
1440     AudioPermissionState state)
1441 {
1442     MessageParcel data;
1443     MessageParcel reply;
1444     MessageOption option;
1445 
1446     if (!data.WriteInterfaceToken(GetDescriptor())) {
1447         AUDIO_ERR_LOG("CheckRecordingStateChange: WriteInterfaceToken failed");
1448         return false;
1449     }
1450 
1451     data.WriteUint32(appTokenId);
1452     data.WriteUint64(appFullTokenId);
1453     data.WriteInt32(appUid);
1454     data.WriteInt32(state);
1455 
1456     int result = Remote()->SendRequest(
1457         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_USING_PEMISSION_FROM_PRIVACY), data, reply, option);
1458     if (result != ERR_NONE) {
1459         AUDIO_ERR_LOG("CheckRecordingStateChange failed, result: %{public}d", result);
1460         return false;
1461     }
1462 
1463     return reply.ReadBool();
1464 }
1465 
ReconfigureAudioChannel(const uint32_t & count,DeviceType deviceType)1466 int32_t AudioPolicyProxy::ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType)
1467 {
1468     MessageParcel data;
1469     MessageParcel reply;
1470     MessageOption option;
1471 
1472     if (!data.WriteInterfaceToken(GetDescriptor())) {
1473         AUDIO_ERR_LOG("ReconfigureAudioChannel: WriteInterfaceToken failed");
1474         return IPC_PROXY_ERR;
1475     }
1476 
1477     data.WriteUint32(count);
1478     data.WriteInt32(deviceType);
1479 
1480     int result = Remote()->SendRequest(
1481         static_cast<uint32_t>(AudioPolicyInterfaceCode::RECONFIGURE_CHANNEL), data, reply, option);
1482     if (result != ERR_NONE) {
1483         AUDIO_ERR_LOG("ReconfigureAudioChannel failed, result: %{public}d", result);
1484         return ERR_TRANSACTION_FAILED;
1485     }
1486 
1487     return reply.ReadInt32();
1488 }
1489 
RegisterAudioRendererEventListener(const int32_t clientPid,const sptr<IRemoteObject> & object)1490 int32_t AudioPolicyProxy::RegisterAudioRendererEventListener(const int32_t clientPid, const sptr<IRemoteObject> &object)
1491 {
1492     MessageParcel data;
1493     MessageParcel reply;
1494     MessageOption option;
1495 
1496     if (!data.WriteInterfaceToken(GetDescriptor())) {
1497         AUDIO_ERR_LOG("RegisterAudioRendererEventListener: WriteInterfaceToken failed");
1498         return ERROR;
1499     }
1500     if (object == nullptr) {
1501         AUDIO_ERR_LOG("RegisterAudioRendererEventListener Event object is null");
1502         return ERR_NULL_OBJECT;
1503     }
1504 
1505     data.WriteInt32(clientPid);
1506     data.WriteRemoteObject(object);
1507     int32_t error = Remote() ->SendRequest(
1508         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_PLAYBACK_EVENT), data, reply, option);
1509     if (error != ERR_NONE) {
1510         AUDIO_ERR_LOG("RegisterAudioRendererEventListener register playback event failed , error: %d", error);
1511         return ERROR;
1512     }
1513 
1514     return reply.ReadInt32();
1515 }
1516 
UnregisterAudioRendererEventListener(const int32_t clientPid)1517 int32_t AudioPolicyProxy::UnregisterAudioRendererEventListener(const int32_t clientPid)
1518 {
1519     MessageParcel data;
1520     MessageParcel reply;
1521     MessageOption option;
1522 
1523     if (!data.WriteInterfaceToken(GetDescriptor())) {
1524         AUDIO_ERR_LOG("UnregisterAudioRendererEventListener WriteInterfaceToken failed");
1525         return ERROR;
1526     }
1527 
1528     data.WriteInt32(clientPid);
1529     int32_t error = Remote() ->SendRequest(
1530         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNREGISTER_PLAYBACK_EVENT), data, reply, option);
1531     if (error != ERR_NONE) {
1532         AUDIO_ERR_LOG("UnregisterAudioRendererEventListener unregister playback event failed , error: %d", error);
1533         return ERROR;
1534     }
1535 
1536     return reply.ReadInt32();
1537 }
1538 
GetAudioLatencyFromXml()1539 int32_t AudioPolicyProxy::GetAudioLatencyFromXml()
1540 {
1541     MessageParcel data;
1542     MessageParcel reply;
1543     MessageOption option;
1544 
1545     if (!data.WriteInterfaceToken(GetDescriptor())) {
1546         AUDIO_ERR_LOG("AudioPolicyProxy: GetAudioLatencyFromXml WriteInterfaceToken failed");
1547         return IPC_PROXY_ERR;
1548     }
1549 
1550     int32_t error = Remote()->SendRequest(
1551         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_LATENCY), data, reply, option);
1552     if (error != ERR_NONE) {
1553         AUDIO_ERR_LOG("GetAudioLatencyFromXml, error: %d", error);
1554         return ERR_TRANSACTION_FAILED;
1555     }
1556 
1557     return reply.ReadInt32();
1558 }
1559 
IsVolumeUnadjustable()1560 bool AudioPolicyProxy::IsVolumeUnadjustable()
1561 {
1562     MessageParcel data;
1563     MessageParcel reply;
1564     MessageOption option;
1565 
1566     if (!data.WriteInterfaceToken(GetDescriptor())) {
1567         AUDIO_ERR_LOG("IsVolumeUnadjustable: WriteInterfaceToken failed");
1568         return false;
1569     }
1570     int32_t error = Remote()->SendRequest(
1571         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_VOLUME_UNADJUSTABLE), data, reply, option);
1572     if (error != ERR_NONE) {
1573         AUDIO_ERR_LOG("isvolumeadjustable failed, error: %d", error);
1574     }
1575     return reply.ReadBool();
1576 }
1577 
AdjustVolumeByStep(VolumeAdjustType adjustType)1578 int32_t AudioPolicyProxy::AdjustVolumeByStep(VolumeAdjustType adjustType)
1579 {
1580     MessageParcel data;
1581     MessageParcel reply;
1582     MessageOption option;
1583 
1584     if (!data.WriteInterfaceToken(GetDescriptor())) {
1585         AUDIO_ERR_LOG("AudioPolicyProxy: AdjustVolumeByStep WriteInterfaceToken failed");
1586         return IPC_PROXY_ERR;
1587     }
1588 
1589     data.WriteInt32(adjustType);
1590 
1591     int32_t error = Remote()->SendRequest(
1592         static_cast<uint32_t>(AudioPolicyInterfaceCode::ADJUST_VOLUME_BY_STEP), data, reply, option);
1593     if (error != ERR_NONE) {
1594         AUDIO_ERR_LOG("GetAudioLatencyFromXml, error: %d", error);
1595         return ERR_TRANSACTION_FAILED;
1596     }
1597 
1598     return reply.ReadInt32();
1599 }
1600 
AdjustSystemVolumeByStep(AudioVolumeType volumeType,VolumeAdjustType adjustType)1601 int32_t AudioPolicyProxy::AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType)
1602 {
1603     MessageParcel data;
1604     MessageParcel reply;
1605     MessageOption option;
1606 
1607     if (!data.WriteInterfaceToken(GetDescriptor())) {
1608         AUDIO_ERR_LOG("AudioPolicyProxy: AdjustSystemVolumeByStep WriteInterfaceToken failed");
1609         return IPC_PROXY_ERR;
1610     }
1611     data.WriteInt32(volumeType);
1612     data.WriteInt32(adjustType);
1613 
1614     int32_t error = Remote()->SendRequest(
1615         static_cast<uint32_t>(AudioPolicyInterfaceCode::ADJUST_SYSTEM_VOLUME_BY_STEP), data, reply, option);
1616     if (error != ERR_NONE) {
1617         AUDIO_ERR_LOG("GetAudioLatencyFromXml, error: %d", error);
1618         return ERR_TRANSACTION_FAILED;
1619     }
1620 
1621     return reply.ReadInt32();
1622 }
1623 
GetSystemVolumeInDb(AudioVolumeType volumeType,int32_t volumeLevel,DeviceType deviceType)1624 float AudioPolicyProxy::GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType)
1625 {
1626     MessageParcel data;
1627     MessageParcel reply;
1628     MessageOption option;
1629 
1630     if (!data.WriteInterfaceToken(GetDescriptor())) {
1631         AUDIO_ERR_LOG("AudioPolicyProxy: GetSystemVolumeInDb failed");
1632         return false;
1633     }
1634 
1635     data.WriteInt32(static_cast<int32_t>(volumeType));
1636     data.WriteInt32(volumeLevel);
1637     data.WriteInt32(static_cast<int32_t>(deviceType));
1638 
1639     int32_t error = Remote()->SendRequest(
1640         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_VOLUME_IN_DB), data, reply, option);
1641     if (error != ERR_NONE) {
1642         AUDIO_ERR_LOG("GetSystemVolumeInDb failed, error: %d", error);
1643     }
1644 
1645     return reply.ReadFloat();
1646 }
1647 
GetSinkLatencyFromXml()1648 uint32_t AudioPolicyProxy::GetSinkLatencyFromXml()
1649 {
1650     MessageParcel data;
1651     MessageParcel reply;
1652     MessageOption option;
1653 
1654     if (!data.WriteInterfaceToken(GetDescriptor())) {
1655         AUDIO_ERR_LOG("AudioPolicyProxy: GetSinkLatencyFromXml WriteInterfaceToken failed");
1656         return 0;
1657     }
1658 
1659     int32_t error = Remote()->SendRequest(
1660         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SINK_LATENCY), data, reply, option);
1661     if (error != ERR_NONE) {
1662         AUDIO_ERR_LOG("GetSinkLatencyFromXml, error: %d", error);
1663         return 0;
1664     }
1665 
1666     return reply.ReadUint32();
1667 }
1668 
RegisterAudioCapturerEventListener(const int32_t clientPid,const sptr<IRemoteObject> & object)1669 int32_t AudioPolicyProxy::RegisterAudioCapturerEventListener(const int32_t clientPid, const sptr<IRemoteObject> &object)
1670 {
1671     MessageParcel data;
1672     MessageParcel reply;
1673     MessageOption option;
1674 
1675     if (!data.WriteInterfaceToken(GetDescriptor())) {
1676         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener:: WriteInterfaceToken failed");
1677         return ERROR;
1678     }
1679     if (object == nullptr) {
1680         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener Event object is null");
1681         return ERR_NULL_OBJECT;
1682     }
1683 
1684     data.WriteInt32(clientPid);
1685     data.WriteRemoteObject(object);
1686     int32_t error = Remote() ->SendRequest(
1687         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_RECORDING_EVENT), data, reply, option);
1688     if (error != ERR_NONE) {
1689         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener recording event failed , error: %d", error);
1690         return ERROR;
1691     }
1692 
1693     return reply.ReadInt32();
1694 }
1695 
UnregisterAudioCapturerEventListener(const int32_t clientPid)1696 int32_t AudioPolicyProxy::UnregisterAudioCapturerEventListener(const int32_t clientPid)
1697 {
1698     MessageParcel data;
1699     MessageParcel reply;
1700     MessageOption option;
1701 
1702     if (!data.WriteInterfaceToken(GetDescriptor())) {
1703         AUDIO_ERR_LOG("AudioPolicyProxy:: WriteInterfaceToken failed");
1704         return ERROR;
1705     }
1706 
1707     data.WriteInt32(clientPid);
1708     int32_t error = Remote() ->SendRequest(
1709         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNREGISTER_RECORDING_EVENT), data, reply, option);
1710     if (error != ERR_NONE) {
1711         AUDIO_ERR_LOG("UnregisterAudioCapturerEventListener recording event failed , error: %d", error);
1712         return ERROR;
1713     }
1714 
1715     return reply.ReadInt32();
1716 }
1717 
RegisterTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo,const sptr<IRemoteObject> & object)1718 int32_t AudioPolicyProxy::RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
1719     const sptr<IRemoteObject> &object)
1720 {
1721     MessageParcel data;
1722     MessageParcel reply;
1723     MessageOption option;
1724 
1725     if (!data.WriteInterfaceToken(GetDescriptor())) {
1726         AUDIO_ERR_LOG("RegisterTracker WriteInterfaceToken failed");
1727         return ERROR;
1728     }
1729 
1730     if (object == nullptr) {
1731         AUDIO_ERR_LOG("Register Tracker Event object is null");
1732         return ERR_NULL_OBJECT;
1733     }
1734 
1735     data.WriteUint32(mode);
1736     WriteStreamChangeInfo(data, mode, streamChangeInfo);
1737     data.WriteRemoteObject(object);
1738 
1739     int32_t error = Remote()->SendRequest(
1740         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_TRACKER), data, reply, option);
1741     if (error != ERR_NONE) {
1742         AUDIO_ERR_LOG("RegisterTracker event failed , error: %d", error);
1743         return ERROR;
1744     }
1745 
1746     return reply.ReadInt32();
1747 }
1748 
UpdateTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo)1749 int32_t AudioPolicyProxy::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo)
1750 {
1751     MessageParcel data;
1752     MessageParcel reply;
1753     MessageOption option;
1754 
1755     if (!data.WriteInterfaceToken(GetDescriptor())) {
1756         AUDIO_ERR_LOG("UpdateTracker: WriteInterfaceToken failed");
1757         return ERROR;
1758     }
1759 
1760     data.WriteUint32(mode);
1761     WriteStreamChangeInfo(data, mode, streamChangeInfo);
1762 
1763     int32_t error = Remote()->SendRequest(
1764         static_cast<uint32_t>(AudioPolicyInterfaceCode::UPDATE_TRACKER), data, reply, option);
1765     if (error != ERR_NONE) {
1766         AUDIO_ERR_LOG("UpdateTracker event failed , error: %d", error);
1767         return ERROR;
1768     }
1769 
1770     return reply.ReadInt32();
1771 }
1772 
ReadAudioRendererChangeInfo(MessageParcel & reply,unique_ptr<AudioRendererChangeInfo> & rendererChangeInfo)1773 void AudioPolicyProxy::ReadAudioRendererChangeInfo(MessageParcel &reply,
1774     unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo)
1775 {
1776     rendererChangeInfo->sessionId = reply.ReadInt32();
1777     rendererChangeInfo->rendererState = static_cast<RendererState>(reply.ReadInt32());
1778     rendererChangeInfo->clientUID = reply.ReadInt32();
1779     rendererChangeInfo->tokenId = reply.ReadInt32();
1780 
1781     rendererChangeInfo->rendererInfo.contentType = static_cast<ContentType>(reply.ReadInt32());
1782     rendererChangeInfo->rendererInfo.streamUsage = static_cast<StreamUsage>(reply.ReadInt32());
1783     rendererChangeInfo->rendererInfo.rendererFlags = reply.ReadInt32();
1784 
1785     rendererChangeInfo->outputDeviceInfo.deviceType = static_cast<DeviceType>(reply.ReadInt32());
1786     rendererChangeInfo->outputDeviceInfo.deviceRole = static_cast<DeviceRole>(reply.ReadInt32());
1787     rendererChangeInfo->outputDeviceInfo.deviceId = reply.ReadInt32();
1788     rendererChangeInfo->outputDeviceInfo.channelMasks = reply.ReadInt32();
1789     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.samplingRate
1790         = static_cast<AudioSamplingRate>(reply.ReadInt32());
1791     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.encoding
1792         = static_cast<AudioEncodingType>(reply.ReadInt32());
1793     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.format = static_cast<AudioSampleFormat>(reply.ReadInt32());
1794     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.channels = static_cast<AudioChannel>(reply.ReadInt32());
1795     rendererChangeInfo->outputDeviceInfo.deviceName = reply.ReadString();
1796     rendererChangeInfo->outputDeviceInfo.macAddress = reply.ReadString();
1797     rendererChangeInfo->outputDeviceInfo.displayName = reply.ReadString();
1798     rendererChangeInfo->outputDeviceInfo.networkId = reply.ReadString();
1799     rendererChangeInfo->outputDeviceInfo.interruptGroupId = reply.ReadInt32();
1800     rendererChangeInfo->outputDeviceInfo.volumeGroupId = reply.ReadInt32();
1801     rendererChangeInfo->outputDeviceInfo.isLowLatencyDevice = reply.ReadBool();
1802 }
1803 
ReadAudioCapturerChangeInfo(MessageParcel & reply,unique_ptr<AudioCapturerChangeInfo> & capturerChangeInfo)1804 void AudioPolicyProxy::ReadAudioCapturerChangeInfo(MessageParcel &reply,
1805     unique_ptr<AudioCapturerChangeInfo> &capturerChangeInfo)
1806 {
1807     capturerChangeInfo->sessionId = reply.ReadInt32();
1808     capturerChangeInfo->capturerState = static_cast<CapturerState>(reply.ReadInt32());
1809     capturerChangeInfo->clientUID = reply.ReadInt32();
1810     capturerChangeInfo->capturerInfo.sourceType = static_cast<SourceType>(reply.ReadInt32());
1811     capturerChangeInfo->capturerInfo.capturerFlags = reply.ReadInt32();
1812 
1813     capturerChangeInfo->inputDeviceInfo.deviceType = static_cast<DeviceType>(reply.ReadInt32());
1814     capturerChangeInfo->inputDeviceInfo.deviceRole = static_cast<DeviceRole>(reply.ReadInt32());
1815     capturerChangeInfo->inputDeviceInfo.deviceId = reply.ReadInt32();
1816     capturerChangeInfo->inputDeviceInfo.channelMasks = reply.ReadInt32();
1817     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.samplingRate
1818         = static_cast<AudioSamplingRate>(reply.ReadInt32());
1819     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.encoding
1820         = static_cast<AudioEncodingType>(reply.ReadInt32());
1821     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.format = static_cast<AudioSampleFormat>(reply.ReadInt32());
1822     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.channels = static_cast<AudioChannel>(reply.ReadInt32());
1823     capturerChangeInfo->inputDeviceInfo.deviceName = reply.ReadString();
1824     capturerChangeInfo->inputDeviceInfo.macAddress = reply.ReadString();
1825     capturerChangeInfo->inputDeviceInfo.displayName = reply.ReadString();
1826     capturerChangeInfo->inputDeviceInfo.networkId = reply.ReadString();
1827     capturerChangeInfo->inputDeviceInfo.interruptGroupId = reply.ReadInt32();
1828     capturerChangeInfo->inputDeviceInfo.volumeGroupId = reply.ReadInt32();
1829     capturerChangeInfo->inputDeviceInfo.isLowLatencyDevice = reply.ReadBool();
1830 }
1831 
GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)1832 int32_t AudioPolicyProxy::GetCurrentRendererChangeInfos(
1833     vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
1834 {
1835     MessageParcel data;
1836     MessageParcel reply;
1837     MessageOption option;
1838 
1839     AUDIO_DEBUG_LOG("AudioPolicyProxy::GetCurrentRendererChangeInfos");
1840 
1841     if (!data.WriteInterfaceToken(GetDescriptor())) {
1842         AUDIO_ERR_LOG("GetCurrentRendererChangeInfo: WriteInterfaceToken failed");
1843         return ERROR;
1844     }
1845 
1846     int32_t error = Remote()->SendRequest(
1847         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_RENDERER_CHANGE_INFOS), data, reply, option);
1848     if (error != ERR_NONE) {
1849         AUDIO_ERR_LOG("Get Renderer change info event failed , error: %d", error);
1850         return ERROR;
1851     }
1852 
1853     int32_t size = reply.ReadInt32();
1854     while (size > 0) {
1855         unique_ptr<AudioRendererChangeInfo> rendererChangeInfo = make_unique<AudioRendererChangeInfo>();
1856         CHECK_AND_RETURN_RET_LOG(rendererChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
1857         ReadAudioRendererChangeInfo(reply, rendererChangeInfo);
1858         audioRendererChangeInfos.push_back(move(rendererChangeInfo));
1859         size--;
1860     }
1861 
1862     return SUCCESS;
1863 }
1864 
GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)1865 int32_t AudioPolicyProxy::GetCurrentCapturerChangeInfos(
1866     vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
1867 {
1868     MessageParcel data;
1869     MessageParcel reply;
1870     MessageOption option;
1871 
1872     AUDIO_DEBUG_LOG("AudioPolicyProxy::GetCurrentCapturerChangeInfos");
1873 
1874     if (!data.WriteInterfaceToken(GetDescriptor())) {
1875         AUDIO_ERR_LOG("GetCurrentCapturerChangeInfos: WriteInterfaceToken failed");
1876         return ERROR;
1877     }
1878 
1879     int32_t error = Remote()->SendRequest(
1880         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_CAPTURER_CHANGE_INFOS), data, reply, option);
1881     if (error != ERR_NONE) {
1882         AUDIO_ERR_LOG("Get capturer change info event failed , error: %d", error);
1883         return ERROR;
1884     }
1885 
1886     int32_t size = reply.ReadInt32();
1887     while (size > 0) {
1888         unique_ptr<AudioCapturerChangeInfo> capturerChangeInfo = make_unique<AudioCapturerChangeInfo>();
1889         CHECK_AND_RETURN_RET_LOG(capturerChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
1890         ReadAudioCapturerChangeInfo(reply, capturerChangeInfo);
1891         audioCapturerChangeInfos.push_back(move(capturerChangeInfo));
1892         size--;
1893     }
1894 
1895     return SUCCESS;
1896 }
1897 
UpdateStreamState(const int32_t clientUid,StreamSetState streamSetState,AudioStreamType audioStreamType)1898 int32_t AudioPolicyProxy::UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState,
1899     AudioStreamType audioStreamType)
1900 {
1901     MessageParcel data;
1902     MessageParcel reply;
1903     MessageOption option;
1904 
1905     AUDIO_DEBUG_LOG("AudioPolicyProxy::UpdateStreamState");
1906 
1907     if (!data.WriteInterfaceToken(GetDescriptor())) {
1908         AUDIO_ERR_LOG("UpdateStreamState: WriteInterfaceToken failed");
1909         return ERROR;
1910     }
1911 
1912     data.WriteInt32(static_cast<int32_t>(clientUid));
1913     data.WriteInt32(static_cast<int32_t>(streamSetState));
1914     data.WriteInt32(static_cast<int32_t>(audioStreamType));
1915 
1916     int32_t error = Remote()->SendRequest(
1917         static_cast<uint32_t>(AudioPolicyInterfaceCode::UPDATE_STREAM_STATE), data, reply, option);
1918     if (error != ERR_NONE) {
1919         AUDIO_ERR_LOG("UPDATE_STREAM_STATE stream changed info event failed , error: %d", error);
1920         return ERROR;
1921     }
1922 
1923     return SUCCESS;
1924 }
1925 
GetVolumeGroupInfos(std::string networkId,std::vector<sptr<VolumeGroupInfo>> & infos)1926 int32_t AudioPolicyProxy::GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos)
1927 {
1928     MessageParcel data;
1929     MessageParcel reply;
1930     MessageOption option;
1931 
1932     if (!data.WriteInterfaceToken(GetDescriptor())) {
1933         AUDIO_ERR_LOG(" GetVolumeGroupById WriteInterfaceToken failed");
1934         return ERROR;
1935     }
1936     data.WriteString(networkId);
1937     int32_t error = Remote()->SendRequest(
1938         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_VOLUME_GROUP_INFO), data, reply, option);
1939     if (error != ERR_NONE) {
1940         AUDIO_ERR_LOG("GetVolumeGroupInfo, error: %d", error);
1941         return error;
1942     }
1943 
1944     int32_t ret = reply.ReadInt32();
1945     if (ret > 0) {
1946         for (int32_t i = 0; i < ret; i++) {
1947             infos.push_back(VolumeGroupInfo::Unmarshalling(reply));
1948         }
1949         return SUCCESS;
1950     } else {
1951         return ret;
1952     }
1953 }
1954 
GetNetworkIdByGroupId(int32_t groupId,std::string & networkId)1955 int32_t AudioPolicyProxy::GetNetworkIdByGroupId(int32_t groupId, std::string &networkId)
1956 {
1957     MessageParcel data;
1958     MessageParcel reply;
1959     MessageOption option;
1960 
1961     if (!data.WriteInterfaceToken(GetDescriptor())) {
1962         AUDIO_ERR_LOG(" GetNetworkIdByGroupId WriteInterfaceToken failed");
1963         return ERROR;
1964     }
1965     data.WriteInt32(groupId);
1966     int32_t error = Remote()->SendRequest(
1967         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_NETWORKID_BY_GROUP_ID), data, reply, option);
1968     if (error != ERR_NONE) {
1969         AUDIO_ERR_LOG("GetNetworkIdByGroupId, error: %d", error);
1970         return error;
1971     }
1972 
1973     networkId = reply.ReadString();
1974     int32_t ret = reply.ReadInt32();
1975     return ret;
1976 }
1977 
IsAudioRendererLowLatencySupported(const AudioStreamInfo & audioStreamInfo)1978 bool AudioPolicyProxy::IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo)
1979 {
1980     MessageParcel data;
1981     MessageParcel reply;
1982     MessageOption option;
1983 
1984     if (!data.WriteInterfaceToken(GetDescriptor())) {
1985         AUDIO_ERR_LOG("IsAudioRendererLowLatencySupported WriteInterfaceToken failed");
1986         return IPC_PROXY_ERR;
1987     }
1988     WriteAudioStreamInfoParams(data, audioStreamInfo);
1989     int32_t error = Remote()->SendRequest(
1990         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_AUDIO_RENDER_LOW_LATENCY_SUPPORTED), data, reply, option);
1991     if (error != ERR_NONE) {
1992         AUDIO_ERR_LOG("IsAudioRendererLowLatencySupported, error: %d", error);
1993         return ERR_TRANSACTION_FAILED;
1994     }
1995 
1996     return reply.ReadBool();
1997 }
1998 
SetSystemSoundUri(const std::string & key,const std::string & uri)1999 int32_t AudioPolicyProxy::SetSystemSoundUri(const std::string &key, const std::string &uri)
2000 {
2001     MessageParcel data;
2002     MessageParcel reply;
2003     MessageOption option;
2004 
2005     if (!data.WriteInterfaceToken(GetDescriptor())) {
2006         AUDIO_ERR_LOG("SetSystemSoundUri WriteInterfaceToken failed");
2007         return IPC_PROXY_ERR;
2008     }
2009     data.WriteString(key);
2010     data.WriteString(uri);
2011     int32_t error = Remote()->SendRequest(
2012         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SYSTEM_SOUND_URI), data, reply, option);
2013     if (error != ERR_NONE) {
2014         AUDIO_ERR_LOG("SetSystemSoundUri failed, error: %d", error);
2015         return error;
2016     }
2017     return reply.ReadInt32();
2018 }
2019 
GetSystemSoundUri(const std::string & key)2020 std::string AudioPolicyProxy::GetSystemSoundUri(const std::string &key)
2021 {
2022     MessageParcel data;
2023     MessageParcel reply;
2024     MessageOption option;
2025 
2026     if (!data.WriteInterfaceToken(GetDescriptor())) {
2027         AUDIO_ERR_LOG("GetSystemSoundUri WriteInterfaceToken failed");
2028         return "";
2029     }
2030     data.WriteString(key);
2031     int32_t error = Remote()->SendRequest(
2032         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_SOUND_URI), data, reply, option);
2033     if (error != ERR_NONE) {
2034         AUDIO_ERR_LOG("GetSystemSoundUri failed, error: %d", error);
2035         return "";
2036     }
2037     return reply.ReadString();
2038 }
2039 
GetMinStreamVolume()2040 float AudioPolicyProxy::GetMinStreamVolume()
2041 {
2042     MessageParcel data;
2043     MessageParcel reply;
2044     MessageOption option;
2045 
2046     if (!data.WriteInterfaceToken(GetDescriptor())) {
2047         AUDIO_ERR_LOG("GetMinStreamVolume WriteInterfaceToken failed");
2048         return -1;
2049     }
2050     int32_t error = Remote()->SendRequest(
2051         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MIN_VOLUME_STREAM), data, reply, option);
2052     if (error != ERR_NONE) {
2053         AUDIO_ERR_LOG("get min volume for stream failed, error: %d", error);
2054         return error;
2055     }
2056     return reply.ReadFloat();
2057 }
2058 
GetMaxStreamVolume()2059 float AudioPolicyProxy::GetMaxStreamVolume()
2060 {
2061     MessageParcel data;
2062     MessageParcel reply;
2063     MessageOption option;
2064 
2065     if (!data.WriteInterfaceToken(GetDescriptor())) {
2066         AUDIO_ERR_LOG("GetMaxStreamVolume: WriteInterfaceToken failed");
2067         return -1;
2068     }
2069     int32_t error = Remote()->SendRequest(
2070         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_VOLUME_STREAM), data, reply, option);
2071     if (error != ERR_NONE) {
2072         AUDIO_ERR_LOG("get max volume for stream failed, error: %d", error);
2073         return error;
2074     }
2075     return reply.ReadFloat();
2076 }
2077 
GetMaxRendererInstances()2078 int32_t AudioPolicyProxy::GetMaxRendererInstances()
2079 {
2080     MessageParcel data;
2081     MessageParcel reply;
2082     MessageOption option;
2083 
2084     if (!data.WriteInterfaceToken(GetDescriptor())) {
2085         AUDIO_ERR_LOG("GetMaxRendererInstances WriteInterfaceToken failed");
2086         return ERROR;
2087     }
2088     int32_t error = Remote()->SendRequest(
2089         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_RENDERER_INSTANCES), data, reply, option);
2090     if (error != ERR_NONE) {
2091         AUDIO_ERR_LOG("GetMaxRendererInstances failed, error: %d", error);
2092         return ERROR;
2093     }
2094     return reply.ReadInt32();
2095 }
2096 
PreprocessMode(Stream & stream,MessageParcel & reply,int countMode)2097 static void PreprocessMode(Stream &stream, MessageParcel &reply, int countMode)
2098 {
2099     int j, k;
2100     for (j = 0; j < countMode; j++) {
2101         StreamEffectMode streamEffectMode;
2102         streamEffectMode.mode = reply.ReadString();
2103         int countDev = reply.ReadInt32();
2104         if (countDev > 0) {
2105             for (k = 0; k < countDev; k++) {
2106                 string type = reply.ReadString();
2107                 string chain = reply.ReadString();
2108                 streamEffectMode.devicePort.push_back({type, chain});
2109             }
2110         }
2111         stream.streamEffectMode.push_back(streamEffectMode);
2112     }
2113 }
2114 
PreprocessProcess(MessageParcel & reply)2115 static Stream PreprocessProcess(MessageParcel &reply)
2116 {
2117     Stream stream;
2118     stream.scene = reply.ReadString();
2119     int countMode = reply.ReadInt32();
2120     if (countMode > 0) {
2121         PreprocessMode(stream, reply, countMode);
2122     }
2123     return stream;
2124 }
2125 
PostprocessMode(Stream & stream,MessageParcel & reply,int countMode)2126 static void PostprocessMode(Stream &stream, MessageParcel &reply, int countMode)
2127 {
2128     int j, k;
2129     for (j = 0; j < countMode; j++) {
2130         StreamEffectMode streamEffectMode;
2131         streamEffectMode.mode = reply.ReadString();
2132         int countDev = reply.ReadInt32();
2133         if (countDev > 0) {
2134             for (k = 0; k < countDev; k++) {
2135                 string type = reply.ReadString();
2136                 string chain = reply.ReadString();
2137                 streamEffectMode.devicePort.push_back({type, chain});
2138             }
2139         }
2140         stream.streamEffectMode.push_back(streamEffectMode);
2141     }
2142 }
2143 
PostprocessProcess(MessageParcel & reply)2144 static Stream PostprocessProcess(MessageParcel &reply)
2145 {
2146     Stream stream;
2147     stream.scene = reply.ReadString();
2148     int countMode = reply.ReadInt32();
2149     if (countMode > 0) {
2150         PostprocessMode(stream, reply, countMode);
2151     }
2152     return stream;
2153 }
2154 
QueryEffectSceneModeChkReply(int countPre,int countPost)2155 static int32_t QueryEffectSceneModeChkReply(int countPre, int countPost)
2156 {
2157     if ((countPre < 0) || (countPre > AUDIO_EFFECT_COUNT_UPPER_LIMIT)) {
2158         AUDIO_ERR_LOG("QUERY_EFFECT_SCENEMODE read replyParcel failed");
2159         return -1;
2160     }
2161     if ((countPost < 0) || (countPost > AUDIO_EFFECT_COUNT_UPPER_LIMIT)) {
2162         AUDIO_ERR_LOG("QUERY_EFFECT_SCENEMODE read replyParcel failed");
2163         return -1;
2164     }
2165     return 0;
2166 }
2167 
QueryEffectSceneMode(SupportedEffectConfig & supportedEffectConfig)2168 int32_t AudioPolicyProxy::QueryEffectSceneMode(SupportedEffectConfig &supportedEffectConfig)
2169 {
2170     int i;
2171     int32_t error;
2172     MessageParcel data;
2173     MessageParcel reply;
2174     MessageOption option;
2175     if (!data.WriteInterfaceToken(GetDescriptor())) {
2176         AUDIO_ERR_LOG("QueryEffectSceneMode: WriteInterfaceToken failed");
2177         return -1;
2178     }
2179     error = Remote()->SendRequest(
2180         static_cast<uint32_t>(AudioPolicyInterfaceCode::QUERY_EFFECT_SCENEMODE), data, reply, option);
2181     if (error != ERR_NONE) {
2182         AUDIO_ERR_LOG("get scene & mode failed, error: %d", error);
2183         return error;
2184     }
2185     int countPre = reply.ReadInt32();
2186     int countPost = reply.ReadInt32();
2187     error = QueryEffectSceneModeChkReply(countPre, countPost);
2188     if (error != ERR_NONE) {
2189         AUDIO_ERR_LOG("get scene & mode failed, error: %d", error);
2190         return error;
2191     }
2192     // preprocess
2193     Stream stream;
2194     if (countPre > 0) {
2195         ProcessNew preProcessNew;
2196         for (i = 0; i < countPre; i++) {
2197             stream = PreprocessProcess(reply);
2198             preProcessNew.stream.push_back(stream);
2199         }
2200         supportedEffectConfig.preProcessNew = preProcessNew;
2201     }
2202     // postprocess
2203     if (countPost > 0) {
2204         ProcessNew postProcessNew;
2205         for (i = 0; i < countPost; i++) {
2206             stream = PostprocessProcess(reply);
2207             postProcessNew.stream.push_back(stream);
2208         }
2209         supportedEffectConfig.postProcessNew = postProcessNew;
2210     }
2211     return 0;
2212 }
2213 
SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig & config,uint32_t appTokenId)2214 int32_t AudioPolicyProxy::SetPlaybackCapturerFilterInfos(const AudioPlaybackCaptureConfig &config, uint32_t appTokenId)
2215 {
2216     MessageParcel data;
2217     MessageParcel reply;
2218     MessageOption option;
2219 
2220     if (!data.WriteInterfaceToken(GetDescriptor())) {
2221         AUDIO_ERR_LOG(" SetPlaybackCapturerFilterInfos WriteInterfaceToken failed");
2222         return ERROR;
2223     }
2224     data.WriteInt32(static_cast<int32_t>(config.silentCapture));
2225     size_t ss = config.filterOptions.usages.size();
2226     data.WriteUint32(ss);
2227     for (size_t i = 0; i < ss; i++) {
2228         data.WriteInt32(static_cast<int32_t>(config.filterOptions.usages[i]));
2229     }
2230     data.WriteUint32(appTokenId);
2231 
2232     int32_t error = Remote()->SendRequest(
2233         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_PLAYBACK_CAPTURER_FILTER_INFO), data, reply, option);
2234     if (error != ERR_NONE) {
2235         AUDIO_ERR_LOG("SetPlaybackCapturerFilterInfos failed, error: %d", error);
2236         return ERROR;
2237     }
2238     return reply.ReadInt32();
2239 }
2240 
2241 } // namespace AudioStandard
2242 } // namespace OHOS
2243