]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/standalone/videos/embed-api.ts
Translated using Weblate (Icelandic)
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed-api.ts
index 94e39ec29c4c3e124442da6780c537917787428f..a99f1edae8e15245abfc3bcd360e6e4405fb7b48 100644 (file)
@@ -1,6 +1,6 @@
 import './embed.scss'
-
 import * as Channel from 'jschannel'
+import { logger } from '../../root-helpers'
 import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions'
 import { PeerTubeEmbed } from './embed'
 
@@ -13,7 +13,14 @@ export class PeerTubeEmbedApi {
   private isReady = false
   private resolutions: PeerTubeResolution[] = []
 
-  constructor (private embed: PeerTubeEmbed) {
+  private oldVideoElement: HTMLVideoElement
+  private videoElPlayListener: () => void
+  private videoElPauseListener: () => void
+  private videoElEndedListener: () => void
+  private videoElInterval: any
+
+  constructor (private readonly embed: PeerTubeEmbed) {
+
   }
 
   initialize () {
@@ -25,12 +32,17 @@ export class PeerTubeEmbedApi {
     this.notifyReady()
   }
 
+  reInit () {
+    this.disposeStateTracking()
+    this.setupStateTracking()
+  }
+
   private get element () {
-    return this.embed.playerElement
+    return this.embed.getPlayerElement()
   }
 
   private constructChannel () {
-    const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope })
+    const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.getScope() })
 
     channel.bind('play', (txn, params) => this.embed.player.play())
     channel.bind('pause', (txn, params) => this.embed.player.pause())
@@ -45,48 +57,39 @@ export class PeerTubeEmbedApi {
     channel.bind('getResolutions', (txn, params) => this.resolutions)
 
     channel.bind('getCaptions', (txn, params) => this.getCaptions())
-    channel.bind('setCaption', (txn, id) => this.setCaption(id)),
+    channel.bind('setCaption', (txn, id) => this.setCaption(id))
 
     channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate))
     channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate())
     channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates)
 
-    channel.bind('playNextVideo', (txn, params) => this.embed.playNextVideo())
-    channel.bind('playPreviousVideo', (txn, params) => this.embed.playPreviousVideo())
-    channel.bind('getCurrentPosition', (txn, params) => this.embed.getCurrentPosition())
+    channel.bind('playNextVideo', (txn, params) => this.embed.playNextPlaylistVideo())
+    channel.bind('playPreviousVideo', (txn, params) => this.embed.playPreviousPlaylistVideo())
+    channel.bind('getCurrentPosition', (txn, params) => this.embed.getCurrentPlaylistPosition())
     this.channel = channel
   }
 
   private setResolution (resolutionId: number) {
-    console.log('set resolution %d', resolutionId)
+    logger.info(`Set resolution ${resolutionId}`)
 
     if (this.isWebtorrent()) {
       if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return
 
-      // Auto resolution
-      if (resolutionId === -1) {
-        this.embed.player.webtorrent().enableAutoResolution()
-        return
-      }
-
-      this.embed.player.webtorrent().disableAutoResolution()
-      this.embed.player.webtorrent().updateResolution(resolutionId)
+      this.embed.player.webtorrent().changeQuality(resolutionId)
 
       return
     }
 
-    this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId
+    this.embed.player.p2pMediaLoader().getHLSJS().currentLevel = resolutionId
   }
 
   private getCaptions (): PeerTubeTextTrack[] {
-    return this.embed.player.textTracks().tracks_.map(t => {
-      return {
-        id: t.id,
-        src: t.src,
-        label: t.label,
-        mode: t.mode as any
-      }
-    })
+    return this.embed.player.textTracks().tracks_.map(t => ({
+      id: t.id,
+      src: t.src,
+      label: t.label,
+      mode: t.mode
+    }))
   }
 
   private setCaption (id: string) {
@@ -109,7 +112,7 @@ export class PeerTubeEmbedApi {
   private setupStateTracking () {
     let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
 
-    setInterval(() => {
+    this.videoElInterval = setInterval(() => {
       const position = this.element.currentTime
       const volume = this.element.volume
 
@@ -124,29 +127,35 @@ export class PeerTubeEmbedApi {
       })
     }, 500)
 
-    this.element.addEventListener('play', ev => {
+    // ---------------------------------------------------------------------------
+
+    this.videoElPlayListener = () => {
       currentState = 'playing'
       this.channel.notify({ method: 'playbackStatusChange', params: 'playing' })
-    })
+    }
+    this.element.addEventListener('play', this.videoElPlayListener)
 
-    this.element.addEventListener('pause', ev => {
+    this.videoElPauseListener = () => {
       currentState = 'paused'
       this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
-    })
+    }
+    this.element.addEventListener('pause', this.videoElPauseListener)
 
-    this.element.addEventListener('ended', ev => {
+    this.videoElEndedListener = () => {
       currentState = 'ended'
       this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
-    })
+    }
+    this.element.addEventListener('ended', this.videoElEndedListener)
+
+    this.oldVideoElement = this.element
+
+    // ---------------------------------------------------------------------------
 
     // PeerTube specific capabilities
+    this.embed.player.peertubeResolutions().on('resolutionsAdded', () => this.loadResolutions())
+    this.embed.player.peertubeResolutions().on('resolutionChanged', () => this.loadResolutions())
 
-    if (this.isWebtorrent()) {
-      this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
-      this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
-    } else {
-      this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
-    }
+    this.loadResolutions()
 
     this.embed.player.on('volumechange', () => {
       this.channel.notify({
@@ -156,49 +165,27 @@ export class PeerTubeEmbedApi {
     })
   }
 
-  private loadWebTorrentResolutions () {
-    this.resolutions = []
-
-    const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
+  private disposeStateTracking () {
+    if (!this.oldVideoElement) return
 
-    for (const videoFile of this.embed.player.webtorrent().videoFiles) {
-      let label = videoFile.resolution.label
-      if (videoFile.fps && videoFile.fps >= 50) {
-        label += videoFile.fps
-      }
+    this.oldVideoElement.removeEventListener('play', this.videoElPlayListener)
+    this.oldVideoElement.removeEventListener('pause', this.videoElPauseListener)
+    this.oldVideoElement.removeEventListener('ended', this.videoElEndedListener)
 
-      this.resolutions.push({
-        id: videoFile.resolution.id,
-        label,
-        src: videoFile.magnetUri,
-        active: videoFile.resolution.id === currentResolutionId,
-        height: videoFile.resolution.id
-      })
-    }
+    clearInterval(this.videoElInterval)
 
-    this.channel.notify({
-      method: 'resolutionUpdate',
-      params: this.resolutions
-    })
+    this.oldVideoElement = undefined
   }
 
-  private loadP2PMediaLoaderResolutions () {
-    this.resolutions = []
-
-    const qualityLevels = this.embed.player.qualityLevels()
-    const currentResolutionId = this.embed.player.qualityLevels().selectedIndex
-
-    for (let i = 0; i < qualityLevels.length; i++) {
-      const level = qualityLevels[i]
-
-      this.resolutions.push({
-        id: level.id,
-        label: level.height + 'p',
-        active: level.id === currentResolutionId,
-        width: level.width,
-        height: level.height
-      })
-    }
+  private loadResolutions () {
+    this.resolutions = this.embed.player.peertubeResolutions().getResolutions()
+      .map(r => ({
+        id: r.id,
+        label: r.label,
+        active: r.selected,
+        width: r.width,
+        height: r.height
+      }))
 
     this.channel.notify({
       method: 'resolutionUpdate',
@@ -207,6 +194,6 @@ export class PeerTubeEmbedApi {
   }
 
   private isWebtorrent () {
-    return this.embed.player.webtorrent
+    return !!this.embed.player.webtorrent
   }
 }