- createEl: function () {
- const link = document.createElement('a')
- link.href = window.location.href.replace('embed', 'watch')
- link.innerHTML = 'PeerTube'
- link.title = 'Go to the video page'
- link.className = 'vjs-peertube-link'
- link.target = '_blank'
-
- return link
- },
-
- handleClick: function () {
- this.player_.pause()
- }
-})
-Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton)
-
-const WebTorrentButton = videojsUntyped.extend(Button, {
- constructor: function (player) {
- Button.call(this, player)
- },
-
- createEl: function () {
- const div = document.createElement('div')
-
- const downloadIcon = document.createElement('span')
- downloadIcon.classList.add('icon', 'icon-download')
- div.appendChild(downloadIcon)
-
- const downloadSpeedText = document.createElement('span')
- downloadSpeedText.classList.add('download-speed-text')
- const downloadSpeedNumber = document.createElement('span')
- downloadSpeedNumber.classList.add('download-speed-number')
- const downloadSpeedUnit = document.createElement('span')
- downloadSpeedText.appendChild(downloadSpeedNumber)
- downloadSpeedText.appendChild(downloadSpeedUnit)
- div.appendChild(downloadSpeedText)
-
- const uploadIcon = document.createElement('span')
- uploadIcon.classList.add('icon', 'icon-upload')
- div.appendChild(uploadIcon)
-
- const uploadSpeedText = document.createElement('span')
- uploadSpeedText.classList.add('upload-speed-text')
- const uploadSpeedNumber = document.createElement('span')
- uploadSpeedNumber.classList.add('upload-speed-number')
- const uploadSpeedUnit = document.createElement('span')
- uploadSpeedText.appendChild(uploadSpeedNumber)
- uploadSpeedText.appendChild(uploadSpeedUnit)
- div.appendChild(uploadSpeedText)
-
- const peersText = document.createElement('span')
- peersText.textContent = ' peers'
- peersText.classList.add('peers-text')
- const peersNumber = document.createElement('span')
- peersNumber.classList.add('peers-number')
- div.appendChild(peersNumber)
- div.appendChild(peersText)
-
- div.className = 'vjs-webtorrent'
- // Hide the stats before we get the info
- div.style.display = 'none'
-
- this.player_.on('torrentInfo', (event, data) => {
- const downloadSpeed = bytes(data.downloadSpeed)
- const uploadSpeed = bytes(data.uploadSpeed)
- const numPeers = data.numPeers
-
- downloadSpeedNumber.textContent = downloadSpeed[0]
- downloadSpeedUnit.textContent = ' ' + downloadSpeed[1]
-
- uploadSpeedNumber.textContent = uploadSpeed[0]
- uploadSpeedUnit.textContent = ' ' + uploadSpeed[1]
-
- peersNumber.textContent = numPeers
-
- div.style.display = 'block'
+const Plugin: VideoJSComponentInterface = videojsUntyped.getPlugin('plugin')
+class PeerTubePlugin extends Plugin {
+ private readonly playerElement: HTMLVideoElement
+ private readonly autoplay: boolean = false
+ private readonly savePlayerSrcFunction: Function
+ private player: any
+ private currentVideoFile: VideoFile
+ private videoFiles: VideoFile[]
+ private torrent: WebTorrent.Torrent
+ private videoViewUrl: string
+ private videoDuration: number
+ private videoViewInterval
+ private torrentInfoInterval
+
+ constructor (player: videojs.Player, options: PeertubePluginOptions) {
+ super(player, options)
+
+ // Fix canplay event on google chrome by disabling default videojs autoplay
+ this.autoplay = this.player.options_.autoplay
+ this.player.options_.autoplay = false
+
+ this.videoFiles = options.videoFiles
+ this.videoViewUrl = options.videoViewUrl
+ this.videoDuration = options.videoDuration
+
+ this.savePlayerSrcFunction = this.player.src
+ // Hack to "simulate" src link in video.js >= 6
+ // Without this, we can't play the video after pausing it
+ // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
+ this.player.src = () => true
+
+ this.playerElement = options.playerElement
+
+ this.player.ready(() => {
+ const volume = getStoredVolume()
+ if (volume !== undefined) this.player.volume(volume)
+ const muted = getStoredMute()
+ if (muted !== undefined) this.player.muted(muted)
+
+ this.initializePlayer()
+ this.runTorrentInfoScheduler()
+ this.runViewAdd()