]>
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' |
09700934 | 5 | import { VideoConstant, VideoResolution } from '../../../../shared/models/videos' |
b6827820 | 6 | import { VideoFile } from '../../../../shared/models/videos/video.model' |
aa8b6df4 | 7 | import { renderVideo } from './video-renderer' |
be6a4802 | 8 | |
339632b4 C |
9 | declare module 'video.js' { |
10 | interface Player { | |
11 | peertube (): PeerTubePlugin | |
12 | } | |
13 | } | |
14 | ||
a22bfc3e | 15 | interface VideoJSComponentInterface { |
339632b4 | 16 | _player: videojs.Player |
a22bfc3e | 17 | |
339632b4 | 18 | new (player: videojs.Player, options?: any) |
a22bfc3e C |
19 | |
20 | registerComponent (name: string, obj: any) | |
21 | } | |
22 | ||
a22bfc3e C |
23 | type PeertubePluginOptions = { |
24 | videoFiles: VideoFile[] | |
25 | playerElement: HTMLVideoElement | |
8cac1b64 | 26 | videoViewUrl: string |
3bcfff7f | 27 | videoDuration: number |
a22bfc3e C |
28 | } |
29 | ||
be6a4802 C |
30 | // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts |
31 | // Don't import all Angular stuff, just copy the code with shame | |
32 | const dictionaryBytes: Array<{max: number, type: string}> = [ | |
33 | { max: 1024, type: 'B' }, | |
34 | { max: 1048576, type: 'KB' }, | |
35 | { max: 1073741824, type: 'MB' }, | |
36 | { max: 1.0995116e12, type: 'GB' } | |
37 | ] | |
38 | function bytes (value) { | |
39 | const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] | |
40 | const calc = Math.floor(value / (format.max / 1024)).toString() | |
41 | ||
42 | return [ calc, format.type ] | |
43 | } | |
aa8b6df4 C |
44 | |
45 | // videojs typings don't have some method we need | |
46 | const videojsUntyped = videojs as any | |
47 | const webtorrent = new WebTorrent({ dht: false }) | |
48 | ||
a22bfc3e C |
49 | const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem') |
50 | class ResolutionMenuItem extends MenuItem { | |
51 | ||
339632b4 | 52 | constructor (player: videojs.Player, options) { |
aa8b6df4 | 53 | options.selectable = true |
a22bfc3e | 54 | super(player, options) |
aa8b6df4 | 55 | |
09700934 C |
56 | const currentResolutionId = this.player_.peertube().getCurrentResolutionId() |
57 | this.selected(this.options_.id === currentResolutionId) | |
a22bfc3e | 58 | } |
aa8b6df4 | 59 | |
a22bfc3e | 60 | handleClick (event) { |
531ab5b6 C |
61 | super.handleClick(event) |
62 | ||
a22bfc3e | 63 | this.player_.peertube().updateResolution(this.options_.id) |
aa8b6df4 | 64 | } |
a22bfc3e | 65 | } |
aa8b6df4 C |
66 | MenuItem.registerComponent('ResolutionMenuItem', ResolutionMenuItem) |
67 | ||
a22bfc3e C |
68 | const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuButton') |
69 | class ResolutionMenuButton extends MenuButton { | |
70 | label: HTMLElement | |
71 | ||
339632b4 | 72 | constructor (player: videojs.Player, options) { |
aa8b6df4 | 73 | options.label = 'Quality' |
a22bfc3e C |
74 | super(player, options) |
75 | ||
76 | this.label = document.createElement('span') | |
aa8b6df4 | 77 | |
aa8b6df4 C |
78 | this.el().setAttribute('aria-label', 'Quality') |
79 | this.controlText('Quality') | |
80 | ||
81 | videojsUntyped.dom.addClass(this.label, 'vjs-resolution-button-label') | |
82 | this.el().appendChild(this.label) | |
83 | ||
a22bfc3e C |
84 | player.peertube().on('videoFileUpdate', () => this.update()) |
85 | } | |
aa8b6df4 | 86 | |
a22bfc3e | 87 | createItems () { |
aa8b6df4 | 88 | const menuItems = [] |
a22bfc3e | 89 | for (const videoFile of this.player_.peertube().videoFiles) { |
aa8b6df4 C |
90 | menuItems.push(new ResolutionMenuItem( |
91 | this.player_, | |
92 | { | |
09700934 C |
93 | id: videoFile.resolution.id, |
94 | label: videoFile.resolution.label, | |
aa8b6df4 | 95 | src: videoFile.magnetUri, |
09700934 | 96 | selected: videoFile.resolution.id === this.currentSelectionId |
aa8b6df4 C |
97 | }) |
98 | ) | |
99 | } | |
100 | ||
101 | return menuItems | |
a22bfc3e C |
102 | } |
103 | ||
104 | update () { | |
105 | if (!this.label) return | |
aa8b6df4 | 106 | |
a22bfc3e | 107 | this.label.innerHTML = this.player_.peertube().getCurrentResolutionLabel() |
be6a4802 | 108 | this.hide() |
a22bfc3e C |
109 | return super.update() |
110 | } | |
111 | ||
112 | buildCSSClass () { | |
113 | return super.buildCSSClass() + ' vjs-resolution-button' | |
114 | } | |
a22bfc3e | 115 | } |
aa8b6df4 C |
116 | MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton) |
117 | ||
a22bfc3e | 118 | const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') |
3ec8dc09 | 119 | class PeerTubeLinkButton extends Button { |
aa8b6df4 | 120 | |
a22bfc3e | 121 | createEl () { |
aa8b6df4 C |
122 | const link = document.createElement('a') |
123 | link.href = window.location.href.replace('embed', 'watch') | |
124 | link.innerHTML = 'PeerTube' | |
125 | link.title = 'Go to the video page' | |
126 | link.className = 'vjs-peertube-link' | |
127 | link.target = '_blank' | |
128 | ||
129 | return link | |
a22bfc3e | 130 | } |
aa8b6df4 | 131 | |
a22bfc3e | 132 | handleClick () { |
be6a4802 | 133 | this.player_.pause() |
aa8b6df4 | 134 | } |
a22bfc3e | 135 | } |
3ec8dc09 | 136 | Button.registerComponent('PeerTubeLinkButton', PeerTubeLinkButton) |
be6a4802 | 137 | |
a22bfc3e C |
138 | class WebTorrentButton extends Button { |
139 | createEl () { | |
be6a4802 | 140 | const div = document.createElement('div') |
bf5685f0 C |
141 | const subDivWebtorrent = document.createElement('div') |
142 | div.appendChild(subDivWebtorrent) | |
be6a4802 C |
143 | |
144 | const downloadIcon = document.createElement('span') | |
145 | downloadIcon.classList.add('icon', 'icon-download') | |
bf5685f0 | 146 | subDivWebtorrent.appendChild(downloadIcon) |
be6a4802 C |
147 | |
148 | const downloadSpeedText = document.createElement('span') | |
149 | downloadSpeedText.classList.add('download-speed-text') | |
150 | const downloadSpeedNumber = document.createElement('span') | |
151 | downloadSpeedNumber.classList.add('download-speed-number') | |
152 | const downloadSpeedUnit = document.createElement('span') | |
153 | downloadSpeedText.appendChild(downloadSpeedNumber) | |
154 | downloadSpeedText.appendChild(downloadSpeedUnit) | |
bf5685f0 | 155 | subDivWebtorrent.appendChild(downloadSpeedText) |
be6a4802 C |
156 | |
157 | const uploadIcon = document.createElement('span') | |
158 | uploadIcon.classList.add('icon', 'icon-upload') | |
bf5685f0 | 159 | subDivWebtorrent.appendChild(uploadIcon) |
be6a4802 C |
160 | |
161 | const uploadSpeedText = document.createElement('span') | |
162 | uploadSpeedText.classList.add('upload-speed-text') | |
163 | const uploadSpeedNumber = document.createElement('span') | |
164 | uploadSpeedNumber.classList.add('upload-speed-number') | |
165 | const uploadSpeedUnit = document.createElement('span') | |
166 | uploadSpeedText.appendChild(uploadSpeedNumber) | |
167 | uploadSpeedText.appendChild(uploadSpeedUnit) | |
bf5685f0 | 168 | subDivWebtorrent.appendChild(uploadSpeedText) |
be6a4802 C |
169 | |
170 | const peersText = document.createElement('span') | |
be6a4802 C |
171 | peersText.classList.add('peers-text') |
172 | const peersNumber = document.createElement('span') | |
173 | peersNumber.classList.add('peers-number') | |
bf5685f0 C |
174 | subDivWebtorrent.appendChild(peersNumber) |
175 | subDivWebtorrent.appendChild(peersText) | |
be6a4802 | 176 | |
bf5685f0 | 177 | div.className = 'vjs-peertube' |
be6a4802 | 178 | // Hide the stats before we get the info |
bf5685f0 C |
179 | subDivWebtorrent.className = 'vjs-peertube-hidden' |
180 | ||
181 | const subDivHttp = document.createElement('div') | |
182 | subDivHttp.className = 'vjs-peertube-hidden' | |
183 | const subDivHttpText = document.createElement('span') | |
184 | subDivHttpText.classList.add('peers-number') | |
185 | subDivHttpText.textContent = 'HTTP' | |
186 | const subDivFallbackText = document.createElement('span') | |
187 | subDivFallbackText.classList.add('peers-text') | |
188 | subDivFallbackText.textContent = ' fallback' | |
189 | ||
190 | subDivHttp.appendChild(subDivHttpText) | |
191 | subDivHttp.appendChild(subDivFallbackText) | |
192 | div.appendChild(subDivHttp) | |
be6a4802 | 193 | |
a22bfc3e | 194 | this.player_.peertube().on('torrentInfo', (event, data) => { |
bf5685f0 C |
195 | // We are in HTTP fallback |
196 | if (!data) { | |
197 | subDivHttp.className = 'vjs-peertube-displayed' | |
198 | subDivWebtorrent.className = 'vjs-peertube-hidden' | |
199 | ||
200 | return | |
201 | } | |
202 | ||
be6a4802 C |
203 | const downloadSpeed = bytes(data.downloadSpeed) |
204 | const uploadSpeed = bytes(data.uploadSpeed) | |
205 | const numPeers = data.numPeers | |
206 | ||
bf5685f0 C |
207 | downloadSpeedNumber.textContent = downloadSpeed[ 0 ] |
208 | downloadSpeedUnit.textContent = ' ' + downloadSpeed[ 1 ] | |
be6a4802 | 209 | |
bf5685f0 C |
210 | uploadSpeedNumber.textContent = uploadSpeed[ 0 ] |
211 | uploadSpeedUnit.textContent = ' ' + uploadSpeed[ 1 ] | |
be6a4802 C |
212 | |
213 | peersNumber.textContent = numPeers | |
bf5685f0 | 214 | peersText.textContent = ' peers' |
be6a4802 | 215 | |
bf5685f0 C |
216 | subDivHttp.className = 'vjs-peertube-hidden' |
217 | subDivWebtorrent.className = 'vjs-peertube-displayed' | |
be6a4802 C |
218 | }) |
219 | ||
220 | return div | |
221 | } | |
aa8b6df4 | 222 | } |
a22bfc3e C |
223 | Button.registerComponent('WebTorrentButton', WebTorrentButton) |
224 | ||
225 | const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin') | |
226 | class PeerTubePlugin extends Plugin { | |
227 | private player: any | |
228 | private currentVideoFile: VideoFile | |
229 | private playerElement: HTMLVideoElement | |
230 | private videoFiles: VideoFile[] | |
231 | private torrent: WebTorrent.Torrent | |
481d3596 | 232 | private autoplay = false |
8cac1b64 | 233 | private videoViewUrl: string |
3bcfff7f | 234 | private videoDuration: number |
8cac1b64 | 235 | private videoViewInterval |
3bcfff7f | 236 | private torrentInfoInterval |
bf5685f0 | 237 | private savePlayerSrcFunction: Function |
a22bfc3e | 238 | |
339632b4 | 239 | constructor (player: videojs.Player, options: PeertubePluginOptions) { |
a22bfc3e C |
240 | super(player, options) |
241 | ||
481d3596 C |
242 | // Fix canplay event on google chrome by disabling default videojs autoplay |
243 | this.autoplay = this.player.options_.autoplay | |
244 | this.player.options_.autoplay = false | |
245 | ||
a22bfc3e | 246 | this.videoFiles = options.videoFiles |
8cac1b64 | 247 | this.videoViewUrl = options.videoViewUrl |
3bcfff7f | 248 | this.videoDuration = options.videoDuration |
a22bfc3e | 249 | |
bf5685f0 | 250 | this.savePlayerSrcFunction = this.player.src |
a22bfc3e C |
251 | // Hack to "simulate" src link in video.js >= 6 |
252 | // Without this, we can't play the video after pausing it | |
253 | // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633 | |
bf5685f0 | 254 | this.player.src = () => true |
a22bfc3e C |
255 | |
256 | this.playerElement = options.playerElement | |
257 | ||
258 | this.player.ready(() => { | |
259 | this.initializePlayer(options) | |
260 | this.runTorrentInfoScheduler() | |
3bcfff7f | 261 | this.runViewAdd() |
a22bfc3e C |
262 | }) |
263 | } | |
264 | ||
265 | dispose () { | |
3bcfff7f C |
266 | clearInterval(this.videoViewInterval) |
267 | clearInterval(this.torrentInfoInterval) | |
268 | ||
a22bfc3e C |
269 | // Don't need to destroy renderer, video player will be destroyed |
270 | this.flushVideoFile(this.currentVideoFile, false) | |
aa8b6df4 C |
271 | } |
272 | ||
09700934 C |
273 | getCurrentResolutionId () { |
274 | return this.currentVideoFile ? this.currentVideoFile.resolution.id : -1 | |
aa8b6df4 C |
275 | } |
276 | ||
a22bfc3e | 277 | getCurrentResolutionLabel () { |
09700934 | 278 | return this.currentVideoFile ? this.currentVideoFile.resolution.label : '' |
aa8b6df4 C |
279 | } |
280 | ||
a22bfc3e | 281 | updateVideoFile (videoFile?: VideoFile, done?: () => void) { |
aa8b6df4 C |
282 | if (done === undefined) { |
283 | done = () => { /* empty */ } | |
284 | } | |
285 | ||
286 | // Pick the first one | |
287 | if (videoFile === undefined) { | |
a22bfc3e | 288 | videoFile = this.videoFiles[0] |
aa8b6df4 C |
289 | } |
290 | ||
291 | // Don't add the same video file once again | |
a22bfc3e | 292 | if (this.currentVideoFile !== undefined && this.currentVideoFile.magnetUri === videoFile.magnetUri) { |
aa8b6df4 C |
293 | return |
294 | } | |
295 | ||
bf5685f0 C |
296 | // Do not display error to user because we will have multiple fallbacks |
297 | this.disableErrorDisplay() | |
1198a08c | 298 | |
bf5685f0 | 299 | this.player.src = () => true |
1198a08c | 300 | this.player.playbackRate(1) |
bf5685f0 | 301 | |
a22bfc3e C |
302 | const previousVideoFile = this.currentVideoFile |
303 | this.currentVideoFile = videoFile | |
aa8b6df4 | 304 | |
a216c623 C |
305 | this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, done) |
306 | ||
307 | this.trigger('videoFileUpdate') | |
308 | } | |
309 | ||
310 | addTorrent (magnetOrTorrentUrl: string, previousVideoFile: VideoFile, done: Function) { | |
311 | console.log('Adding ' + magnetOrTorrentUrl + '.') | |
312 | ||
313 | this.torrent = webtorrent.add(magnetOrTorrentUrl, torrent => { | |
314 | console.log('Added ' + magnetOrTorrentUrl + '.') | |
aa8b6df4 C |
315 | |
316 | this.flushVideoFile(previousVideoFile) | |
317 | ||
318 | const options = { autoplay: true, controls: true } | |
a22bfc3e | 319 | renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => { |
bf5685f0 | 320 | if (err) return this.fallbackToHttp() |
aa8b6df4 C |
321 | |
322 | this.renderer = renderer | |
3bcfff7f C |
323 | if (!this.player.paused()) { |
324 | const playPromise = this.player.play() | |
325 | if (playPromise !== undefined) return playPromise.then(done) | |
326 | ||
327 | return done() | |
328 | } | |
329 | ||
330 | return done() | |
aa8b6df4 C |
331 | }) |
332 | }) | |
333 | ||
a22bfc3e | 334 | this.torrent.on('error', err => this.handleError(err)) |
a216c623 | 335 | |
a22bfc3e | 336 | this.torrent.on('warning', (err: any) => { |
a96aed15 | 337 | // We don't support HTTP tracker but we don't care -> we use the web socket tracker |
531ab5b6 | 338 | if (err.message.indexOf('Unsupported tracker protocol') !== -1) return |
a216c623 | 339 | |
7dbdc3ba C |
340 | // Users don't care about issues with WebRTC, but developers do so log it in the console |
341 | if (err.message.indexOf('Ice connection failed') !== -1) { | |
342 | console.error(err) | |
343 | return | |
344 | } | |
a96aed15 | 345 | |
a216c623 C |
346 | // Magnet hash is not up to date with the torrent file, add directly the torrent file |
347 | if (err.message.indexOf('incorrect info hash') !== -1) { | |
348 | console.error('Incorrect info hash detected, falling back to torrent file.') | |
349 | return this.addTorrent(this.torrent['xs'], previousVideoFile, done) | |
350 | } | |
351 | ||
a22bfc3e | 352 | return this.handleError(err) |
a96aed15 | 353 | }) |
aa8b6df4 C |
354 | } |
355 | ||
09700934 | 356 | updateResolution (resolutionId: number) { |
aa8b6df4 | 357 | // Remember player state |
a22bfc3e C |
358 | const currentTime = this.player.currentTime() |
359 | const isPaused = this.player.paused() | |
aa8b6df4 | 360 | |
531ab5b6 C |
361 | // Remove poster to have black background |
362 | this.playerElement.poster = '' | |
363 | ||
aa8b6df4 | 364 | // Hide bigPlayButton |
8fa5653a | 365 | if (!isPaused) { |
a22bfc3e | 366 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
367 | } |
368 | ||
09700934 | 369 | const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId) |
a22bfc3e C |
370 | this.updateVideoFile(newVideoFile, () => { |
371 | this.player.currentTime(currentTime) | |
372 | this.player.handleTechSeeked_() | |
aa8b6df4 C |
373 | }) |
374 | } | |
375 | ||
a22bfc3e | 376 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
aa8b6df4 | 377 | if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { |
bf5685f0 C |
378 | if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy() |
379 | ||
aa8b6df4 | 380 | webtorrent.remove(videoFile.magnetUri) |
a22bfc3e | 381 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
382 | } |
383 | } | |
384 | ||
3bcfff7f | 385 | setVideoFiles (files: VideoFile[], videoViewUrl: string, videoDuration: number) { |
8cac1b64 | 386 | this.videoViewUrl = videoViewUrl |
3bcfff7f | 387 | this.videoDuration = videoDuration |
a22bfc3e | 388 | this.videoFiles = files |
ed9f9f5f | 389 | |
8cac1b64 | 390 | // Re run view add for the new video |
3bcfff7f | 391 | this.runViewAdd() |
a22bfc3e | 392 | this.updateVideoFile(undefined, () => this.player.play()) |
ed9f9f5f C |
393 | } |
394 | ||
a22bfc3e | 395 | private initializePlayer (options: PeertubePluginOptions) { |
481d3596 C |
396 | if (this.autoplay === true) { |
397 | this.updateVideoFile(undefined, () => this.player.play()) | |
aa8b6df4 | 398 | } else { |
a22bfc3e | 399 | this.player.one('play', () => { |
481d3596 C |
400 | this.player.pause() |
401 | this.updateVideoFile(undefined, () => this.player.play()) | |
4dd551a0 | 402 | }) |
aa8b6df4 | 403 | } |
a22bfc3e | 404 | } |
aa8b6df4 | 405 | |
a22bfc3e | 406 | private runTorrentInfoScheduler () { |
3bcfff7f | 407 | this.torrentInfoInterval = setInterval(() => { |
bf5685f0 C |
408 | // Not initialized yet |
409 | if (this.torrent === undefined) return | |
410 | ||
411 | // Http fallback | |
412 | if (this.torrent === null) return this.trigger('torrentInfo', false) | |
413 | ||
414 | return this.trigger('torrentInfo', { | |
415 | downloadSpeed: this.torrent.downloadSpeed, | |
416 | numPeers: this.torrent.numPeers, | |
417 | uploadSpeed: this.torrent.uploadSpeed | |
418 | }) | |
aa8b6df4 | 419 | }, 1000) |
a22bfc3e | 420 | } |
aa8b6df4 | 421 | |
8cac1b64 C |
422 | private runViewAdd () { |
423 | this.clearVideoViewInterval() | |
424 | ||
425 | // After 30 seconds (or 3/4 of the video), add a view to the video | |
426 | let minSecondsToView = 30 | |
427 | ||
3bcfff7f | 428 | if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4 |
8cac1b64 C |
429 | |
430 | let secondsViewed = 0 | |
431 | this.videoViewInterval = setInterval(() => { | |
432 | if (this.player && !this.player.paused()) { | |
433 | secondsViewed += 1 | |
434 | ||
435 | if (secondsViewed > minSecondsToView) { | |
436 | this.clearVideoViewInterval() | |
437 | ||
438 | this.addViewToVideo().catch(err => console.error(err)) | |
439 | } | |
440 | } | |
441 | }, 1000) | |
442 | } | |
443 | ||
444 | private clearVideoViewInterval () { | |
445 | if (this.videoViewInterval !== undefined) { | |
446 | clearInterval(this.videoViewInterval) | |
447 | this.videoViewInterval = undefined | |
448 | } | |
449 | } | |
450 | ||
451 | private addViewToVideo () { | |
452 | return fetch(this.videoViewUrl, { method: 'POST' }) | |
453 | } | |
454 | ||
bf5685f0 C |
455 | private fallbackToHttp () { |
456 | this.flushVideoFile(this.currentVideoFile, true) | |
457 | this.torrent = null | |
458 | ||
459 | // Enable error display now this is our last fallback | |
460 | this.player.one('error', () => this.enableErrorDisplay()) | |
461 | ||
462 | const httpUrl = this.currentVideoFile.fileUrl | |
463 | this.player.src = this.savePlayerSrcFunction | |
464 | this.player.src(httpUrl) | |
465 | this.player.play() | |
466 | } | |
467 | ||
a22bfc3e C |
468 | private handleError (err: Error | string) { |
469 | return this.player.trigger('customError', { err }) | |
aa8b6df4 | 470 | } |
bf5685f0 C |
471 | |
472 | private enableErrorDisplay () { | |
473 | this.player.addClass('vjs-error-display-enabled') | |
474 | } | |
475 | ||
476 | private disableErrorDisplay () { | |
477 | this.player.removeClass('vjs-error-display-enabled') | |
478 | } | |
aa8b6df4 | 479 | } |
a22bfc3e | 480 | videojsUntyped.registerPlugin('peertube', PeerTubePlugin) |