@yume-chan/scrcpy
Version:
TypeScript implementation of Scrcpy client.
35 lines • 1.52 kB
JavaScript
import { getUint16BigEndian, getUint32BigEndian, } from "@yume-chan/no-data-view";
import { BufferedReadableStream } from "@yume-chan/stream-extra";
import { decodeUtf8 } from "@yume-chan/struct";
import { ScrcpyVideoCodecId } from "../../base/index.js";
/**
* Parse a fixed-length, null-terminated string.
* @param stream The stream to read from
* @param maxLength The maximum length of the string, including the null terminator, in bytes
* @returns The parsed string, without the null terminator
*/
export async function readString(stream, maxLength) {
const buffer = await stream.readExactly(maxLength);
// If null terminator is not found, `subarray(0, -1)` will remove the last byte
// But since it's a invalid case, it's fine
return decodeUtf8(buffer.subarray(0, buffer.indexOf(0)));
}
export async function readU16(stream) {
const buffer = await stream.readExactly(2);
return getUint16BigEndian(buffer, 0);
}
export async function readU32(stream) {
const buffer = await stream.readExactly(4);
return getUint32BigEndian(buffer, 0);
}
export async function parseVideoStreamMetadata(stream) {
const buffered = new BufferedReadableStream(stream);
const metadata = {
codec: ScrcpyVideoCodecId.H264,
};
metadata.deviceName = await readString(buffered, 64);
metadata.width = await readU16(buffered);
metadata.height = await readU16(buffered);
return { stream: buffered.release(), metadata };
}
//# sourceMappingURL=parse-video-stream-metadata.js.map