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