]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/embed-api.ts
Fix webpack config
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed-api.ts
CommitLineData
5efab546
C
1import './embed.scss'
2
3import * as Channel from 'jschannel'
1151f521 4import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions'
5efab546
C
5import { 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 */
11export 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)
9054a8b6
C
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())
5efab546
C
57 this.channel = channel
58 }
59
60 private setResolution (resolutionId: number) {
6377a9f2
C
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)
5efab546 74
5efab546
C
75 return
76 }
77
6377a9f2 78 this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId
5efab546
C
79 }
80
1151f521
C
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
5efab546
C
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 () {
96aae68c 110 let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
5efab546
C
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,
6ccdf9d5 121 duration: this.embed.player.duration(),
5efab546
C
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
96aae68c
C
137 this.element.addEventListener('ended', ev => {
138 currentState = 'ended'
139 this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
140 })
141
5efab546 142 // PeerTube specific capabilities
6377a9f2 143 if (this.isWebtorrent()) {
5efab546
C
144 this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
145 this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
ffacd78f
C
146
147 this.loadWebTorrentResolutions()
6377a9f2
C
148 } else {
149 this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
ffacd78f 150 this.embed.player.p2pMediaLoader().on('resolutionsLoaded', () => this.loadP2PMediaLoaderResolutions())
5efab546 151 }
6377a9f2
C
152
153 this.embed.player.on('volumechange', () => {
154 this.channel.notify({
155 method: 'volumeChange',
156 params: this.embed.player.volume()
157 })
158 })
5efab546
C
159 }
160
161 private loadWebTorrentResolutions () {
6377a9f2
C
162 this.resolutions = []
163
5efab546
C
164 const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
165
166 for (const videoFile of this.embed.player.webtorrent().videoFiles) {
167 let label = videoFile.resolution.label
168 if (videoFile.fps && videoFile.fps >= 50) {
169 label += videoFile.fps
170 }
171
6377a9f2 172 this.resolutions.push({
5efab546
C
173 id: videoFile.resolution.id,
174 label,
175 src: videoFile.magnetUri,
6377a9f2
C
176 active: videoFile.resolution.id === currentResolutionId,
177 height: videoFile.resolution.id
5efab546
C
178 })
179 }
180
5efab546
C
181 this.channel.notify({
182 method: 'resolutionUpdate',
183 params: this.resolutions
184 })
185 }
6377a9f2
C
186
187 private loadP2PMediaLoaderResolutions () {
188 this.resolutions = []
189
190 const qualityLevels = this.embed.player.qualityLevels()
191 const currentResolutionId = this.embed.player.qualityLevels().selectedIndex
192
193 for (let i = 0; i < qualityLevels.length; i++) {
194 const level = qualityLevels[i]
195
196 this.resolutions.push({
197 id: level.id,
198 label: level.height + 'p',
199 active: level.id === currentResolutionId,
200 width: level.width,
201 height: level.height
202 })
203 }
204
205 this.channel.notify({
206 method: 'resolutionUpdate',
207 params: this.resolutions
208 })
209 }
210
211 private isWebtorrent () {
ffacd78f 212 return !!this.embed.player.webtorrent
6377a9f2 213 }
5efab546 214}