-
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathdecoder.ts
More file actions
272 lines (235 loc) · 8.37 KB
/
decoder.ts
File metadata and controls
272 lines (235 loc) · 8.37 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { PromiseResolver } from "@yume-chan/async";
import { H264 } from "@yume-chan/media-codec";
import type { ScrcpyMediaStreamConfigurationPacket } from "@yume-chan/scrcpy";
import {
AndroidAvcLevel,
AndroidAvcProfile,
ScrcpyVideoSizeImpl,
} from "@yume-chan/scrcpy";
import { WritableStream } from "@yume-chan/stream-extra";
import YuvBuffer from "yuv-buffer";
import YuvCanvas from "yuv-canvas";
import type {
ScrcpyVideoDecoder,
ScrcpyVideoDecoderCapability,
} from "./types.js";
import {
createCanvas,
glIsSupported,
PauseController,
PerformanceCounter,
} from "./utils/index.js";
import type { TinyH264Wrapper } from "./wrapper.js";
import { createTinyH264Wrapper } from "./wrapper.js";
export const noop = () => {
// no-op
};
export class TinyH264Decoder implements ScrcpyVideoDecoder {
static readonly capabilities: Record<string, ScrcpyVideoDecoderCapability> =
{
h264: {
maxProfile: AndroidAvcProfile.Baseline,
maxLevel: AndroidAvcLevel.Level4,
},
};
#canvas: HTMLCanvasElement | OffscreenCanvas;
get canvas() {
return this.#canvas;
}
#pause = new PauseController();
get paused() {
return this.#pause.paused;
}
get writable() {
return this.#pause.writable;
}
#size = new ScrcpyVideoSizeImpl();
get width() {
return this.#size.width;
}
get height() {
return this.#size.height;
}
get sizeChanged() {
return this.#size.sizeChanged;
}
#counter = new PerformanceCounter();
/**
* Gets the number of frames that have been drawn on the renderer.
*/
get framesRendered() {
return this.#counter.framesRendered;
}
/**
* Gets the number of frames that's visible to the user.
*
* Multiple frames might be rendered during one vertical sync interval,
* but only the last of them is represented to the user.
* This costs some performance but reduces latency by 1 frame.
*
* Might be `0` if the renderer is in a nested Web Worker on Chrome due to a Chrome bug.
* https://issues.chromium.org/issues/41483010
*/
get framesPresented() {
return this.#counter.framesPresented;
}
/**
* Gets the number of frames that wasn't drawn on the renderer
* because the renderer can't keep up
*/
get framesSkippedRendering() {
return this.#counter.framesSkippedRendering;
}
#renderer: YuvCanvas | undefined;
#decoder: Promise<TinyH264Wrapper> | undefined;
constructor({ canvas }: TinyH264Decoder.Options = {}) {
if (canvas) {
this.#canvas = canvas;
} else {
this.#canvas = createCanvas();
}
this.#renderer = YuvCanvas.attach(this.#canvas, {
// yuv-canvas supports detecting WebGL support by creating a <canvas> itself
// But this doesn't work in Web Worker (with OffscreenCanvas)
// so we implement our own check here
webGL: glIsSupported({
// Disallow software rendering.
// yuv-canvas also supports 2d canvas
// which is faster than software-based WebGL.
failIfMajorPerformanceCaveat: true,
}),
});
void this.#pause.readable
.pipeTo(
new WritableStream({
write: async (packet) => {
if (packet.type === "configuration") {
await this.#configure(packet);
return;
}
if (!this.#decoder) {
throw new Error("Decoder not configured");
}
// TinyH264 decoder doesn't support associating metadata
// with each frame's input/output
// so skipping frames when resuming from pause is not supported
const decoder = await this.#decoder;
// `packet.data` might be from a `BufferCombiner` so we have to copy it using `slice`
decoder.feed(packet.data.slice().buffer);
},
}),
)
.catch(noop);
}
#configure = async ({
data,
}: ScrcpyMediaStreamConfigurationPacket): Promise<undefined> => {
this.#disposeDecoder();
const resolver = new PromiseResolver<TinyH264Wrapper>();
this.#decoder = resolver.promise;
try {
const {
encodedWidth,
encodedHeight,
croppedWidth,
croppedHeight,
cropLeft,
cropTop,
} = H264.parseConfiguration(data);
this.#size.setSize(croppedWidth, croppedHeight);
// H.264 Baseline profile only supports YUV 420 pixel format
// So chroma width/height is each half of video width/height
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 decoder = await createTinyH264Wrapper();
const uPlaneOffset = encodedWidth * encodedHeight;
const vPlaneOffset = uPlaneOffset + chromaWidth * chromaHeight;
decoder.onPictureReady(({ data }) => {
// TinyH264 doesn't pass any frame metadata to `onPictureReady`
// so frames marked as skipped (by pause controller) can't be skipped
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,
),
);
// Can't know if yuv-canvas is dropping frames or not
this.#renderer!.drawFrame(frame);
this.#counter.increaseFramesRendered();
});
decoder.feed(data.slice().buffer);
resolver.resolve(decoder);
} catch (e) {
resolver.reject(e);
}
};
pause(): void {
this.#pause.pause();
}
resume(): undefined {
this.#pause.resume();
}
trackDocumentVisibility(document: Document): () => undefined {
return this.#pause.trackDocumentVisibility(document);
}
/**
* Only dispose the TinyH264 decoder instance.
*
* This will be called when re-configuring multiple times,
* we don't want to dispose other parts (e.g. `#counter`) on that case
*/
#disposeDecoder() {
if (!this.#decoder) {
return;
}
this.#decoder
.then((decoder) => decoder.dispose())
// NOOP: It's disposed so nobody cares about the error
.catch(noop);
this.#decoder = undefined;
}
dispose(): void {
// This class doesn't need to guard against multiple dispose calls
// since most of the logic is already handled in `#pause`
this.#pause.dispose();
this.#disposeDecoder();
this.#counter.dispose();
this.#size.dispose();
this.#canvas.width = 0;
this.#canvas.height = 0;
}
}
export namespace TinyH264Decoder {
export interface Options {
/**
* Optional render target canvas element or offscreen canvas.
* If not provided, a new `<canvas>` (when DOM is available)
* or a `OffscreenCanvas` will be created.
*/
canvas?: HTMLCanvasElement | OffscreenCanvas | undefined;
}
}