@midscene/android-playground 0.14.4-beta-20250416024415.0 → 0.14.4-beta-20250416041002.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/dist/es/index.d.ts +2 -0
- package/dist/es/index.js +439 -0
- package/dist/lib/index.d.ts +2 -0
- package/dist/lib/index.js +460 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +4 -4
- package/static/index.html +1 -1
- package/static/static/js/{index.6a9903a5.js → index.809dee96.js} +9 -9
- package/static/static/js/{index.6a9903a5.js.map → index.809dee96.js.map} +1 -1
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { AndroidAgent, AndroidDevice } from "@midscene/android";
|
|
4
|
+
import {
|
|
5
|
+
PLAYGROUND_SERVER_PORT,
|
|
6
|
+
SCRCPY_SERVER_PORT as SCRCPY_SERVER_PORT2
|
|
7
|
+
} from "@midscene/shared/constants";
|
|
8
|
+
import PlaygroundServer from "@midscene/web/midscene-server";
|
|
9
|
+
|
|
10
|
+
// src/scrcpy-server.ts
|
|
11
|
+
import { exec } from "child_process";
|
|
12
|
+
import { createReadStream } from "fs";
|
|
13
|
+
import { createServer } from "http";
|
|
14
|
+
import { promisify } from "util";
|
|
15
|
+
import { SCRCPY_SERVER_PORT } from "@midscene/shared/constants";
|
|
16
|
+
import { getDebug } from "@midscene/shared/logger";
|
|
17
|
+
import cors from "cors";
|
|
18
|
+
import express from "express";
|
|
19
|
+
import { Server } from "socket.io";
|
|
20
|
+
var debugPage = getDebug("android:playground");
|
|
21
|
+
var promiseExec = promisify(exec);
|
|
22
|
+
var ScrcpyServer = class {
|
|
23
|
+
// use for comparing changes
|
|
24
|
+
constructor() {
|
|
25
|
+
this.defaultPort = SCRCPY_SERVER_PORT;
|
|
26
|
+
this.adbClient = null;
|
|
27
|
+
this.currentDeviceId = null;
|
|
28
|
+
this.devicePollInterval = null;
|
|
29
|
+
this.lastDeviceList = "";
|
|
30
|
+
this.app = express();
|
|
31
|
+
this.httpServer = createServer(this.app);
|
|
32
|
+
this.io = new Server(this.httpServer, {
|
|
33
|
+
cors: {
|
|
34
|
+
origin: [
|
|
35
|
+
/^http:\/\/localhost(:\d+)?$/,
|
|
36
|
+
/^http:\/\/127\.0\.0\.1(:\d+)?$/
|
|
37
|
+
],
|
|
38
|
+
methods: ["GET", "POST"],
|
|
39
|
+
credentials: true
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.app.use(
|
|
43
|
+
cors({
|
|
44
|
+
origin: "*",
|
|
45
|
+
credentials: true
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
this.setupSocketHandlers();
|
|
49
|
+
this.setupApiRoutes();
|
|
50
|
+
}
|
|
51
|
+
// setup API routes
|
|
52
|
+
setupApiRoutes() {
|
|
53
|
+
this.app.get("/api/devices", async (req, res) => {
|
|
54
|
+
try {
|
|
55
|
+
const devices = await this.getDevicesList();
|
|
56
|
+
res.json({ devices, currentDeviceId: this.currentDeviceId });
|
|
57
|
+
} catch (error) {
|
|
58
|
+
res.status(500).json({ error: error.message || "Failed to get devices list" });
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// get devices list
|
|
63
|
+
async getDevicesList() {
|
|
64
|
+
try {
|
|
65
|
+
debugPage("start to get devices list");
|
|
66
|
+
const client = await this.getAdbClient();
|
|
67
|
+
if (!client) {
|
|
68
|
+
console.warn("failed to get adb client");
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
debugPage("success to get adb client, start to request devices list");
|
|
72
|
+
let devices;
|
|
73
|
+
try {
|
|
74
|
+
devices = await client.getDevices();
|
|
75
|
+
debugPage("original devices list:", devices);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("failed to get devices list:", error);
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
if (!devices || devices.length === 0) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const formattedDevices = devices.map((device) => {
|
|
84
|
+
const result = {
|
|
85
|
+
id: device.serial,
|
|
86
|
+
name: device.product || device.model || device.serial,
|
|
87
|
+
status: device.state || "device"
|
|
88
|
+
};
|
|
89
|
+
return result;
|
|
90
|
+
});
|
|
91
|
+
return formattedDevices;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error("failed to get devices list:", error);
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// get adb client
|
|
98
|
+
async getAdbClient() {
|
|
99
|
+
const { AdbServerClient } = await import("@yume-chan/adb");
|
|
100
|
+
const { AdbServerNodeTcpConnector } = await import("@yume-chan/adb-server-node-tcp");
|
|
101
|
+
try {
|
|
102
|
+
if (!this.adbClient) {
|
|
103
|
+
await promiseExec("adb start-server");
|
|
104
|
+
debugPage("adb server started");
|
|
105
|
+
debugPage("initialize adb client");
|
|
106
|
+
this.adbClient = new AdbServerClient(
|
|
107
|
+
new AdbServerNodeTcpConnector({
|
|
108
|
+
host: "localhost",
|
|
109
|
+
port: 5037
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
await debugPage("success to initialize adb client");
|
|
113
|
+
} else {
|
|
114
|
+
debugPage("use existing adb client");
|
|
115
|
+
}
|
|
116
|
+
return this.adbClient;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error("failed to get adb client:", error);
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// get adb object
|
|
123
|
+
async getAdb(deviceId) {
|
|
124
|
+
const { Adb } = await import("@yume-chan/adb");
|
|
125
|
+
try {
|
|
126
|
+
const client = await this.getAdbClient();
|
|
127
|
+
if (!client) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
if (deviceId) {
|
|
131
|
+
this.currentDeviceId = deviceId;
|
|
132
|
+
return new Adb(await client.createTransport({ serial: deviceId }));
|
|
133
|
+
}
|
|
134
|
+
const devices = await client.getDevices();
|
|
135
|
+
if (devices.length === 0) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
this.currentDeviceId = devices[0].serial;
|
|
139
|
+
return new Adb(await client.createTransport(devices[0]));
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error("failed to get adb client:", error);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// start scrcpy
|
|
146
|
+
async startScrcpy(adb, options = {}) {
|
|
147
|
+
const { AdbScrcpyClient, AdbScrcpyOptions2_1 } = await import("@yume-chan/adb-scrcpy");
|
|
148
|
+
const { ReadableStream } = await import("@yume-chan/stream-extra");
|
|
149
|
+
const { ScrcpyOptions3_1, DefaultServerPath } = await import("@yume-chan/scrcpy");
|
|
150
|
+
const { BIN } = await import("@yume-chan/fetch-scrcpy-server");
|
|
151
|
+
try {
|
|
152
|
+
await AdbScrcpyClient.pushServer(
|
|
153
|
+
adb,
|
|
154
|
+
ReadableStream.from(createReadStream(BIN))
|
|
155
|
+
);
|
|
156
|
+
const scrcpyOptions = new ScrcpyOptions3_1({
|
|
157
|
+
// default options
|
|
158
|
+
audio: false,
|
|
159
|
+
control: true,
|
|
160
|
+
maxSize: 1024,
|
|
161
|
+
// use videoBitRate as property name
|
|
162
|
+
videoBitRate: 2e6,
|
|
163
|
+
// override default values with user provided options
|
|
164
|
+
...options
|
|
165
|
+
});
|
|
166
|
+
return await AdbScrcpyClient.start(
|
|
167
|
+
adb,
|
|
168
|
+
DefaultServerPath,
|
|
169
|
+
new AdbScrcpyOptions2_1(scrcpyOptions)
|
|
170
|
+
);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.error("failed to start scrcpy:", error);
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// setup Socket.IO connection handlers
|
|
177
|
+
setupSocketHandlers() {
|
|
178
|
+
this.io.on("connection", async (socket) => {
|
|
179
|
+
debugPage(
|
|
180
|
+
"client connected, id: %s, client address: %s",
|
|
181
|
+
socket.id,
|
|
182
|
+
socket.handshake.address
|
|
183
|
+
);
|
|
184
|
+
let scrcpyClient = null;
|
|
185
|
+
let adb = null;
|
|
186
|
+
const sendDevicesList = async () => {
|
|
187
|
+
try {
|
|
188
|
+
debugPage("Socket request to get devices list");
|
|
189
|
+
const devices = await this.getDevicesList();
|
|
190
|
+
debugPage("send devices list to client:", devices);
|
|
191
|
+
socket.emit("devices-list", {
|
|
192
|
+
devices,
|
|
193
|
+
currentDeviceId: this.currentDeviceId
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error("failed to send devices list:", error);
|
|
197
|
+
socket.emit("error", { message: "failed to get devices list" });
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
await sendDevicesList();
|
|
201
|
+
socket.on("get-devices", async () => {
|
|
202
|
+
debugPage("received client request to get devices list");
|
|
203
|
+
await sendDevicesList();
|
|
204
|
+
});
|
|
205
|
+
socket.on("switch-device", async (deviceId) => {
|
|
206
|
+
debugPage("received client request to switch device:", deviceId);
|
|
207
|
+
try {
|
|
208
|
+
if (scrcpyClient) {
|
|
209
|
+
await scrcpyClient.close();
|
|
210
|
+
scrcpyClient = null;
|
|
211
|
+
}
|
|
212
|
+
this.currentDeviceId = deviceId;
|
|
213
|
+
debugPage("device switched to:", deviceId);
|
|
214
|
+
socket.emit("device-switched", { deviceId });
|
|
215
|
+
this.io.emit("global-device-switched", {
|
|
216
|
+
deviceId,
|
|
217
|
+
timestamp: Date.now()
|
|
218
|
+
});
|
|
219
|
+
} catch (error) {
|
|
220
|
+
console.error("failed to switch device:", error);
|
|
221
|
+
socket.emit("error", {
|
|
222
|
+
message: `Failed to switch device: ${error?.message || "Unknown error"}`
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
socket.on("connect-device", async (options) => {
|
|
227
|
+
const { ScrcpyVideoCodecId } = await import("@yume-chan/scrcpy");
|
|
228
|
+
try {
|
|
229
|
+
debugPage(
|
|
230
|
+
"received device connection request, options: %s, client id: %s",
|
|
231
|
+
options,
|
|
232
|
+
socket.id
|
|
233
|
+
);
|
|
234
|
+
adb = await this.getAdb(this.currentDeviceId || void 0);
|
|
235
|
+
if (!adb) {
|
|
236
|
+
console.error("no available device found");
|
|
237
|
+
socket.emit("error", { message: "No device found" });
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
debugPage(
|
|
241
|
+
"starting scrcpy service, device id: %s",
|
|
242
|
+
this.currentDeviceId
|
|
243
|
+
);
|
|
244
|
+
scrcpyClient = await this.startScrcpy(adb, options);
|
|
245
|
+
debugPage("scrcpy service started successfully");
|
|
246
|
+
debugPage(
|
|
247
|
+
"check scrcpyClient object structure: %s",
|
|
248
|
+
Object.getOwnPropertyNames(scrcpyClient).map((name) => {
|
|
249
|
+
const type = typeof scrcpyClient[name];
|
|
250
|
+
const isPromise = type === "object" && scrcpyClient[name] && typeof scrcpyClient[name].then === "function";
|
|
251
|
+
return `${name}: ${type}${isPromise ? " (Promise)" : ""}`;
|
|
252
|
+
})
|
|
253
|
+
);
|
|
254
|
+
try {
|
|
255
|
+
if (scrcpyClient.videoStream) {
|
|
256
|
+
debugPage(
|
|
257
|
+
"videoStream exists, type: %s",
|
|
258
|
+
typeof scrcpyClient.videoStream
|
|
259
|
+
);
|
|
260
|
+
let videoStream;
|
|
261
|
+
if (typeof scrcpyClient.videoStream === "object" && typeof scrcpyClient.videoStream.then === "function") {
|
|
262
|
+
debugPage(
|
|
263
|
+
"videoStream is a Promise, waiting for resolution..."
|
|
264
|
+
);
|
|
265
|
+
videoStream = await scrcpyClient.videoStream;
|
|
266
|
+
} else {
|
|
267
|
+
debugPage("videoStream is not a Promise, directly use");
|
|
268
|
+
videoStream = scrcpyClient.videoStream;
|
|
269
|
+
}
|
|
270
|
+
debugPage(
|
|
271
|
+
"video stream fetched successfully, metadata: %s",
|
|
272
|
+
videoStream.metadata
|
|
273
|
+
);
|
|
274
|
+
const metadata = videoStream.metadata || {};
|
|
275
|
+
debugPage("original metadata: %s", metadata);
|
|
276
|
+
if (!metadata.codec) {
|
|
277
|
+
debugPage(
|
|
278
|
+
"metadata does not have codec field, use H264 by default"
|
|
279
|
+
);
|
|
280
|
+
metadata.codec = ScrcpyVideoCodecId.H264;
|
|
281
|
+
}
|
|
282
|
+
if (!metadata.width || !metadata.height) {
|
|
283
|
+
debugPage(
|
|
284
|
+
"metadata does not have width or height field, use default values"
|
|
285
|
+
);
|
|
286
|
+
metadata.width = metadata.width || 1080;
|
|
287
|
+
metadata.height = metadata.height || 1920;
|
|
288
|
+
}
|
|
289
|
+
debugPage(
|
|
290
|
+
"prepare to send video-metadata event to client, data: %s",
|
|
291
|
+
JSON.stringify(metadata)
|
|
292
|
+
);
|
|
293
|
+
socket.emit("video-metadata", metadata);
|
|
294
|
+
debugPage(
|
|
295
|
+
"video-metadata event sent to client, id: %s",
|
|
296
|
+
socket.id
|
|
297
|
+
);
|
|
298
|
+
const { stream } = videoStream;
|
|
299
|
+
const reader = stream.getReader();
|
|
300
|
+
const processStream = async () => {
|
|
301
|
+
try {
|
|
302
|
+
while (true) {
|
|
303
|
+
const { done, value } = await reader.read();
|
|
304
|
+
if (done)
|
|
305
|
+
break;
|
|
306
|
+
const frameType = value.type || "data";
|
|
307
|
+
socket.emit("video-data", {
|
|
308
|
+
data: Array.from(value.data),
|
|
309
|
+
type: frameType,
|
|
310
|
+
timestamp: Date.now(),
|
|
311
|
+
// fix keyframe access
|
|
312
|
+
keyFrame: value.keyFrame
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
} catch (error) {
|
|
316
|
+
console.error("error processing video stream:", error);
|
|
317
|
+
socket.emit("error", {
|
|
318
|
+
message: "video stream processing error"
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
processStream();
|
|
323
|
+
} else {
|
|
324
|
+
console.error(
|
|
325
|
+
"scrcpyClient object does not have videoStream property"
|
|
326
|
+
);
|
|
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 (scrcpyClient?.controller) {
|
|
338
|
+
socket.emit("control-ready");
|
|
339
|
+
}
|
|
340
|
+
} catch (error) {
|
|
341
|
+
console.error("failed to connect device:", error);
|
|
342
|
+
socket.emit("error", {
|
|
343
|
+
message: `Failed to connect device: ${error?.message || "Unknown error"}`
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
socket.on("disconnect", async (reason) => {
|
|
348
|
+
debugPage("client disconnected, id: %s, reason: %s", socket.id, reason);
|
|
349
|
+
if (scrcpyClient) {
|
|
350
|
+
try {
|
|
351
|
+
debugPage("closing scrcpy client");
|
|
352
|
+
await scrcpyClient.close();
|
|
353
|
+
} catch (error) {
|
|
354
|
+
console.error("failed to close scrcpy client:", error);
|
|
355
|
+
}
|
|
356
|
+
scrcpyClient = null;
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
// launch server
|
|
362
|
+
async launch(port) {
|
|
363
|
+
this.port = port || this.defaultPort;
|
|
364
|
+
return new Promise((resolve) => {
|
|
365
|
+
this.httpServer.listen(this.port, () => {
|
|
366
|
+
console.log(`Scrcpy server running at: http://localhost:${this.port}`);
|
|
367
|
+
this.startDeviceMonitoring();
|
|
368
|
+
resolve(this);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
// start device monitoring
|
|
373
|
+
startDeviceMonitoring() {
|
|
374
|
+
this.devicePollInterval = setInterval(async () => {
|
|
375
|
+
try {
|
|
376
|
+
const devices = await this.getDevicesList();
|
|
377
|
+
const currentDevicesJson = JSON.stringify(devices);
|
|
378
|
+
if (this.lastDeviceList !== currentDevicesJson) {
|
|
379
|
+
debugPage("devices list changed, push to all connected clients");
|
|
380
|
+
this.lastDeviceList = currentDevicesJson;
|
|
381
|
+
if (!this.currentDeviceId && devices.length > 0) {
|
|
382
|
+
const onlineDevices = devices.filter(
|
|
383
|
+
(device) => device.status.toLowerCase() === "device"
|
|
384
|
+
);
|
|
385
|
+
if (onlineDevices.length > 0) {
|
|
386
|
+
this.currentDeviceId = onlineDevices[0].id;
|
|
387
|
+
debugPage(
|
|
388
|
+
"auto select the first online device:",
|
|
389
|
+
this.currentDeviceId
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
this.io.emit("devices-list", {
|
|
394
|
+
devices,
|
|
395
|
+
currentDeviceId: this.currentDeviceId
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
} catch (error) {
|
|
399
|
+
console.error("device monitoring error:", error);
|
|
400
|
+
}
|
|
401
|
+
}, 3e3);
|
|
402
|
+
}
|
|
403
|
+
// close server
|
|
404
|
+
close() {
|
|
405
|
+
if (this.devicePollInterval) {
|
|
406
|
+
clearInterval(this.devicePollInterval);
|
|
407
|
+
this.devicePollInterval = null;
|
|
408
|
+
}
|
|
409
|
+
if (this.httpServer) {
|
|
410
|
+
return this.httpServer.close();
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
// src/index.ts
|
|
416
|
+
var staticDir = path.join(__dirname, "../../static");
|
|
417
|
+
var playgroundServer = new PlaygroundServer(
|
|
418
|
+
AndroidDevice,
|
|
419
|
+
AndroidAgent,
|
|
420
|
+
staticDir
|
|
421
|
+
);
|
|
422
|
+
var scrcpyServer = new ScrcpyServer();
|
|
423
|
+
var main = async () => {
|
|
424
|
+
const { default: open } = await import("open");
|
|
425
|
+
try {
|
|
426
|
+
await Promise.all([
|
|
427
|
+
playgroundServer.launch(PLAYGROUND_SERVER_PORT),
|
|
428
|
+
scrcpyServer.launch(SCRCPY_SERVER_PORT2)
|
|
429
|
+
]);
|
|
430
|
+
console.log(
|
|
431
|
+
`Midscene playground server is running on http://localhost:${playgroundServer.port}`
|
|
432
|
+
);
|
|
433
|
+
open(`http://localhost:${playgroundServer.port}`);
|
|
434
|
+
} catch (error) {
|
|
435
|
+
console.error("Failed to start servers:", error);
|
|
436
|
+
process.exit(1);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
main();
|