]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/embed-api.ts
Increase max image/caption/torrent upload size
[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
C
142 // PeerTube specific capabilities
143
6377a9f2 144 if (this.isWebtorrent()) {
5efab546
C
145 this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
146 this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
6377a9f2
C
147 } else {
148 this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
5efab546 149 }
6377a9f2
C
150
151 this.embed.player.on('volumechange', () => {
152 this.channel.notify({
153 method: 'volumeChange',
154 params: this.embed.player.volume()
155 })
156 })
5efab546
C
157 }
158
159 private loadWebTorrentResolutions () {
6377a9f2
C
160 this.resolutions = []
161
5efab546
C
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
6377a9f2 170 this.resolutions.push({
5efab546
C
171 id: videoFile.resolution.id,
172 label,
173 src: videoFile.magnetUri,
6377a9f2
C
174 active: videoFile.resolution.id === currentResolutionId,
175 height: videoFile.resolution.id
5efab546
C
176 })
177 }
178
5efab546
C
179 this.channel.notify({
180 method: 'resolutionUpdate',
181 params: this.resolutions
182 })
183 }
6377a9f2
C
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 }
5efab546 212}