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