• 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.streamType));
34     data.WriteUint32(audioInterrupt.sessionID);
35 }
36 
WriteAudioManagerInteruptParams(MessageParcel & data,const AudioInterrupt & audioInterrupt)37 void AudioPolicyProxy::WriteAudioManagerInteruptParams(MessageParcel &data, const AudioInterrupt &audioInterrupt)
38 {
39     data.WriteInt32(static_cast<int32_t>(audioInterrupt.streamUsage));
40     data.WriteInt32(static_cast<int32_t>(audioInterrupt.contentType));
41     data.WriteInt32(static_cast<int32_t>(audioInterrupt.streamType));
42     data.WriteBool(audioInterrupt.pauseWhenDucked);
43 }
44 
ReadAudioInterruptParams(MessageParcel & reply,AudioInterrupt & audioInterrupt)45 void AudioPolicyProxy::ReadAudioInterruptParams(MessageParcel &reply, AudioInterrupt &audioInterrupt)
46 {
47     audioInterrupt.streamUsage = static_cast<StreamUsage>(reply.ReadInt32());
48     audioInterrupt.contentType = static_cast<ContentType>(reply.ReadInt32());
49     audioInterrupt.streamType = static_cast<AudioStreamType>(reply.ReadInt32());
50     audioInterrupt.sessionID = reply.ReadUint32();
51 }
52 
WriteStreamChangeInfo(MessageParcel & data,const AudioMode & mode,const AudioStreamChangeInfo & streamChangeInfo)53 void AudioPolicyProxy::WriteStreamChangeInfo(MessageParcel &data,
54     const AudioMode &mode, const AudioStreamChangeInfo &streamChangeInfo)
55 {
56     if (mode == AUDIO_MODE_PLAYBACK) {
57         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.sessionId);
58         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererState);
59         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.clientUID);
60         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.contentType);
61         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.streamUsage);
62         data.WriteInt32(streamChangeInfo.audioRendererChangeInfo.rendererInfo.rendererFlags);
63     } else {
64         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.sessionId);
65         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerState);
66         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.clientUID);
67         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerInfo.sourceType);
68         data.WriteInt32(streamChangeInfo.audioCapturerChangeInfo.capturerInfo.capturerFlags);
69     }
70 }
71 
WriteAudioStreamInfoParams(MessageParcel & data,const AudioStreamInfo & audioStreamInfo)72 void AudioPolicyProxy::WriteAudioStreamInfoParams(MessageParcel &data, const AudioStreamInfo &audioStreamInfo)
73 {
74     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.samplingRate));
75     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.channels));
76     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.format));
77     data.WriteInt32(static_cast<int32_t>(audioStreamInfo.encoding));
78 }
79 
SetStreamVolume(AudioStreamType streamType,float volume)80 int32_t AudioPolicyProxy::SetStreamVolume(AudioStreamType streamType, float volume)
81 {
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85     if (!data.WriteInterfaceToken(GetDescriptor())) {
86         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
87         return -1;
88     }
89 
90     data.WriteInt32(static_cast<int32_t>(streamType));
91     data.WriteFloat(volume);
92     int32_t error = Remote()->SendRequest(SET_STREAM_VOLUME, data, reply, option);
93     if (error != ERR_NONE) {
94         AUDIO_ERR_LOG("set volume failed, error: %d", error);
95         return error;
96     }
97     return reply.ReadInt32();
98 }
99 
SetRingerMode(AudioRingerMode ringMode)100 int32_t AudioPolicyProxy::SetRingerMode(AudioRingerMode ringMode)
101 {
102     MessageParcel data;
103     MessageParcel reply;
104     MessageOption option;
105 
106     if (!data.WriteInterfaceToken(GetDescriptor())) {
107         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
108         return -1;
109     }
110     data.WriteInt32(static_cast<int>(ringMode));
111     int32_t error = Remote()->SendRequest(SET_RINGER_MODE, data, reply, option);
112     if (error != ERR_NONE) {
113         AUDIO_ERR_LOG("set ringermode failed, error: %d", error);
114         return error;
115     }
116 
117     return reply.ReadInt32();
118 }
119 
GetSupportedTones()120 std::vector<int32_t> AudioPolicyProxy::GetSupportedTones()
121 {
122     MessageParcel data;
123     MessageParcel reply;
124     MessageOption option;
125     int32_t lListSize = 0;
126     AUDIO_DEBUG_LOG("get GetSupportedTones,");
127     std::vector<int> lSupportedToneList = {};
128     if (!data.WriteInterfaceToken(GetDescriptor())) {
129         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
130         return lSupportedToneList;
131     }
132     int32_t error = Remote()->SendRequest(GET_SUPPORTED_TONES, data, reply, option);
133     if (error != ERR_NONE) {
134         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
135     }
136     lListSize = reply.ReadInt32();
137     for (int i = 0; i < lListSize; i++) {
138         lSupportedToneList.push_back(reply.ReadInt32());
139     }
140     AUDIO_DEBUG_LOG("get GetSupportedTones, %{public}d", lListSize);
141     return lSupportedToneList;
142 }
143 
GetToneConfig(int32_t ltonetype)144 std::shared_ptr<ToneInfo> AudioPolicyProxy::GetToneConfig(int32_t ltonetype)
145 {
146     MessageParcel data;
147     MessageParcel reply;
148     MessageOption option;
149     std::shared_ptr<ToneInfo> spToneInfo =  std::make_shared<ToneInfo>();
150     if (spToneInfo == nullptr) {
151         return nullptr;
152     }
153     if (!data.WriteInterfaceToken(GetDescriptor())) {
154         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
155         return spToneInfo;
156     }
157     data.WriteInt32(ltonetype);
158     int32_t error = Remote()->SendRequest(GET_TONEINFO, data, reply, option);
159     if (error != ERR_NONE) {
160         AUDIO_ERR_LOG("get toneinfo failed, error: %d", error);
161     }
162 
163     spToneInfo->segmentCnt = reply.ReadUint32();
164     spToneInfo->repeatCnt = reply.ReadUint32();
165     spToneInfo->repeatSegment = reply.ReadUint32();
166     AUDIO_INFO_LOG("segmentCnt: %{public}d, repeatCnt: %{public}d, repeatSegment: %{public}d",
167         spToneInfo->segmentCnt, spToneInfo->repeatCnt, spToneInfo->repeatSegment);
168     for (uint32_t i = 0; i<spToneInfo->segmentCnt; i++) {
169         spToneInfo->segments[i].duration = reply.ReadUint32();
170         spToneInfo->segments[i].loopCnt = reply.ReadUint16();
171         spToneInfo->segments[i].loopIndx = reply.ReadUint16();
172         AUDIO_INFO_LOG("seg[%{public}d].duration: %{public}d, seg[%{public}d].loopCnt: %{public}d, seg[%{public}d].loopIndex: %{public}d",
173             i, spToneInfo->segments[i].duration, i, spToneInfo->segments[i].loopCnt, i, spToneInfo->segments[i].loopIndx);
174         for (uint32_t j = 0; j < TONEINFO_MAX_WAVES+1; j++) {
175             spToneInfo->segments[i].waveFreq[j] = reply.ReadUint16();
176             AUDIO_INFO_LOG("wave[%{public}d]: %{public}d", j, spToneInfo->segments[i].waveFreq[j]);
177         }
178     }
179     AUDIO_DEBUG_LOG("get rGetToneConfig returned,");
180     return spToneInfo;
181 }
182 
SetMicrophoneMute(bool isMute)183 int32_t AudioPolicyProxy::SetMicrophoneMute(bool isMute)
184 {
185     MessageParcel data;
186     MessageParcel reply;
187     MessageOption option;
188 
189     if (!data.WriteInterfaceToken(GetDescriptor())) {
190         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
191         return -1;
192     }
193     data.WriteBool(isMute);
194     int32_t error = Remote()->SendRequest(SET_MICROPHONE_MUTE, data, reply, option);
195     if (error != ERR_NONE) {
196         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
197         return error;
198     }
199 
200     return reply.ReadInt32();
201 }
202 
SetMicrophoneMuteAudioConfig(bool isMute)203 int32_t AudioPolicyProxy::SetMicrophoneMuteAudioConfig(bool isMute)
204 {
205     MessageParcel data;
206     MessageParcel reply;
207     MessageOption option;
208 
209     if (!data.WriteInterfaceToken(GetDescriptor())) {
210         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
211         return -1;
212     }
213     data.WriteBool(isMute);
214     int32_t error = Remote()->SendRequest(SET_MICROPHONE_MUTE_AUDIO_CONFIG, data, reply, option);
215     if (error != ERR_NONE) {
216         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
217         return error;
218     }
219 
220     return reply.ReadInt32();
221 }
222 
IsMicrophoneMute(API_VERSION api_v)223 bool AudioPolicyProxy::IsMicrophoneMute(API_VERSION api_v)
224 {
225     MessageParcel data;
226     MessageParcel reply;
227     MessageOption option;
228 
229     if (!data.WriteInterfaceToken(GetDescriptor())) {
230         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
231         return -1;
232     }
233     data.WriteInt32(static_cast<int32_t>(api_v));
234     int32_t error = Remote()->SendRequest(IS_MICROPHONE_MUTE, data, reply, option);
235     if (error != ERR_NONE) {
236         AUDIO_ERR_LOG("set microphoneMute failed, error: %d", error);
237         return error;
238     }
239 
240     return reply.ReadBool();
241 }
242 
GetRingerMode()243 AudioRingerMode AudioPolicyProxy::GetRingerMode()
244 {
245     MessageParcel data;
246     MessageParcel reply;
247     MessageOption option;
248 
249     if (!data.WriteInterfaceToken(GetDescriptor())) {
250         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
251         return RINGER_MODE_NORMAL;
252     }
253     int32_t error = Remote()->SendRequest(GET_RINGER_MODE, data, reply, option);
254     if (error != ERR_NONE) {
255         AUDIO_ERR_LOG("get ringermode failed, error: %d", error);
256     }
257     return static_cast<AudioRingerMode>(reply.ReadInt32());
258 }
259 
SetAudioScene(AudioScene scene)260 int32_t AudioPolicyProxy::SetAudioScene(AudioScene scene)
261 {
262     MessageParcel data;
263     MessageParcel reply;
264     MessageOption option;
265 
266     if (!data.WriteInterfaceToken(GetDescriptor())) {
267         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
268         return -1;
269     }
270     data.WriteInt32(static_cast<int>(scene));
271     int32_t error = Remote()->SendRequest(SET_AUDIO_SCENE, data, reply, option);
272     if (error != ERR_NONE) {
273         AUDIO_ERR_LOG("set audio scene failed, error: %d", error);
274         return error;
275     }
276 
277     return reply.ReadInt32();
278 }
279 
GetAudioScene()280 AudioScene AudioPolicyProxy::GetAudioScene()
281 {
282     MessageParcel data;
283     MessageParcel reply;
284     MessageOption option;
285 
286     if (!data.WriteInterfaceToken(GetDescriptor())) {
287         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
288         return AUDIO_SCENE_DEFAULT;
289     }
290     int32_t error = Remote()->SendRequest(GET_AUDIO_SCENE, data, reply, option);
291     if (error != ERR_NONE) {
292         AUDIO_ERR_LOG("get audio scene failed, error: %d", error);
293     }
294     return static_cast<AudioScene>(reply.ReadInt32());
295 }
296 
GetStreamVolume(AudioStreamType streamType)297 float AudioPolicyProxy::GetStreamVolume(AudioStreamType streamType)
298 {
299     MessageParcel data;
300     MessageParcel reply;
301     MessageOption option;
302 
303     if (!data.WriteInterfaceToken(GetDescriptor())) {
304         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
305         return -1;
306     }
307     data.WriteInt32(static_cast<int32_t>(streamType));
308     int32_t error = Remote()->SendRequest(GET_STREAM_VOLUME, data, reply, option);
309     if (error != ERR_NONE) {
310         AUDIO_ERR_LOG("get volume failed, error: %d", error);
311         return error;
312     }
313     return reply.ReadFloat();
314 }
315 
SetLowPowerVolume(int32_t streamId,float volume)316 int32_t AudioPolicyProxy::SetLowPowerVolume(int32_t streamId, float volume)
317 {
318     MessageParcel data;
319     MessageParcel reply;
320     MessageOption option;
321     if (!data.WriteInterfaceToken(GetDescriptor())) {
322         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
323         return -1;
324     }
325 
326     data.WriteInt32(streamId);
327     data.WriteFloat(volume);
328     int32_t error = Remote()->SendRequest(SET_LOW_POWER_STREM_VOLUME, data, reply, option);
329     if (error != ERR_NONE) {
330         AUDIO_ERR_LOG("set low power stream volume failed, error: %d", error);
331         return error;
332     }
333     return reply.ReadInt32();
334 }
335 
GetLowPowerVolume(int32_t streamId)336 float AudioPolicyProxy::GetLowPowerVolume(int32_t streamId)
337 {
338     MessageParcel data;
339     MessageParcel reply;
340     MessageOption option;
341 
342     if (!data.WriteInterfaceToken(GetDescriptor())) {
343         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
344         return -1;
345     }
346     data.WriteInt32(streamId);
347     int32_t error = Remote()->SendRequest(GET_LOW_POWRR_STREM_VOLUME, data, reply, option);
348     if (error != ERR_NONE) {
349         AUDIO_ERR_LOG("get low power stream volume failed, error: %d", error);
350         return error;
351     }
352     return reply.ReadFloat();
353 }
354 
GetSingleStreamVolume(int32_t streamId)355 float AudioPolicyProxy::GetSingleStreamVolume(int32_t streamId)
356 {
357     MessageParcel data;
358     MessageParcel reply;
359     MessageOption option;
360 
361     if (!data.WriteInterfaceToken(GetDescriptor())) {
362         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
363         return -1;
364     }
365     data.WriteInt32(streamId);
366     int32_t error = Remote()->SendRequest(GET_SINGLE_STREAM_VOLUME, data, reply, option);
367     if (error != ERR_NONE) {
368         AUDIO_ERR_LOG("get single stream volume failed, error: %d", error);
369         return error;
370     }
371     return reply.ReadFloat();
372 }
373 
SetStreamMute(AudioStreamType streamType,bool mute)374 int32_t AudioPolicyProxy::SetStreamMute(AudioStreamType streamType, bool mute)
375 {
376     MessageParcel data;
377     MessageParcel reply;
378     MessageOption option;
379 
380     if (!data.WriteInterfaceToken(GetDescriptor())) {
381         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
382         return -1;
383     }
384     data.WriteInt32(static_cast<int32_t>(streamType));
385     data.WriteBool(mute);
386     int32_t error = Remote()->SendRequest(SET_STREAM_MUTE, data, reply, option);
387     if (error != ERR_NONE) {
388         AUDIO_ERR_LOG("set mute failed, error: %d", error);
389         return error;
390     }
391     return reply.ReadInt32();
392 }
393 
GetStreamMute(AudioStreamType streamType)394 bool AudioPolicyProxy::GetStreamMute(AudioStreamType streamType)
395 {
396     MessageParcel data;
397     MessageParcel reply;
398     MessageOption option;
399 
400     if (!data.WriteInterfaceToken(GetDescriptor())) {
401         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
402         return false;
403     }
404     data.WriteInt32(static_cast<int32_t>(streamType));
405     int32_t error = Remote()->SendRequest(GET_STREAM_MUTE, data, reply, option);
406     if (error != ERR_NONE) {
407         AUDIO_ERR_LOG("get mute failed, error: %d", error);
408         return false;
409     }
410     return reply.ReadBool();
411 }
412 
IsStreamActive(AudioStreamType streamType)413 bool AudioPolicyProxy::IsStreamActive(AudioStreamType streamType)
414 {
415     MessageParcel data;
416     MessageParcel reply;
417     MessageOption option;
418 
419     if (!data.WriteInterfaceToken(GetDescriptor())) {
420         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
421         return false;
422     }
423     data.WriteInt32(static_cast<int32_t>(streamType));
424     int32_t error = Remote()->SendRequest(IS_STREAM_ACTIVE, data, reply, option);
425     if (error != ERR_NONE) {
426         AUDIO_ERR_LOG("isStreamActive failed, error: %d", error);
427         return false;
428     }
429     return reply.ReadBool();
430 }
431 
GetDevices(DeviceFlag deviceFlag)432 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetDevices(DeviceFlag deviceFlag)
433 {
434     MessageParcel data;
435     MessageParcel reply;
436     MessageOption option;
437     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
438 
439     if (!data.WriteInterfaceToken(GetDescriptor())) {
440         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
441         return deviceInfo;
442     }
443     data.WriteInt32(static_cast<int32_t>(deviceFlag));
444     int32_t error = Remote()->SendRequest(GET_DEVICES, data, reply, option);
445     if (error != ERR_NONE) {
446         AUDIO_ERR_LOG("Get devices failed, error: %d", error);
447         return deviceInfo;
448     }
449 
450     int32_t size = reply.ReadInt32();
451     for (int32_t i = 0; i < size; i++) {
452         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
453     }
454 
455     return deviceInfo;
456 }
457 
GetActiveOutputDeviceDescriptors()458 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyProxy::GetActiveOutputDeviceDescriptors()
459 {
460     MessageParcel data;
461     MessageParcel reply;
462     MessageOption option;
463     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
464 
465     if (!data.WriteInterfaceToken(GetDescriptor())) {
466         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
467         return deviceInfo;
468     }
469     int32_t error = Remote()->SendRequest(GET_ACTIVE_OUTPUT_DEVICE_DESCRIPTORS, data, reply, option);
470     if (error != ERR_NONE) {
471         AUDIO_ERR_LOG("Get out devices failed, error: %d", error);
472         return deviceInfo;
473     }
474 
475     int32_t size = reply.ReadInt32();
476     for (int32_t i = 0; i < size; i++) {
477         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
478     }
479 
480     return deviceInfo;
481 }
482 
SetDeviceActive(InternalDeviceType deviceType,bool active)483 int32_t AudioPolicyProxy::SetDeviceActive(InternalDeviceType deviceType, bool active)
484 {
485     MessageParcel data;
486     MessageParcel reply;
487     MessageOption option;
488 
489     if (!data.WriteInterfaceToken(GetDescriptor())) {
490         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
491         return -1;
492     }
493     data.WriteInt32(static_cast<int32_t>(deviceType));
494     data.WriteBool(active);
495     int32_t error = Remote()->SendRequest(SET_DEVICE_ACTIVE, data, reply, option);
496     if (error != ERR_NONE) {
497         AUDIO_ERR_LOG("set device active failed, error: %d", error);
498         return error;
499     }
500     return reply.ReadInt32();
501 }
502 
IsDeviceActive(InternalDeviceType deviceType)503 bool AudioPolicyProxy::IsDeviceActive(InternalDeviceType deviceType)
504 {
505     MessageParcel data;
506     MessageParcel reply;
507     MessageOption option;
508 
509     if (!data.WriteInterfaceToken(GetDescriptor())) {
510         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
511         return false;
512     }
513     data.WriteInt32(static_cast<int32_t>(deviceType));
514     int32_t error = Remote()->SendRequest(IS_DEVICE_ACTIVE, data, reply, option);
515     if (error != ERR_NONE) {
516         AUDIO_ERR_LOG("is device active failed, error: %d", error);
517         return false;
518     }
519     return reply.ReadBool();
520 }
521 
GetActiveOutputDevice()522 DeviceType AudioPolicyProxy::GetActiveOutputDevice()
523 {
524     MessageParcel data;
525     MessageParcel reply;
526     MessageOption option;
527 
528     if (!data.WriteInterfaceToken(GetDescriptor())) {
529         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
530         return DEVICE_TYPE_INVALID;
531     }
532 
533     int32_t error = Remote()->SendRequest(GET_ACTIVE_OUTPUT_DEVICE, data, reply, option);
534     if (error != ERR_NONE) {
535         AUDIO_ERR_LOG("get active output device failed, error: %d", error);
536         return DEVICE_TYPE_INVALID;
537     }
538 
539     return static_cast<DeviceType>(reply.ReadInt32());
540 }
541 
GetActiveInputDevice()542 DeviceType AudioPolicyProxy::GetActiveInputDevice()
543 {
544     MessageParcel data;
545     MessageParcel reply;
546     MessageOption option;
547 
548     if (!data.WriteInterfaceToken(GetDescriptor())) {
549         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
550         return DEVICE_TYPE_INVALID;
551     }
552 
553     int32_t error = Remote()->SendRequest(GET_ACTIVE_INPUT_DEVICE, data, reply, option);
554     if (error != ERR_NONE) {
555         AUDIO_ERR_LOG("get active input device failed, error: %d", error);
556         return DEVICE_TYPE_INVALID;
557     }
558 
559     return static_cast<DeviceType>(reply.ReadInt32());
560 }
561 
SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)562 int32_t AudioPolicyProxy::SelectOutputDevice(sptr<AudioRendererFilter> audioRendererFilter,
563     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
564 {
565     MessageParcel data;
566     MessageParcel reply;
567     MessageOption option;
568 
569     if (!data.WriteInterfaceToken(GetDescriptor())) {
570         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
571         return -1;
572     }
573     if (!audioRendererFilter->Marshalling(data)) {
574         AUDIO_ERR_LOG("AudioRendererFilter Marshalling() failed");
575         return -1;
576     }
577     int size = audioDeviceDescriptors.size();
578     int validSize = 20; // Use 20 as limit.
579     if (size <= 0 || size > validSize) {
580         AUDIO_ERR_LOG("SelectOutputDevice get invalid device size.");
581         return -1;
582     }
583     data.WriteInt32(size);
584     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
585         if (!audioDeviceDescriptor->Marshalling(data)) {
586             AUDIO_ERR_LOG("AudioDeviceDescriptor Marshalling() failed");
587             return -1;
588         }
589     }
590     int error = Remote()->SendRequest(SELECT_OUTPUT_DEVICE, data, reply, option);
591     if (error != ERR_NONE) {
592         AUDIO_ERR_LOG("SelectOutputDevice failed, error: %{public}d", error);
593         return error;
594     }
595 
596     return reply.ReadInt32();
597 }
GetSelectedDeviceInfo(int32_t uid,int32_t pid,AudioStreamType streamType)598 std::string AudioPolicyProxy::GetSelectedDeviceInfo(int32_t uid, int32_t pid, AudioStreamType streamType)
599 {
600     MessageParcel data;
601     MessageParcel reply;
602     MessageOption option;
603 
604     if (!data.WriteInterfaceToken(GetDescriptor())) {
605         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
606         return "";
607     }
608     data.WriteInt32(uid);
609     data.WriteInt32(pid);
610     data.WriteInt32(static_cast<int32_t>(streamType));
611     int error = Remote()->SendRequest(GET_SELECTED_DEVICE_INFO, data, reply, option);
612     if (error != ERR_NONE) {
613         AUDIO_ERR_LOG("GetSelectedDeviceInfo failed, error: %{public}d", error);
614         return "";
615     }
616 
617     return reply.ReadString();
618 }
619 
SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)620 int32_t AudioPolicyProxy::SelectInputDevice(sptr<AudioCapturerFilter> audioCapturerFilter,
621     std::vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors)
622 {
623     MessageParcel data;
624     MessageParcel reply;
625     MessageOption option;
626 
627     if (!data.WriteInterfaceToken(GetDescriptor())) {
628         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
629         return -1;
630     }
631     if (!audioCapturerFilter->Marshalling(data)) {
632         AUDIO_ERR_LOG("AudioCapturerFilter Marshalling() failed");
633         return -1;
634     }
635     int size = audioDeviceDescriptors.size();
636     int validSize = 20; // Use 20 as limit.
637     if (size <= 0 || size > validSize) {
638         AUDIO_ERR_LOG("SelectInputDevice get invalid device size.");
639         return -1;
640     }
641     data.WriteInt32(size);
642     for (auto audioDeviceDescriptor : audioDeviceDescriptors) {
643         if (!audioDeviceDescriptor->Marshalling(data)) {
644             AUDIO_ERR_LOG("AudioDeviceDescriptor Marshalling() failed");
645             return -1;
646         }
647     }
648     int error = Remote()->SendRequest(SELECT_INPUT_DEVICE, data, reply, option);
649     if (error != ERR_NONE) {
650         AUDIO_ERR_LOG("SelectInputDevice failed, error: %{public}d", error);
651         return error;
652     }
653 
654     return reply.ReadInt32();
655 }
656 
SetRingerModeCallback(const int32_t clientId,const sptr<IRemoteObject> & object)657 int32_t AudioPolicyProxy::SetRingerModeCallback(const int32_t clientId, const sptr<IRemoteObject> &object)
658 {
659     MessageParcel data;
660     MessageParcel reply;
661     MessageOption option;
662 
663     if (object == nullptr) {
664         AUDIO_ERR_LOG("AudioPolicyProxy: SetRingerModeCallback object is null");
665         return ERR_NULL_OBJECT;
666     }
667     if (!data.WriteInterfaceToken(GetDescriptor())) {
668         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
669         return -1;
670     }
671     data.WriteInt32(clientId);
672     (void)data.WriteRemoteObject(object);
673     int error = Remote()->SendRequest(SET_RINGERMODE_CALLBACK, data, reply, option);
674     if (error != ERR_NONE) {
675         AUDIO_ERR_LOG("AudioPolicyProxy: set ringermode callback failed, error: %{public}d", error);
676         return error;
677     }
678 
679     return reply.ReadInt32();
680 }
681 
UnsetRingerModeCallback(const int32_t clientId)682 int32_t AudioPolicyProxy::UnsetRingerModeCallback(const int32_t clientId)
683 {
684     MessageParcel data;
685     MessageParcel reply;
686     MessageOption option;
687 
688     if (!data.WriteInterfaceToken(GetDescriptor())) {
689         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
690         return -1;
691     }
692     data.WriteInt32(clientId);
693     int error = Remote()->SendRequest(UNSET_RINGERMODE_CALLBACK, data, reply, option);
694     if (error != ERR_NONE) {
695         AUDIO_ERR_LOG("AudioPolicyProxy: unset ringermode callback failed, error: %{public}d", error);
696         return error;
697     }
698 
699     return reply.ReadInt32();
700 }
701 
SetMicStateChangeCallback(const int32_t clientId,const sptr<IRemoteObject> & object)702 int32_t AudioPolicyProxy::SetMicStateChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object)
703 {
704     MessageParcel data;
705     MessageParcel reply;
706     MessageOption option;
707 
708     if (object == nullptr) {
709         AUDIO_ERR_LOG("AudioPolicyProxy: SetMicStateChangeCallback object is null");
710         return ERR_NULL_OBJECT;
711     }
712     if (!data.WriteInterfaceToken(GetDescriptor())) {
713         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
714         return -1;
715     }
716 
717     data.WriteInt32(clientId);
718     (void)data.WriteRemoteObject(object);
719     int error = Remote()->SendRequest(SET_MIC_STATE_CHANGE_CALLBACK, data, reply, option);
720     if (error != ERR_NONE) {
721         AUDIO_ERR_LOG("AudioPolicyProxy: SetMicStateChangeCallback failed, error: %{public}d", error);
722         return error;
723     }
724 
725     return reply.ReadInt32();
726 }
727 
SetDeviceChangeCallback(const int32_t clientId,const DeviceFlag flag,const sptr<IRemoteObject> & object)728 int32_t AudioPolicyProxy::SetDeviceChangeCallback(const int32_t clientId, const DeviceFlag flag,
729     const sptr<IRemoteObject> &object)
730 {
731     MessageParcel data;
732     MessageParcel reply;
733     MessageOption option;
734 
735     if (object == nullptr) {
736         AUDIO_ERR_LOG("AudioPolicyProxy: SetDeviceChangeCallback object is null");
737         return ERR_NULL_OBJECT;
738     }
739     if (!data.WriteInterfaceToken(GetDescriptor())) {
740         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
741         return -1;
742     }
743 
744     data.WriteInt32(clientId);
745     data.WriteInt32(flag);
746     (void)data.WriteRemoteObject(object);
747     int error = Remote()->SendRequest(SET_DEVICE_CHANGE_CALLBACK, data, reply, option);
748     if (error != ERR_NONE) {
749         AUDIO_ERR_LOG("AudioPolicyProxy: SetDeviceChangeCallback failed, error: %{public}d", error);
750         return error;
751     }
752 
753     return reply.ReadInt32();
754 }
755 
UnsetDeviceChangeCallback(const int32_t clientId)756 int32_t AudioPolicyProxy::UnsetDeviceChangeCallback(const int32_t clientId)
757 {
758     MessageParcel data;
759     MessageParcel reply;
760     MessageOption option;
761 
762     if (!data.WriteInterfaceToken(GetDescriptor())) {
763         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
764         return -1;
765     }
766     data.WriteInt32(clientId);
767     int error = Remote()->SendRequest(UNSET_DEVICE_CHANGE_CALLBACK, data, reply, option);
768     if (error != ERR_NONE) {
769         AUDIO_ERR_LOG("AudioPolicyProxy: unset device change callback failed, error: %{public}d", error);
770         return error;
771     }
772 
773     return reply.ReadInt32();
774 }
775 
SetAudioInterruptCallback(const uint32_t sessionID,const sptr<IRemoteObject> & object)776 int32_t AudioPolicyProxy::SetAudioInterruptCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object)
777 {
778     MessageParcel data;
779     MessageParcel reply;
780     MessageOption option;
781 
782     if (object == nullptr) {
783         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioInterruptCallback object is null");
784         return ERR_NULL_OBJECT;
785     }
786     if (!data.WriteInterfaceToken(GetDescriptor())) {
787         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
788         return -1;
789     }
790     data.WriteUint32(sessionID);
791     (void)data.WriteRemoteObject(object);
792     int error = Remote()->SendRequest(SET_CALLBACK, data, reply, option);
793     if (error != ERR_NONE) {
794         AUDIO_ERR_LOG("AudioPolicyProxy: set callback failed, error: %{public}d", error);
795         return error;
796     }
797 
798     return reply.ReadInt32();
799 }
800 
UnsetAudioInterruptCallback(const uint32_t sessionID)801 int32_t AudioPolicyProxy::UnsetAudioInterruptCallback(const uint32_t sessionID)
802 {
803     MessageParcel data;
804     MessageParcel reply;
805     MessageOption option;
806 
807     if (!data.WriteInterfaceToken(GetDescriptor())) {
808         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
809         return -1;
810     }
811     data.WriteUint32(sessionID);
812     int error = Remote()->SendRequest(UNSET_CALLBACK, data, reply, option);
813     if (error != ERR_NONE) {
814         AUDIO_ERR_LOG("AudioPolicyProxy: unset callback failed, error: %{public}d", error);
815         return error;
816     }
817 
818     return reply.ReadInt32();
819 }
820 
ActivateAudioInterrupt(const AudioInterrupt & audioInterrupt)821 int32_t AudioPolicyProxy::ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt)
822 {
823     MessageParcel data;
824     MessageParcel reply;
825     MessageOption option;
826 
827     if (!data.WriteInterfaceToken(GetDescriptor())) {
828         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
829         return -1;
830     }
831     WriteAudioInteruptParams(data, audioInterrupt);
832     int error = Remote()->SendRequest(ACTIVATE_INTERRUPT, data, reply, option);
833     if (error != ERR_NONE) {
834         AUDIO_ERR_LOG("AudioPolicyProxy: activate interrupt failed, error: %{public}d", error);
835         return error;
836     }
837 
838     return reply.ReadInt32();
839 }
840 
DeactivateAudioInterrupt(const AudioInterrupt & audioInterrupt)841 int32_t AudioPolicyProxy::DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt)
842 {
843     MessageParcel data;
844     MessageParcel reply;
845     MessageOption option;
846 
847     if (!data.WriteInterfaceToken(GetDescriptor())) {
848         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
849         return -1;
850     }
851     WriteAudioInteruptParams(data, audioInterrupt);
852     int error = Remote()->SendRequest(DEACTIVATE_INTERRUPT, data, reply, option);
853     if (error != ERR_NONE) {
854         AUDIO_ERR_LOG("AudioPolicyProxy: deactivate interrupt failed, error: %{public}d", error);
855         return error;
856     }
857 
858     return reply.ReadInt32();
859 }
860 
SetAudioManagerInterruptCallback(const uint32_t clientID,const sptr<IRemoteObject> & object)861 int32_t AudioPolicyProxy::SetAudioManagerInterruptCallback(const uint32_t clientID, const sptr<IRemoteObject> &object)
862 {
863     MessageParcel data;
864     MessageParcel reply;
865     MessageOption option;
866 
867     if (object == nullptr) {
868         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioManagerInterruptCallback object is null");
869         return ERR_NULL_OBJECT;
870     }
871     if (!data.WriteInterfaceToken(GetDescriptor())) {
872         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
873         return -1;
874     }
875     data.WriteUint32(clientID);
876     (void)data.WriteRemoteObject(object);
877     int error = Remote()->SendRequest(SET_INTERRUPT_CALLBACK, data, reply, option);
878     if (error != ERR_NONE) {
879         AUDIO_ERR_LOG("AudioPolicyProxy: set callback failed, error: %{public}d", error);
880         return error;
881     }
882 
883     return reply.ReadInt32();
884 }
885 
UnsetAudioManagerInterruptCallback(const uint32_t clientID)886 int32_t AudioPolicyProxy::UnsetAudioManagerInterruptCallback(const uint32_t clientID)
887 {
888     MessageParcel data;
889     MessageParcel reply;
890     MessageOption option;
891 
892     if (!data.WriteInterfaceToken(GetDescriptor())) {
893         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
894         return -1;
895     }
896 
897     data.WriteUint32(clientID);
898 
899     int error = Remote()->SendRequest(UNSET_INTERRUPT_CALLBACK, data, reply, option);
900     if (error != ERR_NONE) {
901         AUDIO_ERR_LOG("AudioPolicyProxy: unset callback failed, error: %{public}d", error);
902         return error;
903     }
904 
905     return reply.ReadInt32();
906 }
907 
RequestAudioFocus(const uint32_t clientID,const AudioInterrupt & audioInterrupt)908 int32_t AudioPolicyProxy::RequestAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt)
909 {
910     MessageParcel data;
911     MessageParcel reply;
912     MessageOption option;
913 
914     if (!data.WriteInterfaceToken(GetDescriptor())) {
915         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
916         return -1;
917     }
918 
919     data.WriteUint32(clientID);
920     WriteAudioManagerInteruptParams(data, audioInterrupt);
921 
922     int error = Remote()->SendRequest(REQUEST_AUDIO_FOCUS, data, reply, option);
923     if (error != ERR_NONE) {
924         AUDIO_ERR_LOG("AudioPolicyProxy: activate interrupt failed, error: %{public}d", error);
925         return error;
926     }
927 
928     return reply.ReadInt32();
929 }
930 
AbandonAudioFocus(const uint32_t clientID,const AudioInterrupt & audioInterrupt)931 int32_t AudioPolicyProxy::AbandonAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt)
932 {
933     MessageParcel data;
934     MessageParcel reply;
935     MessageOption option;
936 
937     if (!data.WriteInterfaceToken(GetDescriptor())) {
938         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
939         return -1;
940     }
941     data.WriteUint32(clientID);
942     WriteAudioManagerInteruptParams(data, audioInterrupt);
943 
944     int error = Remote()->SendRequest(ABANDON_AUDIO_FOCUS, data, reply, option);
945     if (error != ERR_NONE) {
946         AUDIO_ERR_LOG("AudioPolicyProxy: deactivate interrupt failed, error: %{public}d", error);
947         return error;
948     }
949 
950     return reply.ReadInt32();
951 }
952 
GetStreamInFocus()953 AudioStreamType AudioPolicyProxy::GetStreamInFocus()
954 {
955     MessageParcel data;
956     MessageParcel reply;
957     MessageOption option;
958 
959     if (!data.WriteInterfaceToken(GetDescriptor())) {
960         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
961         return STREAM_DEFAULT;
962     }
963     int32_t error = Remote()->SendRequest(GET_STREAM_IN_FOCUS, data, reply, option);
964     if (error != ERR_NONE) {
965         AUDIO_ERR_LOG("get stream in focus failed, error: %d", error);
966     }
967     return static_cast<AudioStreamType>(reply.ReadInt32());
968 }
969 
GetSessionInfoInFocus(AudioInterrupt & audioInterrupt)970 int32_t AudioPolicyProxy::GetSessionInfoInFocus(AudioInterrupt &audioInterrupt)
971 {
972     MessageParcel data;
973     MessageParcel reply;
974     MessageOption option;
975 
976     if (!data.WriteInterfaceToken(GetDescriptor())) {
977         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
978         return -1;
979     }
980     int32_t error = Remote()->SendRequest(GET_SESSION_INFO_IN_FOCUS, data, reply, option);
981     if (error != ERR_NONE) {
982         AUDIO_ERR_LOG("AudioPolicyProxy::GetSessionInfoInFocus failed, error: %d", error);
983     }
984     ReadAudioInterruptParams(reply, audioInterrupt);
985 
986     return reply.ReadInt32();
987 }
988 
SetVolumeKeyEventCallback(const int32_t clientPid,const sptr<IRemoteObject> & object)989 int32_t AudioPolicyProxy::SetVolumeKeyEventCallback(const int32_t clientPid, const sptr<IRemoteObject> &object)
990 {
991     MessageParcel data;
992     MessageParcel reply;
993     MessageOption option;
994 
995     if (!data.WriteInterfaceToken(GetDescriptor())) {
996         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
997         return -1;
998     }
999     if (object == nullptr) {
1000         AUDIO_ERR_LOG("VolumeKeyEventCallback object is null");
1001         return ERR_NULL_OBJECT;
1002     }
1003 
1004     data.WriteInt32(clientPid);
1005     data.WriteRemoteObject(object);
1006     int result = Remote()->SendRequest(SET_VOLUME_KEY_EVENT_CALLBACK, data, reply, option);
1007     if (result != ERR_NONE) {
1008         AUDIO_ERR_LOG("SetAudioVolumeKeyEventCallback failed, result: %{public}d", result);
1009         return result;
1010     }
1011 
1012     return reply.ReadInt32();
1013 }
1014 
UnsetVolumeKeyEventCallback(const int32_t clientPid)1015 int32_t AudioPolicyProxy::UnsetVolumeKeyEventCallback(const int32_t clientPid)
1016 {
1017     MessageParcel data;
1018     MessageParcel reply;
1019     MessageOption option;
1020 
1021     if (!data.WriteInterfaceToken(GetDescriptor())) {
1022         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
1023         return -1;
1024     }
1025 
1026     data.WriteInt32(clientPid);
1027     int result = Remote()->SendRequest(UNSET_VOLUME_KEY_EVENT_CALLBACK, data, reply, option);
1028     if (result != ERR_NONE) {
1029         AUDIO_ERR_LOG("UnsetVolumeKeyEventCallback failed, result: %{public}d", result);
1030         return result;
1031     }
1032 
1033     return reply.ReadInt32();
1034 }
1035 
VerifyClientPermission(const std::string & permissionName,uint32_t appTokenId,int32_t appUid,bool privacyFlag,AudioPermissionState state)1036 bool AudioPolicyProxy::VerifyClientPermission(const std::string &permissionName, uint32_t appTokenId, int32_t appUid,
1037     bool privacyFlag, AudioPermissionState state)
1038 {
1039     AUDIO_DEBUG_LOG("Proxy [permission : %{public}s] | [tid : %{public}d]", permissionName.c_str(), appTokenId);
1040     MessageParcel data;
1041     MessageParcel reply;
1042     MessageOption option;
1043 
1044     if (!data.WriteInterfaceToken(GetDescriptor())) {
1045         AUDIO_ERR_LOG("VerifyClientPermission: WriteInterfaceToken failed");
1046         return false;
1047     }
1048 
1049     data.WriteString(permissionName);
1050     data.WriteUint32(appTokenId);
1051     data.WriteInt32(appUid);
1052     data.WriteBool(privacyFlag);
1053     data.WriteInt32(state);
1054 
1055     int result = Remote()->SendRequest(QUERY_PERMISSION, data, reply, option);
1056     if (result != ERR_NONE) {
1057         AUDIO_ERR_LOG("VerifyClientPermission failed, result: %{public}d", result);
1058         return false;
1059     }
1060 
1061     return reply.ReadBool();
1062 }
1063 
getUsingPemissionFromPrivacy(const std::string & permissionName,uint32_t appTokenId,AudioPermissionState state)1064 bool AudioPolicyProxy::getUsingPemissionFromPrivacy(const std::string &permissionName, uint32_t appTokenId,
1065     AudioPermissionState state)
1066 {
1067     AUDIO_DEBUG_LOG("Proxy [permission : %{public}s] | [tid : %{public}d]", permissionName.c_str(), appTokenId);
1068     MessageParcel data;
1069     MessageParcel reply;
1070     MessageOption option;
1071 
1072     if (!data.WriteInterfaceToken(GetDescriptor())) {
1073         AUDIO_ERR_LOG("getUsingPemissionFromPrivacy: WriteInterfaceToken failed");
1074         return false;
1075     }
1076 
1077     data.WriteString(permissionName);
1078     data.WriteUint32(appTokenId);
1079     data.WriteInt32(state);
1080 
1081     int result = Remote()->SendRequest(GET_USING_PEMISSION_FROM_PRIVACY, data, reply, option);
1082     if (result != ERR_NONE) {
1083         AUDIO_ERR_LOG("getUsingPemissionFromPrivacy failed, result: %{public}d", result);
1084         return false;
1085     }
1086 
1087     return reply.ReadBool();
1088 }
1089 
ReconfigureAudioChannel(const uint32_t & count,DeviceType deviceType)1090 int32_t AudioPolicyProxy::ReconfigureAudioChannel(const uint32_t &count, DeviceType deviceType)
1091 {
1092     AUDIO_ERR_LOG("ReconfigureAudioChannel proxy %{public}d, %{public}d", count, deviceType);
1093     MessageParcel data;
1094     MessageParcel reply;
1095     MessageOption option;
1096 
1097     if (!data.WriteInterfaceToken(GetDescriptor())) {
1098         AUDIO_ERR_LOG("ReconfigureAudioChannel: WriteInterfaceToken failed");
1099         return IPC_PROXY_ERR;
1100     }
1101 
1102     data.WriteUint32(count);
1103     data.WriteInt32(deviceType);
1104 
1105     int result = Remote()->SendRequest(RECONFIGURE_CHANNEL, data, reply, option);
1106     if (result != ERR_NONE) {
1107         AUDIO_ERR_LOG("ReconfigureAudioChannel failed, result: %{public}d", result);
1108         return ERR_TRANSACTION_FAILED;
1109     }
1110 
1111     return reply.ReadInt32();
1112 }
1113 
RegisterAudioRendererEventListener(const int32_t clientUID,const sptr<IRemoteObject> & object)1114 int32_t AudioPolicyProxy::RegisterAudioRendererEventListener(const int32_t clientUID, const sptr<IRemoteObject> &object)
1115 {
1116     MessageParcel data;
1117     MessageParcel reply;
1118     MessageOption option;
1119 
1120     AUDIO_INFO_LOG("AudioPolicyProxy::RegisterAudioRendererEventListener");
1121     if (!data.WriteInterfaceToken(GetDescriptor())) {
1122         AUDIO_ERR_LOG("RegisterAudioRendererEventListener: WriteInterfaceToken failed");
1123         return ERROR;
1124     }
1125     if (object == nullptr) {
1126         AUDIO_ERR_LOG("RegisterAudioRendererEventListener Event object is null");
1127         return ERR_NULL_OBJECT;
1128     }
1129 
1130     data.WriteInt32(clientUID);
1131     data.WriteRemoteObject(object);
1132     int32_t error = Remote() ->SendRequest(REGISTER_PLAYBACK_EVENT, data, reply, option);
1133     if (error != ERR_NONE) {
1134         AUDIO_ERR_LOG("RegisterAudioRendererEventListener register playback event failed , error: %d", error);
1135         return ERROR;
1136     }
1137 
1138     return reply.ReadInt32();
1139 }
1140 
UnregisterAudioRendererEventListener(const int32_t clientUID)1141 int32_t AudioPolicyProxy::UnregisterAudioRendererEventListener(const int32_t clientUID)
1142 {
1143     MessageParcel data;
1144     MessageParcel reply;
1145     MessageOption option;
1146 
1147     AUDIO_INFO_LOG("AudioPolicyProxy::UnregisterAudioRendererEventListener");
1148     if (!data.WriteInterfaceToken(GetDescriptor())) {
1149         AUDIO_ERR_LOG("UnregisterAudioRendererEventListener WriteInterfaceToken failed");
1150         return ERROR;
1151     }
1152 
1153     data.WriteInt32(clientUID);
1154     int32_t error = Remote() ->SendRequest(UNREGISTER_PLAYBACK_EVENT, data, reply, option);
1155     if (error != ERR_NONE) {
1156         AUDIO_ERR_LOG("UnregisterAudioRendererEventListener unregister playback event failed , error: %d", error);
1157         return ERROR;
1158     }
1159 
1160     return reply.ReadInt32();
1161 }
1162 
GetAudioLatencyFromXml()1163 int32_t AudioPolicyProxy::GetAudioLatencyFromXml()
1164 {
1165     MessageParcel data;
1166     MessageParcel reply;
1167     MessageOption option;
1168 
1169     if (!data.WriteInterfaceToken(GetDescriptor())) {
1170         AUDIO_ERR_LOG("AudioPolicyProxy: GetAudioLatencyFromXml WriteInterfaceToken failed");
1171         return IPC_PROXY_ERR;
1172     }
1173 
1174     int32_t error = Remote()->SendRequest(GET_AUDIO_LATENCY, data, reply, option);
1175     if (error != ERR_NONE) {
1176         AUDIO_ERR_LOG("GetAudioLatencyFromXml, error: %d", error);
1177         return ERR_TRANSACTION_FAILED;
1178     }
1179 
1180     return reply.ReadInt32();
1181 }
1182 
GetSinkLatencyFromXml()1183 uint32_t AudioPolicyProxy::GetSinkLatencyFromXml()
1184 {
1185     MessageParcel data;
1186     MessageParcel reply;
1187     MessageOption option;
1188 
1189     if (!data.WriteInterfaceToken(GetDescriptor())) {
1190         AUDIO_ERR_LOG("AudioPolicyProxy: GetSinkLatencyFromXml WriteInterfaceToken failed");
1191         return 0;
1192     }
1193 
1194     int32_t error = Remote()->SendRequest(GET_SINK_LATENCY, data, reply, option);
1195     if (error != ERR_NONE) {
1196         AUDIO_ERR_LOG("GetSinkLatencyFromXml, error: %d", error);
1197         return 0;
1198     }
1199 
1200     return reply.ReadUint32();
1201 }
1202 
RegisterAudioCapturerEventListener(const int32_t clientUID,const sptr<IRemoteObject> & object)1203 int32_t AudioPolicyProxy::RegisterAudioCapturerEventListener(const int32_t clientUID, const sptr<IRemoteObject> &object)
1204 {
1205     MessageParcel data;
1206     MessageParcel reply;
1207     MessageOption option;
1208 
1209     AUDIO_INFO_LOG("AudioPolicyProxy::RegisterAudioCapturerEventListener");
1210 
1211     if (!data.WriteInterfaceToken(GetDescriptor())) {
1212         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener:: WriteInterfaceToken failed");
1213         return ERROR;
1214     }
1215     if (object == nullptr) {
1216         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener Event object is null");
1217         return ERR_NULL_OBJECT;
1218     }
1219 
1220     data.WriteInt32(clientUID);
1221     data.WriteRemoteObject(object);
1222     int32_t error = Remote() ->SendRequest(REGISTER_RECORDING_EVENT, data, reply, option);
1223     if (error != ERR_NONE) {
1224         AUDIO_ERR_LOG("RegisterAudioCapturerEventListener recording event failed , error: %d", error);
1225         return ERROR;
1226     }
1227 
1228     return reply.ReadInt32();
1229 }
1230 
UnregisterAudioCapturerEventListener(const int32_t clientUID)1231 int32_t AudioPolicyProxy::UnregisterAudioCapturerEventListener(const int32_t clientUID)
1232 {
1233     MessageParcel data;
1234     MessageParcel reply;
1235     MessageOption option;
1236 
1237     AUDIO_INFO_LOG("AudioPolicyProxy::UnregisterAudioCapturerEventListener");
1238 
1239     if (!data.WriteInterfaceToken(GetDescriptor())) {
1240         AUDIO_ERR_LOG("AudioPolicyProxy:: WriteInterfaceToken failed");
1241         return ERROR;
1242     }
1243 
1244     data.WriteInt32(clientUID);
1245     int32_t error = Remote() ->SendRequest(UNREGISTER_RECORDING_EVENT, data, reply, option);
1246     if (error != ERR_NONE) {
1247         AUDIO_ERR_LOG("UnregisterAudioCapturerEventListener recording event failed , error: %d", error);
1248         return ERROR;
1249     }
1250 
1251     return reply.ReadInt32();
1252 }
1253 
RegisterTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo,const sptr<IRemoteObject> & object)1254 int32_t AudioPolicyProxy::RegisterTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo,
1255     const sptr<IRemoteObject> &object)
1256 {
1257     MessageParcel data;
1258     MessageParcel reply;
1259     MessageOption option;
1260 
1261     AUDIO_INFO_LOG("AudioPolicyProxy::RegisterTracker");
1262 
1263     if (!data.WriteInterfaceToken(GetDescriptor())) {
1264         AUDIO_ERR_LOG("RegisterTracker WriteInterfaceToken failed");
1265         return ERROR;
1266     }
1267 
1268     if (object == nullptr) {
1269         AUDIO_ERR_LOG("Register Tracker Event object is null");
1270         return ERR_NULL_OBJECT;
1271     }
1272 
1273     data.WriteUint32(mode);
1274     WriteStreamChangeInfo(data, mode, streamChangeInfo);
1275     data.WriteRemoteObject(object);
1276 
1277     int32_t error = Remote()->SendRequest(REGISTER_TRACKER, data, reply, option);
1278     if (error != ERR_NONE) {
1279         AUDIO_ERR_LOG("RegisterTracker event failed , error: %d", error);
1280         return ERROR;
1281     }
1282 
1283     return reply.ReadInt32();
1284 }
1285 
UpdateTracker(AudioMode & mode,AudioStreamChangeInfo & streamChangeInfo)1286 int32_t AudioPolicyProxy::UpdateTracker(AudioMode &mode, AudioStreamChangeInfo &streamChangeInfo)
1287 {
1288     MessageParcel data;
1289     MessageParcel reply;
1290     MessageOption option;
1291 
1292     AUDIO_INFO_LOG("AudioPolicyProxy::UpdateTracker");
1293 
1294     if (!data.WriteInterfaceToken(GetDescriptor())) {
1295         AUDIO_ERR_LOG("UpdateTracker: WriteInterfaceToken failed");
1296         return ERROR;
1297     }
1298 
1299     data.WriteUint32(mode);
1300     WriteStreamChangeInfo(data, mode, streamChangeInfo);
1301 
1302     int32_t error = Remote()->SendRequest(UPDATE_TRACKER, data, reply, option);
1303     if (error != ERR_NONE) {
1304         AUDIO_ERR_LOG("UpdateTracker event failed , error: %d", error);
1305         return ERROR;
1306     }
1307 
1308     return reply.ReadInt32();
1309 }
1310 
ReadAudioRendererChangeInfo(MessageParcel & reply,unique_ptr<AudioRendererChangeInfo> & rendererChangeInfo)1311 void AudioPolicyProxy::ReadAudioRendererChangeInfo(MessageParcel &reply,
1312     unique_ptr<AudioRendererChangeInfo> &rendererChangeInfo)
1313 {
1314     rendererChangeInfo->sessionId = reply.ReadInt32();
1315     rendererChangeInfo->rendererState = static_cast<RendererState>(reply.ReadInt32());
1316     rendererChangeInfo->clientUID = reply.ReadInt32();
1317     rendererChangeInfo->rendererInfo.contentType = static_cast<ContentType>(reply.ReadInt32());
1318     rendererChangeInfo->rendererInfo.streamUsage = static_cast<StreamUsage>(reply.ReadInt32());
1319     rendererChangeInfo->rendererInfo.rendererFlags = reply.ReadInt32();
1320 
1321     rendererChangeInfo->outputDeviceInfo.deviceType = static_cast<DeviceType>(reply.ReadInt32());
1322     rendererChangeInfo->outputDeviceInfo.deviceRole = static_cast<DeviceRole>(reply.ReadInt32());
1323     rendererChangeInfo->outputDeviceInfo.deviceId = reply.ReadInt32();
1324     rendererChangeInfo->outputDeviceInfo.channelMasks = reply.ReadInt32();
1325     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.samplingRate
1326         = static_cast<AudioSamplingRate>(reply.ReadInt32());
1327     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.encoding
1328         = static_cast<AudioEncodingType>(reply.ReadInt32());
1329     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.format = static_cast<AudioSampleFormat>(reply.ReadInt32());
1330     rendererChangeInfo->outputDeviceInfo.audioStreamInfo.channels = static_cast<AudioChannel>(reply.ReadInt32());
1331     rendererChangeInfo->outputDeviceInfo.deviceName = reply.ReadString();
1332     rendererChangeInfo->outputDeviceInfo.macAddress = reply.ReadString();
1333 }
1334 
ReadAudioCapturerChangeInfo(MessageParcel & reply,unique_ptr<AudioCapturerChangeInfo> & capturerChangeInfo)1335 void AudioPolicyProxy::ReadAudioCapturerChangeInfo(MessageParcel &reply,
1336     unique_ptr<AudioCapturerChangeInfo> &capturerChangeInfo)
1337 {
1338     capturerChangeInfo->sessionId = reply.ReadInt32();
1339     capturerChangeInfo->capturerState = static_cast<CapturerState>(reply.ReadInt32());
1340     capturerChangeInfo->clientUID = reply.ReadInt32();
1341     capturerChangeInfo->capturerInfo.sourceType = static_cast<SourceType>(reply.ReadInt32());
1342     capturerChangeInfo->capturerInfo.capturerFlags = reply.ReadInt32();
1343 
1344     capturerChangeInfo->inputDeviceInfo.deviceType = static_cast<DeviceType>(reply.ReadInt32());
1345     capturerChangeInfo->inputDeviceInfo.deviceRole = static_cast<DeviceRole>(reply.ReadInt32());
1346     capturerChangeInfo->inputDeviceInfo.deviceId = reply.ReadInt32();
1347     capturerChangeInfo->inputDeviceInfo.channelMasks = reply.ReadInt32();
1348     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.samplingRate
1349         = static_cast<AudioSamplingRate>(reply.ReadInt32());
1350     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.encoding
1351         = static_cast<AudioEncodingType>(reply.ReadInt32());
1352     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.format = static_cast<AudioSampleFormat>(reply.ReadInt32());
1353     capturerChangeInfo->inputDeviceInfo.audioStreamInfo.channels = static_cast<AudioChannel>(reply.ReadInt32());
1354     capturerChangeInfo->inputDeviceInfo.deviceName = reply.ReadString();
1355     capturerChangeInfo->inputDeviceInfo.macAddress = reply.ReadString();
1356 }
1357 
GetCurrentRendererChangeInfos(vector<unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)1358 int32_t AudioPolicyProxy::GetCurrentRendererChangeInfos(
1359     vector<unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
1360 {
1361     MessageParcel data;
1362     MessageParcel reply;
1363     MessageOption option;
1364 
1365     AUDIO_DEBUG_LOG("AudioPolicyProxy::GetCurrentRendererChangeInfos");
1366 
1367     if (!data.WriteInterfaceToken(GetDescriptor())) {
1368         AUDIO_ERR_LOG("GetCurrentRendererChangeInfo: WriteInterfaceToken failed");
1369         return ERROR;
1370     }
1371 
1372     int32_t error = Remote()->SendRequest(GET_RENDERER_CHANGE_INFOS, data, reply, option);
1373     if (error != ERR_NONE) {
1374         AUDIO_ERR_LOG("Get Renderer change info event failed , error: %d", error);
1375         return ERROR;
1376     }
1377 
1378     int32_t size = reply.ReadInt32();
1379     while (size > 0) {
1380         unique_ptr<AudioRendererChangeInfo> rendererChangeInfo = make_unique<AudioRendererChangeInfo>();
1381         CHECK_AND_RETURN_RET_LOG(rendererChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
1382         ReadAudioRendererChangeInfo(reply, rendererChangeInfo);
1383         audioRendererChangeInfos.push_back(move(rendererChangeInfo));
1384         size--;
1385     }
1386 
1387     return SUCCESS;
1388 }
1389 
GetCurrentCapturerChangeInfos(vector<unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)1390 int32_t AudioPolicyProxy::GetCurrentCapturerChangeInfos(
1391     vector<unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
1392 {
1393     MessageParcel data;
1394     MessageParcel reply;
1395     MessageOption option;
1396 
1397     AUDIO_DEBUG_LOG("AudioPolicyProxy::GetCurrentCapturerChangeInfos");
1398 
1399     if (!data.WriteInterfaceToken(GetDescriptor())) {
1400         AUDIO_ERR_LOG("GetCurrentCapturerChangeInfos: WriteInterfaceToken failed");
1401         return ERROR;
1402     }
1403 
1404     int32_t error = Remote()->SendRequest(GET_CAPTURER_CHANGE_INFOS, data, reply, option);
1405     if (error != ERR_NONE) {
1406         AUDIO_ERR_LOG("Get capturer change info event failed , error: %d", error);
1407         return ERROR;
1408     }
1409 
1410     int32_t size = reply.ReadInt32();
1411     while (size > 0) {
1412         unique_ptr<AudioCapturerChangeInfo> capturerChangeInfo = make_unique<AudioCapturerChangeInfo>();
1413         CHECK_AND_RETURN_RET_LOG(capturerChangeInfo != nullptr, ERR_MEMORY_ALLOC_FAILED, "No memory!!");
1414         ReadAudioCapturerChangeInfo(reply, capturerChangeInfo);
1415         audioCapturerChangeInfos.push_back(move(capturerChangeInfo));
1416         size--;
1417     }
1418 
1419     return SUCCESS;
1420 }
1421 
UpdateStreamState(const int32_t clientUid,StreamSetState streamSetState,AudioStreamType audioStreamType)1422 int32_t AudioPolicyProxy::UpdateStreamState(const int32_t clientUid, StreamSetState streamSetState,
1423     AudioStreamType audioStreamType)
1424 {
1425     MessageParcel data;
1426     MessageParcel reply;
1427     MessageOption option;
1428 
1429     AUDIO_DEBUG_LOG("AudioPolicyProxy::UpdateStreamState");
1430 
1431     if (!data.WriteInterfaceToken(GetDescriptor())) {
1432         AUDIO_ERR_LOG("UpdateStreamState: WriteInterfaceToken failed");
1433         return ERROR;
1434     }
1435 
1436     data.WriteInt32(static_cast<int32_t>(clientUid));
1437     data.WriteInt32(static_cast<int32_t>(streamSetState));
1438     data.WriteInt32(static_cast<int32_t>(audioStreamType));
1439 
1440     int32_t error = Remote()->SendRequest(UPDATE_STREAM_STATE, data, reply, option);
1441     if (error != ERR_NONE) {
1442         AUDIO_ERR_LOG("UPDATE_STREAM_STATE stream changed info event failed , error: %d", error);
1443         return ERROR;
1444     }
1445 
1446     return SUCCESS;
1447 }
1448 
GetVolumeGroupInfos()1449 std::vector<sptr<VolumeGroupInfo>> AudioPolicyProxy::GetVolumeGroupInfos()
1450 {
1451     MessageParcel data;
1452     MessageParcel reply;
1453     MessageOption option;
1454 
1455     std::vector<sptr<VolumeGroupInfo>> infos;
1456 
1457     if (!data.WriteInterfaceToken(GetDescriptor())) {
1458         AUDIO_ERR_LOG("AudioPolicyProxy: GetVolumeGroupById WriteInterfaceToken failed");
1459         return infos;
1460     }
1461 
1462     int32_t error = Remote()->SendRequest(GET_VOLUME_GROUP_INFO, data, reply, option);
1463     if (error != ERR_NONE) {
1464         AUDIO_ERR_LOG("GetVolumeGroupInfo, error: %d", error);
1465         return infos;
1466     }
1467 
1468     int32_t size = reply.ReadInt32();
1469     for (int32_t i = 0; i < size; i++) {
1470         infos.push_back(VolumeGroupInfo::Unmarshalling(reply));
1471     }
1472 
1473     return infos;
1474 }
1475 
IsAudioRendererLowLatencySupported(const AudioStreamInfo & audioStreamInfo)1476 bool AudioPolicyProxy::IsAudioRendererLowLatencySupported(const AudioStreamInfo &audioStreamInfo)
1477 {
1478     MessageParcel data;
1479     MessageParcel reply;
1480     MessageOption option;
1481 
1482     if (!data.WriteInterfaceToken(GetDescriptor())) {
1483         AUDIO_ERR_LOG("IsAudioRendererLowLatencySupported WriteInterfaceToken failed");
1484         return IPC_PROXY_ERR;
1485     }
1486     WriteAudioStreamInfoParams(data, audioStreamInfo);
1487     int32_t error = Remote()->SendRequest(IS_AUDIO_RENDER_LOW_LATENCY_SUPPORTED, data, reply, option);
1488     if (error != ERR_NONE) {
1489         AUDIO_ERR_LOG("IsAudioRendererLowLatencySupported, error: %d", error);
1490         return ERR_TRANSACTION_FAILED;
1491     }
1492 
1493     return reply.ReadBool();
1494 }
1495 } // namespace AudioStandard
1496 } // namespace OHOS
1497