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