]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/embed-api.ts
Add logic to handle playlist in embed
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed-api.ts
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 this.channel = channel
54 }
55
56 private setResolution (resolutionId: number) {
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)
70
71 return
72 }
73
74 this.embed.player.p2pMediaLoader().getHLSJS().nextLevel = resolutionId
75 }
76
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
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 () {
106 let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted'
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,
117 duration: this.embed.player.duration(),
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
133 this.element.addEventListener('ended', ev => {
134 currentState = 'ended'
135 this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
136 })
137
138 // PeerTube specific capabilities
139
140 if (this.isWebtorrent()) {
141 this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
142 this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
143 } else {
144 this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
145 }
146
147 this.embed.player.on('volumechange', () => {
148 this.channel.notify({
149 method: 'volumeChange',
150 params: this.embed.player.volume()
151 })
152 })
153 }
154
155 private loadWebTorrentResolutions () {
156 this.resolutions = []
157
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
166 this.resolutions.push({
167 id: videoFile.resolution.id,
168 label,
169 src: videoFile.magnetUri,
170 active: videoFile.resolution.id === currentResolutionId,
171 height: videoFile.resolution.id
172 })
173 }
174
175 this.channel.notify({
176 method: 'resolutionUpdate',
177 params: this.resolutions
178 })
179 }
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 }
208 }