]>
Commit | Line | Data |
---|---|---|
1 | import './embed.scss' | |
2 | ||
3 | import * as Channel from 'jschannel' | |
4 | import { PeerTubeResolution } 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.videoElement | |
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('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) | |
48 | channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) | |
49 | channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates) | |
50 | this.channel = channel | |
51 | } | |
52 | ||
53 | private setResolution (resolutionId: number) { | |
54 | console.log('set resolution %d', resolutionId) | |
55 | ||
56 | if (this.isWebtorrent()) { | |
57 | if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return | |
58 | ||
59 | // Auto resolution | |
60 | if (resolutionId === -1) { | |
61 | this.embed.player.webtorrent().enableAutoResolution() | |
62 | return | |
63 | } | |
64 | ||
65 | this.embed.player.webtorrent().disableAutoResolution() | |
66 | this.embed.player.webtorrent().updateResolution(resolutionId) | |
67 | ||
68 | return | |
69 | } | |
70 | ||
71 | this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId | |
72 | } | |
73 | ||
74 | /** | |
75 | * Let the host know that we're ready to go! | |
76 | */ | |
77 | private notifyReady () { | |
78 | this.isReady = true | |
79 | this.channel.notify({ method: 'ready', params: true }) | |
80 | } | |
81 | ||
82 | private setupStateTracking () { | |
83 | let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted' | |
84 | ||
85 | setInterval(() => { | |
86 | const position = this.element.currentTime | |
87 | const volume = this.element.volume | |
88 | const duration = this.element.duration | |
89 | ||
90 | this.channel.notify({ | |
91 | method: 'playbackStatusUpdate', | |
92 | params: { | |
93 | position, | |
94 | volume, | |
95 | duration: this.embed.player.duration(), | |
96 | playbackState: currentState | |
97 | } | |
98 | }) | |
99 | }, 500) | |
100 | ||
101 | this.element.addEventListener('play', ev => { | |
102 | currentState = 'playing' | |
103 | this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) | |
104 | }) | |
105 | ||
106 | this.element.addEventListener('pause', ev => { | |
107 | currentState = 'paused' | |
108 | this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) | |
109 | }) | |
110 | ||
111 | this.element.addEventListener('ended', ev => { | |
112 | currentState = 'ended' | |
113 | this.channel.notify({ method: 'playbackStatusChange', params: 'ended' }) | |
114 | }) | |
115 | ||
116 | // PeerTube specific capabilities | |
117 | ||
118 | if (this.isWebtorrent()) { | |
119 | this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions()) | |
120 | this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions()) | |
121 | } else { | |
122 | this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions()) | |
123 | } | |
124 | ||
125 | this.embed.player.on('volumechange', () => { | |
126 | this.channel.notify({ | |
127 | method: 'volumeChange', | |
128 | params: this.embed.player.volume() | |
129 | }) | |
130 | }) | |
131 | } | |
132 | ||
133 | private loadWebTorrentResolutions () { | |
134 | this.resolutions = [] | |
135 | ||
136 | const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId() | |
137 | ||
138 | for (const videoFile of this.embed.player.webtorrent().videoFiles) { | |
139 | let label = videoFile.resolution.label | |
140 | if (videoFile.fps && videoFile.fps >= 50) { | |
141 | label += videoFile.fps | |
142 | } | |
143 | ||
144 | this.resolutions.push({ | |
145 | id: videoFile.resolution.id, | |
146 | label, | |
147 | src: videoFile.magnetUri, | |
148 | active: videoFile.resolution.id === currentResolutionId, | |
149 | height: videoFile.resolution.id | |
150 | }) | |
151 | } | |
152 | ||
153 | this.channel.notify({ | |
154 | method: 'resolutionUpdate', | |
155 | params: this.resolutions | |
156 | }) | |
157 | } | |
158 | ||
159 | private loadP2PMediaLoaderResolutions () { | |
160 | this.resolutions = [] | |
161 | ||
162 | const qualityLevels = this.embed.player.qualityLevels() | |
163 | const currentResolutionId = this.embed.player.qualityLevels().selectedIndex | |
164 | ||
165 | for (let i = 0; i < qualityLevels.length; i++) { | |
166 | const level = qualityLevels[i] | |
167 | ||
168 | this.resolutions.push({ | |
169 | id: level.id, | |
170 | label: level.height + 'p', | |
171 | active: level.id === currentResolutionId, | |
172 | width: level.width, | |
173 | height: level.height | |
174 | }) | |
175 | } | |
176 | ||
177 | this.channel.notify({ | |
178 | method: 'resolutionUpdate', | |
179 | params: this.resolutions | |
180 | }) | |
181 | } | |
182 | ||
183 | private isWebtorrent () { | |
184 | return this.embed.player.webtorrent | |
185 | } | |
186 | } |