]>
Commit | Line | Data |
---|---|---|
5efab546 C |
1 | import './embed.scss' |
2 | ||
3 | import * as Channel from 'jschannel' | |
1151f521 | 4 | import { PeerTubeResolution, PeerTubeTextTrack } from '../player/definitions' |
5efab546 C |
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 | |
6377a9f2 | 14 | private resolutions: PeerTubeResolution[] = [] |
5efab546 | 15 | |
9df52d66 C |
16 | constructor (private readonly embed: PeerTubeEmbed) { |
17 | ||
5efab546 C |
18 | } |
19 | ||
20 | initialize () { | |
21 | this.constructChannel() | |
22 | this.setupStateTracking() | |
23 | ||
24 | // We're ready! | |
25 | ||
26 | this.notifyReady() | |
27 | } | |
28 | ||
29 | private get element () { | |
f1a0f3b7 | 30 | return this.embed.getPlayerElement() |
5efab546 C |
31 | } |
32 | ||
33 | private constructChannel () { | |
f1a0f3b7 | 34 | const channel = Channel.build({ window: window.parent, origin: '*', scope: this.embed.getScope() }) |
5efab546 C |
35 | |
36 | channel.bind('play', (txn, params) => this.embed.player.play()) | |
37 | channel.bind('pause', (txn, params) => this.embed.player.pause()) | |
38 | channel.bind('seek', (txn, time) => this.embed.player.currentTime(time)) | |
6377a9f2 | 39 | |
5efab546 C |
40 | channel.bind('setVolume', (txn, value) => this.embed.player.volume(value)) |
41 | channel.bind('getVolume', (txn, value) => this.embed.player.volume()) | |
6377a9f2 | 42 | |
5efab546 | 43 | channel.bind('isReady', (txn, params) => this.isReady) |
6377a9f2 | 44 | |
5efab546 C |
45 | channel.bind('setResolution', (txn, resolutionId) => this.setResolution(resolutionId)) |
46 | channel.bind('getResolutions', (txn, params) => this.resolutions) | |
6377a9f2 | 47 | |
1151f521 | 48 | channel.bind('getCaptions', (txn, params) => this.getCaptions()) |
9df52d66 | 49 | channel.bind('setCaption', (txn, id) => this.setCaption(id)) |
1151f521 | 50 | |
5efab546 C |
51 | channel.bind('setPlaybackRate', (txn, playbackRate) => this.embed.player.playbackRate(playbackRate)) |
52 | channel.bind('getPlaybackRate', (txn, params) => this.embed.player.playbackRate()) | |
6377a9f2 | 53 | channel.bind('getPlaybackRates', (txn, params) => this.embed.player.options_.playbackRates) |
9054a8b6 | 54 | |
f1a0f3b7 C |
55 | channel.bind('playNextVideo', (txn, params) => this.embed.playNextPlaylistVideo()) |
56 | channel.bind('playPreviousVideo', (txn, params) => this.embed.playPreviousPlaylistVideo()) | |
57 | channel.bind('getCurrentPosition', (txn, params) => this.embed.getCurrentPlaylistPosition()) | |
5efab546 C |
58 | this.channel = channel |
59 | } | |
60 | ||
61 | private setResolution (resolutionId: number) { | |
6377a9f2 C |
62 | console.log('set resolution %d', resolutionId) |
63 | ||
64 | if (this.isWebtorrent()) { | |
65 | if (resolutionId === -1 && this.embed.player.webtorrent().isAutoResolutionPossible() === false) return | |
66 | ||
89ac282e | 67 | this.embed.player.webtorrent().changeQuality(resolutionId) |
5efab546 | 68 | |
5efab546 C |
69 | return |
70 | } | |
71 | ||
89ac282e | 72 | this.embed.player.p2pMediaLoader().getHLSJS().currentLevel = resolutionId |
5efab546 C |
73 | } |
74 | ||
1151f521 | 75 | private getCaptions (): PeerTubeTextTrack[] { |
9df52d66 C |
76 | return this.embed.player.textTracks().tracks_.map(t => ({ |
77 | id: t.id, | |
78 | src: t.src, | |
79 | label: t.label, | |
80 | mode: t.mode | |
81 | })) | |
1151f521 C |
82 | } |
83 | ||
84 | private setCaption (id: string) { | |
85 | const tracks = this.embed.player.textTracks().tracks_ | |
86 | ||
87 | for (const track of tracks) { | |
88 | if (track.id === id) track.mode = 'showing' | |
89 | else track.mode = 'disabled' | |
90 | } | |
91 | } | |
92 | ||
5efab546 C |
93 | /** |
94 | * Let the host know that we're ready to go! | |
95 | */ | |
96 | private notifyReady () { | |
97 | this.isReady = true | |
98 | this.channel.notify({ method: 'ready', params: true }) | |
99 | } | |
100 | ||
101 | private setupStateTracking () { | |
96aae68c | 102 | let currentState: 'playing' | 'paused' | 'unstarted' | 'ended' = 'unstarted' |
5efab546 C |
103 | |
104 | setInterval(() => { | |
105 | const position = this.element.currentTime | |
106 | const volume = this.element.volume | |
107 | ||
108 | this.channel.notify({ | |
109 | method: 'playbackStatusUpdate', | |
110 | params: { | |
111 | position, | |
112 | volume, | |
6ccdf9d5 | 113 | duration: this.embed.player.duration(), |
5efab546 C |
114 | playbackState: currentState |
115 | } | |
116 | }) | |
117 | }, 500) | |
118 | ||
119 | this.element.addEventListener('play', ev => { | |
120 | currentState = 'playing' | |
121 | this.channel.notify({ method: 'playbackStatusChange', params: 'playing' }) | |
122 | }) | |
123 | ||
124 | this.element.addEventListener('pause', ev => { | |
125 | currentState = 'paused' | |
126 | this.channel.notify({ method: 'playbackStatusChange', params: 'paused' }) | |
127 | }) | |
128 | ||
96aae68c C |
129 | this.element.addEventListener('ended', ev => { |
130 | currentState = 'ended' | |
131 | this.channel.notify({ method: 'playbackStatusChange', params: 'ended' }) | |
132 | }) | |
133 | ||
5efab546 | 134 | // PeerTube specific capabilities |
e367da94 C |
135 | this.embed.player.peertubeResolutions().on('resolutionsAdded', () => this.loadResolutions()) |
136 | this.embed.player.peertubeResolutions().on('resolutionChanged', () => this.loadResolutions()) | |
6377a9f2 | 137 | |
89ac282e C |
138 | this.loadResolutions() |
139 | ||
6377a9f2 C |
140 | this.embed.player.on('volumechange', () => { |
141 | this.channel.notify({ | |
142 | method: 'volumeChange', | |
143 | params: this.embed.player.volume() | |
144 | }) | |
145 | }) | |
5efab546 C |
146 | } |
147 | ||
e367da94 C |
148 | private loadResolutions () { |
149 | this.resolutions = this.embed.player.peertubeResolutions().getResolutions() | |
150 | .map(r => ({ | |
151 | id: r.id, | |
89ac282e | 152 | label: r.label, |
e367da94 C |
153 | active: r.selected, |
154 | width: r.width, | |
155 | height: r.height | |
156 | })) | |
6377a9f2 C |
157 | |
158 | this.channel.notify({ | |
159 | method: 'resolutionUpdate', | |
160 | params: this.resolutions | |
161 | }) | |
162 | } | |
163 | ||
164 | private isWebtorrent () { | |
ffacd78f | 165 | return !!this.embed.player.webtorrent |
6377a9f2 | 166 | } |
5efab546 | 167 | } |