-const videojs = require('video.js')
+// FIXME: something weird with our path definition in tsconfig and typings
+// @ts-ignore
+import * as videojs from 'video.js'
+
import * as WebTorrent from 'webtorrent'
import { VideoFile } from '../../../../shared/models/videos/video.model'
import { renderVideo } from './video-renderer'
import './settings-menu-button'
import { PeertubePluginOptions, UserWatching, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from './utils'
-const CacheChunkStore = require('cache-chunk-store')
import { PeertubeChunkStore } from './peertube-chunk-store'
import {
getAverageBandwidthInStore,
+ getStoredLastSubtitle,
getStoredMute,
getStoredVolume,
getStoredWebTorrentEnabled,
saveAverageBandwidth,
+ saveLastSubtitle,
saveMuteInStore,
saveVolumeInStore
} from './peertube-player-local-storage'
+const CacheChunkStore = require('cache-chunk-store')
+
type PlayOptions = {
forcePlay?: boolean,
seek?: number,
private player: any
private currentVideoFile: VideoFile
- private torrent: any
+ private torrent: WebTorrent.Torrent
private videoCaptions: VideoJSCaption[]
+ private defaultSubtitle: string
private renderer: any
private fakeRenderer: any
- private destoyingFakeRenderer = false
+ private destroyingFakeRenderer = false
private autoResolution = true
private forbidAutoResolution = false
private downloadSpeeds: number[] = []
- constructor (player: any, options: PeertubePluginOptions) {
+ constructor (player: videojs.Player, options: PeertubePluginOptions) {
super(player, options)
// Disable auto play on iOS
if (this.autoplay === true) this.player.addClass('vjs-has-autoplay')
this.player.ready(() => {
+ const playerOptions = this.player.options_
+
const volume = getStoredVolume()
if (volume !== undefined) this.player.volume(volume)
- const muted = getStoredMute()
+
+ const muted = playerOptions.muted !== undefined ? playerOptions.muted : getStoredMute()
if (muted !== undefined) this.player.muted(muted)
+ this.defaultSubtitle = options.subtitle || getStoredLastSubtitle()
+
+ this.player.on('volumechange', () => {
+ saveVolumeInStore(this.player.volume())
+ saveMuteInStore(this.player.muted())
+ })
+
+ this.player.textTracks().on('change', () => {
+ const showing = this.player.textTracks().tracks_.find((t: { kind: string, mode: string }) => {
+ return t.kind === 'captions' && t.mode === 'showing'
+ })
+
+ if (!showing) {
+ saveLastSubtitle('off')
+ return
+ }
+
+ saveLastSubtitle(showing.language)
+ })
+
+ this.player.duration(options.videoDuration)
+
this.initializePlayer()
this.runTorrentInfoScheduler()
this.runViewAdd()
this.runAutoQualitySchedulerTimer = setTimeout(() => this.runAutoQualityScheduler(), this.CONSTANTS.AUTO_QUALITY_SCHEDULER)
})
})
-
- this.player.on('volumechange', () => {
- saveVolumeInStore(this.player.volume())
- saveMuteInStore(this.player.muted())
- })
}
dispose () {
const oldTorrent = this.torrent
const torrentOptions = {
- store: (chunkLength: any, storeOpts: any) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
+ store: (chunkLength: number, storeOpts: any) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), {
max: 100
})
}
this.flushVideoFile(previousVideoFile)
+ // Update progress bar (just for the UI), do not wait rendering
+ if (options.seek) this.player.currentTime(options.seek)
+
const renderVideoOptions = { autoplay: false, controls: true }
renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => {
this.renderer = renderer
if (err) return this.fallbackToHttp(options, done)
- return this.tryToPlay((err: Error) => {
+ return this.tryToPlay(err => {
if (err) return done(err)
if (options.seek) this.seek(options.seek)
})
}
- private tryToPlay (done?: Function) {
+ private tryToPlay (done?: (err?: Error) => void) {
if (!done) done = function () { /* empty */ }
const playPromise = this.player.play()
return this.videoFiles[Math.floor(this.videoFiles.length / 2)]
}
- private stopTorrent (torrent: any) {
+ private stopTorrent (torrent: WebTorrent.Torrent) {
torrent.pause()
// Pause does not remove actual peers (in particular the webseed peer)
torrent.removePeer(torrent[ 'ws' ])
}
private renderFileInFakeElement (file: WebTorrent.TorrentFile, delay: number) {
- this.destoyingFakeRenderer = false
+ this.destroyingFakeRenderer = false
const fakeVideoElem = document.createElement('video')
renderVideo(file, fakeVideoElem, { autoplay: false, controls: false }, (err, renderer) => {
this.fakeRenderer = renderer
// The renderer returns an error when we destroy it, so skip them
- if (this.destoyingFakeRenderer === false && err) {
+ if (this.destroyingFakeRenderer === false && err) {
console.error('Cannot render new torrent in fake video element.', err)
}
private destroyFakeRenderer () {
if (this.fakeRenderer) {
- this.destoyingFakeRenderer = true
+ this.destroyingFakeRenderer = true
if (this.fakeRenderer.destroy) {
try {
label: caption.label,
language: caption.language,
id: caption.language,
- src: caption.src
+ src: caption.src,
+ default: this.defaultSubtitle === caption.language
}, false)
}
}