@@ -26,21 +31,21 @@ It's compatible with the official Scrcpy server binaries.
## Transport agnostic
Although it was initially written to use with `@yume-chan/adb`, the `ScrcpyClient` class can be used with any transportation. More details later.
It was initially designed to be used with `@yume-chan/adb`, but now it can also to used with any other transportation.
## Prepare server binary
Scrcpy needs a server binary running on the device in order to work. This package doesn't ship with one.
You can download the server binary from official releases (https://github.com/Genymobile/scrcpy/releases) yourself, or use the built-in `fetch-scrcpy-server` script to automate the process.
You can download the server binary from official releases (https://github.com/Genymobile/scrcpy/releases), or use the built-in `fetch-scrcpy-server` script to automate the process.
The server binary is subject to [Apache License 2.0](https://github.com/Genymobile/scrcpy/blob/master/LICENSE).
### `fetch-scrcpy-server`
To use the script, first add `gh-release-fetch@3` to your `devDependencies`. It's not automatically installed to minimize download size.
To use the script, first add `gh-release-fetch@3` to `devDependencies` of your `package.json`. It's an optional peer dependency fpr minimized download size.
Then you can execute it from terminal:
Then you can invoke it in a terminal:
```
$ npx fetch-scrcpy-server <version>
@@ -52,7 +57,7 @@ For example:
$ npx fetch-scrcpy-server 1.24
```
Or adding it to the `postinstall` script in `package.json`, so running `npm install` will automatically invoke the script.
You can also add it to the `postinstall` script in your `package.json`. After that, running `npm install` will automatically invoke the script.
```json
"scripts":{
@@ -60,11 +65,11 @@ Or adding it to the `postinstall` script in `package.json`, so running `npm inst
},
```
The server binary will be named `bin/scrcpy-server`.
The server binary will be named `bin/scrcpy-server` in this package's installation directory (usually in `node_modules`).
### Use the server binary
The server binary file needs to be embedded in your application, the exact method depends on your runtime.
The server binary file needs to be embedded into your application, the exact method depends on the runtime.
In future it should be possible to use `import.meta.resolve` (https://nodejs.org/api/esm.html#importmetaresolvespecifier-parent) instead.
Currently, ES Module doesn't have a `resolve` function like `require.resolve` in CommonJS, so `createRequire` is used to create a CommonJS resolver.
`import.meta.resolve` (https://github.com/whatwg/html/pull/5572) is a proposal that fills this gap. Node.js already has experimental support for it behind a flag. See https://nodejs.org/api/esm.html#importmetaresolvespecifier-parent for more information.
Scrcpy server options changes over time, and some of them are not backwards compatible. This package provides option types for each version (or range). Using wrong option version usually results in errors.
The latest one may continue to work for future server versions, but there is no guarantee.
| Version | Type |
| --------- | ------------------- |
| 1.16~1.17 | `ScrcpyOptions1_16` |
| 1.18~1.20 | `ScrcpyOptions1_18` |
| 1.21 | `ScrcpyOptions1_21` |
| 1.22 | `ScrcpyOptions1_22` |
| 1.23 | `ScrcpyOptions1_23` |
| 1.24 | `ScrcpyOptions1_24` |
When using `AdbScrcpyClient`, there is another `AdbScrcpyOptions` contains `@yume-chan/adb` related logics:
| Version | Type |
| --------- | ---------------------- |
| 1.16~1.21 | `AdbScrcpyOptions1_16` |
| 1.22~1.24 | `AdbScrcpyOptions1_22` |
## Use with `@yume-chan/adb`
The the server binary needs to be copied to the device and run on it.
### Using `@yume-chan/adb`
### Push server binary
The `Adb#sync()#write()` method can be used to push files to the device. Read more at `@yume-chan/adb`'s documentation (https://github.com/yume-chan/ya-webadb/tree/master/libraries/adb#readme).
This package also provides the `pushServer()` method as a shortcut for `Adb#sync().write()`, plus automatically close the `AdbSync` object when complete.
This package also provides the `AdbScrcpyClient.pushServer()`static method as a shortcut, plus it will automatically close the `AdbSync` object on completion.
The `WrapReadableStream` is required because native `ReadableStream`s can't `pipeTo()` non-native `WritableStream`s (`@yume-chan/adb` is using ponyfill from `web-streams-polyfill`)
### Start server on device
To start the server, use the `start()` method:
To start the server, use the `AdbScrcpyClient.start()` method. It automatically sets up port forwarding, launches the server, and connects to it.
Both `stdout` and`videoStream`must be continuously read, otherwise the connection will stall.
Any `ReadableStream` (`stdout` when using `AdbScrcpyClient`,`videoPacketStream`and `deviceMessageStream` when control is enabled) must be continuously read (even if you don't care about the data), otherwise the whole connection will stall.
The data from `videoStream` has two types: `configuration` and `frame`. How much parsed data is available depends on the server options.
The data from `videoPacketStream` has two types: `configuration` and `frame`. Some fields may not be populated depending on the server version and options.
When `sendFrameMeta: false` is set, `videoStream` only contains `frame` packets, and only the `data` field is available. It's commonly used when feeding into decoders like FFmpeg that can parse the H.264 stream itself, or saving to disk directly.
When `sendFrameMeta: false` is set, `videoPacketStream` only contains `frame` packets, and only the `data` field in it is available. It's commonly used when feeding into decoders like FFmpeg that can parse the H.264 stream itself, or saving to disk directly.
Otherwise, both `configuration` and `frame` packets are available.
@@ -283,10 +336,10 @@ There are two built-in decoders for using in Web Browsers:
General usage:
```ts
constdecoder=newH264Decoder();// `WebCodecsDecoder` or `TinyH264Decoder`