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 "media_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(AudioSystemManager::AudioVolumeType volumeType)29 int32_t AudioManagerProxy::GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType)
30 {
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 MEDIA_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 MEDIA_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(AudioSystemManager::AudioVolumeType volumeType)50 int32_t AudioManagerProxy::GetMinVolume(AudioSystemManager::AudioVolumeType volumeType)
51 {
52 MessageParcel data;
53 MessageParcel reply;
54 MessageOption option;
55
56 if (!data.WriteInterfaceToken(GetDescriptor())) {
57 MEDIA_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 MEDIA_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 MEDIA_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 MEDIA_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 MEDIA_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 MEDIA_ERR_LOG("IsMicrophoneMute failed, error: %d", error);
105 return false;
106 }
107
108 bool isMute = reply.ReadBool();
109 return isMute;
110 }
111
SetAudioScene(AudioScene audioScene)112 int32_t AudioManagerProxy::SetAudioScene(AudioScene audioScene)
113 {
114 MessageParcel data;
115 MessageParcel reply;
116 MessageOption option;
117
118 if (!data.WriteInterfaceToken(GetDescriptor())) {
119 MEDIA_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
120 return -1;
121 }
122
123 data.WriteInt32(static_cast<int32_t>(audioScene));
124
125 int32_t error = Remote()->SendRequest(SET_AUDIO_SCENE, data, reply, option);
126 if (error != ERR_NONE) {
127 MEDIA_ERR_LOG("SetAudioScene failed, error: %d", error);
128 return false;
129 }
130
131 int32_t result = reply.ReadInt32();
132 return result;
133 }
134
GetDevices(DeviceFlag deviceFlag)135 std::vector<sptr<AudioDeviceDescriptor>> AudioManagerProxy::GetDevices(DeviceFlag deviceFlag)
136 {
137 std::vector<sptr<AudioDeviceDescriptor>> deviceInfo = {};
138 return deviceInfo;
139 }
140
GetAudioParameter(const std::string & key)141 const std::string AudioManagerProxy::GetAudioParameter(const std::string &key)
142 {
143 MessageParcel data;
144 MessageParcel reply;
145 MessageOption option;
146
147 if (!data.WriteInterfaceToken(GetDescriptor())) {
148 MEDIA_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
149 return "";
150 }
151 data.WriteString(static_cast<std::string>(key));
152 int32_t error = Remote()->SendRequest(GET_AUDIO_PARAMETER, data, reply, option);
153 if (error != ERR_NONE) {
154 MEDIA_ERR_LOG("Get audio parameter failed, error: %d", error);
155 const std::string value = "";
156 return value;
157 }
158
159 const std::string value = reply.ReadString();
160 return value;
161 }
162
SetAudioParameter(const std::string & key,const std::string & value)163 void AudioManagerProxy::SetAudioParameter(const std::string &key, const std::string &value)
164 {
165 MessageParcel data;
166 MessageParcel reply;
167 MessageOption option;
168
169 if (!data.WriteInterfaceToken(GetDescriptor())) {
170 MEDIA_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
171 return;
172 }
173 data.WriteString(static_cast<std::string>(key));
174 data.WriteString(static_cast<std::string>(value));
175 int32_t error = Remote()->SendRequest(SET_AUDIO_PARAMETER, data, reply, option);
176 if (error != ERR_NONE) {
177 MEDIA_ERR_LOG("Get audio parameter failed, error: %d", error);
178 return;
179 }
180 }
181
RetrieveCookie(int32_t & size)182 const char *AudioManagerProxy::RetrieveCookie(int32_t &size)
183 {
184 MessageParcel data;
185 MessageParcel reply;
186 MessageOption option;
187 const char *cookieInfo = nullptr;
188
189 if (!data.WriteInterfaceToken(GetDescriptor())) {
190 MEDIA_ERR_LOG("AudioManagerProxy: WriteInterfaceToken failed");
191 return nullptr;
192 }
193
194 int32_t error = Remote()->SendRequest(RETRIEVE_COOKIE, data, reply, option);
195 if (error != ERR_NONE) {
196 MEDIA_ERR_LOG("retrieve cookie failed, error: %d", error);
197 return nullptr;
198 }
199
200 size = reply.ReadInt32();
201 if (size > 0) {
202 cookieInfo = reinterpret_cast<const char *>(reply.ReadRawData(size));
203 }
204
205 return cookieInfo;
206 }
207
UpdateActiveDeviceRoute(DeviceType type,DeviceFlag flag)208 int32_t AudioManagerProxy::UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag)
209 {
210 return ERR_NONE;
211 }
212 } // namespace AudioStandard
213 } // namespace OHOS
214