1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyProxy"
17 #endif
18 
19 
20 #include "audio_policy_log.h"
21 #include "audio_policy_proxy.h"
22 
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 using namespace std;
27 
SetAudioInterruptCallback(const uint32_t sessionID,const sptr<IRemoteObject> & object,uint32_t clientUid,const int32_t zoneID)28 int32_t AudioPolicyProxy::SetAudioInterruptCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object,
29     uint32_t clientUid, const int32_t zoneID)
30 {
31     MessageParcel data;
32     MessageParcel reply;
33     MessageOption option;
34 
35     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT,
36         "SetAudioInterruptCallback object is null");
37     bool ret = data.WriteInterfaceToken(GetDescriptor());
38     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
39     data.WriteUint32(sessionID);
40     (void)data.WriteRemoteObject(object);
41     data.WriteInt32(zoneID);
42     data.WriteUint32(clientUid);
43     int error = Remote()->SendRequest(
44         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_CALLBACK), data, reply, option);
45     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
46         "set callback failed, error: %{public}d", error);
47 
48     return reply.ReadInt32();
49 }
50 
UnsetAudioInterruptCallback(const uint32_t sessionID,const int32_t zoneID)51 int32_t AudioPolicyProxy::UnsetAudioInterruptCallback(const uint32_t sessionID,
52     const int32_t zoneID)
53 {
54     MessageParcel data;
55     MessageParcel reply;
56     MessageOption option;
57 
58     bool ret = data.WriteInterfaceToken(GetDescriptor());
59     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
60     data.WriteUint32(sessionID);
61     data.WriteInt32(zoneID);
62     int error = Remote()->SendRequest(
63         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_CALLBACK), data, reply, option);
64     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
65         "unset callback failed, error: %{public}d", error);
66 
67     return reply.ReadInt32();
68 }
69 
SetAudioManagerInterruptCallback(const int32_t clientId,const sptr<IRemoteObject> & object)70 int32_t AudioPolicyProxy::SetAudioManagerInterruptCallback(const int32_t clientId, const sptr<IRemoteObject> &object)
71 {
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option;
75 
76     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT,
77         "SetAudioManagerInterruptCallback object is null");
78     bool ret = data.WriteInterfaceToken(GetDescriptor());
79     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
80     data.WriteInt32(clientId);
81     (void)data.WriteRemoteObject(object);
82     int error = Remote()->SendRequest(
83         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_INTERRUPT_CALLBACK), data, reply, option);
84     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
85         "set callback failed, error: %{public}d", error);
86 
87     return reply.ReadInt32();
88 }
89 
UnsetAudioManagerInterruptCallback(const int32_t clientId)90 int32_t AudioPolicyProxy::UnsetAudioManagerInterruptCallback(const int32_t clientId)
91 {
92     MessageParcel data;
93     MessageParcel reply;
94     MessageOption option;
95 
96     bool ret = data.WriteInterfaceToken(GetDescriptor());
97     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
98 
99     data.WriteInt32(clientId);
100 
101     int error = Remote()->SendRequest(
102         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_INTERRUPT_CALLBACK), data, reply, option);
103     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
104         "unset callback failed, error: %{public}d", error);
105 
106     return reply.ReadInt32();
107 }
108 
SetQueryClientTypeCallback(const sptr<IRemoteObject> & object)109 int32_t AudioPolicyProxy::SetQueryClientTypeCallback(const sptr<IRemoteObject> &object)
110 {
111     MessageParcel data;
112     MessageParcel reply;
113     MessageOption option;
114 
115     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT,
116         "SetAudioInterruptCallback object is null");
117     bool ret = data.WriteInterfaceToken(GetDescriptor());
118     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
119     (void)data.WriteRemoteObject(object);
120     int error = Remote()->SendRequest(
121         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_QUERY_CLIENT_TYPE_CALLBACK), data, reply, option);
122     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
123         "set callback failed, error: %{public}d", error);
124     return reply.ReadInt32();
125 }
126 
SetAvailableDeviceChangeCallback(const int32_t clientId,const AudioDeviceUsage usage,const sptr<IRemoteObject> & object)127 int32_t AudioPolicyProxy::SetAvailableDeviceChangeCallback(const int32_t clientId, const AudioDeviceUsage usage,
128     const sptr<IRemoteObject> &object)
129 {
130     MessageParcel data;
131     MessageParcel reply;
132     MessageOption option;
133 
134     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "SetAvailableDeviceChangeCallback object is null");
135 
136     bool token = data.WriteInterfaceToken(GetDescriptor());
137     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data WriteInterfaceToken failed");
138     token = data.WriteInt32(clientId) && data.WriteInt32(usage);
139     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data write failed");
140 
141     token = data.WriteRemoteObject(object);
142     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data WriteRemoteObject failed");
143 
144     int error = Remote()->SendRequest(
145         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_AVAILABLE_DEVICE_CHANGE_CALLBACK), data, reply, option);
146     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
147         "SetAvailableDeviceChangeCallback failed, error: %{public}d", error);
148 
149     return reply.ReadInt32();
150 }
151 
UnsetAvailableDeviceChangeCallback(const int32_t clientId,AudioDeviceUsage usage)152 int32_t AudioPolicyProxy::UnsetAvailableDeviceChangeCallback(const int32_t clientId, AudioDeviceUsage usage)
153 {
154     MessageParcel data;
155     MessageParcel reply;
156     MessageOption option;
157 
158     bool token = data.WriteInterfaceToken(GetDescriptor());
159     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data WriteInterfaceToken failed");
160     token = data.WriteInt32(clientId) && data.WriteInt32(usage);
161     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data write failed");
162 
163     int error = Remote()->SendRequest(
164         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_AVAILABLE_DEVICE_CHANGE_CALLBACK), data, reply, option);
165     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
166         "UnsetAvailableDeviceChangeCallback failed, error: %{public}d", error);
167 
168     return reply.ReadInt32();
169 }
170 
171 
SetAudioConcurrencyCallback(const uint32_t sessionID,const sptr<IRemoteObject> & object)172 int32_t AudioPolicyProxy::SetAudioConcurrencyCallback(const uint32_t sessionID,
173     const sptr<IRemoteObject> &object)
174 {
175     MessageParcel data;
176     MessageParcel reply;
177     MessageOption option;
178 
179     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT,
180         "SetAudioConcurrencyCallback object is null");
181     bool ret = data.WriteInterfaceToken(GetDescriptor());
182     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
183 
184     data.WriteUint32(sessionID);
185     (void)data.WriteRemoteObject(object);
186     int32_t error = Remote()->SendRequest(
187         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_AUDIO_CONCURRENCY_CALLBACK), data, reply, option);
188     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
189 
190     return reply.ReadInt32();
191 }
192 
UnsetAudioConcurrencyCallback(const uint32_t sessionID)193 int32_t AudioPolicyProxy::UnsetAudioConcurrencyCallback(const uint32_t sessionID)
194 {
195     MessageParcel data;
196     MessageParcel reply;
197     MessageOption option;
198 
199     bool ret = data.WriteInterfaceToken(GetDescriptor());
200     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
201     data.WriteUint32(sessionID);
202     int error = Remote()->SendRequest(
203         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_AUDIO_CONCURRENCY_CALLBACK), data, reply, option);
204     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
205         "unset concurrency callback failed, error: %{public}d", error);
206 
207     return reply.ReadInt32();
208 }
209 
SetDistributedRoutingRoleCallback(const sptr<IRemoteObject> & object)210 int32_t AudioPolicyProxy::SetDistributedRoutingRoleCallback(const sptr<IRemoteObject> &object)
211 {
212     MessageParcel data;
213     MessageParcel reply;
214     MessageOption option;
215 
216     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null");
217     bool ret = data.WriteInterfaceToken(GetDescriptor());
218     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
219     (void)data.WriteRemoteObject(object);
220     int error = Remote()->SendRequest(
221         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DISTRIBUTED_ROUTING_ROLE_CALLBACK), data, reply, option);
222     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "failed error : %{public}d", error);
223     return reply.ReadInt32();
224 }
225 
UnsetDistributedRoutingRoleCallback()226 int32_t AudioPolicyProxy::UnsetDistributedRoutingRoleCallback()
227 {
228     MessageParcel data;
229     MessageParcel reply;
230     MessageOption option;
231 
232     bool token = data.WriteInterfaceToken(GetDescriptor());
233     CHECK_AND_RETURN_RET_LOG(token, ERROR, "data writeInterfaceToken failed");
234 
235     int error = Remote()->SendRequest(
236         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_DISTRIBUTED_ROUTING_ROLE_CALLBACK), data, reply, option);
237     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error,
238         "AudioPolicyProxy UnsetDistributedRoutingRoleCallback failed error : %{public}d", error);
239     return reply.ReadInt32();
240 }
241 
SetClientCallbacksEnable(const CallbackChange & callbackchange,const bool & enable)242 int32_t AudioPolicyProxy::SetClientCallbacksEnable(const CallbackChange &callbackchange, const bool &enable)
243 {
244     MessageParcel data;
245     MessageParcel reply;
246     MessageOption option;
247 
248     bool ret = data.WriteInterfaceToken(GetDescriptor());
249     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
250     data.WriteInt32(static_cast<int32_t>(callbackchange));
251     data.WriteBool(enable);
252 
253     int error = Remote()->SendRequest(
254         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_CALLBACKS_ENABLE), data, reply, option);
255     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "Set client callback failed, error: %{public}d", error);
256 
257     return reply.ReadInt32();
258 }
259 
RegisterPolicyCallbackClient(const sptr<IRemoteObject> & object,const int32_t zoneID)260 int32_t AudioPolicyProxy::RegisterPolicyCallbackClient(const sptr<IRemoteObject> &object, const int32_t zoneID)
261 {
262     MessageParcel data;
263     MessageParcel reply;
264     MessageOption option;
265 
266     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "RegisterPolicyCallbackClient object is null");
267     bool ret = data.WriteInterfaceToken(GetDescriptor());
268     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
269 
270     data.WriteRemoteObject(object);
271     data.WriteInt32(zoneID);
272     int error = Remote()->SendRequest(
273         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_POLICY_CALLBACK_CLIENT), data, reply, option);
274     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SendRequest failed , error: %{public}d", error);
275     return reply.ReadInt32();
276 }
277 
SetAudioDeviceRefinerCallback(const sptr<IRemoteObject> & object)278 int32_t AudioPolicyProxy::SetAudioDeviceRefinerCallback(const sptr<IRemoteObject> &object)
279 {
280     MessageParcel data;
281     MessageParcel reply;
282     MessageOption option;
283 
284     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "object is null");
285 
286     bool ret = data.WriteInterfaceToken(GetDescriptor());
287     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
288 
289     (void)data.WriteRemoteObject(object);
290     int32_t error = Remote()->SendRequest(
291         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_AUDIO_DEVICE_REFINER_CALLBACK), data, reply, option);
292     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
293     return reply.ReadInt32();
294 }
295 
UnsetAudioDeviceRefinerCallback()296 int32_t AudioPolicyProxy::UnsetAudioDeviceRefinerCallback()
297 {
298     MessageParcel data;
299     MessageParcel reply;
300     MessageOption option;
301 
302     bool ret = data.WriteInterfaceToken(GetDescriptor());
303     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
304 
305     int32_t error = Remote()->SendRequest(
306         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNSET_AUDIO_DEVICE_REFINER_CALLBACK), data, reply, option);
307     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "SendRequest failed, error: %{public}d", error);
308     return reply.ReadInt32();
309 }
310 
311 
RegisterSpatializationStateEventListener(const uint32_t sessionID,const StreamUsage streamUsage,const sptr<IRemoteObject> & object)312 int32_t AudioPolicyProxy::RegisterSpatializationStateEventListener(const uint32_t sessionID,
313     const StreamUsage streamUsage, const sptr<IRemoteObject> &object)
314 {
315     MessageParcel data;
316     MessageParcel reply;
317     MessageOption option;
318 
319     bool ret = data.WriteInterfaceToken(GetDescriptor());
320     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
321     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT, "SpatializationStateEventListener object is null");
322 
323     data.WriteInt32(static_cast<int32_t>(sessionID));
324     data.WriteInt32(static_cast<int32_t>(streamUsage));
325     data.WriteRemoteObject(object);
326     int32_t error = Remote() ->SendRequest(
327         static_cast<uint32_t>(AudioPolicyInterfaceCode::REGISTER_SPATIALIZATION_STATE_EVENT), data, reply, option);
328     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SendRequest failed , error: %{public}d", error);
329 
330     return reply.ReadInt32();
331 }
332 
UnregisterSpatializationStateEventListener(const uint32_t sessionID)333 int32_t AudioPolicyProxy::UnregisterSpatializationStateEventListener(const uint32_t sessionID)
334 {
335     MessageParcel data;
336     MessageParcel reply;
337     MessageOption option;
338 
339     bool ret = data.WriteInterfaceToken(GetDescriptor());
340     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
341 
342     data.WriteInt32(static_cast<int32_t>(sessionID));
343     int32_t error = Remote() ->SendRequest(
344         static_cast<uint32_t>(AudioPolicyInterfaceCode::UNREGISTER_SPATIALIZATION_STATE_EVENT), data, reply, option);
345     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SendRequest failed , error: %{public}d", error);
346 
347     return reply.ReadInt32();
348 }
349 
350 } // namespace AudioStandard
351 } // namespace OHOS