1 import * as Channel from 'jschannel'
2 import { EventRegistrar } from './events'
3 import { EventHandler, PlayerEventType, PeerTubeResolution } from './definitions'
5 const PASSTHROUGH_EVENTS = [
8 'playbackStatusUpdate',
9 'playbackStatusChange',
14 * Allows for programmatic control of a PeerTube embed running in an <iframe>
17 export class PeerTubePlayer {
19 private eventRegistrar: EventRegistrar = new EventRegistrar()
20 private channel: Channel.MessagingChannel
21 private readyPromise: Promise<void>
24 * Construct a new PeerTubePlayer for the given PeerTube embed iframe.
25 * Optionally provide a `scope` to ensure that messages are not crossed
26 * between multiple PeerTube embeds. The string passed here must match the
27 * `scope=` query parameter on the embed URL.
33 private embedElement: HTMLIFrameElement,
34 private scope?: string
36 this.eventRegistrar.registerTypes(PASSTHROUGH_EVENTS)
38 this.constructChannel()
39 this.prepareToBeReady()
43 * Destroy the player object and remove the associated player from the DOM.
46 this.embedElement.remove()
50 * Listen to an event emitted by this player.
52 * @param event One of the supported event types
53 * @param handler A handler which will be passed an event object (or undefined if no event object is included)
55 addEventListener (event: PlayerEventType, handler: EventHandler<any>): boolean {
56 return this.eventRegistrar.addListener(event, handler)
60 * Remove an event listener previously added with addEventListener().
62 * @param event The name of the event previously listened to
65 removeEventListener (event: PlayerEventType, handler: EventHandler<any>): boolean {
66 return this.eventRegistrar.removeListener(event, handler)
70 * Promise resolves when the player is ready.
72 get ready (): Promise<void> {
73 return this.readyPromise
77 * Tell the embed to start/resume playback
80 await this.sendMessage('play')
84 * Tell the embed to pause playback.
87 await this.sendMessage('pause')
91 * Tell the embed to change the audio volume
92 * @param value A number from 0 to 1
94 async setVolume (value: number) {
95 await this.sendMessage('setVolume', value)
99 * Get the current volume level in the embed.
100 * @param value A number from 0 to 1
102 async getVolume (): Promise<number> {
103 return this.sendMessage<void, number>('setVolume')
107 * Tell the embed to seek to a specific position (in seconds)
110 async seek (seconds: number) {
111 await this.sendMessage('seek', seconds)
115 * Tell the embed to switch resolutions to the resolution identified
118 * @param resolutionId The ID of the resolution as found with getResolutions()
120 async setResolution (resolutionId: any) {
121 await this.sendMessage('setResolution', resolutionId)
125 * Retrieve a list of the available resolutions. This may change later, listen to the
126 * `resolutionUpdate` event with `addEventListener` in order to be updated as the available
127 * resolutions change.
129 async getResolutions (): Promise<PeerTubeResolution[]> {
130 return this.sendMessage<void, PeerTubeResolution[]>('getResolutions')
134 * Retrieve a list of available playback rates.
136 async getPlaybackRates (): Promise<number[]> {
137 return this.sendMessage<void, number[]>('getPlaybackRates')
141 * Get the current playback rate. Defaults to 1 (1x playback rate).
143 async getPlaybackRate (): Promise<number> {
144 return this.sendMessage<void, number>('getPlaybackRate')
148 * Set the playback rate. Should be one of the options returned by getPlaybackRates().
149 * Passing 0.5 means half speed, 1 means normal, 2 means 2x speed, etc.
153 async setPlaybackRate (rate: number) {
154 await this.sendMessage('setPlaybackRate', rate)
157 private constructChannel () {
158 this.channel = Channel.build({
159 window: this.embedElement.contentWindow,
161 scope: this.scope || 'peertube'
163 this.eventRegistrar.bindToChannel(this.channel)
166 private prepareToBeReady () {
167 let readyResolve: Function
168 let readyReject: Function
170 this.readyPromise = new Promise<void>((res, rej) => {
175 this.channel.bind('ready', success => success ? readyResolve() : readyReject())
178 success: isReady => isReady ? readyResolve() : null
182 private sendMessage<TIn, TOut> (method: string, params?: TIn): Promise<TOut> {
183 return new Promise<TOut>((resolve, reject) => {
186 success: result => resolve(result),
187 error: error => reject(error)
193 // put it on the window as well as the export
194 window[ 'PeerTubePlayer' ] = PeerTubePlayer