3 import * as Channel from 'jschannel'
4 import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions'
5 import { PeerTubeEmbed } from './embed'
8 * Embed API exposes control of the embed player to the outside world via
9 * JSChannels and window.postMessage
11 export class PeerTubeEmbedApi {
12 private channel: Channel.MessagingChannel
13 private isReady = false
14 private resolutions: PeerTubeResolution[] = []
16 constructor (private embed: PeerTubeEmbed) {
20 this.constructChannel()
21 this.setupStateTracking()
28 private get element () {
29 return this.embed.videoElement
32 private constructChannel () {
33 const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope })
35 channel.bind('play', (txn, params) => this.embed.player.play())
36 channel.bind('pause', (txn, params) => this.embed.player.pause())
37 channel.bind('seek', (txn, time) => this.embed.player.currentTime(time))
39 channel.bind('setVolume', (txn, value) => this.embed.player.volume(value))
40 channel.bind('getVolume', (txn, value) => this.embed.player.volume())
42 channel.bind('isReady', (txn, params) => this.isReady)
44 channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId))
45 channel.bind('getResolutions', (txn, params) => this.resolutions)
47 channel.bind('getCaptions', (txn, params) => this.getCaptions())
48 channel.bind('setCaption', (txn, id) => this.setCaption(id)),
50 channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate))
51 channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate())
52 channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates)
53 this.channel = channel
56 private setResolution (resolutionId: number) {
57 console.log('set resolution %d', resolutionId)
59 if (this.isWebtorrent()) {
60 if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return
63 if (resolutionId === -1) {
64 this.embed.player.webtorrent().enableAutoResolution()
68 this.embed.player.webtorrent().disableAutoResolution()
69 this.embed.player.webtorrent().updateResolution(resolutionId)
74 this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId
77 private getCaptions (): PeerTubeTextTrack[] {
78 return this.embed.player.textTracks().tracks_.map(t => {
88 private setCaption (id: string) {
89 const tracks = this.embed.player.textTracks().tracks_
91 for (const track of tracks) {
92 if (track.id === id) track.mode = 'showing'
93 else track.mode = 'disabled'
98 * Let the host know that we're ready to go!
100 private notifyReady () {
102 this.channel.notify({ method: 'ready', params: true })
105 private setupStateTracking () {
106 let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
109 const position = this.element.currentTime
110 const volume = this.element.volume
111 const duration = this.element.duration
113 this.channel.notify({
114 method: 'playbackStatusUpdate',
118 duration: this.embed.player.duration(),
119 playbackState: currentState
124 this.element.addEventListener('play', ev => {
125 currentState = 'playing'
126 this.channel.notify({ method: 'playbackStatusChange', params: 'playing' })
129 this.element.addEventListener('pause', ev => {
130 currentState = 'paused'
131 this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
134 this.element.addEventListener('ended', ev => {
135 currentState = 'ended'
136 this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
139 // PeerTube specific capabilities
141 if (this.isWebtorrent()) {
142 this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
143 this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
145 this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
148 this.embed.player.on('volumechange', () => {
149 this.channel.notify({
150 method: 'volumeChange',
151 params: this.embed.player.volume()
156 private loadWebTorrentResolutions () {
157 this.resolutions = []
159 const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
161 for (const videoFile of this.embed.player.webtorrent().videoFiles) {
162 let label = videoFile.resolution.label
163 if (videoFile.fps && videoFile.fps >= 50) {
164 label += videoFile.fps
167 this.resolutions.push({
168 id: videoFile.resolution.id,
170 src: videoFile.magnetUri,
171 active: videoFile.resolution.id === currentResolutionId,
172 height: videoFile.resolution.id
176 this.channel.notify({
177 method: 'resolutionUpdate',
178 params: this.resolutions
182 private loadP2PMediaLoaderResolutions () {
183 this.resolutions = []
185 const qualityLevels = this.embed.player.qualityLevels()
186 const currentResolutionId = this.embed.player.qualityLevels().selectedIndex
188 for (let i = 0; i < qualityLevels.length; i++) {
189 const level = qualityLevels[i]
191 this.resolutions.push({
193 label: level.height + 'p',
194 active: level.id === currentResolutionId,
200 this.channel.notify({
201 method: 'resolutionUpdate',
202 params: this.resolutions
206 private isWebtorrent () {
207 return this.embed.player.webtorrent