Skip to content

Commit 2a97785

Browse files
committed
feat: enhance error handling in Scrcpy class and update process exit logging
1 parent e6aed84 commit 2a97785

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"internalConsoleOptions": "openOnSessionStart",
3434
"skipFiles": ["<node_internals>/**"],
3535
"env": {
36+
"DEBUG__ALL": "*",
3637
"DEBUG": "adb:minicap, adb:scrcpy",
3738
"DEBUG_COLORS": "1"
3839
}

src/adb/thirdparty/scrcpy/Scrcpy.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,14 @@ export default class Scrcpy extends EventEmitter {
221221
}
222222
}
223223

224-
private _setFatalError(msg: string) {
224+
private _setFatalError(msg: string | Error) {
225+
// console.error(`_setFatalError. Scrcpy fatal error:`, msg);
225226
if (this.setFatalError) {
226-
this.setFatalError(msg);
227+
if (msg instanceof Error) {
228+
this.setFatalError(msg.message + '\n' + msg.stack);
229+
} else {
230+
this.setFatalError(msg);
231+
}
227232
this.setFatalError = undefined;
228233
}
229234
}
@@ -598,6 +603,7 @@ export default class Scrcpy extends EventEmitter {
598603
let codec = "H264";
599604
// let header: Uint8Array | undefined;
600605
if (this.major >= 2) {
606+
await Utils.waitforReadable(this.videoSocket, 0, 'videoSocket Codec header');
601607
const frameMeta = this.videoSocket.stream.read(12) as Buffer;
602608
const codecId = frameMeta.readUInt32BE(0);
603609
// Read width (4 bytes)
@@ -638,6 +644,8 @@ export default class Scrcpy extends EventEmitter {
638644
await Utils.waitforReadable(this.videoSocket, 0, 'videoSocket packet size');
639645
let len: number | undefined = undefined;
640646
if (this.config.sendFrameMeta) {
647+
if (!this.videoSocket)
648+
break;
641649
const frameMeta = this.videoSocket.stream.read(12) as Buffer;
642650
if (!frameMeta) {
643651
// regular end condition

tasks/realTest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,18 @@ const stressTestScrCpy = async (deviceClient: DeviceClient) => {
224224
await Utils.delay(100);
225225
scrcpys.push(scrcpy)
226226
try {
227+
scrcpy.on("error", (e) => {
228+
console.error(`scrcpy #${pass} error`, e);
229+
});
227230
scrcpy.once('frame', (data) => {
228231
console.log(`${pc.magenta('scrcpy')} emit hit first frame isKeyframe: ${data.keyframe} from #${pass}`);
229232
})
230233
scrcpy.once('error', (data) => {
231234
console.log(`${pc.red('scrcpy')} emit hit error: ${data} from #${pass}`);
232235
})
233-
234236
await scrcpy.start();
235237
await scrcpy.firstFrame;
236238
console.log(`${pc.magenta('scrcpy')} Should had emit hit first frame isKeyframe from #${pass}`);
237-
await Utils.delay(500);
238239
} catch (e) {
239240
console.error(`Start Scrcpy PASS:${pass} failed ${(e as Error).message}`);
240241
}
@@ -535,7 +536,7 @@ const main = async () => {
535536
// await stressMinicap(deviceClient);
536537

537538
// process.stdin.resume();
538-
await stressTestScrCpy(deviceClient);
539+
// await stressTestScrCpy(deviceClient);
539540

540541
// await testSTFService(deviceClient);
541542
// await testService(deviceClient);
@@ -554,11 +555,10 @@ process.on('unhandledRejection', (reason, promise) => {
554555
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
555556
});
556557

557-
process.on('exit', () => console.log('Processus en cours de fermeture'));
558+
process.on('exit', (code) => console.log('Processus is closing, exit code:', code));
558559
process.on('SIGINT', () => console.log('SIGINT reçu'));
559560
process.on('SIGTERM', () => console.log('SIGTERM reçu'));
560561

561-
562562
main().catch(e => console.error("ERROR", e)).finally(() => {
563563
console.log('Processus terminé');
564564
});

0 commit comments

Comments
 (0)