-
Notifications
You must be signed in to change notification settings - Fork 302
/
Decoder.ts
368 lines (328 loc) · 12.5 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import VideoSettings from '../VideoSettings';
import ScreenInfo from '../ScreenInfo';
import Rect from '../Rect';
import Size from '../Size';
import Util from '../Util';
interface BitrateStat {
timestamp: number;
bytes: number;
}
interface FramesPerSecondStats {
avgInput: number;
avgDecoded: number;
avgDropped: number;
avgSize: number;
}
export interface PlaybackQuality {
decodedFrames: number;
droppedFrames: number;
inputFrames: number;
inputBytes: number;
timestamp: number;
}
export interface VideoResizeListener {
onVideoResize(size: Size): void;
}
export default abstract class Decoder {
private static readonly STAT_BACKGROUND: string = 'rgba(0, 0, 0, 0.5)';
private static readonly STAT_TEXT_COLOR: string = 'hsl(24, 85%, 50%)';
public static readonly DEFAULT_SHOW_QUALITY_STATS = false;
public static STATE: Record<string, number> = {
PLAYING: 1,
PAUSED: 2,
STOPPED: 3,
};
private static STATS_HEIGHT = 12;
protected screenInfo?: ScreenInfo;
protected videoSettings: VideoSettings;
protected parentElement?: HTMLElement;
protected touchableCanvas: HTMLCanvasElement;
protected inputBytes: BitrateStat[] = [];
protected perSecondQualityStats?: FramesPerSecondStats;
protected momentumQualityStats?: PlaybackQuality;
private totalStats: PlaybackQuality = {
decodedFrames: 0,
droppedFrames: 0,
inputFrames: 0,
inputBytes: 0,
timestamp: 0,
};
private totalStatsCounter = 0;
private dirtyStatsWidth = 0;
private state: number = Decoder.STATE.STOPPED;
private resizeListeners: Set<VideoResizeListener> = new Set();
private showQualityStats = Decoder.DEFAULT_SHOW_QUALITY_STATS;
private statLines: string[] = [];
public readonly supportsScreenshot: boolean = false;
constructor(
protected udid: string,
protected name: string = 'Decoder',
protected tag: HTMLElement = document.createElement('div'),
) {
this.touchableCanvas = document.createElement('canvas');
this.touchableCanvas.className = 'touch-layer';
this.touchableCanvas.oncontextmenu = function (e: MouseEvent): void {
e.preventDefault();
};
const preferred = this.getPreferredVideoSetting();
this.videoSettings = Decoder.getVideoSettingFromStorage(preferred, this.name, udid);
}
protected static isIFrame(frame: Uint8Array): boolean {
return frame && frame.length > 4 && frame[4] === 0x65;
}
private static getStorageKey(decoderName: string, udid: string): string {
const { innerHeight, innerWidth } = window;
return `${decoderName}:${udid}:${innerWidth}x${innerHeight}`;
}
private static getVideoSettingFromStorage(
preferred: VideoSettings,
decoderName: string,
deviceName: string,
): VideoSettings {
if (!window.localStorage) {
return preferred;
}
const key = this.getStorageKey(decoderName, deviceName);
const saved = window.localStorage.getItem(key);
if (!saved) {
return preferred;
}
const parsed = JSON.parse(saved);
const { crop, bitrate, iFrameInterval, sendFrameMeta, lockedVideoOrientation } = parsed;
// REMOVE `frameRate`
const maxFps = isNaN(parsed.maxFps) ? parsed.frameRate : parsed.maxFps;
// REMOVE `maxSize`
let bounds: Size | null = null;
if (typeof parsed.bounds !== 'object' || isNaN(parsed.bounds.width) || isNaN(parsed.bounds.height)) {
if (!isNaN(parsed.maxSize)) {
bounds = new Size(parsed.maxSize, parsed.maxSize);
}
} else {
bounds = new Size(parsed.bounds.width, parsed.bounds.height);
}
return new VideoSettings({
crop: crop ? new Rect(crop.left, crop.top, crop.right, crop.bottom) : preferred.crop,
bitrate: !isNaN(bitrate) ? bitrate : preferred.bitrate,
bounds: bounds !== null ? bounds : preferred.bounds,
maxFps: !isNaN(maxFps) ? maxFps : preferred.maxFps,
iFrameInterval: !isNaN(iFrameInterval) ? iFrameInterval : preferred.iFrameInterval,
sendFrameMeta: typeof sendFrameMeta === 'boolean' ? sendFrameMeta : preferred.sendFrameMeta,
lockedVideoOrientation: !isNaN(lockedVideoOrientation)
? lockedVideoOrientation
: preferred.lockedVideoOrientation,
});
}
private static putVideoSettingsToStorage(
decoderName: string,
deviceName: string,
videoSettings: VideoSettings,
): void {
if (!window.localStorage) {
return;
}
const key = this.getStorageKey(decoderName, deviceName);
window.localStorage.setItem(key, JSON.stringify(videoSettings));
}
public abstract getImageDataURL(): string;
public createScreenshot(deviceName: string): void {
const a = document.createElement('a');
a.href = this.getImageDataURL();
a.download = `${deviceName} ${new Date().toLocaleString()}.png`;
a.click();
}
public play(): void {
if (!this.screenInfo) {
return;
}
this.state = Decoder.STATE.PLAYING;
requestAnimationFrame(this.updateQualityStats);
}
public pause(): void {
this.state = Decoder.STATE.PAUSED;
}
public stop(): void {
this.state = Decoder.STATE.STOPPED;
}
public getState(): number {
return this.state;
}
public pushFrame(frame: Uint8Array): void {
this.inputBytes.push({
timestamp: Date.now(),
bytes: frame.byteLength,
});
}
public abstract getPreferredVideoSetting(): VideoSettings;
protected abstract calculateMomentumStats(): void;
public getTouchableElement(): HTMLElement {
return this.touchableCanvas;
}
public setParent(parent: HTMLElement): void {
this.parentElement = parent;
parent.appendChild(this.tag);
parent.appendChild(this.touchableCanvas);
}
public getVideoSettings(): VideoSettings {
return this.videoSettings;
}
public setVideoSettings(videoSettings: VideoSettings, saveToStorage: boolean): void {
this.videoSettings = videoSettings;
if (saveToStorage) {
Decoder.putVideoSettingsToStorage(this.name, this.udid, videoSettings);
}
this.resetStats();
}
public getScreenInfo(): ScreenInfo | undefined {
return this.screenInfo;
}
public setScreenInfo(screenInfo: ScreenInfo): void {
this.pause();
this.screenInfo = screenInfo;
const { width, height } = screenInfo.videoSize;
this.touchableCanvas.width = width;
this.touchableCanvas.height = height;
if (this.parentElement) {
this.parentElement.style.height = `${height}px`;
this.parentElement.style.width = `${width}px`;
}
const size = new Size(width, height);
this.resizeListeners.forEach((listener) => {
listener.onVideoResize(size);
});
}
public getName(): string {
return this.name;
}
public addResizeListener(listener: VideoResizeListener): void {
this.resizeListeners.add(listener);
}
public removeResizeListener(listener: VideoResizeListener): void {
this.resizeListeners.delete(listener);
}
protected resetStats(): void {
this.totalStatsCounter = 0;
this.totalStats = {
droppedFrames: 0,
decodedFrames: 0,
inputFrames: 0,
inputBytes: 0,
timestamp: 0,
};
this.perSecondQualityStats = {
avgDecoded: 0,
avgDropped: 0,
avgInput: 0,
avgSize: 0,
};
}
private updateQualityStats = (): void => {
const now = Date.now();
const oneSecondBefore = now - 1000;
this.calculateMomentumStats();
if (!this.momentumQualityStats) {
return;
}
if (this.totalStats.timestamp < oneSecondBefore) {
this.totalStats = {
timestamp: now,
decodedFrames: this.totalStats.decodedFrames + this.momentumQualityStats.decodedFrames,
droppedFrames: this.totalStats.droppedFrames + this.momentumQualityStats.droppedFrames,
inputFrames: this.totalStats.inputFrames + this.momentumQualityStats.inputFrames,
inputBytes: this.totalStats.inputBytes + this.momentumQualityStats.inputBytes,
};
if (this.totalStatsCounter !== 0) {
this.perSecondQualityStats = {
avgDecoded: this.totalStats.decodedFrames / this.totalStatsCounter,
avgDropped: this.totalStats.droppedFrames / this.totalStatsCounter,
avgInput: this.totalStats.inputFrames / this.totalStatsCounter,
avgSize: this.totalStats.inputBytes / this.totalStatsCounter,
};
}
this.totalStatsCounter++;
}
this.drawStats();
requestAnimationFrame(this.updateQualityStats);
};
private drawStats(): void {
if (!this.showQualityStats) {
return;
}
const ctx = this.touchableCanvas.getContext('2d');
if (!ctx) {
return;
}
const newStats = [];
if (this.perSecondQualityStats && this.momentumQualityStats) {
const { decodedFrames, droppedFrames, inputBytes, inputFrames } = this.momentumQualityStats;
const { avgDecoded, avgDropped, avgSize, avgInput } = this.perSecondQualityStats;
const padInput = inputFrames.toString().padStart(3, ' ');
const padDecoded = decodedFrames.toString().padStart(3, ' ');
const padDropped = droppedFrames.toString().padStart(3, ' ');
const padAvgDecoded = avgDecoded.toFixed(1).padStart(5, ' ');
const padAvgDropped = avgDropped.toFixed(1).padStart(5, ' ');
const padAvgInput = avgInput.toFixed(1).padStart(5, ' ');
const prettyBytes = Util.prettyBytes(inputBytes).padStart(8, ' ');
const prettyAvgBytes = Util.prettyBytes(avgSize).padStart(8, ' ');
newStats.push(`Input bytes: ${prettyBytes} (avg: ${prettyAvgBytes}/s)`);
newStats.push(`Input FPS: ${padInput} (avg: ${padAvgInput})`);
newStats.push(`Dropped FPS: ${padDropped} (avg: ${padAvgDropped})`);
newStats.push(`Decoded FPS: ${padDecoded} (avg: ${padAvgDecoded})`);
} else {
newStats.push(`Not supported`);
}
let changed = this.statLines.length !== newStats.length;
let i = 0;
while (!changed && i++ < newStats.length) {
if (newStats[i] !== this.statLines[i]) {
changed = true;
}
}
if (changed) {
this.statLines = newStats;
this.updateCanvas(false);
}
}
private updateCanvas(onlyClear: boolean): void {
const ctx = this.touchableCanvas.getContext('2d');
if (!ctx) {
return;
}
const y = this.touchableCanvas.height;
const height = Decoder.STATS_HEIGHT;
const lines = this.statLines.length;
const spaces = lines + 1;
const p = height / 2;
const d = p * 2;
const totalHeight = height * lines + p * spaces;
ctx.clearRect(0, y - totalHeight, this.dirtyStatsWidth + d, totalHeight);
this.dirtyStatsWidth = 0;
if (onlyClear) {
return;
}
ctx.save();
ctx.font = `${height}px monospace`;
this.statLines.forEach((text) => {
const textMetrics = ctx.measureText(text);
const dirty = Math.abs(textMetrics.actualBoundingBoxLeft) + Math.abs(textMetrics.actualBoundingBoxRight);
this.dirtyStatsWidth = Math.max(dirty, this.dirtyStatsWidth);
});
ctx.fillStyle = Decoder.STAT_BACKGROUND;
ctx.fillRect(0, y - totalHeight, this.dirtyStatsWidth + d, totalHeight);
ctx.fillStyle = Decoder.STAT_TEXT_COLOR;
this.statLines.forEach((text, line) => {
ctx.fillText(text, p, y - p - line * (height + p));
});
ctx.restore();
}
public setShowQualityStats(value: boolean): void {
this.showQualityStats = value;
if (!value) {
this.updateCanvas(true);
} else {
this.drawStats();
}
}
public getShowQualityStats(): boolean {
return this.showQualityStats;
}
}