]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player.ts
Add user history and resume videos
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player.ts
CommitLineData
c6352f2c
C
1import { VideoFile } from '../../../../shared/models/videos'
2
3import 'videojs-hotkeys'
c7b0dacb 4import 'videojs-dock'
960a11e8 5import 'videojs-contextmenu-ui'
c6352f2c
C
6import './peertube-link-button'
7import './resolution-menu-button'
8import './settings-menu-button'
9import './webtorrent-info-button'
10import './peertube-videojs-plugin'
77728efa 11import './peertube-load-progress-bar'
054a103b 12import './theater-button'
6e46de09 13import { UserWatching, VideoJSCaption, videojsUntyped } from './peertube-videojs-typings'
960a11e8 14import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
9f164722 15import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
c6352f2c
C
16
17// Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
18videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
16f7022b
C
19// Change Captions to Subtitles/CC
20videojsUntyped.getComponent('CaptionsButton').prototype.controlText_ = 'Subtitles/CC'
21// We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know)
22videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' '
c6352f2c
C
23
24function getVideojsOptions (options: {
25 autoplay: boolean,
26 playerElement: HTMLVideoElement,
27 videoViewUrl: string,
28 videoDuration: number,
29 videoFiles: VideoFile[],
30 enableHotkeys: boolean,
31 inactivityTimeout: number,
33d78552 32 peertubeLink: boolean,
f37bad63 33 poster: string,
1f6824c9 34 startTime: number | string
99941732 35 theaterMode: boolean,
16f7022b 36 videoCaptions: VideoJSCaption[],
6e46de09 37
95d51135 38 language?: string,
99941732
WL
39 controls?: boolean,
40 muted?: boolean,
41 loop?: boolean
6e46de09
C
42
43 userWatching?: UserWatching
c6352f2c
C
44}) {
45 const videojsOptions = {
16f7022b
C
46 // We don't use text track settings for now
47 textTrackSettings: false,
99941732
WL
48 controls: options.controls !== undefined ? options.controls : true,
49 muted: options.controls !== undefined ? options.muted : false,
50 loop: options.loop !== undefined ? options.loop : false,
33d78552 51 poster: options.poster,
80109b2d 52 autoplay: false,
c6352f2c 53 inactivityTimeout: options.inactivityTimeout,
4ccb6c08 54 playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 2 ],
c6352f2c
C
55 plugins: {
56 peertube: {
80109b2d 57 autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
16f7022b 58 videoCaptions: options.videoCaptions,
c6352f2c
C
59 videoFiles: options.videoFiles,
60 playerElement: options.playerElement,
61 videoViewUrl: options.videoViewUrl,
f37bad63 62 videoDuration: options.videoDuration,
6e46de09
C
63 startTime: options.startTime,
64 userWatching: options.userWatching
c6352f2c
C
65 }
66 },
67 controlBar: {
68 children: getControlBarChildren(options)
69 }
70 }
71
72 if (options.enableHotkeys === true) {
73 Object.assign(videojsOptions.plugins, {
74 hotkeys: {
c4ccb08a 75 enableVolumeScroll: false,
52b1ba03 76 enableModifiersForNumbers: false,
0f40d69a
RK
77
78 fullscreenKey: function (event) {
79 // fullscreen with the f key or Ctrl+Enter
80 return event.key === 'f' || (event.ctrlKey && event.key === 'Enter')
81 },
82
83 seekStep: function (event) {
84 // mimic VLC seek behavior, and default to 5 (original value is 5).
85 if (event.ctrlKey && event.altKey) {
86 return 5 * 60
87 } else if (event.ctrlKey) {
88 return 60
89 } else if (event.altKey) {
90 return 10
91 } else {
92 return 5
93 }
94 },
95
52b1ba03
RK
96 customKeys: {
97 increasePlaybackRateKey: {
98 key: function (event) {
5363a766 99 return event.key === '>'
52b1ba03 100 },
5363a766
C
101 handler: function (player) {
102 player.playbackRate((player.playbackRate() + 0.1).toFixed(2))
52b1ba03
RK
103 }
104 },
105 decreasePlaybackRateKey: {
106 key: function (event) {
5363a766 107 return event.key === '<'
52b1ba03 108 },
5363a766
C
109 handler: function (player) {
110 player.playbackRate((player.playbackRate() - 0.1).toFixed(2))
52b1ba03 111 }
0f40d69a
RK
112 },
113 frameByFrame: {
114 key: function (event) {
115 return event.key === '.'
116 },
117 handler: function (player, options, event) {
118 player.pause()
119 // Calculate movement distance (assuming 30 fps)
120 const dist = 1 / 30
121 player.currentTime(player.currentTime() + dist)
122 }
52b1ba03
RK
123 }
124 }
c6352f2c
C
125 }
126 })
127 }
128
95d51135
C
129 if (options.language && !isDefaultLocale(options.language)) {
130 Object.assign(videojsOptions, { language: options.language })
131 }
132
c6352f2c
C
133 return videojsOptions
134}
135
136function getControlBarChildren (options: {
137 peertubeLink: boolean
16f7022b
C
138 theaterMode: boolean,
139 videoCaptions: VideoJSCaption[]
c6352f2c 140}) {
16f7022b
C
141 const settingEntries = []
142
143 // Keep an order
144 settingEntries.push('playbackRateMenuButton')
145 if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton')
146 settingEntries.push('resolutionMenuButton')
147
c6352f2c
C
148 const children = {
149 'playToggle': {},
150 'currentTimeDisplay': {},
151 'timeDivider': {},
152 'durationDisplay': {},
153 'liveDisplay': {},
154
155 'flexibleWidthSpacer': {},
77728efa
C
156 'progressControl': {
157 children: {
158 'seekBar': {
159 children: {
160 'peerTubeLoadProgressBar': {},
db5529f5 161 'mouseTimeDisplay': {},
77728efa
C
162 'playProgressBar': {}
163 }
164 }
165 }
166 },
c6352f2c
C
167
168 'webTorrentButton': {},
169
170 'muteToggle': {},
171 'volumeControl': {},
172
173 'settingsButton': {
174 setup: {
175 maxHeightOffset: 40
176 },
16f7022b 177 entries: settingEntries
c6352f2c
C
178 }
179 }
180
181 if (options.peertubeLink === true) {
182 Object.assign(children, {
183 'peerTubeLinkButton': {}
184 })
185 }
186
054a103b
C
187 if (options.theaterMode === true) {
188 Object.assign(children, {
189 'theaterButton': {}
190 })
191 }
192
c6352f2c
C
193 Object.assign(children, {
194 'fullscreenToggle': {}
195 })
196
197 return children
198}
199
e945b184 200function addContextMenu (player: any, videoEmbedUrl: string) {
e945b184
C
201 player.contextmenuUI({
202 content: [
203 {
204 label: player.localize('Copy the video URL'),
205 listener: function () {
206 copyToClipboard(buildVideoLink())
207 }
208 },
209 {
210 label: player.localize('Copy the video URL at the current time'),
211 listener: function () {
212 const player = this
213 copyToClipboard(buildVideoLink(player.currentTime()))
214 }
215 },
216 {
217 label: player.localize('Copy embed code'),
218 listener: () => {
219 copyToClipboard(buildVideoEmbed(videoEmbedUrl))
220 }
5511da62
RK
221 },
222 {
223 label: player.localize('Copy magnet URI'),
224 listener: function () {
225 const player = this
226 copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri)
227 }
e945b184
C
228 }
229 ]
230 })
231}
232
3dfa8494
C
233function loadLocaleInVideoJS (serverUrl: string, videojs: any, locale: string) {
234 const path = getLocalePath(serverUrl, locale)
235 // It is the default locale, nothing to translate
236 if (!path) return Promise.resolve(undefined)
e945b184 237
5d128505
C
238 let p: Promise<any>
239
240 if (loadLocaleInVideoJS.cache[path]) {
241 p = Promise.resolve(loadLocaleInVideoJS.cache[path])
242 } else {
243 p = fetch(path + '/player.json')
244 .then(res => res.json())
245 .then(json => {
246 loadLocaleInVideoJS.cache[path] = json
247 return json
248 })
249 }
74b7c6d4 250
5d128505
C
251 const completeLocale = getCompleteLocale(locale)
252 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
253}
254namespace loadLocaleInVideoJS {
255 export const cache: { [ path: string ]: any } = {}
e945b184
C
256}
257
3dfa8494
C
258function getServerTranslations (serverUrl: string, locale: string) {
259 const path = getLocalePath(serverUrl, locale)
260 // It is the default locale, nothing to translate
261 if (!path) return Promise.resolve(undefined)
262
263 return fetch(path + '/server.json')
264 .then(res => res.json())
265}
266
267// ############################################################################
268
e945b184 269export {
3dfa8494
C
270 getServerTranslations,
271 loadLocaleInVideoJS,
e945b184
C
272 getVideojsOptions,
273 addContextMenu
274}
3dfa8494
C
275
276// ############################################################################
277
278function getLocalePath (serverUrl: string, locale: string) {
279 const completeLocale = getCompleteLocale(locale)
280
281 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
282
283 return serverUrl + '/client/locales/' + completeLocale
284}