-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreviewThumbnail.ts
213 lines (188 loc) · 7.32 KB
/
PreviewThumbnail.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
import * as shadyCss from '@webcomponents/shadycss';
import previewThumbnailCss from './PreviewThumbnail.css';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { ChromelessPlayer, TextTrack, TextTrackCue, TextTrackCueList, TextTracksList } from 'theoplayer/chromeless';
import { arrayFind, noOp } from '../util/CommonUtils';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate('theoplayer-preview-thumbnail', `<style>${previewThumbnailCss}</style><canvas></canvas>`);
const TRACK_EVENTS = ['addtrack', 'removetrack'] as const;
/**
* `<theoplayer-preview-thumbnail>` - A display that shows the thumbnail image at the current preview time
* of a {@link TimeRange | `<theoplayer-time-range>`}.
*
* The first `metadata` text track whose label equals `"thumbnails"` is used as source for the thumbnails.
* This track is expected to contain cues whose content is the URL for the thumbnail image.
* If the thumbnail image URL ends with a [spatial fragment](https://www.w3.org/TR/media-frags/#naming-space)
* (e.g. `#xywh=180,80,60,40`), then the thumbnail is clipped to the rectangle defined by that fragment.
*
* If the stream does not contain thumbnails, then this display shows nothing.
* @group Components
*/
export class PreviewThumbnail extends StateReceiverMixin(HTMLElement, ['player', 'previewTime']) {
private readonly _canvasEl: HTMLCanvasElement;
private readonly _canvasContext: CanvasRenderingContext2D;
private readonly _thumbnailImageSource: HTMLImageElement;
private _player: ChromelessPlayer | undefined;
private _textTrackList: TextTracksList | undefined;
private _previewTime: number = NaN;
private _thumbnailTextTrack: TextTrack | undefined;
private _lastLoadedThumbnailUrl: string | undefined;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
this._canvasEl = shadowRoot.querySelector('canvas')!;
this._canvasContext = this._canvasEl.getContext('2d')!;
this._thumbnailImageSource = document.createElement('img');
this._thumbnailImageSource.decoding = 'async';
this._upgradeProperty('previewTime');
this._upgradeProperty('player');
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
connectedCallback(): void {
shadyCss.styleElement(this);
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
this._textTrackList?.removeEventListener(TRACK_EVENTS, this._updateThumbnailTextTrack);
this._player = player;
this._textTrackList = player?.textTracks;
this._updateThumbnailTextTrack();
this._textTrackList?.addEventListener(TRACK_EVENTS, this._updateThumbnailTextTrack);
}
get previewTime(): number {
return this._previewTime;
}
set previewTime(previewTime: number) {
this._previewTime = previewTime;
this.updateThumbnail_();
}
private readonly _updateThumbnailTextTrack = (): void => {
const oldTrack = this._thumbnailTextTrack;
const newTrack = this._player ? arrayFind(this._player.textTracks, isThumbnailTextTrack) : undefined;
if (oldTrack === newTrack) {
return;
}
this._thumbnailTextTrack = newTrack;
this.updateThumbnail_();
};
private updateThumbnail_(): void {
const track = this._thumbnailTextTrack;
if (track === undefined) {
this.hideThumbnail_();
return;
}
if (track.mode === 'disabled') {
track.mode = 'hidden';
}
const cue = getCueAtTime(track.cues!, this._previewTime);
if (cue === undefined) {
this.hideThumbnail_();
return;
}
const content = (cue.content as string).trim();
const sprite = parseSpriteUrl(content);
// Resolve relative URLs against track's URL (or page URL)
let thumbnailUrl = sprite ? sprite.url : content;
thumbnailUrl = new URL(thumbnailUrl, track.src || location.href).href;
// Load thumbnail image, if not yet loaded
if (this._lastLoadedThumbnailUrl !== thumbnailUrl) {
this.loadThumbnailImage_(thumbnailUrl);
return;
}
this.showThumbnail_(sprite);
}
private loadThumbnailImage_(url: string): void {
if (this._thumbnailImageSource.src === url) {
// Already loading this URL, wait for the current load to complete.
return;
}
const onLoad = () => {
this._lastLoadedThumbnailUrl = url;
this.updateThumbnail_();
};
if (typeof this._thumbnailImageSource.decode === 'function') {
// Load and decode in parallel
this._thumbnailImageSource.src = url;
this._thumbnailImageSource.decode().then(onLoad, noOp);
} else {
// Load in parallel, decode synchronously
this._thumbnailImageSource.onload = onLoad;
// Reset `src` to ensure `onload` will be triggered.
this._thumbnailImageSource.src = '';
this._thumbnailImageSource.src = url;
}
}
private showThumbnail_(sprite: Sprite | undefined): void {
if (sprite) {
// Draw part of image
this._canvasEl.width = sprite.w;
this._canvasEl.height = sprite.h;
this._canvasContext.drawImage(this._thumbnailImageSource, sprite.x, sprite.y, sprite.w, sprite.h, 0, 0, sprite.w, sprite.h);
} else {
// Draw entire image
this._canvasEl.width = this._thumbnailImageSource.naturalWidth;
this._canvasEl.height = this._thumbnailImageSource.naturalHeight;
this._canvasContext.drawImage(this._thumbnailImageSource, 0, 0);
}
this._canvasEl.style.display = 'block';
}
private hideThumbnail_(): void {
this._canvasEl.style.display = 'none';
}
}
customElements.define('theoplayer-preview-thumbnail', PreviewThumbnail);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-preview-thumbnail': PreviewThumbnail;
}
}
function isThumbnailTextTrack(track: TextTrack): boolean {
return track.kind === 'metadata' && track.label === 'thumbnails';
}
function getCueAtTime(cues: TextTrackCueList, time: number): TextTrackCue | undefined {
if (cues.length === 0) {
return undefined;
}
let result = cues[0];
for (let cue of cues) {
if (cue.startTime <= time) {
result = cue;
} else if (time >= cue.endTime) {
return result;
}
}
return result;
}
interface Sprite {
url: string;
x: number;
y: number;
w: number;
h: number;
}
function parseSpriteUrl(spriteUrl: string): Sprite | undefined {
const match = spriteUrl.match(/^(.+)#xywh=(\d+),(\d+),(\d+),(\d+)$/);
if (!match) {
return undefined;
}
const [, url, x, y, w, h] = match;
return {
url,
x: +x,
y: +y,
w: +w,
h: +h
};
}