@yume-chan/scrcpy 0.0.15 → 0.0.17
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/CHANGELOG.json +33 -0
- package/CHANGELOG.md +18 -1
- package/LICENSE +21 -21
- package/README.md +327 -89
- package/esm/adb/client.d.ts +40 -0
- package/esm/adb/client.d.ts.map +1 -0
- package/esm/adb/client.js +218 -0
- package/esm/adb/client.js.map +1 -0
- package/esm/adb/connection.d.ts +38 -0
- package/esm/adb/connection.d.ts.map +1 -0
- package/esm/{connection.js → adb/connection.js} +20 -24
- package/esm/adb/connection.js.map +1 -0
- package/esm/adb/index.d.ts +4 -0
- package/esm/adb/index.d.ts.map +1 -0
- package/esm/adb/index.js +4 -0
- package/esm/adb/index.js.map +1 -0
- package/esm/adb/options/1_16.d.ts +8 -0
- package/esm/adb/options/1_16.d.ts.map +1 -0
- package/esm/adb/options/1_16.js +20 -0
- package/esm/adb/options/1_16.js.map +1 -0
- package/esm/adb/options/1_22.d.ts +8 -0
- package/esm/adb/options/1_22.d.ts.map +1 -0
- package/esm/adb/options/1_22.js +17 -0
- package/esm/adb/options/1_22.js.map +1 -0
- package/esm/adb/options/index.d.ts +4 -0
- package/esm/adb/options/index.d.ts.map +1 -0
- package/esm/adb/options/index.js +4 -0
- package/esm/adb/options/index.js.map +1 -0
- package/esm/adb/options/types.d.ts +23 -0
- package/esm/adb/options/types.d.ts.map +1 -0
- package/esm/adb/options/types.js +30 -0
- package/esm/adb/options/types.js.map +1 -0
- package/esm/codec.js +2 -2
- package/esm/codec.js.map +1 -1
- package/esm/control/index.d.ts +8 -0
- package/esm/control/index.d.ts.map +1 -0
- package/esm/control/index.js +8 -0
- package/esm/control/index.js.map +1 -0
- package/esm/control/inject-keycode.d.ts +53 -0
- package/esm/control/inject-keycode.d.ts.map +1 -0
- package/esm/control/inject-keycode.js +54 -0
- package/esm/control/inject-keycode.js.map +1 -0
- package/esm/control/inject-text.d.ts +9 -0
- package/esm/control/inject-text.d.ts.map +1 -0
- package/esm/control/inject-text.js +7 -0
- package/esm/control/inject-text.js.map +1 -0
- package/esm/control/inject-touch.d.ts +30 -0
- package/esm/control/inject-touch.d.ts.map +1 -0
- package/esm/control/inject-touch.js +30 -0
- package/esm/control/inject-touch.js.map +1 -0
- package/esm/control/rotate-device.d.ts +7 -0
- package/esm/control/rotate-device.d.ts.map +1 -0
- package/esm/control/rotate-device.js +5 -0
- package/esm/control/rotate-device.js.map +1 -0
- package/esm/control/serializer.d.ts +23 -0
- package/esm/control/serializer.d.ts.map +1 -0
- package/esm/control/serializer.js +73 -0
- package/esm/control/serializer.js.map +1 -0
- package/esm/control/set-screen-power-mode.d.ts +12 -0
- package/esm/control/set-screen-power-mode.d.ts.map +1 -0
- package/esm/control/set-screen-power-mode.js +12 -0
- package/esm/control/set-screen-power-mode.js.map +1 -0
- package/esm/control/type.d.ts +15 -0
- package/esm/control/type.d.ts.map +1 -0
- package/esm/control/type.js +19 -0
- package/esm/control/type.js.map +1 -0
- package/esm/decoder/index.d.ts +27 -3
- package/esm/decoder/index.d.ts.map +1 -1
- package/esm/decoder/index.js +1 -3
- package/esm/decoder/index.js.map +1 -1
- package/esm/decoder/tinyh264/index.d.ts +5 -5
- package/esm/decoder/tinyh264/index.d.ts.map +1 -1
- package/esm/decoder/tinyh264/index.js +4 -4
- package/esm/decoder/tinyh264/index.js.map +1 -1
- package/esm/decoder/tinyh264/worker.js.map +1 -1
- package/esm/decoder/tinyh264/wrapper.d.ts.map +1 -1
- package/esm/decoder/tinyh264/wrapper.js +14 -8
- package/esm/decoder/tinyh264/wrapper.js.map +1 -1
- package/esm/decoder/types.d.ts +1 -27
- package/esm/decoder/types.d.ts.map +1 -1
- package/esm/decoder/web-codecs/index.d.ts +4 -4
- package/esm/decoder/web-codecs/index.d.ts.map +1 -1
- package/esm/decoder/web-codecs/index.js +1 -1
- package/esm/decoder/web-codecs/index.js.map +1 -1
- package/esm/device-message/clipboard.d.ts +10 -0
- package/esm/device-message/clipboard.d.ts.map +1 -0
- package/esm/device-message/clipboard.js +7 -0
- package/esm/device-message/clipboard.js.map +1 -0
- package/esm/device-message/index.d.ts +4 -0
- package/esm/device-message/index.d.ts.map +1 -0
- package/esm/device-message/index.js +4 -0
- package/esm/device-message/index.js.map +1 -0
- package/esm/device-message/stream.d.ts +7 -0
- package/esm/device-message/stream.d.ts.map +1 -0
- package/esm/device-message/stream.js +16 -0
- package/esm/device-message/stream.js.map +1 -0
- package/esm/device-message/type.d.ts +5 -0
- package/esm/device-message/type.d.ts.map +1 -0
- package/esm/device-message/type.js +7 -0
- package/esm/device-message/type.js.map +1 -0
- package/esm/index.d.ts +3 -6
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -6
- package/esm/index.js.map +1 -1
- package/esm/options/1_16/codec-options.d.ts +19 -0
- package/esm/options/1_16/codec-options.d.ts.map +1 -0
- package/esm/options/1_16/codec-options.js +27 -0
- package/esm/options/1_16/codec-options.js.map +1 -0
- package/esm/options/1_16/index.d.ts +3 -107
- package/esm/options/1_16/index.d.ts.map +1 -1
- package/esm/options/1_16/index.js +3 -216
- package/esm/options/1_16/index.js.map +1 -1
- package/esm/options/1_16/options.d.ts +97 -0
- package/esm/options/1_16/options.d.ts.map +1 -0
- package/esm/options/1_16/options.js +188 -0
- package/esm/options/1_16/options.js.map +1 -0
- package/esm/options/1_18.d.ts +8 -8
- package/esm/options/1_18.d.ts.map +1 -1
- package/esm/options/1_18.js +6 -6
- package/esm/options/1_18.js.map +1 -1
- package/esm/options/1_21.d.ts +1 -1
- package/esm/options/1_21.d.ts.map +1 -1
- package/esm/options/1_21.js +1 -1
- package/esm/options/1_21.js.map +1 -1
- package/esm/options/1_22.d.ts +5 -8
- package/esm/options/1_22.d.ts.map +1 -1
- package/esm/options/1_22.js +4 -17
- package/esm/options/1_22.js.map +1 -1
- package/esm/options/1_23.d.ts +4 -4
- package/esm/options/1_23.d.ts.map +1 -1
- package/esm/options/1_23.js +1 -1
- package/esm/options/1_23.js.map +1 -1
- package/esm/options/1_24.d.ts +1 -1
- package/esm/options/1_24.d.ts.map +1 -1
- package/esm/options/1_24.js.map +1 -1
- package/esm/options/index.d.ts +1 -1
- package/esm/options/index.d.ts.map +1 -1
- package/esm/options/index.js +1 -1
- package/esm/options/index.js.map +1 -1
- package/esm/options/types.d.ts +45 -0
- package/esm/options/types.d.ts.map +1 -0
- package/esm/options/{common.js → types.js} +1 -1
- package/esm/options/types.js.map +1 -0
- package/package.json +24 -24
- package/scrcpy.LICENSE +203 -203
- package/scripts/fetch-server.cjs +28 -28
- package/src/adb/client.ts +292 -0
- package/src/{connection.ts → adb/connection.ts} +145 -148
- package/src/adb/index.ts +3 -0
- package/src/adb/options/1_16.ts +23 -0
- package/src/adb/options/1_22.ts +20 -0
- package/src/adb/options/index.ts +3 -0
- package/src/adb/options/types.ts +55 -0
- package/src/codec.ts +35 -35
- package/src/control/index.ts +7 -0
- package/src/control/inject-keycode.ts +59 -0
- package/src/control/inject-text.ts +12 -0
- package/src/control/inject-touch.ts +34 -0
- package/src/control/rotate-device.ts +10 -0
- package/src/control/serializer.ts +86 -0
- package/src/control/set-screen-power-mode.ts +17 -0
- package/src/control/type.ts +17 -0
- package/src/device-message/clipboard.ts +11 -0
- package/src/device-message/index.ts +3 -0
- package/src/device-message/stream.ts +19 -0
- package/src/device-message/type.ts +5 -0
- package/src/index.ts +5 -8
- package/src/options/1_16/codec-options.ts +48 -0
- package/src/options/1_16/index.ts +3 -330
- package/src/options/1_16/options.ts +294 -0
- package/src/options/1_16/sps.ts +300 -300
- package/src/options/1_18.ts +63 -61
- package/src/options/1_21.ts +44 -34
- package/src/options/1_22.ts +67 -78
- package/src/options/1_23.ts +7 -5
- package/src/options/1_24.ts +3 -2
- package/src/options/index.ts +7 -7
- package/src/options/{common.ts → types.ts} +85 -66
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.test.tsbuildinfo +1 -0
- package/esm/client.d.ts +0 -48
- package/esm/client.d.ts.map +0 -1
- package/esm/client.js +0 -341
- package/esm/client.js.map +0 -1
- package/esm/connection.d.ts +0 -37
- package/esm/connection.d.ts.map +0 -1
- package/esm/connection.js.map +0 -1
- package/esm/message.d.ts +0 -97
- package/esm/message.d.ts.map +0 -1
- package/esm/message.js +0 -97
- package/esm/message.js.map +0 -1
- package/esm/options/common.d.ts +0 -34
- package/esm/options/common.d.ts.map +0 -1
- package/esm/options/common.js.map +0 -1
- package/esm/push-server.d.ts +0 -6
- package/esm/push-server.d.ts.map +0 -1
- package/esm/push-server.js +0 -16
- package/esm/push-server.js.map +0 -1
- package/esm/utils.d.ts +0 -2
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -6
- package/esm/utils.js.map +0 -1
- package/src/client.ts +0 -435
- package/src/decoder/index.ts +0 -3
- package/src/decoder/tinyh264/index.ts +0 -112
- package/src/decoder/tinyh264/types.d.ts +0 -137
- package/src/decoder/tinyh264/worker.ts +0 -2
- package/src/decoder/tinyh264/wrapper.ts +0 -89
- package/src/decoder/types.ts +0 -35
- package/src/decoder/web-codecs/index.ts +0 -99
- package/src/message.ts +0 -112
- package/src/push-server.ts +0 -24
- package/src/utils.ts +0 -5
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { Adb, AdbSubprocessNoneProtocol, AdbSubprocessProtocol, AdbSync } from '@yume-chan/adb';
|
|
2
|
+
import { AbortController, DecodeUtf8Stream, InspectStream, ReadableStream, SplitStringStream, WrapWritableStream, WritableStream, type ReadableStreamDefaultController, type ReadableStreamDefaultReader, type ReadableWritablePair } from '@yume-chan/stream-extra';
|
|
3
|
+
|
|
4
|
+
import { ScrcpyControlMessageSerializer } from '../control/index.js';
|
|
5
|
+
import { ScrcpyDeviceMessageDeserializeStream, type ScrcpyDeviceMessage } from '../device-message/index.js';
|
|
6
|
+
import { DEFAULT_SERVER_PATH, type ScrcpyVideoStreamPacket } from '../options/index.js';
|
|
7
|
+
import type { AdbScrcpyOptions } from './options/index.js';
|
|
8
|
+
|
|
9
|
+
class ArrayToStream<T> extends ReadableStream<T>{
|
|
10
|
+
private array!: T[];
|
|
11
|
+
private index = 0;
|
|
12
|
+
|
|
13
|
+
constructor(array: T[]) {
|
|
14
|
+
super({
|
|
15
|
+
start: async () => {
|
|
16
|
+
await Promise.resolve();
|
|
17
|
+
this.array = array;
|
|
18
|
+
},
|
|
19
|
+
pull: (controller) => {
|
|
20
|
+
if (this.index < this.array.length) {
|
|
21
|
+
controller.enqueue(this.array[this.index]!);
|
|
22
|
+
this.index += 1;
|
|
23
|
+
} else {
|
|
24
|
+
controller.close();
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class ConcatStream<T> extends ReadableStream<T>{
|
|
32
|
+
private streams!: ReadableStream<T>[];
|
|
33
|
+
private index = 0;
|
|
34
|
+
private reader!: ReadableStreamDefaultReader<T>;
|
|
35
|
+
|
|
36
|
+
constructor(...streams: ReadableStream<T>[]) {
|
|
37
|
+
super({
|
|
38
|
+
start: async (controller) => {
|
|
39
|
+
await Promise.resolve();
|
|
40
|
+
|
|
41
|
+
this.streams = streams;
|
|
42
|
+
this.advance(controller);
|
|
43
|
+
},
|
|
44
|
+
pull: async (controller) => {
|
|
45
|
+
const result = await this.reader.read();
|
|
46
|
+
if (!result.done) {
|
|
47
|
+
controller.enqueue(result.value);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.advance(controller);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private advance(controller: ReadableStreamDefaultController<T>) {
|
|
56
|
+
if (this.index < this.streams.length) {
|
|
57
|
+
this.reader = this.streams[this.index]!.getReader();
|
|
58
|
+
this.index += 1;
|
|
59
|
+
} else {
|
|
60
|
+
controller.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class AdbScrcpyClient {
|
|
66
|
+
public static pushServer(
|
|
67
|
+
adb: Adb,
|
|
68
|
+
path = DEFAULT_SERVER_PATH,
|
|
69
|
+
) {
|
|
70
|
+
let sync!: AdbSync;
|
|
71
|
+
return new WrapWritableStream<Uint8Array>({
|
|
72
|
+
async start() {
|
|
73
|
+
sync = await adb.sync();
|
|
74
|
+
return sync.write(path);
|
|
75
|
+
},
|
|
76
|
+
async close() {
|
|
77
|
+
await sync.dispose();
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public static async start(
|
|
83
|
+
adb: Adb,
|
|
84
|
+
path: string,
|
|
85
|
+
version: string,
|
|
86
|
+
options: AdbScrcpyOptions<any>
|
|
87
|
+
) {
|
|
88
|
+
const connection = options.createConnection(adb);
|
|
89
|
+
let process: AdbSubprocessProtocol | undefined;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
await connection.initialize();
|
|
93
|
+
|
|
94
|
+
process = await adb.subprocess.spawn(
|
|
95
|
+
[
|
|
96
|
+
// cspell: disable-next-line
|
|
97
|
+
`CLASSPATH=${path}`,
|
|
98
|
+
'app_process',
|
|
99
|
+
/* unused */ '/',
|
|
100
|
+
'com.genymobile.scrcpy.Server',
|
|
101
|
+
version,
|
|
102
|
+
...options.formatServerArguments(),
|
|
103
|
+
],
|
|
104
|
+
{
|
|
105
|
+
// Scrcpy server doesn't use stderr,
|
|
106
|
+
// so disable Shell Protocol to simplify processing
|
|
107
|
+
protocols: [AdbSubprocessNoneProtocol],
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const stdout = process.stdout
|
|
112
|
+
.pipeThrough(new DecodeUtf8Stream())
|
|
113
|
+
.pipeThrough(new SplitStringStream('\n'));
|
|
114
|
+
|
|
115
|
+
// Read stdout, otherwise `process.exit` won't resolve.
|
|
116
|
+
const output: string[] = [];
|
|
117
|
+
const abortController = new AbortController();
|
|
118
|
+
const pipe = stdout
|
|
119
|
+
.pipeTo(new WritableStream({
|
|
120
|
+
write(chunk) {
|
|
121
|
+
output.push(chunk);
|
|
122
|
+
}
|
|
123
|
+
}), {
|
|
124
|
+
signal: abortController.signal,
|
|
125
|
+
preventCancel: true,
|
|
126
|
+
})
|
|
127
|
+
.catch(() => { });
|
|
128
|
+
|
|
129
|
+
const result = await Promise.race([
|
|
130
|
+
process.exit,
|
|
131
|
+
connection.getStreams(),
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
if (typeof result === 'number') {
|
|
135
|
+
const error = new Error('scrcpy server exited prematurely');
|
|
136
|
+
(error as any).output = output;
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
abortController.abort();
|
|
141
|
+
await pipe;
|
|
142
|
+
|
|
143
|
+
const [videoStream, controlStream] = result;
|
|
144
|
+
return new AdbScrcpyClient(
|
|
145
|
+
options,
|
|
146
|
+
process,
|
|
147
|
+
new ConcatStream(
|
|
148
|
+
new ArrayToStream(output),
|
|
149
|
+
stdout,
|
|
150
|
+
),
|
|
151
|
+
videoStream,
|
|
152
|
+
controlStream
|
|
153
|
+
);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
await process?.kill();
|
|
156
|
+
throw e;
|
|
157
|
+
} finally {
|
|
158
|
+
connection.dispose();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* This method will modify the given `options`,
|
|
164
|
+
* so don't reuse it elsewhere.
|
|
165
|
+
*/
|
|
166
|
+
public static async getEncoders(
|
|
167
|
+
adb: Adb,
|
|
168
|
+
path: string,
|
|
169
|
+
version: string,
|
|
170
|
+
options: AdbScrcpyOptions<any>
|
|
171
|
+
): Promise<string[]> {
|
|
172
|
+
// Provide an invalid encoder name
|
|
173
|
+
// So the server will return all available encoders
|
|
174
|
+
options.value.encoderName = '_';
|
|
175
|
+
// Disable control for faster connection in 1.22+
|
|
176
|
+
options.value.control = false;
|
|
177
|
+
options.value.sendDeviceMeta = false;
|
|
178
|
+
options.value.sendDummyByte = false;
|
|
179
|
+
|
|
180
|
+
// Scrcpy server will open connections, before initializing encoder
|
|
181
|
+
// Thus although an invalid encoder name is given, the start process will success
|
|
182
|
+
const client = await AdbScrcpyClient.start(
|
|
183
|
+
adb,
|
|
184
|
+
path,
|
|
185
|
+
version,
|
|
186
|
+
options
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const encoderNameRegex = options.getOutputEncoderNameRegex();
|
|
190
|
+
const encoders: string[] = [];
|
|
191
|
+
await client.stdout.pipeTo(new WritableStream({
|
|
192
|
+
write(line) {
|
|
193
|
+
const match = line.match(encoderNameRegex);
|
|
194
|
+
if (match) {
|
|
195
|
+
encoders.push(match[1]!);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
}));
|
|
199
|
+
|
|
200
|
+
return encoders;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* This method will modify the given `options`,
|
|
205
|
+
* so don't reuse it elsewhere.
|
|
206
|
+
*/
|
|
207
|
+
public static async getDisplays(
|
|
208
|
+
adb: Adb,
|
|
209
|
+
path: string,
|
|
210
|
+
version: string,
|
|
211
|
+
options: AdbScrcpyOptions<any>
|
|
212
|
+
): Promise<number[]> {
|
|
213
|
+
// Similar to `getEncoders`, pass an invalid option and parse the output
|
|
214
|
+
options.value.displayId = -1;
|
|
215
|
+
|
|
216
|
+
options.value.control = false;
|
|
217
|
+
options.value.sendDeviceMeta = false;
|
|
218
|
+
options.value.sendDummyByte = false;
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
// Server will exit before opening connections when an invalid display id was given.
|
|
222
|
+
await AdbScrcpyClient.start(adb, path, version, options);
|
|
223
|
+
} catch (e) {
|
|
224
|
+
if (e instanceof Error) {
|
|
225
|
+
const output = (e as any).output as string[];
|
|
226
|
+
|
|
227
|
+
const displayIdRegex = /\s+scrcpy --display (\d+)/;
|
|
228
|
+
const displays: number[] = [];
|
|
229
|
+
for (const line of output) {
|
|
230
|
+
const match = line.match(displayIdRegex);
|
|
231
|
+
if (match) {
|
|
232
|
+
displays.push(Number.parseInt(match[1]!, 10));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return displays;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
throw new Error('failed to get displays');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private process: AdbSubprocessProtocol;
|
|
243
|
+
|
|
244
|
+
private _stdout: ReadableStream<string>;
|
|
245
|
+
public get stdout() { return this._stdout; }
|
|
246
|
+
|
|
247
|
+
public get exit() { return this.process.exit; }
|
|
248
|
+
|
|
249
|
+
private _screenWidth: number | undefined;
|
|
250
|
+
public get screenWidth() { return this._screenWidth; }
|
|
251
|
+
|
|
252
|
+
private _screenHeight: number | undefined;
|
|
253
|
+
public get screenHeight() { return this._screenHeight; }
|
|
254
|
+
|
|
255
|
+
private _videoStream: ReadableStream<ScrcpyVideoStreamPacket>;
|
|
256
|
+
public get videoStream() { return this._videoStream; }
|
|
257
|
+
|
|
258
|
+
private _controlMessageSerializer: ScrcpyControlMessageSerializer | undefined;
|
|
259
|
+
public get controlMessageSerializer() { return this._controlMessageSerializer; }
|
|
260
|
+
|
|
261
|
+
private _deviceMessageStream: ReadableStream<ScrcpyDeviceMessage> | undefined;
|
|
262
|
+
public get deviceMessageStream() { return this._deviceMessageStream; }
|
|
263
|
+
|
|
264
|
+
public constructor(
|
|
265
|
+
options: AdbScrcpyOptions<any>,
|
|
266
|
+
process: AdbSubprocessProtocol,
|
|
267
|
+
stdout: ReadableStream<string>,
|
|
268
|
+
videoStream: ReadableStream<Uint8Array>,
|
|
269
|
+
controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined,
|
|
270
|
+
) {
|
|
271
|
+
this.process = process;
|
|
272
|
+
this._stdout = stdout;
|
|
273
|
+
|
|
274
|
+
this._videoStream = videoStream
|
|
275
|
+
.pipeThrough(options.createVideoStreamTransformer())
|
|
276
|
+
.pipeThrough(new InspectStream(packet => {
|
|
277
|
+
if (packet.type === 'configuration') {
|
|
278
|
+
this._screenWidth = packet.data.croppedWidth;
|
|
279
|
+
this._screenHeight = packet.data.croppedHeight;
|
|
280
|
+
}
|
|
281
|
+
}));
|
|
282
|
+
|
|
283
|
+
if (controlStream) {
|
|
284
|
+
this._controlMessageSerializer = new ScrcpyControlMessageSerializer(controlStream.writable, options);
|
|
285
|
+
this._deviceMessageStream = controlStream.readable.pipeThrough(new ScrcpyDeviceMessageDeserializeStream());
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
public async close() {
|
|
290
|
+
await this.process.kill();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -1,148 +1,145 @@
|
|
|
1
|
-
import type { Adb
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.
|
|
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
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if
|
|
77
|
-
|
|
78
|
-
const
|
|
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
|
-
this.device.reverse.remove(this.address);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
1
|
+
import type { Adb } from '@yume-chan/adb';
|
|
2
|
+
import { delay } from '@yume-chan/async';
|
|
3
|
+
import type { Disposable } from '@yume-chan/event';
|
|
4
|
+
import { TransformStream, type ReadableStream, type ReadableStreamDefaultReader, type ReadableWritablePair } from '@yume-chan/stream-extra';
|
|
5
|
+
import type { ValueOrPromise } from '@yume-chan/struct';
|
|
6
|
+
|
|
7
|
+
export interface AdbScrcpyConnectionOptions {
|
|
8
|
+
control: boolean;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Write a byte on start to detect connection issues
|
|
12
|
+
*/
|
|
13
|
+
sendDummyByte: boolean;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Send device name and size
|
|
17
|
+
*/
|
|
18
|
+
sendDeviceMeta: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export abstract class AdbScrcpyConnection implements Disposable {
|
|
22
|
+
protected adb: Adb;
|
|
23
|
+
|
|
24
|
+
protected options: AdbScrcpyConnectionOptions;
|
|
25
|
+
|
|
26
|
+
public constructor(adb: Adb, options: AdbScrcpyConnectionOptions) {
|
|
27
|
+
this.adb = adb;
|
|
28
|
+
this.options = options;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public initialize(): ValueOrPromise<void> { }
|
|
32
|
+
|
|
33
|
+
public abstract getStreams(): ValueOrPromise<[videoSteam: ReadableStream<Uint8Array>, controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined]>;
|
|
34
|
+
|
|
35
|
+
public dispose(): void { }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class AdbScrcpyForwardConnection extends AdbScrcpyConnection {
|
|
39
|
+
private connect(): Promise<ReadableWritablePair<Uint8Array, Uint8Array>> {
|
|
40
|
+
return this.adb.createSocket('localabstract:scrcpy');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private async connectAndRetry(): Promise<ReadableWritablePair<Uint8Array, Uint8Array>> {
|
|
44
|
+
for (let i = 0; i < 100; i++) {
|
|
45
|
+
try {
|
|
46
|
+
return await this.connect();
|
|
47
|
+
} catch (e) {
|
|
48
|
+
await delay(100);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw new Error(`Can't connect to server after 100 retries`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async connectVideoStream(): Promise<ReadableStream<Uint8Array>> {
|
|
55
|
+
const { readable: videoStream } = await this.connectAndRetry();
|
|
56
|
+
if (this.options.sendDummyByte) {
|
|
57
|
+
const reader = videoStream.getReader();
|
|
58
|
+
const { done, value } = await reader.read();
|
|
59
|
+
// server will write a `0` to signal connection success
|
|
60
|
+
if (done || value.byteLength !== 1 || value[0] !== 0) {
|
|
61
|
+
throw new Error('Unexpected response from server');
|
|
62
|
+
}
|
|
63
|
+
reader.releaseLock();
|
|
64
|
+
}
|
|
65
|
+
return videoStream;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public async getStreams(): Promise<[videoSteam: ReadableStream<Uint8Array>, controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined]> {
|
|
69
|
+
const videoStream = await this.connectVideoStream();
|
|
70
|
+
|
|
71
|
+
let controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined;
|
|
72
|
+
if (this.options.control) {
|
|
73
|
+
controlStream = await this.connectAndRetry();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Server only writes device meta after control socket is connected (if enabled)
|
|
77
|
+
if (this.options.sendDeviceMeta) {
|
|
78
|
+
const reader = videoStream.getReader();
|
|
79
|
+
const { done, value } = await reader.read();
|
|
80
|
+
// 64 bytes device name + 2 bytes video width + 2 bytes video height
|
|
81
|
+
if (done || value.byteLength !== 64 + 2 + 2) {
|
|
82
|
+
throw new Error('Unexpected response from server');
|
|
83
|
+
}
|
|
84
|
+
reader.releaseLock();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return [videoStream, controlStream];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class AdbScrcpyReverseConnection extends AdbScrcpyConnection {
|
|
92
|
+
private streams!: ReadableStreamDefaultReader<ReadableWritablePair<Uint8Array, Uint8Array>>;
|
|
93
|
+
|
|
94
|
+
private address!: string;
|
|
95
|
+
|
|
96
|
+
public override async initialize(): Promise<void> {
|
|
97
|
+
// try to unbind first, ignore errors
|
|
98
|
+
await this.adb.reverse.remove('localabstract:scrcpy').catch(() => { });
|
|
99
|
+
|
|
100
|
+
const queue = new TransformStream<ReadableWritablePair<Uint8Array, Uint8Array>>();
|
|
101
|
+
this.streams = queue.readable.getReader();
|
|
102
|
+
const writer = queue.writable.getWriter();
|
|
103
|
+
this.address = await this.adb.reverse.add(
|
|
104
|
+
'localabstract:scrcpy',
|
|
105
|
+
'tcp:27183',
|
|
106
|
+
socket => {
|
|
107
|
+
writer.write(socket);
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private async accept(): Promise<ReadableWritablePair<Uint8Array, Uint8Array>> {
|
|
114
|
+
return (await this.streams.read()).value!;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async getStreams(): Promise<[videoSteam: ReadableStream<Uint8Array>, controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined]> {
|
|
118
|
+
const { readable: videoStream } = await this.accept();
|
|
119
|
+
|
|
120
|
+
let controlStream: ReadableWritablePair<Uint8Array, Uint8Array> | undefined;
|
|
121
|
+
if (this.options.control) {
|
|
122
|
+
controlStream = await this.accept();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Server only writes device meta after control socket is connected (if enabled)
|
|
126
|
+
if (this.options.sendDeviceMeta) {
|
|
127
|
+
const reader = videoStream.getReader();
|
|
128
|
+
const { done, value } = await reader.read();
|
|
129
|
+
// 64 bytes device name + 2 bytes video width + 2 bytes video height
|
|
130
|
+
if (done || value.byteLength !== 64 + 2 + 2) {
|
|
131
|
+
throw new Error('Unexpected response from server');
|
|
132
|
+
}
|
|
133
|
+
reader.releaseLock();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return [videoStream, controlStream];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public override dispose() {
|
|
140
|
+
// Don't await this!
|
|
141
|
+
// `reverse.remove`'s response will never arrive
|
|
142
|
+
// before we read all pending data from `videoStream`
|
|
143
|
+
this.adb.reverse.remove(this.address);
|
|
144
|
+
}
|
|
145
|
+
}
|
package/src/adb/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Adb } from '@yume-chan/adb';
|
|
2
|
+
|
|
3
|
+
import type { ScrcpyOptionsInit1_16 } from '../../options/index.js';
|
|
4
|
+
import { AdbScrcpyConnection, AdbScrcpyConnectionOptions, AdbScrcpyForwardConnection, AdbScrcpyReverseConnection } from '../connection.js';
|
|
5
|
+
import { AdbScrcpyOptionsBase } from './types.js';
|
|
6
|
+
|
|
7
|
+
export class AdbScrcpyOptions1_16<T extends ScrcpyOptionsInit1_16 = ScrcpyOptionsInit1_16>
|
|
8
|
+
extends AdbScrcpyOptionsBase<T> {
|
|
9
|
+
public override createConnection(adb: Adb): AdbScrcpyConnection {
|
|
10
|
+
const options: AdbScrcpyConnectionOptions = {
|
|
11
|
+
// Old versions always have control stream no matter what the option is
|
|
12
|
+
// Pass `control: false` to `Connection` will disable the control stream
|
|
13
|
+
control: true,
|
|
14
|
+
sendDummyByte: true,
|
|
15
|
+
sendDeviceMeta: true,
|
|
16
|
+
};
|
|
17
|
+
if (this.value.tunnelForward) {
|
|
18
|
+
return new AdbScrcpyForwardConnection(adb, options);
|
|
19
|
+
} else {
|
|
20
|
+
return new AdbScrcpyReverseConnection(adb, options);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Adb } from '@yume-chan/adb';
|
|
2
|
+
|
|
3
|
+
import type { ScrcpyOptionsInit1_22 } from '../../options/index.js';
|
|
4
|
+
import { AdbScrcpyConnection, AdbScrcpyForwardConnection, AdbScrcpyReverseConnection } from '../connection.js';
|
|
5
|
+
import { AdbScrcpyOptions1_16 } from './1_16.js';
|
|
6
|
+
|
|
7
|
+
export class AdbScrcpyOptions1_22<T extends ScrcpyOptionsInit1_22 = ScrcpyOptionsInit1_22>
|
|
8
|
+
extends AdbScrcpyOptions1_16<T> {
|
|
9
|
+
public override createConnection(adb: Adb): AdbScrcpyConnection {
|
|
10
|
+
const options = {
|
|
11
|
+
...this.getDefaultValue(),
|
|
12
|
+
...this.value,
|
|
13
|
+
};
|
|
14
|
+
if (this.value.tunnelForward) {
|
|
15
|
+
return new AdbScrcpyForwardConnection(adb, options);
|
|
16
|
+
} else {
|
|
17
|
+
return new AdbScrcpyReverseConnection(adb, options);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|