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