Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 02f5bd59 authored by Simon Chan's avatar Simon Chan
Browse files

feat(adb-scrcpy): add aliases for all AdbScrcpyOptions versions

parent 7ad568d1
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
---
"@yume-chan/adb-scrcpy": minor
"@yume-chan/scrcpy": minor
---

Add alias for all AdbScrcpyOptions versions
+29 −0
Original line number Diff line number Diff line
import type { Adb } from "@yume-chan/adb";
import type { ScrcpyOptions1_15 } from "@yume-chan/scrcpy";

import type {
    AdbScrcpyConnection,
    AdbScrcpyConnectionOptions,
} from "../../connection.js";
import {
    AdbScrcpyForwardConnection,
    AdbScrcpyReverseConnection,
} from "../../connection.js";

export function createConnection(
    adb: Adb,
    options: Pick<ScrcpyOptions1_15.Init, "tunnelForward">,
): AdbScrcpyConnection {
    const connectionOptions: AdbScrcpyConnectionOptions = {
        scid: undefined, // Not Supported
        video: true, // Always enabled
        audio: false, // Not Supported
        control: true, // Always enabled even when `--no-control` is specified
        sendDummyByte: true, // Always enabled
    };
    if (options.tunnelForward) {
        return new AdbScrcpyForwardConnection(adb, connectionOptions);
    } else {
        return new AdbScrcpyReverseConnection(adb, connectionOptions);
    }
}
+38 −0
Original line number Diff line number Diff line
import type { Adb } from "@yume-chan/adb";
import type { ScrcpyDisplay, ScrcpyOptions1_15 } from "@yume-chan/scrcpy";

import { AdbScrcpyClient, AdbScrcpyExitedError } from "../../client.js";
import type { AdbScrcpyOptions } from "../../types.js";

export async function getDisplays(
    adb: Adb,
    path: string,
    options: AdbScrcpyOptions<Pick<ScrcpyOptions1_15.Init, "tunnelForward">>,
): Promise<ScrcpyDisplay[]> {
    try {
        // Server will exit before opening connections when an invalid display id was given
        // so `start` will throw an `AdbScrcpyExitedError`
        const client = await AdbScrcpyClient.start(adb, path, options);

        // If the server didn't exit, manually stop it and throw an error
        await client.close();
        throw new Error("Unexpected server output");
    } catch (e) {
        if (e instanceof AdbScrcpyExitedError) {
            if (e.output[0]?.startsWith("[server] ERROR:")) {
                throw e;
            }

            const displays: ScrcpyDisplay[] = [];
            for (const line of e.output) {
                const display = options.parseDisplay(line);
                if (display) {
                    displays.push(display);
                }
            }
            return displays;
        }

        throw e;
    }
}
+24 −0
Original line number Diff line number Diff line
import type { Adb } from "@yume-chan/adb";
import type { ScrcpyEncoder, ScrcpyOptions1_15 } from "@yume-chan/scrcpy";

import { AdbScrcpyClient } from "../../client.js";
import type { AdbScrcpyOptions } from "../../types.js";

export async function getEncoders(
    adb: Adb,
    path: string,
    options: AdbScrcpyOptions<Pick<ScrcpyOptions1_15.Init, "tunnelForward">>,
): Promise<ScrcpyEncoder[]> {
    const client = await AdbScrcpyClient.start(adb, path, options);

    const encoders: ScrcpyEncoder[] = [];

    for await (const line of client.stdout) {
        const encoder = options.parseEncoder(line);
        if (encoder) {
            encoders.push(encoder);
        }
    }

    return encoders;
}
+3 −0
Original line number Diff line number Diff line
export * from "./create-connection.js";
export * from "./get-displays.js";
export * from "./get-encoders.js";
Loading