• 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_errors.h"
17 #include "audio_policy_server.h"
18 #include "audio_policy_types.h"
19 #include "media_log.h"
20 #include "audio_policy_manager_stub.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
ReadAudioInterruptParams(MessageParcel & data,AudioInterrupt & audioInterrupt)24 void AudioPolicyManagerStub::ReadAudioInterruptParams(MessageParcel &data, AudioInterrupt &audioInterrupt)
25 {
26     audioInterrupt.streamUsage = static_cast<StreamUsage>(data.ReadInt32());
27     audioInterrupt.contentType = static_cast<ContentType>(data.ReadInt32());
28     audioInterrupt.streamType = static_cast<AudioStreamType>(data.ReadInt32());
29     audioInterrupt.sessionID = data.ReadUint32();
30 }
31 
ReadAudioManagerInterruptParams(MessageParcel & data,AudioInterrupt & audioInterrupt)32 void AudioPolicyManagerStub::ReadAudioManagerInterruptParams(MessageParcel &data, AudioInterrupt &audioInterrupt)
33 {
34     audioInterrupt.streamUsage = static_cast<StreamUsage>(data.ReadInt32());
35     audioInterrupt.contentType = static_cast<ContentType>(data.ReadInt32());
36     audioInterrupt.streamType = static_cast<AudioStreamType>(data.ReadInt32());
37     audioInterrupt.pauseWhenDucked = data.ReadBool();
38 }
39 
WriteAudioInteruptParams(MessageParcel & reply,const AudioInterrupt & audioInterrupt)40 void AudioPolicyManagerStub::WriteAudioInteruptParams(MessageParcel &reply, const AudioInterrupt &audioInterrupt)
41 {
42     reply.WriteInt32(static_cast<int32_t>(audioInterrupt.streamUsage));
43     reply.WriteInt32(static_cast<int32_t>(audioInterrupt.contentType));
44     reply.WriteInt32(static_cast<int32_t>(audioInterrupt.streamType));
45     reply.WriteUint32(audioInterrupt.sessionID);
46 }
47 
SetStreamVolumeInternal(MessageParcel & data,MessageParcel & reply)48 void AudioPolicyManagerStub::SetStreamVolumeInternal(MessageParcel &data, MessageParcel &reply)
49 {
50     AudioStreamType streamType = static_cast<AudioStreamType>(data.ReadInt32());
51     float volume = data.ReadFloat();
52     int result = SetStreamVolume(streamType, volume);
53     if (result == SUCCESS)
54         reply.WriteInt32(MEDIA_OK);
55     else
56         reply.WriteInt32(MEDIA_ERR);
57 }
58 
SetRingerModeInternal(MessageParcel & data,MessageParcel & reply)59 void AudioPolicyManagerStub::SetRingerModeInternal(MessageParcel &data, MessageParcel &reply)
60 {
61     AudioRingerMode rMode = static_cast<AudioRingerMode>(data.ReadInt32());
62     int32_t result = SetRingerMode(rMode);
63     reply.WriteInt32(result);
64 }
65 
GetRingerModeInternal(MessageParcel & reply)66 void AudioPolicyManagerStub::GetRingerModeInternal(MessageParcel &reply)
67 {
68     AudioRingerMode rMode = GetRingerMode();
69     reply.WriteInt32(static_cast<int>(rMode));
70 }
71 
SetAudioSceneInternal(MessageParcel & data,MessageParcel & reply)72 void AudioPolicyManagerStub::SetAudioSceneInternal(MessageParcel &data, MessageParcel &reply)
73 {
74     AudioScene audioScene = static_cast<AudioScene>(data.ReadInt32());
75     int32_t result = SetAudioScene(audioScene);
76     reply.WriteInt32(result);
77 }
78 
GetAudioSceneInternal(MessageParcel & reply)79 void AudioPolicyManagerStub::GetAudioSceneInternal(MessageParcel &reply)
80 {
81     AudioScene audioScene = GetAudioScene();
82     reply.WriteInt32(static_cast<int>(audioScene));
83 }
84 
GetStreamVolumeInternal(MessageParcel & data,MessageParcel & reply)85 void AudioPolicyManagerStub::GetStreamVolumeInternal(MessageParcel &data, MessageParcel &reply)
86 {
87     AudioStreamType streamType = static_cast<AudioStreamType>(data.ReadInt32());
88     float volume = GetStreamVolume(streamType);
89     reply.WriteFloat(volume);
90 }
91 
SetStreamMuteInternal(MessageParcel & data,MessageParcel & reply)92 void AudioPolicyManagerStub::SetStreamMuteInternal(MessageParcel &data, MessageParcel &reply)
93 {
94     AudioStreamType streamType = static_cast<AudioStreamType>(data.ReadInt32());
95     bool mute = data.ReadBool();
96     int result = SetStreamMute(streamType, mute);
97     if (result == SUCCESS)
98         reply.WriteInt32(MEDIA_OK);
99     else
100         reply.WriteInt32(MEDIA_ERR);
101 }
102 
GetStreamMuteInternal(MessageParcel & data,MessageParcel & reply)103 void AudioPolicyManagerStub::GetStreamMuteInternal(MessageParcel &data, MessageParcel &reply)
104 {
105     AudioStreamType streamType = static_cast<AudioStreamType>(data.ReadInt32());
106     bool mute = GetStreamMute(streamType);
107     reply.WriteBool(mute);
108 }
109 
IsStreamActiveInternal(MessageParcel & data,MessageParcel & reply)110 void AudioPolicyManagerStub::IsStreamActiveInternal(MessageParcel &data, MessageParcel &reply)
111 {
112     AudioStreamType streamType = static_cast<AudioStreamType>(data.ReadInt32());
113     bool isActive = IsStreamActive(streamType);
114     reply.WriteBool(isActive);
115 }
116 
GetDevicesInternal(MessageParcel & data,MessageParcel & reply)117 void AudioPolicyManagerStub::GetDevicesInternal(MessageParcel &data, MessageParcel &reply)
118 {
119     MEDIA_DEBUG_LOG("GET_DEVICES AudioManagerStub");
120     int deviceFlag = data.ReadInt32();
121     DeviceFlag deviceFlagConfig = static_cast<DeviceFlag>(deviceFlag);
122     std::vector<sptr<AudioDeviceDescriptor>> devices = GetDevices(deviceFlagConfig);
123     int32_t size = static_cast<int32_t>(devices.size());
124     MEDIA_DEBUG_LOG("GET_DEVICES size= %{public}d", size);
125     reply.WriteInt32(size);
126     for (int i = 0; i < size; i++) {
127         devices[i]->Marshalling(reply);
128     }
129 }
130 
SetDeviceActiveInternal(MessageParcel & data,MessageParcel & reply)131 void AudioPolicyManagerStub::SetDeviceActiveInternal(MessageParcel &data, MessageParcel &reply)
132 {
133     InternalDeviceType deviceType = static_cast<InternalDeviceType>(data.ReadInt32());
134     bool active = data.ReadBool();
135     int32_t result = SetDeviceActive(deviceType, active);
136     if (result == SUCCESS)
137         reply.WriteInt32(MEDIA_OK);
138     else
139         reply.WriteInt32(MEDIA_ERR);
140 }
141 
IsDeviceActiveInternal(MessageParcel & data,MessageParcel & reply)142 void AudioPolicyManagerStub::IsDeviceActiveInternal(MessageParcel &data, MessageParcel &reply)
143 {
144     InternalDeviceType deviceType = static_cast<InternalDeviceType>(data.ReadInt32());
145     bool result = IsDeviceActive(deviceType);
146     reply.WriteBool(result);
147 }
148 
SetRingerModeCallbackInternal(MessageParcel & data,MessageParcel & reply)149 void AudioPolicyManagerStub::SetRingerModeCallbackInternal(MessageParcel &data, MessageParcel &reply)
150 {
151     int32_t clientId = data.ReadInt32();
152     sptr<IRemoteObject> object = data.ReadRemoteObject();
153     if (object == nullptr) {
154         MEDIA_ERR_LOG("AudioPolicyManagerStub: SetRingerModeCallback obj is null");
155         return;
156     }
157     int32_t result = SetRingerModeCallback(clientId, object);
158     reply.WriteInt32(result);
159 }
160 
UnsetRingerModeCallbackInternal(MessageParcel & data,MessageParcel & reply)161 void AudioPolicyManagerStub::UnsetRingerModeCallbackInternal(MessageParcel &data, MessageParcel &reply)
162 {
163     int32_t clientId = data.ReadInt32();
164     int32_t result = UnsetRingerModeCallback(clientId);
165     reply.WriteInt32(result);
166 }
167 
SetDeviceChangeCallbackInternal(MessageParcel & data,MessageParcel & reply)168 void AudioPolicyManagerStub::SetDeviceChangeCallbackInternal(MessageParcel &data, MessageParcel &reply)
169 {
170     int32_t clientId = data.ReadInt32();
171     sptr<IRemoteObject> object = data.ReadRemoteObject();
172     if (object == nullptr) {
173         MEDIA_ERR_LOG("AudioPolicyManagerStub: AudioInterruptCallback obj is null");
174         return;
175     }
176     int32_t result = SetDeviceChangeCallback(clientId, object);
177     reply.WriteInt32(result);
178 }
179 
UnsetDeviceChangeCallbackInternal(MessageParcel & data,MessageParcel & reply)180 void AudioPolicyManagerStub::UnsetDeviceChangeCallbackInternal(MessageParcel &data, MessageParcel &reply)
181 {
182     int32_t clientId = data.ReadInt32();
183     int32_t result = UnsetDeviceChangeCallback(clientId);
184     reply.WriteInt32(result);
185 }
186 
SetInterruptCallbackInternal(MessageParcel & data,MessageParcel & reply)187 void AudioPolicyManagerStub::SetInterruptCallbackInternal(MessageParcel &data, MessageParcel &reply)
188 {
189     uint32_t sessionID = data.ReadUint32();
190     sptr<IRemoteObject> object = data.ReadRemoteObject();
191     if (object == nullptr) {
192         MEDIA_ERR_LOG("AudioPolicyManagerStub: AudioInterruptCallback obj is null");
193         return;
194     }
195     int32_t result = SetAudioInterruptCallback(sessionID, object);
196     reply.WriteInt32(result);
197 }
198 
UnsetInterruptCallbackInternal(MessageParcel & data,MessageParcel & reply)199 void AudioPolicyManagerStub::UnsetInterruptCallbackInternal(MessageParcel &data, MessageParcel &reply)
200 {
201     uint32_t sessionID = data.ReadUint32();
202     int32_t result = UnsetAudioInterruptCallback(sessionID);
203     reply.WriteInt32(result);
204 }
205 
ActivateInterruptInternal(MessageParcel & data,MessageParcel & reply)206 void AudioPolicyManagerStub::ActivateInterruptInternal(MessageParcel &data, MessageParcel &reply)
207 {
208     AudioInterrupt audioInterrupt = {};
209     ReadAudioInterruptParams(data, audioInterrupt);
210     int32_t result = ActivateAudioInterrupt(audioInterrupt);
211     reply.WriteInt32(result);
212 }
213 
DeactivateInterruptInternal(MessageParcel & data,MessageParcel & reply)214 void AudioPolicyManagerStub::DeactivateInterruptInternal(MessageParcel &data, MessageParcel &reply)
215 {
216     AudioInterrupt audioInterrupt = {};
217     ReadAudioInterruptParams(data, audioInterrupt);
218     int32_t result = DeactivateAudioInterrupt(audioInterrupt);
219     reply.WriteInt32(result);
220 }
221 
SetAudioManagerInterruptCbInternal(MessageParcel & data,MessageParcel & reply)222 void AudioPolicyManagerStub::SetAudioManagerInterruptCbInternal(MessageParcel &data, MessageParcel &reply)
223 {
224     uint32_t clientID = data.ReadUint32();
225     sptr<IRemoteObject> object = data.ReadRemoteObject();
226     if (object == nullptr) {
227         MEDIA_ERR_LOG("AudioPolicyManagerStub: AudioInterruptCallback obj is null");
228         return;
229     }
230     int32_t result = SetAudioManagerInterruptCallback(clientID, object);
231     reply.WriteInt32(result);
232 }
233 
UnsetAudioManagerInterruptCbInternal(MessageParcel & data,MessageParcel & reply)234 void AudioPolicyManagerStub::UnsetAudioManagerInterruptCbInternal(MessageParcel &data, MessageParcel &reply)
235 {
236     uint32_t clientID = data.ReadUint32();
237     int32_t result = UnsetAudioManagerInterruptCallback(clientID);
238     reply.WriteInt32(result);
239 }
240 
RequestAudioFocusInternal(MessageParcel & data,MessageParcel & reply)241 void AudioPolicyManagerStub::RequestAudioFocusInternal(MessageParcel &data, MessageParcel &reply)
242 {
243     AudioInterrupt audioInterrupt = {};
244     uint32_t clientID = data.ReadUint32();
245     ReadAudioManagerInterruptParams(data, audioInterrupt);
246     int32_t result = RequestAudioFocus(clientID, audioInterrupt);
247     reply.WriteInt32(result);
248 }
249 
AbandonAudioFocusInternal(MessageParcel & data,MessageParcel & reply)250 void AudioPolicyManagerStub::AbandonAudioFocusInternal(MessageParcel &data, MessageParcel &reply)
251 {
252     AudioInterrupt audioInterrupt = {};
253     uint32_t clientID = data.ReadUint32();
254     ReadAudioManagerInterruptParams(data, audioInterrupt);
255     int32_t result = AbandonAudioFocus(clientID, audioInterrupt);
256     reply.WriteInt32(result);
257 }
258 
GetStreamInFocusInternal(MessageParcel & reply)259 void AudioPolicyManagerStub::GetStreamInFocusInternal(MessageParcel &reply)
260 {
261     AudioStreamType streamInFocus = GetStreamInFocus();
262     reply.WriteInt32(static_cast<int32_t>(streamInFocus));
263 }
264 
GetSessionInfoInFocusInternal(MessageParcel & reply)265 void AudioPolicyManagerStub::GetSessionInfoInFocusInternal(MessageParcel &reply)
266 {
267     uint32_t invalidSessionID = static_cast<uint32_t>(-1);
268     AudioInterrupt audioInterrupt {STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, STREAM_DEFAULT, invalidSessionID};
269     int32_t ret = GetSessionInfoInFocus(audioInterrupt);
270     WriteAudioInteruptParams(reply, audioInterrupt);
271     reply.WriteInt32(ret);
272 }
273 
SetVolumeKeyEventCallbackInternal(MessageParcel & data,MessageParcel & reply)274 void AudioPolicyManagerStub::SetVolumeKeyEventCallbackInternal(MessageParcel &data, MessageParcel &reply)
275 {
276     int32_t clientPid =  data.ReadInt32();
277     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
278     if (remoteObject == nullptr) {
279         MEDIA_ERR_LOG("AudioPolicyManagerStub: AudioManagerCallback obj is null");
280         return;
281     }
282     int ret = SetVolumeKeyEventCallback(clientPid, remoteObject);
283     reply.WriteInt32(ret);
284 }
285 
UnsetVolumeKeyEventCallbackInternal(MessageParcel & data,MessageParcel & reply)286 void AudioPolicyManagerStub::UnsetVolumeKeyEventCallbackInternal(MessageParcel &data, MessageParcel &reply)
287 {
288     int32_t clientPid = data.ReadInt32();
289     int ret = UnsetVolumeKeyEventCallback(clientPid);
290     reply.WriteInt32(ret);
291 }
292 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)293 int AudioPolicyManagerStub::OnRemoteRequest(
294     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
295 {
296     if (data.ReadInterfaceToken() != GetDescriptor()) {
297         MEDIA_ERR_LOG("AudioPolicyManagerStub: ReadInterfaceToken failed");
298         return -1;
299     }
300     switch (code) {
301         case SET_STREAM_VOLUME:
302             SetStreamVolumeInternal(data, reply);
303             break;
304 
305         case SET_RINGER_MODE:
306             SetRingerModeInternal(data, reply);
307             break;
308 
309         case GET_RINGER_MODE:
310             GetRingerModeInternal(reply);
311             break;
312 
313         case SET_AUDIO_SCENE:
314             SetAudioSceneInternal(data, reply);
315             break;
316 
317         case GET_AUDIO_SCENE:
318             GetAudioSceneInternal(reply);
319             break;
320 
321         case GET_STREAM_VOLUME:
322             GetStreamVolumeInternal(data, reply);
323             break;
324 
325         case SET_STREAM_MUTE:
326             SetStreamMuteInternal(data, reply);
327             break;
328 
329         case GET_STREAM_MUTE:
330             GetStreamMuteInternal(data, reply);
331             break;
332 
333         case IS_STREAM_ACTIVE:
334             IsStreamActiveInternal(data, reply);
335             break;
336 
337         case SET_DEVICE_ACTIVE:
338             SetDeviceActiveInternal(data, reply);
339             break;
340 
341         case IS_DEVICE_ACTIVE:
342             IsDeviceActiveInternal(data, reply);
343             break;
344 
345         case SET_RINGERMODE_CALLBACK:
346             SetRingerModeCallbackInternal(data, reply);
347             break;
348 
349         case UNSET_RINGERMODE_CALLBACK:
350             UnsetRingerModeCallbackInternal(data, reply);
351             break;
352 
353         case SET_DEVICE_CHANGE_CALLBACK:
354             SetDeviceChangeCallbackInternal(data, reply);
355             break;
356 
357         case UNSET_DEVICE_CHANGE_CALLBACK:
358             UnsetDeviceChangeCallbackInternal(data, reply);
359             break;
360 
361         case SET_CALLBACK:
362             SetInterruptCallbackInternal(data, reply);
363             break;
364 
365         case UNSET_CALLBACK:
366             UnsetInterruptCallbackInternal(data, reply);
367             break;
368 
369         case ACTIVATE_INTERRUPT:
370             ActivateInterruptInternal(data, reply);
371             break;
372 
373         case DEACTIVATE_INTERRUPT:
374             DeactivateInterruptInternal(data, reply);
375             break;
376 
377         case SET_INTERRUPT_CALLBACK:
378             SetAudioManagerInterruptCbInternal(data, reply);
379             break;
380 
381         case UNSET_INTERRUPT_CALLBACK:
382             UnsetAudioManagerInterruptCbInternal(data, reply);
383             break;
384 
385         case REQUEST_AUDIO_FOCUS:
386             RequestAudioFocusInternal(data, reply);
387             break;
388 
389         case ABANDON_AUDIO_FOCUS:
390             AbandonAudioFocusInternal(data, reply);
391             break;
392 
393         case SET_VOLUME_KEY_EVENT_CALLBACK:
394             SetVolumeKeyEventCallbackInternal(data, reply);
395             break;
396 
397         case UNSET_VOLUME_KEY_EVENT_CALLBACK:
398             UnsetVolumeKeyEventCallbackInternal(data, reply);
399             break;
400 
401         case GET_STREAM_IN_FOCUS:
402             GetStreamInFocusInternal(reply);
403             break;
404 
405         case GET_SESSION_INFO_IN_FOCUS:
406             GetSessionInfoInFocusInternal(reply);
407             break;
408 
409         case GET_DEVICES:
410             GetDevicesInternal(data, reply);
411             break;
412 
413         default:
414             MEDIA_ERR_LOG("default case, need check AudioPolicyManagerStub");
415             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
416     }
417     return MEDIA_OK;
418 }
419 
IsPermissionValid()420 bool AudioPolicyManagerStub::IsPermissionValid()
421 {
422     return true;
423 }
424 } // namespace audio_policy
425 } // namespace OHOS
426