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