From f0a3988066f72a28bb44520af072f18d91d77dde Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 7 Mar 2019 17:06:00 +0100 Subject: Add to playlist dropdown --- client/src/assets/player/peertube-player-manager.ts | 9 ++++++--- client/src/assets/player/peertube-plugin.ts | 16 +++++++++------- client/src/assets/player/peertube-videojs-typings.ts | 7 ++++++- client/src/assets/player/utils.ts | 20 +++++++++++++++----- .../assets/player/webtorrent/webtorrent-plugin.ts | 4 +++- 5 files changed, 39 insertions(+), 17 deletions(-) (limited to 'client/src/assets/player') diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts index 7631d095f..6cdd54372 100644 --- a/client/src/assets/player/peertube-player-manager.ts +++ b/client/src/assets/player/peertube-player-manager.ts @@ -49,6 +49,7 @@ export type CommonOptions = { inactivityTimeout: number poster: string startTime: number | string + stopTime: number | string theaterMode: boolean captions: boolean @@ -199,10 +200,10 @@ export class PeertubePlayerManager { autoplay, // Use peertube plugin autoplay because we get the file by webtorrent videoViewUrl: commonOptions.videoViewUrl, videoDuration: commonOptions.videoDuration, - startTime: commonOptions.startTime, userWatching: commonOptions.userWatching, subtitle: commonOptions.subtitle, - videoCaptions: commonOptions.videoCaptions + videoCaptions: commonOptions.videoCaptions, + stopTime: commonOptions.stopTime } } @@ -210,6 +211,7 @@ export class PeertubePlayerManager { const p2pMediaLoader: P2PMediaLoaderPluginOptions = { redundancyBaseUrls: options.p2pMediaLoader.redundancyBaseUrls, type: 'application/x-mpegURL', + startTime: commonOptions.startTime, src: p2pMediaLoaderOptions.playlistUrl } @@ -254,7 +256,8 @@ export class PeertubePlayerManager { autoplay, videoDuration: commonOptions.videoDuration, playerElement: commonOptions.playerElement, - videoFiles: webtorrentOptions.videoFiles + videoFiles: webtorrentOptions.videoFiles, + startTime: commonOptions.startTime } Object.assign(plugins, { webtorrent }) diff --git a/client/src/assets/player/peertube-plugin.ts b/client/src/assets/player/peertube-plugin.ts index 92ac57cf5..3991e4627 100644 --- a/client/src/assets/player/peertube-plugin.ts +++ b/client/src/assets/player/peertube-plugin.ts @@ -22,7 +22,6 @@ import { const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') class PeerTubePlugin extends Plugin { - private readonly startTime: number = 0 private readonly videoViewUrl: string private readonly videoDuration: number private readonly CONSTANTS = { @@ -35,13 +34,11 @@ class PeerTubePlugin extends Plugin { private videoViewInterval: any private userWatchingVideoInterval: any - private qualityObservationTimer: any private lastResolutionChange: ResolutionUpdateData constructor (player: videojs.Player, options: PeerTubePluginOptions) { super(player, options) - this.startTime = timeToInt(options.startTime) this.videoViewUrl = options.videoViewUrl this.videoDuration = options.videoDuration this.videoCaptions = options.videoCaptions @@ -84,6 +81,14 @@ class PeerTubePlugin extends Plugin { saveMuteInStore(this.player.muted()) }) + if (options.stopTime) { + const stopTime = timeToInt(options.stopTime) + + this.player.on('timeupdate', () => { + if (this.player.currentTime() > stopTime) this.player.pause() + }) + } + this.player.textTracks().on('change', () => { const showing = this.player.textTracks().tracks_.find((t: { kind: string, mode: string }) => { return t.kind === 'captions' && t.mode === 'showing' @@ -109,10 +114,7 @@ class PeerTubePlugin extends Plugin { } dispose () { - clearTimeout(this.qualityObservationTimer) - - clearInterval(this.videoViewInterval) - + if (this.videoViewInterval) clearInterval(this.videoViewInterval) if (this.userWatchingVideoInterval) clearInterval(this.userWatchingVideoInterval) } diff --git a/client/src/assets/player/peertube-videojs-typings.ts b/client/src/assets/player/peertube-videojs-typings.ts index 79a5a6c4d..a96b0bc8c 100644 --- a/client/src/assets/player/peertube-videojs-typings.ts +++ b/client/src/assets/player/peertube-videojs-typings.ts @@ -41,12 +41,13 @@ type PeerTubePluginOptions = { autoplay: boolean videoViewUrl: string videoDuration: number - startTime: number | string userWatching?: UserWatching subtitle?: string videoCaptions: VideoJSCaption[] + + stopTime: number | string } type WebtorrentPluginOptions = { @@ -56,12 +57,16 @@ type WebtorrentPluginOptions = { videoDuration: number videoFiles: VideoFile[] + + startTime: number | string } type P2PMediaLoaderPluginOptions = { redundancyBaseUrls: string[] type: string src: string + + startTime: number | string } type VideoJSPluginOptions = { diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index 8d87567c2..54f131310 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts @@ -42,7 +42,7 @@ function timeToInt (time: number | string) { if (!time) return 0 if (typeof time === 'number') return time - const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ + const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ const matches = time.match(reg) if (!matches) return 0 @@ -54,18 +54,27 @@ function timeToInt (time: number | string) { return hours * 3600 + minutes * 60 + seconds } -function secondsToTime (seconds: number) { +function secondsToTime (seconds: number, full = false, symbol?: string) { let time = '' + const hourSymbol = (symbol || 'h') + const minuteSymbol = (symbol || 'm') + const secondsSymbol = full ? '' : 's' + let hours = Math.floor(seconds / 3600) - if (hours >= 1) time = hours + 'h' + if (hours >= 1) time = hours + hourSymbol + else if (full) time = '0' + hourSymbol seconds %= 3600 let minutes = Math.floor(seconds / 60) - if (minutes >= 1) time += minutes + 'm' + if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol + else if (minutes >= 1) time += minutes + minuteSymbol + else if (full) time += '00' + minuteSymbol seconds %= 60 - if (seconds >= 1) time += seconds + 's' + if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol + else if (seconds >= 1) time += seconds + secondsSymbol + else if (full) time += '00' return time } @@ -131,6 +140,7 @@ export { getRtcConfig, toTitleCase, timeToInt, + secondsToTime, buildVideoLink, buildVideoEmbed, videoFileMaxByResolution, diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.ts index c69bf31fa..c7182acc9 100644 --- a/client/src/assets/player/webtorrent/webtorrent-plugin.ts +++ b/client/src/assets/player/webtorrent/webtorrent-plugin.ts @@ -6,7 +6,7 @@ import * as WebTorrent from 'webtorrent' import { VideoFile } from '../../../../../shared/models/videos/video.model' import { renderVideo } from './video-renderer' import { LoadedQualityData, PlayerNetworkInfo, VideoJSComponentInterface, WebtorrentPluginOptions } from '../peertube-videojs-typings' -import { getRtcConfig, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' +import { getRtcConfig, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from '../utils' import { PeertubeChunkStore } from './peertube-chunk-store' import { getAverageBandwidthInStore, @@ -73,6 +73,8 @@ class WebTorrentPlugin extends Plugin { constructor (player: videojs.Player, options: WebtorrentPluginOptions) { super(player, options) + this.startTime = timeToInt(options.startTime) + // Disable auto play on iOS this.autoplay = options.autoplay && this.isIOS() === false this.playerRefusedP2P = !getStoredWebTorrentEnabled() -- cgit v1.2.3