X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fstandalone%2Fvideos%2Fembed-api.ts;h=94e39ec29c4c3e124442da6780c537917787428f;hb=dc48fdbe68e9dd3a3a6028181e61d8595d98e654;hp=259113215ba3177aeea9794884ba5d4f84aad1da;hpb=3f9c4955af81702591a6eeb2069f99faf0d2814d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/standalone/videos/embed-api.ts b/client/src/standalone/videos/embed-api.ts index 259113215..94e39ec29 100644 --- a/client/src/standalone/videos/embed-api.ts +++ b/client/src/standalone/videos/embed-api.ts @@ -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,7 +11,7 @@ import { PeerTubeEmbed } from './embed' export class PeerTubeEmbedApi { private channel: Channel.MessagingChannel private isReady = false - private resolutions: PeerTubeResolution[] = null + private resolutions: PeerTubeResolution[] = [] constructor (private embed: PeerTubeEmbed) { } @@ -26,7 +26,7 @@ export class PeerTubeEmbedApi { } private get element () { - return this.embed.videoElement + return this.embed.playerElement } private constructChannel () { @@ -35,28 +35,67 @@ 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 + + // Auto resolution + if (resolutionId === -1) { + this.embed.player.webtorrent().enableAutoResolution() + return + } + + this.embed.player.webtorrent().disableAutoResolution() + this.embed.player.webtorrent().updateResolution(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().nextLevel = 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 + } + }) + } + + 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 +107,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 +118,7 @@ export class PeerTubeEmbedApi { params: { position, volume, + duration: this.embed.player.duration(), playbackState: currentState } }) @@ -94,16 +134,31 @@ 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 - if (this.embed.player.webtorrent) { + 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.embed.player.on('volumechange', () => { + this.channel.notify({ + method: 'volumeChange', + params: this.embed.player.volume() + }) + }) } private loadWebTorrentResolutions () { - const resolutions = [] + this.resolutions = [] + const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId() for (const videoFile of this.embed.player.webtorrent().videoFiles) { @@ -112,18 +167,46 @@ export class PeerTubeEmbedApi { label += videoFile.fps } - resolutions.push({ + this.resolutions.push({ id: videoFile.resolution.id, label, src: videoFile.magnetUri, - active: videoFile.resolution.id === currentResolutionId + active: videoFile.resolution.id === currentResolutionId, + height: videoFile.resolution.id }) } - this.resolutions = resolutions this.channel.notify({ method: 'resolutionUpdate', params: this.resolutions }) } + + 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 + }) + } + + this.channel.notify({ + method: 'resolutionUpdate', + params: this.resolutions + }) + } + + private isWebtorrent () { + return this.embed.player.webtorrent + } }