// @ts-ignore
import * as videojs from 'video.js'
import './videojs-components/settings-menu-button'
-import { PeerTubePluginOptions, UserWatching, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
+import {
+ PeerTubePluginOptions,
+ ResolutionUpdateData,
+ UserWatching,
+ VideoJSCaption,
+ VideoJSComponentInterface,
+ videojsUntyped
+} from './peertube-videojs-typings'
import { isMobile, timeToInt } from './utils'
import {
getStoredLastSubtitle,
const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
class PeerTubePlugin extends Plugin {
- private readonly autoplay: boolean = false
private readonly startTime: number = 0
private readonly videoViewUrl: string
private readonly videoDuration: number
private videoViewInterval: any
private userWatchingVideoInterval: any
private qualityObservationTimer: any
+ private lastResolutionChange: ResolutionUpdateData
constructor (player: videojs.Player, options: PeerTubePluginOptions) {
super(player, options)
this.videoDuration = options.videoDuration
this.videoCaptions = options.videoCaptions
- if (this.autoplay === true) this.player.addClass('vjs-has-autoplay')
+ if (options.autoplay === true) this.player.addClass('vjs-has-autoplay')
+
+ this.player.on('autoplay-failure', () => {
+ this.player.removeClass('vjs-has-autoplay')
+ })
this.player.ready(() => {
const playerOptions = this.player.options_
+ if (options.mode === 'webtorrent') {
+ this.player.webtorrent().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
+ this.player.webtorrent().on('autoResolutionChange', (_: any, d: any) => this.trigger('autoResolutionChange', d))
+ }
+
+ if (options.mode === 'p2p-media-loader') {
+ this.player.p2pMediaLoader().on('resolutionChange', (_: any, d: any) => this.handleResolutionChange(d))
+ }
+
+ this.player.tech_.on('loadedqualitydata', () => {
+ setTimeout(() => {
+ // Replay a resolution change, now we loaded all quality data
+ if (this.lastResolutionChange) this.handleResolutionChange(this.lastResolutionChange)
+ }, 0)
+ })
+
const volume = getStoredVolume()
if (volume !== undefined) this.player.volume(volume)
return fetch(url, { method: 'PUT', body, headers })
}
+ private handleResolutionChange (data: ResolutionUpdateData) {
+ this.lastResolutionChange = data
+
+ const qualityLevels = this.player.qualityLevels()
+
+ for (let i = 0; i < qualityLevels.length; i++) {
+ if (qualityLevels[i].height === data.resolutionId) {
+ data.id = qualityLevels[i].id
+ break
+ }
+ }
+
+ this.trigger('resolutionChange', data)
+ }
+
private alterInactivity () {
let saveInactivityTimeout: number