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