-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathstreamscreen.js
543 lines (514 loc) · 16.6 KB
/
streamscreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// @ts-check
import {fs, logger, system, util} from '@appium/support';
import {waitForCondition} from 'asyncbox';
import B from 'bluebird';
import _ from 'lodash';
import {spawn} from 'node:child_process';
import http from 'node:http';
import net from 'node:net';
import url from 'node:url';
import {checkPortStatus} from 'portscanner';
import {SubProcess, exec} from 'teen_process';
const RECORDING_INTERVAL_SEC = 5;
const STREAMING_STARTUP_TIMEOUT_MS = 5000;
const GSTREAMER_BINARY = `gst-launch-1.0${system.isWindows() ? '.exe' : ''}`;
const GST_INSPECT_BINARY = `gst-inspect-1.0${system.isWindows() ? '.exe' : ''}`;
const REQUIRED_GST_PLUGINS = {
avdec_h264: 'gst-libav',
h264parse: 'gst-plugins-bad',
jpegenc: 'gst-plugins-good',
tcpserversink: 'gst-plugins-base',
multipartmux: 'gst-plugins-good',
};
const SCREENRECORD_BINARY = 'screenrecord';
const GST_TUTORIAL_URL = 'https://gstreamer.freedesktop.org/documentation/installing/index.html';
const DEFAULT_HOST = '127.0.0.1';
const TCP_HOST = '127.0.0.1';
const DEFAULT_PORT = 8093;
const DEFAULT_QUALITY = 70;
const DEFAULT_BITRATE = 4000000; // 4 Mbps
const BOUNDARY_STRING = '--2ae9746887f170b8cf7c271047ce314c';
const ADB_SCREEN_STREAMING_FEATURE = 'adb_screen_streaming';
/**
* @this {import('../driver').AndroidDriver}
* @param {number} [width] The scaled width of the device's screen.
* If unset then the script will assign it to the actual screen width measured
* in pixels.
* @param {number} [height] The scaled height of the device's screen.
* If unset then the script will assign it to the actual screen height
* measured in pixels.
* @param {number} [bitRate=4000000] The video bit rate for the video, in bits per second.
* The default value is 4 Mb/s. You can increase the bit rate to improve video
* quality, but doing so results in larger movie files.
* @param {string} [host='127.0.0.1'] The IP address/host name to start the MJPEG server on.
* You can set it to `0.0.0.0` to trigger the broadcast on all available
* network interfaces.
* @param {number} [port=8093] The port number to start the MJPEG server on.
* @param {number} [tcpPort=8094] The port number to start the internal TCP MJPEG broadcast on.
* @param {string} [pathname] The HTTP request path the MJPEG server should be available on.
* If unset, then any pathname on the given `host`/`port` combination will
* work. Note that the value should always start with a single slash: `/`
* @param {number} [quality=70] The quality value for the streamed JPEG images.
* This number should be in range `[1,100]`, where `100` is the best quality.
* @param {boolean} [considerRotation=false] If set to `true` then GStreamer pipeline will increase the dimensions of
* the resulting images to properly fit images in both landscape and portrait
* orientations.
* Set it to `true` if the device rotation is not going to be the same during
* the broadcasting session.
* @param {boolean} [logPipelineDetails=false] Whether to log GStreamer pipeline events into the standard log output.
* Might be useful for debugging purposes.
* @returns {Promise<void>}
*/
export async function mobileStartScreenStreaming(
width,
height,
bitRate,
host = DEFAULT_HOST,
port = DEFAULT_PORT,
pathname,
tcpPort = DEFAULT_PORT + 1,
quality = DEFAULT_QUALITY,
considerRotation = false,
logPipelineDetails = false,
) {
this.assertFeatureEnabled(ADB_SCREEN_STREAMING_FEATURE);
if (_.isUndefined(this._screenStreamingProps)) {
await verifyStreamingRequirements(this.adb);
}
if (!_.isEmpty(this._screenStreamingProps)) {
this.log.info(
`The screen streaming session is already running. ` +
`Stop it first in order to start a new one.`,
);
return;
}
if ((await checkPortStatus(port, host)) === 'open') {
this.log.info(
`The port #${port} at ${host} is busy. ` + `Assuming the screen streaming is already running`,
);
return;
}
if ((await checkPortStatus(tcpPort, TCP_HOST)) === 'open') {
throw this.log.errorWithException(
`The port #${tcpPort} at ${TCP_HOST} is busy. ` +
`Make sure there are no leftovers from previous sessions.`,
);
}
this._screenStreamingProps = undefined;
const deviceInfo = await getDeviceInfo(this.adb, this.log);
const deviceStreamingProc = await initDeviceStreamingProc(this.adb, this.log, deviceInfo, {
width,
height,
bitRate,
});
let gstreamerPipeline;
try {
gstreamerPipeline = await initGstreamerPipeline(deviceStreamingProc, deviceInfo, this.log, {
width,
height,
quality,
tcpPort,
considerRotation,
logPipelineDetails,
});
} catch (e) {
if (deviceStreamingProc.kill(0)) {
deviceStreamingProc.kill();
}
throw e;
}
/** @type {import('node:net').Socket|undefined} */
let mjpegSocket;
/** @type {import('node:http').Server|undefined} */
let mjpegServer;
try {
await new B((resolve, reject) => {
mjpegSocket = net.createConnection(tcpPort, TCP_HOST, () => {
this.log.info(`Successfully connected to MJPEG stream at tcp://${TCP_HOST}:${tcpPort}`);
mjpegServer = http.createServer((req, res) => {
const remoteAddress = extractRemoteAddress(req);
const currentPathname = url.parse(String(req.url)).pathname;
this.log.info(
`Got an incoming screen broadcasting request from ${remoteAddress} ` +
`(${req.headers['user-agent'] || 'User Agent unknown'}) at ${currentPathname}`,
);
if (pathname && currentPathname !== pathname) {
this.log.info(
'Rejecting the broadcast request since it does not match the given pathname',
);
res.writeHead(404, {
Connection: 'close',
'Content-Type': 'text/plain; charset=utf-8',
});
res.write(`'${currentPathname}' did not match any known endpoints`);
res.end();
return;
}
this.log.info('Starting MJPEG broadcast');
res.writeHead(200, {
'Cache-Control':
'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0',
Pragma: 'no-cache',
Connection: 'close',
'Content-Type': `multipart/x-mixed-replace; boundary=${BOUNDARY_STRING}`,
});
/** @type {import('node:net').Socket} */ (mjpegSocket).pipe(res);
});
mjpegServer.on('error', (e) => {
this.log.warn(e);
reject(e);
});
mjpegServer.on('close', () => {
this.log.debug(`MJPEG server at http://${host}:${port} has been closed`);
});
mjpegServer.on('listening', () => {
this.log.info(`Successfully started MJPEG server at http://${host}:${port}`);
resolve();
});
mjpegServer.listen(port, host);
});
mjpegSocket.on('error', (e) => {
this.log.error(e);
reject(e);
});
}).timeout(
STREAMING_STARTUP_TIMEOUT_MS,
`Cannot connect to the streaming server within ${STREAMING_STARTUP_TIMEOUT_MS}ms`,
);
} catch (e) {
if (deviceStreamingProc.kill(0)) {
deviceStreamingProc.kill();
}
if (gstreamerPipeline.isRunning) {
await gstreamerPipeline.stop();
}
if (mjpegSocket) {
mjpegSocket.destroy();
}
if (mjpegServer && mjpegServer.listening) {
mjpegServer.close();
}
throw e;
}
this._screenStreamingProps = {
deviceStreamingProc,
gstreamerPipeline,
mjpegSocket,
mjpegServer,
};
}
/**
* @this {import('../driver').AndroidDriver}
* @returns {Promise<void>}
*/
export async function mobileStopScreenStreaming() {
if (_.isEmpty(this._screenStreamingProps)) {
if (!_.isUndefined(this._screenStreamingProps)) {
this.log.debug(`Screen streaming is not running. There is nothing to stop`);
}
return;
}
const {deviceStreamingProc, gstreamerPipeline, mjpegSocket, mjpegServer} =
this._screenStreamingProps;
try {
mjpegSocket.end();
if (mjpegServer.listening) {
mjpegServer.close();
}
if (deviceStreamingProc.kill(0)) {
deviceStreamingProc.kill('SIGINT');
}
if (gstreamerPipeline.isRunning) {
try {
await gstreamerPipeline.stop('SIGINT');
} catch (e) {
this.log.warn(e);
try {
await gstreamerPipeline.stop('SIGKILL');
} catch (e1) {
this.log.error(e1);
}
}
}
this.log.info(`Successfully terminated the screen streaming MJPEG server`);
} finally {
this._screenStreamingProps = undefined;
}
}
// #region Internal helpers
/**
*
* @param {string} streamName
* @param {string} udid
* @returns {AppiumLogger}
*/
function createStreamingLogger(streamName, udid) {
return logger.getLogger(
`${streamName}@` +
_.truncate(udid, {
length: 8,
omission: '',
}),
);
}
/**
*
* @param {ADB} adb
*/
async function verifyStreamingRequirements(adb) {
if (!_.trim(await adb.shell(['which', SCREENRECORD_BINARY]))) {
throw new Error(
`The required '${SCREENRECORD_BINARY}' binary is not available on the device under test`,
);
}
const gstreamerCheckPromises = [];
for (const binaryName of [GSTREAMER_BINARY, GST_INSPECT_BINARY]) {
gstreamerCheckPromises.push(
(async () => {
try {
await fs.which(binaryName);
} catch {
throw new Error(
`The '${binaryName}' binary is not available in the PATH on the host system. ` +
`See ${GST_TUTORIAL_URL} for more details on how to install it.`,
);
}
})(),
);
}
await B.all(gstreamerCheckPromises);
const moduleCheckPromises = [];
for (const [name, modName] of _.toPairs(REQUIRED_GST_PLUGINS)) {
moduleCheckPromises.push(
(async () => {
const {stdout} = await exec(GST_INSPECT_BINARY, [name]);
if (!_.includes(stdout, modName)) {
throw new Error(
`The required GStreamer plugin '${name}' from '${modName}' module is not installed. ` +
`See ${GST_TUTORIAL_URL} for more details on how to install it.`,
);
}
})(),
);
}
await B.all(moduleCheckPromises);
}
const deviceInfoRegexes = /** @type {const} */ ([
['width', /\bdeviceWidth=(\d+)/],
['height', /\bdeviceHeight=(\d+)/],
['fps', /\bfps=(\d+)/],
]);
/**
*
* @param {ADB} adb
* @param {AppiumLogger} [log]
*/
async function getDeviceInfo(adb, log) {
const output = await adb.shell(['dumpsys', 'display']);
/**
* @type {DeviceInfo}
*/
const result = {};
for (const [key, pattern] of deviceInfoRegexes) {
const match = pattern.exec(output);
if (!match) {
log?.debug(output);
throw new Error(
`Cannot parse the device ${key} from the adb command output. ` +
`Check the server log for more details.`,
);
}
result[key] = parseInt(match[1], 10);
}
result.udid = String(adb.curDeviceId);
return result;
}
/**
*
* @param {ADB} adb
* @param {AppiumLogger} log
* @param {DeviceInfo} deviceInfo
* @param {{width?: string|number, height?: string|number, bitRate?: string|number}} opts
* @returns
*/
async function initDeviceStreamingProc(adb, log, deviceInfo, opts = {}) {
const {width, height, bitRate} = opts;
const adjustedWidth = _.isUndefined(width) ? deviceInfo.width : parseInt(String(width), 10);
const adjustedHeight = _.isUndefined(height) ? deviceInfo.height : parseInt(String(height), 10);
const adjustedBitrate = _.isUndefined(bitRate) ? DEFAULT_BITRATE : parseInt(String(bitRate), 10);
let screenRecordCmd =
SCREENRECORD_BINARY +
` --output-format=h264` +
// 5 seconds is fine to detect rotation changes
` --time-limit=${RECORDING_INTERVAL_SEC}`;
if (width || height) {
screenRecordCmd += ` --size=${adjustedWidth}x${adjustedHeight}`;
}
if (bitRate) {
screenRecordCmd += ` --bit-rate=${adjustedBitrate}`;
}
const adbArgs = [
...adb.executable.defaultArgs,
'exec-out',
// The loop is required, because by default the maximum record duration
// for screenrecord is always limited
`while true; do ${screenRecordCmd} -; done`,
];
const deviceStreaming = spawn(adb.executable.path, adbArgs);
deviceStreaming.on('exit', (code, signal) => {
log.debug(`Device streaming process exited with code ${code}, signal ${signal}`);
});
let isStarted = false;
const deviceStreamingLogger = createStreamingLogger(SCREENRECORD_BINARY, deviceInfo.udid);
/**
*
* @param {Buffer|string} chunk
*/
const errorsListener = (chunk) => {
const stderr = chunk.toString();
if (_.trim(stderr)) {
deviceStreamingLogger.debug(stderr);
}
};
deviceStreaming.stderr.on('data', errorsListener);
/**
*
* @param {Buffer|string} chunk
*/
const startupListener = (chunk) => {
if (!isStarted) {
isStarted = !_.isEmpty(chunk);
}
};
deviceStreaming.stdout.on('data', startupListener);
try {
log.info(`Starting device streaming: ${util.quote([adb.executable.path, ...adbArgs])}`);
await waitForCondition(() => isStarted, {
waitMs: STREAMING_STARTUP_TIMEOUT_MS,
intervalMs: 300,
});
} catch (e) {
throw log.errorWithException(
`Cannot start the screen streaming process. Original error: ${
/** @type {Error} */ (e).message
}`,
);
} finally {
deviceStreaming.stderr.removeListener('data', errorsListener);
deviceStreaming.stdout.removeListener('data', startupListener);
}
return deviceStreaming;
}
/**
*
* @param {import('node:child_process').ChildProcess} deviceStreamingProc
* @param {DeviceInfo} deviceInfo
* @param {AppiumLogger} log
* @param {import('./types').InitGStreamerPipelineOpts} opts
*/
async function initGstreamerPipeline(deviceStreamingProc, deviceInfo, log, opts) {
const {width, height, quality, tcpPort, considerRotation, logPipelineDetails} = opts;
const adjustedWidth = parseInt(String(width), 10) || deviceInfo.width;
const adjustedHeight = parseInt(String(height), 10) || deviceInfo.height;
const gstreamerPipeline = new SubProcess(
GSTREAMER_BINARY,
[
'-v',
'fdsrc',
'fd=0',
'!',
'video/x-h264,' +
`width=${considerRotation ? Math.max(adjustedWidth, adjustedHeight) : adjustedWidth},` +
`height=${considerRotation ? Math.max(adjustedWidth, adjustedHeight) : adjustedHeight},` +
`framerate=${deviceInfo.fps}/1,` +
'byte-stream=true',
'!',
'h264parse',
'!',
'queue',
'leaky=downstream',
'!',
'avdec_h264',
'!',
'queue',
'leaky=downstream',
'!',
'jpegenc',
`quality=${quality}`,
'!',
'multipartmux',
`boundary=${BOUNDARY_STRING}`,
'!',
'tcpserversink',
`host=${TCP_HOST}`,
`port=${tcpPort}`,
],
{
stdio: [deviceStreamingProc.stdout, 'pipe', 'pipe'],
},
);
gstreamerPipeline.on('exit', (code, signal) => {
log.debug(`Pipeline streaming process exited with code ${code}, signal ${signal}`);
});
const gstreamerLogger = createStreamingLogger('gst', deviceInfo.udid);
/**
*
* @param {string} stdout
* @param {string} stderr
*/
const gstOutputListener = (stdout, stderr) => {
if (_.trim(stderr || stdout)) {
gstreamerLogger.debug(stderr || stdout);
}
};
gstreamerPipeline.on('output', gstOutputListener);
let didFail = false;
try {
log.info(`Starting GStreamer pipeline: ${gstreamerPipeline.rep}`);
await gstreamerPipeline.start(0);
await waitForCondition(
async () => {
try {
return (await checkPortStatus(tcpPort, TCP_HOST)) === 'open';
} catch {
return false;
}
},
{
waitMs: STREAMING_STARTUP_TIMEOUT_MS,
intervalMs: 300,
},
);
} catch (e) {
didFail = true;
throw log.errorWithException(
`Cannot start the screen streaming pipeline. Original error: ${
/** @type {Error} */ (e).message
}`,
);
} finally {
if (!logPipelineDetails || didFail) {
gstreamerPipeline.removeListener('output', gstOutputListener);
}
}
return gstreamerPipeline;
}
/**
* @param {import('node:http').IncomingMessage} req
* @privateRemarks This may need to be future-proofed, as `IncomingMessage.connection` is deprecated and its `socket` prop is likely private
*/
function extractRemoteAddress(req) {
return (
req.headers['x-forwarded-for'] ||
req.socket.remoteAddress ||
req.connection.remoteAddress ||
// @ts-expect-error socket may be a private API??
req.connection.socket.remoteAddress
);
}
// #endregion
/**
* @typedef {import('appium-adb').ADB} ADB
* @typedef {import('@appium/types').AppiumLogger} AppiumLogger
* @typedef {import('./types').DeviceInfo} DeviceInfo
*/