@yume-chan/adb-scrcpy
Version:
Use `@yume-chan/adb` to bootstrap `@yume-chan/scrcpy`.
76 lines • 2.69 kB
JavaScript
import { StickyEventEmitter } from "@yume-chan/event";
import { Av1, h264ParseConfiguration, h265ParseConfiguration, ScrcpyVideoCodecId, } from "@yume-chan/scrcpy";
import { InspectStream } from "@yume-chan/stream-extra";
export class AdbScrcpyVideoStream {
#options;
#metadata;
get metadata() {
return this.#metadata;
}
#stream;
get stream() {
return this.#stream;
}
#sizeChanged = new StickyEventEmitter();
get sizeChanged() {
return this.#sizeChanged.event;
}
#width = 0;
get width() {
return this.#width;
}
#height = 0;
get height() {
return this.#height;
}
constructor(options, metadata, stream) {
this.#options = options;
this.#metadata = metadata;
this.#stream = stream
.pipeThrough(this.#options.createMediaStreamTransformer())
.pipeThrough(new InspectStream((packet) => {
if (packet.type === "configuration") {
switch (metadata.codec) {
case ScrcpyVideoCodecId.H264:
this.#configureH264(packet.data);
break;
case ScrcpyVideoCodecId.H265:
this.#configureH265(packet.data);
break;
case ScrcpyVideoCodecId.AV1:
// AV1 configuration is in data packet
break;
}
}
else if (metadata.codec === ScrcpyVideoCodecId.AV1) {
this.#configureAv1(packet.data);
}
}));
}
#configureH264(data) {
const { croppedWidth, croppedHeight } = h264ParseConfiguration(data);
this.#width = croppedWidth;
this.#height = croppedHeight;
this.#sizeChanged.fire({ width: croppedWidth, height: croppedHeight });
}
#configureH265(data) {
const { croppedWidth, croppedHeight } = h265ParseConfiguration(data);
this.#width = croppedWidth;
this.#height = croppedHeight;
this.#sizeChanged.fire({ width: croppedWidth, height: croppedHeight });
}
#configureAv1(data) {
const parser = new Av1(data);
const sequenceHeader = parser.searchSequenceHeaderObu();
if (!sequenceHeader) {
return;
}
const { max_frame_width_minus_1, max_frame_height_minus_1 } = sequenceHeader;
const width = max_frame_width_minus_1 + 1;
const height = max_frame_height_minus_1 + 1;
this.#width = width;
this.#height = height;
this.#sizeChanged.fire({ width, height });
}
}
//# sourceMappingURL=video.js.map