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