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 readonly embed: PeerTubeEmbed) {
21 this.constructChannel()
22 this.setupStateTracking()
29 private get element () {
30 return this.embed.playerElement
33 private constructChannel () {
34 const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope })
36 channel.bind('play', (txn, params) => this.embed.player.play())
37 channel.bind('pause', (txn, params) => this.embed.player.pause())
38 channel.bind('seek', (txn, time) => this.embed.player.currentTime(time))
40 channel.bind('setVolume', (txn, value) => this.embed.player.volume(value))
41 channel.bind('getVolume', (txn, value) => this.embed.player.volume())
43 channel.bind('isReady', (txn, params) => this.isReady)
45 channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId))
46 channel.bind('getResolutions', (txn, params) => this.resolutions)
48 channel.bind('getCaptions', (txn, params) => this.getCaptions())
49 channel.bind('setCaption', (txn, id) => this.setCaption(id))
51 channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate))
52 channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate())
53 channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates)
55 channel.bind('playNextVideo', (txn, params) => this.embed.playNextVideo())
56 channel.bind('playPreviousVideo', (txn, params) => this.embed.playPreviousVideo())
57 channel.bind('getCurrentPosition', (txn, params) => this.embed.getCurrentPosition())
58 this.channel = channel
61 private setResolution (resolutionId: number) {
62 console.log('set resolution %d', resolutionId)
64 if (this.isWebtorrent()) {
65 if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return
68 if (resolutionId === -1) {
69 this.embed.player.webtorrent().enableAutoResolution()
73 this.embed.player.webtorrent().disableAutoResolution()
74 this.embed.player.webtorrent().updateResolution(resolutionId)
79 this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId
82 private getCaptions (): PeerTubeTextTrack[] {
83 return this.embed.player.textTracks().tracks_.map(t => ({
91 private setCaption (id: string) {
92 const tracks = this.embed.player.textTracks().tracks_
94 for (const track of tracks) {
95 if (track.id === id) track.mode = 'showing'
96 else track.mode = 'disabled'
101 * Let the host know that we're ready to go!
103 private notifyReady () {
105 this.channel.notify({ method: 'ready', params: true })
108 private setupStateTracking () {
109 let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
112 const position = this.element.currentTime
113 const volume = this.element.volume
115 this.channel.notify({
116 method: 'playbackStatusUpdate',
120 duration: this.embed.player.duration(),
121 playbackState: currentState
126 this.element.addEventListener('play', ev => {
127 currentState = 'playing'
128 this.channel.notify({ method: 'playbackStatusChange', params: 'playing' })
131 this.element.addEventListener('pause', ev => {
132 currentState = 'paused'
133 this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
136 this.element.addEventListener('ended', ev => {
137 currentState = 'ended'
138 this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
141 // PeerTube specific capabilities
142 this.embed.player.peertubeResolutions().on('resolutionsAdded', () => this.loadResolutions())
143 this.embed.player.peertubeResolutions().on('resolutionChanged', () => this.loadResolutions())
145 this.embed.player.on('volumechange', () => {
146 this.channel.notify({
147 method: 'volumeChange',
148 params: this.embed.player.volume()
153 private loadWebTorrentResolutions () {
154 this.resolutions = []
156 const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
158 for (const videoFile of this.embed.player.webtorrent().videoFiles) {
159 let label = videoFile.resolution.label
160 if (videoFile.fps && videoFile.fps >= 50) {
161 label += videoFile.fps
164 this.resolutions.push({
165 id: videoFile.resolution.id,
167 src: videoFile.magnetUri,
168 active: videoFile.resolution.id === currentResolutionId,
169 height: videoFile.resolution.id
173 this.channel.notify({
174 method: 'resolutionUpdate',
175 params: this.resolutions
179 private loadResolutions () {
180 this.resolutions = this.embed.player.peertubeResolutions().getResolutions()
183 label: r.height + 'p',
189 this.channel.notify({
190 method: 'resolutionUpdate',
191 params: this.resolutions
195 private isWebtorrent () {
196 return !!this.embed.player.webtorrent