]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
6ea59f41 | 2 | import { forkJoin, Subscription } from 'rxjs' |
e972e046 | 3 | import { catchError } from 'rxjs/operators' |
67ed6552 | 4 | import { PlatformLocation } from '@angular/common' |
3b492bff | 5 | import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
df98563e | 6 | import { ActivatedRoute, Router } from '@angular/router' |
a5cf76af C |
7 | import { |
8 | AuthService, | |
9 | AuthUser, | |
10 | ConfirmService, | |
0f01a8ba | 11 | MetaService, |
a5cf76af C |
12 | Notifier, |
13 | PeerTubeSocket, | |
72f611ca | 14 | PluginService, |
a5cf76af | 15 | RestExtractor, |
2666fd7c | 16 | ScreenService, |
a5cf76af C |
17 | ServerService, |
18 | UserService | |
19 | } from '@app/core' | |
67ed6552 | 20 | import { HooksService } from '@app/core/plugins/hooks.service' |
4504f09f | 21 | import { isXPercentInViewport, scrollToTop } from '@app/helpers' |
67ed6552 C |
22 | import { Video, VideoCaptionService, VideoDetails, VideoService } from '@app/shared/shared-main' |
23 | import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription' | |
06a55579 | 24 | import { VideoActionsDisplayType } from '@app/shared/shared-video-miniature' |
67ed6552 | 25 | import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
a800dbf3 | 26 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
06a55579 | 27 | import { HTMLServerConfig, PeerTubeProblemDocument, ServerErrorCode, VideoCaption, VideoPrivacy, VideoState } from '@shared/models' |
6ebdd12f | 28 | import { cleanupVideoWatch, getStoredTheater, getStoredVideoWatchHistory } from '../../../assets/player/peertube-player-local-storage' |
6ec0b75b | 29 | import { |
5efab546 | 30 | CustomizationOptions, |
6ec0b75b C |
31 | P2PMediaLoaderOptions, |
32 | PeertubePlayerManager, | |
33 | PeertubePlayerManagerOptions, | |
67ed6552 C |
34 | PlayerMode, |
35 | videojs | |
6ec0b75b | 36 | } from '../../../assets/player/peertube-player-manager' |
6ebdd12f | 37 | import { timeToInt } from '../../../assets/player/utils' |
67ed6552 | 38 | import { environment } from '../../../environments/environment' |
911186da | 39 | import { VideoWatchPlaylistComponent } from './shared' |
dc8bc31b | 40 | |
a5cf76af C |
41 | type URLOptions = CustomizationOptions & { playerMode: PlayerMode } |
42 | ||
dc8bc31b C |
43 | @Component({ |
44 | selector: 'my-video-watch', | |
ec8d8440 C |
45 | templateUrl: './video-watch.component.html', |
46 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 47 | }) |
0629423c | 48 | export class VideoWatchComponent implements OnInit, OnDestroy { |
f36da21e | 49 | @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent |
2f5d2ec5 | 50 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
df98563e | 51 | |
2adfc7ea | 52 | player: any |
0826c92d | 53 | playerElement: HTMLVideoElement |
c15d61f5 | 54 | |
9a18a625 | 55 | theaterEnabled = false |
c15d61f5 | 56 | |
c15d61f5 | 57 | playerPlaceholderImgSrc: string |
2de96f4d | 58 | |
2f4c784a C |
59 | video: VideoDetails = null |
60 | videoCaptions: VideoCaption[] = [] | |
61 | ||
d142c7b9 | 62 | playlistPosition: number |
e2f01c47 | 63 | playlist: VideoPlaylist = null |
e2f01c47 | 64 | |
e9189001 | 65 | likesBarTooltipText = '' |
c15d61f5 | 66 | |
6d88de72 | 67 | remoteServerDown = false |
c15d61f5 | 68 | |
94dfca3e RK |
69 | tooltipSupport = '' |
70 | tooltipSaveToPlaylist = '' | |
71 | ||
f8c00564 C |
72 | videoActionsOptions: VideoActionsDisplayType = { |
73 | playlist: false, | |
74 | download: true, | |
75 | update: true, | |
76 | blacklist: true, | |
77 | delete: true, | |
78 | report: true, | |
79 | duplicate: true, | |
80 | mute: true, | |
81 | liveInfo: true | |
82 | } | |
83 | ||
6aa54148 | 84 | private nextVideoUuid = '' |
3bcb4fd7 | 85 | private nextVideoTitle = '' |
f0a39880 | 86 | private currentTime: number |
df98563e | 87 | private paramsSub: Subscription |
e2f01c47 | 88 | private queryParamsSub: Subscription |
31b6ddf8 | 89 | private configSub: Subscription |
a5cf76af | 90 | private liveVideosSub: Subscription |
df98563e | 91 | |
2989628b | 92 | private serverConfig: HTMLServerConfig |
ba430d75 | 93 | |
6ea59f41 C |
94 | private hotkeys: Hotkey[] = [] |
95 | ||
df98563e | 96 | constructor ( |
4fd8aa32 | 97 | private elementRef: ElementRef, |
3b492bff | 98 | private changeDetector: ChangeDetectorRef, |
0629423c | 99 | private route: ActivatedRoute, |
92fb909c | 100 | private router: Router, |
d3ef341a | 101 | private videoService: VideoService, |
e2f01c47 | 102 | private playlistService: VideoPlaylistService, |
92fb909c | 103 | private confirmService: ConfirmService, |
3ec343a4 | 104 | private metaService: MetaService, |
7ddd02c9 | 105 | private authService: AuthService, |
d3217560 | 106 | private userService: UserService, |
0883b324 | 107 | private serverService: ServerService, |
a51bad1a | 108 | private restExtractor: RestExtractor, |
f8b2c1b4 | 109 | private notifier: Notifier, |
901637bb | 110 | private zone: NgZone, |
16f7022b | 111 | private videoCaptionService: VideoCaptionService, |
20d21199 | 112 | private hotkeysService: HotkeysService, |
93cae479 | 113 | private hooks: HooksService, |
72f611ca | 114 | private pluginService: PluginService, |
a5cf76af | 115 | private peertubeSocket: PeerTubeSocket, |
2666fd7c | 116 | private screenService: ScreenService, |
60c2bc80 | 117 | private location: PlatformLocation, |
e945b184 | 118 | @Inject(LOCALE_ID) private localeId: string |
2666fd7c | 119 | ) { } |
dc8bc31b | 120 | |
b2731bff C |
121 | get user () { |
122 | return this.authService.getUser() | |
123 | } | |
124 | ||
d3217560 RK |
125 | get anonymousUser () { |
126 | return this.userService.getAnonymousUser() | |
127 | } | |
128 | ||
18a6f04c | 129 | async ngOnInit () { |
6ebdd12f C |
130 | this.serverConfig = this.serverService.getHTMLConfig() |
131 | ||
2666fd7c C |
132 | // Hide the tooltips for unlogged users in mobile view, this adds confusion with the popover |
133 | if (this.user || !this.screenService.isInMobileView()) { | |
2666fd7c C |
134 | this.tooltipSupport = $localize`Support options for this video` |
135 | this.tooltipSaveToPlaylist = $localize`Save to playlist` | |
136 | } | |
137 | ||
1a568b6f C |
138 | PeertubePlayerManager.initState() |
139 | ||
13fc89f4 | 140 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
141 | const videoId = routeParams[ 'videoId' ] |
142 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 143 | |
e2f01c47 C |
144 | const playlistId = routeParams[ 'playlistId' ] |
145 | if (playlistId) this.loadPlaylist(playlistId) | |
146 | }) | |
bf079b7b | 147 | |
d142c7b9 | 148 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
e771e82d | 149 | // Handle the ?playlistPosition |
e1a5ad70 | 150 | const positionParam = queryParams[ 'playlistPosition' ] ?? 1 |
e771e82d FC |
151 | |
152 | this.playlistPosition = positionParam === 'last' | |
153 | ? -1 // Handle the "last" index | |
e1a5ad70 | 154 | : parseInt(positionParam + '', 10) |
e771e82d FC |
155 | |
156 | if (isNaN(this.playlistPosition)) { | |
157 | console.error(`playlistPosition query param '${positionParam}' was parsed as NaN, defaulting to 1.`) | |
158 | this.playlistPosition = 1 | |
159 | } | |
160 | ||
d142c7b9 | 161 | this.videoWatchPlaylist.updatePlaylistIndex(this.playlistPosition) |
b29bf61d RK |
162 | |
163 | const start = queryParams[ 'start' ] | |
164 | if (this.player && start) this.player.currentTime(parseInt(start, 10)) | |
df98563e | 165 | }) |
20d21199 | 166 | |
1c8ddbfa | 167 | this.initHotkeys() |
011e1e6b C |
168 | |
169 | this.theaterEnabled = getStoredTheater() | |
18a6f04c | 170 | |
c9e3eeed | 171 | this.hooks.runAction('action:video-watch.init', 'video-watch') |
58b9ce30 | 172 | |
173 | setTimeout(cleanupVideoWatch, 1500) // Run in timeout to ensure we're not blocking the UI | |
d1992b93 C |
174 | } |
175 | ||
df98563e | 176 | ngOnDestroy () { |
09edde40 | 177 | this.flushPlayer() |
067e3f84 | 178 | |
13fc89f4 | 179 | // Unsubscribe subscriptions |
e2f01c47 C |
180 | if (this.paramsSub) this.paramsSub.unsubscribe() |
181 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
5abc96fc | 182 | if (this.configSub) this.configSub.unsubscribe() |
a5cf76af | 183 | if (this.liveVideosSub) this.liveVideosSub.unsubscribe() |
20d21199 RK |
184 | |
185 | // Unbind hotkeys | |
3d216ea0 | 186 | this.hotkeysService.remove(this.hotkeys) |
dc8bc31b | 187 | } |
98b01bac | 188 | |
06a55579 C |
189 | getCurrentTime () { |
190 | return this.currentTime | |
6863f814 RK |
191 | } |
192 | ||
06a55579 C |
193 | getCurrentPlaylistPosition () { |
194 | return this.videoWatchPlaylist.currentPlaylistPosition | |
99cc4f49 C |
195 | } |
196 | ||
df98563e C |
197 | isUserLoggedIn () { |
198 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
199 | } |
200 | ||
de779034 RK |
201 | getVideoUrl () { |
202 | if (!this.video.url) { | |
d4a8e7a6 | 203 | return this.video.originInstanceUrl + VideoDetails.buildWatchUrl(this.video) |
de779034 RK |
204 | } |
205 | return this.video.url | |
206 | } | |
207 | ||
b1fa3eba C |
208 | getVideoTags () { |
209 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
210 | ||
4278710d | 211 | return this.video.tags |
b1fa3eba C |
212 | } |
213 | ||
6aa54148 L |
214 | onRecommendations (videos: Video[]) { |
215 | if (videos.length > 0) { | |
3bcb4fd7 RK |
216 | // The recommended videos's first element should be the next video |
217 | const video = videos[0] | |
218 | this.nextVideoUuid = video.uuid | |
219 | this.nextVideoTitle = video.name | |
6aa54148 L |
220 | } |
221 | } | |
222 | ||
2186386c C |
223 | isVideoToTranscode () { |
224 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
225 | } | |
226 | ||
516df59b C |
227 | isVideoToImport () { |
228 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
229 | } | |
230 | ||
bbe0f064 C |
231 | hasVideoScheduledPublication () { |
232 | return this.video && this.video.scheduledUpdate !== undefined | |
233 | } | |
234 | ||
a5cf76af C |
235 | isWaitingForLive () { |
236 | return this.video?.state.id === VideoState.WAITING_FOR_LIVE | |
237 | } | |
238 | ||
239 | isLiveEnded () { | |
240 | return this.video?.state.id === VideoState.LIVE_ENDED | |
241 | } | |
242 | ||
e2f01c47 | 243 | isVideoBlur (video: Video) { |
ba430d75 | 244 | return video.isVideoNSFWForUser(this.user, this.serverConfig) |
e2f01c47 C |
245 | } |
246 | ||
706c5a47 RK |
247 | isAutoPlayEnabled () { |
248 | return ( | |
7c93905d | 249 | (this.user && this.user.autoPlayNextVideo) || |
d3217560 | 250 | this.anonymousUser.autoPlayNextVideo |
706c5a47 | 251 | ) |
b29bf61d RK |
252 | } |
253 | ||
254 | handleTimestampClicked (timestamp: number) { | |
18429d01 C |
255 | if (!this.player || this.video.isLive) return |
256 | ||
257 | this.player.currentTime(timestamp) | |
b29bf61d | 258 | scrollToTop() |
706c5a47 RK |
259 | } |
260 | ||
261 | isPlaylistAutoPlayEnabled () { | |
262 | return ( | |
7c93905d | 263 | (this.user && this.user.autoPlayNextVideoPlaylist) || |
d3217560 | 264 | this.anonymousUser.autoPlayNextVideoPlaylist |
706c5a47 RK |
265 | ) |
266 | } | |
267 | ||
b40a2193 K |
268 | isChannelDisplayNameGeneric () { |
269 | const genericChannelDisplayName = [ | |
270 | `Main ${this.video.channel.ownerAccount.name} channel`, | |
271 | `Default ${this.video.channel.ownerAccount.name} channel` | |
272 | ] | |
273 | ||
274 | return genericChannelDisplayName.includes(this.video.channel.displayName) | |
275 | } | |
276 | ||
d142c7b9 C |
277 | onPlaylistVideoFound (videoId: string) { |
278 | this.loadVideo(videoId) | |
279 | } | |
280 | ||
0f7407d9 C |
281 | displayOtherVideosAsRow () { |
282 | // Use the same value as in the SASS file | |
283 | return this.screenService.getWindowInnerWidth() <= 1100 | |
284 | } | |
285 | ||
e2f01c47 C |
286 | private loadVideo (videoId: string) { |
287 | // Video did not change | |
d4a8e7a6 C |
288 | if ( |
289 | this.video && | |
290 | (this.video.uuid === videoId || this.video.shortUUID === videoId) | |
291 | ) return | |
e2f01c47 C |
292 | |
293 | if (this.player) this.player.pause() | |
294 | ||
93cae479 C |
295 | const videoObs = this.hooks.wrapObsFun( |
296 | this.videoService.getVideo.bind(this.videoService), | |
297 | { videoId }, | |
298 | 'video-watch', | |
299 | 'filter:api.video-watch.video.get.params', | |
300 | 'filter:api.video-watch.video.get.result' | |
301 | ) | |
302 | ||
e2f01c47 | 303 | // Video did change |
c8861d5d | 304 | forkJoin([ |
93cae479 | 305 | videoObs, |
e2f01c47 | 306 | this.videoCaptionService.listCaptions(videoId) |
c8861d5d | 307 | ]) |
e2f01c47 | 308 | .pipe( |
ab398a05 | 309 | // If 400, 403 or 404, the video is private or blocked so redirect to 404 |
e6abf95e | 310 | catchError(err => { |
e030bfb5 C |
311 | const errorBody = err.body as PeerTubeProblemDocument |
312 | ||
313 | if (errorBody.code === ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS && errorBody.originUrl) { | |
e6abf95e | 314 | const search = window.location.search |
e030bfb5 | 315 | let originUrl = errorBody.originUrl |
e6abf95e C |
316 | if (search) originUrl += search |
317 | ||
318 | this.confirmService.confirm( | |
319 | $localize`This video is not available on this instance. Do you want to be redirected on the origin instance: <a href="${originUrl}">${originUrl}</a>?`, | |
320 | $localize`Redirection` | |
321 | ).then(res => { | |
f2eb23cd | 322 | if (res === false) { |
ab398a05 | 323 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 324 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
325 | HttpStatusCode.FORBIDDEN_403, |
326 | HttpStatusCode.NOT_FOUND_404 | |
327 | ]) | |
328 | } | |
e6abf95e C |
329 | |
330 | return window.location.href = originUrl | |
331 | }) | |
332 | } | |
333 | ||
ab398a05 | 334 | return this.restExtractor.redirectTo404IfNotFound(err, 'video', [ |
f2eb23cd | 335 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
336 | HttpStatusCode.FORBIDDEN_403, |
337 | HttpStatusCode.NOT_FOUND_404 | |
338 | ]) | |
e6abf95e | 339 | }) |
e2f01c47 C |
340 | ) |
341 | .subscribe(([ video, captionsResult ]) => { | |
342 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 343 | |
4c72c1cd | 344 | const urlOptions = { |
3c6a44a1 C |
345 | resume: queryParams.resume, |
346 | ||
4c72c1cd C |
347 | startTime: queryParams.start, |
348 | stopTime: queryParams.stop, | |
5efab546 C |
349 | |
350 | muted: queryParams.muted, | |
351 | loop: queryParams.loop, | |
4c72c1cd | 352 | subtitle: queryParams.subtitle, |
5efab546 C |
353 | |
354 | playerMode: queryParams.mode, | |
355 | peertubeLink: false | |
4c72c1cd C |
356 | } |
357 | ||
358 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
359 | .catch(err => this.handleError(err)) |
360 | }) | |
361 | } | |
362 | ||
363 | private loadPlaylist (playlistId: string) { | |
364 | // Playlist did not change | |
d4a8e7a6 C |
365 | if ( |
366 | this.playlist && | |
367 | (this.playlist.uuid === playlistId || this.playlist.shortUUID === playlistId) | |
368 | ) return | |
e2f01c47 C |
369 | |
370 | this.playlistService.getVideoPlaylist(playlistId) | |
371 | .pipe( | |
ab398a05 RK |
372 | // If 400 or 403, the video is private or blocked so redirect to 404 |
373 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'video', [ | |
f2eb23cd | 374 | HttpStatusCode.BAD_REQUEST_400, |
f2eb23cd RK |
375 | HttpStatusCode.FORBIDDEN_403, |
376 | HttpStatusCode.NOT_FOUND_404 | |
377 | ])) | |
e2f01c47 C |
378 | ) |
379 | .subscribe(playlist => { | |
380 | this.playlist = playlist | |
381 | ||
d142c7b9 | 382 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !this.playlistPosition, this.playlistPosition) |
e2f01c47 C |
383 | }) |
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 | ||
597a9266 C |
402 | private async onVideoFetched ( |
403 | video: VideoDetails, | |
404 | videoCaptions: VideoCaption[], | |
a5cf76af | 405 | urlOptions: URLOptions |
597a9266 | 406 | ) { |
a5cf76af C |
407 | this.subscribeToLiveEventsIfNeeded(this.video, video) |
408 | ||
df98563e | 409 | this.video = video |
2f4c784a | 410 | this.videoCaptions = videoCaptions |
92fb909c | 411 | |
c448d412 | 412 | // Re init attributes |
c15d61f5 | 413 | this.playerPlaceholderImgSrc = undefined |
6d88de72 | 414 | this.remoteServerDown = false |
f0a39880 | 415 | this.currentTime = undefined |
c448d412 | 416 | |
e2f01c47 | 417 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 418 | const res = await this.confirmService.confirm( |
66357162 C |
419 | $localize`This video contains mature or explicit content. Are you sure you want to watch it?`, |
420 | $localize`Mature or explicit content` | |
d6e32a2e | 421 | ) |
60c2bc80 | 422 | if (res === false) return this.location.back() |
92fb909c C |
423 | } |
424 | ||
0a6817f0 C |
425 | this.buildPlayer(urlOptions) |
426 | .catch(err => console.error('Cannot build the player', err)) | |
427 | ||
0a6817f0 | 428 | this.setOpenGraphTags() |
0a6817f0 | 429 | |
55b84d53 C |
430 | const hookOptions = { |
431 | videojs, | |
432 | video: this.video, | |
433 | playlist: this.playlist | |
434 | } | |
435 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) | |
0a6817f0 C |
436 | } |
437 | ||
438 | private async buildPlayer (urlOptions: URLOptions) { | |
09edde40 C |
439 | // Flush old player if needed |
440 | this.flushPlayer() | |
b891f9bc | 441 | |
c15d61f5 C |
442 | const videoState = this.video.state.id |
443 | if (videoState === VideoState.LIVE_ENDED || videoState === VideoState.WAITING_FOR_LIVE) { | |
444 | this.playerPlaceholderImgSrc = this.video.previewPath | |
445 | return | |
446 | } | |
447 | ||
60c2bc80 | 448 | // Build video element, because videojs removes it on dispose |
e2f01c47 | 449 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
450 | this.playerElement = document.createElement('video') |
451 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 452 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
453 | playerElementWrapper.appendChild(this.playerElement) |
454 | ||
3d9a63d3 C |
455 | const params = { |
456 | video: this.video, | |
0a6817f0 | 457 | videoCaptions: this.videoCaptions, |
3d9a63d3 C |
458 | urlOptions, |
459 | user: this.user | |
e945b184 | 460 | } |
3d9a63d3 C |
461 | const { playerMode, playerOptions } = await this.hooks.wrapFun( |
462 | this.buildPlayerManagerOptions.bind(this), | |
463 | params, | |
c2023a9f C |
464 | 'video-watch', |
465 | 'filter:internal.video-watch.player.build-options.params', | |
3d9a63d3 C |
466 | 'filter:internal.video-watch.player.build-options.result' |
467 | ) | |
e945b184 | 468 | |
e945b184 | 469 | this.zone.runOutsideAngular(async () => { |
3d9a63d3 | 470 | this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player) |
9a18a625 | 471 | |
2adfc7ea | 472 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
473 | |
474 | this.player.on('timeupdate', () => { | |
475 | this.currentTime = Math.floor(this.player.currentTime()) | |
476 | }) | |
e2f01c47 | 477 | |
3bcb4fd7 RK |
478 | /** |
479 | * replaces this.player.one('ended') | |
223b24e6 RK |
480 | * 'condition()': true to make the upnext functionality trigger, |
481 | * false to disable the upnext functionality | |
482 | * go to the next video in 'condition()' if you don't want of the timer. | |
483 | * 'next': function triggered at the end of the timer. | |
484 | * 'suspended': function used at each clic of the timer checking if we need | |
485 | * to reset progress and wait until 'suspended' becomes truthy again. | |
3bcb4fd7 RK |
486 | */ |
487 | this.player.upnext({ | |
ddefb8c9 | 488 | timeout: 10000, // 10s |
66357162 C |
489 | headText: $localize`Up Next`, |
490 | cancelText: $localize`Cancel`, | |
491 | suspendedText: $localize`Autoplay is suspended`, | |
3bcb4fd7 RK |
492 | getTitle: () => this.nextVideoTitle, |
493 | next: () => this.zone.run(() => this.autoplayNext()), | |
494 | condition: () => { | |
495 | if (this.playlist) { | |
496 | if (this.isPlaylistAutoPlayEnabled()) { | |
497 | // upnext will not trigger, and instead the next video will play immediately | |
498 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
499 | } | |
500 | } else if (this.isAutoPlayEnabled()) { | |
501 | return true // upnext will trigger | |
502 | } | |
503 | return false // upnext will not trigger, and instead leave the video stopping | |
223b24e6 RK |
504 | }, |
505 | suspended: () => { | |
506 | return ( | |
507 | !isXPercentInViewport(this.player.el(), 80) || | |
508 | !document.getElementById('content').contains(document.activeElement) | |
509 | ) | |
e2f01c47 C |
510 | } |
511 | }) | |
512 | ||
513 | this.player.one('stopped', () => { | |
514 | if (this.playlist) { | |
706c5a47 | 515 | if (this.isPlaylistAutoPlayEnabled()) this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
516 | } |
517 | }) | |
9a18a625 | 518 | |
e772bdf1 C |
519 | this.player.one('ended', () => { |
520 | if (this.video.isLive) { | |
ceb8f322 | 521 | this.zone.run(() => this.video.state.id = VideoState.LIVE_ENDED) |
e772bdf1 C |
522 | } |
523 | }) | |
524 | ||
9a18a625 C |
525 | this.player.on('theaterChange', (_: any, enabled: boolean) => { |
526 | this.zone.run(() => this.theaterEnabled = enabled) | |
527 | }) | |
5f85f8aa | 528 | |
781ba981 | 529 | this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player, videojs, video: this.video }) |
b891f9bc | 530 | }) |
92fb909c C |
531 | } |
532 | ||
6aa54148 | 533 | private autoplayNext () { |
6dd873d6 RK |
534 | if (this.playlist) { |
535 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) | |
536 | } else if (this.nextVideoUuid) { | |
a1eda903 | 537 | this.router.navigate([ '/w', this.nextVideoUuid ]) |
6aa54148 L |
538 | } |
539 | } | |
540 | ||
df98563e C |
541 | private setOpenGraphTags () { |
542 | this.metaService.setTitle(this.video.name) | |
758b996d | 543 | |
df98563e | 544 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 545 | |
df98563e C |
546 | this.metaService.setTag('og:title', this.video.name) |
547 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 548 | |
df98563e C |
549 | this.metaService.setTag('og:description', this.video.description) |
550 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 551 | |
d38309c3 | 552 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 553 | |
df98563e | 554 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 555 | |
df98563e | 556 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 557 | |
df98563e C |
558 | this.metaService.setTag('og:url', window.location.href) |
559 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 560 | } |
1f3e9fec | 561 | |
d4c6a3b9 | 562 | private isAutoplay () { |
bf079b7b C |
563 | // We'll jump to the thread id, so do not play the video |
564 | if (this.route.snapshot.params['threadId']) return false | |
565 | ||
566 | // Otherwise true by default | |
d4c6a3b9 C |
567 | if (!this.user) return true |
568 | ||
569 | // Be sure the autoPlay is set to false | |
570 | return this.user.autoPlayVideo !== false | |
571 | } | |
09edde40 C |
572 | |
573 | private flushPlayer () { | |
574 | // Remove player if it exists | |
d4a8e7a6 C |
575 | if (!this.player) return |
576 | ||
577 | try { | |
578 | this.player.dispose() | |
579 | this.player = undefined | |
580 | } catch (err) { | |
581 | console.error('Cannot dispose player.', err) | |
09edde40 C |
582 | } |
583 | } | |
1c8ddbfa | 584 | |
3d9a63d3 C |
585 | private buildPlayerManagerOptions (params: { |
586 | video: VideoDetails, | |
587 | videoCaptions: VideoCaption[], | |
588 | urlOptions: CustomizationOptions & { playerMode: PlayerMode }, | |
589 | user?: AuthUser | |
590 | }) { | |
591 | const { video, videoCaptions, urlOptions, user } = params | |
706c5a47 RK |
592 | const getStartTime = () => { |
593 | const byUrl = urlOptions.startTime !== undefined | |
96f6278f | 594 | const byHistory = video.userHistory && (!this.playlist || urlOptions.resume !== undefined) |
58b9ce30 | 595 | const byLocalStorage = getStoredVideoWatchHistory(video.uuid) |
706c5a47 | 596 | |
3c6a44a1 C |
597 | if (byUrl) return timeToInt(urlOptions.startTime) |
598 | if (byHistory) return video.userHistory.currentTime | |
58b9ce30 | 599 | if (byLocalStorage) return byLocalStorage.duration |
3c6a44a1 C |
600 | |
601 | return 0 | |
706c5a47 | 602 | } |
3d9a63d3 | 603 | |
706c5a47 | 604 | let startTime = getStartTime() |
3c6a44a1 | 605 | |
3d9a63d3 C |
606 | // If we are at the end of the video, reset the timer |
607 | if (video.duration - startTime <= 1) startTime = 0 | |
608 | ||
609 | const playerCaptions = videoCaptions.map(c => ({ | |
610 | label: c.language.label, | |
611 | language: c.language.id, | |
612 | src: environment.apiUrl + c.captionPath | |
613 | })) | |
614 | ||
615 | const options: PeertubePlayerManagerOptions = { | |
616 | common: { | |
617 | autoplay: this.isAutoplay(), | |
1dc240a9 | 618 | nextVideo: () => this.zone.run(() => this.autoplayNext()), |
3d9a63d3 C |
619 | |
620 | playerElement: this.playerElement, | |
621 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
622 | ||
623 | videoDuration: video.duration, | |
624 | enableHotkeys: true, | |
625 | inactivityTimeout: 2500, | |
626 | poster: video.previewUrl, | |
627 | ||
628 | startTime, | |
629 | stopTime: urlOptions.stopTime, | |
630 | controls: urlOptions.controls, | |
631 | muted: urlOptions.muted, | |
632 | loop: urlOptions.loop, | |
633 | subtitle: urlOptions.subtitle, | |
634 | ||
635 | peertubeLink: urlOptions.peertubeLink, | |
636 | ||
637 | theaterButton: true, | |
638 | captions: videoCaptions.length !== 0, | |
639 | ||
640 | videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE | |
641 | ? this.videoService.getVideoViewUrl(video.uuid) | |
642 | : null, | |
643 | embedUrl: video.embedUrl, | |
4097c6d6 | 644 | embedTitle: video.name, |
3d9a63d3 | 645 | |
25b7c847 C |
646 | isLive: video.isLive, |
647 | ||
3d9a63d3 C |
648 | language: this.localeId, |
649 | ||
650 | userWatching: user && user.videosHistoryEnabled === true ? { | |
651 | url: this.videoService.getUserWatchingVideoUrl(video.uuid), | |
652 | authorizationHeader: this.authService.getRequestHeaderValue() | |
653 | } : undefined, | |
654 | ||
655 | serverUrl: environment.apiUrl, | |
656 | ||
58b9ce30 | 657 | videoCaptions: playerCaptions, |
658 | ||
3e0e8d4a | 659 | videoUUID: video.uuid |
3d9a63d3 C |
660 | }, |
661 | ||
662 | webtorrent: { | |
663 | videoFiles: video.files | |
72f611ca | 664 | }, |
665 | ||
666 | pluginsManager: this.pluginService.getPluginsManager() | |
3d9a63d3 C |
667 | } |
668 | ||
dfdcbb94 | 669 | // Only set this if we're in a playlist |
5bb2ed6b P |
670 | if (this.playlist) { |
671 | options.common.previousVideo = () => { | |
672 | this.zone.run(() => this.videoWatchPlaylist.navigateToPreviousPlaylistVideo()) | |
673 | } | |
dfdcbb94 P |
674 | } |
675 | ||
3d9a63d3 C |
676 | let mode: PlayerMode |
677 | ||
678 | if (urlOptions.playerMode) { | |
679 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
680 | else mode = 'webtorrent' | |
681 | } else { | |
682 | if (video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
683 | else mode = 'webtorrent' | |
684 | } | |
685 | ||
089af69b C |
686 | // p2p-media-loader needs TextEncoder, try to fallback on WebTorrent |
687 | if (typeof TextEncoder === 'undefined') { | |
688 | mode = 'webtorrent' | |
689 | } | |
690 | ||
3d9a63d3 C |
691 | if (mode === 'p2p-media-loader') { |
692 | const hlsPlaylist = video.getHlsPlaylist() | |
693 | ||
694 | const p2pMediaLoader = { | |
695 | playlistUrl: hlsPlaylist.playlistUrl, | |
696 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
697 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
698 | trackerAnnounce: video.trackerUrls, | |
699 | videoFiles: hlsPlaylist.files | |
700 | } as P2PMediaLoaderOptions | |
701 | ||
702 | Object.assign(options, { p2pMediaLoader }) | |
703 | } | |
704 | ||
705 | return { playerMode: mode, playerOptions: options } | |
706 | } | |
707 | ||
a5cf76af C |
708 | private async subscribeToLiveEventsIfNeeded (oldVideo: VideoDetails, newVideo: VideoDetails) { |
709 | if (!this.liveVideosSub) { | |
a800dbf3 | 710 | this.liveVideosSub = this.buildLiveEventsSubscription() |
a5cf76af C |
711 | } |
712 | ||
713 | if (oldVideo && oldVideo.id !== newVideo.id) { | |
714 | await this.peertubeSocket.unsubscribeLiveVideos(oldVideo.id) | |
715 | } | |
716 | ||
717 | if (!newVideo.isLive) return | |
718 | ||
719 | await this.peertubeSocket.subscribeToLiveVideosSocket(newVideo.id) | |
720 | } | |
721 | ||
a800dbf3 C |
722 | private buildLiveEventsSubscription () { |
723 | return this.peertubeSocket.getLiveVideosObservable() | |
724 | .subscribe(({ type, payload }) => { | |
725 | if (type === 'state-change') return this.handleLiveStateChange(payload.state) | |
726 | if (type === 'views-change') return this.handleLiveViewsChange(payload.views) | |
727 | }) | |
728 | } | |
729 | ||
730 | private handleLiveStateChange (newState: VideoState) { | |
731 | if (newState !== VideoState.PUBLISHED) return | |
732 | ||
733 | const videoState = this.video.state.id | |
734 | if (videoState !== VideoState.WAITING_FOR_LIVE && videoState !== VideoState.LIVE_ENDED) return | |
735 | ||
736 | console.log('Loading video after live update.') | |
737 | ||
738 | const videoUUID = this.video.uuid | |
739 | ||
740 | // Reset to refetch the video | |
741 | this.video = undefined | |
742 | this.loadVideo(videoUUID) | |
743 | } | |
744 | ||
745 | private handleLiveViewsChange (newViews: number) { | |
746 | if (!this.video) { | |
747 | console.error('Cannot update video live views because video is no defined.') | |
748 | return | |
749 | } | |
750 | ||
e43b5a3f C |
751 | console.log('Updating live views.') |
752 | ||
a800dbf3 C |
753 | this.video.views = newViews |
754 | } | |
755 | ||
941c5eac C |
756 | private initHotkeys () { |
757 | this.hotkeys = [ | |
941c5eac | 758 | // These hotkeys are managed by the player |
66357162 C |
759 | new Hotkey('f', e => e, undefined, $localize`Enter/exit fullscreen (requires player focus)`), |
760 | new Hotkey('space', e => e, undefined, $localize`Play/Pause the video (requires player focus)`), | |
761 | new Hotkey('m', e => e, undefined, $localize`Mute/unmute the video (requires player focus)`), | |
941c5eac | 762 | |
66357162 | 763 | new Hotkey('0-9', e => e, undefined, $localize`Skip to a percentage of the video: 0 is 0% and 9 is 90% (requires player focus)`), |
941c5eac | 764 | |
66357162 C |
765 | new Hotkey('up', e => e, undefined, $localize`Increase the volume (requires player focus)`), |
766 | new Hotkey('down', e => e, undefined, $localize`Decrease the volume (requires player focus)`), | |
941c5eac | 767 | |
66357162 C |
768 | new Hotkey('right', e => e, undefined, $localize`Seek the video forward (requires player focus)`), |
769 | new Hotkey('left', e => e, undefined, $localize`Seek the video backward (requires player focus)`), | |
941c5eac | 770 | |
66357162 C |
771 | new Hotkey('>', e => e, undefined, $localize`Increase playback rate (requires player focus)`), |
772 | new Hotkey('<', e => e, undefined, $localize`Decrease playback rate (requires player focus)`), | |
941c5eac | 773 | |
66357162 | 774 | new Hotkey('.', e => e, undefined, $localize`Navigate in the video frame by frame (requires player focus)`) |
941c5eac | 775 | ] |
3d216ea0 C |
776 | |
777 | if (this.isUserLoggedIn()) { | |
778 | this.hotkeys = this.hotkeys.concat([ | |
3d216ea0 C |
779 | new Hotkey('shift+s', () => { |
780 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
781 | return false | |
66357162 | 782 | }, undefined, $localize`Subscribe to the account`) |
3d216ea0 C |
783 | ]) |
784 | } | |
785 | ||
786 | this.hotkeysService.add(this.hotkeys) | |
941c5eac | 787 | } |
dc8bc31b | 788 | } |