-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
decoder.ts
161 lines (137 loc) · 4.77 KB
/
decoder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { PromiseResolver } from "@yume-chan/async";
import type { ScrcpyMediaStreamPacket } from "@yume-chan/scrcpy";
import {
AndroidAvcLevel,
AndroidAvcProfile,
h264ParseConfiguration,
} from "@yume-chan/scrcpy";
import { WritableStream } from "@yume-chan/stream-extra";
import type { default as YuvBuffer } from "yuv-buffer";
import type { default as YuvCanvas } from "yuv-canvas";
import type {
ScrcpyVideoDecoder,
ScrcpyVideoDecoderCapability,
} from "./types.js";
import type { TinyH264Wrapper } from "./wrapper.js";
import { createTinyH264Wrapper } from "./wrapper.js";
const NOOP = () => {
// no-op
};
let cachedInitializePromise:
| Promise<{ YuvBuffer: typeof YuvBuffer; YuvCanvas: typeof YuvCanvas }>
| undefined;
function initialize() {
if (!cachedInitializePromise) {
cachedInitializePromise = Promise.all([
import("yuv-buffer"),
import("yuv-canvas"),
]).then(([YuvBuffer, { default: YuvCanvas }]) => ({
YuvBuffer,
YuvCanvas,
}));
}
return cachedInitializePromise;
}
export class TinyH264Decoder implements ScrcpyVideoDecoder {
static readonly capabilities: Record<string, ScrcpyVideoDecoderCapability> =
{
h264: {
maxProfile: AndroidAvcProfile.Baseline,
maxLevel: AndroidAvcLevel.Level4,
},
};
#renderer: HTMLCanvasElement;
get renderer() {
return this.#renderer;
}
#frameRendered = 0;
get frameRendered() {
return this.#frameRendered;
}
#frameSkipped = 0;
get frameSkipped() {
return this.#frameSkipped;
}
#writable: WritableStream<ScrcpyMediaStreamPacket>;
get writable() {
return this.#writable;
}
#yuvCanvas: YuvCanvas | undefined;
#initializer: PromiseResolver<TinyH264Wrapper> | undefined;
constructor() {
void initialize();
this.#renderer = document.createElement("canvas");
this.#writable = new WritableStream<ScrcpyMediaStreamPacket>({
write: async (packet) => {
switch (packet.type) {
case "configuration":
await this.#configure(packet.data);
break;
case "data": {
if (!this.#initializer) {
throw new Error("Decoder not configured");
}
const wrapper = await this.#initializer.promise;
wrapper.feed(packet.data.slice().buffer);
break;
}
}
},
});
}
async #configure(data: Uint8Array) {
this.dispose();
this.#initializer = new PromiseResolver<TinyH264Wrapper>();
const { YuvBuffer, YuvCanvas } = await initialize();
if (!this.#yuvCanvas) {
this.#yuvCanvas = YuvCanvas.attach(this.#renderer);
}
const {
encodedWidth,
encodedHeight,
croppedWidth,
croppedHeight,
cropLeft,
cropTop,
} = h264ParseConfiguration(data);
// H.264 Baseline profile only supports YUV 420 pixel format
const chromaWidth = encodedWidth / 2;
const chromaHeight = encodedHeight / 2;
// YUVCanvas will set canvas size when format changes
const format = YuvBuffer.format({
width: encodedWidth,
height: encodedHeight,
chromaWidth,
chromaHeight,
cropLeft: cropLeft,
cropTop: cropTop,
cropWidth: croppedWidth,
cropHeight: croppedHeight,
displayWidth: croppedWidth,
displayHeight: croppedHeight,
});
const wrapper = await createTinyH264Wrapper();
this.#initializer.resolve(wrapper);
const uPlaneOffset = encodedWidth * encodedHeight;
const vPlaneOffset = uPlaneOffset + chromaWidth * chromaHeight;
wrapper.onPictureReady(({ data }) => {
this.#frameRendered += 1;
const array = new Uint8Array(data);
const frame = YuvBuffer.frame(
format,
YuvBuffer.lumaPlane(format, array, encodedWidth, 0),
YuvBuffer.chromaPlane(format, array, chromaWidth, uPlaneOffset),
YuvBuffer.chromaPlane(format, array, chromaWidth, vPlaneOffset),
);
this.#yuvCanvas!.drawFrame(frame);
});
wrapper.feed(data.slice().buffer);
}
dispose(): void {
this.#initializer?.promise
.then((wrapper) => wrapper.dispose())
// NOOP: It's disposed so nobody cares about the error
.catch(NOOP);
this.#initializer = undefined;
}
}