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