@aivin-labs/cli 1.0.42 → 1.0.43
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/bin/lib/remote/client.mjs +29 -16
- package/bin/lib/remote/desktop/castInput.mjs +113 -111
- package/bin/lib/remote/desktop/desktopCast.mjs +417 -337
- package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +362 -0
- package/bin/lib/remote/desktop/desktopCastSource.mjs +1091 -960
- package/bin/lib/remote/desktop/desktopWebRtc.mjs +365 -276
- package/bin/lib/remote/desktop/webrtcWorker.mjs +223 -191
- package/bin/lib/remote/mascot/index.mjs +25 -9
- package/package.json +1 -1
|
@@ -1,337 +1,417 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import readline from 'readline';
|
|
4
|
-
import { createHash, randomBytes } from 'crypto';
|
|
5
|
-
import { spawn } from 'child_process';
|
|
6
|
-
import { REMOTE_CONFIG_DIR } from '../constants.mjs';
|
|
7
|
-
import { DESKTOP_CAST_CSHARP, DESKTOP_CAST_PS1 } from './desktopCastSource.mjs';
|
|
8
|
-
|
|
9
|
-
const CAST_DIR = path.join(REMOTE_CONFIG_DIR, 'desktop-cast');
|
|
10
|
-
|
|
11
|
-
function hostFiles(dir = CAST_DIR) {
|
|
12
|
-
const hash = createHash('sha256').update(DESKTOP_CAST_CSHARP).update(DESKTOP_CAST_PS1).digest('hex').slice(0, 16);
|
|
13
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
-
const src = path.join(dir, `cast-${hash}.cs`);
|
|
15
|
-
const ps1 = path.join(dir, `cast-${hash}.ps1`);
|
|
16
|
-
if (!fs.existsSync(src)) fs.writeFileSync(src, DESKTOP_CAST_CSHARP);
|
|
17
|
-
if (!fs.existsSync(ps1)) fs.writeFileSync(ps1, DESKTOP_CAST_PS1);
|
|
18
|
-
for (const name of fs.readdirSync(dir)) {
|
|
19
|
-
if (name.startsWith('cast-') && !name.startsWith(`cast-${hash}`)) fs.rmSync(path.join(dir, name), { force: true });
|
|
20
|
-
}
|
|
21
|
-
return { src, ps1, dll: path.join(dir, `cast-${hash}.dll`) };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const RAW_HEADER_BYTES = 12;
|
|
25
|
-
const MAX_RAW_FRAME_BYTES = 64 * 1024 * 1024;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Tách luồng khung thô từ named pipe:
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
let
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
pos
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
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
|
-
if (
|
|
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
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import readline from 'readline';
|
|
4
|
+
import { createHash, randomBytes } from 'crypto';
|
|
5
|
+
import { spawn } from 'child_process';
|
|
6
|
+
import { REMOTE_CONFIG_DIR } from '../constants.mjs';
|
|
7
|
+
import { DESKTOP_CAST_CSHARP, DESKTOP_CAST_PS1 } from './desktopCastSource.mjs';
|
|
8
|
+
|
|
9
|
+
const CAST_DIR = path.join(REMOTE_CONFIG_DIR, 'desktop-cast');
|
|
10
|
+
|
|
11
|
+
function hostFiles(dir = CAST_DIR) {
|
|
12
|
+
const hash = createHash('sha256').update(DESKTOP_CAST_CSHARP).update(DESKTOP_CAST_PS1).digest('hex').slice(0, 16);
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
const src = path.join(dir, `cast-${hash}.cs`);
|
|
15
|
+
const ps1 = path.join(dir, `cast-${hash}.ps1`);
|
|
16
|
+
if (!fs.existsSync(src)) fs.writeFileSync(src, DESKTOP_CAST_CSHARP);
|
|
17
|
+
if (!fs.existsSync(ps1)) fs.writeFileSync(ps1, DESKTOP_CAST_PS1);
|
|
18
|
+
for (const name of fs.readdirSync(dir)) {
|
|
19
|
+
if (name.startsWith('cast-') && !name.startsWith(`cast-${hash}`)) fs.rmSync(path.join(dir, name), { force: true });
|
|
20
|
+
}
|
|
21
|
+
return { src, ps1, dll: path.join(dir, `cast-${hash}.dll`) };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const RAW_HEADER_BYTES = 12;
|
|
25
|
+
const MAX_RAW_FRAME_BYTES = 64 * 1024 * 1024;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Tách luồng khung thô từ named pipe:
|
|
29
|
+
* - Video: [rộng > 0, cao > 0, độ dài] int32 LE + dữ liệu I420.
|
|
30
|
+
* - Audio: [rộng = -số_kênh < 0, cao = tần_số_lấy_mẫu, độ dài] int32 LE + dữ liệu PCM 16-bit LE.
|
|
31
|
+
* Độ dài vô lý → `onCorrupt` (đóng pipe, không cố đồng bộ lại).
|
|
32
|
+
*/
|
|
33
|
+
export function createRawFrameReader(onFrame, onCorrupt = () => {}, onAudio = () => {}) {
|
|
34
|
+
const header = Buffer.alloc(RAW_HEADER_BYTES);
|
|
35
|
+
let headerFilled = 0;
|
|
36
|
+
let frame = null;
|
|
37
|
+
let filled = 0;
|
|
38
|
+
return (chunk) => {
|
|
39
|
+
let pos = 0;
|
|
40
|
+
while (pos < chunk.length) {
|
|
41
|
+
if (!frame) {
|
|
42
|
+
const n = chunk.copy(header, headerFilled, pos, pos + RAW_HEADER_BYTES - headerFilled);
|
|
43
|
+
headerFilled += n;
|
|
44
|
+
pos += n;
|
|
45
|
+
if (headerFilled < RAW_HEADER_BYTES) return;
|
|
46
|
+
headerFilled = 0;
|
|
47
|
+
const width = header.readInt32LE(0);
|
|
48
|
+
const height = header.readInt32LE(4);
|
|
49
|
+
const length = header.readInt32LE(8);
|
|
50
|
+
if (width < 0) {
|
|
51
|
+
const channelCount = -width;
|
|
52
|
+
const sampleRate = height;
|
|
53
|
+
const bytesPerFrame = channelCount * 2;
|
|
54
|
+
if ((channelCount !== 1 && channelCount !== 2) || sampleRate < 8000 || sampleRate > 96000 || sampleRate % 100 !== 0 || length <= 0 || length > 192000 || length % bytesPerFrame !== 0) {
|
|
55
|
+
onCorrupt();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
frame = { audio: true, sampleRate, channelCount, numberOfFrames: length / bytesPerFrame, data: new Uint8Array(length) };
|
|
59
|
+
filled = 0;
|
|
60
|
+
} else {
|
|
61
|
+
if (width <= 0 || height <= 0 || length !== width * height * 1.5 || length > MAX_RAW_FRAME_BYTES) {
|
|
62
|
+
onCorrupt();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Vùng nhớ riêng cho từng khung (bộ mã hoá giữ tham chiếu), chép thẳng từng khúc: khúc pipe 64 KB, ghép dần cả khung ăn luồng chính ~9 ms.
|
|
66
|
+
frame = { width, height, data: new Uint8ClampedArray(length) };
|
|
67
|
+
filled = 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const piece = chunk.subarray(pos, pos + frame.data.length - filled);
|
|
71
|
+
frame.data.set(piece, filled);
|
|
72
|
+
filled += piece.length;
|
|
73
|
+
pos += piece.length;
|
|
74
|
+
if (filled === frame.data.length) {
|
|
75
|
+
const done = frame;
|
|
76
|
+
frame = null;
|
|
77
|
+
if (done.audio) {
|
|
78
|
+
onAudio({
|
|
79
|
+
samples: new Int16Array(done.data.buffer, done.data.byteOffset, done.data.byteLength / 2),
|
|
80
|
+
sampleRate: done.sampleRate,
|
|
81
|
+
bitsPerSample: 16,
|
|
82
|
+
channelCount: done.channelCount,
|
|
83
|
+
numberOfFrames: done.numberOfFrames,
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
onFrame(done);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function isDesktopCastSupported(platform = process.platform) {
|
|
94
|
+
return platform === 'win32';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Quản lý phiên phát hình ảnh và điều khiển máy tính từ xa.
|
|
99
|
+
*/
|
|
100
|
+
export function createDesktopCastManager({ dir = CAST_DIR, spawnImpl = spawn } = {}) {
|
|
101
|
+
let activeSession = null;
|
|
102
|
+
let prewarming = null;
|
|
103
|
+
|
|
104
|
+
/** Biên dịch sẵn DLL C# ở nền khi daemon khởi động để lần mở phiên đầu tiên không mất ~800 ms chờ csc. */
|
|
105
|
+
function prewarm() {
|
|
106
|
+
if (!isDesktopCastSupported() || prewarming || activeSession) return prewarming;
|
|
107
|
+
let files;
|
|
108
|
+
try { files = hostFiles(dir); } catch { return null; }
|
|
109
|
+
if (fs.existsSync(files.dll)) return null;
|
|
110
|
+
prewarming = new Promise((resolve) => {
|
|
111
|
+
try {
|
|
112
|
+
const proc = spawnImpl('powershell.exe', [
|
|
113
|
+
'-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', files.ps1,
|
|
114
|
+
], {
|
|
115
|
+
env: {
|
|
116
|
+
...process.env,
|
|
117
|
+
AIVIN_CAST_SRC: files.src,
|
|
118
|
+
AIVIN_CAST_DLL: files.dll,
|
|
119
|
+
AIVIN_CAST_COMPILE_ONLY: '1',
|
|
120
|
+
},
|
|
121
|
+
stdio: 'ignore',
|
|
122
|
+
windowsHide: true,
|
|
123
|
+
});
|
|
124
|
+
proc.on('exit', () => { prewarming = null; resolve(true); });
|
|
125
|
+
proc.on('error', () => { prewarming = null; resolve(false); });
|
|
126
|
+
proc.unref?.();
|
|
127
|
+
} catch {
|
|
128
|
+
prewarming = null;
|
|
129
|
+
resolve(false);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return prewarming;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function start({
|
|
136
|
+
sessionId,
|
|
137
|
+
maxFps = 15,
|
|
138
|
+
maxWidth = 1366,
|
|
139
|
+
quality = 65,
|
|
140
|
+
monitor = -1,
|
|
141
|
+
onFrame,
|
|
142
|
+
onMeta,
|
|
143
|
+
onCursor,
|
|
144
|
+
onCursorShape,
|
|
145
|
+
onInputFocus,
|
|
146
|
+
onClipboard,
|
|
147
|
+
onSecureDesktop,
|
|
148
|
+
onCapture,
|
|
149
|
+
rawPipe = false,
|
|
150
|
+
onReady,
|
|
151
|
+
onError,
|
|
152
|
+
onStop,
|
|
153
|
+
} = {}) {
|
|
154
|
+
if (activeSession) {
|
|
155
|
+
stop();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!isDesktopCastSupported()) {
|
|
159
|
+
onError?.(new Error('Desktop cast is currently supported on Windows only'));
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const files = hostFiles(dir);
|
|
164
|
+
// Có nơi nhận khung thô (nguồn video WebRTC ở tiến trình con webrtcWorker.mjs) → tiến trình quay mở named pipe gửi I420; không thì chỉ JPEG.
|
|
165
|
+
const pipeName = rawPipe ? `aivin-cast-${process.pid}-${randomBytes(6).toString('hex')}` : '';
|
|
166
|
+
const argsJson = JSON.stringify({
|
|
167
|
+
max_fps: maxFps,
|
|
168
|
+
max_width: maxWidth,
|
|
169
|
+
quality,
|
|
170
|
+
monitor,
|
|
171
|
+
...(pipeName ? { pipe: pipeName } : {}),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const child = spawnImpl('powershell.exe', [
|
|
175
|
+
'-NoProfile',
|
|
176
|
+
'-NonInteractive',
|
|
177
|
+
'-ExecutionPolicy',
|
|
178
|
+
'Bypass',
|
|
179
|
+
'-File',
|
|
180
|
+
files.ps1,
|
|
181
|
+
], {
|
|
182
|
+
env: {
|
|
183
|
+
...process.env,
|
|
184
|
+
AIVIN_CAST_SRC: files.src,
|
|
185
|
+
AIVIN_CAST_DLL: files.dll,
|
|
186
|
+
AIVIN_CAST_ARGS: argsJson,
|
|
187
|
+
},
|
|
188
|
+
windowsHide: true,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
let rl = readline.createInterface({ input: child.stdout });
|
|
192
|
+
|
|
193
|
+
rl.on('line', (line) => {
|
|
194
|
+
let msg;
|
|
195
|
+
try {
|
|
196
|
+
msg = JSON.parse(line);
|
|
197
|
+
} catch {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (msg.t === 'ready') {
|
|
202
|
+
onReady?.(msg);
|
|
203
|
+
} else if (msg.t === 'frame') {
|
|
204
|
+
onFrame?.({
|
|
205
|
+
sessionId: session.sessionId,
|
|
206
|
+
image: msg.image,
|
|
207
|
+
width: msg.width,
|
|
208
|
+
height: msg.height,
|
|
209
|
+
screenWidth: msg.screenWidth,
|
|
210
|
+
screenHeight: msg.screenHeight,
|
|
211
|
+
screenX: msg.screenX,
|
|
212
|
+
screenY: msg.screenY,
|
|
213
|
+
monitor: msg.monitor,
|
|
214
|
+
screens: msg.screens,
|
|
215
|
+
ts: msg.ts,
|
|
216
|
+
stats: msg.stats,
|
|
217
|
+
...(msg.secureDesktop ? { secureDesktop: true } : {}),
|
|
218
|
+
});
|
|
219
|
+
} else if (msg.t === 'meta') {
|
|
220
|
+
onMeta?.({
|
|
221
|
+
sessionId: session.sessionId,
|
|
222
|
+
width: msg.width,
|
|
223
|
+
height: msg.height,
|
|
224
|
+
screenWidth: msg.screenWidth,
|
|
225
|
+
screenHeight: msg.screenHeight,
|
|
226
|
+
screenX: msg.screenX,
|
|
227
|
+
screenY: msg.screenY,
|
|
228
|
+
monitor: msg.monitor,
|
|
229
|
+
screens: msg.screens,
|
|
230
|
+
ts: msg.ts,
|
|
231
|
+
stats: msg.stats,
|
|
232
|
+
...(msg.secureDesktop ? { secureDesktop: true } : {}),
|
|
233
|
+
});
|
|
234
|
+
} else if (msg.t === 'cursor') {
|
|
235
|
+
onCursor?.({ x: msg.x, y: msg.y, visible: msg.visible, shape: msg.shape });
|
|
236
|
+
} else if (msg.t === 'cursor_shape') {
|
|
237
|
+
onCursorShape?.({ id: msg.id, png: msg.png, hotX: msg.hotX, hotY: msg.hotY });
|
|
238
|
+
} else if (msg.t === 'input_focus') {
|
|
239
|
+
onInputFocus?.({ editable: msg.editable === true });
|
|
240
|
+
} else if (msg.t === 'clipboard') {
|
|
241
|
+
if (typeof msg.text === 'string' && msg.text) onClipboard?.({ text: msg.text });
|
|
242
|
+
} else if (msg.t === 'secure_desktop') {
|
|
243
|
+
onSecureDesktop?.({ active: msg.active === true });
|
|
244
|
+
} else if (msg.t === 'capture') {
|
|
245
|
+
onCapture?.({ mode: msg.mode, reason: msg.reason });
|
|
246
|
+
} else if (msg.t === 'error') {
|
|
247
|
+
onError?.(new Error(msg.error || 'Desktop cast error'));
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
child.on('error', (err) => {
|
|
252
|
+
onError?.(err);
|
|
253
|
+
if (activeSession === session) activeSession = null;
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
child.on('close', () => {
|
|
257
|
+
if (activeSession === session) activeSession = null;
|
|
258
|
+
onStop?.();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
function sendInput(inputCmd) {
|
|
262
|
+
if (!child.stdin || child.stdin.destroyed) return;
|
|
263
|
+
try {
|
|
264
|
+
const payload = JSON.stringify({ ...inputCmd, op: 'input' });
|
|
265
|
+
child.stdin.write(payload + '\n');
|
|
266
|
+
} catch {
|
|
267
|
+
// bỏ qua lỗi ghi stdin nếu tiến trình đang đóng
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function config(configCmd) {
|
|
272
|
+
if (!child.stdin || child.stdin.destroyed) return;
|
|
273
|
+
try {
|
|
274
|
+
const payload = JSON.stringify({ ...configCmd, op: 'config' });
|
|
275
|
+
child.stdin.write(payload + '\n');
|
|
276
|
+
} catch {
|
|
277
|
+
// bỏ qua
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Bật / tắt nén JPEG (chỉ cần khi còn người xem chưa có video WebRTC). */
|
|
282
|
+
let jpegOn = true;
|
|
283
|
+
function setJpeg(on) {
|
|
284
|
+
if (!pipeName || jpegOn === on) return;
|
|
285
|
+
jpegOn = on;
|
|
286
|
+
config({ jpeg: on });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Con trỏ lồng vào khung hình (mặc định) hay gửi riêng để FE tự vẽ. */
|
|
290
|
+
let cursorInFrame = true;
|
|
291
|
+
function setCursorInFrame(on) {
|
|
292
|
+
if (cursorInFrame === on) return;
|
|
293
|
+
cursorInFrame = on;
|
|
294
|
+
config({ cursor: on });
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Bật / tắt thu âm thanh hệ thống qua WASAPI loopback. */
|
|
298
|
+
let audioOn = false;
|
|
299
|
+
function setAudio(on) {
|
|
300
|
+
const next = on !== false;
|
|
301
|
+
if (!pipeName || audioOn === next) return;
|
|
302
|
+
audioOn = next;
|
|
303
|
+
config({ audio: next });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Gửi ngay một khung (người xem mới nối, màn hình đang đứng yên). */
|
|
307
|
+
function refresh() {
|
|
308
|
+
if (!child.stdin || child.stdin.destroyed) return;
|
|
309
|
+
try { child.stdin.write(JSON.stringify({ op: 'refresh' }) + '\n'); } catch { /* đang đóng */ }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function stop() {
|
|
313
|
+
if (activeSession === session) activeSession = null;
|
|
314
|
+
try {
|
|
315
|
+
if (child.stdin && !child.stdin.destroyed) {
|
|
316
|
+
child.stdin.write('stop\n');
|
|
317
|
+
}
|
|
318
|
+
} catch {
|
|
319
|
+
// bỏ qua
|
|
320
|
+
}
|
|
321
|
+
setTimeout(() => {
|
|
322
|
+
try {
|
|
323
|
+
child.kill();
|
|
324
|
+
} catch {
|
|
325
|
+
// bỏ qua
|
|
326
|
+
}
|
|
327
|
+
}, 500);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const session = {
|
|
331
|
+
sessionId,
|
|
332
|
+
child,
|
|
333
|
+
sendInput,
|
|
334
|
+
config,
|
|
335
|
+
setJpeg,
|
|
336
|
+
setCursorInFrame,
|
|
337
|
+
setAudio,
|
|
338
|
+
refresh,
|
|
339
|
+
stop,
|
|
340
|
+
pipeName,
|
|
341
|
+
};
|
|
342
|
+
activeSession = session;
|
|
343
|
+
|
|
344
|
+
return activeSession;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function setSessionId(sessionId) {
|
|
348
|
+
if (activeSession && sessionId) {
|
|
349
|
+
activeSession.sessionId = sessionId;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function sendInput(inputCmd) {
|
|
354
|
+
if (activeSession) {
|
|
355
|
+
activeSession.sendInput(inputCmd);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function config(configCmd) {
|
|
360
|
+
if (activeSession) {
|
|
361
|
+
activeSession.config(configCmd);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function stop() {
|
|
366
|
+
if (activeSession) {
|
|
367
|
+
activeSession.stop();
|
|
368
|
+
activeSession = null;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function setJpeg(on) {
|
|
373
|
+
activeSession?.setJpeg(on);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function setCursorInFrame(on) {
|
|
377
|
+
activeSession?.setCursorInFrame(on);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function setAudio(on) {
|
|
381
|
+
activeSession?.setAudio(on);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function refresh() {
|
|
385
|
+
activeSession?.refresh();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Tên named pipe khung thô của phiên đang phát (rỗng khi không bật `rawPipe`). */
|
|
389
|
+
function pipeName() {
|
|
390
|
+
return activeSession?.pipeName || '';
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function isActive() {
|
|
394
|
+
return activeSession !== null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Mã phiên đang phát (BE đặt) — lệnh / thao tác mang mã khác bị bỏ. */
|
|
398
|
+
function sessionId() {
|
|
399
|
+
return activeSession?.sessionId || null;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return {
|
|
403
|
+
prewarm,
|
|
404
|
+
start,
|
|
405
|
+
setSessionId,
|
|
406
|
+
sessionId,
|
|
407
|
+
sendInput,
|
|
408
|
+
config,
|
|
409
|
+
setJpeg,
|
|
410
|
+
setCursorInFrame,
|
|
411
|
+
setAudio,
|
|
412
|
+
refresh,
|
|
413
|
+
pipeName,
|
|
414
|
+
stop,
|
|
415
|
+
isActive,
|
|
416
|
+
};
|
|
417
|
+
}
|