1 /*
2  * Copyright (c) 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 #include "audio_policy_proxy.h"
20 
21 #include "audio_policy_log.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 using namespace std;
26 
GetMaxVolumeLevel(AudioVolumeType volumeType)27 int32_t AudioPolicyProxy::GetMaxVolumeLevel(AudioVolumeType volumeType)
28 {
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option;
32 
33     bool ret = data.WriteInterfaceToken(GetDescriptor());
34     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
35 
36     data.WriteInt32(static_cast<int32_t>(volumeType));
37     int32_t error = Remote()->SendRequest(
38         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_VOLUMELEVEL), data, reply, option);
39     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get max volume failed, error: %d", error);
40     return reply.ReadInt32();
41 }
42 
GetMinVolumeLevel(AudioVolumeType volumeType)43 int32_t AudioPolicyProxy::GetMinVolumeLevel(AudioVolumeType volumeType)
44 {
45     MessageParcel data;
46     MessageParcel reply;
47     MessageOption option;
48 
49     bool ret = data.WriteInterfaceToken(GetDescriptor());
50     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
51     data.WriteInt32(static_cast<int32_t>(volumeType));
52     int32_t error = Remote()->SendRequest(
53         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MIN_VOLUMELEVEL), data, reply, option);
54     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get min volume failed, error: %d", error);
55     return reply.ReadInt32();
56 }
57 
SetSystemVolumeLevelLegacy(AudioVolumeType volumeType,int32_t volumeLevel)58 int32_t AudioPolicyProxy::SetSystemVolumeLevelLegacy(AudioVolumeType volumeType, int32_t volumeLevel)
59 {
60     MessageParcel data;
61     MessageParcel reply;
62     MessageOption option;
63     bool ret = data.WriteInterfaceToken(GetDescriptor());
64     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
65 
66     data.WriteInt32(static_cast<int32_t>(volumeType));
67     data.WriteInt32(volumeLevel);
68     int32_t error = Remote()->SendRequest(
69         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SYSTEM_VOLUMELEVEL_LEGACY), data, reply, option);
70     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set volume failed, error: %d", error);
71     return reply.ReadInt32();
72 }
73 
SetSystemVolumeLevel(AudioVolumeType volumeType,int32_t volumeLevel,int32_t volumeFlag)74 int32_t AudioPolicyProxy::SetSystemVolumeLevel(AudioVolumeType volumeType, int32_t volumeLevel, int32_t volumeFlag)
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option;
79     bool ret = data.WriteInterfaceToken(GetDescriptor());
80     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
81 
82     data.WriteInt32(static_cast<int32_t>(volumeType));
83     data.WriteInt32(volumeLevel);
84     data.WriteInt32(volumeFlag);
85     int32_t error = Remote()->SendRequest(
86         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_SYSTEM_VOLUMELEVEL), data, reply, option);
87     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set volume failed, error: %d", error);
88     return reply.ReadInt32();
89 }
90 
GetSystemActiveVolumeType(const int32_t clientUid)91 AudioStreamType AudioPolicyProxy::GetSystemActiveVolumeType(const int32_t clientUid)
92 {
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96 
97     bool ret = data.WriteInterfaceToken(GetDescriptor());
98     CHECK_AND_RETURN_RET_LOG(ret, STREAM_DEFAULT, "WriteInterfaceToken failed");
99     data.WriteInt32(clientUid);
100     int32_t error = Remote()->SendRequest(
101         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_ACTIVEVOLUME_TYPE), data, reply, option);
102     if (error != ERR_NONE) {
103         AUDIO_ERR_LOG("get stream in focus failed, error: %d", error);
104     }
105     return static_cast<AudioStreamType>(reply.ReadInt32());
106 }
107 
GetSystemVolumeLevel(AudioVolumeType volumeType)108 int32_t AudioPolicyProxy::GetSystemVolumeLevel(AudioVolumeType volumeType)
109 {
110     MessageParcel data;
111     MessageParcel reply;
112     MessageOption option;
113 
114     bool ret = data.WriteInterfaceToken(GetDescriptor());
115     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
116     data.WriteInt32(static_cast<int32_t>(volumeType));
117     int32_t error = Remote()->SendRequest(
118         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_VOLUMELEVEL), data, reply, option);
119     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get volume failed, error: %d", error);
120     return reply.ReadInt32();
121 }
122 
SetLowPowerVolume(int32_t streamId,float volume)123 int32_t AudioPolicyProxy::SetLowPowerVolume(int32_t streamId, float volume)
124 {
125     MessageParcel data;
126     MessageParcel reply;
127     MessageOption option;
128     bool ret = data.WriteInterfaceToken(GetDescriptor());
129     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
130 
131     data.WriteInt32(streamId);
132     data.WriteFloat(volume);
133     int32_t error = Remote()->SendRequest(
134         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_LOW_POWER_STREM_VOLUME), data, reply, option);
135     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set low power stream volume failed, error: %d", error);
136     return reply.ReadInt32();
137 }
138 
GetLowPowerVolume(int32_t streamId)139 float AudioPolicyProxy::GetLowPowerVolume(int32_t streamId)
140 {
141     MessageParcel data;
142     MessageParcel reply;
143     MessageOption option;
144 
145     bool ret = data.WriteInterfaceToken(GetDescriptor());
146     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
147     data.WriteInt32(streamId);
148     int32_t error = Remote()->SendRequest(
149         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_LOW_POWRR_STREM_VOLUME), data, reply, option);
150     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get low power stream volume failed, error: %d", error);
151     return reply.ReadFloat();
152 }
153 
GetSingleStreamVolume(int32_t streamId)154 float AudioPolicyProxy::GetSingleStreamVolume(int32_t streamId)
155 {
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159 
160     bool ret = data.WriteInterfaceToken(GetDescriptor());
161     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
162     data.WriteInt32(streamId);
163     int32_t error = Remote()->SendRequest(
164         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SINGLE_STREAM_VOLUME), data, reply, option);
165     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get single stream volume failed, error: %d", error);
166     return reply.ReadFloat();
167 }
168 
IsVolumeUnadjustable()169 bool AudioPolicyProxy::IsVolumeUnadjustable()
170 {
171     MessageParcel data;
172     MessageParcel reply;
173     MessageOption option;
174 
175     bool ret = data.WriteInterfaceToken(GetDescriptor());
176     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
177     int32_t error = Remote()->SendRequest(
178         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_VOLUME_UNADJUSTABLE), data, reply, option);
179     if (error != ERR_NONE) {
180         AUDIO_ERR_LOG("isvolumeadjustable failed, error: %d", error);
181     }
182     return reply.ReadBool();
183 }
184 
AdjustVolumeByStep(VolumeAdjustType adjustType)185 int32_t AudioPolicyProxy::AdjustVolumeByStep(VolumeAdjustType adjustType)
186 {
187     MessageParcel data;
188     MessageParcel reply;
189     MessageOption option;
190 
191     bool ret = data.WriteInterfaceToken(GetDescriptor());
192     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
193 
194     data.WriteInt32(adjustType);
195 
196     int32_t error = Remote()->SendRequest(
197         static_cast<uint32_t>(AudioPolicyInterfaceCode::ADJUST_VOLUME_BY_STEP), data, reply, option);
198     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERR_TRANSACTION_FAILED,
199         "GetAudioLatencyFromXml, error: %d", error);
200 
201     return reply.ReadInt32();
202 }
203 
AdjustSystemVolumeByStep(AudioVolumeType volumeType,VolumeAdjustType adjustType)204 int32_t AudioPolicyProxy::AdjustSystemVolumeByStep(AudioVolumeType volumeType, VolumeAdjustType adjustType)
205 {
206     MessageParcel data;
207     MessageParcel reply;
208     MessageOption option;
209 
210     bool ret = data.WriteInterfaceToken(GetDescriptor());
211     CHECK_AND_RETURN_RET_LOG(ret, IPC_PROXY_ERR, "WriteInterfaceToken failed");
212     data.WriteInt32(volumeType);
213     data.WriteInt32(adjustType);
214 
215     int32_t error = Remote()->SendRequest(
216         static_cast<uint32_t>(AudioPolicyInterfaceCode::ADJUST_SYSTEM_VOLUME_BY_STEP), data, reply, option);
217     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERR_TRANSACTION_FAILED,
218         "GetAudioLatencyFromXml, error: %d", error);
219 
220     return reply.ReadInt32();
221 }
222 
GetSystemVolumeInDb(AudioVolumeType volumeType,int32_t volumeLevel,DeviceType deviceType)223 float AudioPolicyProxy::GetSystemVolumeInDb(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType deviceType)
224 {
225     MessageParcel data;
226     MessageParcel reply;
227     MessageOption option;
228 
229     bool ret = data.WriteInterfaceToken(GetDescriptor());
230     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteInterfaceToken failed");
231 
232     data.WriteInt32(static_cast<int32_t>(volumeType));
233     data.WriteInt32(volumeLevel);
234     data.WriteInt32(static_cast<int32_t>(deviceType));
235 
236     int32_t error = Remote()->SendRequest(
237         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_SYSTEM_VOLUME_IN_DB), data, reply, option);
238     if (error != ERR_NONE) {
239         AUDIO_ERR_LOG("GetSystemVolumeInDb failed, error: %d", error);
240     }
241 
242     return reply.ReadFloat();
243 }
244 
GetVolumeGroupInfos(std::string networkId,std::vector<sptr<VolumeGroupInfo>> & infos)245 int32_t AudioPolicyProxy::GetVolumeGroupInfos(std::string networkId, std::vector<sptr<VolumeGroupInfo>> &infos)
246 {
247     MessageParcel data;
248     MessageParcel reply;
249     MessageOption option;
250 
251     bool res = data.WriteInterfaceToken(GetDescriptor());
252     CHECK_AND_RETURN_RET_LOG(res, ERROR, "WriteInterfaceToken failed");
253 
254     data.WriteString(networkId);
255     int32_t error = Remote()->SendRequest(
256         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_VOLUME_GROUP_INFO), data, reply, option);
257     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "GetVolumeGroupInfo, error: %d", error);
258 
259     int32_t ret = reply.ReadInt32();
260     if (ret > 0) {
261         for (int32_t i = 0; i < ret; i++) {
262             infos.push_back(VolumeGroupInfo::Unmarshalling(reply));
263         }
264         return SUCCESS;
265     } else {
266         return ret;
267     }
268 }
269 
GetMinStreamVolume()270 float AudioPolicyProxy::GetMinStreamVolume()
271 {
272     MessageParcel data;
273     MessageParcel reply;
274     MessageOption option;
275 
276     bool ret = data.WriteInterfaceToken(GetDescriptor());
277     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
278     int32_t error = Remote()->SendRequest(
279         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MIN_VOLUME_STREAM), data, reply, option);
280     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get min volume for stream failed, error: %d", error);
281     return reply.ReadFloat();
282 }
283 
GetMaxStreamVolume()284 float AudioPolicyProxy::GetMaxStreamVolume()
285 {
286     MessageParcel data;
287     MessageParcel reply;
288     MessageOption option;
289 
290     bool ret = data.WriteInterfaceToken(GetDescriptor());
291     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
292     int32_t error = Remote()->SendRequest(
293         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_MAX_VOLUME_STREAM), data, reply, option);
294     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "get max volume for stream failed, error: %d", error);
295     return reply.ReadFloat();
296 }
297 
SetDeviceAbsVolumeSupported(const std::string & macAddress,const bool support)298 int32_t AudioPolicyProxy::SetDeviceAbsVolumeSupported(const std::string &macAddress, const bool support)
299 {
300     MessageParcel data;
301     MessageParcel reply;
302     MessageOption option;
303 
304     bool ret = data.WriteInterfaceToken(GetDescriptor());
305     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
306     data.WriteString(macAddress);
307     data.WriteBool(support);
308 
309     int32_t error = Remote()->SendRequest(
310         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_DEVICE_ABSOLUTE_VOLUME_SUPPORTED), data, reply, option);
311     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR,
312         "SetDeviceAbsVolumeSupported failed, error: %d", error);
313     return reply.ReadInt32();
314 }
315 
IsAbsVolumeScene()316 bool AudioPolicyProxy::IsAbsVolumeScene()
317 {
318     MessageParcel data;
319     MessageParcel reply;
320     MessageOption option;
321 
322     bool ret = data.WriteInterfaceToken(GetDescriptor());
323     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
324 
325     int32_t error = Remote()->SendRequest(
326         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_ABS_VOLUME_SCENE), data, reply, option);
327     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "IsAbsVolumeScene failed, error: %d", error);
328     return reply.ReadBool();
329 }
330 
SetA2dpDeviceVolume(const std::string & macAddress,const int32_t volume,const bool updateUi)331 int32_t AudioPolicyProxy::SetA2dpDeviceVolume(const std::string &macAddress, const int32_t volume,
332     const bool updateUi)
333 {
334     MessageParcel data;
335     MessageParcel reply;
336     MessageOption option;
337 
338     bool ret = data.WriteInterfaceToken(GetDescriptor());
339     CHECK_AND_RETURN_RET_LOG(ret, ERROR, "WriteInterfaceToken failed");
340     data.WriteString(macAddress);
341     data.WriteInt32(volume);
342     data.WriteBool(updateUi);
343 
344     int32_t error = Remote()->SendRequest(
345         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_A2DP_DEVICE_VOLUME), data, reply, option);
346     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, ERROR, "SetA2dpDeviceVolume failed, error: %d", error);
347     return reply.ReadInt32();
348 }
349 
350 } // namespace AudioStandard
351 } // namespace OHOS