@sqaitech/android-playground 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/bin/server.bin +0 -0
- package/bin/sqai-android-playground +3 -0
- package/dist/es/bin.mjs +470 -0
- package/dist/lib/bin.js +543 -0
- package/dist/types/bin.d.ts +1 -0
- package/package.json +48 -0
- package/static/favicon.ico +0 -0
- package/static/index.html +1 -0
- package/static/static/css/index.ea878c95.css +2 -0
- package/static/static/css/index.ea878c95.css.map +1 -0
- package/static/static/image/sqai-logo.b7f781cd.png +0 -0
- package/static/static/js/374.e8fd2f39.js +650 -0
- package/static/static/js/374.e8fd2f39.js.LICENSE.txt +173 -0
- package/static/static/js/374.e8fd2f39.js.map +1 -0
- package/static/static/js/async/166.834644b5.js +2 -0
- package/static/static/js/async/166.834644b5.js.map +1 -0
- package/static/static/js/async/173.f2381e64.js +3 -0
- package/static/static/js/async/173.f2381e64.js.map +1 -0
- package/static/static/js/async/212.850ade70.js +158 -0
- package/static/static/js/async/212.850ade70.js.map +1 -0
- package/static/static/js/async/329.261bc4a1.js +26 -0
- package/static/static/js/async/329.261bc4a1.js.map +1 -0
- package/static/static/js/async/364.d88c3cff.js +30 -0
- package/static/static/js/async/364.d88c3cff.js.map +1 -0
- package/static/static/js/async/544.18ac9afb.js +2 -0
- package/static/static/js/async/544.18ac9afb.js.map +1 -0
- package/static/static/js/async/582.8f4b5264.js +21 -0
- package/static/static/js/async/582.8f4b5264.js.map +1 -0
- package/static/static/js/async/624.8a1fe2e8.js +3 -0
- package/static/static/js/async/624.8a1fe2e8.js.map +1 -0
- package/static/static/js/async/644.910ce3d0.js +1 -0
- package/static/static/js/async/659.d19e8c15.js +21 -0
- package/static/static/js/async/659.d19e8c15.js.map +1 -0
- package/static/static/js/async/702.1f38a17e.js +231 -0
- package/static/static/js/async/702.1f38a17e.js.map +1 -0
- package/static/static/js/async/920.48d269c8.js +2 -0
- package/static/static/js/async/920.48d269c8.js.map +1 -0
- package/static/static/js/async/983.b98b40af.js +1 -0
- package/static/static/js/index.601ef220.js +10 -0
- package/static/static/js/index.601ef220.js.LICENSE.txt +7 -0
- package/static/static/js/index.601ef220.js.map +1 -0
- package/static/static/js/index.6a22ac98.js +10 -0
- package/static/static/js/index.6a22ac98.js.LICENSE.txt +7 -0
- package/static/static/js/index.6a22ac98.js.map +1 -0
- package/static/static/js/index.7dfa4eb9.js +10 -0
- package/static/static/js/index.7dfa4eb9.js.LICENSE.txt +7 -0
- package/static/static/js/index.7dfa4eb9.js.map +1 -0
- package/static/static/js/index.a6c47344.js +10 -0
- package/static/static/js/index.a6c47344.js.LICENSE.txt +7 -0
- package/static/static/js/index.a6c47344.js.map +1 -0
- package/static/static/js/index.c1b7cd4d.js +10 -0
- package/static/static/js/index.c1b7cd4d.js.LICENSE.txt +7 -0
- package/static/static/js/index.c1b7cd4d.js.map +1 -0
- package/static/static/js/index.c645d278.js +10 -0
- package/static/static/js/index.c645d278.js.LICENSE.txt +7 -0
- package/static/static/js/index.c645d278.js.map +1 -0
- package/static/static/js/index.d16798c4.js +10 -0
- package/static/static/js/index.d16798c4.js.LICENSE.txt +7 -0
- package/static/static/js/index.d16798c4.js.map +1 -0
- package/static/static/js/index.d9ec7a58.js +10 -0
- package/static/static/js/index.d9ec7a58.js.LICENSE.txt +7 -0
- package/static/static/js/index.d9ec7a58.js.map +1 -0
- package/static/static/js/index.f02202b8.js +10 -0
- package/static/static/js/index.f02202b8.js.LICENSE.txt +7 -0
- package/static/static/js/index.f02202b8.js.map +1 -0
- package/static/static/js/index.f5c7fdf6.js +10 -0
- package/static/static/js/index.f5c7fdf6.js.LICENSE.txt +7 -0
- package/static/static/js/index.f5c7fdf6.js.map +1 -0
- package/static/static/js/lib-react.c74a0742.js +3 -0
- package/static/static/js/lib-react.c74a0742.js.LICENSE.txt +39 -0
- package/static/static/js/lib-react.c74a0742.js.map +1 -0
- package/static/static/wasm/9e906fbf55e08f98.module.wasm +0 -0
package/dist/lib/bin.js
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"@yume-chan/adb": function(module) {
|
|
4
|
+
module.exports = import("@yume-chan/adb").then(function(module) {
|
|
5
|
+
return module;
|
|
6
|
+
});
|
|
7
|
+
},
|
|
8
|
+
"@yume-chan/adb-scrcpy": function(module) {
|
|
9
|
+
module.exports = import("@yume-chan/adb-scrcpy").then(function(module) {
|
|
10
|
+
return module;
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
"@yume-chan/adb-server-node-tcp": function(module) {
|
|
14
|
+
module.exports = import("@yume-chan/adb-server-node-tcp").then(function(module) {
|
|
15
|
+
return module;
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
"@yume-chan/scrcpy": function(module) {
|
|
19
|
+
module.exports = import("@yume-chan/scrcpy").then(function(module) {
|
|
20
|
+
return module;
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
"@yume-chan/stream-extra": function(module) {
|
|
24
|
+
module.exports = import("@yume-chan/stream-extra").then(function(module) {
|
|
25
|
+
return module;
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
open: function(module) {
|
|
29
|
+
module.exports = import("open").then(function(module) {
|
|
30
|
+
return module;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var __webpack_module_cache__ = {};
|
|
35
|
+
function __webpack_require__(moduleId) {
|
|
36
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
37
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
38
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
39
|
+
exports: {}
|
|
40
|
+
};
|
|
41
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
42
|
+
return module.exports;
|
|
43
|
+
}
|
|
44
|
+
(()=>{
|
|
45
|
+
__webpack_require__.n = (module)=>{
|
|
46
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
47
|
+
__webpack_require__.d(getter, {
|
|
48
|
+
a: getter
|
|
49
|
+
});
|
|
50
|
+
return getter;
|
|
51
|
+
};
|
|
52
|
+
})();
|
|
53
|
+
(()=>{
|
|
54
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
55
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: definition[key]
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
})();
|
|
61
|
+
(()=>{
|
|
62
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
63
|
+
})();
|
|
64
|
+
var __webpack_exports__ = {};
|
|
65
|
+
(()=>{
|
|
66
|
+
const external_node_child_process_namespaceObject = require("node:child_process");
|
|
67
|
+
const external_node_net_namespaceObject = require("node:net");
|
|
68
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
69
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
70
|
+
const external_node_util_namespaceObject = require("node:util");
|
|
71
|
+
const prompts_namespaceObject = require("@inquirer/prompts");
|
|
72
|
+
const android_namespaceObject = require("@sqaitech/android");
|
|
73
|
+
const playground_namespaceObject = require("@sqaitech/playground");
|
|
74
|
+
const constants_namespaceObject = require("@sqaitech/shared/constants");
|
|
75
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
76
|
+
const external_node_http_namespaceObject = require("node:http");
|
|
77
|
+
require("node:url");
|
|
78
|
+
const logger_namespaceObject = require("@sqaitech/shared/logger");
|
|
79
|
+
const external_cors_namespaceObject = require("cors");
|
|
80
|
+
var external_cors_default = /*#__PURE__*/ __webpack_require__.n(external_cors_namespaceObject);
|
|
81
|
+
const external_express_namespaceObject = require("express");
|
|
82
|
+
var external_express_default = /*#__PURE__*/ __webpack_require__.n(external_express_namespaceObject);
|
|
83
|
+
const external_socket_io_namespaceObject = require("socket.io");
|
|
84
|
+
function _define_property(obj, key, value) {
|
|
85
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
86
|
+
value: value,
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true
|
|
90
|
+
});
|
|
91
|
+
else obj[key] = value;
|
|
92
|
+
return obj;
|
|
93
|
+
}
|
|
94
|
+
const debugPage = (0, logger_namespaceObject.getDebug)('android:playground');
|
|
95
|
+
const promiseExec = (0, external_node_util_namespaceObject.promisify)(external_node_child_process_namespaceObject.exec);
|
|
96
|
+
class ScrcpyServer {
|
|
97
|
+
setupApiRoutes() {
|
|
98
|
+
this.app.get('/api/devices', async (req, res)=>{
|
|
99
|
+
try {
|
|
100
|
+
const devices = await this.getDevicesList();
|
|
101
|
+
res.json({
|
|
102
|
+
devices,
|
|
103
|
+
currentDeviceId: this.currentDeviceId
|
|
104
|
+
});
|
|
105
|
+
} catch (error) {
|
|
106
|
+
res.status(500).json({
|
|
107
|
+
error: error.message || 'Failed to get devices list'
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async getDevicesList() {
|
|
113
|
+
try {
|
|
114
|
+
debugPage('start to get devices list');
|
|
115
|
+
const client = await this.getAdbClient();
|
|
116
|
+
if (!client) {
|
|
117
|
+
console.warn('failed to get adb client');
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
debugPage('success to get adb client, start to request devices list');
|
|
121
|
+
let devices;
|
|
122
|
+
try {
|
|
123
|
+
devices = await client.getDevices();
|
|
124
|
+
debugPage('original devices list:', devices);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error('failed to get devices list:', error);
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
if (!devices || 0 === devices.length) return [];
|
|
130
|
+
const formattedDevices = devices.map((device)=>{
|
|
131
|
+
const result = {
|
|
132
|
+
id: device.serial,
|
|
133
|
+
name: device.product || device.model || device.serial,
|
|
134
|
+
status: device.state || 'device'
|
|
135
|
+
};
|
|
136
|
+
return result;
|
|
137
|
+
});
|
|
138
|
+
return formattedDevices;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error('failed to get devices list:', error);
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async getAdbClient() {
|
|
145
|
+
const { AdbServerClient } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/adb"));
|
|
146
|
+
const { AdbServerNodeTcpConnector } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/adb-server-node-tcp"));
|
|
147
|
+
try {
|
|
148
|
+
if (this.adbClient) debugPage('use existing adb client');
|
|
149
|
+
else {
|
|
150
|
+
await promiseExec('adb start-server');
|
|
151
|
+
debugPage('adb server started');
|
|
152
|
+
debugPage('initialize adb client');
|
|
153
|
+
this.adbClient = new AdbServerClient(new AdbServerNodeTcpConnector({
|
|
154
|
+
host: '127.0.0.1',
|
|
155
|
+
port: 5037
|
|
156
|
+
}));
|
|
157
|
+
await debugPage('success to initialize adb client');
|
|
158
|
+
}
|
|
159
|
+
return this.adbClient;
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error('failed to get adb client:', error);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async getAdb(deviceId) {
|
|
166
|
+
const { Adb } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/adb"));
|
|
167
|
+
try {
|
|
168
|
+
const client = await this.getAdbClient();
|
|
169
|
+
if (!client) return null;
|
|
170
|
+
const targetDeviceId = deviceId || this.currentDeviceId;
|
|
171
|
+
if (targetDeviceId) {
|
|
172
|
+
this.currentDeviceId = targetDeviceId;
|
|
173
|
+
return new Adb(await client.createTransport({
|
|
174
|
+
serial: targetDeviceId
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
const devices = await client.getDevices();
|
|
178
|
+
if (0 === devices.length) return null;
|
|
179
|
+
this.currentDeviceId = devices[0].serial;
|
|
180
|
+
return new Adb(await client.createTransport(devices[0]));
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error('failed to get adb client:', error);
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async startScrcpy(adb, options = {}) {
|
|
187
|
+
const { AdbScrcpyClient, AdbScrcpyOptions2_1 } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/adb-scrcpy"));
|
|
188
|
+
const { ReadableStream } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/stream-extra"));
|
|
189
|
+
const { ScrcpyOptions3_1, DefaultServerPath } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/scrcpy"));
|
|
190
|
+
const currentDir = __dirname;
|
|
191
|
+
const serverBinPath = external_node_path_default().resolve(currentDir, '../../bin/server.bin');
|
|
192
|
+
try {
|
|
193
|
+
await AdbScrcpyClient.pushServer(adb, ReadableStream.from((0, external_node_fs_namespaceObject.createReadStream)(serverBinPath)));
|
|
194
|
+
const scrcpyOptions = new ScrcpyOptions3_1({
|
|
195
|
+
audio: false,
|
|
196
|
+
control: true,
|
|
197
|
+
maxSize: 1024,
|
|
198
|
+
videoBitRate: 2000000,
|
|
199
|
+
...options
|
|
200
|
+
});
|
|
201
|
+
return await AdbScrcpyClient.start(adb, DefaultServerPath, new AdbScrcpyOptions2_1(scrcpyOptions));
|
|
202
|
+
} catch (error) {
|
|
203
|
+
console.error('failed to start scrcpy:', error);
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
setupSocketHandlers() {
|
|
208
|
+
this.io.on('connection', async (socket)=>{
|
|
209
|
+
debugPage('client connected, id: %s, client address: %s', socket.id, socket.handshake.address);
|
|
210
|
+
let scrcpyClient = null;
|
|
211
|
+
let adb = null;
|
|
212
|
+
const sendDevicesList = async ()=>{
|
|
213
|
+
try {
|
|
214
|
+
debugPage('Socket request to get devices list');
|
|
215
|
+
const devices = await this.getDevicesList();
|
|
216
|
+
debugPage('send devices list to client:', devices);
|
|
217
|
+
socket.emit('devices-list', {
|
|
218
|
+
devices,
|
|
219
|
+
currentDeviceId: this.currentDeviceId
|
|
220
|
+
});
|
|
221
|
+
} catch (error) {
|
|
222
|
+
console.error('failed to send devices list:', error);
|
|
223
|
+
socket.emit('error', {
|
|
224
|
+
message: 'failed to get devices list'
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
await sendDevicesList();
|
|
229
|
+
socket.on('get-devices', async ()=>{
|
|
230
|
+
debugPage('received client request to get devices list');
|
|
231
|
+
await sendDevicesList();
|
|
232
|
+
});
|
|
233
|
+
socket.on('switch-device', async (deviceId)=>{
|
|
234
|
+
debugPage('received client request to switch device:', deviceId);
|
|
235
|
+
try {
|
|
236
|
+
if (scrcpyClient) {
|
|
237
|
+
await scrcpyClient.close();
|
|
238
|
+
scrcpyClient = null;
|
|
239
|
+
}
|
|
240
|
+
this.currentDeviceId = deviceId;
|
|
241
|
+
debugPage('device switched to:', deviceId);
|
|
242
|
+
socket.emit('device-switched', {
|
|
243
|
+
deviceId
|
|
244
|
+
});
|
|
245
|
+
this.io.emit('global-device-switched', {
|
|
246
|
+
deviceId,
|
|
247
|
+
timestamp: Date.now()
|
|
248
|
+
});
|
|
249
|
+
} catch (error) {
|
|
250
|
+
console.error('failed to switch device:', error);
|
|
251
|
+
socket.emit('error', {
|
|
252
|
+
message: `Failed to switch device: ${(null == error ? void 0 : error.message) || 'Unknown error'}`
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
socket.on('connect-device', async (options)=>{
|
|
257
|
+
const { ScrcpyVideoCodecId } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@yume-chan/scrcpy"));
|
|
258
|
+
try {
|
|
259
|
+
debugPage('received device connection request, options: %s, client id: %s', options, socket.id);
|
|
260
|
+
adb = await this.getAdb(this.currentDeviceId || void 0);
|
|
261
|
+
if (!adb) {
|
|
262
|
+
console.error('no available device found');
|
|
263
|
+
socket.emit('error', {
|
|
264
|
+
message: 'No device found'
|
|
265
|
+
});
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
debugPage('starting scrcpy service, device id: %s', this.currentDeviceId);
|
|
269
|
+
scrcpyClient = await this.startScrcpy(adb, options);
|
|
270
|
+
debugPage('scrcpy service started successfully');
|
|
271
|
+
debugPage('check scrcpyClient object structure: %s', Object.getOwnPropertyNames(scrcpyClient).map((name)=>{
|
|
272
|
+
const type = typeof scrcpyClient[name];
|
|
273
|
+
const isPromise = 'object' === type && scrcpyClient[name] && 'function' == typeof scrcpyClient[name].then;
|
|
274
|
+
return `${name}: ${type}${isPromise ? ' (Promise)' : ''}`;
|
|
275
|
+
}));
|
|
276
|
+
try {
|
|
277
|
+
if (scrcpyClient.videoStream) {
|
|
278
|
+
debugPage('videoStream exists, type: %s', typeof scrcpyClient.videoStream);
|
|
279
|
+
let videoStream;
|
|
280
|
+
if ('object' == typeof scrcpyClient.videoStream && 'function' == typeof scrcpyClient.videoStream.then) {
|
|
281
|
+
debugPage('videoStream is a Promise, waiting for resolution...');
|
|
282
|
+
videoStream = await scrcpyClient.videoStream;
|
|
283
|
+
} else {
|
|
284
|
+
debugPage('videoStream is not a Promise, directly use');
|
|
285
|
+
videoStream = scrcpyClient.videoStream;
|
|
286
|
+
}
|
|
287
|
+
debugPage('video stream fetched successfully, metadata: %s', videoStream.metadata);
|
|
288
|
+
const metadata = videoStream.metadata || {};
|
|
289
|
+
debugPage('original metadata: %s', metadata);
|
|
290
|
+
if (!metadata.codec) {
|
|
291
|
+
debugPage('metadata does not have codec field, use H264 by default');
|
|
292
|
+
metadata.codec = ScrcpyVideoCodecId.H264;
|
|
293
|
+
}
|
|
294
|
+
if (!metadata.width || !metadata.height) {
|
|
295
|
+
debugPage('metadata does not have width or height field, use default values');
|
|
296
|
+
metadata.width = metadata.width || 1080;
|
|
297
|
+
metadata.height = metadata.height || 1920;
|
|
298
|
+
}
|
|
299
|
+
debugPage('prepare to send video-metadata event to client, data: %s', JSON.stringify(metadata));
|
|
300
|
+
socket.emit('video-metadata', metadata);
|
|
301
|
+
debugPage('video-metadata event sent to client, id: %s', socket.id);
|
|
302
|
+
const { stream } = videoStream;
|
|
303
|
+
const reader = stream.getReader();
|
|
304
|
+
const processStream = async ()=>{
|
|
305
|
+
try {
|
|
306
|
+
while(true){
|
|
307
|
+
const { done, value } = await reader.read();
|
|
308
|
+
if (done) break;
|
|
309
|
+
const frameType = value.type || 'data';
|
|
310
|
+
socket.emit('video-data', {
|
|
311
|
+
data: Array.from(value.data),
|
|
312
|
+
type: frameType,
|
|
313
|
+
timestamp: Date.now(),
|
|
314
|
+
keyFrame: value.keyFrame
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.error('error processing video stream:', error);
|
|
319
|
+
socket.emit('error', {
|
|
320
|
+
message: 'video stream processing error'
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
processStream();
|
|
325
|
+
} else {
|
|
326
|
+
console.error('scrcpyClient object does not have videoStream property');
|
|
327
|
+
socket.emit('error', {
|
|
328
|
+
message: 'Video stream not available in scrcpy client'
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
} catch (error) {
|
|
332
|
+
console.error('error processing video stream:', error);
|
|
333
|
+
socket.emit('error', {
|
|
334
|
+
message: `Video stream processing error: ${error.message}`
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
if (null == scrcpyClient ? void 0 : scrcpyClient.controller) socket.emit('control-ready');
|
|
338
|
+
} catch (error) {
|
|
339
|
+
console.error('failed to connect device:', error);
|
|
340
|
+
socket.emit('error', {
|
|
341
|
+
message: `Failed to connect device: ${(null == error ? void 0 : error.message) || 'Unknown error'}`
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
socket.on('disconnect', async (reason)=>{
|
|
346
|
+
debugPage('client disconnected, id: %s, reason: %s', socket.id, reason);
|
|
347
|
+
if (scrcpyClient) {
|
|
348
|
+
try {
|
|
349
|
+
debugPage('closing scrcpy client');
|
|
350
|
+
await scrcpyClient.close();
|
|
351
|
+
} catch (error) {
|
|
352
|
+
console.error('failed to close scrcpy client:', error);
|
|
353
|
+
}
|
|
354
|
+
scrcpyClient = null;
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
async launch(port) {
|
|
360
|
+
this.port = port || this.defaultPort;
|
|
361
|
+
return new Promise((resolve)=>{
|
|
362
|
+
this.httpServer.listen(this.port, ()=>{
|
|
363
|
+
console.log(`Scrcpy server running at: http://localhost:${this.port}`);
|
|
364
|
+
this.startDeviceMonitoring();
|
|
365
|
+
resolve(this);
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
startDeviceMonitoring() {
|
|
370
|
+
this.devicePollInterval = setInterval(async ()=>{
|
|
371
|
+
try {
|
|
372
|
+
const devices = await this.getDevicesList();
|
|
373
|
+
const currentDevicesJson = JSON.stringify(devices);
|
|
374
|
+
if (this.lastDeviceList !== currentDevicesJson) {
|
|
375
|
+
debugPage('devices list changed, push to all connected clients');
|
|
376
|
+
this.lastDeviceList = currentDevicesJson;
|
|
377
|
+
if (!this.currentDeviceId && devices.length > 0) {
|
|
378
|
+
const onlineDevices = devices.filter((device)=>'device' === device.status.toLowerCase());
|
|
379
|
+
if (onlineDevices.length > 0) {
|
|
380
|
+
this.currentDeviceId = onlineDevices[0].id;
|
|
381
|
+
debugPage('auto select the first online device:', this.currentDeviceId);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
this.io.emit('devices-list', {
|
|
385
|
+
devices,
|
|
386
|
+
currentDeviceId: this.currentDeviceId
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
} catch (error) {
|
|
390
|
+
console.error('device monitoring error:', error);
|
|
391
|
+
}
|
|
392
|
+
}, 3000);
|
|
393
|
+
}
|
|
394
|
+
close() {
|
|
395
|
+
if (this.devicePollInterval) {
|
|
396
|
+
clearInterval(this.devicePollInterval);
|
|
397
|
+
this.devicePollInterval = null;
|
|
398
|
+
}
|
|
399
|
+
if (this.httpServer) return this.httpServer.close();
|
|
400
|
+
}
|
|
401
|
+
constructor(){
|
|
402
|
+
_define_property(this, "app", void 0);
|
|
403
|
+
_define_property(this, "httpServer", void 0);
|
|
404
|
+
_define_property(this, "io", void 0);
|
|
405
|
+
_define_property(this, "port", void 0);
|
|
406
|
+
_define_property(this, "defaultPort", constants_namespaceObject.SCRCPY_SERVER_PORT);
|
|
407
|
+
_define_property(this, "adbClient", null);
|
|
408
|
+
_define_property(this, "currentDeviceId", null);
|
|
409
|
+
_define_property(this, "devicePollInterval", null);
|
|
410
|
+
_define_property(this, "lastDeviceList", '');
|
|
411
|
+
this.app = external_express_default()();
|
|
412
|
+
this.httpServer = (0, external_node_http_namespaceObject.createServer)(this.app);
|
|
413
|
+
this.io = new external_socket_io_namespaceObject.Server(this.httpServer, {
|
|
414
|
+
cors: {
|
|
415
|
+
origin: [
|
|
416
|
+
/^http:\/\/localhost(:\d+)?$/,
|
|
417
|
+
/^http:\/\/127\.0\.0\.1(:\d+)?$/
|
|
418
|
+
],
|
|
419
|
+
methods: [
|
|
420
|
+
'GET',
|
|
421
|
+
'POST'
|
|
422
|
+
],
|
|
423
|
+
credentials: true
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
this.app.use(external_cors_default()({
|
|
427
|
+
origin: '*',
|
|
428
|
+
credentials: true
|
|
429
|
+
}));
|
|
430
|
+
this.setupSocketHandlers();
|
|
431
|
+
this.setupApiRoutes();
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const bin_promiseExec = (0, external_node_util_namespaceObject.promisify)(external_node_child_process_namespaceObject.exec);
|
|
435
|
+
async function isPortAvailable(port) {
|
|
436
|
+
return new Promise((resolve)=>{
|
|
437
|
+
const server = (0, external_node_net_namespaceObject.createServer)();
|
|
438
|
+
server.on('error', ()=>resolve(false));
|
|
439
|
+
server.listen(port, ()=>{
|
|
440
|
+
server.close(()=>resolve(true));
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
async function findAvailablePort(startPort) {
|
|
445
|
+
let port = startPort;
|
|
446
|
+
let attempts = 0;
|
|
447
|
+
const maxAttempts = 15;
|
|
448
|
+
while(!await isPortAvailable(port)){
|
|
449
|
+
attempts++;
|
|
450
|
+
if (attempts >= maxAttempts) {
|
|
451
|
+
console.error(`\u{274C} Unable to find available port after ${maxAttempts} attempts starting from ${startPort}`);
|
|
452
|
+
process.exit(1);
|
|
453
|
+
}
|
|
454
|
+
port++;
|
|
455
|
+
}
|
|
456
|
+
return port;
|
|
457
|
+
}
|
|
458
|
+
async function getAdbDevices() {
|
|
459
|
+
try {
|
|
460
|
+
await bin_promiseExec('adb start-server');
|
|
461
|
+
const { stdout } = await bin_promiseExec('adb devices');
|
|
462
|
+
const lines = stdout.trim().split('\n').slice(1);
|
|
463
|
+
const devices = lines.map((line)=>{
|
|
464
|
+
const parts = line.trim().split('\t');
|
|
465
|
+
if (parts.length >= 2) return {
|
|
466
|
+
id: parts[0],
|
|
467
|
+
status: parts[1],
|
|
468
|
+
name: parts[0]
|
|
469
|
+
};
|
|
470
|
+
return null;
|
|
471
|
+
}).filter((device)=>null !== device && 'device' === device.status);
|
|
472
|
+
return devices;
|
|
473
|
+
} catch (error) {
|
|
474
|
+
console.error('Error getting ADB devices:', error);
|
|
475
|
+
return [];
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
async function selectDevice() {
|
|
479
|
+
console.log("\uD83D\uDD0D Scanning for Android devices...");
|
|
480
|
+
const devices = await getAdbDevices();
|
|
481
|
+
if (0 === devices.length) {
|
|
482
|
+
console.error("\u274C No Android devices found!");
|
|
483
|
+
console.log("\uD83D\uDCF1 Please ensure:");
|
|
484
|
+
console.log(" \u2022 Your device is connected via USB");
|
|
485
|
+
console.log(" \u2022 USB debugging is enabled");
|
|
486
|
+
console.log(" \u2022 Device is authorized for debugging");
|
|
487
|
+
process.exit(1);
|
|
488
|
+
}
|
|
489
|
+
if (1 === devices.length) {
|
|
490
|
+
console.log(`\u{1F4F1} Found device: ${devices[0].name} (${devices[0].id})`);
|
|
491
|
+
return devices[0].id;
|
|
492
|
+
}
|
|
493
|
+
const choices = devices.map((device)=>({
|
|
494
|
+
name: `${device.name} (${device.id})`,
|
|
495
|
+
value: device.id
|
|
496
|
+
}));
|
|
497
|
+
const selectedDevice = await (0, prompts_namespaceObject.select)({
|
|
498
|
+
message: "\uD83D\uDCF1 Multiple devices found. Please select one:",
|
|
499
|
+
choices
|
|
500
|
+
});
|
|
501
|
+
return selectedDevice;
|
|
502
|
+
}
|
|
503
|
+
const staticDir = external_node_path_default().join(__dirname, '../../static');
|
|
504
|
+
const main = async ()=>{
|
|
505
|
+
const { default: open } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "open"));
|
|
506
|
+
try {
|
|
507
|
+
const selectedDeviceId = await selectDevice();
|
|
508
|
+
console.log(`\u{2705} Selected device: ${selectedDeviceId}`);
|
|
509
|
+
const playgroundServer = new playground_namespaceObject.PlaygroundServer(async ()=>{
|
|
510
|
+
const device = new android_namespaceObject.AndroidDevice(selectedDeviceId);
|
|
511
|
+
await device.connect();
|
|
512
|
+
return new android_namespaceObject.AndroidAgent(device);
|
|
513
|
+
}, staticDir);
|
|
514
|
+
const scrcpyServer = new ScrcpyServer();
|
|
515
|
+
scrcpyServer.currentDeviceId = selectedDeviceId;
|
|
516
|
+
console.log("\uD83D\uDE80 Starting servers...");
|
|
517
|
+
const availablePlaygroundPort = await findAvailablePort(constants_namespaceObject.PLAYGROUND_SERVER_PORT);
|
|
518
|
+
const availableScrcpyPort = await findAvailablePort(constants_namespaceObject.SCRCPY_SERVER_PORT);
|
|
519
|
+
if (availablePlaygroundPort !== constants_namespaceObject.PLAYGROUND_SERVER_PORT) console.log(`\u{26A0}\u{FE0F} Port ${constants_namespaceObject.PLAYGROUND_SERVER_PORT} is busy, using port ${availablePlaygroundPort} instead`);
|
|
520
|
+
if (availableScrcpyPort !== constants_namespaceObject.SCRCPY_SERVER_PORT) console.log(`\u{26A0}\u{FE0F} Port ${constants_namespaceObject.SCRCPY_SERVER_PORT} is busy, using port ${availableScrcpyPort} instead`);
|
|
521
|
+
await Promise.all([
|
|
522
|
+
playgroundServer.launch(availablePlaygroundPort),
|
|
523
|
+
scrcpyServer.launch(availableScrcpyPort)
|
|
524
|
+
]);
|
|
525
|
+
global.scrcpyServerPort = availableScrcpyPort;
|
|
526
|
+
console.log('');
|
|
527
|
+
console.log("\u2728 Midscene Android Playground is ready!");
|
|
528
|
+
console.log(`\u{1F3AE} Playground: http://localhost:${playgroundServer.port}`);
|
|
529
|
+
console.log(`\u{1F4F1} Device: ${selectedDeviceId}`);
|
|
530
|
+
console.log(`\u{1F511} Generated Server ID: ${playgroundServer.id}`);
|
|
531
|
+
console.log('');
|
|
532
|
+
open(`http://localhost:${playgroundServer.port}`);
|
|
533
|
+
} catch (error) {
|
|
534
|
+
console.error('Failed to start servers:', error);
|
|
535
|
+
process.exit(1);
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
main();
|
|
539
|
+
})();
|
|
540
|
+
for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
541
|
+
Object.defineProperty(exports, '__esModule', {
|
|
542
|
+
value: true
|
|
543
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { }
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sqaitech/android-playground",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Android playground for SQAI",
|
|
5
|
+
"main": "./dist/lib/index.js",
|
|
6
|
+
"types": "./dist/types/index.d.ts",
|
|
7
|
+
"module": "./dist/es/index.mjs",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"static",
|
|
11
|
+
"bin",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"bin": {
|
|
15
|
+
"sqai-android-playground": "./bin/sqai-android-playground",
|
|
16
|
+
"@sqaitech/android-playground": "./bin/sqai-android-playground"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@yume-chan/adb": "^1.1.0",
|
|
20
|
+
"@yume-chan/adb-scrcpy": "^1.1.0",
|
|
21
|
+
"@yume-chan/adb-server-node-tcp": "^1.1.0",
|
|
22
|
+
"@yume-chan/scrcpy": "^1.1.0",
|
|
23
|
+
"@yume-chan/stream-extra": "^1.0.0",
|
|
24
|
+
"cors": "^2.8.5",
|
|
25
|
+
"express": "^4.21.2",
|
|
26
|
+
"@inquirer/prompts": "^7.8.6",
|
|
27
|
+
"open": "10.1.0",
|
|
28
|
+
"socket.io": "^4.8.1",
|
|
29
|
+
"@sqaitech/playground": "0.5.0",
|
|
30
|
+
"@sqaitech/core": "0.5.0",
|
|
31
|
+
"@sqaitech/android": "0.5.0",
|
|
32
|
+
"@sqaitech/shared": "0.5.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@rslib/core": "^0.11.2",
|
|
36
|
+
"@types/cors": "^2.8.17",
|
|
37
|
+
"@types/express": "^4.17.21",
|
|
38
|
+
"@types/node": "^18.0.0",
|
|
39
|
+
"typescript": "^5.8.3"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"dev": "npm run build:watch",
|
|
44
|
+
"dev:server": "npm run build && ./bin/playground",
|
|
45
|
+
"build": "rslib build",
|
|
46
|
+
"build:watch": "rslib build --watch"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<!doctype html><html><head><title>Midscene Android Playground</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script defer src="/static/js/lib-react.c74a0742.js"></script><script defer src="/static/js/374.e8fd2f39.js"></script><script defer src="/static/js/index.7dfa4eb9.js"></script><link href="/static/css/index.ea878c95.css" rel="stylesheet"></head><body><div id="root"></div></body></html>
|