]>
Commit | Line | Data |
---|---|---|
e972e046 | 1 | import { catchError } from 'rxjs/operators' |
3b492bff | 2 | import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
901637bb | 4 | import { RedirectService } from '@app/core/routing/redirect.service' |
0bd78bf3 | 5 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
07fa4c97 | 6 | import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component' |
1f3e9fec | 7 | import { MetaService } from '@ngx-meta/core' |
f8b2c1b4 | 8 | import { Notifier, ServerService } from '@app/core' |
16f7022b | 9 | import { forkJoin, Subscription } from 'rxjs' |
20d21199 | 10 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
e2f01c47 | 11 | import { UserVideoRateType, VideoCaption, VideoPlaylistPrivacy, VideoPrivacy, VideoState } from '../../../../../shared' |
df98563e | 12 | import { AuthService, ConfirmService } from '../../core' |
a51bad1a | 13 | import { RestExtractor, VideoBlacklistService } from '../../shared' |
ff249f49 | 14 | import { VideoDetails } from '../../shared/video/video-details.model' |
63c4db6d | 15 | import { VideoService } from '../../shared/video/video.service' |
4635f59d | 16 | import { VideoShareComponent } from './modal/video-share.component' |
20d21199 | 17 | import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component' |
989e526a | 18 | import { I18n } from '@ngx-translate/i18n-polyfill' |
e945b184 | 19 | import { environment } from '../../../environments/environment' |
16f7022b | 20 | import { VideoCaptionService } from '@app/shared/video-caption' |
1506307f | 21 | import { MarkdownService } from '@app/shared/renderer' |
6ec0b75b C |
22 | import { |
23 | P2PMediaLoaderOptions, | |
24 | PeertubePlayerManager, | |
25 | PeertubePlayerManagerOptions, | |
597a9266 | 26 | PlayerMode |
6ec0b75b | 27 | } from '../../../assets/player/peertube-player-manager' |
e2f01c47 C |
28 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
29 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
30 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
31 | import { Video } from '@app/shared/video/video.model' | |
31b6ddf8 | 32 | import { isWebRTCDisabled } from '../../../assets/player/utils' |
dc8bc31b | 33 | |
dc8bc31b C |
34 | @Component({ |
35 | selector: 'my-video-watch', | |
ec8d8440 C |
36 | templateUrl: './video-watch.component.html', |
37 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 38 | }) |
0629423c | 39 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
40 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
41 | ||
df98563e | 42 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent |
07fa4c97 | 43 | @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent |
20d21199 | 44 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
df98563e | 45 | |
2adfc7ea | 46 | player: any |
0826c92d | 47 | playerElement: HTMLVideoElement |
9a18a625 | 48 | theaterEnabled = false |
154898b0 | 49 | userRating: UserVideoRateType = null |
404b54e1 | 50 | video: VideoDetails = null |
80958c78 | 51 | descriptionLoading = false |
2de96f4d | 52 | |
e2f01c47 C |
53 | playlist: VideoPlaylist = null |
54 | playlistVideos: Video[] = [] | |
55 | playlistPagination: ComponentPagination = { | |
56 | currentPage: 1, | |
bce47964 | 57 | itemsPerPage: 30, |
e2f01c47 C |
58 | totalItems: null |
59 | } | |
60 | noPlaylistVideos = false | |
61 | currentPlaylistPosition = 1 | |
62 | ||
2de96f4d C |
63 | completeDescriptionShown = false |
64 | completeVideoDescription: string | |
65 | shortVideoDescription: string | |
9d9597df | 66 | videoHTMLDescription = '' |
e9189001 | 67 | likesBarTooltipText = '' |
73e09f27 | 68 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 69 | remoteServerDown = false |
20d21199 | 70 | hotkeys: Hotkey[] |
df98563e | 71 | |
f0a39880 | 72 | private currentTime: number |
df98563e | 73 | private paramsSub: Subscription |
e2f01c47 | 74 | private queryParamsSub: Subscription |
31b6ddf8 | 75 | private configSub: Subscription |
df98563e C |
76 | |
77 | constructor ( | |
4fd8aa32 | 78 | private elementRef: ElementRef, |
3b492bff | 79 | private changeDetector: ChangeDetectorRef, |
0629423c | 80 | private route: ActivatedRoute, |
92fb909c | 81 | private router: Router, |
d3ef341a | 82 | private videoService: VideoService, |
e2f01c47 | 83 | private playlistService: VideoPlaylistService, |
35bf0c83 | 84 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 85 | private confirmService: ConfirmService, |
3ec343a4 | 86 | private metaService: MetaService, |
7ddd02c9 | 87 | private authService: AuthService, |
0883b324 | 88 | private serverService: ServerService, |
a51bad1a | 89 | private restExtractor: RestExtractor, |
f8b2c1b4 | 90 | private notifier: Notifier, |
7ae71355 | 91 | private markdownService: MarkdownService, |
901637bb | 92 | private zone: NgZone, |
989e526a | 93 | private redirectService: RedirectService, |
16f7022b | 94 | private videoCaptionService: VideoCaptionService, |
e945b184 | 95 | private i18n: I18n, |
20d21199 | 96 | private hotkeysService: HotkeysService, |
e945b184 | 97 | @Inject(LOCALE_ID) private localeId: string |
d3ef341a | 98 | ) {} |
dc8bc31b | 99 | |
b2731bff C |
100 | get user () { |
101 | return this.authService.getUser() | |
102 | } | |
103 | ||
df98563e | 104 | ngOnInit () { |
31b6ddf8 C |
105 | this.configSub = this.serverService.configLoaded |
106 | .subscribe(() => { | |
107 | if ( | |
108 | isWebRTCDisabled() || | |
109 | this.serverService.getConfig().tracker.enabled === false || | |
110 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' | |
111 | ) { | |
112 | this.hasAlreadyAcceptedPrivacyConcern = true | |
113 | } | |
114 | }) | |
2b3b76ab | 115 | |
13fc89f4 | 116 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
117 | const videoId = routeParams[ 'videoId' ] |
118 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 119 | |
e2f01c47 C |
120 | const playlistId = routeParams[ 'playlistId' ] |
121 | if (playlistId) this.loadPlaylist(playlistId) | |
122 | }) | |
bf079b7b | 123 | |
e2f01c47 C |
124 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
125 | const videoId = queryParams[ 'videoId' ] | |
126 | if (videoId) this.loadVideo(videoId) | |
df98563e | 127 | }) |
20d21199 RK |
128 | |
129 | this.hotkeys = [ | |
a157b3a3 | 130 | new Hotkey('shift+l', (event: KeyboardEvent): boolean => { |
20d21199 RK |
131 | this.setLike() |
132 | return false | |
e33f888b | 133 | }, undefined, this.i18n('Like the video')), |
a157b3a3 | 134 | new Hotkey('shift+d', (event: KeyboardEvent): boolean => { |
20d21199 RK |
135 | this.setDislike() |
136 | return false | |
e33f888b | 137 | }, undefined, this.i18n('Dislike the video')), |
a157b3a3 | 138 | new Hotkey('shift+s', (event: KeyboardEvent): boolean => { |
20d21199 RK |
139 | this.subscribeButton.subscribed ? |
140 | this.subscribeButton.unsubscribe() : | |
141 | this.subscribeButton.subscribe() | |
142 | return false | |
e33f888b | 143 | }, undefined, this.i18n('Subscribe to the account')) |
20d21199 RK |
144 | ] |
145 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
d1992b93 C |
146 | } |
147 | ||
df98563e | 148 | ngOnDestroy () { |
09edde40 | 149 | this.flushPlayer() |
067e3f84 | 150 | |
13fc89f4 | 151 | // Unsubscribe subscriptions |
e2f01c47 C |
152 | if (this.paramsSub) this.paramsSub.unsubscribe() |
153 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
20d21199 RK |
154 | |
155 | // Unbind hotkeys | |
156 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
dc8bc31b | 157 | } |
98b01bac | 158 | |
df98563e C |
159 | setLike () { |
160 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
161 | if (this.userRating === 'like') { |
162 | // Already liked this video | |
163 | this.setRating('none') | |
164 | } else { | |
165 | this.setRating('like') | |
166 | } | |
d38b8281 C |
167 | } |
168 | ||
df98563e C |
169 | setDislike () { |
170 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
171 | if (this.userRating === 'dislike') { |
172 | // Already disliked this video | |
173 | this.setRating('none') | |
174 | } else { | |
175 | this.setRating('dislike') | |
176 | } | |
d38b8281 C |
177 | } |
178 | ||
2de96f4d | 179 | showMoreDescription () { |
2de96f4d C |
180 | if (this.completeVideoDescription === undefined) { |
181 | return this.loadCompleteDescription() | |
182 | } | |
183 | ||
184 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 185 | this.completeDescriptionShown = true |
2de96f4d C |
186 | } |
187 | ||
188 | showLessDescription () { | |
2de96f4d | 189 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 190 | this.completeDescriptionShown = false |
2de96f4d C |
191 | } |
192 | ||
193 | loadCompleteDescription () { | |
80958c78 C |
194 | this.descriptionLoading = true |
195 | ||
2de96f4d | 196 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
197 | .subscribe( |
198 | description => { | |
199 | this.completeDescriptionShown = true | |
200 | this.descriptionLoading = false | |
201 | ||
202 | this.shortVideoDescription = this.video.description | |
203 | this.completeVideoDescription = description | |
204 | ||
205 | this.updateVideoDescription(this.completeVideoDescription) | |
206 | }, | |
207 | ||
208 | error => { | |
209 | this.descriptionLoading = false | |
f8b2c1b4 | 210 | this.notifier.error(error.message) |
2186386c C |
211 | } |
212 | ) | |
2de96f4d C |
213 | } |
214 | ||
07fa4c97 C |
215 | showSupportModal () { |
216 | this.videoSupportModal.show() | |
217 | } | |
218 | ||
df98563e | 219 | showShareModal () { |
f0a39880 | 220 | this.videoShareModal.show(this.currentTime) |
99cc4f49 C |
221 | } |
222 | ||
df98563e C |
223 | isUserLoggedIn () { |
224 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
225 | } |
226 | ||
b1fa3eba C |
227 | getVideoTags () { |
228 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
229 | ||
4278710d | 230 | return this.video.tags |
b1fa3eba C |
231 | } |
232 | ||
6725d05c C |
233 | isVideoRemovable () { |
234 | return this.video.isRemovableBy(this.authService.getUser()) | |
235 | } | |
236 | ||
3a0fb65c C |
237 | onVideoRemoved () { |
238 | this.redirectService.redirectToHomepage() | |
6725d05c C |
239 | } |
240 | ||
73e09f27 | 241 | acceptedPrivacyConcern () { |
0bd78bf3 | 242 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
243 | this.hasAlreadyAcceptedPrivacyConcern = true |
244 | } | |
245 | ||
2186386c C |
246 | isVideoToTranscode () { |
247 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
248 | } | |
249 | ||
156c50af | 250 | isVideoDownloadable () { |
7f2cfe3a | 251 | return this.video && this.video.downloadEnabled |
156c50af LD |
252 | } |
253 | ||
516df59b C |
254 | isVideoToImport () { |
255 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
256 | } | |
257 | ||
bbe0f064 C |
258 | hasVideoScheduledPublication () { |
259 | return this.video && this.video.scheduledUpdate !== undefined | |
260 | } | |
261 | ||
e2f01c47 C |
262 | isVideoBlur (video: Video) { |
263 | return video.isVideoNSFWForUser(this.user, this.serverService.getConfig()) | |
264 | } | |
265 | ||
266 | isPlaylistOwned () { | |
267 | return this.playlist.isLocal === true && this.playlist.ownerAccount.name === this.user.username | |
268 | } | |
269 | ||
270 | isUnlistedPlaylist () { | |
271 | return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED | |
272 | } | |
273 | ||
274 | isPrivatePlaylist () { | |
275 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE | |
276 | } | |
277 | ||
278 | isPublicPlaylist () { | |
279 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC | |
280 | } | |
281 | ||
282 | onPlaylistVideosNearOfBottom () { | |
283 | // Last page | |
284 | if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return | |
285 | ||
286 | this.playlistPagination.currentPage += 1 | |
287 | this.loadPlaylistElements(false) | |
288 | } | |
289 | ||
290 | onElementRemoved (video: Video) { | |
291 | this.playlistVideos = this.playlistVideos.filter(v => v.id !== video.id) | |
292 | ||
293 | this.playlistPagination.totalItems-- | |
294 | } | |
295 | ||
296 | private loadVideo (videoId: string) { | |
297 | // Video did not change | |
298 | if (this.video && this.video.uuid === videoId) return | |
299 | ||
300 | if (this.player) this.player.pause() | |
301 | ||
302 | // Video did change | |
303 | forkJoin( | |
304 | this.videoService.getVideo(videoId), | |
305 | this.videoCaptionService.listCaptions(videoId) | |
306 | ) | |
307 | .pipe( | |
308 | // If 401, the video is private or blacklisted so redirect to 404 | |
309 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
310 | ) | |
311 | .subscribe(([ video, captionsResult ]) => { | |
312 | const queryParams = this.route.snapshot.queryParams | |
313 | const startTime = queryParams.start | |
314 | const stopTime = queryParams.stop | |
315 | const subtitle = queryParams.subtitle | |
316 | const playerMode = queryParams.mode | |
317 | ||
318 | this.onVideoFetched(video, captionsResult.data, { startTime, stopTime, subtitle, playerMode }) | |
319 | .catch(err => this.handleError(err)) | |
320 | }) | |
321 | } | |
322 | ||
323 | private loadPlaylist (playlistId: string) { | |
324 | // Playlist did not change | |
325 | if (this.playlist && this.playlist.uuid === playlistId) return | |
326 | ||
327 | this.playlistService.getVideoPlaylist(playlistId) | |
328 | .pipe( | |
329 | // If 401, the video is private or blacklisted so redirect to 404 | |
330 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
331 | ) | |
332 | .subscribe(playlist => { | |
333 | this.playlist = playlist | |
334 | ||
335 | const videoId = this.route.snapshot.queryParams['videoId'] | |
336 | this.loadPlaylistElements(!videoId) | |
337 | }) | |
338 | } | |
339 | ||
340 | private loadPlaylistElements (redirectToFirst = false) { | |
bce47964 | 341 | this.videoService.getPlaylistVideos(this.playlist.uuid, this.playlistPagination) |
e2f01c47 C |
342 | .subscribe(({ totalVideos, videos }) => { |
343 | this.playlistVideos = this.playlistVideos.concat(videos) | |
344 | this.playlistPagination.totalItems = totalVideos | |
345 | ||
346 | if (totalVideos === 0) { | |
347 | this.noPlaylistVideos = true | |
348 | return | |
349 | } | |
350 | ||
351 | this.updatePlaylistIndex() | |
352 | ||
353 | if (redirectToFirst) { | |
354 | const extras = { | |
355 | queryParams: { videoId: this.playlistVideos[ 0 ].uuid }, | |
356 | replaceUrl: true | |
357 | } | |
358 | this.router.navigate([], extras) | |
359 | } | |
360 | }) | |
361 | } | |
362 | ||
2de96f4d C |
363 | private updateVideoDescription (description: string) { |
364 | this.video.description = description | |
365 | this.setVideoDescriptionHTML() | |
366 | } | |
367 | ||
41d71344 C |
368 | private async setVideoDescriptionHTML () { |
369 | this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description) | |
2de96f4d C |
370 | } |
371 | ||
e9189001 | 372 | private setVideoLikesBarTooltipText () { |
2186386c C |
373 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
374 | likesNumber: this.video.likes, | |
375 | dislikesNumber: this.video.dislikes | |
376 | }) | |
e9189001 C |
377 | } |
378 | ||
0c31c33d C |
379 | private handleError (err: any) { |
380 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
381 | if (!errorMessage) return |
382 | ||
6d88de72 | 383 | // Display a message in the video player instead of a notification |
0f7fedc3 | 384 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
385 | this.flushPlayer() |
386 | this.remoteServerDown = true | |
3b492bff C |
387 | this.changeDetector.detectChanges() |
388 | ||
6d88de72 | 389 | return |
0c31c33d C |
390 | } |
391 | ||
f8b2c1b4 | 392 | this.notifier.error(errorMessage) |
0c31c33d C |
393 | } |
394 | ||
df98563e | 395 | private checkUserRating () { |
d38b8281 | 396 | // Unlogged users do not have ratings |
df98563e | 397 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
398 | |
399 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
400 | .subscribe( |
401 | ratingObject => { | |
402 | if (ratingObject) { | |
403 | this.userRating = ratingObject.rating | |
404 | } | |
405 | }, | |
406 | ||
f8b2c1b4 | 407 | err => this.notifier.error(err.message) |
2186386c | 408 | ) |
d38b8281 C |
409 | } |
410 | ||
597a9266 C |
411 | private async onVideoFetched ( |
412 | video: VideoDetails, | |
413 | videoCaptions: VideoCaption[], | |
f0a39880 | 414 | urlOptions: { startTime?: number, stopTime?: number, subtitle?: string, playerMode?: string } |
597a9266 | 415 | ) { |
df98563e | 416 | this.video = video |
92fb909c | 417 | |
c448d412 C |
418 | // Re init attributes |
419 | this.descriptionLoading = false | |
420 | this.completeDescriptionShown = false | |
6d88de72 | 421 | this.remoteServerDown = false |
f0a39880 | 422 | this.currentTime = undefined |
c448d412 | 423 | |
e2f01c47 C |
424 | this.updatePlaylistIndex() |
425 | ||
1b04f19c | 426 | let startTime = urlOptions.startTime || (this.video.userHistory ? this.video.userHistory.currentTime : 0) |
43483d12 | 427 | // If we are at the end of the video, reset the timer |
6e46de09 C |
428 | if (this.video.duration - startTime <= 1) startTime = 0 |
429 | ||
e2f01c47 | 430 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 431 | const res = await this.confirmService.confirm( |
989e526a C |
432 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
433 | this.i18n('Mature or explicit content') | |
d6e32a2e | 434 | ) |
901637bb | 435 | if (res === false) return this.redirectService.redirectToHomepage() |
92fb909c C |
436 | } |
437 | ||
09edde40 C |
438 | // Flush old player if needed |
439 | this.flushPlayer() | |
b891f9bc C |
440 | |
441 | // Build video element, because videojs remove it on dispose | |
e2f01c47 | 442 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
443 | this.playerElement = document.createElement('video') |
444 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 445 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
446 | playerElementWrapper.appendChild(this.playerElement) |
447 | ||
16f7022b C |
448 | const playerCaptions = videoCaptions.map(c => ({ |
449 | label: c.language.label, | |
450 | language: c.language.id, | |
451 | src: environment.apiUrl + c.captionPath | |
452 | })) | |
453 | ||
6ec0b75b | 454 | const options: PeertubePlayerManagerOptions = { |
2adfc7ea C |
455 | common: { |
456 | autoplay: this.isAutoplay(), | |
6ec0b75b | 457 | |
2adfc7ea | 458 | playerElement: this.playerElement, |
6ec0b75b C |
459 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, |
460 | ||
2adfc7ea C |
461 | videoDuration: this.video.duration, |
462 | enableHotkeys: true, | |
463 | inactivityTimeout: 2500, | |
464 | poster: this.video.previewUrl, | |
465 | startTime, | |
f0a39880 | 466 | stopTime: urlOptions.stopTime, |
2adfc7ea C |
467 | |
468 | theaterMode: true, | |
469 | captions: videoCaptions.length !== 0, | |
470 | peertubeLink: false, | |
471 | ||
472 | videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE ? this.videoService.getVideoViewUrl(this.video.uuid) : null, | |
473 | embedUrl: this.video.embedUrl, | |
474 | ||
475 | language: this.localeId, | |
476 | ||
477 | subtitle: urlOptions.subtitle, | |
aa8b6df4 | 478 | |
2adfc7ea C |
479 | userWatching: this.user && this.user.videosHistoryEnabled === true ? { |
480 | url: this.videoService.getUserWatchingVideoUrl(this.video.uuid), | |
481 | authorizationHeader: this.authService.getRequestHeaderValue() | |
482 | } : undefined, | |
aa8b6df4 | 483 | |
2adfc7ea C |
484 | serverUrl: environment.apiUrl, |
485 | ||
486 | videoCaptions: playerCaptions | |
6ec0b75b C |
487 | }, |
488 | ||
489 | webtorrent: { | |
490 | videoFiles: this.video.files | |
09209296 | 491 | } |
e945b184 C |
492 | } |
493 | ||
597a9266 C |
494 | const mode: PlayerMode = urlOptions.playerMode === 'p2p-media-loader' ? 'p2p-media-loader' : 'webtorrent' |
495 | ||
496 | if (mode === 'p2p-media-loader') { | |
497 | const hlsPlaylist = this.video.getHlsPlaylist() | |
e945b184 | 498 | |
09209296 C |
499 | const p2pMediaLoader = { |
500 | playlistUrl: hlsPlaylist.playlistUrl, | |
501 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
502 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
503 | trackerAnnounce: this.video.trackerUrls, | |
2adfc7ea | 504 | videoFiles: this.video.files |
09209296 C |
505 | } as P2PMediaLoaderOptions |
506 | ||
507 | Object.assign(options, { p2pMediaLoader }) | |
e945b184 C |
508 | } |
509 | ||
e945b184 | 510 | this.zone.runOutsideAngular(async () => { |
09209296 | 511 | this.player = await PeertubePlayerManager.initialize(mode, options) |
9a18a625 C |
512 | this.theaterEnabled = this.player.theaterEnabled |
513 | ||
2adfc7ea | 514 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
515 | |
516 | this.player.on('timeupdate', () => { | |
517 | this.currentTime = Math.floor(this.player.currentTime()) | |
518 | }) | |
e2f01c47 C |
519 | |
520 | this.player.one('ended', () => { | |
521 | if (this.playlist) { | |
522 | this.zone.run(() => this.navigateToNextPlaylistVideo()) | |
523 | } | |
524 | }) | |
525 | ||
526 | this.player.one('stopped', () => { | |
527 | if (this.playlist) { | |
528 | this.zone.run(() => this.navigateToNextPlaylistVideo()) | |
529 | } | |
530 | }) | |
9a18a625 C |
531 | |
532 | this.player.on('theaterChange', (_: any, enabled: boolean) => { | |
533 | this.zone.run(() => this.theaterEnabled = enabled) | |
534 | }) | |
b891f9bc | 535 | }) |
22b59e80 C |
536 | |
537 | this.setVideoDescriptionHTML() | |
538 | this.setVideoLikesBarTooltipText() | |
539 | ||
540 | this.setOpenGraphTags() | |
541 | this.checkUserRating() | |
92fb909c C |
542 | } |
543 | ||
5c6d985f | 544 | private setRating (nextRating: UserVideoRateType) { |
57a49263 BB |
545 | let method |
546 | switch (nextRating) { | |
547 | case 'like': | |
548 | method = this.videoService.setVideoLike | |
549 | break | |
550 | case 'dislike': | |
551 | method = this.videoService.setVideoDislike | |
552 | break | |
553 | case 'none': | |
554 | method = this.videoService.unsetVideoLike | |
555 | break | |
556 | } | |
557 | ||
558 | method.call(this.videoService, this.video.id) | |
2186386c C |
559 | .subscribe( |
560 | () => { | |
561 | // Update the video like attribute | |
562 | this.updateVideoRating(this.userRating, nextRating) | |
563 | this.userRating = nextRating | |
564 | }, | |
565 | ||
f8b2c1b4 | 566 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 567 | ) |
57a49263 BB |
568 | } |
569 | ||
5c6d985f | 570 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
571 | let likesToIncrement = 0 |
572 | let dislikesToIncrement = 0 | |
d38b8281 C |
573 | |
574 | if (oldRating) { | |
df98563e C |
575 | if (oldRating === 'like') likesToIncrement-- |
576 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
577 | } |
578 | ||
df98563e C |
579 | if (newRating === 'like') likesToIncrement++ |
580 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 581 | |
df98563e C |
582 | this.video.likes += likesToIncrement |
583 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 584 | |
22b59e80 | 585 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 586 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
587 | } |
588 | ||
e2f01c47 C |
589 | private updatePlaylistIndex () { |
590 | if (this.playlistVideos.length === 0 || !this.video) return | |
591 | ||
592 | for (const video of this.playlistVideos) { | |
593 | if (video.id === this.video.id) { | |
594 | this.currentPlaylistPosition = video.playlistElement.position | |
595 | return | |
596 | } | |
597 | } | |
598 | ||
599 | // Load more videos to find our video | |
600 | this.onPlaylistVideosNearOfBottom() | |
601 | } | |
602 | ||
df98563e C |
603 | private setOpenGraphTags () { |
604 | this.metaService.setTitle(this.video.name) | |
758b996d | 605 | |
df98563e | 606 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 607 | |
df98563e C |
608 | this.metaService.setTag('og:title', this.video.name) |
609 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 610 | |
df98563e C |
611 | this.metaService.setTag('og:description', this.video.description) |
612 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 613 | |
d38309c3 | 614 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 615 | |
df98563e | 616 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 617 | |
df98563e | 618 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 619 | |
df98563e C |
620 | this.metaService.setTag('og:url', window.location.href) |
621 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 622 | } |
1f3e9fec | 623 | |
d4c6a3b9 | 624 | private isAutoplay () { |
bf079b7b C |
625 | // We'll jump to the thread id, so do not play the video |
626 | if (this.route.snapshot.params['threadId']) return false | |
627 | ||
628 | // Otherwise true by default | |
d4c6a3b9 C |
629 | if (!this.user) return true |
630 | ||
631 | // Be sure the autoPlay is set to false | |
632 | return this.user.autoPlayVideo !== false | |
633 | } | |
09edde40 C |
634 | |
635 | private flushPlayer () { | |
636 | // Remove player if it exists | |
637 | if (this.player) { | |
638 | this.player.dispose() | |
639 | this.player = undefined | |
640 | } | |
641 | } | |
e2f01c47 C |
642 | |
643 | private navigateToNextPlaylistVideo () { | |
644 | if (this.currentPlaylistPosition < this.playlistPagination.totalItems) { | |
645 | const next = this.playlistVideos.find(v => v.playlistElement.position === this.currentPlaylistPosition + 1) | |
646 | ||
647 | const start = next.playlistElement.startTimestamp | |
648 | const stop = next.playlistElement.stopTimestamp | |
649 | this.router.navigate([],{ queryParams: { videoId: next.uuid, start, stop } }) | |
650 | } | |
651 | } | |
dc8bc31b | 652 | } |