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