1 /*
2 * Copyright (c) 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 "avsession_proxy.h"
17 #include "avsession_callback_client.h"
18 #include "avsession_controller_proxy.h"
19 #include "iavsession_callback.h"
20 #include "avsession_log.h"
21 #include "avsession_errors.h"
22 #include "avsession_trace.h"
23
24 namespace OHOS::AVSession {
AVSessionProxy(const sptr<IRemoteObject> & impl)25 AVSessionProxy::AVSessionProxy(const sptr<IRemoteObject>& impl)
26 : IRemoteProxy<IAVSession>(impl)
27 {
28 SLOGD("construct");
29 }
30
~AVSessionProxy()31 AVSessionProxy::~AVSessionProxy()
32 {
33 SLOGI("destroy");
34 Destroy();
35 }
36
GetSessionId()37 std::string AVSessionProxy::GetSessionId()
38 {
39 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, "", "session is destroyed");
40 MessageParcel data;
41 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
42 MessageParcel reply;
43 MessageOption option;
44 auto remote = Remote();
45 CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
46 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_SESSION_ID, data, reply, option) == 0,
47 "", "send request failed");
48
49 std::string sessionId;
50 CHECK_AND_RETURN_RET_LOG(reply.ReadString(sessionId), "", "read sessionId failed");
51 return sessionId;
52 }
53
RegisterCallback(const std::shared_ptr<AVSessionCallback> & callback)54 int32_t AVSessionProxy::RegisterCallback(const std::shared_ptr<AVSessionCallback>& callback)
55 {
56 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
57 CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
58 callback_ = new(std::nothrow) AVSessionCallbackClient(callback);
59 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVSessionCallbackClient failed");
60 if (RegisterCallbackInner(callback_) != AVSESSION_SUCCESS) {
61 callback_.clear();
62 return AVSESSION_ERROR;
63 }
64 return AVSESSION_SUCCESS;
65 }
66
RegisterCallbackInner(const sptr<IAVSessionCallback> & callback)67 int32_t AVSessionProxy::RegisterCallbackInner(const sptr<IAVSessionCallback>& callback)
68 {
69 MessageParcel data;
70 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
71 "write interface token failed");
72 CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), ERR_MARSHALLING,
73 "write remote object failed");
74 MessageParcel reply;
75 MessageOption option;
76 auto remote = Remote();
77 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
78 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_REGISTER_CALLBACK, data, reply, option) == 0,
79 ERR_IPC_SEND_REQUEST, "send request failed");
80
81 int32_t ret = AVSESSION_ERROR;
82 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
83 }
84
Destroy()85 int32_t AVSessionProxy::Destroy()
86 {
87 SLOGI("enter");
88 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
89 MessageParcel data;
90 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
91 ERR_MARSHALLING, "write interface token failed");
92 MessageParcel reply;
93 MessageOption option;
94 auto remote = Remote();
95 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
96 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DESTROY, data, reply, option) == 0,
97 ERR_IPC_SEND_REQUEST, "send request failed");
98
99 int32_t ret = AVSESSION_ERROR;
100 if (!reply.ReadInt32(ret)) {
101 return AVSESSION_ERROR;
102 }
103 if (ret == AVSESSION_SUCCESS) {
104 isDestroyed_ = true;
105 controller_ = nullptr;
106 }
107 return ret;
108 }
109
SetAVMetaData(const AVMetaData & meta)110 int32_t AVSessionProxy::SetAVMetaData(const AVMetaData& meta)
111 {
112 AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVMetaData");
113 CHECK_AND_RETURN_RET_LOG(meta.IsValid(), ERR_INVALID_PARAM, "invalid meta data");
114 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
115 MessageParcel data;
116 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
117 ERR_MARSHALLING, "write interface token failed");
118 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&meta),
119 ERR_MARSHALLING, "WriteParcelable failed");
120 MessageParcel reply;
121 MessageOption option;
122 auto remote = Remote();
123 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
124 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_META_DATA, data, reply, option) == 0,
125 ERR_IPC_SEND_REQUEST, "send request failed");
126
127 int32_t ret = AVSESSION_ERROR;
128 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
129 }
130
GetAVMetaData(AVMetaData & meta)131 int32_t AVSessionProxy::GetAVMetaData(AVMetaData& meta)
132 {
133 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
134 MessageParcel data;
135 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
136 ERR_MARSHALLING, "write interface token failed");
137 MessageParcel reply;
138 MessageOption option;
139 auto remote = Remote();
140 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
141 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_META_DATA, data, reply, option) == 0,
142 ERR_IPC_SEND_REQUEST, "send request failed");
143
144 int32_t ret = AVSESSION_ERROR;
145 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
146 if (ret == AVSESSION_SUCCESS) {
147 std::shared_ptr<AVMetaData> metaData(reply.ReadParcelable<AVMetaData>());
148 CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_UNMARSHALLING, "read metaData failed");
149 meta = *metaData;
150 }
151 return ret;
152 }
153
GetAVPlaybackState(AVPlaybackState & state)154 int32_t AVSessionProxy::GetAVPlaybackState(AVPlaybackState& state)
155 {
156 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
157 MessageParcel data;
158 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
159 ERR_MARSHALLING, "write interface token failed");
160 MessageParcel reply;
161 MessageOption option;
162 auto remote = Remote();
163 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
164 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_PLAYBACK_STATE, data, reply, option) == 0,
165 ERR_IPC_SEND_REQUEST, "send request failed");
166
167 int32_t ret = AVSESSION_ERROR;
168 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
169 if (ret == AVSESSION_SUCCESS) {
170 std::shared_ptr<AVPlaybackState> playbackState(reply.ReadParcelable<AVPlaybackState>());
171 CHECK_AND_RETURN_RET_LOG(playbackState != nullptr, ERR_UNMARSHALLING, "read playbackState failed");
172 state = *playbackState;
173 }
174 return ret;
175 }
176
SetAVPlaybackState(const AVPlaybackState & state)177 int32_t AVSessionProxy::SetAVPlaybackState(const AVPlaybackState& state)
178 {
179 AVSESSION_TRACE_SYNC_START("AVSessionProxy::SetAVPlaybackState");
180 CHECK_AND_RETURN_RET_LOG(state.IsValid(), ERR_INVALID_PARAM, "state not valid");
181 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
182 MessageParcel data;
183 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
184 ERR_MARSHALLING, "write interface token failed");
185 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&state),
186 ERR_MARSHALLING, "Write state failed");
187 MessageParcel reply;
188 MessageOption option;
189 auto remote = Remote();
190 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
191 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_PLAYBACK_STATE, data, reply, option) == 0,
192 ERR_IPC_SEND_REQUEST, "send request failed");
193
194 int32_t ret = AVSESSION_ERROR;
195 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
196 }
197
SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent & ability)198 int32_t AVSessionProxy::SetLaunchAbility(const AbilityRuntime::WantAgent::WantAgent& ability)
199 {
200 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
201 MessageParcel data;
202 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
203 ERR_MARSHALLING, "write interface token failed");
204 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&ability),
205 ERR_MARSHALLING, "Write WantAgent failed");
206 MessageParcel reply;
207 MessageOption option;
208 auto remote = Remote();
209 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
210 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_SET_LAUNCH_ABILITY, data, reply, option) == 0,
211 ERR_IPC_SEND_REQUEST, "send request failed");
212
213 int32_t ret = AVSESSION_ERROR;
214 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
215 }
216
GetControllerInner()217 sptr<IRemoteObject> AVSessionProxy::GetControllerInner()
218 {
219 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
220 MessageParcel data;
221 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
222 nullptr, "write interface token failed");
223 MessageParcel reply;
224 MessageOption option;
225 auto remote = Remote();
226 CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
227 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_GET_CONTROLLER, data, reply, option) == 0,
228 nullptr, "send request failed");
229
230 int32_t ret = AVSESSION_ERROR;
231 CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), nullptr, "read int32 failed");
232 sptr <IRemoteObject> controller = nullptr;
233 if (ret == AVSESSION_SUCCESS) {
234 controller = reply.ReadRemoteObject();
235 CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "read IRemoteObject failed");
236 }
237 return controller;
238 }
239
GetController()240 std::shared_ptr<AVSessionController> AVSessionProxy::GetController()
241 {
242 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, nullptr, "session is destroyed");
243 CHECK_AND_RETURN_RET_LOG(controller_ == nullptr || controller_->IsDestroy(), controller_,
244 "controller already exist");
245 sptr <IRemoteObject> object = GetControllerInner();
246 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "get object failed");
247 auto controller = iface_cast<AVSessionControllerProxy>(object);
248 controller_ = std::shared_ptr<AVSessionController>(controller.GetRefPtr(), [holder = controller](const auto*) {});
249 return controller_;
250 }
251
Activate()252 int32_t AVSessionProxy::Activate()
253 {
254 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
255 MessageParcel data;
256 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
257 ERR_MARSHALLING, "write interface token failed");
258 MessageParcel reply;
259 MessageOption option;
260 auto remote = Remote();
261 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
262 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ACTIVATE, data, reply, option) == 0,
263 ERR_IPC_SEND_REQUEST, "send request failed");
264
265 int32_t ret = AVSESSION_ERROR;
266 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
267 }
268
Deactivate()269 int32_t AVSessionProxy::Deactivate()
270 {
271 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
272 MessageParcel data;
273 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
274 ERR_MARSHALLING, "write interface token failed");
275 MessageParcel reply;
276 MessageOption option;
277 auto remote = Remote();
278 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
279 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DEACTIVATE, data, reply, option) == 0,
280 ERR_IPC_SEND_REQUEST, "send request failed");
281
282 int32_t ret = AVSESSION_ERROR;
283 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
284 }
285
IsActive()286 bool AVSessionProxy::IsActive()
287 {
288 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
289 MessageParcel data;
290 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
291 ERR_MARSHALLING, "write interface token failed");
292 MessageParcel reply;
293 MessageOption option;
294 auto remote = Remote();
295 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
296 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ISACTIVE, data, reply, option) == 0,
297 ERR_IPC_SEND_REQUEST, "send request failed");
298
299 bool ret = false;
300 return reply.ReadBool(ret) ? ret : false;
301 }
302
AddSupportCommand(const int32_t cmd)303 int32_t AVSessionProxy::AddSupportCommand(const int32_t cmd)
304 {
305 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
306 CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
307 CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
308 MessageParcel data;
309 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
310 ERR_MARSHALLING, "write interface token failed");
311 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
312 ERR_MARSHALLING, "Write cmd failed");
313 MessageParcel reply;
314 MessageOption option;
315 auto remote = Remote();
316 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
317 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_ADD_SUPPORT_COMMAND, data, reply, option) == 0,
318 ERR_IPC_SEND_REQUEST, "send request failed");
319
320 int32_t ret = AVSESSION_ERROR;
321 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
322 }
323
DeleteSupportCommand(const int32_t cmd)324 int32_t AVSessionProxy::DeleteSupportCommand(const int32_t cmd)
325 {
326 CHECK_AND_RETURN_RET_LOG(isDestroyed_ == false, ERR_SESSION_NOT_EXIST, "session is destroyed");
327 CHECK_AND_RETURN_RET_LOG(cmd > AVControlCommand::SESSION_CMD_INVALID, AVSESSION_ERROR, "invalid cmd");
328 CHECK_AND_RETURN_RET_LOG(cmd < AVControlCommand::SESSION_CMD_MAX, AVSESSION_ERROR, "invalid cmd");
329 MessageParcel data;
330 CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()),
331 ERR_MARSHALLING, "write interface token failed");
332 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(cmd),
333 ERR_MARSHALLING, "Write cmd failed");
334 MessageParcel reply;
335 MessageOption option;
336 auto remote = Remote();
337 CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
338 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SESSION_CMD_DELETE_SUPPORT_COMMAND, data, reply, option) == 0,
339 ERR_IPC_SEND_REQUEST, "send request failed");
340
341 int32_t ret = AVSESSION_ERROR;
342 return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
343 }
344 }