forked from muxinc/media-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash-video-element.js
More file actions
72 lines (56 loc) · 2.29 KB
/
dash-video-element.js
File metadata and controls
72 lines (56 loc) · 2.29 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
import { CustomVideoElement } from 'custom-media-element';
import { MediaTracksMixin } from 'media-tracks';
class DashVideoElement extends MediaTracksMixin(CustomVideoElement) {
static shadowRootOptions = { ...CustomVideoElement.shadowRootOptions };
static getTemplateHTML = (attrs) => {
const { src, ...rest } = attrs; // eslint-disable-line no-unused-vars
return CustomVideoElement.getTemplateHTML(rest);
};
#apiInit;
attributeChangedCallback(attrName, oldValue, newValue) {
if (attrName !== 'src') {
super.attributeChangedCallback(attrName, oldValue, newValue);
}
if (attrName === 'src' && oldValue != newValue) {
this.load();
}
}
async load() {
if (this.#apiInit) {
this.api.attachSource(this.src);
return;
}
this.#apiInit = true;
const Dash = await import('dashjs');
this.api = Dash.MediaPlayer().create();
this.api.initialize(this.nativeEl, this.src, this.autoplay);
this.api.on(Dash.MediaPlayer.events.STREAM_INITIALIZED, () => {
const bitrateList = this.api.getRepresentationsByType('video');
let videoTrack = this.videoTracks.getTrackById('main');
if (!videoTrack) {
videoTrack = this.addVideoTrack('main');
videoTrack.id = 'main';
videoTrack.selected = true;
}
bitrateList.forEach((rep) => {
const bitrate =
rep.bandwidth ?? rep.bitrate ?? (Number.isFinite(rep.bitrateInKbit) ? rep.bitrateInKbit * 1000 : undefined);
const rendition = videoTrack.addRendition(rep.id, rep.width, rep.height, rep.mimeType ?? rep.codec, bitrate);
rendition.id = rep.id;
});
this.videoRenditions.addEventListener('change', () => {
const selected = this.videoRenditions[this.videoRenditions.selectedIndex];
if (selected?.id) {
this.api.updateSettings({ streaming: { abr: { autoSwitchBitrate: { video: false } } } });
this.api.setRepresentationForTypeById('video', selected.id, true);
} else {
this.api.updateSettings({ streaming: { abr: { autoSwitchBitrate: { video: true } } } });
}
});
});
}
}
if (globalThis.customElements && !globalThis.customElements.get('dash-video')) {
globalThis.customElements.define('dash-video', DashVideoElement);
}
export default DashVideoElement;