]>
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 C |
304 | |
305 | console.log('Adding ' + videoFile.magnetUri + '.') | |
a22bfc3e | 306 | this.torrent = webtorrent.add(videoFile.magnetUri, torrent => { |
aa8b6df4 C |
307 | console.log('Added ' + videoFile.magnetUri + '.') |
308 | ||
309 | this.flushVideoFile(previousVideoFile) | |
310 | ||
311 | const options = { autoplay: true, controls: true } | |
a22bfc3e | 312 | renderVideo(torrent.files[0], this.playerElement, options,(err, renderer) => { |
bf5685f0 | 313 | if (err) return this.fallbackToHttp() |
aa8b6df4 C |
314 | |
315 | this.renderer = renderer | |
3bcfff7f C |
316 | if (!this.player.paused()) { |
317 | const playPromise = this.player.play() | |
318 | if (playPromise !== undefined) return playPromise.then(done) | |
319 | ||
320 | return done() | |
321 | } | |
322 | ||
323 | return done() | |
aa8b6df4 C |
324 | }) |
325 | }) | |
326 | ||
a22bfc3e C |
327 | this.torrent.on('error', err => this.handleError(err)) |
328 | this.torrent.on('warning', (err: any) => { | |
a96aed15 | 329 | // We don't support HTTP tracker but we don't care -> we use the web socket tracker |
531ab5b6 | 330 | if (err.message.indexOf('Unsupported tracker protocol') !== -1) return |
7dbdc3ba C |
331 | // Users don't care about issues with WebRTC, but developers do so log it in the console |
332 | if (err.message.indexOf('Ice connection failed') !== -1) { | |
333 | console.error(err) | |
334 | return | |
335 | } | |
a96aed15 | 336 | |
a22bfc3e | 337 | return this.handleError(err) |
a96aed15 | 338 | }) |
aa8b6df4 | 339 | |
a22bfc3e | 340 | this.trigger('videoFileUpdate') |
aa8b6df4 C |
341 | } |
342 | ||
09700934 | 343 | updateResolution (resolutionId: number) { |
aa8b6df4 | 344 | // Remember player state |
a22bfc3e C |
345 | const currentTime = this.player.currentTime() |
346 | const isPaused = this.player.paused() | |
aa8b6df4 | 347 | |
531ab5b6 C |
348 | // Remove poster to have black background |
349 | this.playerElement.poster = '' | |
350 | ||
aa8b6df4 | 351 | // Hide bigPlayButton |
8fa5653a | 352 | if (!isPaused) { |
a22bfc3e | 353 | this.player.bigPlayButton.hide() |
aa8b6df4 C |
354 | } |
355 | ||
09700934 | 356 | const newVideoFile = this.videoFiles.find(f => f.resolution.id === resolutionId) |
a22bfc3e C |
357 | this.updateVideoFile(newVideoFile, () => { |
358 | this.player.currentTime(currentTime) | |
359 | this.player.handleTechSeeked_() | |
aa8b6df4 C |
360 | }) |
361 | } | |
362 | ||
a22bfc3e | 363 | flushVideoFile (videoFile: VideoFile, destroyRenderer = true) { |
aa8b6df4 | 364 | if (videoFile !== undefined && webtorrent.get(videoFile.magnetUri)) { |
bf5685f0 C |
365 | if (destroyRenderer === true && this.renderer && this.renderer.destroy) this.renderer.destroy() |
366 | ||
aa8b6df4 | 367 | webtorrent.remove(videoFile.magnetUri) |
a22bfc3e | 368 | console.log('Removed ' + videoFile.magnetUri) |
aa8b6df4 C |
369 | } |
370 | } | |
371 | ||
3bcfff7f | 372 | setVideoFiles (files: VideoFile[], videoViewUrl: string, videoDuration: number) { |
8cac1b64 | 373 | this.videoViewUrl = videoViewUrl |
3bcfff7f | 374 | this.videoDuration = videoDuration |
a22bfc3e | 375 | this.videoFiles = files |
ed9f9f5f | 376 | |
8cac1b64 | 377 | // Re run view add for the new video |
3bcfff7f | 378 | this.runViewAdd() |
a22bfc3e | 379 | this.updateVideoFile(undefined, () => this.player.play()) |
ed9f9f5f C |
380 | } |
381 | ||
a22bfc3e | 382 | private initializePlayer (options: PeertubePluginOptions) { |
481d3596 C |
383 | if (this.autoplay === true) { |
384 | this.updateVideoFile(undefined, () => this.player.play()) | |
aa8b6df4 | 385 | } else { |
a22bfc3e | 386 | this.player.one('play', () => { |
481d3596 C |
387 | this.player.pause() |
388 | this.updateVideoFile(undefined, () => this.player.play()) | |
4dd551a0 | 389 | }) |
aa8b6df4 | 390 | } |
a22bfc3e | 391 | } |
aa8b6df4 | 392 | |
a22bfc3e | 393 | private runTorrentInfoScheduler () { |
3bcfff7f | 394 | this.torrentInfoInterval = setInterval(() => { |
bf5685f0 C |
395 | // Not initialized yet |
396 | if (this.torrent === undefined) return | |
397 | ||
398 | // Http fallback | |
399 | if (this.torrent === null) return this.trigger('torrentInfo', false) | |
400 | ||
401 | return this.trigger('torrentInfo', { | |
402 | downloadSpeed: this.torrent.downloadSpeed, | |
403 | numPeers: this.torrent.numPeers, | |
404 | uploadSpeed: this.torrent.uploadSpeed | |
405 | }) | |
aa8b6df4 | 406 | }, 1000) |
a22bfc3e | 407 | } |
aa8b6df4 | 408 | |
8cac1b64 C |
409 | private runViewAdd () { |
410 | this.clearVideoViewInterval() | |
411 | ||
412 | // After 30 seconds (or 3/4 of the video), add a view to the video | |
413 | let minSecondsToView = 30 | |
414 | ||
3bcfff7f | 415 | if (this.videoDuration < minSecondsToView) minSecondsToView = (this.videoDuration * 3) / 4 |
8cac1b64 C |
416 | |
417 | let secondsViewed = 0 | |
418 | this.videoViewInterval = setInterval(() => { | |
419 | if (this.player && !this.player.paused()) { | |
420 | secondsViewed += 1 | |
421 | ||
422 | if (secondsViewed > minSecondsToView) { | |
423 | this.clearVideoViewInterval() | |
424 | ||
425 | this.addViewToVideo().catch(err => console.error(err)) | |
426 | } | |
427 | } | |
428 | }, 1000) | |
429 | } | |
430 | ||
431 | private clearVideoViewInterval () { | |
432 | if (this.videoViewInterval !== undefined) { | |
433 | clearInterval(this.videoViewInterval) | |
434 | this.videoViewInterval = undefined | |
435 | } | |
436 | } | |
437 | ||
438 | private addViewToVideo () { | |
439 | return fetch(this.videoViewUrl, { method: 'POST' }) | |
440 | } | |
441 | ||
bf5685f0 C |
442 | private fallbackToHttp () { |
443 | this.flushVideoFile(this.currentVideoFile, true) | |
444 | this.torrent = null | |
445 | ||
446 | // Enable error display now this is our last fallback | |
447 | this.player.one('error', () => this.enableErrorDisplay()) | |
448 | ||
449 | const httpUrl = this.currentVideoFile.fileUrl | |
450 | this.player.src = this.savePlayerSrcFunction | |
451 | this.player.src(httpUrl) | |
452 | this.player.play() | |
453 | } | |
454 | ||
a22bfc3e C |
455 | private handleError (err: Error | string) { |
456 | return this.player.trigger('customError', { err }) | |
aa8b6df4 | 457 | } |
bf5685f0 C |
458 | |
459 | private enableErrorDisplay () { | |
460 | this.player.addClass('vjs-error-display-enabled') | |
461 | } | |
462 | ||
463 | private disableErrorDisplay () { | |
464 | this.player.removeClass('vjs-error-display-enabled') | |
465 | } | |
aa8b6df4 | 466 | } |
a22bfc3e | 467 | videojsUntyped.registerPlugin('peertube', PeerTubePlugin) |