]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Add max file size, max files and ip anonymize log options
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
e972e046 1import { catchError } from 'rxjs/operators'
3b492bff 2import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 3import { ActivatedRoute, Router } from '@angular/router'
901637bb 4import { RedirectService } from '@app/core/routing/redirect.service'
0bd78bf3 5import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
07fa4c97 6import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
1f3e9fec 7import { MetaService } from '@ngx-meta/core'
3d9a63d3 8import { AuthUser, Notifier, ServerService } from '@app/core'
4c72c1cd 9import { forkJoin, Observable, Subscription } from 'rxjs'
20d21199 10import { Hotkey, HotkeysService } from 'angular2-hotkeys'
72675ebe 11import { UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '../../../../../shared'
df98563e 12import { AuthService, ConfirmService } from '../../core'
a51bad1a 13import { RestExtractor, VideoBlacklistService } from '../../shared'
ff249f49 14import { VideoDetails } from '../../shared/video/video-details.model'
63c4db6d 15import { VideoService } from '../../shared/video/video.service'
4635f59d 16import { VideoShareComponent } from './modal/video-share.component'
20d21199 17import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
989e526a 18import { I18n } from '@ngx-translate/i18n-polyfill'
e945b184 19import { environment } from '../../../environments/environment'
16f7022b 20import { VideoCaptionService } from '@app/shared/video-caption'
1506307f 21import { MarkdownService } from '@app/shared/renderer'
6ec0b75b 22import {
5f85f8aa 23 videojs,
5efab546 24 CustomizationOptions,
6ec0b75b
C
25 P2PMediaLoaderOptions,
26 PeertubePlayerManager,
27 PeertubePlayerManagerOptions,
597a9266 28 PlayerMode
6ec0b75b 29} from '../../../assets/player/peertube-player-manager'
e2f01c47
C
30import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
31import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
e2f01c47 32import { Video } from '@app/shared/video/video.model'
5efab546 33import { isWebRTCDisabled, timeToInt } from '../../../assets/player/utils'
72675ebe 34import { VideoWatchPlaylistComponent } from '@app/videos/+video-watch/video-watch-playlist.component'
011e1e6b 35import { getStoredTheater } from '../../../assets/player/peertube-player-local-storage'
18a6f04c 36import { PluginService } from '@app/core/plugins/plugin.service'
93cae479 37import { HooksService } from '@app/core/plugins/hooks.service'
60c2bc80 38import { PlatformLocation } from '@angular/common'
6aa54148 39import { randomInt } from '@shared/core-utils/miscs/miscs'
dc8bc31b 40
dc8bc31b
C
41@Component({
42 selector: 'my-video-watch',
ec8d8440
C
43 templateUrl: './video-watch.component.html',
44 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b 45})
0629423c 46export class VideoWatchComponent implements OnInit, OnDestroy {
22b59e80
C
47 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
48
f36da21e
C
49 @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent
50 @ViewChild('videoShareModal', { static: false }) videoShareModal: VideoShareComponent
51 @ViewChild('videoSupportModal', { static: false }) videoSupportModal: VideoSupportComponent
52 @ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent
df98563e 53
2adfc7ea 54 player: any
0826c92d 55 playerElement: HTMLVideoElement
9a18a625 56 theaterEnabled = false
154898b0 57 userRating: UserVideoRateType = null
80958c78 58 descriptionLoading = false
2de96f4d 59
2f4c784a
C
60 video: VideoDetails = null
61 videoCaptions: VideoCaption[] = []
62
e2f01c47 63 playlist: VideoPlaylist = null
e2f01c47 64
2de96f4d
C
65 completeDescriptionShown = false
66 completeVideoDescription: string
67 shortVideoDescription: string
9d9597df 68 videoHTMLDescription = ''
e9189001 69 likesBarTooltipText = ''
73e09f27 70 hasAlreadyAcceptedPrivacyConcern = false
6d88de72 71 remoteServerDown = false
3d216ea0 72 hotkeys: Hotkey[] = []
df98563e 73
6aa54148 74 private nextVideoUuid = ''
f0a39880 75 private currentTime: number
df98563e 76 private paramsSub: Subscription
e2f01c47 77 private queryParamsSub: Subscription
31b6ddf8 78 private configSub: Subscription
df98563e
C
79
80 constructor (
4fd8aa32 81 private elementRef: ElementRef,
3b492bff 82 private changeDetector: ChangeDetectorRef,
0629423c 83 private route: ActivatedRoute,
92fb909c 84 private router: Router,
d3ef341a 85 private videoService: VideoService,
e2f01c47 86 private playlistService: VideoPlaylistService,
35bf0c83 87 private videoBlacklistService: VideoBlacklistService,
92fb909c 88 private confirmService: ConfirmService,
3ec343a4 89 private metaService: MetaService,
7ddd02c9 90 private authService: AuthService,
0883b324 91 private serverService: ServerService,
a51bad1a 92 private restExtractor: RestExtractor,
f8b2c1b4 93 private notifier: Notifier,
18a6f04c 94 private pluginService: PluginService,
7ae71355 95 private markdownService: MarkdownService,
901637bb 96 private zone: NgZone,
989e526a 97 private redirectService: RedirectService,
16f7022b 98 private videoCaptionService: VideoCaptionService,
e945b184 99 private i18n: I18n,
20d21199 100 private hotkeysService: HotkeysService,
93cae479 101 private hooks: HooksService,
60c2bc80 102 private location: PlatformLocation,
e945b184 103 @Inject(LOCALE_ID) private localeId: string
d3ef341a 104 ) {}
dc8bc31b 105
b2731bff
C
106 get user () {
107 return this.authService.getUser()
108 }
109
18a6f04c 110 async ngOnInit () {
31b6ddf8
C
111 this.configSub = this.serverService.configLoaded
112 .subscribe(() => {
113 if (
114 isWebRTCDisabled() ||
115 this.serverService.getConfig().tracker.enabled === false ||
116 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
117 ) {
118 this.hasAlreadyAcceptedPrivacyConcern = true
119 }
120 })
2b3b76ab 121
13fc89f4 122 this.paramsSub = this.route.params.subscribe(routeParams => {
e2f01c47
C
123 const videoId = routeParams[ 'videoId' ]
124 if (videoId) this.loadVideo(videoId)
a51bad1a 125
e2f01c47
C
126 const playlistId = routeParams[ 'playlistId' ]
127 if (playlistId) this.loadPlaylist(playlistId)
128 })
bf079b7b 129
e2f01c47
C
130 this.queryParamsSub = this.route.queryParams.subscribe(queryParams => {
131 const videoId = queryParams[ 'videoId' ]
132 if (videoId) this.loadVideo(videoId)
df98563e 133 })
20d21199 134
1c8ddbfa 135 this.initHotkeys()
011e1e6b
C
136
137 this.theaterEnabled = getStoredTheater()
18a6f04c 138
c9e3eeed 139 this.hooks.runAction('action:video-watch.init', 'video-watch')
d1992b93
C
140 }
141
df98563e 142 ngOnDestroy () {
09edde40 143 this.flushPlayer()
067e3f84 144
13fc89f4 145 // Unsubscribe subscriptions
e2f01c47
C
146 if (this.paramsSub) this.paramsSub.unsubscribe()
147 if (this.queryParamsSub) this.queryParamsSub.unsubscribe()
20d21199
RK
148
149 // Unbind hotkeys
3d216ea0 150 this.hotkeysService.remove(this.hotkeys)
dc8bc31b 151 }
98b01bac 152
df98563e
C
153 setLike () {
154 if (this.isUserLoggedIn() === false) return
4c72c1cd
C
155
156 // Already liked this video
157 if (this.userRating === 'like') this.setRating('none')
158 else this.setRating('like')
d38b8281
C
159 }
160
df98563e
C
161 setDislike () {
162 if (this.isUserLoggedIn() === false) return
4c72c1cd
C
163
164 // Already disliked this video
165 if (this.userRating === 'dislike') this.setRating('none')
166 else this.setRating('dislike')
d38b8281
C
167 }
168
0d3a9be9
C
169 getRatePopoverText () {
170 if (this.isUserLoggedIn()) return undefined
171
172 return this.i18n('You need to be connected to rate this content.')
173 }
174
2de96f4d 175 showMoreDescription () {
2de96f4d
C
176 if (this.completeVideoDescription === undefined) {
177 return this.loadCompleteDescription()
178 }
179
180 this.updateVideoDescription(this.completeVideoDescription)
80958c78 181 this.completeDescriptionShown = true
2de96f4d
C
182 }
183
184 showLessDescription () {
2de96f4d 185 this.updateVideoDescription(this.shortVideoDescription)
80958c78 186 this.completeDescriptionShown = false
2de96f4d
C
187 }
188
189 loadCompleteDescription () {
80958c78
C
190 this.descriptionLoading = true
191
2de96f4d 192 this.videoService.loadCompleteDescription(this.video.descriptionPath)
2186386c
C
193 .subscribe(
194 description => {
195 this.completeDescriptionShown = true
196 this.descriptionLoading = false
197
198 this.shortVideoDescription = this.video.description
199 this.completeVideoDescription = description
200
201 this.updateVideoDescription(this.completeVideoDescription)
202 },
203
204 error => {
205 this.descriptionLoading = false
f8b2c1b4 206 this.notifier.error(error.message)
2186386c
C
207 }
208 )
2de96f4d
C
209 }
210
07fa4c97 211 showSupportModal () {
689a4f69
C
212 this.pausePlayer()
213
07fa4c97
C
214 this.videoSupportModal.show()
215 }
216
df98563e 217 showShareModal () {
689a4f69
C
218 this.pausePlayer()
219
f0a39880 220 this.videoShareModal.show(this.currentTime)
99cc4f49
C
221 }
222
df98563e
C
223 isUserLoggedIn () {
224 return this.authService.isLoggedIn()
4f8c0eb0
C
225 }
226
b1fa3eba
C
227 getVideoTags () {
228 if (!this.video || Array.isArray(this.video.tags) === false) return []
229
4278710d 230 return this.video.tags
b1fa3eba
C
231 }
232
6aa54148
L
233 onRecommendations (videos: Video[]) {
234 if (videos.length > 0) {
235 // Pick a random video until the recommendations are improved
236 this.nextVideoUuid = videos[randomInt(0,videos.length - 1)].uuid
237 }
238 }
239
689a4f69
C
240 onModalOpened () {
241 this.pausePlayer()
242 }
243
3a0fb65c
C
244 onVideoRemoved () {
245 this.redirectService.redirectToHomepage()
6725d05c
C
246 }
247
73e09f27 248 acceptedPrivacyConcern () {
0bd78bf3 249 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
73e09f27
C
250 this.hasAlreadyAcceptedPrivacyConcern = true
251 }
252
2186386c
C
253 isVideoToTranscode () {
254 return this.video && this.video.state.id === VideoState.TO_TRANSCODE
255 }
256
516df59b
C
257 isVideoToImport () {
258 return this.video && this.video.state.id === VideoState.TO_IMPORT
259 }
260
bbe0f064
C
261 hasVideoScheduledPublication () {
262 return this.video && this.video.scheduledUpdate !== undefined
263 }
264
e2f01c47
C
265 isVideoBlur (video: Video) {
266 return video.isVideoNSFWForUser(this.user, this.serverService.getConfig())
267 }
268
e2f01c47
C
269 private loadVideo (videoId: string) {
270 // Video did not change
271 if (this.video && this.video.uuid === videoId) return
272
273 if (this.player) this.player.pause()
274
93cae479
C
275 const videoObs = this.hooks.wrapObsFun(
276 this.videoService.getVideo.bind(this.videoService),
277 { videoId },
278 'video-watch',
279 'filter:api.video-watch.video.get.params',
280 'filter:api.video-watch.video.get.result'
281 )
282
e2f01c47 283 // Video did change
c8861d5d 284 forkJoin([
93cae479 285 videoObs,
e2f01c47 286 this.videoCaptionService.listCaptions(videoId)
c8861d5d 287 ])
e2f01c47
C
288 .pipe(
289 // If 401, the video is private or blacklisted so redirect to 404
290 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ]))
291 )
292 .subscribe(([ video, captionsResult ]) => {
293 const queryParams = this.route.snapshot.queryParams
e2f01c47 294
4c72c1cd
C
295 const urlOptions = {
296 startTime: queryParams.start,
297 stopTime: queryParams.stop,
5efab546
C
298
299 muted: queryParams.muted,
300 loop: queryParams.loop,
4c72c1cd 301 subtitle: queryParams.subtitle,
5efab546
C
302
303 playerMode: queryParams.mode,
304 peertubeLink: false
4c72c1cd
C
305 }
306
307 this.onVideoFetched(video, captionsResult.data, urlOptions)
e2f01c47
C
308 .catch(err => this.handleError(err))
309 })
310 }
311
312 private loadPlaylist (playlistId: string) {
313 // Playlist did not change
314 if (this.playlist && this.playlist.uuid === playlistId) return
315
316 this.playlistService.getVideoPlaylist(playlistId)
317 .pipe(
318 // If 401, the video is private or blacklisted so redirect to 404
319 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ]))
320 )
321 .subscribe(playlist => {
322 this.playlist = playlist
323
324 const videoId = this.route.snapshot.queryParams['videoId']
72675ebe 325 this.videoWatchPlaylist.loadPlaylistElements(playlist, !videoId)
e2f01c47
C
326 })
327 }
328
2de96f4d
C
329 private updateVideoDescription (description: string) {
330 this.video.description = description
331 this.setVideoDescriptionHTML()
4c72c1cd 332 .catch(err => console.error(err))
2de96f4d
C
333 }
334
41d71344
C
335 private async setVideoDescriptionHTML () {
336 this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description)
2de96f4d
C
337 }
338
e9189001 339 private setVideoLikesBarTooltipText () {
2186386c
C
340 this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', {
341 likesNumber: this.video.likes,
342 dislikesNumber: this.video.dislikes
343 })
e9189001
C
344 }
345
0c31c33d
C
346 private handleError (err: any) {
347 const errorMessage: string = typeof err === 'string' ? err : err.message
bf5685f0
C
348 if (!errorMessage) return
349
6d88de72 350 // Display a message in the video player instead of a notification
0f7fedc3 351 if (errorMessage.indexOf('from xs param') !== -1) {
6d88de72
C
352 this.flushPlayer()
353 this.remoteServerDown = true
3b492bff
C
354 this.changeDetector.detectChanges()
355
6d88de72 356 return
0c31c33d
C
357 }
358
f8b2c1b4 359 this.notifier.error(errorMessage)
0c31c33d
C
360 }
361
df98563e 362 private checkUserRating () {
d38b8281 363 // Unlogged users do not have ratings
df98563e 364 if (this.isUserLoggedIn() === false) return
d38b8281
C
365
366 this.videoService.getUserVideoRating(this.video.id)
2186386c
C
367 .subscribe(
368 ratingObject => {
369 if (ratingObject) {
370 this.userRating = ratingObject.rating
371 }
372 },
373
f8b2c1b4 374 err => this.notifier.error(err.message)
2186386c 375 )
d38b8281
C
376 }
377
597a9266
C
378 private async onVideoFetched (
379 video: VideoDetails,
380 videoCaptions: VideoCaption[],
5efab546 381 urlOptions: CustomizationOptions & { playerMode: PlayerMode }
597a9266 382 ) {
df98563e 383 this.video = video
2f4c784a 384 this.videoCaptions = videoCaptions
92fb909c 385
c448d412
C
386 // Re init attributes
387 this.descriptionLoading = false
388 this.completeDescriptionShown = false
6d88de72 389 this.remoteServerDown = false
f0a39880 390 this.currentTime = undefined
c448d412 391
72675ebe 392 this.videoWatchPlaylist.updatePlaylistIndex(video)
e2f01c47 393
e2f01c47 394 if (this.isVideoBlur(this.video)) {
22b59e80 395 const res = await this.confirmService.confirm(
989e526a
C
396 this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'),
397 this.i18n('Mature or explicit content')
d6e32a2e 398 )
60c2bc80 399 if (res === false) return this.location.back()
92fb909c
C
400 }
401
09edde40
C
402 // Flush old player if needed
403 this.flushPlayer()
b891f9bc 404
60c2bc80 405 // Build video element, because videojs removes it on dispose
e2f01c47 406 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper')
b891f9bc
C
407 this.playerElement = document.createElement('video')
408 this.playerElement.className = 'video-js vjs-peertube-skin'
e7eb5b39 409 this.playerElement.setAttribute('playsinline', 'true')
b891f9bc
C
410 playerElementWrapper.appendChild(this.playerElement)
411
3d9a63d3
C
412 const params = {
413 video: this.video,
414 videoCaptions,
415 urlOptions,
416 user: this.user
e945b184 417 }
3d9a63d3
C
418 const { playerMode, playerOptions } = await this.hooks.wrapFun(
419 this.buildPlayerManagerOptions.bind(this),
420 params,
c2023a9f
C
421 'video-watch',
422 'filter:internal.video-watch.player.build-options.params',
3d9a63d3
C
423 'filter:internal.video-watch.player.build-options.result'
424 )
e945b184 425
e945b184 426 this.zone.runOutsideAngular(async () => {
3d9a63d3 427 this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player)
d275e754 428 this.player.focus()
9a18a625 429
2adfc7ea 430 this.player.on('customError', ({ err }: { err: any }) => this.handleError(err))
f0a39880
C
431
432 this.player.on('timeupdate', () => {
433 this.currentTime = Math.floor(this.player.currentTime())
434 })
e2f01c47
C
435
436 this.player.one('ended', () => {
437 if (this.playlist) {
72675ebe 438 this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo())
6aa54148
L
439 } else if (this.user && this.user.autoPlayNextVideo) {
440 this.zone.run(() => this.autoplayNext())
e2f01c47
C
441 }
442 })
443
444 this.player.one('stopped', () => {
445 if (this.playlist) {
72675ebe 446 this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo())
e2f01c47
C
447 }
448 })
9a18a625
C
449
450 this.player.on('theaterChange', (_: any, enabled: boolean) => {
451 this.zone.run(() => this.theaterEnabled = enabled)
452 })
5f85f8aa
RK
453
454 this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player })
b891f9bc 455 })
22b59e80
C
456
457 this.setVideoDescriptionHTML()
458 this.setVideoLikesBarTooltipText()
459
460 this.setOpenGraphTags()
461 this.checkUserRating()
93cae479 462
5f85f8aa 463 this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', { videojs })
92fb909c
C
464 }
465
6aa54148
L
466 private autoplayNext () {
467 if (this.nextVideoUuid) {
468 this.router.navigate([ '/videos/watch', this.nextVideoUuid ])
469 }
470 }
471
5c6d985f 472 private setRating (nextRating: UserVideoRateType) {
4c72c1cd
C
473 const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = {
474 like: this.videoService.setVideoLike,
475 dislike: this.videoService.setVideoDislike,
476 none: this.videoService.unsetVideoLike
57a49263
BB
477 }
478
4c72c1cd 479 ratingMethods[nextRating].call(this.videoService, this.video.id)
2186386c
C
480 .subscribe(
481 () => {
482 // Update the video like attribute
483 this.updateVideoRating(this.userRating, nextRating)
484 this.userRating = nextRating
485 },
486
f8b2c1b4 487 (err: { message: string }) => this.notifier.error(err.message)
2186386c 488 )
57a49263
BB
489 }
490
5c6d985f 491 private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) {
df98563e
C
492 let likesToIncrement = 0
493 let dislikesToIncrement = 0
d38b8281
C
494
495 if (oldRating) {
df98563e
C
496 if (oldRating === 'like') likesToIncrement--
497 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
498 }
499
df98563e
C
500 if (newRating === 'like') likesToIncrement++
501 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 502
df98563e
C
503 this.video.likes += likesToIncrement
504 this.video.dislikes += dislikesToIncrement
20b40b19 505
22b59e80 506 this.video.buildLikeAndDislikePercents()
20b40b19 507 this.setVideoLikesBarTooltipText()
d38b8281
C
508 }
509
df98563e
C
510 private setOpenGraphTags () {
511 this.metaService.setTitle(this.video.name)
758b996d 512
df98563e 513 this.metaService.setTag('og:type', 'video')
3ec343a4 514
df98563e
C
515 this.metaService.setTag('og:title', this.video.name)
516 this.metaService.setTag('name', this.video.name)
3ec343a4 517
df98563e
C
518 this.metaService.setTag('og:description', this.video.description)
519 this.metaService.setTag('description', this.video.description)
3ec343a4 520
d38309c3 521 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 522
df98563e 523 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 524
df98563e 525 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 526
df98563e
C
527 this.metaService.setTag('og:url', window.location.href)
528 this.metaService.setTag('url', window.location.href)
3ec343a4 529 }
1f3e9fec 530
d4c6a3b9 531 private isAutoplay () {
bf079b7b
C
532 // We'll jump to the thread id, so do not play the video
533 if (this.route.snapshot.params['threadId']) return false
534
535 // Otherwise true by default
d4c6a3b9
C
536 if (!this.user) return true
537
538 // Be sure the autoPlay is set to false
539 return this.user.autoPlayVideo !== false
540 }
09edde40
C
541
542 private flushPlayer () {
543 // Remove player if it exists
544 if (this.player) {
536598cf
C
545 try {
546 this.player.dispose()
547 this.player = undefined
548 } catch (err) {
549 console.error('Cannot dispose player.', err)
550 }
09edde40
C
551 }
552 }
1c8ddbfa 553
3d9a63d3
C
554 private buildPlayerManagerOptions (params: {
555 video: VideoDetails,
556 videoCaptions: VideoCaption[],
557 urlOptions: CustomizationOptions & { playerMode: PlayerMode },
558 user?: AuthUser
559 }) {
560 const { video, videoCaptions, urlOptions, user } = params
561
562 let startTime = timeToInt(urlOptions.startTime) || (video.userHistory ? video.userHistory.currentTime : 0)
563 // If we are at the end of the video, reset the timer
564 if (video.duration - startTime <= 1) startTime = 0
565
566 const playerCaptions = videoCaptions.map(c => ({
567 label: c.language.label,
568 language: c.language.id,
569 src: environment.apiUrl + c.captionPath
570 }))
571
572 const options: PeertubePlayerManagerOptions = {
573 common: {
574 autoplay: this.isAutoplay(),
575
576 playerElement: this.playerElement,
577 onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,
578
579 videoDuration: video.duration,
580 enableHotkeys: true,
581 inactivityTimeout: 2500,
582 poster: video.previewUrl,
583
584 startTime,
585 stopTime: urlOptions.stopTime,
586 controls: urlOptions.controls,
587 muted: urlOptions.muted,
588 loop: urlOptions.loop,
589 subtitle: urlOptions.subtitle,
590
591 peertubeLink: urlOptions.peertubeLink,
592
593 theaterButton: true,
594 captions: videoCaptions.length !== 0,
595
596 videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
597 ? this.videoService.getVideoViewUrl(video.uuid)
598 : null,
599 embedUrl: video.embedUrl,
600
601 language: this.localeId,
602
603 userWatching: user && user.videosHistoryEnabled === true ? {
604 url: this.videoService.getUserWatchingVideoUrl(video.uuid),
605 authorizationHeader: this.authService.getRequestHeaderValue()
606 } : undefined,
607
608 serverUrl: environment.apiUrl,
609
610 videoCaptions: playerCaptions
611 },
612
613 webtorrent: {
614 videoFiles: video.files
615 }
616 }
617
618 let mode: PlayerMode
619
620 if (urlOptions.playerMode) {
621 if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader'
622 else mode = 'webtorrent'
623 } else {
624 if (video.hasHlsPlaylist()) mode = 'p2p-media-loader'
625 else mode = 'webtorrent'
626 }
627
628 if (mode === 'p2p-media-loader') {
629 const hlsPlaylist = video.getHlsPlaylist()
630
631 const p2pMediaLoader = {
632 playlistUrl: hlsPlaylist.playlistUrl,
633 segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
634 redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
635 trackerAnnounce: video.trackerUrls,
636 videoFiles: hlsPlaylist.files
637 } as P2PMediaLoaderOptions
638
639 Object.assign(options, { p2pMediaLoader })
640 }
641
642 return { playerMode: mode, playerOptions: options }
643 }
644
689a4f69
C
645 private pausePlayer () {
646 if (!this.player) return
647
648 this.player.pause()
649 }
941c5eac
C
650
651 private initHotkeys () {
652 this.hotkeys = [
941c5eac
C
653 // These hotkeys are managed by the player
654 new Hotkey('f', e => e, undefined, this.i18n('Enter/exit fullscreen (requires player focus)')),
655 new Hotkey('space', e => e, undefined, this.i18n('Play/Pause the video (requires player focus)')),
656 new Hotkey('m', e => e, undefined, this.i18n('Mute/unmute the video (requires player focus)')),
657
658 new Hotkey('0-9', e => e, undefined, this.i18n('Skip to a percentage of the video: 0 is 0% and 9 is 90% (requires player focus)')),
659
660 new Hotkey('up', e => e, undefined, this.i18n('Increase the volume (requires player focus)')),
661 new Hotkey('down', e => e, undefined, this.i18n('Decrease the volume (requires player focus)')),
662
663 new Hotkey('right', e => e, undefined, this.i18n('Seek the video forward (requires player focus)')),
664 new Hotkey('left', e => e, undefined, this.i18n('Seek the video backward (requires player focus)')),
665
666 new Hotkey('>', e => e, undefined, this.i18n('Increase playback rate (requires player focus)')),
667 new Hotkey('<', e => e, undefined, this.i18n('Decrease playback rate (requires player focus)')),
668
669 new Hotkey('.', e => e, undefined, this.i18n('Navigate in the video frame by frame (requires player focus)'))
670 ]
3d216ea0
C
671
672 if (this.isUserLoggedIn()) {
673 this.hotkeys = this.hotkeys.concat([
674 new Hotkey('shift+l', () => {
675 this.setLike()
676 return false
677 }, undefined, this.i18n('Like the video')),
678
679 new Hotkey('shift+d', () => {
680 this.setDislike()
681 return false
682 }, undefined, this.i18n('Dislike the video')),
683
684 new Hotkey('shift+s', () => {
685 this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe()
686 return false
687 }, undefined, this.i18n('Subscribe to the account'))
688 ])
689 }
690
691 this.hotkeysService.add(this.hotkeys)
941c5eac 692 }
dc8bc31b 693}