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
DeliverInputEvent(const OHRemoteControlEvent & event)92 int32_t MirrorPlayerImplProxy::DeliverInputEvent(const OHRemoteControlEvent &event)
93 {
94 MessageParcel data, reply;
95 MessageOption option;
96
97 CLOGD("In, eventType:%d", static_cast<uint32_t>(event.eventType));
98
99 if (!data.WriteInterfaceToken(GetDescriptor())) {
100 CLOGE("Failed to write the interface token");
101 return CAST_ENGINE_ERROR;
102 }
103 if (!WriteRemoteControlEvent(data, event)) {
104 CLOGE("Failed to write the remote control event");
105 return CAST_ENGINE_ERROR;
106 }
107
108 int32_t ret = Remote()->SendRequest(DELIVER_INPUT_EVENT, data, reply, option);
109 if (ret == ERR_UNKNOWN_TRANSACTION) {
110 CLOGE("No permission when deliver input event");
111 return ERR_NO_PERMISSION;
112 } else if (ret == ERR_INVALID_DATA) {
113 CLOGE("Invalid parameter when deliver input event");
114 return ERR_INVALID_PARAM;
115 } else if (ret != ERR_NONE) {
116 CLOGE("Failed to send ipc request when deliver input event");
117 return CAST_ENGINE_ERROR;
118 }
119
120 return reply.ReadInt32();
121 }
122
InjectEvent(const OHRemoteControlEvent & event)123 int32_t MirrorPlayerImplProxy::InjectEvent(const OHRemoteControlEvent &event)
124 {
125 MessageParcel data;
126 MessageParcel reply;
127 MessageOption option;
128
129 CLOGD("In, eventType:%d", static_cast<uint32_t>(event.eventType));
130
131 if (!data.WriteInterfaceToken(GetDescriptor())) {
132 CLOGE("Failed to write the interface token");
133 return CAST_ENGINE_ERROR;
134 }
135 if (!WriteRemoteControlEvent(data, event)) {
136 CLOGE("Failed to write the remote control event");
137 return CAST_ENGINE_ERROR;
138 }
139
140 int32_t ret = Remote()->SendRequest(INJECT_EVENT, data, reply, option);
141 if (ret == ERR_UNKNOWN_TRANSACTION) {
142 CLOGE("No permission when deliver input event");
143 return ERR_NO_PERMISSION;
144 } else if (ret == ERR_INVALID_DATA) {
145 CLOGE("Invalid parameter when deliver input event");
146 return ERR_INVALID_PARAM;
147 } else if (ret != ERR_NONE) {
148 CLOGE("Failed to send ipc request when deliver input event");
149 return CAST_ENGINE_ERROR;
150 }
151
152 return reply.ReadInt32();
153 }
154
Release()155 int32_t MirrorPlayerImplProxy::Release()
156 {
157 MessageParcel data;
158 MessageParcel reply;
159 MessageOption option;
160
161 if (!data.WriteInterfaceToken(GetDescriptor())) {
162 CLOGE("Failed to write the interface token");
163 return CAST_ENGINE_ERROR;
164 }
165
166 int32_t ret = Remote()->SendRequest(RELEASE, data, reply, option);
167 if (ret == ERR_UNKNOWN_TRANSACTION) {
168 CLOGE("No permission when setting surface");
169 return ERR_NO_PERMISSION;
170 } else if (ret != ERR_NONE) {
171 CLOGE("Failed to send ipc request when setting surface");
172 return CAST_ENGINE_ERROR;
173 }
174
175 return reply.ReadInt32();
176 }
177
178
ResizeVirtualScreen(uint32_t width,uint32_t height)179 int32_t MirrorPlayerImplProxy::ResizeVirtualScreen(uint32_t width, uint32_t height)
180 {
181 MessageParcel data;
182 MessageParcel reply;
183 MessageOption option;
184
185 if (!data.WriteInterfaceToken(GetDescriptor())) {
186 CLOGE("Failed to write the interface token");
187 return CAST_ENGINE_ERROR;
188 }
189 if (!data.WriteUint32(width)) {
190 CLOGE("Failed to write the width");
191 return CAST_ENGINE_ERROR;
192 }
193 if (!data.WriteUint32(height)) {
194 CLOGE("Failed to write the height");
195 return CAST_ENGINE_ERROR;
196 }
197 if (Remote()->SendRequest(RESIZE_VIRTUAL_SCREEN, data, reply, option) != ERR_NONE) {
198 CLOGE("Failed to send ipc request when ResizeVirtualScreen");
199 return CAST_ENGINE_ERROR;
200 }
201
202 return reply.ReadInt32();
203 }
204
GetDisplayId(std::string & displayId)205 int32_t MirrorPlayerImplProxy::GetDisplayId(std::string &displayId)
206 {
207 MessageParcel data;
208 MessageParcel reply;
209 MessageOption option;
210 displayId = std::string{};
211 if (!data.WriteInterfaceToken(GetDescriptor())) {
212 CLOGE("Failed to write the interface token");
213 return CAST_ENGINE_ERROR;
214 }
215
216 int32_t ret = Remote()->SendRequest(GET_DISPLAYID, data, reply, option);
217 if (ret == ERR_NO_PERMISSION) {
218 CLOGE("No permission when getDisplayId");
219 return ERR_NO_PERMISSION;
220 } else if (ret != ERR_NONE) {
221 CLOGE("Failed to send ipc request when getDisplayId");
222 return CAST_ENGINE_ERROR;
223 }
224 int32_t errorCode = reply.ReadInt32();
225 CHECK_AND_RETURN_RET_LOG(errorCode != CAST_ENGINE_SUCCESS, errorCode, "CastEngine Errors");
226 displayId = reply.ReadString();
227 return errorCode;
228 }
229
SetAppInfo(const AppInfo & appInfo)230 int32_t MirrorPlayerImplProxy::SetAppInfo(const AppInfo &appInfo)
231 {
232 MessageParcel data, reply;
233 MessageOption option;
234
235 if (!data.WriteInterfaceToken(GetDescriptor())) {
236 CLOGE("Failed to write the interface token");
237 return CAST_ENGINE_ERROR;
238 }
239 if (!data.WriteInt32(appInfo.appUid) || !data.WriteUint32(appInfo.appTokenId) ||
240 !data.WriteInt32(appInfo.appPid)) {
241 CLOGE("Failed to write appInfo");
242 return CAST_ENGINE_ERROR;
243 }
244 CLOGI("start set app info impl proxy");
245 int32_t ret = Remote()->SendRequest(SET_APP_INFO, data, reply, option);
246 if (ret == ERR_NO_PERMISSION) {
247 CLOGE("No permission when setting surface");
248 return ERR_NO_PERMISSION;
249 } else if (ret != ERR_NONE) {
250 CLOGE("Failed to send ipc request when setting surface");
251 return CAST_ENGINE_ERROR;
252 }
253 return reply.ReadInt32();
254 }
255
SetSurface(sptr<IBufferProducer> producer)256 int32_t MirrorPlayerImplProxy::SetSurface(sptr<IBufferProducer> producer)
257 {
258 MessageParcel data, 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.WriteRemoteObject(producer->AsObject())) {
266 CLOGE("Failed to write surface producer");
267 return CAST_ENGINE_ERROR;
268 }
269
270 int32_t ret = Remote()->SendRequest(SET_SURFACE, data, reply, option);
271 if (ret == ERR_NO_PERMISSION) {
272 CLOGE("No permission when setting surface");
273 return ERR_NO_PERMISSION;
274 } else if (ret != ERR_NONE) {
275 CLOGE("Failed to send ipc request when setting surface");
276 return CAST_ENGINE_ERROR;
277 }
278
279 return reply.ReadInt32();
280 }
281
282 } // namespace CastEngineClient
283 } // namespace CastEngine
284 } // namespace OHOS
285