Skip to content

Commit

Permalink
feat(decoder): add sizeChanged event
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Jan 8, 2024
1 parent 4fbd24f commit bef336d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions libraries/scrcpy-decoder-tinyh264/src/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PromiseResolver } from "@yume-chan/async";
import { EventEmitter } from "@yume-chan/event";
import type { ScrcpyMediaStreamPacket } from "@yume-chan/scrcpy";
import {
AndroidAvcLevel,
Expand Down Expand Up @@ -51,6 +52,11 @@ export class TinyH264Decoder implements ScrcpyVideoDecoder {
return this.#renderer;
}

#sizeChanged = new EventEmitter<{ width: number; height: number }>();
get sizeChanged() {
return this.#sizeChanged.event;
}

#frameRendered = 0;
get frameRendered() {
return this.#frameRendered;
Expand Down Expand Up @@ -112,6 +118,10 @@ export class TinyH264Decoder implements ScrcpyVideoDecoder {
cropLeft,
cropTop,
} = h264ParseConfiguration(data);
this.#sizeChanged.fire({
width: croppedWidth,
height: croppedHeight,
});

// H.264 Baseline profile only supports YUV 420 pixel format
const chromaWidth = encodedWidth / 2;
Expand Down
3 changes: 2 additions & 1 deletion libraries/scrcpy-decoder-tinyh264/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Disposable } from "@yume-chan/event";
import type { Disposable, Event } from "@yume-chan/event";
import type {
ScrcpyMediaStreamPacket,
ScrcpyVideoCodecId,
Expand All @@ -12,6 +12,7 @@ export interface ScrcpyVideoDecoderCapability {

export interface ScrcpyVideoDecoder extends Disposable {
readonly renderer: HTMLElement;
readonly sizeChanged: Event<{ width: number; height: number }>;
readonly frameRendered: number;
readonly frameSkipped: number;
readonly writable: WritableStream<ScrcpyMediaStreamPacket>;
Expand Down
1 change: 1 addition & 0 deletions libraries/scrcpy-decoder-webcodecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@yume-chan/event": "workspace:^0.0.22",
"@yume-chan/scrcpy": "workspace:^0.0.22",
"@yume-chan/scrcpy-decoder-tinyh264": "workspace:^0.0.22",
"@yume-chan/stream-extra": "workspace:^0.0.22",
Expand Down
10 changes: 10 additions & 0 deletions libraries/scrcpy-decoder-webcodecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from "@yume-chan/event";
import type {
ScrcpyMediaStreamDataPacket,
ScrcpyMediaStreamPacket,
Expand Down Expand Up @@ -62,6 +63,11 @@ export class WebCodecsDecoder implements ScrcpyVideoDecoder {
return this.#frameSkipped;
}

#sizeChanged = new EventEmitter<{ width: number; height: number }>();
get sizeChanged() {
return this.#sizeChanged.event;
}

#context: CanvasRenderingContext2D;
#decoder: VideoDecoder;
#config: Uint8Array | undefined;
Expand Down Expand Up @@ -132,6 +138,10 @@ export class WebCodecsDecoder implements ScrcpyVideoDecoder {

this.#renderer.width = croppedWidth;
this.#renderer.height = croppedHeight;
this.#sizeChanged.fire({
width: croppedWidth,
height: croppedHeight,
});

// https://www.rfc-editor.org/rfc/rfc6381#section-3.3
// ISO Base Media File Format Name Space
Expand Down

0 comments on commit bef336d

Please sign in to comment.