-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeRange.ts
337 lines (298 loc) · 12.4 KB
/
TimeRange.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
import * as shadyCss from '@webcomponents/shadycss';
import { Range, rangeTemplate } from './Range';
import timeRangeHtml from './TimeRange.html';
import timeRangeCss from './TimeRange.css';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { Ads, ChromelessPlayer } from 'theoplayer/chromeless';
import { formatAsTimePhrase } from '../util/TimeUtils';
import { createCustomEvent } from '../util/EventUtils';
import type { PreviewTimeChangeEvent } from '../events/PreviewTimeChangeEvent';
import { PREVIEW_TIME_CHANGE_EVENT } from '../events/PreviewTimeChangeEvent';
import { Attribute } from '../util/Attribute';
import type { StreamType } from '../util/StreamType';
import { isLinearAd } from '../util/AdUtils';
import type { ColorStops } from '../util/ColorStops';
import { KeyCode } from '../util/KeyCode';
import { createTemplate } from '../util/TemplateUtils';
// Load components used in template
import './PreviewThumbnail';
import './PreviewTimeDisplay';
const template = createTemplate('theoplayer-time-range', rangeTemplate(timeRangeHtml, timeRangeCss));
const UPDATE_EVENTS = ['timeupdate', 'durationchange', 'ratechange', 'seeking', 'seeked'] as const;
const AUTO_ADVANCE_EVENTS = ['play', 'pause', 'ended', 'readystatechange', 'error'] as const;
const AD_EVENTS = ['adbreakbegin', 'adbreakend', 'adbreakchange', 'updateadbreak', 'adbegin', 'adend', 'adskip', 'addad', 'updatead'] as const;
const DEFAULT_MISSING_TIME_PHRASE = 'video not loaded, unknown time';
/**
* Width of an ad marker on the progress bar, in percent of the total bar width.
*/
const AD_MARKER_WIDTH = 1;
/**
* `<theoplayer-time-range>` - A seek bar, showing the current time of the player,
* and which seeks the player when clicked or dragged.
*
* @slot `preview` - A slot holding a preview of the seek time, shown while hovering the seek bar.
* By default, this shows the {@link PreviewTimeDisplay | preview time} and
* the {@link PreviewThumbnail | preview thumbnail}.
* @group Components
*/
export class TimeRange extends StateReceiverMixin(Range, ['player', 'streamType', 'deviceType']) {
static get observedAttributes() {
return [...Range.observedAttributes, Attribute.SHOW_AD_MARKERS];
}
private readonly _previewRailEl: HTMLElement;
private readonly _previewBoxEl: HTMLElement;
private _player: ChromelessPlayer | undefined;
private _ads: Ads | undefined;
private _pausedWhileScrubbing: boolean = false;
private _autoAdvanceId: number = 0;
private _lastUpdateTime: number = 0;
private _lastCurrentTime: number = 0;
private _lastPlaybackRate: number = 0;
constructor() {
super({ template: template() });
this._previewRailEl = this.shadowRoot!.querySelector('.theoplayer-time-range-preview-rail')!;
this._previewBoxEl = this._previewRailEl.querySelector('[part~="preview-box"]')!;
this._rangeEl.setAttribute(Attribute.ARIA_LIVE, 'off');
this._rangeEl.addEventListener('mousedown', this._pauseOnScrubStart);
this._rangeEl.addEventListener('touchstart', this._pauseOnScrubStart);
this._upgradeProperty('player');
}
override connectedCallback(): void {
super.connectedCallback();
this._toggleAutoAdvance();
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this._toggleAutoAdvance();
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
if (this._player !== undefined) {
this._player.removeEventListener(UPDATE_EVENTS, this._updateFromPlayer);
this._player.removeEventListener(AUTO_ADVANCE_EVENTS, this._toggleAutoAdvance);
}
this._ads?.removeEventListener(AD_EVENTS, this._onAdChange);
this._player = player;
this._ads = player?.ads;
this._updateFromPlayer();
this._toggleAutoAdvance();
if (this._player !== undefined) {
this._player.addEventListener(UPDATE_EVENTS, this._updateFromPlayer);
this._player.addEventListener(AUTO_ADVANCE_EVENTS, this._toggleAutoAdvance);
}
this._ads?.addEventListener(AD_EVENTS, this._onAdChange);
}
get streamType(): StreamType {
return (this.getAttribute(Attribute.STREAM_TYPE) || 'vod') as StreamType;
}
set streamType(streamType: StreamType) {
this.setAttribute(Attribute.STREAM_TYPE, streamType);
}
private readonly _updateFromPlayer = () => {
if (this._player === undefined) {
return;
}
this._lastUpdateTime = performance.now();
this._lastCurrentTime = this._player.currentTime;
this._lastPlaybackRate = this._player.playbackRate;
const seekable = this._player.seekable;
if (seekable.length !== 0) {
this.min = seekable.start(0);
this.max = seekable.end(0);
} else {
this.min = 0;
this.max = this._player.duration;
}
if (!isFinite(this._lastCurrentTime)) {
const isLive = this._player.duration === Infinity;
this._lastCurrentTime = isLive ? this.max : this.min;
}
this._rangeEl.valueAsNumber = this._lastCurrentTime;
this.update();
this._updateDisabled();
};
private readonly _updateDisabled = () => {
let disabled = this.streamType === 'live';
if (this._player !== undefined) {
disabled ||= this._player.seekable.length === 0;
}
if (this.disabled !== disabled) {
this.disabled = disabled;
}
};
protected override getAriaLabel(): string {
return 'seek';
}
protected override getAriaValueText(): string {
const currentTimePhrase = formatAsTimePhrase(this.value);
const totalTimePhrase = formatAsTimePhrase(this.max);
if (currentTimePhrase && totalTimePhrase) {
return `${currentTimePhrase} of ${totalTimePhrase}`;
}
return DEFAULT_MISSING_TIME_PHRASE;
}
override attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
super.attributeChangedCallback(attrName, oldValue, newValue);
if (newValue === oldValue) {
return;
}
if (attrName === Attribute.STREAM_TYPE) {
this._updateDisabled();
} else if (attrName === Attribute.SHOW_AD_MARKERS) {
this.update();
}
if (TimeRange.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
protected override handleInput(): void {
if (this._player !== undefined && this._player.currentTime !== this.value) {
this._player.currentTime = this.value;
}
super.handleInput();
}
private readonly _pauseOnScrubStart = () => {
if (this._pausedWhileScrubbing) {
// Already scrubbing.
return;
}
if (this._player === undefined || this._player.paused) {
// Player is already paused.
return;
}
this._pausedWhileScrubbing = true;
this._player.pause();
document.addEventListener('mouseup', this._playOnScrubEnd);
document.addEventListener('touchend', this._playOnScrubEnd);
};
private readonly _playOnScrubEnd = () => {
if (!this._pausedWhileScrubbing) {
return;
}
this._pausedWhileScrubbing = false;
document.removeEventListener('mouseup', this._playOnScrubEnd);
document.removeEventListener('touchend', this._playOnScrubEnd);
if (this._player !== undefined && this._player.paused) {
this._player.play();
}
};
private shouldAutoAdvance_(): boolean {
return (
this.isConnected &&
this._player !== undefined &&
!this._player.paused &&
!this._player.ended &&
!this._player.errorObject &&
this._player.readyState >= 3
);
}
private readonly _toggleAutoAdvance = () => {
if (this.shouldAutoAdvance_()) {
if (this._autoAdvanceId === 0) {
this._updateFromPlayer();
this._autoAdvanceId = requestAnimationFrame(this._autoAdvanceWhilePlaying);
}
} else {
if (this._autoAdvanceId !== 0) {
this._updateFromPlayer();
cancelAnimationFrame(this._autoAdvanceId);
this._autoAdvanceId = 0;
}
}
};
private readonly _autoAdvanceWhilePlaying = () => {
if (!this.shouldAutoAdvance_()) {
this._autoAdvanceId = 0;
return;
}
const delta = (performance.now() - this._lastUpdateTime) / 1000;
this._rangeEl.valueAsNumber = this._lastCurrentTime + delta * this._lastPlaybackRate;
this.update();
this._autoAdvanceId = requestAnimationFrame(this._autoAdvanceWhilePlaying);
};
protected override updatePointer_(mousePercent: number, rangeRect: DOMRectReadOnly): void {
super.updatePointer_(mousePercent, rangeRect);
// Update preview rail, keeping the preview box within bounds
let previewPos = rangeRect.left + mousePercent * rangeRect.width;
const previewBoxRect = this._previewBoxEl.getBoundingClientRect();
const minPreviewPos = rangeRect.left + previewBoxRect.width / 2;
const maxPreviewPos = rangeRect.right - previewBoxRect.width / 2;
previewPos = Math.max(minPreviewPos, Math.min(maxPreviewPos, previewPos));
const previewPosFraction = (previewPos - rangeRect.left) / rangeRect.width;
this._previewRailEl.style.transform = `translateX(${(previewPosFraction * 100 * 100).toFixed(6)}%)`;
// Propagate preview time to parent
if (this._player === undefined) {
return;
}
const { min, max } = this;
const previewTime = min + mousePercent * (max - min);
const previewTimeChangeEvent: PreviewTimeChangeEvent = createCustomEvent(PREVIEW_TIME_CHANGE_EVENT, {
bubbles: true,
composed: true,
detail: { previewTime }
});
this.dispatchEvent(previewTimeChangeEvent);
}
protected override getBarColors(): ColorStops {
const colorStops = super.getBarColors();
if (!this.hasAttribute(Attribute.SHOW_AD_MARKERS) || !this._player || !this._player.ads) {
return colorStops;
}
if (this._player.ads.playing) {
// Currently playing a linear ad
return colorStops;
}
const { min, max } = this;
for (const adBreak of this._player.ads.scheduledAdBreaks) {
if (adBreak.ads !== undefined && adBreak.ads.length > 0 && !adBreak.ads.some(isLinearAd)) {
// Ad break definitely does not contain any linear ads
continue;
}
let adBreakTime = adBreak.timeOffset;
if (adBreakTime < 0) {
// Post-rolls have timeOffset == -1
adBreakTime = this._player.duration;
}
if (min <= adBreakTime && adBreakTime <= max) {
const adBreakPercent = ((adBreakTime - min) / (max - min)) * 100;
let adBreakFrom: number;
let adBreakTo: number;
if (adBreakPercent + AD_MARKER_WIDTH < 100) {
adBreakFrom = adBreakPercent;
adBreakTo = adBreakPercent + AD_MARKER_WIDTH;
} else {
adBreakFrom = 100 - AD_MARKER_WIDTH;
adBreakTo = 100;
}
colorStops.add('var(--theoplayer-time-range-ad-marker-color, #ffc50f)', adBreakFrom, adBreakTo);
}
}
return colorStops;
}
private readonly _onAdChange = () => {
this.update();
};
protected override handleKeyDown_(e: KeyboardEvent) {
super.handleKeyDown_(e);
if (this.deviceType === 'tv' && e.keyCode === KeyCode.ENTER) {
if (this._player !== undefined) {
if (this._player.paused) {
this._player.play();
} else {
this._player.pause();
}
}
}
}
}
customElements.define('theoplayer-time-range', TimeRange);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-time-range': TimeRange;
}
}