1 /*
2  * Copyright (c) 2023 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 "AudioProcessProxy"
17 #endif
18 
19 #include "audio_process_cb_stub.h"
20 #include "audio_process_proxy.h"
21 #include "audio_service_log.h"
22 #include "audio_errors.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
CheckInterfaceToken(MessageParcel & data)26 bool ProcessCbStub::CheckInterfaceToken(MessageParcel &data)
27 {
28     static auto localDescriptor = IProcessCb::GetDescriptor();
29     auto remoteDescriptor = data.ReadInterfaceToken();
30     CHECK_AND_RETURN_RET_LOG(remoteDescriptor == localDescriptor, false, "CheckInterFfaceToken failed.");
31     return true;
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int ProcessCbStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     bool ret = CheckInterfaceToken(data);
37     CHECK_AND_RETURN_RET(ret, AUDIO_ERR);
38     if (code >= IProcessCbMsg::PROCESS_CB_MAX_MSG) {
39         AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
40         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
41     }
42     switch (code) {
43         case ON_ENDPOINT_CHANGE:
44             return HandleOnEndpointChange(data, reply);
45         default:
46             AUDIO_WARNING_LOG("OnRemoteRequest not supported code:%{public}d.", code);
47             return AUDIO_ERR;
48     }
49 }
50 
HandleOnEndpointChange(MessageParcel & data,MessageParcel & reply)51 int32_t ProcessCbStub::HandleOnEndpointChange(MessageParcel &data, MessageParcel &reply)
52 {
53     int32_t status = data.ReadInt32();
54     reply.WriteInt32(OnEndpointChange(status));
55     return AUDIO_OK;
56 }
57 
AudioProcessProxy(const sptr<IRemoteObject> & impl)58 AudioProcessProxy::AudioProcessProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAudioProcess>(impl)
59 {
60 }
61 
~AudioProcessProxy()62 AudioProcessProxy::~AudioProcessProxy()
63 {
64 }
65 
ResolveBuffer(std::shared_ptr<OHAudioBuffer> & buffer)66 int32_t AudioProcessProxy::ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer)
67 {
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71 
72     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
73 
74     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RESOLVE_BUFFER, data, reply, option);
75     CHECK_AND_RETURN_RET_LOG((ret == AUDIO_OK && reply.ReadInt32() == AUDIO_OK), ERR_OPERATION_FAILED,
76         "ResolveBuffer failed, error: %{public}d", ret);
77     buffer = OHAudioBuffer::ReadFromParcel(reply);
78     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, ERR_OPERATION_FAILED, "ReadFromParcel failed");
79     return SUCCESS;
80 }
81 
GetSessionId(uint32_t & sessionId)82 int32_t AudioProcessProxy::GetSessionId(uint32_t &sessionId)
83 {
84     MessageParcel data;
85     MessageParcel reply;
86     MessageOption option;
87 
88     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
89 
90     int ret = Remote()->SendRequest(IAudioProcessMsg::OH_GET_SESSIONID, data, reply, option);
91     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Start failed, error: %{public}d", ret);
92 
93     ret = reply.ReadInt32();
94     sessionId = reply.ReadUint32();
95     return ret;
96 }
97 
Start()98 int32_t AudioProcessProxy::Start()
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103 
104     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
105 
106     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_START, data, reply, option);
107     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Start failed, error: %{public}d", ret);
108 
109     return reply.ReadInt32();
110 }
111 
Pause(bool isFlush)112 int32_t AudioProcessProxy::Pause(bool isFlush)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option;
117 
118     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
119     data.WriteBool(isFlush);
120     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_PAUSE, data, reply, option);
121     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Pause failed, error: %{public}d", ret);
122 
123     return reply.ReadInt32();
124 }
125 
Resume()126 int32_t AudioProcessProxy::Resume()
127 {
128     MessageParcel data;
129     MessageParcel reply;
130     MessageOption option;
131 
132     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
133 
134     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RESUME, data, reply, option);
135     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Resume failed, error: %{public}d", ret);
136 
137     return reply.ReadInt32();
138 }
139 
Stop()140 int32_t AudioProcessProxy::Stop()
141 {
142     MessageParcel data;
143     MessageParcel reply;
144     MessageOption option;
145 
146     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
147 
148     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_STOP, data, reply, option);
149     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Stop failed, error: %{public}d", ret);
150 
151     return reply.ReadInt32();
152 }
153 
RequestHandleInfo(bool isAsync)154 int32_t AudioProcessProxy::RequestHandleInfo(bool isAsync)
155 {
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option = isAsync ? MessageOption(MessageOption::TF_ASYNC) : MessageOption();
159 
160     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
161 
162     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_REQUEST_HANDLE_INFO, data, reply, option);
163     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "RequestHandleInfo failed: %{public}d", ret);
164 
165     return reply.ReadInt32();
166 }
167 
Release(bool isSwitchStream)168 int32_t AudioProcessProxy::Release(bool isSwitchStream)
169 {
170     MessageParcel data;
171     MessageParcel reply;
172     MessageOption option;
173 
174     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
175     data.WriteBool(isSwitchStream);
176 
177     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RELEASE, data, reply, option);
178     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Release failed, error: %{public}d", ret);
179 
180     return reply.ReadInt32();
181 }
182 
RegisterProcessCb(sptr<IRemoteObject> object)183 int32_t AudioProcessProxy::RegisterProcessCb(sptr<IRemoteObject> object)
184 {
185     MessageParcel data;
186     MessageParcel reply;
187     MessageOption option;
188 
189     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
190 
191     CHECK_AND_RETURN_RET_LOG(object != nullptr, ERR_NULL_OBJECT,
192         "RegisterProcessCb object is null");
193 
194     data.WriteRemoteObject(object);
195 
196     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_REGISTER_PROCESS_CB, data, reply, option);
197     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "RegisterProcessCb failed, error: %{public}d", ret);
198 
199     return reply.ReadInt32();
200 }
201 
RegisterThreadPriority(uint32_t tid,const std::string & bundleName)202 int32_t AudioProcessProxy::RegisterThreadPriority(uint32_t tid, const std::string &bundleName)
203 {
204     MessageParcel data;
205     MessageParcel reply;
206     MessageOption option;
207 
208     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
209     data.WriteUint32(tid);
210     data.WriteString(bundleName);
211     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_REGISTER_THREAD_PRIORITY, data, reply, option);
212     CHECK_AND_RETURN_RET(ret == AUDIO_OK, ret, "failed, ipc error: %{public}d", ret);
213     ret = reply.ReadInt32();
214     CHECK_AND_RETURN_RET(ret == SUCCESS, ret, "failed, error: %{public}d", ret);
215     return ret;
216 }
217 
SetSilentModeAndMixWithOthers(bool on)218 int32_t AudioProcessProxy::SetSilentModeAndMixWithOthers(bool on)
219 {
220     MessageParcel data;
221     MessageParcel reply;
222     MessageOption option;
223 
224     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
225     data.WriteBool(on);
226     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_SET_SLITNT_MODE_AND_MIX_WITH_OTHERS, data, reply, option);
227     CHECK_AND_RETURN_RET(ret == AUDIO_OK, ret, "ipc error: %{public}d", ret);
228     return reply.ReadInt32();
229 }
230 } // namespace AudioStandard
231 } // namespace OHOS
232