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