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 <iostream>
17
18 #include "accesstoken_kit.h"
19 #include "display.h"
20 #include "display_manager.h"
21 #include "dscreen_source_handler.h"
22 #include "dscreen_sink_handler.h"
23 #include "dscreen_util.h"
24 #include "idistributed_hardware_sink.h"
25 #include "idistributed_hardware_source.h"
26 #include "screen.h"
27 #include "screen_client.h"
28 #include "screen_client_common.h"
29 #include "screen_manager.h"
30 #include "wm_common.h"
31 #include "window.h"
32 #include "window_option.h"
33 #include "nativetoken_kit.h"
34 #include "token_setproc.h"
35
36 #include "decoder_demo.h"
37 #include "softbus_adapter_mem.h"
38 #include "softbus_bus_center.h"
39 #include "softbus_common.h"
40
41 using namespace std;
42 using namespace OHOS;
43 using namespace OHOS::DistributedHardware;
44 using namespace OHOS::Rosen;
45 using namespace OHOS::Media;
46 using namespace OHOS::Security::AccessToken;
47
48 namespace {
49 static char const *g_pkgName = "ohos.dsoftbus.tool";
50 }
51
52 constexpr int32_t SLEEP_FIVE_SECOND = 10;
53 static vector<sptr<Screen>> remoteScreens;
54 static uint64_t g_screenId = 0;
55
QueryRemoteScreenInfo(int mode)56 int QueryRemoteScreenInfo(int mode)
57 {
58 if (mode != 0) {
59 DHLOGE("QueryRemoteScreenInfo mode error");
60 return -1;
61 }
62 vector<sptr<Screen>> allScreens = ScreenManager::GetInstance().GetAllScreens();
63 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
64 for (const auto &screen : allScreens) {
65 if (screen == nullptr) {
66 continue;
67 }
68 if (!screen->IsReal() && screen->GetWidth() > 0) {
69 remoteScreens.push_back(screen);
70 }
71 }
72 DHLOGE("-------------remote screen info---------------");
73 DHLOGE("remote screen Num: %d", remoteScreens.size());
74 for (const auto &screen : remoteScreens) {
75 if (screen == nullptr) {
76 continue;
77 }
78 g_screenId = screen->GetId();
79 DHLOGE("--------screen id: %d ---------", screen->GetId());
80 DHLOGE("screen name: : %s", GetAnonyString(screen->GetName()).c_str());
81 DHLOGE("width: : %d", screen->GetWidth());
82 DHLOGE("height : %d", screen->GetHeight());
83 DHLOGE("-------------------------------------------");
84 }
85
86 return 0;
87 }
88
StartMirror(int mode)89 int StartMirror(int mode)
90 {
91 if (mode != 0) {
92 DHLOGE("StartMirror mode error");
93 return -1;
94 }
95 uint64_t ret = QueryRemoteScreenInfo(0);
96 if (ret != 0) {
97 DHLOGE("Error: no remote screens enabled");
98 return -1;
99 }
100
101 DHLOGE("select remote screen id to mirror");
102
103 bool isMirrorIdValid = false;
104 for (const auto &screen : remoteScreens) {
105 if (screen == nullptr) {
106 continue;
107 }
108 if (screen->GetId() == g_screenId) {
109 isMirrorIdValid = true;
110 break;
111 }
112 }
113
114 if (!isMirrorIdValid) {
115 DHLOGE("input mirrorId is not valid!");
116 return -1;
117 }
118
119 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
120 DHLOGE("------------start mirror----------");
121 DHLOGE("mirror screen Id is: %d", g_screenId);
122 vector<uint64_t> mirrorIds;
123 mirrorIds.push_back(g_screenId);
124 ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds);
125 sleep(SLEEP_FIVE_SECOND);
126 return 0;
127 }
128
StopMirror(int mode)129 int StopMirror(int mode)
130 {
131 if (mode != 0) {
132 DHLOGE("StopMirror mode error");
133 return -1;
134 }
135 uint64_t ret = QueryRemoteScreenInfo(0);
136 if (ret != 0) {
137 DHLOGE("no remote screens enabled, no need stop mirror ");
138 return -1;
139 }
140
141 bool isStopMirrorIdValid = false;
142 for (const auto &screen : remoteScreens) {
143 if (screen == nullptr) {
144 continue;
145 }
146 if (screen->GetId() == g_screenId) {
147 isStopMirrorIdValid = true;
148 break;
149 }
150 }
151 if (!isStopMirrorIdValid) {
152 DHLOGE("input g_screenId is not valid! ");
153 return -1;
154 }
155
156 DHLOGE("-------------- stop mirror ------------");
157 DHLOGE("stop mirror screen id is: %d", g_screenId);
158 vector<uint64_t> stopMirrorIds;
159 stopMirrorIds.push_back(g_screenId);
160 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopMirrorIds);
161 sleep(SLEEP_FIVE_SECOND);
162 return 0;
163 }
164
StartExpand(int mode)165 int StartExpand(int mode)
166 {
167 if (mode != 0) {
168 DHLOGE("StartExpand mode error");
169 return -1;
170 }
171 uint64_t ret = QueryRemoteScreenInfo(0);
172 if (ret != 0) {
173 DHLOGE("Error: no remote screens enabled");
174 return -1;
175 }
176
177 bool isExpandIdValid = false;
178 for (const auto &screen : remoteScreens) {
179 if (screen == nullptr) {
180 continue;
181 }
182 if (screen->GetId() == g_screenId) {
183 isExpandIdValid = true;
184 break;
185 }
186 }
187
188 if (!isExpandIdValid) {
189 DHLOGE("input expandId is not valid!");
190 return -1;
191 }
192
193 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
194 DHLOGE("------------start expand----------");
195 DHLOGE("expand screen Id is: %d", g_screenId);
196 vector<ExpandOption> options = {{defaultDisplay->GetScreenId(), 0, 0}, {g_screenId, defaultDisplay->GetWidth(), 0}};
197 ScreenManager::GetInstance().MakeExpand(options);
198 sleep(SLEEP_FIVE_SECOND);
199 return 0;
200 }
201
StopExpand(int mode)202 int StopExpand(int mode)
203 {
204 if (mode != 0) {
205 DHLOGE("StopExpand mode error");
206 return -1;
207 }
208 uint64_t ret = QueryRemoteScreenInfo(0);
209 if (ret != 0) {
210 DHLOGE("no remote screens enabled, no need stop expand");
211 return -1;
212 }
213
214 bool isStopExpandIdValid = false;
215 for (const auto &screen : remoteScreens) {
216 if (screen == nullptr) {
217 continue;
218 }
219 if (screen->GetId() == g_screenId) {
220 isStopExpandIdValid = true;
221 break;
222 }
223 }
224 if (!isStopExpandIdValid) {
225 DHLOGE("input g_screenId is not valid!");
226 return -1;
227 }
228
229 DHLOGE("-------------- stop expand ------------");
230 DHLOGE("stop expand screen id is : %d", g_screenId);
231 vector<uint64_t> stopExpandIds;
232 stopExpandIds.push_back(g_screenId);
233 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopExpandIds);
234 sleep(SLEEP_FIVE_SECOND);
235 return 0;
236 }
237
PrintNodeProperty(NodeBasicInfo * nodeInfo)238 static void PrintNodeProperty(NodeBasicInfo *nodeInfo)
239 {
240 if (nodeInfo == nullptr) {
241 DHLOGE("nodeInfo is nullptr");
242 return;
243 }
244
245 DHLOGE("DeviceName = %s", nodeInfo->deviceName);
246 DHLOGE("NetworkId = %s", GetAnonyString(nodeInfo->networkId).c_str());
247 NodeDeviceInfoKey key = NODE_KEY_UDID;
248 unsigned char udid[UDID_BUF_LEN] = {0};
249 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, udid, UDID_BUF_LEN) != 0) {
250 DHLOGE("GetNodeKeyInfo Fail!");
251 }
252 key = NODE_KEY_UUID;
253 unsigned char uuid[UUID_BUF_LEN] = {0};
254 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, uuid, UUID_BUF_LEN) != 0) {
255 DHLOGE("GetNodeKeyInfo Fail!");
256 } else {
257 DHLOGE("Uuid = %s\n", GetAnonyString(reinterpret_cast<char *>(udid)).c_str());
258 }
259 }
260
QueryRemoteDeviceInfo(int mode)261 int QueryRemoteDeviceInfo(int mode)
262 {
263 if (mode != 0) {
264 DHLOGE("QueryRemoteDeviceInfo mode error");
265 return -1;
266 }
267 uint64_t tokenId;
268 const char *perms[2];
269 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
270 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
271 NativeTokenInfoParams infoInstance = {
272 .dcapsNum = 0,
273 .permsNum = 2,
274 .aclsNum = 0,
275 .dcaps = NULL,
276 .perms = perms,
277 .acls = NULL,
278 .processName = "dscreen_test_demo",
279 .aplStr = "system_core",
280 };
281 tokenId = GetAccessTokenId(&infoInstance);
282 SetSelfTokenID(tokenId);
283 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
284
285 NodeBasicInfo localNodeinfo;
286 NodeBasicInfo *remoteNodeInfo = nullptr;
287 int32_t infoNum = 0;
288
289 DHLOGE("-----------Local Device Info------");
290
291 if (GetLocalNodeDeviceInfo(g_pkgName, &localNodeinfo) != 0) {
292 printf("LnnGetLocalNodeInfo Fail!\n");
293 DHLOGE("LnnGetLocalNodeInfo Fail!");
294 return -1;
295 }
296
297 PrintNodeProperty(&localNodeinfo);
298 DHLOGE("-------Remote Device info---------");
299 if (GetAllNodeDeviceInfo(g_pkgName, &remoteNodeInfo, &infoNum) != 0) {
300 DHLOGE("GetAllNodeDeviceInfo Fail!");
301 return -1;
302 }
303
304 DHLOGE("Device Num = %d", infoNum);
305 for (int i = 0; i < infoNum; ++i) {
306 DHLOGE("[No.%d]", i + 1);
307 PrintNodeProperty(remoteNodeInfo + i);
308 }
309
310 FreeNodeInfo(remoteNodeInfo);
311 DHLOGE("SoftBusDumpDeviceInfo complete");
312 sleep(SLEEP_FIVE_SECOND);
313 return 0;
314 }
315
CreateWindow(int mode)316 int CreateWindow(int mode)
317 {
318 if (mode != 0) {
319 DHLOGE("CreateWindow mode error");
320 return -1;
321 }
322 DHLOGE("create window, please input window size");
323
324 uint32_t windowWidth = 640;
325 uint32_t windowHeight = 480;
326
327 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
328 shared_ptr<WindowProperty> windowProperty = make_shared<WindowProperty>();
329 windowProperty->displayId = defaultDisplay->GetId();
330 windowProperty->startX = 0;
331 windowProperty->startY = 0;
332 windowProperty->width = windowWidth;
333 windowProperty->height = windowHeight;
334 int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty);
335 ScreenClient::GetInstance().ShowWindow(windowId);
336 sptr<Surface> surface = ScreenClient::GetInstance().GetSurface(windowId);
337 DHLOGE("create window success.");
338
339 auto vdec = make_shared<VDecDemo>();
340
341 vdec->SetWindowSize(windowWidth, windowHeight);
342 vdec->SetOutputSurface(surface);
343 DHLOGE("start run decoder");
344 vdec->RunCase();
345 DHLOGE("create window success, window id: %d, width: %d, height: %d", windowId, windowWidth, windowHeight);
346 ScreenClient::GetInstance().RemoveWindow(windowId);
347 sleep(SLEEP_FIVE_SECOND);
348 return 0;
349 }
350