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