]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/standalone/videos/embed-api.ts
Reorganize player files
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed-api.ts
index 259113215ba3177aeea9794884ba5d4f84aad1da..a28aeeaefd4cbec06ec74ce291b2894a2a93c6cc 100644 (file)
@@ -1,7 +1,7 @@
 import './embed.scss'
 
 import * as Channel from 'jschannel'
-import { PeerTubeResolution } from '../player/definitions'
+import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions'
 import { PeerTubeEmbed } from './embed'
 
 /**
@@ -11,9 +11,10 @@ import { PeerTubeEmbed } from './embed'
 export class PeerTubeEmbedApi {
   private channel: Channel.MessagingChannel
   private isReady = false
-  private resolutions: PeerTubeResolution[] = null
+  private resolutions: PeerTubeResolution[] = []
+
+  constructor (private readonly embed: PeerTubeEmbed) {
 
-  constructor (private embed: PeerTubeEmbed) {
   }
 
   initialize () {
@@ -26,7 +27,7 @@ export class PeerTubeEmbedApi {
   }
 
   private get element () {
-    return this.embed.videoElement
+    return this.embed.playerElement
   }
 
   private constructChannel () {
@@ -35,28 +36,58 @@ export class PeerTubeEmbedApi {
     channel.bind('play', (txn, params) => this.embed.player.play())
     channel.bind('pause', (txn, params) => this.embed.player.pause())
     channel.bind('seek', (txn, time) => this.embed.player.currentTime(time))
+
     channel.bind('setVolume', (txn, value) => this.embed.player.volume(value))
     channel.bind('getVolume', (txn, value) => this.embed.player.volume())
+
     channel.bind('isReady', (txn, params) => this.isReady)
+
     channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId))
     channel.bind('getResolutions', (txn, params) => this.resolutions)
+
+    channel.bind('getCaptions', (txn, params) => this.getCaptions())
+    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.playerOptions.playbackRates)
+    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())
     this.channel = channel
   }
 
   private setResolution (resolutionId: number) {
-    if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionForbidden()) return
+    console.log('set resolution %d', resolutionId)
+
+    if (this.isWebtorrent()) {
+      if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return
+
+      this.embed.player.webtorrent().changeQuality(resolutionId)
 
-    // 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.p2pMediaLoader().getHLSJS().currentLevel = resolutionId
+  }
+
+  private getCaptions (): PeerTubeTextTrack[] {
+    return this.embed.player.textTracks().tracks_.map(t => ({
+      id: t.id,
+      src: t.src,
+      label: t.label,
+      mode: t.mode
+    }))
+  }
+
+  private setCaption (id: string) {
+    const tracks = this.embed.player.textTracks().tracks_
+
+    for (const track of tracks) {
+      if (track.id === id) track.mode = 'showing'
+      else track.mode = 'disabled'
+    }
   }
 
   /**
@@ -68,7 +99,7 @@ export class PeerTubeEmbedApi {
   }
 
   private setupStateTracking () {
-    let currentState: 'playing' | 'paused' | 'unstarted' = 'unstarted'
+    let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
 
     setInterval(() => {
       const position = this.element.currentTime
@@ -79,6 +110,7 @@ export class PeerTubeEmbedApi {
         params: {
           position,
           volume,
+          duration: this.embed.player.duration(),
           playbackState: currentState
         }
       })
@@ -94,36 +126,42 @@ export class PeerTubeEmbedApi {
       this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
     })
 
+    this.element.addEventListener('ended', ev => {
+      currentState = 'ended'
+      this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
+    })
+
     // PeerTube specific capabilities
+    this.embed.player.peertubeResolutions().on('resolutionsAdded', () => this.loadResolutions())
+    this.embed.player.peertubeResolutions().on('resolutionChanged', () => this.loadResolutions())
 
-    if (this.embed.player.webtorrent) {
-      this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
-      this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
-    }
-  }
+    this.loadResolutions()
 
-  private loadWebTorrentResolutions () {
-    const resolutions = []
-    const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
-
-    for (const videoFile of this.embed.player.webtorrent().videoFiles) {
-      let label = videoFile.resolution.label
-      if (videoFile.fps && videoFile.fps >= 50) {
-        label += videoFile.fps
-      }
-
-      resolutions.push({
-        id: videoFile.resolution.id,
-        label,
-        src: videoFile.magnetUri,
-        active: videoFile.resolution.id === currentResolutionId
+    this.embed.player.on('volumechange', () => {
+      this.channel.notify({
+        method: 'volumeChange',
+        params: this.embed.player.volume()
       })
-    }
+    })
+  }
+
+  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.resolutions = resolutions
     this.channel.notify({
       method: 'resolutionUpdate',
       params: this.resolutions
     })
   }
+
+  private isWebtorrent () {
+    return !!this.embed.player.webtorrent
+  }
 }