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