1 /*
2 * Copyright (C) 2023-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 * Description: supply mirror player implement proxy
15 * Author: zhangjingnan
16 * Create: 2023-5-27
17 */
18
19 #include "mirror_player_impl_proxy.h"
20 #include "cast_engine_common_helper.h"
21 #include "cast_engine_errors.h"
22 #include "cast_engine_log.h"
23
24 namespace OHOS {
25 namespace CastEngine {
26 namespace CastEngineClient {
27 DEFINE_CAST_ENGINE_LABEL("Cast-Client-MirrorPlayer");
28
~MirrorPlayerImplProxy()29 MirrorPlayerImplProxy::~MirrorPlayerImplProxy()
30 {
31 CLOGI("Stop the client mirror player proxy.");
32 }
33
Play(const std::string & deviceId)34 int32_t MirrorPlayerImplProxy::Play(const std::string &deviceId)
35 {
36 MessageParcel data, reply;
37 MessageOption option;
38
39 if (!data.WriteInterfaceToken(GetDescriptor())) {
40 CLOGE("Failed to write the interface token");
41 return CAST_ENGINE_ERROR;
42 }
43 if (!data.WriteString(deviceId)) {
44 CLOGE("Failed to write the the device id");
45 return CAST_ENGINE_ERROR;
46 }
47
48 int32_t ret = Remote()->SendRequest(PLAY, data, reply, option);
49 if (ret == ERR_UNKNOWN_TRANSACTION) {
50 CLOGE("No permission when executing the playing action");
51 return ERR_NO_PERMISSION;
52 } else if (ret == ERR_INVALID_DATA) {
53 CLOGE("Invalid parameter when executing the playing action");
54 return ERR_INVALID_PARAM;
55 } else if (ret != ERR_NONE) {
56 CLOGE("Failed to send ipc request when executing the playing action");
57 return CAST_ENGINE_ERROR;
58 }
59
60 return reply.ReadInt32();
61 }
62
Pause(const std::string & deviceId)63 int32_t MirrorPlayerImplProxy::Pause(const std::string &deviceId)
64 {
65 MessageParcel data, reply;
66 MessageOption option;
67
68 if (!data.WriteInterfaceToken(GetDescriptor())) {
69 CLOGE("Failed to write the interface token");
70 return CAST_ENGINE_ERROR;
71 }
72 if (!data.WriteString(deviceId)) {
73 CLOGE("Failed to write the the device id");
74 return CAST_ENGINE_ERROR;
75 }
76
77 int32_t ret = Remote()->SendRequest(PAUSE, data, reply, option);
78 if (ret == ERR_UNKNOWN_TRANSACTION) {
79 CLOGE("No permission when executing the pausing action");
80 return ERR_NO_PERMISSION;
81 } else if (ret == ERR_INVALID_DATA) {
82 CLOGE("Invalid parameter when executing the pausing action");
83 return ERR_INVALID_PARAM;
84 } else if (ret != ERR_NONE) {
85 CLOGE("Failed to send ipc request when executing the pausing action");
86 return CAST_ENGINE_ERROR;
87 }
88
89 return reply.ReadInt32();
90 }
91
SetSurface(sptr<IBufferProducer> producer)92 int32_t MirrorPlayerImplProxy::SetSurface(sptr<IBufferProducer> producer)
93 {
94 MessageParcel data, reply;
95 MessageOption option;
96
97 if (!data.WriteInterfaceToken(GetDescriptor())) {
98 CLOGE("Failed to write the interface token");
99 return CAST_ENGINE_ERROR;
100 }
101 if (!data.WriteRemoteObject(producer->AsObject())) {
102 CLOGE("Failed to write surface producer");
103 return CAST_ENGINE_ERROR;
104 }
105
106 int32_t ret = Remote()->SendRequest(SET_SURFACE, data, reply, option);
107 if (ret == ERR_UNKNOWN_TRANSACTION) {
108 CLOGE("No permission when setting surface");
109 return ERR_NO_PERMISSION;
110 } else if (ret != ERR_NONE) {
111 CLOGE("Failed to send ipc request when setting surface");
112 return CAST_ENGINE_ERROR;
113 }
114
115 return reply.ReadInt32();
116 }
117
SetAppInfo(const AppInfo & appInfo)118 int32_t MirrorPlayerImplProxy::SetAppInfo(const AppInfo &appInfo)
119 {
120 MessageParcel data, reply;
121 MessageOption option;
122
123 if (!data.WriteInterfaceToken(GetDescriptor())) {
124 CLOGE("Failed to write the interface token");
125 return CAST_ENGINE_ERROR;
126 }
127 if (!data.WriteInt32(appInfo.appUid) || !data.WriteUint32(appInfo.appTokenId) ||
128 !data.WriteInt32(appInfo.appPid)) {
129 CLOGE("Failed to write appInfo");
130 return CAST_ENGINE_ERROR;
131 }
132 CLOGI("start set app info impl proxy");
133 int32_t ret = Remote()->SendRequest(SET_APP_INFO, data, reply, option);
134 if (ret == ERR_UNKNOWN_TRANSACTION) {
135 CLOGE("No permission when setting surface");
136 return ERR_NO_PERMISSION;
137 } else if (ret != ERR_NONE) {
138 CLOGE("Failed to send ipc request when setting surface");
139 return CAST_ENGINE_ERROR;
140 }
141 return reply.ReadInt32();
142 }
143
DeliverInputEvent(const OHRemoteControlEvent & event)144 int32_t MirrorPlayerImplProxy::DeliverInputEvent(const OHRemoteControlEvent &event)
145 {
146 MessageParcel data, reply;
147 MessageOption option;
148
149 CLOGD("In, eventType:%d", static_cast<uint32_t>(event.eventType));
150
151 if (!data.WriteInterfaceToken(GetDescriptor())) {
152 CLOGE("Failed to write the interface token");
153 return CAST_ENGINE_ERROR;
154 }
155 if (!WriteRemoteControlEvent(data, event)) {
156 CLOGE("Failed to write the remote control event");
157 return CAST_ENGINE_ERROR;
158 }
159
160 int32_t ret = Remote()->SendRequest(DELIVER_INPUT_EVENT, data, reply, option);
161 if (ret == ERR_UNKNOWN_TRANSACTION) {
162 CLOGE("No permission when deliver input event");
163 return ERR_NO_PERMISSION;
164 } else if (ret == ERR_INVALID_DATA) {
165 CLOGE("Invalid parameter when deliver input event");
166 return ERR_INVALID_PARAM;
167 } else if (ret != ERR_NONE) {
168 CLOGE("Failed to send ipc request when deliver input event");
169 return CAST_ENGINE_ERROR;
170 }
171
172 return reply.ReadInt32();
173 }
174
InjectEvent(const OHRemoteControlEvent & event)175 int32_t MirrorPlayerImplProxy::InjectEvent(const OHRemoteControlEvent &event)
176 {
177 MessageParcel data;
178 MessageParcel reply;
179 MessageOption option;
180
181 CLOGD("In, eventType:%d", static_cast<uint32_t>(event.eventType));
182
183 if (!data.WriteInterfaceToken(GetDescriptor())) {
184 CLOGE("Failed to write the interface token");
185 return CAST_ENGINE_ERROR;
186 }
187 if (!WriteRemoteControlEvent(data, event)) {
188 CLOGE("Failed to write the remote control event");
189 return CAST_ENGINE_ERROR;
190 }
191
192 int32_t ret = Remote()->SendRequest(INJECT_EVENT, data, reply, option);
193 if (ret == ERR_UNKNOWN_TRANSACTION) {
194 CLOGE("No permission when deliver input event");
195 return ERR_NO_PERMISSION;
196 } else if (ret == ERR_INVALID_DATA) {
197 CLOGE("Invalid parameter when deliver input event");
198 return ERR_INVALID_PARAM;
199 } else if (ret != ERR_NONE) {
200 CLOGE("Failed to send ipc request when deliver input event");
201 return CAST_ENGINE_ERROR;
202 }
203
204 return reply.ReadInt32();
205 }
206
Release()207 int32_t MirrorPlayerImplProxy::Release()
208 {
209 MessageParcel data;
210 MessageParcel reply;
211 MessageOption option;
212
213 if (!data.WriteInterfaceToken(GetDescriptor())) {
214 CLOGE("Failed to write the interface token");
215 return CAST_ENGINE_ERROR;
216 }
217
218 int32_t ret = Remote()->SendRequest(RELEASE, data, reply, option);
219 if (ret == ERR_UNKNOWN_TRANSACTION) {
220 CLOGE("No permission when setting surface");
221 return ERR_NO_PERMISSION;
222 } else if (ret != ERR_NONE) {
223 CLOGE("Failed to send ipc request when setting surface");
224 return CAST_ENGINE_ERROR;
225 }
226
227 return reply.ReadInt32();
228 }
229
GetDisplayId(std::string & displayId)230 int32_t MirrorPlayerImplProxy::GetDisplayId(std::string &displayId)
231 {
232 MessageParcel data;
233 MessageParcel reply;
234 MessageOption option;
235 displayId = std::string{};
236 if (!data.WriteInterfaceToken(GetDescriptor())) {
237 CLOGE("Failed to write the interface token");
238 return CAST_ENGINE_ERROR;
239 }
240
241 int32_t ret = Remote()->SendRequest(GET_DISPLAYID, data, reply, option);
242 if (ret == ERR_UNKNOWN_TRANSACTION) {
243 CLOGE("No permission when getDisplayId");
244 return ERR_NO_PERMISSION;
245 } else if (ret != ERR_NONE) {
246 CLOGE("Failed to send ipc request when getDisplayId");
247 return CAST_ENGINE_ERROR;
248 }
249 int32_t errorCode = reply.ReadInt32();
250 CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
251 displayId = reply.ReadString();
252 return errorCode;
253 }
254
ResizeVirtualScreen(uint32_t width,uint32_t height)255 int32_t MirrorPlayerImplProxy::ResizeVirtualScreen(uint32_t width, uint32_t height)
256 {
257 MessageParcel data;
258 MessageParcel reply;
259 MessageOption option;
260
261 if (!data.WriteInterfaceToken(GetDescriptor())) {
262 CLOGE("Failed to write the interface token");
263 return CAST_ENGINE_ERROR;
264 }
265 if (!data.WriteUint32(width)) {
266 CLOGE("Failed to write the width");
267 return CAST_ENGINE_ERROR;
268 }
269 if (!data.WriteUint32(height)) {
270 CLOGE("Failed to write the height");
271 return CAST_ENGINE_ERROR;
272 }
273 if (Remote()->SendRequest(RESIZE_VIRTUAL_SCREEN, data, reply, option) != ERR_NONE) {
274 CLOGE("Failed to send ipc request when ResizeVirtualScreen");
275 return CAST_ENGINE_ERROR;
276 }
277
278 return reply.ReadInt32();
279 }
280 } // namespace CastEngineClient
281 } // namespace CastEngine
282 } // namespace OHOS
283