• 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_manager_proxy.h"
17 #include "audio_system_manager.h"
18 #include "audio_log.h"
19 
20 using namespace std;
21 
22 namespace OHOS {
23 namespace AudioStandard {
AudioManagerProxy(const sptr<IRemoteObject> & impl)24 AudioManagerProxy::AudioManagerProxy(const sptr<IRemoteObject> &impl)
25     : IRemoteProxy<IStandardAudioService>(impl)
26 {
27 }
28 
GetMaxVolume(AudioVolumeType volumeType)29 int32_t AudioManagerProxy::GetMaxVolume(AudioVolumeType volumeType)
30 {
31     MessageParcel data;
32     MessageParcel reply;
33     MessageOption option;
34 
35     if (!data.WriteInterfaceToken(GetDescriptor())) {
36         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
37         return -1;
38     }
39     data.WriteInt32(static_cast<int>(volumeType));
40     int32_t error = Remote()->SendRequest(GET_MAX_VOLUME, data, reply, option);
41     if (error != ERR_NONE) {
42         AUDIO_ERR_LOG("Get max volume failed, error: %d", error);
43         return error;
44     }
45 
46     int32_t volume = reply.ReadInt32();
47     return volume;
48 }
49 
GetMinVolume(AudioVolumeType volumeType)50 int32_t AudioManagerProxy::GetMinVolume(AudioVolumeType volumeType)
51 {
52     MessageParcel data;
53     MessageParcel reply;
54     MessageOption option;
55 
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
58         return -1;
59     }
60     data.WriteInt32(static_cast<int32_t>(volumeType));
61 
62     int32_t error = Remote()->SendRequest(GET_MIN_VOLUME, data, reply, option);
63     if (error != ERR_NONE) {
64         AUDIO_ERR_LOG("Get min volume failed, error: %d", error);
65         return error;
66     }
67 
68     int32_t volume = reply.ReadInt32();
69     return volume;
70 }
71 
SetMicrophoneMute(bool isMute)72 int32_t AudioManagerProxy::SetMicrophoneMute(bool isMute)
73 {
74     MessageParcel data;
75     MessageParcel reply;
76     MessageOption option;
77 
78     if (!data.WriteInterfaceToken(GetDescriptor())) {
79         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
80         return -1;
81     }
82     data.WriteBool(isMute);
83     int32_t error = Remote()->SendRequest(SET_MICROPHONE_MUTE, data, reply, option);
84     if (error != ERR_NONE) {
85         AUDIO_ERR_LOG("SetMicrophoneMute failed, error: %d", error);
86         return error;
87     }
88 
89     int32_t result = reply.ReadInt32();
90     return result;
91 }
92 
IsMicrophoneMute()93 bool AudioManagerProxy::IsMicrophoneMute()
94 {
95     MessageParcel data;
96     MessageParcel reply;
97     MessageOption option;
98     if (!data.WriteInterfaceToken(GetDescriptor())) {
99         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
100         return false;
101     }
102     int32_t error = Remote()->SendRequest(IS_MICROPHONE_MUTE, data, reply, option);
103     if (error != ERR_NONE) {
104         AUDIO_ERR_LOG("IsMicrophoneMute failed, error: %d", error);
105         return false;
106     }
107 
108     bool isMute = reply.ReadBool();
109     return isMute;
110 }
111 
SetVoiceVolume(float volume)112 int32_t AudioManagerProxy::SetVoiceVolume(float volume)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option;
117 
118     if (!data.WriteInterfaceToken(GetDescriptor())) {
119         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
120         return -1;
121     }
122 
123     data.WriteFloat(volume);
124 
125     int32_t error = Remote()->SendRequest(SET_VOICE_VOLUME, data, reply, option);
126     if (error != ERR_NONE) {
127         AUDIO_ERR_LOG("SetVoiceVolume failed, error: %d", error);
128         return false;
129     }
130 
131     int32_t result = reply.ReadInt32();
132     return result;
133 }
134 
SetAudioScene(AudioScene audioScene,DeviceType activeDevice)135 int32_t AudioManagerProxy::SetAudioScene(AudioScene audioScene, DeviceType activeDevice)
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option;
140 
141     if (!data.WriteInterfaceToken(GetDescriptor())) {
142         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
143         return -1;
144     }
145 
146     data.WriteInt32(static_cast<int32_t>(audioScene));
147     data.WriteInt32(static_cast<int32_t>(activeDevice));
148 
149     int32_t error = Remote()->SendRequest(SET_AUDIO_SCENE, data, reply, option);
150     if (error != ERR_NONE) {
151         AUDIO_ERR_LOG("SetAudioScene failed, error: %d", error);
152         return false;
153     }
154 
155     int32_t result = reply.ReadInt32();
156     return result;
157 }
158 
GetDevices(DeviceFlag deviceFlag)159 std::vector<sptr<AudioDeviceDescriptor>> AudioManagerProxy::GetDevices(DeviceFlag deviceFlag)
160 {
161     MessageParcel data;
162     MessageParcel reply;
163     MessageOption option;
164     std::vector<sptr<AudioDeviceDescriptor>> deviceInfo;
165 
166     if (!data.WriteInterfaceToken(GetDescriptor())) {
167         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
168         return deviceInfo;
169     }
170     data.WriteInt32(static_cast<int32_t>(deviceFlag));
171 
172     int32_t error = Remote()->SendRequest(GET_DEVICES, data, reply, option);
173     if (error != ERR_NONE) {
174         AUDIO_ERR_LOG("Get devices failed, error: %d", error);
175         return deviceInfo;
176     }
177 
178     int32_t size = reply.ReadInt32();
179     for (int32_t i = 0; i < size; i++) {
180         deviceInfo.push_back(AudioDeviceDescriptor::Unmarshalling(reply));
181     }
182 
183     return deviceInfo;
184 }
185 
GetAudioParameter(const std::string & key)186 const std::string AudioManagerProxy::GetAudioParameter(const std::string &key)
187 {
188     MessageParcel data;
189     MessageParcel reply;
190     MessageOption option;
191 
192     if (!data.WriteInterfaceToken(GetDescriptor())) {
193         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
194         return "";
195     }
196     data.WriteString(static_cast<std::string>(key));
197     int32_t error = Remote()->SendRequest(GET_AUDIO_PARAMETER, data, reply, option);
198     if (error != ERR_NONE) {
199         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
200         const std::string value = "";
201         return value;
202     }
203 
204     const std::string value = reply.ReadString();
205     return value;
206 }
207 
GetAudioParameter(const std::string & networkId,const AudioParamKey key,const std::string & condition)208 const std::string AudioManagerProxy::GetAudioParameter(const std::string& networkId, const AudioParamKey key,
209     const std::string& condition)
210 {
211     MessageParcel data;
212     MessageParcel reply;
213     MessageOption option;
214 
215     if (!data.WriteInterfaceToken(GetDescriptor())) {
216         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
217         return "";
218     }
219     data.WriteString(static_cast<std::string>(networkId));
220     data.WriteInt32(static_cast<int32_t>(key));
221     data.WriteString(static_cast<std::string>(condition));
222     int32_t error = Remote()->SendRequest(GET_REMOTE_AUDIO_PARAMETER, data, reply, option);
223     if (error != ERR_NONE) {
224         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
225         const std::string value = "";
226         return value;
227     }
228 
229     const std::string value = reply.ReadString();
230     return value;
231 }
232 
SetAudioParameter(const std::string & key,const std::string & value)233 void AudioManagerProxy::SetAudioParameter(const std::string &key, const std::string &value)
234 {
235     MessageParcel data;
236     MessageParcel reply;
237     MessageOption option;
238 
239     if (!data.WriteInterfaceToken(GetDescriptor())) {
240         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
241         return;
242     }
243     data.WriteString(static_cast<std::string>(key));
244     data.WriteString(static_cast<std::string>(value));
245     int32_t error = Remote()->SendRequest(SET_AUDIO_PARAMETER, data, reply, option);
246     if (error != ERR_NONE) {
247         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
248         return;
249     }
250 }
251 
SetAudioParameter(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)252 void AudioManagerProxy::SetAudioParameter(const std::string& networkId, const AudioParamKey key,
253     const std::string& condition, const std::string& value)
254 {
255     MessageParcel data;
256     MessageParcel reply;
257     MessageOption option;
258 
259     if (!data.WriteInterfaceToken(GetDescriptor())) {
260         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
261         return;
262     }
263     data.WriteString(static_cast<std::string>(networkId));
264     data.WriteInt32(static_cast<int32_t>(key));
265     data.WriteString(static_cast<std::string>(condition));
266     data.WriteString(static_cast<std::string>(value));
267     int32_t error = Remote()->SendRequest(SET_REMOTE_AUDIO_PARAMETER, data, reply, option);
268     if (error != ERR_NONE) {
269         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
270         return;
271     }
272 }
273 
RetrieveCookie(int32_t & size)274 const char *AudioManagerProxy::RetrieveCookie(int32_t &size)
275 {
276     MessageParcel data;
277     MessageParcel reply;
278     MessageOption option;
279     const char *cookieInfo = nullptr;
280 
281     if (!data.WriteInterfaceToken(GetDescriptor())) {
282         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
283         return nullptr;
284     }
285 
286     int32_t error = Remote()->SendRequest(RETRIEVE_COOKIE, data, reply, option);
287     if (error != ERR_NONE) {
288         AUDIO_ERR_LOG("retrieve cookie failed, error: %d", error);
289         return nullptr;
290     }
291 
292     size = reply.ReadInt32();
293     if (size > 0) {
294         cookieInfo = reinterpret_cast<const char *>(reply.ReadRawData(size));
295     }
296 
297     return cookieInfo;
298 }
299 
GetTransactionId(DeviceType deviceType,DeviceRole deviceRole)300 uint64_t AudioManagerProxy::GetTransactionId(DeviceType deviceType, DeviceRole deviceRole)
301 {
302     MessageParcel data;
303     MessageParcel reply;
304     MessageOption option;
305     uint32_t transactionId = 0;
306 
307     if (!data.WriteInterfaceToken(GetDescriptor())) {
308         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
309         return transactionId;
310     }
311 
312     data.WriteInt32(static_cast<int32_t>(deviceType));
313     data.WriteInt32(static_cast<int32_t>(deviceRole));
314 
315     int32_t error = Remote()->SendRequest(GET_TRANSACTION_ID, data, reply, option);
316     if (error != ERR_NONE) {
317         AUDIO_ERR_LOG("get transaction id failed, error: %d", error);
318         return transactionId;
319     }
320 
321     transactionId = reply.ReadUint64();
322 
323     return transactionId;
324 }
325 
NotifyDeviceInfo(std::string networkId,bool connected)326 void AudioManagerProxy::NotifyDeviceInfo(std::string networkId, bool connected)
327 {
328     MessageParcel data;
329     MessageParcel reply;
330     MessageOption option;
331 
332     if (!data.WriteInterfaceToken(GetDescriptor())) {
333         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
334         return;
335     }
336     data.WriteString(networkId);
337     data.WriteBool(connected);
338     int32_t error = Remote()->SendRequest(NOTIFY_DEVICE_INFO, data, reply, option);
339     if (error != ERR_NONE) {
340         AUDIO_ERR_LOG("Get audio parameter failed, error: %d", error);
341         return;
342     }
343 }
344 
CheckRemoteDeviceState(std::string networkId,DeviceRole deviceRole,bool isStartDevice)345 int32_t AudioManagerProxy::CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice)
346 {
347     MessageParcel data;
348     MessageParcel reply;
349     MessageOption option;
350 
351     if (!data.WriteInterfaceToken(GetDescriptor())) {
352         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
353         return ERR_TRANSACTION_FAILED;
354     }
355     data.WriteString(networkId);
356     data.WriteInt32(static_cast<int32_t>(deviceRole));
357     data.WriteBool(isStartDevice);
358     int32_t error = Remote()->SendRequest(CHECK_REMOTE_DEVICE_STATE, data, reply, option);
359     if (error != ERR_NONE) {
360         AUDIO_ERR_LOG("CheckRemoteDeviceState failed in proxy, error: %d", error);
361         return error;
362     }
363     return reply.ReadInt32();
364 }
365 
UpdateActiveDeviceRoute(DeviceType type,DeviceFlag flag)366 int32_t AudioManagerProxy::UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag)
367 {
368     AUDIO_DEBUG_LOG("[%{public}s]", __func__);
369     MessageParcel data;
370     MessageParcel reply;
371     MessageOption option;
372 
373     if (!data.WriteInterfaceToken(GetDescriptor())) {
374         AUDIO_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
375         return -1;
376     }
377     data.WriteInt32(type);
378     data.WriteInt32(flag);
379 
380     auto error = Remote()->SendRequest(UPDATE_ROUTE_REQ, data, reply, option);
381     if (error != ERR_NONE) {
382         AUDIO_ERR_LOG("UpdateActiveDeviceRoute failed, error: %{public}d", error);
383         return false;
384     }
385 
386     auto result = reply.ReadInt32();
387     AUDIO_DEBUG_LOG("[UPDATE_ROUTE_REQ] result %{public}d", result);
388     return result;
389 }
390 
SetParameterCallback(const sptr<IRemoteObject> & object)391 int32_t AudioManagerProxy::SetParameterCallback(const sptr<IRemoteObject>& object)
392 {
393     MessageParcel data;
394     MessageParcel reply;
395     MessageOption option;
396 
397     if (object == nullptr) {
398         AUDIO_ERR_LOG("AudioManagerProxy: SetParameterCallback object is null");
399         return ERR_NULL_OBJECT;
400     }
401     if (!data.WriteInterfaceToken(GetDescriptor())) {
402         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
403         return -1;
404     }
405 
406     (void)data.WriteRemoteObject(object);
407     int error = Remote()->SendRequest(SET_PARAMETER_CALLBACK, data, reply, option);
408     if (error != ERR_NONE) {
409         AUDIO_ERR_LOG("AudioPolicyProxy: SetParameterCallback failed, error: %{public}d", error);
410         return error;
411     }
412 
413     return reply.ReadInt32();
414 }
415 
SetAudioMonoState(bool audioMono)416 void AudioManagerProxy::SetAudioMonoState(bool audioMono)
417 {
418     MessageParcel data;
419     MessageParcel reply;
420     MessageOption option;
421 
422     if (!data.WriteInterfaceToken(GetDescriptor())) {
423         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
424         return;
425     }
426     (void)data.WriteBool(audioMono);
427     int error = Remote()->SendRequest(SET_AUDIO_MONO_STATE, data, reply, option);
428     if (error != ERR_NONE) {
429         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioMonoState failed, error: %{public}d", error);
430         return;
431     }
432 }
433 
SetAudioBalanceValue(float audioBalance)434 void AudioManagerProxy::SetAudioBalanceValue(float audioBalance)
435 {
436     MessageParcel data;
437     MessageParcel reply;
438     MessageOption option;
439 
440     if (!data.WriteInterfaceToken(GetDescriptor())) {
441         AUDIO_ERR_LOG("AudioPolicyProxy: WriteInterfaceToken failed");
442         return;
443     }
444     (void)data.WriteFloat(audioBalance);
445     int error = Remote()->SendRequest(SET_AUDIO_BALANCE_VALUE, data, reply, option);
446     if (error != ERR_NONE) {
447         AUDIO_ERR_LOG("AudioPolicyProxy: SetAudioBalanceValue failed, error: %{public}d", error);
448         return;
449     }
450 }
451 } // namespace AudioStandard
452 } // namespace OHOS
453