1 /*
2 * Copyright (c) 2022-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
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_bus_center.h"
38 #include "softbus_common.h"
39
40 using namespace std;
41 using namespace OHOS;
42 using namespace OHOS::DistributedHardware;
43 using namespace OHOS::Rosen;
44 using namespace OHOS::Media;
45 using namespace OHOS::Security::AccessToken;
46
47 namespace {
48 static char const *g_pkgName = "ohos.dsoftbus.tool";
49 }
50
51 constexpr int32_t SLEEP_FIVE_SECOND = 10;
52 static vector<sptr<Screen>> remoteScreens;
53 static uint64_t g_screenId = 0;
54
QueryRemoteScreenInfo(int mode)55 int QueryRemoteScreenInfo(int mode)
56 {
57 if (mode != 0) {
58 DHLOGE("QueryRemoteScreenInfo mode error");
59 return -1;
60 }
61 vector<sptr<Screen>> allScreens;
62 ScreenManager::GetInstance().GetAllScreens(allScreens);
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 ScreenId screenGroupId;
125 ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, screenGroupId);
126 sleep(SLEEP_FIVE_SECOND);
127 return 0;
128 }
129
StopMirror(int mode)130 int StopMirror(int mode)
131 {
132 if (mode != 0) {
133 DHLOGE("StopMirror mode error");
134 return -1;
135 }
136 uint64_t ret = QueryRemoteScreenInfo(0);
137 if (ret != 0) {
138 DHLOGE("no remote screens enabled, no need stop mirror ");
139 return -1;
140 }
141
142 bool isStopMirrorIdValid = false;
143 for (const auto &screen : remoteScreens) {
144 if (screen == nullptr) {
145 continue;
146 }
147 if (screen->GetId() == g_screenId) {
148 isStopMirrorIdValid = true;
149 break;
150 }
151 }
152 if (!isStopMirrorIdValid) {
153 DHLOGE("input g_screenId is not valid! ");
154 return -1;
155 }
156
157 DHLOGE("-------------- stop mirror ------------");
158 DHLOGE("stop mirror screen id is: %d", g_screenId);
159 vector<uint64_t> stopMirrorIds;
160 stopMirrorIds.push_back(g_screenId);
161 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopMirrorIds);
162 sleep(SLEEP_FIVE_SECOND);
163 return 0;
164 }
165
StartExpand(int mode)166 int StartExpand(int mode)
167 {
168 if (mode != 0) {
169 DHLOGE("StartExpand mode error");
170 return -1;
171 }
172 uint64_t ret = QueryRemoteScreenInfo(0);
173 if (ret != 0) {
174 DHLOGE("Error: no remote screens enabled");
175 return -1;
176 }
177
178 bool isExpandIdValid = false;
179 for (const auto &screen : remoteScreens) {
180 if (screen == nullptr) {
181 continue;
182 }
183 if (screen->GetId() == g_screenId) {
184 isExpandIdValid = true;
185 break;
186 }
187 }
188
189 if (!isExpandIdValid) {
190 DHLOGE("input expandId is not valid!");
191 return -1;
192 }
193
194 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
195 DHLOGE("------------start expand----------");
196 DHLOGE("expand screen Id is: %d", g_screenId);
197 vector<ExpandOption> options = {{defaultDisplay->GetScreenId(), 0, 0}, {g_screenId, defaultDisplay->GetWidth(), 0}};
198 ScreenId screenGroupId;
199 ScreenManager::GetInstance().MakeExpand(options, screenGroupId);
200 sleep(SLEEP_FIVE_SECOND);
201 return 0;
202 }
203
StopExpand(int mode)204 int StopExpand(int mode)
205 {
206 if (mode != 0) {
207 DHLOGE("StopExpand mode error");
208 return -1;
209 }
210 uint64_t ret = QueryRemoteScreenInfo(0);
211 if (ret != 0) {
212 DHLOGE("no remote screens enabled, no need stop expand");
213 return -1;
214 }
215
216 bool isStopExpandIdValid = false;
217 for (const auto &screen : remoteScreens) {
218 if (screen == nullptr) {
219 continue;
220 }
221 if (screen->GetId() == g_screenId) {
222 isStopExpandIdValid = true;
223 break;
224 }
225 }
226 if (!isStopExpandIdValid) {
227 DHLOGE("input g_screenId is not valid!");
228 return -1;
229 }
230
231 DHLOGE("-------------- stop expand ------------");
232 DHLOGE("stop expand screen id is : %d", g_screenId);
233 vector<uint64_t> stopExpandIds;
234 stopExpandIds.push_back(g_screenId);
235 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopExpandIds);
236 sleep(SLEEP_FIVE_SECOND);
237 return 0;
238 }
239
PrintNodeProperty(NodeBasicInfo * nodeInfo)240 static void PrintNodeProperty(NodeBasicInfo *nodeInfo)
241 {
242 if (nodeInfo == nullptr) {
243 DHLOGE("nodeInfo is nullptr");
244 return;
245 }
246
247 DHLOGE("DeviceName = %s", nodeInfo->deviceName);
248 DHLOGE("NetworkId = %s", GetAnonyString(nodeInfo->networkId).c_str());
249 NodeDeviceInfoKey key = NODE_KEY_UDID;
250 unsigned char udid[UDID_BUF_LEN] = {0};
251 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, udid, UDID_BUF_LEN) != 0) {
252 DHLOGE("GetNodeKeyInfo Fail!");
253 }
254 key = NODE_KEY_UUID;
255 unsigned char uuid[UUID_BUF_LEN] = {0};
256 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, uuid, UUID_BUF_LEN) != 0) {
257 DHLOGE("GetNodeKeyInfo Fail!");
258 } else {
259 DHLOGE("Uuid = %s\n", GetAnonyString(reinterpret_cast<char *>(udid)).c_str());
260 }
261 }
262
QueryRemoteDeviceInfo(int mode)263 int QueryRemoteDeviceInfo(int mode)
264 {
265 if (mode != 0) {
266 DHLOGE("QueryRemoteDeviceInfo mode error");
267 return -1;
268 }
269 uint64_t tokenId;
270 const char *perms[2];
271 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
272 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
273 NativeTokenInfoParams infoInstance = {
274 .dcapsNum = 0,
275 .permsNum = 2,
276 .aclsNum = 0,
277 .dcaps = NULL,
278 .perms = perms,
279 .acls = NULL,
280 .processName = "dscreen_test_demo",
281 .aplStr = "system_core",
282 };
283 tokenId = GetAccessTokenId(&infoInstance);
284 SetSelfTokenID(tokenId);
285 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
286
287 NodeBasicInfo localNodeinfo;
288 NodeBasicInfo *remoteNodeInfo = nullptr;
289 int32_t infoNum = 0;
290
291 DHLOGE("-----------Local Device Info------");
292
293 if (GetLocalNodeDeviceInfo(g_pkgName, &localNodeinfo) != 0) {
294 printf("LnnGetLocalNodeInfo Fail!\n");
295 DHLOGE("LnnGetLocalNodeInfo Fail!");
296 return -1;
297 }
298
299 PrintNodeProperty(&localNodeinfo);
300 DHLOGE("-------Remote Device info---------");
301 if (GetAllNodeDeviceInfo(g_pkgName, &remoteNodeInfo, &infoNum) != 0) {
302 DHLOGE("GetAllNodeDeviceInfo Fail!");
303 return -1;
304 }
305
306 DHLOGE("Device Num = %d", infoNum);
307 for (int i = 0; i < infoNum; ++i) {
308 DHLOGE("[No.%d]", i + 1);
309 PrintNodeProperty(remoteNodeInfo + i);
310 }
311
312 FreeNodeInfo(remoteNodeInfo);
313 DHLOGE("SoftBusDumpDeviceInfo complete");
314 sleep(SLEEP_FIVE_SECOND);
315 return 0;
316 }
317
CreateWindow(int mode)318 int CreateWindow(int mode)
319 {
320 if (mode != 0) {
321 DHLOGE("CreateWindow mode error");
322 return -1;
323 }
324 DHLOGE("create window, please input window size");
325
326 uint32_t windowWidth = 640;
327 uint32_t windowHeight = 480;
328
329 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
330 shared_ptr<WindowProperty> windowProperty = make_shared<WindowProperty>();
331 windowProperty->displayId = defaultDisplay->GetId();
332 windowProperty->startX = 0;
333 windowProperty->startY = 0;
334 windowProperty->width = windowWidth;
335 windowProperty->height = windowHeight;
336 int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty);
337 ScreenClient::GetInstance().ShowWindow(windowId);
338 sptr<Surface> surface = ScreenClient::GetInstance().GetSurface(windowId);
339 DHLOGE("create window success.");
340
341 auto vdec = make_shared<VDecDemo>();
342
343 vdec->SetWindowSize(windowWidth, windowHeight);
344 vdec->SetOutputSurface(surface);
345 DHLOGE("start run decoder");
346 vdec->RunCase();
347 DHLOGE("create window success, window id: %d, width: %d, height: %d", windowId, windowWidth, windowHeight);
348 ScreenClient::GetInstance().RemoveWindow(windowId);
349 sleep(SLEEP_FIVE_SECOND);
350 return 0;
351 }
352