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