]>
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' |
0bd78bf3 | 5 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-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' |
72675ebe | 11 | import { UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '../../../../../shared' |
df98563e | 12 | import { AuthService, ConfirmService } from '../../core' |
a51bad1a | 13 | import { RestExtractor, VideoBlacklistService } 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' |
18a6f04c | 36 | import { PluginService } from '@app/core/plugins/plugin.service' |
93cae479 | 37 | import { HooksService } from '@app/core/plugins/hooks.service' |
60c2bc80 | 38 | import { PlatformLocation } from '@angular/common' |
6aa54148 | 39 | import { randomInt } from '@shared/core-utils/miscs/miscs' |
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 | 47 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
d816f3a0 | 48 | private static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video' |
22b59e80 | 49 | |
f36da21e C |
50 | @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent |
51 | @ViewChild('videoShareModal', { static: false }) videoShareModal: VideoShareComponent | |
52 | @ViewChild('videoSupportModal', { static: false }) videoSupportModal: VideoSupportComponent | |
53 | @ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent | |
df98563e | 54 | |
2adfc7ea | 55 | player: any |
0826c92d | 56 | playerElement: HTMLVideoElement |
9a18a625 | 57 | theaterEnabled = false |
154898b0 | 58 | userRating: UserVideoRateType = null |
80958c78 | 59 | descriptionLoading = false |
2de96f4d | 60 | |
2f4c784a C |
61 | video: VideoDetails = null |
62 | videoCaptions: VideoCaption[] = [] | |
63 | ||
e2f01c47 | 64 | playlist: VideoPlaylist = null |
e2f01c47 | 65 | |
2de96f4d C |
66 | completeDescriptionShown = false |
67 | completeVideoDescription: string | |
68 | shortVideoDescription: string | |
9d9597df | 69 | videoHTMLDescription = '' |
e9189001 | 70 | likesBarTooltipText = '' |
73e09f27 | 71 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 72 | remoteServerDown = false |
3d216ea0 | 73 | hotkeys: Hotkey[] = [] |
df98563e | 74 | |
6aa54148 | 75 | private nextVideoUuid = '' |
f0a39880 | 76 | private currentTime: number |
df98563e | 77 | private paramsSub: Subscription |
e2f01c47 | 78 | private queryParamsSub: Subscription |
31b6ddf8 | 79 | private configSub: Subscription |
df98563e C |
80 | |
81 | constructor ( | |
4fd8aa32 | 82 | private elementRef: ElementRef, |
3b492bff | 83 | private changeDetector: ChangeDetectorRef, |
0629423c | 84 | private route: ActivatedRoute, |
92fb909c | 85 | private router: Router, |
d3ef341a | 86 | private videoService: VideoService, |
e2f01c47 | 87 | private playlistService: VideoPlaylistService, |
35bf0c83 | 88 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 89 | private confirmService: ConfirmService, |
3ec343a4 | 90 | private metaService: MetaService, |
7ddd02c9 | 91 | private authService: AuthService, |
0883b324 | 92 | private serverService: ServerService, |
a51bad1a | 93 | private restExtractor: RestExtractor, |
f8b2c1b4 | 94 | private notifier: Notifier, |
18a6f04c | 95 | private pluginService: PluginService, |
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 |
d3ef341a | 105 | ) {} |
dc8bc31b | 106 | |
b2731bff C |
107 | get user () { |
108 | return this.authService.getUser() | |
109 | } | |
110 | ||
18a6f04c | 111 | async ngOnInit () { |
31b6ddf8 C |
112 | this.configSub = this.serverService.configLoaded |
113 | .subscribe(() => { | |
114 | if ( | |
115 | isWebRTCDisabled() || | |
116 | this.serverService.getConfig().tracker.enabled === false || | |
117 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' | |
118 | ) { | |
119 | this.hasAlreadyAcceptedPrivacyConcern = true | |
120 | } | |
121 | }) | |
2b3b76ab | 122 | |
13fc89f4 | 123 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
124 | const videoId = routeParams[ 'videoId' ] |
125 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 126 | |
e2f01c47 C |
127 | const playlistId = routeParams[ 'playlistId' ] |
128 | if (playlistId) this.loadPlaylist(playlistId) | |
129 | }) | |
bf079b7b | 130 | |
e2f01c47 C |
131 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
132 | const videoId = queryParams[ 'videoId' ] | |
133 | if (videoId) this.loadVideo(videoId) | |
df98563e | 134 | }) |
20d21199 | 135 | |
1c8ddbfa | 136 | this.initHotkeys() |
011e1e6b C |
137 | |
138 | this.theaterEnabled = getStoredTheater() | |
18a6f04c | 139 | |
c9e3eeed | 140 | this.hooks.runAction('action:video-watch.init', 'video-watch') |
d1992b93 C |
141 | } |
142 | ||
df98563e | 143 | ngOnDestroy () { |
09edde40 | 144 | this.flushPlayer() |
067e3f84 | 145 | |
13fc89f4 | 146 | // Unsubscribe subscriptions |
e2f01c47 C |
147 | if (this.paramsSub) this.paramsSub.unsubscribe() |
148 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
20d21199 RK |
149 | |
150 | // Unbind hotkeys | |
3d216ea0 | 151 | this.hotkeysService.remove(this.hotkeys) |
dc8bc31b | 152 | } |
98b01bac | 153 | |
df98563e C |
154 | setLike () { |
155 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
156 | |
157 | // Already liked this video | |
158 | if (this.userRating === 'like') this.setRating('none') | |
159 | else this.setRating('like') | |
d38b8281 C |
160 | } |
161 | ||
df98563e C |
162 | setDislike () { |
163 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
164 | |
165 | // Already disliked this video | |
166 | if (this.userRating === 'dislike') this.setRating('none') | |
167 | else this.setRating('dislike') | |
d38b8281 C |
168 | } |
169 | ||
0d3a9be9 C |
170 | getRatePopoverText () { |
171 | if (this.isUserLoggedIn()) return undefined | |
172 | ||
173 | return this.i18n('You need to be connected to rate this content.') | |
174 | } | |
175 | ||
2de96f4d | 176 | showMoreDescription () { |
2de96f4d C |
177 | if (this.completeVideoDescription === undefined) { |
178 | return this.loadCompleteDescription() | |
179 | } | |
180 | ||
181 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 182 | this.completeDescriptionShown = true |
2de96f4d C |
183 | } |
184 | ||
185 | showLessDescription () { | |
2de96f4d | 186 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 187 | this.completeDescriptionShown = false |
2de96f4d C |
188 | } |
189 | ||
190 | loadCompleteDescription () { | |
80958c78 C |
191 | this.descriptionLoading = true |
192 | ||
2de96f4d | 193 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
194 | .subscribe( |
195 | description => { | |
196 | this.completeDescriptionShown = true | |
197 | this.descriptionLoading = false | |
198 | ||
199 | this.shortVideoDescription = this.video.description | |
200 | this.completeVideoDescription = description | |
201 | ||
202 | this.updateVideoDescription(this.completeVideoDescription) | |
203 | }, | |
204 | ||
205 | error => { | |
206 | this.descriptionLoading = false | |
f8b2c1b4 | 207 | this.notifier.error(error.message) |
2186386c C |
208 | } |
209 | ) | |
2de96f4d C |
210 | } |
211 | ||
07fa4c97 | 212 | showSupportModal () { |
689a4f69 C |
213 | this.pausePlayer() |
214 | ||
07fa4c97 C |
215 | this.videoSupportModal.show() |
216 | } | |
217 | ||
df98563e | 218 | showShareModal () { |
689a4f69 C |
219 | this.pausePlayer() |
220 | ||
f0a39880 | 221 | this.videoShareModal.show(this.currentTime) |
99cc4f49 C |
222 | } |
223 | ||
df98563e C |
224 | isUserLoggedIn () { |
225 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
226 | } |
227 | ||
b1fa3eba C |
228 | getVideoTags () { |
229 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
230 | ||
4278710d | 231 | return this.video.tags |
b1fa3eba C |
232 | } |
233 | ||
6aa54148 L |
234 | onRecommendations (videos: Video[]) { |
235 | if (videos.length > 0) { | |
236 | // Pick a random video until the recommendations are improved | |
237 | this.nextVideoUuid = videos[randomInt(0,videos.length - 1)].uuid | |
238 | } | |
239 | } | |
240 | ||
689a4f69 C |
241 | onModalOpened () { |
242 | this.pausePlayer() | |
243 | } | |
244 | ||
3a0fb65c C |
245 | onVideoRemoved () { |
246 | this.redirectService.redirectToHomepage() | |
6725d05c C |
247 | } |
248 | ||
73e09f27 | 249 | acceptedPrivacyConcern () { |
0bd78bf3 | 250 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
251 | this.hasAlreadyAcceptedPrivacyConcern = true |
252 | } | |
253 | ||
2186386c C |
254 | isVideoToTranscode () { |
255 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
256 | } | |
257 | ||
516df59b C |
258 | isVideoToImport () { |
259 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
260 | } | |
261 | ||
bbe0f064 C |
262 | hasVideoScheduledPublication () { |
263 | return this.video && this.video.scheduledUpdate !== undefined | |
264 | } | |
265 | ||
e2f01c47 C |
266 | isVideoBlur (video: Video) { |
267 | return video.isVideoNSFWForUser(this.user, this.serverService.getConfig()) | |
268 | } | |
269 | ||
e2f01c47 C |
270 | private loadVideo (videoId: string) { |
271 | // Video did not change | |
272 | if (this.video && this.video.uuid === videoId) return | |
273 | ||
274 | if (this.player) this.player.pause() | |
275 | ||
93cae479 C |
276 | const videoObs = this.hooks.wrapObsFun( |
277 | this.videoService.getVideo.bind(this.videoService), | |
278 | { videoId }, | |
279 | 'video-watch', | |
280 | 'filter:api.video-watch.video.get.params', | |
281 | 'filter:api.video-watch.video.get.result' | |
282 | ) | |
283 | ||
e2f01c47 | 284 | // Video did change |
c8861d5d | 285 | forkJoin([ |
93cae479 | 286 | videoObs, |
e2f01c47 | 287 | this.videoCaptionService.listCaptions(videoId) |
c8861d5d | 288 | ]) |
e2f01c47 C |
289 | .pipe( |
290 | // If 401, the video is private or blacklisted so redirect to 404 | |
291 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
292 | ) | |
293 | .subscribe(([ video, captionsResult ]) => { | |
294 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 295 | |
4c72c1cd C |
296 | const urlOptions = { |
297 | startTime: queryParams.start, | |
298 | stopTime: queryParams.stop, | |
5efab546 C |
299 | |
300 | muted: queryParams.muted, | |
301 | loop: queryParams.loop, | |
4c72c1cd | 302 | subtitle: queryParams.subtitle, |
5efab546 C |
303 | |
304 | playerMode: queryParams.mode, | |
305 | peertubeLink: false | |
4c72c1cd C |
306 | } |
307 | ||
308 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
309 | .catch(err => this.handleError(err)) |
310 | }) | |
311 | } | |
312 | ||
313 | private loadPlaylist (playlistId: string) { | |
314 | // Playlist did not change | |
315 | if (this.playlist && this.playlist.uuid === playlistId) return | |
316 | ||
317 | this.playlistService.getVideoPlaylist(playlistId) | |
318 | .pipe( | |
319 | // If 401, the video is private or blacklisted so redirect to 404 | |
320 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
321 | ) | |
322 | .subscribe(playlist => { | |
323 | this.playlist = playlist | |
324 | ||
325 | const videoId = this.route.snapshot.queryParams['videoId'] | |
72675ebe | 326 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !videoId) |
e2f01c47 C |
327 | }) |
328 | } | |
329 | ||
2de96f4d C |
330 | private updateVideoDescription (description: string) { |
331 | this.video.description = description | |
332 | this.setVideoDescriptionHTML() | |
4c72c1cd | 333 | .catch(err => console.error(err)) |
2de96f4d C |
334 | } |
335 | ||
41d71344 C |
336 | private async setVideoDescriptionHTML () { |
337 | this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description) | |
2de96f4d C |
338 | } |
339 | ||
e9189001 | 340 | private setVideoLikesBarTooltipText () { |
2186386c C |
341 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
342 | likesNumber: this.video.likes, | |
343 | dislikesNumber: this.video.dislikes | |
344 | }) | |
e9189001 C |
345 | } |
346 | ||
0c31c33d C |
347 | private handleError (err: any) { |
348 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
349 | if (!errorMessage) return |
350 | ||
6d88de72 | 351 | // Display a message in the video player instead of a notification |
0f7fedc3 | 352 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
353 | this.flushPlayer() |
354 | this.remoteServerDown = true | |
3b492bff C |
355 | this.changeDetector.detectChanges() |
356 | ||
6d88de72 | 357 | return |
0c31c33d C |
358 | } |
359 | ||
f8b2c1b4 | 360 | this.notifier.error(errorMessage) |
0c31c33d C |
361 | } |
362 | ||
df98563e | 363 | private checkUserRating () { |
d38b8281 | 364 | // Unlogged users do not have ratings |
df98563e | 365 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
366 | |
367 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
368 | .subscribe( |
369 | ratingObject => { | |
370 | if (ratingObject) { | |
371 | this.userRating = ratingObject.rating | |
372 | } | |
373 | }, | |
374 | ||
f8b2c1b4 | 375 | err => this.notifier.error(err.message) |
2186386c | 376 | ) |
d38b8281 C |
377 | } |
378 | ||
597a9266 C |
379 | private async onVideoFetched ( |
380 | video: VideoDetails, | |
381 | videoCaptions: VideoCaption[], | |
5efab546 | 382 | urlOptions: CustomizationOptions & { playerMode: PlayerMode } |
597a9266 | 383 | ) { |
df98563e | 384 | this.video = video |
2f4c784a | 385 | this.videoCaptions = videoCaptions |
92fb909c | 386 | |
c448d412 C |
387 | // Re init attributes |
388 | this.descriptionLoading = false | |
389 | this.completeDescriptionShown = false | |
6d88de72 | 390 | this.remoteServerDown = false |
f0a39880 | 391 | this.currentTime = undefined |
c448d412 | 392 | |
72675ebe | 393 | this.videoWatchPlaylist.updatePlaylistIndex(video) |
e2f01c47 | 394 | |
e2f01c47 | 395 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 396 | const res = await this.confirmService.confirm( |
989e526a C |
397 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
398 | this.i18n('Mature or explicit content') | |
d6e32a2e | 399 | ) |
60c2bc80 | 400 | if (res === false) return this.location.back() |
92fb909c C |
401 | } |
402 | ||
09edde40 C |
403 | // Flush old player if needed |
404 | this.flushPlayer() | |
b891f9bc | 405 | |
60c2bc80 | 406 | // Build video element, because videojs removes it on dispose |
e2f01c47 | 407 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
408 | this.playerElement = document.createElement('video') |
409 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 410 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
411 | playerElementWrapper.appendChild(this.playerElement) |
412 | ||
3d9a63d3 C |
413 | const params = { |
414 | video: this.video, | |
415 | videoCaptions, | |
416 | urlOptions, | |
417 | user: this.user | |
e945b184 | 418 | } |
3d9a63d3 C |
419 | const { playerMode, playerOptions } = await this.hooks.wrapFun( |
420 | this.buildPlayerManagerOptions.bind(this), | |
421 | params, | |
c2023a9f C |
422 | 'video-watch', |
423 | 'filter:internal.video-watch.player.build-options.params', | |
3d9a63d3 C |
424 | 'filter:internal.video-watch.player.build-options.result' |
425 | ) | |
e945b184 | 426 | |
e945b184 | 427 | this.zone.runOutsideAngular(async () => { |
3d9a63d3 | 428 | this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player) |
d275e754 | 429 | this.player.focus() |
9a18a625 | 430 | |
2adfc7ea | 431 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
432 | |
433 | this.player.on('timeupdate', () => { | |
434 | this.currentTime = Math.floor(this.player.currentTime()) | |
435 | }) | |
e2f01c47 C |
436 | |
437 | this.player.one('ended', () => { | |
438 | if (this.playlist) { | |
72675ebe | 439 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
d816f3a0 RK |
440 | } else if ( |
441 | this.user && this.user.autoPlayNextVideo || | |
442 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' | |
443 | ) { | |
6aa54148 | 444 | this.zone.run(() => this.autoplayNext()) |
e2f01c47 C |
445 | } |
446 | }) | |
447 | ||
448 | this.player.one('stopped', () => { | |
449 | if (this.playlist) { | |
72675ebe | 450 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
451 | } |
452 | }) | |
9a18a625 C |
453 | |
454 | this.player.on('theaterChange', (_: any, enabled: boolean) => { | |
455 | this.zone.run(() => this.theaterEnabled = enabled) | |
456 | }) | |
5f85f8aa RK |
457 | |
458 | this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player }) | |
b891f9bc | 459 | }) |
22b59e80 C |
460 | |
461 | this.setVideoDescriptionHTML() | |
462 | this.setVideoLikesBarTooltipText() | |
463 | ||
464 | this.setOpenGraphTags() | |
465 | this.checkUserRating() | |
93cae479 | 466 | |
5f85f8aa | 467 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', { videojs }) |
92fb909c C |
468 | } |
469 | ||
6aa54148 L |
470 | private autoplayNext () { |
471 | if (this.nextVideoUuid) { | |
472 | this.router.navigate([ '/videos/watch', this.nextVideoUuid ]) | |
473 | } | |
474 | } | |
475 | ||
5c6d985f | 476 | private setRating (nextRating: UserVideoRateType) { |
4c72c1cd C |
477 | const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = { |
478 | like: this.videoService.setVideoLike, | |
479 | dislike: this.videoService.setVideoDislike, | |
480 | none: this.videoService.unsetVideoLike | |
57a49263 BB |
481 | } |
482 | ||
4c72c1cd | 483 | ratingMethods[nextRating].call(this.videoService, this.video.id) |
2186386c C |
484 | .subscribe( |
485 | () => { | |
486 | // Update the video like attribute | |
487 | this.updateVideoRating(this.userRating, nextRating) | |
488 | this.userRating = nextRating | |
489 | }, | |
490 | ||
f8b2c1b4 | 491 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 492 | ) |
57a49263 BB |
493 | } |
494 | ||
5c6d985f | 495 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
496 | let likesToIncrement = 0 |
497 | let dislikesToIncrement = 0 | |
d38b8281 C |
498 | |
499 | if (oldRating) { | |
df98563e C |
500 | if (oldRating === 'like') likesToIncrement-- |
501 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
502 | } |
503 | ||
df98563e C |
504 | if (newRating === 'like') likesToIncrement++ |
505 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 506 | |
df98563e C |
507 | this.video.likes += likesToIncrement |
508 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 509 | |
22b59e80 | 510 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 511 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
512 | } |
513 | ||
df98563e C |
514 | private setOpenGraphTags () { |
515 | this.metaService.setTitle(this.video.name) | |
758b996d | 516 | |
df98563e | 517 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 518 | |
df98563e C |
519 | this.metaService.setTag('og:title', this.video.name) |
520 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 521 | |
df98563e C |
522 | this.metaService.setTag('og:description', this.video.description) |
523 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 524 | |
d38309c3 | 525 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 526 | |
df98563e | 527 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 528 | |
df98563e | 529 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 530 | |
df98563e C |
531 | this.metaService.setTag('og:url', window.location.href) |
532 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 533 | } |
1f3e9fec | 534 | |
d4c6a3b9 | 535 | private isAutoplay () { |
bf079b7b C |
536 | // We'll jump to the thread id, so do not play the video |
537 | if (this.route.snapshot.params['threadId']) return false | |
538 | ||
539 | // Otherwise true by default | |
d4c6a3b9 C |
540 | if (!this.user) return true |
541 | ||
542 | // Be sure the autoPlay is set to false | |
543 | return this.user.autoPlayVideo !== false | |
544 | } | |
09edde40 C |
545 | |
546 | private flushPlayer () { | |
547 | // Remove player if it exists | |
548 | if (this.player) { | |
536598cf C |
549 | try { |
550 | this.player.dispose() | |
551 | this.player = undefined | |
552 | } catch (err) { | |
553 | console.error('Cannot dispose player.', err) | |
554 | } | |
09edde40 C |
555 | } |
556 | } | |
1c8ddbfa | 557 | |
3d9a63d3 C |
558 | private buildPlayerManagerOptions (params: { |
559 | video: VideoDetails, | |
560 | videoCaptions: VideoCaption[], | |
561 | urlOptions: CustomizationOptions & { playerMode: PlayerMode }, | |
562 | user?: AuthUser | |
563 | }) { | |
564 | const { video, videoCaptions, urlOptions, user } = params | |
565 | ||
566 | let startTime = timeToInt(urlOptions.startTime) || (video.userHistory ? video.userHistory.currentTime : 0) | |
567 | // If we are at the end of the video, reset the timer | |
568 | if (video.duration - startTime <= 1) startTime = 0 | |
569 | ||
570 | const playerCaptions = videoCaptions.map(c => ({ | |
571 | label: c.language.label, | |
572 | language: c.language.id, | |
573 | src: environment.apiUrl + c.captionPath | |
574 | })) | |
575 | ||
576 | const options: PeertubePlayerManagerOptions = { | |
577 | common: { | |
578 | autoplay: this.isAutoplay(), | |
579 | ||
580 | playerElement: this.playerElement, | |
581 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
582 | ||
583 | videoDuration: video.duration, | |
584 | enableHotkeys: true, | |
585 | inactivityTimeout: 2500, | |
586 | poster: video.previewUrl, | |
587 | ||
588 | startTime, | |
589 | stopTime: urlOptions.stopTime, | |
590 | controls: urlOptions.controls, | |
591 | muted: urlOptions.muted, | |
592 | loop: urlOptions.loop, | |
593 | subtitle: urlOptions.subtitle, | |
594 | ||
595 | peertubeLink: urlOptions.peertubeLink, | |
596 | ||
597 | theaterButton: true, | |
598 | captions: videoCaptions.length !== 0, | |
599 | ||
600 | videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE | |
601 | ? this.videoService.getVideoViewUrl(video.uuid) | |
602 | : null, | |
603 | embedUrl: video.embedUrl, | |
604 | ||
605 | language: this.localeId, | |
606 | ||
607 | userWatching: user && user.videosHistoryEnabled === true ? { | |
608 | url: this.videoService.getUserWatchingVideoUrl(video.uuid), | |
609 | authorizationHeader: this.authService.getRequestHeaderValue() | |
610 | } : undefined, | |
611 | ||
612 | serverUrl: environment.apiUrl, | |
613 | ||
614 | videoCaptions: playerCaptions | |
615 | }, | |
616 | ||
617 | webtorrent: { | |
618 | videoFiles: video.files | |
619 | } | |
620 | } | |
621 | ||
622 | let mode: PlayerMode | |
623 | ||
624 | if (urlOptions.playerMode) { | |
625 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
626 | else mode = 'webtorrent' | |
627 | } else { | |
628 | if (video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
629 | else mode = 'webtorrent' | |
630 | } | |
631 | ||
632 | if (mode === 'p2p-media-loader') { | |
633 | const hlsPlaylist = video.getHlsPlaylist() | |
634 | ||
635 | const p2pMediaLoader = { | |
636 | playlistUrl: hlsPlaylist.playlistUrl, | |
637 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
638 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
639 | trackerAnnounce: video.trackerUrls, | |
640 | videoFiles: hlsPlaylist.files | |
641 | } as P2PMediaLoaderOptions | |
642 | ||
643 | Object.assign(options, { p2pMediaLoader }) | |
644 | } | |
645 | ||
646 | return { playerMode: mode, playerOptions: options } | |
647 | } | |
648 | ||
689a4f69 C |
649 | private pausePlayer () { |
650 | if (!this.player) return | |
651 | ||
652 | this.player.pause() | |
653 | } | |
941c5eac C |
654 | |
655 | private initHotkeys () { | |
656 | this.hotkeys = [ | |
941c5eac C |
657 | // These hotkeys are managed by the player |
658 | new Hotkey('f', e => e, undefined, this.i18n('Enter/exit fullscreen (requires player focus)')), | |
659 | new Hotkey('space', e => e, undefined, this.i18n('Play/Pause the video (requires player focus)')), | |
660 | new Hotkey('m', e => e, undefined, this.i18n('Mute/unmute the video (requires player focus)')), | |
661 | ||
662 | 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)')), | |
663 | ||
664 | new Hotkey('up', e => e, undefined, this.i18n('Increase the volume (requires player focus)')), | |
665 | new Hotkey('down', e => e, undefined, this.i18n('Decrease the volume (requires player focus)')), | |
666 | ||
667 | new Hotkey('right', e => e, undefined, this.i18n('Seek the video forward (requires player focus)')), | |
668 | new Hotkey('left', e => e, undefined, this.i18n('Seek the video backward (requires player focus)')), | |
669 | ||
670 | new Hotkey('>', e => e, undefined, this.i18n('Increase playback rate (requires player focus)')), | |
671 | new Hotkey('<', e => e, undefined, this.i18n('Decrease playback rate (requires player focus)')), | |
672 | ||
673 | new Hotkey('.', e => e, undefined, this.i18n('Navigate in the video frame by frame (requires player focus)')) | |
674 | ] | |
3d216ea0 C |
675 | |
676 | if (this.isUserLoggedIn()) { | |
677 | this.hotkeys = this.hotkeys.concat([ | |
678 | new Hotkey('shift+l', () => { | |
679 | this.setLike() | |
680 | return false | |
681 | }, undefined, this.i18n('Like the video')), | |
682 | ||
683 | new Hotkey('shift+d', () => { | |
684 | this.setDislike() | |
685 | return false | |
686 | }, undefined, this.i18n('Dislike the video')), | |
687 | ||
688 | new Hotkey('shift+s', () => { | |
689 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
690 | return false | |
691 | }, undefined, this.i18n('Subscribe to the account')) | |
692 | ]) | |
693 | } | |
694 | ||
695 | this.hotkeysService.add(this.hotkeys) | |
941c5eac | 696 | } |
dc8bc31b | 697 | } |