]>
Commit | Line | Data |
---|---|---|
5efab546 C |
1 | import './embed.scss' |
2 | ||
3 | import * as Channel from 'jschannel' | |
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 C |
15 | |
16 | constructor (private embed: PeerTubeEmbed) { | |
17 | } | |
18 | ||
19 | initialize () { | |
20 | this.constructChannel() | |
21 | this.setupStateTracking() | |
22 | ||
23 | // We're ready! | |
24 | ||
25 | this.notifyReady() | |
26 | } | |
27 | ||
28 | private get element () { | |
5abc96fc | 29 | return this.embed.playerElement |
5efab546 C |
30 | } |
31 | ||
32 | private constructChannel () { | |
33 | const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.scope }) | |
34 | ||
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)) | |
6377a9f2 | 38 | |
5efab546 C |
39 | channel.bind('setVolume', (txn, value) => this.embed.player.volume(value)) |
40 | channel.bind('getVolume', (txn, value) => this.embed.player.volume()) | |
6377a9f2 | 41 | |
5efab546 | 42 | channel.bind('isReady', (txn, params) => this.isReady) |
6377a9f2 | 43 | |
5efab546 C |
44 | channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId)) |
45 | channel.bind('getResolutions', (txn, params) => this.resolutions) | |
6377a9f2 | 46 | |
1151f521 C |
47 | channel.bind('getCaptions', (txn, params) => this.getCaptions()) |
48 | channel.bind('setCaption', (txn, id) => this.setCaption(id)), | |
49 | ||
5efab546 C |
50 | channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) |
51 | channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) | |
6377a9f2 | 52 | channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates) |
5efab546 C |
53 | this.channel = channel |
54 | } | |
55 | ||
56 | private setResolution (resolutionId: number) { | |
6377a9f2 C |
57 | console.log('set resolution %d', resolutionId) |
58 | ||
59 | if (this.isWebtorrent()) { | |
60 | if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return | |
61 | ||
62 | // Auto resolution | |
63 | if (resolutionId === -1) { | |
64 | this.embed.player.webtorrent().enableAutoResolution() | |
65 | return | |
66 | } | |
67 | ||
68 | this.embed.player.webtorrent().disableAutoResolution() | |
69 | this.embed.player.webtorrent().updateResolution(resolutionId) | |
5efab546 | 70 | |
5efab546 C |
71 | return |
72 | } | |
73 | ||
6377a9f2 | 74 | this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId |
5efab546 C |
75 | } |
76 | ||
1151f521 C |
77 | private getCaptions (): PeerTubeTextTrack[] { |
78 | return this.embed.player.textTracks().tracks_.map(t => { | |
79 | return { | |
80 | id: t.id, | |
81 | src: t.src, | |
82 | label: t.label, | |
83 | mode: t.mode as any | |
84 | } | |
85 | }) | |
86 | } | |
87 | ||
88 | private setCaption (id: string) { | |
89 | const tracks = this.embed.player.textTracks().tracks_ | |
90 | ||
91 | for (const track of tracks) { | |
92 | if (track.id === id) track.mode = 'showing' | |
93 | else track.mode = 'disabled' | |
94 | } | |
95 | } | |
96 | ||
5efab546 C |
97 | /** |
98 | * Let the host know that we're ready to go! | |
99 | */ | |
100 | private notifyReady () { | |
101 | this.isReady = true | |
102 | this.channel.notify({ method: 'ready', params: true }) | |
103 | } | |
104 | ||
105 | private setupStateTracking () { | |
96aae68c | 106 | let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted' |
5efab546 C |
107 | |
108 | setInterval(() => { | |
109 | const position = this.element.currentTime | |
110 | const volume = this.element.volume | |
111 | ||
112 | this.channel.notify({ | |
113 | method: 'playbackStatusUpdate', | |
114 | params: { | |
115 | position, | |
116 | volume, | |
6ccdf9d5 | 117 | duration: this.embed.player.duration(), |
5efab546 C |
118 | playbackState: currentState |
119 | } | |
120 | }) | |
121 | }, 500) | |
122 | ||
123 | this.element.addEventListener('play', ev => { | |
124 | currentState = 'playing' | |
125 | this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) | |
126 | }) | |
127 | ||
128 | this.element.addEventListener('pause', ev => { | |
129 | currentState = 'paused' | |
130 | this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) | |
131 | }) | |
132 | ||
96aae68c C |
133 | this.element.addEventListener('ended', ev => { |
134 | currentState = 'ended' | |
135 | this.channel.notify({ method: 'playbackStatusChange', params: 'ended' }) | |
136 | }) | |
137 | ||
5efab546 C |
138 | // PeerTube specific capabilities |
139 | ||
6377a9f2 | 140 | if (this.isWebtorrent()) { |
5efab546 C |
141 | this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions()) |
142 | this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions()) | |
6377a9f2 C |
143 | } else { |
144 | this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions()) | |
5efab546 | 145 | } |
6377a9f2 C |
146 | |
147 | this.embed.player.on('volumechange', () => { | |
148 | this.channel.notify({ | |
149 | method: 'volumeChange', | |
150 | params: this.embed.player.volume() | |
151 | }) | |
152 | }) | |
5efab546 C |
153 | } |
154 | ||
155 | private loadWebTorrentResolutions () { | |
6377a9f2 C |
156 | this.resolutions = [] |
157 | ||
5efab546 C |
158 | const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId() |
159 | ||
160 | for (const videoFile of this.embed.player.webtorrent().videoFiles) { | |
161 | let label = videoFile.resolution.label | |
162 | if (videoFile.fps && videoFile.fps >= 50) { | |
163 | label += videoFile.fps | |
164 | } | |
165 | ||
6377a9f2 | 166 | this.resolutions.push({ |
5efab546 C |
167 | id: videoFile.resolution.id, |
168 | label, | |
169 | src: videoFile.magnetUri, | |
6377a9f2 C |
170 | active: videoFile.resolution.id === currentResolutionId, |
171 | height: videoFile.resolution.id | |
5efab546 C |
172 | }) |
173 | } | |
174 | ||
5efab546 C |
175 | this.channel.notify({ |
176 | method: 'resolutionUpdate', | |
177 | params: this.resolutions | |
178 | }) | |
179 | } | |
6377a9f2 C |
180 | |
181 | private loadP2PMediaLoaderResolutions () { | |
182 | this.resolutions = [] | |
183 | ||
184 | const qualityLevels = this.embed.player.qualityLevels() | |
185 | const currentResolutionId = this.embed.player.qualityLevels().selectedIndex | |
186 | ||
187 | for (let i = 0; i < qualityLevels.length; i++) { | |
188 | const level = qualityLevels[i] | |
189 | ||
190 | this.resolutions.push({ | |
191 | id: level.id, | |
192 | label: level.height + 'p', | |
193 | active: level.id === currentResolutionId, | |
194 | width: level.width, | |
195 | height: level.height | |
196 | }) | |
197 | } | |
198 | ||
199 | this.channel.notify({ | |
200 | method: 'resolutionUpdate', | |
201 | params: this.resolutions | |
202 | }) | |
203 | } | |
204 | ||
205 | private isWebtorrent () { | |
206 | return this.embed.player.webtorrent | |
207 | } | |
5efab546 | 208 | } |