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