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