]>
Commit | Line | Data |
---|---|---|
aa8b6df4 C |
1 | // Big thanks to: https://github.com/kmoskwiak/videojs-resolution-switcher |
2 | ||
63c4db6d | 3 | import * as videojs from 'video.js' |
aa8b6df4 | 4 | import * as WebTorrent from 'webtorrent' |
b6827820 | 5 | import { VideoFile } from '../../../../shared/models/videos/video.model' |
aa8b6df4 | 6 | import { renderVideo } from './video-renderer' |
be6a4802 | 7 | |
a22bfc3e C |
8 | interface VideoJSComponentInterface { |
9 | _player: VideoJSPlayer | |
10 | ||
11 | new (player: VideoJSPlayer, options?: any) | |
12 | ||
13 | registerComponent (name: string, obj: any) | |
14 | } | |
15 | ||
16 | interface VideoJSPlayer extends videojs.Player { | |
17 | peertube (): PeerTubePlugin | |
18 | } | |
19 | ||
20 | type PeertubePluginOptions = { | |
21 | videoFiles: VideoFile[] | |
22 | playerElement: HTMLVideoElement | |
23 | peerTubeLink: boolean | |
24 | } | |
25 | ||
be6a4802 C |
26 | // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts |
27 | // Don't import all Angular stuff, just copy the code with shame | |
28 | const dictionaryBytes: Array<{max: number, type: string}> = [ | |
29 | { max: 1024, type: 'B' }, | |
30 | { max: 1048576, type: 'KB' }, | |
31 | { max: 1073741824, type: 'MB' }, | |
32 | { max: 1.0995116e12, type: 'GB' } | |
33 | ] | |
34 | function bytes (value) { | |
35 | const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] | |
36 | const calc = Math.floor(value / (format.max / 1024)).toString() | |
37 | ||
38 | return [ calc, format.type ] | |
39 | } | |
aa8b6df4 C |
40 | |
41 | // videojs typings don't have some method we need | |
42 | const videojsUntyped = videojs as any | |
43 | const webtorrent = new WebTorrent({ dht: false }) | |
44 | ||
a22bfc3e C |
45 | const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem') |
46 | class ResolutionMenuItem extends MenuItem { | |
47 | ||
48 | constructor (player: VideoJSPlayer, options) { | |
aa8b6df4 | 49 | options.selectable = true |
a22bfc3e | 50 | super(player, options) |
aa8b6df4 | 51 | |
a22bfc3e | 52 | const currentResolution = this.player_.peertube().getCurrentResolution() |
aa8b6df4 | 53 | this.selected(this.options_.id === currentResolution) |
a22bfc3e | 54 | } |
aa8b6df4 | 55 | |
a22bfc3e | 56 | handleClick (event) { |
aa8b6df4 | 57 | MenuItem.prototype.handleClick.call(this, event) |
a22bfc3e | 58 | this.player_.peertube().updateResolution(this.options_.id) |
aa8b6df4 | 59 | } |
a22bfc3e | 60 | } |
aa8b6df4 C |
61 | MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem) |
62 | ||
a22bfc3e C |
63 | const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton') |
64 | class ResolutionMenuButton extends MenuButton { | |
65 | label: HTMLElement | |
66 | ||
67 | constructor (player: VideoJSPlayer, options) { | |
aa8b6df4 | 68 | options.label = 'Quality' |
a22bfc3e C |
69 | super(player, options) |
70 | ||
71 | this.label = document.createElement('span') | |
aa8b6df4 | 72 | |
aa8b6df4 C |
73 | this.el().setAttribute('aria-label', 'Quality') |
74 | this.controlText('Quality') | |
75 | ||
76 | videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label') | |
77 | this.el().appendChild(this.label) | |
78 | ||
a22bfc3e C |
79 | player.peertube().on('videoFileUpdate', () => this.update()) |
80 | } | |
aa8b6df4 | 81 | |
a22bfc3e | 82 | createItems () { |
aa8b6df4 | 83 | const menuItems = [] |
a22bfc3e | 84 | for (const videoFile of this.player_.peertube().videoFiles) { |
aa8b6df4 C |
85 | menuItems.push(new ResolutionMenuItem( |
86 | this.player_, | |
87 | { | |
88 | id: videoFile.resolution, | |
89 | label: videoFile.resolutionLabel, | |
90 | src: videoFile.magnetUri, | |
91 | selected: videoFile.resolution === this.currentSelection | |
92 | }) | |
93 | ) | |
94 | } | |
95 | ||
96 | return menuItems | |
a22bfc3e C |
97 | } |
98 | ||
99 | update () { | |
100 | if (!this.label) return | |
aa8b6df4 | 101 | |
a22bfc3e | 102 | this.label.innerHTML = this.player_.peertube().getCurrentResolutionLabel() |
be6a4802 | 103 | this.hide() |
a22bfc3e C |
104 | return super.update() |
105 | } | |
106 | ||
107 | buildCSSClass () { | |
108 | return super.buildCSSClass() + ' vjs-resolution-button' | |
109 | } | |
aa8b6df4 | 110 | |
a22bfc3e C |
111 | dispose () { |
112 | this.parentNode.removeChild(this) | |
aa8b6df4 | 113 | } |
a22bfc3e | 114 | } |
aa8b6df4 C |
115 | MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton) |
116 | ||
a22bfc3e C |
117 | const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') |
118 | class PeertubeLinkButton extends Button { | |
aa8b6df4 | 119 | |
a22bfc3e | 120 | createEl () { |
aa8b6df4 C |
121 | const link = document.createElement('a') |
122 | link.href = window.location.href.replace('embed', 'watch') | |
123 | link.innerHTML = 'PeerTube' | |
124 | link.title = 'Go to the video page' | |
125 | link.className = 'vjs-peertube-link' | |
126 | link.target = '_blank' | |
127 | ||
128 | return link | |
a22bfc3e | 129 | } |
aa8b6df4 | 130 | |
a22bfc3e | 131 | handleClick () { |
be6a4802 | 132 | this.player_.pause() |
aa8b6df4 | 133 | } |
aa8b6df4 | 134 | |
a22bfc3e C |
135 | dispose () { |
136 | this.parentNode.removeChild(this) | |
137 | } | |
138 | } | |
139 | Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton) | |
be6a4802 | 140 | |
a22bfc3e C |
141 | class WebTorrentButton extends Button { |
142 | createEl () { | |
be6a4802 | 143 | const div = document.createElement('div') |
a06a31c7 C |
144 | const subDiv = document.createElement('div') |
145 | div.appendChild(subDiv) | |
be6a4802 C |
146 | |
147 | const downloadIcon = document.createElement('span') | |
148 | downloadIcon.classList.add('icon', 'icon-download') | |
a06a31c7 | 149 | subDiv.appendChild(downloadIcon) |
be6a4802 C |
150 | |
151 | const downloadSpeedText = document.createElement('span') | |
152 | downloadSpeedText.classList.add('download-speed-text') | |
153 | const downloadSpeedNumber = document.createElement('span') | |
154 | downloadSpeedNumber.classList.add('download-speed-number') | |
155 | const downloadSpeedUnit = document.createElement('span') | |
156 | downloadSpeedText.appendChild(downloadSpeedNumber) | |
157 | downloadSpeedText.appendChild(downloadSpeedUnit) | |
a06a31c7 | 158 | subDiv.appendChild(downloadSpeedText) |
be6a4802 C |
159 | |
160 | const uploadIcon = document.createElement('span') | |
161 | uploadIcon.classList.add('icon', 'icon-upload') | |
a06a31c7 | 162 | subDiv.appendChild(uploadIcon) |
be6a4802 C |
163 | |
164 | const uploadSpeedText = document.createElement('span') | |
165 | uploadSpeedText.classList.add('upload-speed-text') | |
166 | const uploadSpeedNumber = document.createElement('span') | |
167 | uploadSpeedNumber.classList.add('upload-speed-number') | |
168 | const uploadSpeedUnit = document.createElement('span') | |
169 | uploadSpeedText.appendChild(uploadSpeedNumber) | |
170 | uploadSpeedText.appendChild(uploadSpeedUnit) | |
a06a31c7 | 171 | subDiv.appendChild(uploadSpeedText) |
be6a4802 C |
172 | |
173 | const peersText = document.createElement('span') | |
174 | peersText.textContent = ' peers' | |
175 | peersText.classList.add('peers-text') | |
176 | const peersNumber = document.createElement('span') | |
177 | peersNumber.classList.add('peers-number') | |
a06a31c7 C |
178 | subDiv.appendChild(peersNumber) |
179 | subDiv.appendChild(peersText) | |
be6a4802 C |
180 | |
181 | div.className = 'vjs-webtorrent' | |
182 | // Hide the stats before we get the info | |
a86309b4 | 183 | subDiv.className = 'vjs-webtorrent-hidden' |
be6a4802 | 184 | |
a22bfc3e | 185 | this.player_.peertube().on('torrentInfo', (event, data) => { |
be6a4802 C |
186 | const downloadSpeed = bytes(data.downloadSpeed) |
187 | const uploadSpeed = bytes(data.uploadSpeed) | |
188 | const numPeers = data.numPeers | |
189 | ||
190 | downloadSpeedNumber.textContent = downloadSpeed[0] | |
191 | downloadSpeedUnit.textContent = ' ' + downloadSpeed[1] | |
192 | ||
193 | uploadSpeedNumber.textContent = uploadSpeed[0] | |
194 | uploadSpeedUnit.textContent = ' ' + uploadSpeed[1] | |
195 | ||
196 | peersNumber.textContent = numPeers | |
197 | ||
a86309b4 | 198 | subDiv.className = 'vjs-webtorrent-displayed' |
be6a4802 C |
199 | }) |
200 | ||
201 | return div | |
202 | } | |
be6a4802 | 203 | |
a22bfc3e C |
204 | dispose () { |
205 | this.parentNode.removeChild(this) | |
206 | } | |
aa8b6df4 | 207 | } |
a22bfc3e C |
208 | Button.registerComponent('WebTorrentButton', WebTorrentButton) |
209 | ||
210 | const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin') | |
211 | class PeerTubePlugin extends Plugin { | |
212 | private player: any | |
213 | private currentVideoFile: VideoFile | |
214 | private playerElement: HTMLVideoElement | |
215 | private videoFiles: VideoFile[] | |
216 | private torrent: WebTorrent.Torrent | |
217 | ||
218 | constructor (player: VideoJSPlayer, options: PeertubePluginOptions) { | |
219 | super(player, options) | |
220 | ||
221 | this.videoFiles = options.videoFiles | |
222 | ||
223 | // Hack to "simulate" src link in video.js >= 6 | |
224 | // Without this, we can't play the video after pausing it | |
225 | // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 | |
226 | this.player.src = function () { | |
227 | return true | |
228 | } | |
229 | ||
230 | this.playerElement = options.playerElement | |
231 | ||
232 | this.player.ready(() => { | |
233 | this.initializePlayer(options) | |
234 | this.runTorrentInfoScheduler() | |
235 | }) | |
236 | } | |
237 | ||
238 | dispose () { | |
239 | // Don't need to destroy renderer, video player will be destroyed | |
240 | this.flushVideoFile(this.currentVideoFile, false) | |
aa8b6df4 C |
241 | } |
242 | ||
a22bfc3e C |
243 | getCurrentResolution () { |
244 | return this.currentVideoFile ? this.currentVideoFile.resolution : -1 | |
aa8b6df4 C |
245 | } |
246 | ||
a22bfc3e C |
247 | getCurrentResolutionLabel () { |
248 | return this.currentVideoFile ? this.currentVideoFile.resolutionLabel : '' | |
aa8b6df4 C |
249 | } |
250 | ||
a22bfc3e | 251 | updateVideoFile (videoFile?: VideoFile, done?: () => void) { |
aa8b6df4 C |
252 | if (done === undefined) { |
253 | done = () => { /* empty */ } | |
254 | } | |
255 | ||
256 | // Pick the first one | |
257 | if (videoFile === undefined) { | |
a22bfc3e | 258 | videoFile = this.videoFiles[0] |
aa8b6df4 C |
259 | } |
260 | ||
261 | // Don't add the same video file once again | |
a22bfc3e | 262 | if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) { |
aa8b6df4 C |
263 | return |
264 | } | |
265 | ||
a22bfc3e C |
266 | const previousVideoFile = this.currentVideoFile |
267 | this.currentVideoFile = videoFile | |
aa8b6df4 C |
268 | |
269 | console.log('Adding ' + videoFile.magnetUri + '.') | |
a22bfc3e | 270 | this.torrent = webtorrent.add(videoFile.magnetUri, torrent => { |
aa8b6df4 C |
271 | console.log('Added ' + videoFile.magnetUri + '.') |
272 | ||
273 | this.flushVideoFile(previousVideoFile) | |
274 | ||
275 | const options = { autoplay: true, controls: true } | |
a22bfc3e C |
276 | renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => { |
277 | if (err) return this.handleError(err) | |
aa8b6df4 C |
278 | |
279 | this.renderer = renderer | |
a22bfc3e | 280 | this.player.play().then(done) |
aa8b6df4 C |
281 | }) |
282 | }) | |
283 | ||
a22bfc3e C |
284 | this.torrent.on('error', err => this.handleError(err)) |
285 | this.torrent.on('warning', (err: any) => { | |
a96aed15 | 286 | // We don't support HTTP tracker but we don't care -> we use the web socket tracker |
8113a93a | 287 | if (err.message.indexOf('Unsupported tracker protocol: http') !== -1) return |
7dbdc3ba C |
288 | // Users don't care about issues with WebRTC, but developers do so log it in the console |
289 | if (err.message.indexOf('Ice connection failed') !== -1) { | |
290 | console.error(err) | |
291 | return | |
292 | } | |
a96aed15 | 293 | |
a22bfc3e | 294 | return this.handleError(err) |
a96aed15 | 295 | }) |
aa8b6df4 | 296 | |
a22bfc3e | 297 | this.trigger('videoFileUpdate') |
aa8b6df4 C |
298 | } |
299 | ||
a22bfc3e | 300 | updateResolution (resolution) { |
aa8b6df4 | 301 | // Remember player state |
a22bfc3e C |
302 | const currentTime = this.player.currentTime() |
303 | const isPaused = this.player.paused() | |
aa8b6df4 C |
304 | |
305 | // Hide bigPlayButton | |
8fa5653a | 306 | if (!isPaused) { |
a22bfc3e | 307 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
308 | } |
309 | ||
a22bfc3e C |
310 | const newVideoFile = this.videoFiles.find(f => f.resolution === resolution) |
311 | this.updateVideoFile(newVideoFile, () => { | |
312 | this.player.currentTime(currentTime) | |
313 | this.player.handleTechSeeked_() | |
aa8b6df4 C |
314 | }) |
315 | } | |
316 | ||
a22bfc3e | 317 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
aa8b6df4 C |
318 | if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { |
319 | if (destroyRenderer === true) this.renderer.destroy() | |
320 | webtorrent.remove(videoFile.magnetUri) | |
a22bfc3e | 321 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
322 | } |
323 | } | |
324 | ||
a22bfc3e C |
325 | setVideoFiles (files: VideoFile[]) { |
326 | this.videoFiles = files | |
ed9f9f5f | 327 | |
a22bfc3e | 328 | this.updateVideoFile(undefined, () => this.player.play()) |
ed9f9f5f C |
329 | } |
330 | ||
a22bfc3e C |
331 | private initializePlayer (options: PeertubePluginOptions) { |
332 | const controlBar = this.player.controlBar | |
aa8b6df4 | 333 | |
a22bfc3e | 334 | const menuButton = new ResolutionMenuButton(this.player, options) |
aa8b6df4 C |
335 | const fullscreenElement = controlBar.fullscreenToggle.el() |
336 | controlBar.resolutionSwitcher = controlBar.el().insertBefore(menuButton.el(), fullscreenElement) | |
aa8b6df4 C |
337 | |
338 | if (options.peerTubeLink === true) { | |
a22bfc3e | 339 | const peerTubeLinkButton = new PeertubeLinkButton(this.player) |
aa8b6df4 | 340 | controlBar.peerTubeLink = controlBar.el().insertBefore(peerTubeLinkButton.el(), fullscreenElement) |
aa8b6df4 C |
341 | } |
342 | ||
a22bfc3e | 343 | const webTorrentButton = new WebTorrentButton(this.player) |
be6a4802 | 344 | controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el()) |
be6a4802 | 345 | |
a22bfc3e C |
346 | if (this.player.options_.autoplay === true) { |
347 | this.updateVideoFile() | |
aa8b6df4 | 348 | } else { |
a22bfc3e | 349 | this.player.one('play', () => { |
85414add C |
350 | // On firefox, we need to wait to load the video before playing |
351 | if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) { | |
a22bfc3e C |
352 | this.player.pause() |
353 | this.updateVideoFile(undefined, () => this.player.play()) | |
85414add C |
354 | return |
355 | } | |
356 | ||
a22bfc3e | 357 | this.updateVideoFile(undefined) |
4dd551a0 | 358 | }) |
aa8b6df4 | 359 | } |
a22bfc3e | 360 | } |
aa8b6df4 | 361 | |
a22bfc3e | 362 | private runTorrentInfoScheduler () { |
aa8b6df4 | 363 | setInterval(() => { |
a22bfc3e C |
364 | if (this.torrent !== undefined) { |
365 | this.trigger('torrentInfo', { | |
366 | downloadSpeed: this.torrent.downloadSpeed, | |
367 | numPeers: this.torrent.numPeers, | |
368 | uploadSpeed: this.torrent.uploadSpeed | |
aa8b6df4 C |
369 | }) |
370 | } | |
371 | }, 1000) | |
a22bfc3e | 372 | } |
aa8b6df4 | 373 | |
a22bfc3e C |
374 | private handleError (err: Error | string) { |
375 | return this.player.trigger('customError', { err }) | |
aa8b6df4 C |
376 | } |
377 | } | |
a22bfc3e | 378 | videojsUntyped.registerPlugin('peertube', PeerTubePlugin) |