@cldmv/node-android-tv-remote 2.1.0 → 2.1.8
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/LICENSE +202 -0
- package/README.md +188 -88
- package/index.cjs +20 -8
- package/index.mjs +7 -5
- package/package.json +31 -9
- package/scripts/postinstall.mjs +51 -36
- package/scripts/setup-device.mjs +62 -16
- package/scripts/wake-tv.mjs +45 -29
- package/src/lib/android-tv-remote.mjs +349 -258
|
@@ -1,49 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
|
+
*
|
|
2
3
|
* @Project: @cldmv/node-android-tv-remote
|
|
3
4
|
* @Filename: /src/lib/android-tv-remote.mjs
|
|
4
|
-
* @Date: 2025-10-
|
|
5
|
-
* @Author: Nate
|
|
5
|
+
* @Date: 2025-10-15T10:19:05-07:00 (1760548745)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
6
7
|
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
8
|
* -----
|
|
8
|
-
* @Last modified by: Nate
|
|
9
|
-
* @Last modified time:
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-02T12:16:15-07:00 (1790968575)
|
|
10
11
|
* -----
|
|
11
|
-
* @Copyright: Copyright (c) 2013-
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
12
14
|
*/
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* Android TV Remote module - Event-driven ADB remote control for Android TV devices.
|
|
16
|
-
*
|
|
18
|
+
*
|
|
17
19
|
* @module android-tv-remote
|
|
18
|
-
*
|
|
20
|
+
*
|
|
19
21
|
* @description
|
|
20
22
|
* This module provides an event-driven interface for controlling Android TV devices via ADB.
|
|
21
23
|
* All operations emit events instead of console logging, allowing for better integration
|
|
22
24
|
* and programmatic handling of device communication.
|
|
23
|
-
*
|
|
25
|
+
*
|
|
24
26
|
* @example
|
|
25
27
|
* // ESM usage with event handling
|
|
26
28
|
* import createRemote from "@cldmv/node-android-tv-remote";
|
|
27
|
-
*
|
|
29
|
+
*
|
|
28
30
|
* const remote = createRemote({ ip: "192.168.1.100" });
|
|
29
|
-
*
|
|
31
|
+
*
|
|
30
32
|
* // Listen for log events
|
|
31
33
|
* remote.on('log', (data) => {
|
|
32
34
|
* console.log(`[${data.level}] ${data.message}`, data.source);
|
|
33
35
|
* });
|
|
34
|
-
*
|
|
36
|
+
*
|
|
35
37
|
* // Listen for errors
|
|
36
38
|
* remote.on('error', (data) => {
|
|
37
39
|
* console.error(`Error from ${data.source}:`, data.error.message);
|
|
38
40
|
* });
|
|
39
|
-
*
|
|
41
|
+
*
|
|
40
42
|
* // Use the remote
|
|
41
43
|
* await remote.press.home();
|
|
42
|
-
*
|
|
44
|
+
*
|
|
43
45
|
* @example
|
|
44
46
|
* // Static create method
|
|
45
47
|
* import { createAndroidTVRemote } from "@cldmv/node-android-tv-remote";
|
|
46
|
-
*
|
|
48
|
+
*
|
|
47
49
|
* const remote = await createAndroidTVRemote({ ip: "192.168.1.100" });
|
|
48
50
|
* remote.on('log', console.log);
|
|
49
51
|
* await remote.press.play();
|
|
@@ -100,12 +102,12 @@
|
|
|
100
102
|
* @property {Object} keyboard - Keyboard interface for all keys, with text and keycode fallback.
|
|
101
103
|
* @property {Object} keyboard.key - Contains all key functions.
|
|
102
104
|
* @property {Object} keyboard.key.shift - Contains all shifted key functions.
|
|
103
|
-
*
|
|
105
|
+
*
|
|
104
106
|
* @property {function(string, Function): Remote} on - Add event listener. Returns remote instance for chaining.
|
|
105
107
|
* @property {function(string, Function): Remote} off - Remove event listener. Returns remote instance for chaining.
|
|
106
108
|
* @property {function(string, Function): Remote} once - Add one-time event listener. Returns remote instance for chaining.
|
|
107
109
|
* @property {function(string, ...any): boolean} emit - Emit an event. Returns true if event had listeners.
|
|
108
|
-
*
|
|
110
|
+
*
|
|
109
111
|
* @fires Remote#log - Emitted for informational messages, warnings, and debug info.
|
|
110
112
|
* @fires Remote#error - Emitted when errors occur during ADB operations.
|
|
111
113
|
* @fires Remote#screencap-start - Emitted when screenshot capture begins.
|
|
@@ -114,7 +116,7 @@
|
|
|
114
116
|
* @fires Remote#screencap-ready - Emitted when final processed stream is ready.
|
|
115
117
|
* @fires Remote#screencap-saved - Emitted when screenshot is saved to file.
|
|
116
118
|
* @fires Remote#screencap-complete - Emitted when entire screenshot operation is complete.
|
|
117
|
-
*
|
|
119
|
+
*
|
|
118
120
|
* @example
|
|
119
121
|
* // Event handling examples
|
|
120
122
|
* remote.on('log', (data) => {
|
|
@@ -126,11 +128,11 @@
|
|
|
126
128
|
* console.log(`INFO [${data.source}]: ${data.message}`);
|
|
127
129
|
* }
|
|
128
130
|
* });
|
|
129
|
-
*
|
|
131
|
+
*
|
|
130
132
|
* remote.on('error', (data) => {
|
|
131
133
|
* console.error(`FATAL ERROR from ${data.source}:`);
|
|
132
134
|
* console.error(data.error.stack || data.error.message);
|
|
133
|
-
*
|
|
135
|
+
*
|
|
134
136
|
* // Handle specific error types
|
|
135
137
|
* if (data.error.message.includes('device unauthorized')) {
|
|
136
138
|
* console.log('Please authorize ADB on your Android TV device');
|
|
@@ -140,10 +142,16 @@
|
|
|
140
142
|
|
|
141
143
|
import adbkit from "@devicefarmer/adbkit";
|
|
142
144
|
import { EventEmitter } from "events";
|
|
145
|
+
import { debuglog } from "node:util";
|
|
143
146
|
import { createWriteStream } from "fs";
|
|
144
|
-
import { pipeline } from "stream/promises";
|
|
145
147
|
import sharp from "sharp";
|
|
146
|
-
|
|
148
|
+
// @devicefarmer/adbkit is plain CJS (`exports.default` / `exports.Adb` both
|
|
149
|
+
// getter re-exports of the same class). Under plain Node's ESM/CJS interop,
|
|
150
|
+
// the default import resolves to `module.exports` (so `.Adb` holds the
|
|
151
|
+
// class); under Vite/Rollup-based CJS interop (e.g. vitest with the module
|
|
152
|
+
// inlined), the default import unwraps straight to the class itself. Cover
|
|
153
|
+
// both shapes rather than assuming one.
|
|
154
|
+
const Adb = adbkit.Adb ?? adbkit;
|
|
147
155
|
|
|
148
156
|
/**
|
|
149
157
|
* Wraps an async function to support both promise and callback styles.
|
|
@@ -163,92 +171,141 @@ function wrapAsync(fn) {
|
|
|
163
171
|
};
|
|
164
172
|
}
|
|
165
173
|
|
|
166
|
-
// Create event emitter for this instance
|
|
167
|
-
const emitter = new EventEmitter();
|
|
168
|
-
|
|
169
174
|
/**
|
|
170
|
-
*
|
|
175
|
+
* Debug channel for errors no "error" listener is attached to.
|
|
176
|
+
* Enable with NODE_DEBUG=android-tv-remote.
|
|
171
177
|
* @private
|
|
172
|
-
* @param {string} level - Log level (info, warn, error, debug).
|
|
173
|
-
* @param {string} message - Log message.
|
|
174
|
-
* @param {string} [source] - Source of the log message.
|
|
175
|
-
* @param {any} [data] - Additional data to include.
|
|
176
178
|
*/
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
level,
|
|
180
|
-
message,
|
|
181
|
-
source,
|
|
182
|
-
timestamp: new Date().toISOString(),
|
|
183
|
-
...(data && { data })
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
179
|
+
const debug = debuglog("android-tv-remote");
|
|
180
|
+
|
|
187
181
|
/**
|
|
188
|
-
*
|
|
182
|
+
* Create the event channel for one remote instance. Every remote gets its own
|
|
183
|
+
* EventEmitter, so listeners on one remote never see another remote's events.
|
|
189
184
|
* @private
|
|
190
|
-
* @
|
|
191
|
-
* @param {string} [source] - Source of the error.
|
|
192
|
-
* @param {string} [message] - Additional error message.
|
|
185
|
+
* @returns {{ emitter: EventEmitter, emitLog: Function, emitError: Function, handleDisconnectError: Function }}
|
|
193
186
|
*/
|
|
194
|
-
function
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
187
|
+
function createEventChannel() {
|
|
188
|
+
const emitter = new EventEmitter();
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Emit a log event with structured data.
|
|
192
|
+
* @private
|
|
193
|
+
* @param {string} level - Log level (info, warn, error, debug).
|
|
194
|
+
* @param {string} message - Log message.
|
|
195
|
+
* @param {string} [source] - Source of the log message.
|
|
196
|
+
* @param {any} [data] - Additional data to include.
|
|
197
|
+
*/
|
|
198
|
+
function emitLog(level, message, source = "android-tv-remote", data = null) {
|
|
199
|
+
emitter.emit("log", {
|
|
200
|
+
level,
|
|
201
|
+
message,
|
|
202
|
+
source,
|
|
203
|
+
timestamp: new Date().toISOString(),
|
|
204
|
+
...(data && { data })
|
|
205
|
+
});
|
|
204
206
|
}
|
|
205
|
-
|
|
206
|
-
emitter.emit("error", {
|
|
207
|
-
error,
|
|
208
|
-
source,
|
|
209
|
-
message: message || error.message,
|
|
210
|
-
timestamp: new Date().toISOString()
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Handles disconnect and connection errors, emits helpful messages.
|
|
216
|
-
* Also provides onboarding steps for common authentication and connection issues.
|
|
217
|
-
* @private
|
|
218
|
-
* @param {Error} err - The error object.
|
|
219
|
-
* @example
|
|
220
|
-
* try {
|
|
221
|
-
* // ...code that may throw
|
|
222
|
-
* } catch (err) {
|
|
223
|
-
* handleDisconnectError(err);
|
|
224
|
-
* }
|
|
225
|
-
*/
|
|
226
207
|
|
|
227
|
-
|
|
228
|
-
|
|
208
|
+
/**
|
|
209
|
+
* Emit an error event with structured data.
|
|
210
|
+
* @private
|
|
211
|
+
* @param {Error} error - The error object.
|
|
212
|
+
* @param {string} [source] - Source of the error.
|
|
213
|
+
* @param {string} [message] - Additional error message.
|
|
214
|
+
*/
|
|
215
|
+
function emitError(error, source = "android-tv-remote", message = null) {
|
|
216
|
+
// Filter out libspng/PNG processing errors that occur after disconnection
|
|
217
|
+
// These are common when background operations try to process data after disconnect
|
|
218
|
+
const errorMsg = error.message || "";
|
|
219
|
+
if (errorMsg.includes("libspng") || errorMsg.includes("pngload_buffer") || errorMsg.includes("read error")) {
|
|
220
|
+
// Log as debug instead of error to avoid noise
|
|
221
|
+
emitLog("debug", `PNG processing error (likely post-disconnect): ${errorMsg}`, source);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
229
224
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
225
|
+
const payload = {
|
|
226
|
+
error,
|
|
227
|
+
source,
|
|
228
|
+
message: message || error.message,
|
|
229
|
+
timestamp: new Date().toISOString()
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// EventEmitter throws when "error" is emitted with no listener, which would crash
|
|
233
|
+
// the consumer's process. Without a listener, route the error to the log channel
|
|
234
|
+
// (and NODE_DEBUG=android-tv-remote) instead; the failing operation still rejects
|
|
235
|
+
// or resolves with the error, so it is never silently lost.
|
|
236
|
+
if (emitter.listenerCount("error") > 0) {
|
|
237
|
+
emitter.emit("error", payload);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
debug("unhandled error from %s: %s", source, payload.message);
|
|
241
|
+
emitLog("error", payload.message, source, { error });
|
|
236
242
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Handles disconnect and connection errors, emits helpful messages.
|
|
246
|
+
* Also provides onboarding steps for common authentication and connection issues.
|
|
247
|
+
* @private
|
|
248
|
+
* @param {Error} err - The error object.
|
|
249
|
+
* @example
|
|
250
|
+
* try {
|
|
251
|
+
* // ...code that may throw
|
|
252
|
+
* } catch (err) {
|
|
253
|
+
* handleDisconnectError(err);
|
|
254
|
+
* }
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
function handleDisconnectError(err) {
|
|
258
|
+
emitError(err, "handleDisconnectError");
|
|
259
|
+
|
|
260
|
+
if (err.message && (err.message.includes("device unauthorized") || err.message.includes("failed to authenticate"))) {
|
|
261
|
+
emitError(err, "handleDisconnectError", "Device unauthorized - authentication required");
|
|
262
|
+
emitLog(
|
|
263
|
+
"error",
|
|
264
|
+
"Your device is unauthorized or failed to authenticate. Please check your TV and accept the authorization dialog to allow this system to connect via ADB.",
|
|
265
|
+
"handleDisconnectError"
|
|
266
|
+
);
|
|
267
|
+
emitLog(
|
|
268
|
+
"info",
|
|
269
|
+
"If you do not see a prompt, try disconnecting and reconnecting the device, or reboot your TV.",
|
|
270
|
+
"handleDisconnectError"
|
|
271
|
+
);
|
|
272
|
+
emitLog(
|
|
273
|
+
"info",
|
|
274
|
+
"If the problem persists, remove the device from the list of authorized ADB devices in Developer Options and try again.",
|
|
275
|
+
"handleDisconnectError"
|
|
276
|
+
);
|
|
277
|
+
emitLog(
|
|
278
|
+
"info",
|
|
279
|
+
"Tip: In Developer Options on your TV, try toggling 'ADB Debugging' off and then back on. This often resolves authentication issues.",
|
|
280
|
+
"handleDisconnectError"
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (err.message && (err.message.includes("actively refused") || err.message.includes("No connection could be made"))) {
|
|
285
|
+
emitError(err, "handleDisconnectError", "Connection refused - ADB not enabled");
|
|
286
|
+
emitLog(
|
|
287
|
+
"error",
|
|
288
|
+
"The device refused the connection. To enable ADB, follow these steps on your Android TV or Fire TV:",
|
|
289
|
+
"handleDisconnectError"
|
|
290
|
+
);
|
|
291
|
+
emitLog("info", "1. Open Settings > Device Preferences > About (or My Fire TV > About)", "handleDisconnectError");
|
|
292
|
+
emitLog("info", "2. Scroll to 'Build' and press OK 7 times to enable Developer Options", "handleDisconnectError");
|
|
293
|
+
emitLog("info", "3. Go back to Settings > Device Preferences > Developer Options", "handleDisconnectError");
|
|
294
|
+
emitLog(
|
|
295
|
+
"info",
|
|
296
|
+
"4. Enable 'Developer Options' if needed, then enable 'ADB Debugging' and 'Apps from Unknown Sources'",
|
|
297
|
+
"handleDisconnectError"
|
|
298
|
+
);
|
|
299
|
+
emitLog("info", "5. Ensure your TV and computer are on the same network", "handleDisconnectError");
|
|
300
|
+
emitLog("info", "6. On your computer, run: adb connect <device-ip>:5555", "handleDisconnectError");
|
|
301
|
+
emitLog("info", "7. Accept the authorization prompt on your TV", "handleDisconnectError");
|
|
302
|
+
emitLog("info", "If you do not see 'Developer Options', repeat step 2 until it appears.", "handleDisconnectError");
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return err;
|
|
249
306
|
}
|
|
250
|
-
|
|
251
|
-
return
|
|
307
|
+
|
|
308
|
+
return { emitter, emitLog, emitError, handleDisconnectError };
|
|
252
309
|
}
|
|
253
310
|
|
|
254
311
|
/**
|
|
@@ -272,16 +329,32 @@ function getShiftedCharacter(keyName, char) {
|
|
|
272
329
|
if (/^[a-z]$/.test(char)) {
|
|
273
330
|
return char.toUpperCase();
|
|
274
331
|
}
|
|
275
|
-
|
|
332
|
+
|
|
276
333
|
// Handle shifted symbols
|
|
277
334
|
const shiftMap = {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
335
|
+
"1": "!",
|
|
336
|
+
"2": "@",
|
|
337
|
+
"3": "#",
|
|
338
|
+
"4": "$",
|
|
339
|
+
"5": "%",
|
|
340
|
+
"6": "^",
|
|
341
|
+
"7": "&",
|
|
342
|
+
"8": "*",
|
|
343
|
+
"9": "(",
|
|
344
|
+
"0": ")",
|
|
345
|
+
"-": "_",
|
|
346
|
+
"=": "+",
|
|
347
|
+
"[": "{",
|
|
348
|
+
"]": "}",
|
|
349
|
+
"\\": "|",
|
|
350
|
+
";": ":",
|
|
351
|
+
"'": '"',
|
|
352
|
+
",": "<",
|
|
353
|
+
".": ">",
|
|
354
|
+
"/": "?",
|
|
355
|
+
"`": "~"
|
|
283
356
|
};
|
|
284
|
-
|
|
357
|
+
|
|
285
358
|
return shiftMap[char] || char;
|
|
286
359
|
}
|
|
287
360
|
|
|
@@ -313,9 +386,11 @@ export default async function createRemote(config) {
|
|
|
313
386
|
timeout: connectTimeout
|
|
314
387
|
});
|
|
315
388
|
const device = client.getDevice(host);
|
|
389
|
+
// Per-instance event channel: listeners are scoped to this remote.
|
|
390
|
+
const { emitter, emitLog, emitError, handleDisconnectError } = createEventChannel();
|
|
316
391
|
let connected = false;
|
|
317
392
|
let backgroundOperations = new Set();
|
|
318
|
-
|
|
393
|
+
|
|
319
394
|
/**
|
|
320
395
|
* Local emitError function that has access to connection state
|
|
321
396
|
* @private
|
|
@@ -326,18 +401,16 @@ export default async function createRemote(config) {
|
|
|
326
401
|
function localEmitError(error, source = "android-tv-remote", message = null) {
|
|
327
402
|
// Filter out libspng/PNG processing errors that occur after disconnection
|
|
328
403
|
const errorMsg = error.message || "";
|
|
329
|
-
if (!connected && (errorMsg.includes(
|
|
330
|
-
errorMsg.includes('pngload_buffer') ||
|
|
331
|
-
errorMsg.includes('read error'))) {
|
|
404
|
+
if (!connected && (errorMsg.includes("libspng") || errorMsg.includes("pngload_buffer") || errorMsg.includes("read error"))) {
|
|
332
405
|
// Log as debug instead of error to avoid noise
|
|
333
406
|
emitLog("debug", `PNG processing error (post-disconnect): ${errorMsg}`, source);
|
|
334
407
|
return;
|
|
335
408
|
}
|
|
336
|
-
|
|
409
|
+
|
|
337
410
|
// Use global emitError for other cases
|
|
338
411
|
emitError(error, source, message);
|
|
339
412
|
}
|
|
340
|
-
|
|
413
|
+
|
|
341
414
|
const autoConnect = config.autoConnect !== false; // default true
|
|
342
415
|
const autoDisconnect = config.autoDisconnect === true; // default false
|
|
343
416
|
const disconnectTimeout = typeof config.disconnectTimeout === "number" ? config.disconnectTimeout : 10;
|
|
@@ -411,6 +484,9 @@ export default async function createRemote(config) {
|
|
|
411
484
|
}, INIT_TIMEOUT_MS);
|
|
412
485
|
})
|
|
413
486
|
]);
|
|
487
|
+
// Callers who never look at initPromise must not get an unhandled rejection (which
|
|
488
|
+
// crashes Node.js) when auto-connect fails; awaiting it still rejects as before.
|
|
489
|
+
initPromise.catch(() => {});
|
|
414
490
|
|
|
415
491
|
/**
|
|
416
492
|
* Resets the disconnect timer if autoDisconnect is enabled.
|
|
@@ -537,23 +613,33 @@ export default async function createRemote(config) {
|
|
|
537
613
|
disconnectTimer = null;
|
|
538
614
|
}
|
|
539
615
|
stopHeartbeat();
|
|
540
|
-
|
|
616
|
+
|
|
541
617
|
// Wait for all background operations to complete before disconnecting
|
|
542
618
|
if (backgroundOperations.size > 0) {
|
|
543
|
-
if (!quiet)
|
|
619
|
+
if (!quiet)
|
|
620
|
+
emitLog("info", `🔄 [DISCONNECT] Waiting for ${backgroundOperations.size} background operations to complete...`, "disconnect");
|
|
544
621
|
const waitStartTime = performance.now();
|
|
545
622
|
try {
|
|
546
623
|
await Promise.allSettled([...backgroundOperations]);
|
|
547
624
|
const waitEndTime = performance.now();
|
|
548
|
-
if (!quiet)
|
|
625
|
+
if (!quiet)
|
|
626
|
+
emitLog(
|
|
627
|
+
"info",
|
|
628
|
+
`✅ [DISCONNECT] All background operations completed in ${(waitEndTime - waitStartTime).toFixed(2)}ms`,
|
|
629
|
+
"disconnect"
|
|
630
|
+
);
|
|
549
631
|
} catch (error) {
|
|
550
632
|
const waitEndTime = performance.now();
|
|
551
|
-
emitLog(
|
|
633
|
+
emitLog(
|
|
634
|
+
"warn",
|
|
635
|
+
`⚠️ [DISCONNECT] Some background operations failed after ${(waitEndTime - waitStartTime).toFixed(2)}ms: ${error.message}`,
|
|
636
|
+
"disconnect"
|
|
637
|
+
);
|
|
552
638
|
}
|
|
553
639
|
} else {
|
|
554
640
|
if (!quiet) emitLog("info", `✅ [DISCONNECT] No background operations to wait for`, "disconnect");
|
|
555
641
|
}
|
|
556
|
-
|
|
642
|
+
|
|
557
643
|
try {
|
|
558
644
|
await client.disconnect(ip, port);
|
|
559
645
|
connected = false;
|
|
@@ -694,7 +780,7 @@ export default async function createRemote(config) {
|
|
|
694
780
|
* remote.initPromise.then(() => { ... }).catch((err) => { ... });
|
|
695
781
|
*/
|
|
696
782
|
initPromise,
|
|
697
|
-
|
|
783
|
+
|
|
698
784
|
/**
|
|
699
785
|
* Add event listener.
|
|
700
786
|
* @public
|
|
@@ -709,7 +795,7 @@ export default async function createRemote(config) {
|
|
|
709
795
|
emitter.on(event, listener);
|
|
710
796
|
return remoteApi;
|
|
711
797
|
},
|
|
712
|
-
|
|
798
|
+
|
|
713
799
|
/**
|
|
714
800
|
* Remove event listener.
|
|
715
801
|
* @public
|
|
@@ -723,7 +809,7 @@ export default async function createRemote(config) {
|
|
|
723
809
|
emitter.off(event, listener);
|
|
724
810
|
return remoteApi;
|
|
725
811
|
},
|
|
726
|
-
|
|
812
|
+
|
|
727
813
|
/**
|
|
728
814
|
* Add one-time event listener.
|
|
729
815
|
* @public
|
|
@@ -737,7 +823,7 @@ export default async function createRemote(config) {
|
|
|
737
823
|
emitter.once(event, listener);
|
|
738
824
|
return remoteApi;
|
|
739
825
|
},
|
|
740
|
-
|
|
826
|
+
|
|
741
827
|
/**
|
|
742
828
|
* Emit an event.
|
|
743
829
|
* @public
|
|
@@ -788,7 +874,7 @@ export default async function createRemote(config) {
|
|
|
788
874
|
get isConnected() {
|
|
789
875
|
return isConnected();
|
|
790
876
|
},
|
|
791
|
-
|
|
877
|
+
|
|
792
878
|
/**
|
|
793
879
|
* Returns the last screencap data (PNG stream) captured by screencap() or thumbnail().
|
|
794
880
|
* Updated every time a screenshot is taken, regardless of options used.
|
|
@@ -797,7 +883,7 @@ export default async function createRemote(config) {
|
|
|
797
883
|
* @example
|
|
798
884
|
* await remote.screencap({ filepath: './screenshot.png' });
|
|
799
885
|
* console.log('Last screencap stream:', remote.lastScreencapData);
|
|
800
|
-
*
|
|
886
|
+
*
|
|
801
887
|
* // Use the cached stream data
|
|
802
888
|
* if (remote.lastScreencapData) {
|
|
803
889
|
* remote.lastScreencapData.pipe(someOtherStream);
|
|
@@ -817,27 +903,25 @@ export default async function createRemote(config) {
|
|
|
817
903
|
* await remote.setSettings(); // Set all optimal settings
|
|
818
904
|
* await remote.setSettings('get'); // Get current settings values
|
|
819
905
|
*/
|
|
820
|
-
async setSettings(mode =
|
|
906
|
+
async setSettings(mode = "set") {
|
|
821
907
|
try {
|
|
822
|
-
emitLog("info", `=== ${mode ===
|
|
823
|
-
|
|
908
|
+
emitLog("info", `=== ${mode === "set" ? "Configuring" : "Retrieving"} Android TV settings ===`, "setSettings");
|
|
909
|
+
|
|
824
910
|
await ensureConnected();
|
|
825
|
-
|
|
911
|
+
|
|
826
912
|
// Core settings for power management and display
|
|
827
913
|
const coreSettings = [
|
|
828
914
|
{ ns: "system", key: "screen_off_timeout", value: 2147483647 },
|
|
829
915
|
{ ns: "secure", key: "sleep_timeout", value: 0 },
|
|
830
916
|
{ ns: "global", key: "stay_on_while_plugged_in", value: 3 }
|
|
831
917
|
];
|
|
832
|
-
|
|
918
|
+
|
|
833
919
|
// Process core settings
|
|
834
920
|
for (const item of coreSettings) {
|
|
835
|
-
const cmd = mode === "set"
|
|
836
|
-
|
|
837
|
-
: `settings get ${item.ns} ${item.key}`;
|
|
838
|
-
|
|
921
|
+
const cmd = mode === "set" ? `settings put ${item.ns} ${item.key} ${item.value}` : `settings get ${item.ns} ${item.key}`;
|
|
922
|
+
|
|
839
923
|
const result = await device.shell(cmd).then(Adb.util.readAll);
|
|
840
|
-
|
|
924
|
+
|
|
841
925
|
if (!quiet) {
|
|
842
926
|
if (mode === "set") {
|
|
843
927
|
emitLog("info", `Set ${item.ns} ${item.key} to ${item.value}`, "setSettings");
|
|
@@ -846,27 +930,26 @@ export default async function createRemote(config) {
|
|
|
846
930
|
}
|
|
847
931
|
}
|
|
848
932
|
}
|
|
849
|
-
|
|
933
|
+
|
|
850
934
|
// Additional settings (only applied in set mode)
|
|
851
|
-
if (mode ===
|
|
935
|
+
if (mode === "set") {
|
|
852
936
|
emitLog("info", "Setting additional display and power settings", "setSettings");
|
|
853
|
-
|
|
937
|
+
|
|
854
938
|
const additionalSettings = [
|
|
855
|
-
"settings put system screen_brightness_mode 0",
|
|
856
|
-
"settings put system screen_brightness 255",
|
|
857
|
-
"svc power stayon true"
|
|
939
|
+
"settings put system screen_brightness_mode 0", // Manual brightness
|
|
940
|
+
"settings put system screen_brightness 255", // Max brightness
|
|
941
|
+
"svc power stayon true" // Stay on while plugged
|
|
858
942
|
];
|
|
859
|
-
|
|
943
|
+
|
|
860
944
|
for (const cmd of additionalSettings) {
|
|
861
945
|
if (!quiet) emitLog("info", `Running: ${cmd}`, "setSettings");
|
|
862
946
|
await device.shell(cmd);
|
|
863
947
|
}
|
|
864
948
|
}
|
|
865
|
-
|
|
866
|
-
emitLog("info", `✅ Android TV settings ${mode ===
|
|
867
|
-
|
|
949
|
+
|
|
950
|
+
emitLog("info", `✅ Android TV settings ${mode === "set" ? "configured" : "retrieved"} successfully`, "setSettings");
|
|
868
951
|
} catch (error) {
|
|
869
|
-
localEmitError(error, "setSettings", `Failed to ${mode ===
|
|
952
|
+
localEmitError(error, "setSettings", `Failed to ${mode === "set" ? "configure" : "retrieve"} Android TV settings`);
|
|
870
953
|
throw error;
|
|
871
954
|
}
|
|
872
955
|
},
|
|
@@ -923,7 +1006,7 @@ export default async function createRemote(config) {
|
|
|
923
1006
|
|
|
924
1007
|
// Determine what commands need to be sent
|
|
925
1008
|
const commandsToSend = [];
|
|
926
|
-
|
|
1009
|
+
|
|
927
1010
|
if (initialPowerState.mIsPowered !== "true") {
|
|
928
1011
|
emitLog("info", `Device not powered (${initialPowerState.mIsPowered}), will send POWER keycode`, "ensureAwake");
|
|
929
1012
|
commandsToSend.push({ keycode: keycodes.power, reason: "power on device" });
|
|
@@ -948,18 +1031,18 @@ export default async function createRemote(config) {
|
|
|
948
1031
|
// Send required commands
|
|
949
1032
|
if (commandsToSend.length > 0) {
|
|
950
1033
|
emitLog("info", `Sending ${commandsToSend.length} keycode(s)...`, "ensureAwake");
|
|
951
|
-
|
|
1034
|
+
|
|
952
1035
|
for (const cmd of commandsToSend) {
|
|
953
1036
|
emitLog("info", `Sending keycode ${cmd.keycode} to ${cmd.reason}`, "ensureAwake");
|
|
954
1037
|
await inputKeycode(cmd.keycode);
|
|
955
|
-
|
|
1038
|
+
|
|
956
1039
|
// Brief delay between commands
|
|
957
|
-
await new Promise(resolve => setTimeout(resolve, 500));
|
|
1040
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
958
1041
|
}
|
|
959
1042
|
|
|
960
1043
|
// Wait for commands to take effect
|
|
961
1044
|
emitLog("info", "Waiting for commands to take effect...", "ensureAwake");
|
|
962
|
-
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
1045
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
963
1046
|
}
|
|
964
1047
|
|
|
965
1048
|
// Verify final state
|
|
@@ -986,16 +1069,15 @@ export default async function createRemote(config) {
|
|
|
986
1069
|
for (const issue of issues) {
|
|
987
1070
|
emitLog("warn", issue, "ensureAwake");
|
|
988
1071
|
}
|
|
989
|
-
|
|
1072
|
+
|
|
990
1073
|
// Only throw error if critical issues persist
|
|
991
1074
|
emitError(
|
|
992
|
-
new Error(`ensureAwake failed with ${issues.length} persistent issue(s): ${issues.join(
|
|
993
|
-
"ensureAwake",
|
|
1075
|
+
new Error(`ensureAwake failed with ${issues.length} persistent issue(s): ${issues.join(", ")}`),
|
|
1076
|
+
"ensureAwake",
|
|
994
1077
|
"Device may not be fully responsive"
|
|
995
1078
|
);
|
|
996
1079
|
return false;
|
|
997
1080
|
}
|
|
998
|
-
|
|
999
1081
|
} catch (error) {
|
|
1000
1082
|
emitError(error, "ensureAwake", "Failed to ensure device is awake");
|
|
1001
1083
|
throw error;
|
|
@@ -1013,9 +1095,10 @@ export default async function createRemote(config) {
|
|
|
1013
1095
|
* await remote.connect();
|
|
1014
1096
|
* remote.connect((err) => { ... });
|
|
1015
1097
|
*/
|
|
1016
|
-
connect:
|
|
1017
|
-
|
|
1018
|
-
|
|
1098
|
+
connect:
|
|
1099
|
+
/**
|
|
1100
|
+
* @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
1101
|
+
*/ (connectWrapped),
|
|
1019
1102
|
|
|
1020
1103
|
/**
|
|
1021
1104
|
* Disconnect from the device. Supports both promise and callback styles.
|
|
@@ -1028,9 +1111,10 @@ export default async function createRemote(config) {
|
|
|
1028
1111
|
* await remote.disconnect();
|
|
1029
1112
|
* remote.disconnect((err) => { ... });
|
|
1030
1113
|
*/
|
|
1031
|
-
disconnect:
|
|
1032
|
-
|
|
1033
|
-
|
|
1114
|
+
disconnect:
|
|
1115
|
+
/**
|
|
1116
|
+
* @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
1117
|
+
*/ (disconnectWrapped),
|
|
1034
1118
|
|
|
1035
1119
|
/**
|
|
1036
1120
|
* Send a keycode. Supports both promise and callback styles.
|
|
@@ -1042,9 +1126,10 @@ export default async function createRemote(config) {
|
|
|
1042
1126
|
* await remote.inputKeycode(23);
|
|
1043
1127
|
* remote.inputKeycode(23, (err) => { ... });
|
|
1044
1128
|
*/
|
|
1045
|
-
inputKeycode:
|
|
1046
|
-
|
|
1047
|
-
|
|
1129
|
+
inputKeycode:
|
|
1130
|
+
/**
|
|
1131
|
+
* @type {(code: number, cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
1132
|
+
*/ (inputKeycodeWrapped),
|
|
1048
1133
|
|
|
1049
1134
|
/**
|
|
1050
1135
|
* Reboots the Android TV device using ADB's native reboot method.
|
|
@@ -1058,12 +1143,12 @@ export default async function createRemote(config) {
|
|
|
1058
1143
|
async reboot() {
|
|
1059
1144
|
try {
|
|
1060
1145
|
emitLog("info", "=== Starting device reboot ===", "reboot");
|
|
1061
|
-
|
|
1146
|
+
|
|
1062
1147
|
// Ensure we're connected before sending reboot command
|
|
1063
1148
|
await ensureConnected();
|
|
1064
|
-
|
|
1149
|
+
|
|
1065
1150
|
emitLog("info", "Sending reboot command to device", "reboot");
|
|
1066
|
-
|
|
1151
|
+
|
|
1067
1152
|
// Use the native device.reboot() method with timeout handling
|
|
1068
1153
|
let result;
|
|
1069
1154
|
try {
|
|
@@ -1080,7 +1165,7 @@ export default async function createRemote(config) {
|
|
|
1080
1165
|
}
|
|
1081
1166
|
}
|
|
1082
1167
|
emitLog("warn", "Device will reboot shortly and connection will be lost", "reboot");
|
|
1083
|
-
|
|
1168
|
+
|
|
1084
1169
|
// Wait a moment for command to take effect, then disconnect
|
|
1085
1170
|
setTimeout(() => {
|
|
1086
1171
|
emitLog("info", "Device should be rebooting now - connection will be terminated", "reboot");
|
|
@@ -1091,10 +1176,9 @@ export default async function createRemote(config) {
|
|
|
1091
1176
|
|
|
1092
1177
|
emitLog("info", "=== Device reboot initiated ===", "reboot");
|
|
1093
1178
|
return result;
|
|
1094
|
-
|
|
1095
1179
|
} catch (error) {
|
|
1096
1180
|
emitError(error, "reboot", "Failed to reboot device");
|
|
1097
|
-
|
|
1181
|
+
|
|
1098
1182
|
// Provide helpful error messages for common issues
|
|
1099
1183
|
if (error.message && error.message.includes("unauthorized")) {
|
|
1100
1184
|
emitLog("error", "Device unauthorized - ensure ADB debugging is enabled and device is authorized", "reboot");
|
|
@@ -1105,7 +1189,7 @@ export default async function createRemote(config) {
|
|
|
1105
1189
|
} else if (error.message && error.message.includes("timeout")) {
|
|
1106
1190
|
emitLog("warn", "Reboot command timed out - this may be normal behavior during reboot", "reboot");
|
|
1107
1191
|
}
|
|
1108
|
-
|
|
1192
|
+
|
|
1109
1193
|
throw error;
|
|
1110
1194
|
}
|
|
1111
1195
|
},
|
|
@@ -1115,7 +1199,7 @@ export default async function createRemote(config) {
|
|
|
1115
1199
|
* @public
|
|
1116
1200
|
* @param {Object} [options={}] - Screenshot options
|
|
1117
1201
|
* @param {number} [options.width] - Target width for resizing (optional)
|
|
1118
|
-
* @param {number} [options.height] - Target height for resizing (optional)
|
|
1202
|
+
* @param {number} [options.height] - Target height for resizing (optional)
|
|
1119
1203
|
* @param {string} [options.filepath] - File path to save screenshot (optional)
|
|
1120
1204
|
* @returns {Promise<ReadableStream|void>} Returns PNG stream if no filepath, void if saved to file
|
|
1121
1205
|
* @fires Remote#screencap-start - Emitted when screenshot capture begins
|
|
@@ -1129,16 +1213,16 @@ export default async function createRemote(config) {
|
|
|
1129
1213
|
* @example
|
|
1130
1214
|
* // Basic screenshot - returns stream
|
|
1131
1215
|
* const pngStream = await remote.screencap();
|
|
1132
|
-
*
|
|
1216
|
+
*
|
|
1133
1217
|
* // Screenshot with resizing
|
|
1134
1218
|
* const resizedStream = await remote.screencap({ width: 1280, height: 720 });
|
|
1135
|
-
*
|
|
1219
|
+
*
|
|
1136
1220
|
* // Screenshot saved to file
|
|
1137
1221
|
* await remote.screencap({ filepath: './screenshot.png' });
|
|
1138
|
-
*
|
|
1222
|
+
*
|
|
1139
1223
|
* // Screenshot with resizing and file save
|
|
1140
1224
|
* await remote.screencap({ width: 640, height: 360, filepath: './thumb.png' });
|
|
1141
|
-
*
|
|
1225
|
+
*
|
|
1142
1226
|
* // Event-driven usage (non-blocking)
|
|
1143
1227
|
* remote.screencap({ width: 1280, filepath: './shot.png' });
|
|
1144
1228
|
* remote.on('screencap-complete', (data) => {
|
|
@@ -1148,27 +1232,27 @@ export default async function createRemote(config) {
|
|
|
1148
1232
|
async screencap(options = {}) {
|
|
1149
1233
|
const startTime = performance.now();
|
|
1150
1234
|
const { width, height, filepath } = options;
|
|
1151
|
-
|
|
1235
|
+
|
|
1152
1236
|
try {
|
|
1153
1237
|
emitLog("info", "=== Taking device screenshot ===", "screencap");
|
|
1154
|
-
|
|
1238
|
+
|
|
1155
1239
|
// Emit start event
|
|
1156
1240
|
emitter.emit("screencap-start", {
|
|
1157
1241
|
timestamp: new Date().toISOString(),
|
|
1158
1242
|
options: { width, height, filepath }
|
|
1159
1243
|
});
|
|
1160
|
-
|
|
1244
|
+
|
|
1161
1245
|
// Ensure we're connected before taking screenshot
|
|
1162
1246
|
await ensureConnected();
|
|
1163
|
-
|
|
1247
|
+
|
|
1164
1248
|
emitLog("info", "Capturing screenshot using native screencap utility", "screencap");
|
|
1165
|
-
|
|
1249
|
+
|
|
1166
1250
|
// Use the native device.screencap() method which should return PNG
|
|
1167
1251
|
const screencapStream = await device.screencap();
|
|
1168
1252
|
const captureTime = performance.now();
|
|
1169
|
-
|
|
1253
|
+
|
|
1170
1254
|
emitLog("info", `✅ Screenshot captured successfully (${(captureTime - startTime).toFixed(2)}ms)`, "screencap");
|
|
1171
|
-
|
|
1255
|
+
|
|
1172
1256
|
// Emit captured event
|
|
1173
1257
|
emitter.emit("screencap-captured", {
|
|
1174
1258
|
timestamp: new Date().toISOString(),
|
|
@@ -1177,44 +1261,44 @@ export default async function createRemote(config) {
|
|
|
1177
1261
|
|
|
1178
1262
|
// Determine if we need Sharp processing (only for resizing)
|
|
1179
1263
|
const needsSharpProcessing = width || height;
|
|
1180
|
-
|
|
1264
|
+
|
|
1181
1265
|
// Handle file save without processing (direct PNG stream to file)
|
|
1182
1266
|
if (filepath && !needsSharpProcessing) {
|
|
1183
1267
|
const backgroundOperation = (async () => {
|
|
1184
1268
|
try {
|
|
1185
1269
|
const fileOpStartTime = performance.now();
|
|
1186
|
-
|
|
1270
|
+
|
|
1187
1271
|
emitLog("info", `💾 [BACKGROUND] Starting direct save to: ${filepath}`, "screencap");
|
|
1188
|
-
|
|
1272
|
+
|
|
1189
1273
|
// Direct pipe: ADB PNG stream -> File (no Sharp processing)
|
|
1190
1274
|
const directPipeStartTime = performance.now();
|
|
1191
1275
|
const writeStream = createWriteStream(filepath);
|
|
1192
|
-
|
|
1276
|
+
|
|
1193
1277
|
// Store the raw PNG stream for user access
|
|
1194
1278
|
lastScreencapData = screencapStream;
|
|
1195
|
-
|
|
1279
|
+
|
|
1196
1280
|
emitLog("info", `🔄 [BACKGROUND] Piping stream to file...`, "screencap");
|
|
1197
1281
|
screencapStream.pipe(writeStream);
|
|
1198
|
-
|
|
1282
|
+
|
|
1199
1283
|
emitLog("info", `⏳ [BACKGROUND] Waiting for write stream to finish...`, "screencap");
|
|
1200
1284
|
await new Promise((resolve, reject) => {
|
|
1201
|
-
writeStream.on(
|
|
1285
|
+
writeStream.on("finish", () => {
|
|
1202
1286
|
emitLog("info", `✅ [BACKGROUND] Write stream finished for ${filepath}`, "screencap");
|
|
1203
1287
|
resolve();
|
|
1204
1288
|
});
|
|
1205
|
-
writeStream.on(
|
|
1289
|
+
writeStream.on("error", (err) => {
|
|
1206
1290
|
emitLog("error", `❌ [BACKGROUND] Write stream error for ${filepath}: ${err.message}`, "screencap");
|
|
1207
1291
|
reject(err);
|
|
1208
1292
|
});
|
|
1209
1293
|
});
|
|
1210
|
-
|
|
1294
|
+
|
|
1211
1295
|
const directPipeTime = performance.now() - directPipeStartTime;
|
|
1212
1296
|
const totalFileOpTime = performance.now() - fileOpStartTime;
|
|
1213
1297
|
const totalTime = performance.now() - startTime;
|
|
1214
|
-
|
|
1298
|
+
|
|
1215
1299
|
emitLog("info", `✅ Screenshot saved directly to ${filepath}`, "screencap");
|
|
1216
1300
|
emitLog("info", `⚡ Direct pipe timing: ${directPipeTime.toFixed(2)}ms (no Sharp processing!)`, "screencap");
|
|
1217
|
-
|
|
1301
|
+
|
|
1218
1302
|
// Emit saved event with timing
|
|
1219
1303
|
emitter.emit("screencap-saved", {
|
|
1220
1304
|
timestamp: new Date().toISOString(),
|
|
@@ -1226,7 +1310,7 @@ export default async function createRemote(config) {
|
|
|
1226
1310
|
total: totalTime
|
|
1227
1311
|
}
|
|
1228
1312
|
});
|
|
1229
|
-
|
|
1313
|
+
|
|
1230
1314
|
// Emit complete event
|
|
1231
1315
|
emitter.emit("screencap-complete", {
|
|
1232
1316
|
timestamp: new Date().toISOString(),
|
|
@@ -1239,7 +1323,6 @@ export default async function createRemote(config) {
|
|
|
1239
1323
|
total: totalTime
|
|
1240
1324
|
}
|
|
1241
1325
|
});
|
|
1242
|
-
|
|
1243
1326
|
} catch (saveError) {
|
|
1244
1327
|
emitLog("error", `💥 [BACKGROUND] Direct pipe error: ${saveError.message}`, "screencap");
|
|
1245
1328
|
localEmitError(saveError, "screencap", `Failed to save screenshot directly to ${filepath}: ${saveError.message}`);
|
|
@@ -1248,16 +1331,16 @@ export default async function createRemote(config) {
|
|
|
1248
1331
|
backgroundOperations.delete(backgroundOperation);
|
|
1249
1332
|
}
|
|
1250
1333
|
})();
|
|
1251
|
-
|
|
1334
|
+
|
|
1252
1335
|
backgroundOperations.add(backgroundOperation);
|
|
1253
1336
|
emitLog("info", "Direct PNG pipe started in background", "screencap");
|
|
1254
1337
|
return; // Don't return a stream when saving to file
|
|
1255
1338
|
}
|
|
1256
|
-
|
|
1339
|
+
|
|
1257
1340
|
if (!needsSharpProcessing && !filepath) {
|
|
1258
1341
|
// Store the raw PNG stream for user access
|
|
1259
1342
|
lastScreencapData = screencapStream;
|
|
1260
|
-
|
|
1343
|
+
|
|
1261
1344
|
// Return raw stream if no processing or file save needed
|
|
1262
1345
|
emitLog("info", "Raw PNG stream ready (no processing needed)", "screencap");
|
|
1263
1346
|
emitter.emit("screencap-ready", {
|
|
@@ -1272,7 +1355,7 @@ export default async function createRemote(config) {
|
|
|
1272
1355
|
});
|
|
1273
1356
|
return screencapStream;
|
|
1274
1357
|
}
|
|
1275
|
-
|
|
1358
|
+
|
|
1276
1359
|
// Process the image
|
|
1277
1360
|
emitLog("info", "Processing screenshot image...", "screencap");
|
|
1278
1361
|
emitter.emit("screencap-processing", {
|
|
@@ -1281,67 +1364,65 @@ export default async function createRemote(config) {
|
|
|
1281
1364
|
height,
|
|
1282
1365
|
filepath
|
|
1283
1366
|
});
|
|
1284
|
-
|
|
1367
|
+
|
|
1285
1368
|
const processStartTime = performance.now();
|
|
1286
|
-
|
|
1369
|
+
|
|
1287
1370
|
// Create Sharp transform pipeline
|
|
1288
1371
|
let sharpTransform = sharp();
|
|
1289
|
-
|
|
1372
|
+
|
|
1290
1373
|
// Apply resizing if width or height specified
|
|
1291
1374
|
if (width || height) {
|
|
1292
1375
|
const resizeOptions = {
|
|
1293
|
-
fit:
|
|
1376
|
+
fit: "inside", // Maintain aspect ratio
|
|
1294
1377
|
withoutEnlargement: true // Don't upscale
|
|
1295
1378
|
};
|
|
1296
1379
|
if (width) resizeOptions.width = width;
|
|
1297
1380
|
if (height) resizeOptions.height = height;
|
|
1298
|
-
|
|
1381
|
+
|
|
1299
1382
|
sharpTransform = sharpTransform.resize(resizeOptions);
|
|
1300
|
-
emitLog("info", `Resizing to ${width ||
|
|
1383
|
+
emitLog("info", `Resizing to ${width || "auto"}x${height || "auto"}`, "screencap");
|
|
1301
1384
|
}
|
|
1302
|
-
|
|
1385
|
+
|
|
1303
1386
|
// Ensure PNG format with fastest compression settings
|
|
1304
|
-
sharpTransform = sharpTransform.png({
|
|
1387
|
+
sharpTransform = sharpTransform.png({
|
|
1305
1388
|
compressionLevel: 1, // Fastest compression (0-9, lower = faster)
|
|
1306
|
-
progressive: false
|
|
1389
|
+
progressive: false // Disable progressive encoding
|
|
1307
1390
|
});
|
|
1308
|
-
|
|
1391
|
+
|
|
1309
1392
|
if (filepath) {
|
|
1310
1393
|
// Background process handles saving and updates lastScreencapData when done
|
|
1311
1394
|
const backgroundOperation = (async () => {
|
|
1312
1395
|
try {
|
|
1313
1396
|
const fileOpStartTime = performance.now();
|
|
1314
|
-
const writeStreamStartTime = performance.now();
|
|
1315
1397
|
const writeStream = createWriteStream(filepath);
|
|
1316
|
-
|
|
1317
|
-
|
|
1398
|
+
|
|
1318
1399
|
// Process and save data, collecting it for lastScreencapData
|
|
1319
1400
|
const chunks = [];
|
|
1320
1401
|
const processedStream = screencapStream.pipe(sharpTransform);
|
|
1321
|
-
|
|
1402
|
+
|
|
1322
1403
|
await new Promise((resolve, reject) => {
|
|
1323
|
-
processedStream.on(
|
|
1404
|
+
processedStream.on("data", (chunk) => {
|
|
1324
1405
|
chunks.push(chunk);
|
|
1325
1406
|
writeStream.write(chunk);
|
|
1326
1407
|
});
|
|
1327
|
-
|
|
1328
|
-
processedStream.on(
|
|
1408
|
+
|
|
1409
|
+
processedStream.on("end", () => {
|
|
1329
1410
|
writeStream.end();
|
|
1330
1411
|
// Save processed data to lastScreencapData when done
|
|
1331
1412
|
lastScreencapData = Buffer.concat(chunks);
|
|
1332
|
-
|
|
1413
|
+
|
|
1333
1414
|
const totalFileOpTime = performance.now() - fileOpStartTime;
|
|
1334
1415
|
const totalTime = performance.now() - startTime;
|
|
1335
|
-
|
|
1416
|
+
|
|
1336
1417
|
emitLog("info", `✅ Screenshot saved to ${filepath}`, "screencap");
|
|
1337
1418
|
emitLog("info", `⏱️ File save timing: ${totalFileOpTime.toFixed(2)}ms, Total: ${totalTime.toFixed(2)}ms`, "screencap");
|
|
1338
|
-
|
|
1419
|
+
|
|
1339
1420
|
emitter.emit("screencap-saved", {
|
|
1340
1421
|
timestamp: new Date().toISOString(),
|
|
1341
1422
|
filepath,
|
|
1342
1423
|
timing: { fileOperation: totalFileOpTime, total: totalTime }
|
|
1343
1424
|
});
|
|
1344
|
-
|
|
1425
|
+
|
|
1345
1426
|
emitter.emit("screencap-complete", {
|
|
1346
1427
|
timestamp: new Date().toISOString(),
|
|
1347
1428
|
filepath,
|
|
@@ -1350,26 +1431,27 @@ export default async function createRemote(config) {
|
|
|
1350
1431
|
height,
|
|
1351
1432
|
timing: { fileOperation: totalFileOpTime, total: totalTime }
|
|
1352
1433
|
});
|
|
1353
|
-
|
|
1434
|
+
|
|
1354
1435
|
resolve();
|
|
1355
1436
|
});
|
|
1356
|
-
|
|
1357
|
-
processedStream.on(
|
|
1437
|
+
|
|
1438
|
+
processedStream.on("error", (error) => {
|
|
1358
1439
|
reject(error);
|
|
1359
1440
|
});
|
|
1360
1441
|
});
|
|
1361
|
-
|
|
1362
1442
|
} catch (saveError) {
|
|
1363
1443
|
// Show full error details for debugging
|
|
1364
1444
|
emitLog("error", `💥 [BACKGROUND SHARP] File save error: ${saveError.message}`, "screencap");
|
|
1365
1445
|
emitLog("error", `💥 [BACKGROUND SHARP] Error stack: ${saveError.stack}`, "screencap");
|
|
1366
|
-
|
|
1446
|
+
|
|
1367
1447
|
// Only suppress specific libspng/PNG processing errors that occur after disconnection
|
|
1368
|
-
if (
|
|
1369
|
-
|
|
1370
|
-
saveError.message
|
|
1371
|
-
saveError.message.includes(
|
|
1372
|
-
|
|
1448
|
+
if (
|
|
1449
|
+
!connected &&
|
|
1450
|
+
saveError.message &&
|
|
1451
|
+
(saveError.message.includes("libspng") ||
|
|
1452
|
+
saveError.message.includes("pngload_buffer") ||
|
|
1453
|
+
saveError.message.includes("read error"))
|
|
1454
|
+
) {
|
|
1373
1455
|
emitLog("debug", `PNG processing error after disconnection (suppressed): ${filepath}`, "screencap");
|
|
1374
1456
|
} else {
|
|
1375
1457
|
// Emit all other errors with full details
|
|
@@ -1381,31 +1463,42 @@ export default async function createRemote(config) {
|
|
|
1381
1463
|
backgroundOperations.delete(backgroundOperation);
|
|
1382
1464
|
}
|
|
1383
1465
|
})();
|
|
1384
|
-
|
|
1466
|
+
|
|
1385
1467
|
// Track the background operation
|
|
1386
|
-
backgroundOperations.add(backgroundOperation);
|
|
1468
|
+
backgroundOperations.add(backgroundOperation);
|
|
1387
1469
|
emitLog("info", "Screenshot processing started in background", "screencap");
|
|
1388
1470
|
return; // Don't return a stream when saving to file
|
|
1389
|
-
|
|
1390
1471
|
} else {
|
|
1391
1472
|
// Return processed stream
|
|
1392
1473
|
const processedStream = screencapStream.pipe(sharpTransform);
|
|
1393
1474
|
const processEndTime = performance.now();
|
|
1394
|
-
|
|
1475
|
+
|
|
1395
1476
|
const streamCaptureTime = captureTime - startTime;
|
|
1396
1477
|
const sharpProcessTime = processEndTime - processStartTime;
|
|
1397
1478
|
const totalTime = processEndTime - startTime;
|
|
1398
|
-
|
|
1479
|
+
|
|
1399
1480
|
// Debug timing variables
|
|
1400
|
-
emitLog(
|
|
1401
|
-
|
|
1481
|
+
emitLog(
|
|
1482
|
+
"debug",
|
|
1483
|
+
`🔍 Timing variables: captureTime=${captureTime}, startTime=${startTime}, processEndTime=${processEndTime}, processStartTime=${processStartTime}`,
|
|
1484
|
+
"screencap"
|
|
1485
|
+
);
|
|
1486
|
+
|
|
1402
1487
|
emitLog("info", `PNG stream processed and ready`, "screencap");
|
|
1403
1488
|
try {
|
|
1404
|
-
emitLog(
|
|
1489
|
+
emitLog(
|
|
1490
|
+
"info",
|
|
1491
|
+
`⏱️ Timing breakdown: Capture=${streamCaptureTime.toFixed(2)}ms, Sharp=${sharpProcessTime.toFixed(2)}ms, Total=${totalTime.toFixed(2)}ms`,
|
|
1492
|
+
"screencap"
|
|
1493
|
+
);
|
|
1405
1494
|
} catch (timingError) {
|
|
1406
|
-
emitLog(
|
|
1495
|
+
emitLog(
|
|
1496
|
+
"error",
|
|
1497
|
+
`⚠️ Timing log error in stream processing: ${timingError.message}. Variables: streamCaptureTime=${streamCaptureTime}, sharpProcessTime=${sharpProcessTime}, totalTime=${totalTime}`,
|
|
1498
|
+
"screencap"
|
|
1499
|
+
);
|
|
1407
1500
|
}
|
|
1408
|
-
|
|
1501
|
+
|
|
1409
1502
|
// Emit ready event
|
|
1410
1503
|
emitter.emit("screencap-ready", {
|
|
1411
1504
|
timestamp: new Date().toISOString(),
|
|
@@ -1418,7 +1511,7 @@ export default async function createRemote(config) {
|
|
|
1418
1511
|
total: totalTime
|
|
1419
1512
|
}
|
|
1420
1513
|
});
|
|
1421
|
-
|
|
1514
|
+
|
|
1422
1515
|
// Emit complete event
|
|
1423
1516
|
emitter.emit("screencap-complete", {
|
|
1424
1517
|
timestamp: new Date().toISOString(),
|
|
@@ -1431,16 +1524,15 @@ export default async function createRemote(config) {
|
|
|
1431
1524
|
total: totalTime
|
|
1432
1525
|
}
|
|
1433
1526
|
});
|
|
1434
|
-
|
|
1527
|
+
|
|
1435
1528
|
// Store the processed stream for user access
|
|
1436
1529
|
lastScreencapData = processedStream;
|
|
1437
|
-
|
|
1530
|
+
|
|
1438
1531
|
return processedStream;
|
|
1439
1532
|
}
|
|
1440
|
-
|
|
1441
1533
|
} catch (error) {
|
|
1442
1534
|
localEmitError(error, "screencap", "Failed to capture screenshot");
|
|
1443
|
-
|
|
1535
|
+
|
|
1444
1536
|
// Provide helpful error messages for common issues
|
|
1445
1537
|
if (error.message && error.message.includes("unauthorized")) {
|
|
1446
1538
|
emitLog("error", "Device unauthorized - ensure ADB debugging is enabled and device is authorized", "screencap");
|
|
@@ -1451,7 +1543,7 @@ export default async function createRemote(config) {
|
|
|
1451
1543
|
} else if (error.message && error.message.includes("ENOENT")) {
|
|
1452
1544
|
emitLog("error", `File path error: ${error.message}`, "screencap");
|
|
1453
1545
|
}
|
|
1454
|
-
|
|
1546
|
+
|
|
1455
1547
|
throw error;
|
|
1456
1548
|
}
|
|
1457
1549
|
},
|
|
@@ -1475,13 +1567,13 @@ export default async function createRemote(config) {
|
|
|
1475
1567
|
* @example
|
|
1476
1568
|
* // Basic thumbnail with default 240px width
|
|
1477
1569
|
* const thumbnailStream = await remote.thumbnail();
|
|
1478
|
-
*
|
|
1570
|
+
*
|
|
1479
1571
|
* // Thumbnail with custom dimensions
|
|
1480
1572
|
* const customThumb = await remote.thumbnail({ width: 320, height: 180 });
|
|
1481
|
-
*
|
|
1573
|
+
*
|
|
1482
1574
|
* // Thumbnail saved to file
|
|
1483
1575
|
* await remote.thumbnail({ filepath: './thumb.png' });
|
|
1484
|
-
*
|
|
1576
|
+
*
|
|
1485
1577
|
* // Thumbnail with custom size and file save
|
|
1486
1578
|
* await remote.thumbnail({ width: 160, height: 90, filepath: './small-thumb.png' });
|
|
1487
1579
|
*/
|
|
@@ -1491,9 +1583,9 @@ export default async function createRemote(config) {
|
|
|
1491
1583
|
width: 240,
|
|
1492
1584
|
...options
|
|
1493
1585
|
};
|
|
1494
|
-
|
|
1495
|
-
emitLog("info", `📸 Taking thumbnail screenshot (${thumbnailOptions.width}x${thumbnailOptions.height ||
|
|
1496
|
-
|
|
1586
|
+
|
|
1587
|
+
emitLog("info", `📸 Taking thumbnail screenshot (${thumbnailOptions.width}x${thumbnailOptions.height || "auto"})`, "thumbnail");
|
|
1588
|
+
|
|
1497
1589
|
// Call screencap with thumbnail options
|
|
1498
1590
|
return await this.screencap(thumbnailOptions);
|
|
1499
1591
|
},
|
|
@@ -1512,23 +1604,22 @@ export default async function createRemote(config) {
|
|
|
1512
1604
|
async waitBootComplete(timeout = 60000) {
|
|
1513
1605
|
try {
|
|
1514
1606
|
emitLog("info", "=== Waiting for device boot completion ===", "waitBootComplete");
|
|
1515
|
-
|
|
1607
|
+
|
|
1516
1608
|
// Ensure we're connected before monitoring boot status
|
|
1517
1609
|
await ensureConnected();
|
|
1518
|
-
|
|
1610
|
+
|
|
1519
1611
|
emitLog("info", `Monitoring boot status (timeout: ${timeout}ms)`, "waitBootComplete");
|
|
1520
|
-
|
|
1612
|
+
|
|
1521
1613
|
// Use the native device.waitBootComplete() method
|
|
1522
1614
|
const result = await device.waitBootComplete();
|
|
1523
|
-
|
|
1615
|
+
|
|
1524
1616
|
emitLog("info", "✅ Device boot completed successfully", "waitBootComplete");
|
|
1525
1617
|
emitLog("info", "Device is now ready for operations", "waitBootComplete");
|
|
1526
1618
|
|
|
1527
1619
|
return result;
|
|
1528
|
-
|
|
1529
1620
|
} catch (error) {
|
|
1530
1621
|
emitError(error, "waitBootComplete", "Failed while waiting for boot completion");
|
|
1531
|
-
|
|
1622
|
+
|
|
1532
1623
|
// Provide helpful error messages for common issues
|
|
1533
1624
|
if (error.message && error.message.includes("unauthorized")) {
|
|
1534
1625
|
emitLog("error", "Device unauthorized - ensure ADB debugging is enabled and device is authorized", "waitBootComplete");
|
|
@@ -1539,7 +1630,7 @@ export default async function createRemote(config) {
|
|
|
1539
1630
|
} else if (error.message && error.message.includes("connection")) {
|
|
1540
1631
|
emitLog("error", "Connection lost while waiting for boot completion", "waitBootComplete");
|
|
1541
1632
|
}
|
|
1542
|
-
|
|
1633
|
+
|
|
1543
1634
|
throw error;
|
|
1544
1635
|
}
|
|
1545
1636
|
},
|
|
@@ -1742,7 +1833,7 @@ export default async function createRemote(config) {
|
|
|
1742
1833
|
Object.keys(keyboardKeys).forEach((keyName) => {
|
|
1743
1834
|
const char = keyboardKeys[keyName];
|
|
1744
1835
|
const shiftedChar = getShiftedCharacter(keyName, char);
|
|
1745
|
-
|
|
1836
|
+
|
|
1746
1837
|
// Only create shift function if the character actually changes when shifted
|
|
1747
1838
|
if (shiftedChar !== char) {
|
|
1748
1839
|
/**
|
|
@@ -1785,10 +1876,10 @@ export default async function createRemote(config) {
|
|
|
1785
1876
|
* @public
|
|
1786
1877
|
* @param {RemoteConfig} config - Configuration for the remote.
|
|
1787
1878
|
* @returns {Promise<Remote>} Resolves with a ready remote instance.
|
|
1788
|
-
*
|
|
1879
|
+
*
|
|
1789
1880
|
* @description
|
|
1790
1881
|
* Alias for createRemote() - both functions now return ready-to-use remote instances.
|
|
1791
|
-
*
|
|
1882
|
+
*
|
|
1792
1883
|
* @example
|
|
1793
1884
|
* // ESM with event handling
|
|
1794
1885
|
* const remote = await createAndroidTVRemote({ ip: "192.168.1.100" });
|