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