]>
Commit | Line | Data |
---|---|---|
5efab546 | 1 | import './embed.scss' |
5efab546 | 2 | import * as Channel from 'jschannel' |
42b40636 | 3 | import { logger } from '../../root-helpers' |
1151f521 | 4 | import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions' |
5efab546 C |
5 | import { PeerTubeEmbed } from './embed' |
6 | ||
7 | /** | |
8 | * Embed API exposes control of the embed player to the outside world via | |
9 | * JSChannels and window.postMessage | |
10 | */ | |
11 | export class PeerTubeEmbedApi { | |
12 | private channel: Channel.MessagingChannel | |
13 | private isReady = false | |
6377a9f2 | 14 | private resolutions: PeerTubeResolution[] = [] |
5efab546 | 15 | |
d9102154 C |
16 | private oldVideoElement: HTMLVideoElement |
17 | private videoElPlayListener: () => void | |
18 | private videoElPauseListener: () => void | |
19 | private videoElEndedListener: () => void | |
20 | private videoElInterval: any | |
21 | ||
9df52d66 C |
22 | constructor (private readonly embed: PeerTubeEmbed) { |
23 | ||
5efab546 C |
24 | } |
25 | ||
26 | initialize () { | |
27 | this.constructChannel() | |
28 | this.setupStateTracking() | |
29 | ||
30 | // We're ready! | |
31 | ||
32 | this.notifyReady() | |
33 | } | |
34 | ||
d9102154 C |
35 | reInit () { |
36 | this.disposeStateTracking() | |
37 | this.setupStateTracking() | |
38 | } | |
39 | ||
5efab546 | 40 | private get element () { |
f1a0f3b7 | 41 | return this.embed.getPlayerElement() |
5efab546 C |
42 | } |
43 | ||
44 | private constructChannel () { | |
f1a0f3b7 | 45 | const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.getScope() }) |
5efab546 C |
46 | |
47 | channel.bind('play', (txn, params) => this.embed.player.play()) | |
48 | channel.bind('pause', (txn, params) => this.embed.player.pause()) | |
49 | channel.bind('seek', (txn, time) => this.embed.player.currentTime(time)) | |
6377a9f2 | 50 | |
5efab546 C |
51 | channel.bind('setVolume', (txn, value) => this.embed.player.volume(value)) |
52 | channel.bind('getVolume', (txn, value) => this.embed.player.volume()) | |
6377a9f2 | 53 | |
5efab546 | 54 | channel.bind('isReady', (txn, params) => this.isReady) |
6377a9f2 | 55 | |
5efab546 C |
56 | channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId)) |
57 | channel.bind('getResolutions', (txn, params) => this.resolutions) | |
6377a9f2 | 58 | |
1151f521 | 59 | channel.bind('getCaptions', (txn, params) => this.getCaptions()) |
9df52d66 | 60 | channel.bind('setCaption', (txn, id) => this.setCaption(id)) |
1151f521 | 61 | |
5efab546 C |
62 | channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) |
63 | channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) | |
6377a9f2 | 64 | channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates) |
9054a8b6 | 65 | |
f1a0f3b7 C |
66 | channel.bind('playNextVideo', (txn, params) => this.embed.playNextPlaylistVideo()) |
67 | channel.bind('playPreviousVideo', (txn, params) => this.embed.playPreviousPlaylistVideo()) | |
68 | channel.bind('getCurrentPosition', (txn, params) => this.embed.getCurrentPlaylistPosition()) | |
5efab546 C |
69 | this.channel = channel |
70 | } | |
71 | ||
72 | private setResolution (resolutionId: number) { | |
42b40636 | 73 | logger.info(`Set resolution ${resolutionId}`) |
6377a9f2 C |
74 | |
75 | if (this.isWebtorrent()) { | |
76 | if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return | |
77 | ||
89ac282e | 78 | this.embed.player.webtorrent().changeQuality(resolutionId) |
5efab546 | 79 | |
5efab546 C |
80 | return |
81 | } | |
82 | ||
89ac282e | 83 | this.embed.player.p2pMediaLoader().getHLSJS().currentLevel = resolutionId |
5efab546 C |
84 | } |
85 | ||
1151f521 | 86 | private getCaptions (): PeerTubeTextTrack[] { |
9df52d66 C |
87 | return this.embed.player.textTracks().tracks_.map(t => ({ |
88 | id: t.id, | |
89 | src: t.src, | |
90 | label: t.label, | |
91 | mode: t.mode | |
92 | })) | |
1151f521 C |
93 | } |
94 | ||
95 | private setCaption (id: string) { | |
96 | const tracks = this.embed.player.textTracks().tracks_ | |
97 | ||
98 | for (const track of tracks) { | |
99 | if (track.id === id) track.mode = 'showing' | |
100 | else track.mode = 'disabled' | |
101 | } | |
102 | } | |
103 | ||
5efab546 C |
104 | /** |
105 | * Let the host know that we're ready to go! | |
106 | */ | |
107 | private notifyReady () { | |
108 | this.isReady = true | |
109 | this.channel.notify({ method: 'ready', params: true }) | |
110 | } | |
111 | ||
112 | private setupStateTracking () { | |
96aae68c | 113 | let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted' |
5efab546 | 114 | |
d9102154 | 115 | this.videoElInterval = setInterval(() => { |
5efab546 C |
116 | const position = this.element.currentTime |
117 | const volume = this.element.volume | |
118 | ||
119 | this.channel.notify({ | |
120 | method: 'playbackStatusUpdate', | |
121 | params: { | |
122 | position, | |
123 | volume, | |
6ccdf9d5 | 124 | duration: this.embed.player.duration(), |
5efab546 C |
125 | playbackState: currentState |
126 | } | |
127 | }) | |
128 | }, 500) | |
129 | ||
d9102154 C |
130 | // --------------------------------------------------------------------------- |
131 | ||
132 | this.videoElPlayListener = () => { | |
5efab546 C |
133 | currentState = 'playing' |
134 | this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) | |
d9102154 C |
135 | } |
136 | this.element.addEventListener('play', this.videoElPlayListener) | |
5efab546 | 137 | |
d9102154 | 138 | this.videoElPauseListener = () => { |
5efab546 C |
139 | currentState = 'paused' |
140 | this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) | |
d9102154 C |
141 | } |
142 | this.element.addEventListener('pause', this.videoElPauseListener) | |
5efab546 | 143 | |
d9102154 | 144 | this.videoElEndedListener = () => { |
96aae68c C |
145 | currentState = 'ended' |
146 | this.channel.notify({ method: 'playbackStatusChange', params: 'ended' }) | |
d9102154 C |
147 | } |
148 | this.element.addEventListener('ended', this.videoElEndedListener) | |
149 | ||
150 | this.oldVideoElement = this.element | |
151 | ||
152 | // --------------------------------------------------------------------------- | |
96aae68c | 153 | |
5efab546 | 154 | // PeerTube specific capabilities |
e367da94 C |
155 | this.embed.player.peertubeResolutions().on('resolutionsAdded', () => this.loadResolutions()) |
156 | this.embed.player.peertubeResolutions().on('resolutionChanged', () => this.loadResolutions()) | |
6377a9f2 | 157 | |
89ac282e C |
158 | this.loadResolutions() |
159 | ||
6377a9f2 C |
160 | this.embed.player.on('volumechange', () => { |
161 | this.channel.notify({ | |
162 | method: 'volumeChange', | |
163 | params: this.embed.player.volume() | |
164 | }) | |
165 | }) | |
5efab546 C |
166 | } |
167 | ||
d9102154 C |
168 | private disposeStateTracking () { |
169 | if (!this.oldVideoElement) return | |
170 | ||
171 | this.oldVideoElement.removeEventListener('play', this.videoElPlayListener) | |
172 | this.oldVideoElement.removeEventListener('pause', this.videoElPauseListener) | |
173 | this.oldVideoElement.removeEventListener('ended', this.videoElEndedListener) | |
174 | ||
175 | clearInterval(this.videoElInterval) | |
176 | ||
177 | this.oldVideoElement = undefined | |
178 | } | |
179 | ||
e367da94 C |
180 | private loadResolutions () { |
181 | this.resolutions = this.embed.player.peertubeResolutions().getResolutions() | |
182 | .map(r => ({ | |
183 | id: r.id, | |
89ac282e | 184 | label: r.label, |
e367da94 C |
185 | active: r.selected, |
186 | width: r.width, | |
187 | height: r.height | |
188 | })) | |
6377a9f2 C |
189 | |
190 | this.channel.notify({ | |
191 | method: 'resolutionUpdate', | |
192 | params: this.resolutions | |
193 | }) | |
194 | } | |
195 | ||
196 | private isWebtorrent () { | |
ffacd78f | 197 | return !!this.embed.player.webtorrent |
6377a9f2 | 198 | } |
5efab546 | 199 | } |