|
| 1 | +import { Events, HlsPlayer } from "@superstreamer/player"; |
| 2 | +import { MediaTracksMixin } from "media-tracks"; |
| 3 | + |
| 4 | +function getTemplateHTML() { |
| 5 | + return ` |
| 6 | + <style> |
| 7 | + :host { |
| 8 | + width: 100%; |
| 9 | + height: 100%; |
| 10 | + } |
| 11 | + </style> |
| 12 | + <div class="container"></div> |
| 13 | + `; |
| 14 | +} |
| 15 | + |
| 16 | +const symbolTrackId_ = Symbol("superstreamer.trackId"); |
| 17 | + |
| 18 | +class SuperstreamerVideoElement extends MediaTracksMixin( |
| 19 | + globalThis.HTMLElement, |
| 20 | +) { |
| 21 | + static getTemplateHTML = getTemplateHTML; |
| 22 | + |
| 23 | + static shadowRootOptions = { |
| 24 | + mode: "open", |
| 25 | + }; |
| 26 | + |
| 27 | + static observedAttributes = ["src"]; |
| 28 | + |
| 29 | + #player; |
| 30 | + |
| 31 | + #readyState = 0; |
| 32 | + |
| 33 | + #video; |
| 34 | + |
| 35 | + constructor() { |
| 36 | + super(); |
| 37 | + |
| 38 | + if (!this.shadowRoot) { |
| 39 | + this.attachShadow({ |
| 40 | + mode: "open", |
| 41 | + }); |
| 42 | + this.shadowRoot.innerHTML = getTemplateHTML(); |
| 43 | + } |
| 44 | + |
| 45 | + const container = this.shadowRoot.querySelector(".container"); |
| 46 | + this.#player = new HlsPlayer(container); |
| 47 | + |
| 48 | + this.#video = document.createElement("video"); |
| 49 | + |
| 50 | + this.#bindListeners(); |
| 51 | + } |
| 52 | + |
| 53 | + #bindListeners() { |
| 54 | + this.#player.on(Events.PLAYHEAD_CHANGE, () => { |
| 55 | + switch (this.#player.playhead) { |
| 56 | + case "play": |
| 57 | + this.dispatchEvent(new Event("play")); |
| 58 | + break; |
| 59 | + case "playing": |
| 60 | + this.dispatchEvent(new Event("playing")); |
| 61 | + break; |
| 62 | + case "pause": |
| 63 | + this.dispatchEvent(new Event("pause")); |
| 64 | + break; |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + this.#player.on(Events.TIME_CHANGE, () => { |
| 69 | + this.dispatchEvent(new Event("timeupdate")); |
| 70 | + }); |
| 71 | + |
| 72 | + this.#player.on(Events.VOLUME_CHANGE, () => { |
| 73 | + this.dispatchEvent(new Event("volumechange")); |
| 74 | + }); |
| 75 | + |
| 76 | + this.#player.on(Events.SEEKING_CHANGE, () => { |
| 77 | + if (this.#player.seeking) { |
| 78 | + this.dispatchEvent(new Event("seeking")); |
| 79 | + } else { |
| 80 | + this.dispatchEvent(new Event("seeked")); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + this.#player.on(Events.READY, async () => { |
| 85 | + this.#readyState = 1; |
| 86 | + |
| 87 | + this.dispatchEvent(new Event("loadedmetadata")); |
| 88 | + this.dispatchEvent(new Event("durationchange")); |
| 89 | + this.dispatchEvent(new Event("volumechange")); |
| 90 | + this.dispatchEvent(new Event("loadcomplete")); |
| 91 | + |
| 92 | + this.#createVideoTracks(); |
| 93 | + this.#createAudioTracks(); |
| 94 | + this.#createTextTracks(); |
| 95 | + }); |
| 96 | + |
| 97 | + this.#player.on(Events.STARTED, () => { |
| 98 | + this.#readyState = 3; |
| 99 | + }); |
| 100 | + |
| 101 | + this.#player.on(Events.ASSET_CHANGE, () => { |
| 102 | + const controller = this.closest("media-controller"); |
| 103 | + if (controller) { |
| 104 | + controller.setAttribute("interstitial", this.#player.asset ? "1" : "0"); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + get src() { |
| 110 | + return this.getAttribute("src"); |
| 111 | + } |
| 112 | + |
| 113 | + set src(val) { |
| 114 | + if (this.src === val) { |
| 115 | + return; |
| 116 | + } |
| 117 | + this.setAttribute("src", val); |
| 118 | + } |
| 119 | + |
| 120 | + get textTracks() { |
| 121 | + return this.#video.textTracks; |
| 122 | + } |
| 123 | + |
| 124 | + attributeChangedCallback(attrName, oldValue, newValue) { |
| 125 | + if (attrName === "src" && oldValue !== newValue) { |
| 126 | + this.load(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + async load() { |
| 131 | + this.#readyState = 0; |
| 132 | + |
| 133 | + while (this.#video.firstChild) { |
| 134 | + this.#video.firstChild.remove(); |
| 135 | + } |
| 136 | + |
| 137 | + for (const videoTrack of this.videoTracks) { |
| 138 | + this.removeVideoTrack(videoTrack); |
| 139 | + } |
| 140 | + for (const audioTrack of this.audioTracks) { |
| 141 | + this.removeAudioTrack(audioTrack); |
| 142 | + } |
| 143 | + |
| 144 | + this.#player.unload(); |
| 145 | + |
| 146 | + this.dispatchEvent(new Event("emptied")); |
| 147 | + |
| 148 | + if (this.src) { |
| 149 | + this.dispatchEvent(new Event("loadstart")); |
| 150 | + this.#player.load(this.src); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + get currentTime() { |
| 155 | + return this.#player.time; |
| 156 | + } |
| 157 | + |
| 158 | + set currentTime(val) { |
| 159 | + this.#player.seekTo(val); |
| 160 | + } |
| 161 | + |
| 162 | + get duration() { |
| 163 | + return this.#player.duration; |
| 164 | + } |
| 165 | + |
| 166 | + get paused() { |
| 167 | + const { playhead } = this.#player; |
| 168 | + if (playhead === "play" || playhead === "playing") { |
| 169 | + return false; |
| 170 | + } |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + get readyState() { |
| 175 | + return this.#readyState; |
| 176 | + } |
| 177 | + |
| 178 | + get muted() { |
| 179 | + return this.#player.volume === 0; |
| 180 | + } |
| 181 | + |
| 182 | + set muted(val) { |
| 183 | + this.#player.setVolume(val ? 0 : 1); |
| 184 | + } |
| 185 | + |
| 186 | + get volume() { |
| 187 | + return this.#player.volume; |
| 188 | + } |
| 189 | + |
| 190 | + set volume(val) { |
| 191 | + this.#player.setVolume(val); |
| 192 | + } |
| 193 | + |
| 194 | + async play() { |
| 195 | + this.#player.playOrPause(); |
| 196 | + await Promise.resolve(); |
| 197 | + } |
| 198 | + |
| 199 | + pause() { |
| 200 | + this.#player.playOrPause(); |
| 201 | + } |
| 202 | + |
| 203 | + #createVideoTracks() { |
| 204 | + let videoTrack = this.videoTracks.getTrackById("main"); |
| 205 | + |
| 206 | + if (!videoTrack) { |
| 207 | + videoTrack = this.addVideoTrack("main"); |
| 208 | + videoTrack.id = "main"; |
| 209 | + videoTrack.selected = true; |
| 210 | + } |
| 211 | + |
| 212 | + this.#player.qualities.forEach((quality) => { |
| 213 | + videoTrack.addRendition( |
| 214 | + undefined, |
| 215 | + quality.height, |
| 216 | + quality.height, |
| 217 | + undefined, |
| 218 | + undefined, |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + this.videoRenditions.addEventListener("change", (event) => { |
| 223 | + if (event.target.selectedIndex < 0) { |
| 224 | + this.#player.setQuality(null); |
| 225 | + } else { |
| 226 | + const rendition = this.videoRenditions[event.target.selectedIndex]; |
| 227 | + this.#player.setQuality(rendition.height); |
| 228 | + } |
| 229 | + }); |
| 230 | + } |
| 231 | + |
| 232 | + #createAudioTracks() { |
| 233 | + this.#player.audioTracks.forEach((a) => { |
| 234 | + const audioTrack = this.addAudioTrack("main", a.label, a.label); |
| 235 | + audioTrack[symbolTrackId_] = a.id; |
| 236 | + audioTrack.enabled = a.active; |
| 237 | + }); |
| 238 | + |
| 239 | + this.audioTracks.addEventListener("change", () => { |
| 240 | + const track = [...this.audioTracks].find((a) => a.enabled); |
| 241 | + if (track) { |
| 242 | + const id = track[symbolTrackId_]; |
| 243 | + this.#player.setAudioTrack(id); |
| 244 | + } |
| 245 | + }); |
| 246 | + } |
| 247 | + |
| 248 | + #createTextTracks() { |
| 249 | + this.#player.subtitleTracks.forEach((s) => { |
| 250 | + const textTrack = this.addTextTrack("subtitles", s.label, s.track.lang); |
| 251 | + textTrack[symbolTrackId_] = s.id; |
| 252 | + }); |
| 253 | + |
| 254 | + this.textTracks.addEventListener("change", () => { |
| 255 | + const track = [...this.textTracks].find((t) => t.mode === "showing"); |
| 256 | + if (track) { |
| 257 | + const id = track[symbolTrackId_]; |
| 258 | + this.#player.setSubtitleTrack(id); |
| 259 | + } else { |
| 260 | + this.#player.setSubtitleTrack(null); |
| 261 | + } |
| 262 | + }); |
| 263 | + } |
| 264 | + |
| 265 | + addTextTrack(kind, label, language) { |
| 266 | + const trackEl = document.createElement("track"); |
| 267 | + trackEl.kind = kind; |
| 268 | + trackEl.label = label; |
| 269 | + trackEl.srclang = language; |
| 270 | + trackEl.track.mode = "hidden"; |
| 271 | + this.#video.append(trackEl); |
| 272 | + return trackEl.track; |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +if (!globalThis.customElements?.get("superstreamer-video")) { |
| 277 | + globalThis.customElements.define( |
| 278 | + "superstreamer-video", |
| 279 | + SuperstreamerVideoElement, |
| 280 | + ); |
| 281 | +} |
| 282 | + |
| 283 | +export default SuperstreamerVideoElement; |
0 commit comments