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 const uint32_t MAX_WINDOW_WIDTH = 2560;
50 const uint32_t MAX_WINDOW_HEIGHT = 2772;
51 const uint32_t DCODE_WIDTH = 1920;
52 const uint32_t DCODE_HEIGHT = 1080;
53 }
54
QueryRemoteScreenInfo()55 vector<sptr<Screen>> QueryRemoteScreenInfo()
56 {
57 vector<sptr<Screen>> allScreens;
58 ScreenManager::GetInstance().GetAllScreens(allScreens);
59 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
60 vector<sptr<Screen>> remoteScreens;
61 for (const auto &screen : allScreens) {
62 if (screen == nullptr) {
63 continue;
64 }
65 if (!screen->IsReal() && screen->GetWidth() > 0) {
66 remoteScreens.push_back(screen);
67 }
68 }
69 cout << "-------------remote screen info---------------" << endl;
70 cout << "remote screen Num: " << remoteScreens.size() << endl;
71 for (const auto &screen : remoteScreens) {
72 if (screen == nullptr) {
73 continue;
74 }
75 cout << endl;
76 cout << "--------screen id " << screen->GetId() << "---------" << endl;
77 cout << "screen name: " << GetAnonyString(screen->GetName()).c_str() << endl;
78 cout << "width: " << screen->GetWidth() << endl;
79 cout << "height: " << screen->GetHeight() << endl;
80 cout << "-------------------------------------------" << endl;
81 }
82 return remoteScreens;
83 }
84
StartMirror()85 static void StartMirror()
86 {
87 vector<sptr<Screen>> remoteScreens = QueryRemoteScreenInfo();
88 if (remoteScreens.size() == 0) {
89 cout << "Error: no remote screens enabled" << endl;
90 return;
91 }
92
93 cout << "select remote screen id to mirror: " << endl;
94 uint64_t mirrorId;
95 cin >> mirrorId;
96
97 bool isMirrorIdValid = false;
98 for (const auto &screen : remoteScreens) {
99 if (screen == nullptr) {
100 continue;
101 }
102 if (screen->GetId() == mirrorId) {
103 isMirrorIdValid = true;
104 break;
105 }
106 }
107
108 if (!isMirrorIdValid) {
109 cout << "input mirrorId is not valid!" << endl;
110 return;
111 }
112
113 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
114 cout << "------------start mirror----------" << endl;
115 cout << "mirror screen Id is " << mirrorId << endl;
116 vector<uint64_t> mirrorIds;
117 mirrorIds.push_back(mirrorId);
118 ScreenId screenGroupId;
119 ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, screenGroupId);
120 }
121
StopMirror()122 static void StopMirror()
123 {
124 vector<sptr<Screen>> remoteScreens = QueryRemoteScreenInfo();
125 if (remoteScreens.size() == 0) {
126 cout << "no remote screens enabled, no need stop mirror" << endl;
127 return;
128 }
129
130 bool isStopMirrorIdValid = false;
131 cout << "select remote screen id to stop mirror: " << endl;
132 uint64_t stopMirrorId;
133 cin >> stopMirrorId;
134
135 for (const auto &screen : remoteScreens) {
136 if (screen == nullptr) {
137 continue;
138 }
139 if (screen->GetId() == stopMirrorId) {
140 isStopMirrorIdValid = true;
141 break;
142 }
143 }
144 if (!isStopMirrorIdValid) {
145 cout << "input screenId is not valid!" << endl;
146 return;
147 }
148
149 cout << "-------------- stop mirror ------------" << endl;
150 cout << "stop mirror screen id is " << stopMirrorId << endl;
151 vector<uint64_t> stopMirrorIds;
152 stopMirrorIds.push_back(stopMirrorId);
153 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopMirrorIds);
154 }
155
StartExpand()156 static void StartExpand()
157 {
158 vector<sptr<Screen>> remoteScreens = QueryRemoteScreenInfo();
159 if (remoteScreens.size() == 0) {
160 cout << "Error: no remote screens enabled" << endl;
161 return;
162 }
163
164 cout << "select remote screen id to expand: " << endl;
165 uint64_t expandId;
166 cin >> expandId;
167
168 bool isExpandIdValid = false;
169 for (const auto &screen : remoteScreens) {
170 if (screen == nullptr) {
171 continue;
172 }
173 if (screen->GetId() == expandId) {
174 isExpandIdValid = true;
175 break;
176 }
177 }
178
179 if (!isExpandIdValid) {
180 cout << "input expandId is not valid!" << endl;
181 return;
182 }
183
184 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
185 cout << endl << "------------start expand----------" << endl;
186 cout << "expand screen Id is " << expandId << endl;
187 vector<ExpandOption> options = {{defaultDisplay->GetScreenId(), 0, 0}, {expandId, defaultDisplay->GetWidth(), 0}};
188 ScreenId screenGroupId;
189 ScreenManager::GetInstance().MakeExpand(options, screenGroupId);
190 }
191
StopExpand()192 static void StopExpand()
193 {
194 vector<sptr<Screen>> remoteScreens = QueryRemoteScreenInfo();
195 if (remoteScreens.size() == 0) {
196 cout << "no remote screens enabled, no need stop expand" << endl;
197 return;
198 }
199
200 bool isStopExpandIdValid = false;
201 cout << "select remote screen id to stop expand: " << endl;
202 uint64_t stopExpandId;
203 cin >> stopExpandId;
204
205 for (const auto &screen : remoteScreens) {
206 if (screen == nullptr) {
207 continue;
208 }
209 if (screen->GetId() == stopExpandId) {
210 isStopExpandIdValid = true;
211 break;
212 }
213 }
214 if (!isStopExpandIdValid) {
215 cout << "input screenId is not valid!" << endl;
216 return;
217 }
218
219 cout << "-------------- stop expand ------------" << endl;
220 cout << "stop expand screen id is " << stopExpandId << endl;
221 vector<uint64_t> stopExpandIds;
222 stopExpandIds.push_back(stopExpandId);
223 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopExpandIds);
224 }
225
PrintNodeProperty(NodeBasicInfo * nodeInfo)226 static void PrintNodeProperty(NodeBasicInfo *nodeInfo)
227 {
228 if (nodeInfo == nullptr) {
229 cout << "nodeInfo is nullptr" << endl;
230 return;
231 }
232
233 printf("DeviceName = %s\n", nodeInfo->deviceName);
234 printf("NetworkId = %s\n", GetAnonyString(nodeInfo->networkId).c_str());
235 NodeDeviceInfoKey key = NODE_KEY_UDID;
236 unsigned char udid[UDID_BUF_LEN] = {0};
237 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, udid, UDID_BUF_LEN) != 0) {
238 printf("GetNodeKeyInfo Fail!\n");
239 } else {
240 printf("Udid = %s\n", GetAnonyString(reinterpret_cast<char *>(udid)).c_str());
241 }
242 key = NODE_KEY_UUID;
243 unsigned char uuid[UUID_BUF_LEN] = {0};
244 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, uuid, UUID_BUF_LEN) != 0) {
245 printf("GetNodeKeyInfo Fail!\n");
246 } else {
247 printf("Uuid = %s\n", GetAnonyString(reinterpret_cast<char *>(udid)).c_str());
248 }
249 }
250
QueryRemoteDeviceInfo()251 static void QueryRemoteDeviceInfo()
252 {
253 uint64_t tokenId;
254 const char *perms[2];
255 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
256 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
257 NativeTokenInfoParams infoInstance = {
258 .dcapsNum = 0,
259 .permsNum = 2,
260 .aclsNum = 0,
261 .dcaps = NULL,
262 .perms = perms,
263 .acls = NULL,
264 .processName = "dscreen_test_demo",
265 .aplStr = "system_core",
266 };
267 tokenId = GetAccessTokenId(&infoInstance);
268 SetSelfTokenID(tokenId);
269 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
270
271 NodeBasicInfo localNodeinfo;
272 NodeBasicInfo *remoteNodeInfo = nullptr;
273 int32_t infoNum = 0;
274 printf("-----------Local Device Info------\n");
275 if (GetLocalNodeDeviceInfo(g_pkgName, &localNodeinfo) != 0) {
276 printf("LnnGetLocalNodeInfo Fail!\n");
277 return;
278 }
279 PrintNodeProperty(&localNodeinfo);
280 printf("-------Remote Device info---------\n");
281 if (GetAllNodeDeviceInfo(g_pkgName, &remoteNodeInfo, &infoNum) != 0) {
282 printf("GetAllNodeDeviceInfo Fail!\n");
283 return;
284 }
285 printf("Device Num = %" PRId32 "\n", infoNum);
286 for (int i = 0; i < infoNum; ++i) {
287 printf("\n[No.%" PRId32 "]", i + 1);
288 PrintNodeProperty(remoteNodeInfo + i);
289 }
290 FreeNodeInfo(remoteNodeInfo);
291 printf("SoftBusDumpDeviceInfo complete!\n");
292 }
293
CreateWindow()294 static void CreateWindow()
295 {
296 cout << "create window, please input window size" << endl;
297 cout << "width: ";
298 uint32_t windowWidth;
299 cin >> windowWidth;
300 cout << "height: ";
301 uint32_t windowHeight;
302 cin >> windowHeight;
303
304 if (windowWidth == 0 || windowWidth >= MAX_WINDOW_WIDTH ||
305 windowHeight == 0 || windowHeight >= MAX_WINDOW_HEIGHT) {
306 cout << "Invalid window size." << endl;
307 return;
308 }
309
310 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
311 shared_ptr<WindowProperty> windowProperty = make_shared<WindowProperty>();
312 windowProperty->displayId = defaultDisplay->GetId();
313 windowProperty->startX = 0;
314 windowProperty->startY = 0;
315 windowProperty->width = windowWidth;
316 windowProperty->height = windowHeight;
317 int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty);
318 ScreenClient::GetInstance().ShowWindow(windowId);
319 sptr<Surface> surface = ScreenClient::GetInstance().GetSurface(windowId);
320 cout << "create window success." << endl;
321
322 auto vdec = make_shared<VDecDemo>();
323
324 vdec->SetWindowSize(DCODE_WIDTH, DCODE_HEIGHT);
325 vdec->SetOutputSurface(surface);
326 cout << "start run decoder" << endl;
327 vdec->RunCase();
328 cout << "create window success, window id: " << windowId
329 << ", width: " << windowWidth
330 << ", height: " << windowHeight << endl;
331 ScreenClient::GetInstance().RemoveWindow(windowId);
332 _Exit(0);
333 }
334
main()335 int main()
336 {
337 cout << "Please select a test scenario number(default StartMirror): " << endl;
338 cout << "0:StartMirror" << endl;
339 cout << "1:StopMirror" << endl;
340 cout << "2:StartExpand" << endl;
341 cout << "3:StopExpand" << endl;
342 cout << "4:CreateWindow" << endl;
343 cout << "5:QueryRemoteDeviceInfo" << endl;
344 cout << "6:QueryRemoteScreenInfo" << endl;
345 string mode;
346 (void)getline(cin, mode);
347 if (mode == "" || mode == "0") {
348 (void)StartMirror();
349 } else if (mode == "1") {
350 (void)StopMirror();
351 } else if (mode == "2") {
352 (void)StartExpand();
353 } else if (mode == "3") {
354 (void)StopExpand();
355 } else if (mode == "4") {
356 (void)CreateWindow();
357 } else if (mode == "5") {
358 (void)QueryRemoteDeviceInfo();
359 } else if (mode == "6") {
360 (void)QueryRemoteScreenInfo();
361 } else {
362 cout << "no that selection" << endl;
363 }
364 return 0;
365 }
366