]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/embed-api.ts
Reorganize shared models
[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.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('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 const duration = this.element.duration
112
113 this.channel.notify({
114 method: 'playbackStatusUpdate',
115 params: {
116 position,
117 volume,
118 duration: this.embed.player.duration(),
119 playbackState: currentState
120 }
121 })
122 }, 500)
123
124 this.element.addEventListener('play', ev => {
125 currentState = 'playing'
126 this.channel.notify({ method: 'playbackStatusChange', params: 'playing' })
127 })
128
129 this.element.addEventListener('pause', ev => {
130 currentState = 'paused'
131 this.channel.notify({ method: 'playbackStatusChange', params: 'paused' })
132 })
133
134 this.element.addEventListener('ended', ev => {
135 currentState = 'ended'
136 this.channel.notify({ method: 'playbackStatusChange', params: 'ended' })
137 })
138
139 // PeerTube specific capabilities
140
141 if (this.isWebtorrent()) {
142 this.embed.player.webtorrent().on('autoResolutionUpdate', () => this.loadWebTorrentResolutions())
143 this.embed.player.webtorrent().on('videoFileUpdate', () => this.loadWebTorrentResolutions())
144 } else {
145 this.embed.player.p2pMediaLoader().on('resolutionChange', () => this.loadP2PMediaLoaderResolutions())
146 }
147
148 this.embed.player.on('volumechange', () => {
149 this.channel.notify({
150 method: 'volumeChange',
151 params: this.embed.player.volume()
152 })
153 })
154 }
155
156 private loadWebTorrentResolutions () {
157 this.resolutions = []
158
159 const currentResolutionId = this.embed.player.webtorrent().getCurrentResolutionId()
160
161 for (const videoFile of this.embed.player.webtorrent().videoFiles) {
162 let label = videoFile.resolution.label
163 if (videoFile.fps && videoFile.fps >= 50) {
164 label += videoFile.fps
165 }
166
167 this.resolutions.push({
168 id: videoFile.resolution.id,
169 label,
170 src: videoFile.magnetUri,
171 active: videoFile.resolution.id === currentResolutionId,
172 height: videoFile.resolution.id
173 })
174 }
175
176 this.channel.notify({
177 method: 'resolutionUpdate',
178 params: this.resolutions
179 })
180 }
181
182 private loadP2PMediaLoaderResolutions () {
183 this.resolutions = []
184
185 const qualityLevels = this.embed.player.qualityLevels()
186 const currentResolutionId = this.embed.player.qualityLevels().selectedIndex
187
188 for (let i = 0; i < qualityLevels.length; i++) {
189 const level = qualityLevels[i]
190
191 this.resolutions.push({
192 id: level.id,
193 label: level.height + 'p',
194 active: level.id === currentResolutionId,
195 width: level.width,
196 height: level.height
197 })
198 }
199
200 this.channel.notify({
201 method: 'resolutionUpdate',
202 params: this.resolutions
203 })
204 }
205
206 private isWebtorrent () {
207 return this.embed.player.webtorrent
208 }
209 }