1
0
Fork 0
peertube/client/src/app/videos/+video-watch/video-watch.component.ts

731 lines
23 KiB
TypeScript
Raw Normal View History

2018-09-18 09:59:05 +00:00
import { catchError } from 'rxjs/operators'
2018-07-17 15:06:34 +00:00
import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
2018-03-01 12:57:29 +00:00
import { RedirectService } from '@app/core/routing/redirect.service'
import { peertubeLocalStorage, peertubeSessionStorage } from '@app/shared/misc/peertube-web-storage'
2018-02-20 15:13:05 +00:00
import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
2017-11-30 08:21:11 +00:00
import { MetaService } from '@ngx-meta/core'
2019-12-05 16:06:18 +00:00
import { AuthUser, Notifier, ServerService } from '@app/core'
2019-05-31 09:48:28 +00:00
import { forkJoin, Observable, Subscription } from 'rxjs'
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
import { UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '../../../../../shared'
import { AuthService, ConfirmService } from '../../core'
2018-05-31 09:35:01 +00:00
import { RestExtractor, VideoBlacklistService } from '../../shared'
2017-12-07 10:15:19 +00:00
import { VideoDetails } from '../../shared/video/video-details.model'
2017-12-11 16:36:46 +00:00
import { VideoService } from '../../shared/video/video.service'
2017-12-27 15:11:53 +00:00
import { VideoShareComponent } from './modal/video-share.component'
import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
2018-05-31 16:12:15 +00:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-06-06 12:23:40 +00:00
import { environment } from '../../../environments/environment'
2018-07-13 16:21:19 +00:00
import { VideoCaptionService } from '@app/shared/video-caption'
import { MarkdownService } from '@app/shared/renderer'
2019-02-06 09:39:50 +00:00
import {
videojs,
2019-06-11 13:59:10 +00:00
CustomizationOptions,
2019-02-06 09:39:50 +00:00
P2PMediaLoaderOptions,
PeertubePlayerManager,
PeertubePlayerManagerOptions,
2019-02-07 14:56:17 +00:00
PlayerMode
2019-02-06 09:39:50 +00:00
} from '../../../assets/player/peertube-player-manager'
2019-03-13 13:18:58 +00:00
import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
import { Video } from '@app/shared/video/video.model'
2019-06-11 13:59:10 +00:00
import { isWebRTCDisabled, timeToInt } from '../../../assets/player/utils'
import { VideoWatchPlaylistComponent } from '@app/videos/+video-watch/video-watch-playlist.component'
2019-06-11 14:26:48 +00:00
import { getStoredTheater } from '../../../assets/player/peertube-player-local-storage'
2019-07-08 13:54:08 +00:00
import { PluginService } from '@app/core/plugins/plugin.service'
2019-07-22 13:40:13 +00:00
import { HooksService } from '@app/core/plugins/hooks.service'
import { PlatformLocation } from '@angular/common'
import { randomInt } from '@shared/core-utils/miscs/miscs'
import { RecommendedVideosComponent } from '../recommendations/recommended-videos.component'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-video-watch',
templateUrl: './video-watch.component.html',
styleUrls: [ './video-watch.component.scss' ]
2016-03-14 12:50:19 +00:00
})
2016-07-08 15:15:14 +00:00
export class VideoWatchComponent implements OnInit, OnDestroy {
private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
2019-07-24 14:05:59 +00:00
@ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent
@ViewChild('videoShareModal', { static: false }) videoShareModal: VideoShareComponent
@ViewChild('videoSupportModal', { static: false }) videoSupportModal: VideoSupportComponent
@ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent
player: any
2017-12-20 08:57:00 +00:00
playerElement: HTMLVideoElement
2019-03-18 09:26:53 +00:00
theaterEnabled = false
2017-06-17 09:28:11 +00:00
userRating: UserVideoRateType = null
descriptionLoading = false
2019-06-12 10:40:24 +00:00
video: VideoDetails = null
videoCaptions: VideoCaption[] = []
2019-03-13 13:18:58 +00:00
playlist: VideoPlaylist = null
completeDescriptionShown = false
completeVideoDescription: string
shortVideoDescription: string
videoHTMLDescription = ''
2017-12-21 09:49:52 +00:00
likesBarTooltipText = ''
hasAlreadyAcceptedPrivacyConcern = false
remoteServerDown = false
hotkeys: Hotkey[] = []
tooltipLike = ''
tooltipDislike = ''
tooltipSupport = ''
tooltipSaveToPlaylist = ''
private nextVideoUuid = ''
2019-03-07 16:06:00 +00:00
private currentTime: number
private paramsSub: Subscription
2019-03-13 13:18:58 +00:00
private queryParamsSub: Subscription
2019-04-10 07:23:18 +00:00
private configSub: Subscription
constructor (
2016-05-27 15:49:18 +00:00
private elementRef: ElementRef,
2018-07-17 15:06:34 +00:00
private changeDetector: ChangeDetectorRef,
2016-07-08 15:15:14 +00:00
private route: ActivatedRoute,
2017-04-04 19:37:03 +00:00
private router: Router,
2016-05-31 20:39:36 +00:00
private videoService: VideoService,
2019-03-13 13:18:58 +00:00
private playlistService: VideoPlaylistService,
2017-10-10 08:02:18 +00:00
private videoBlacklistService: VideoBlacklistService,
2017-04-04 19:37:03 +00:00
private confirmService: ConfirmService,
2016-11-04 16:37:44 +00:00
private metaService: MetaService,
private authService: AuthService,
private serverService: ServerService,
2018-05-31 09:35:01 +00:00
private restExtractor: RestExtractor,
private notifier: Notifier,
2019-07-08 13:54:08 +00:00
private pluginService: PluginService,
2018-01-10 16:36:35 +00:00
private markdownService: MarkdownService,
2018-03-01 12:57:29 +00:00
private zone: NgZone,
2018-05-31 16:12:15 +00:00
private redirectService: RedirectService,
2018-07-13 16:21:19 +00:00
private videoCaptionService: VideoCaptionService,
2018-06-06 12:23:40 +00:00
private i18n: I18n,
private hotkeysService: HotkeysService,
2019-07-22 13:40:13 +00:00
private hooks: HooksService,
private location: PlatformLocation,
2018-06-06 12:23:40 +00:00
@Inject(LOCALE_ID) private localeId: string
) {
this.tooltipLike = this.i18n('Like this video')
this.tooltipDislike = this.i18n('Dislike this video')
this.tooltipSupport = this.i18n('Support options for this video')
this.tooltipSaveToPlaylist = this.i18n('Save to playlist')
}
2016-03-14 12:50:19 +00:00
2017-12-12 13:41:59 +00:00
get user () {
return this.authService.getUser()
}
2019-07-08 13:54:08 +00:00
async ngOnInit () {
2019-04-10 07:23:18 +00:00
this.configSub = this.serverService.configLoaded
.subscribe(() => {
if (
isWebRTCDisabled() ||
this.serverService.getConfig().tracker.enabled === false ||
peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
) {
this.hasAlreadyAcceptedPrivacyConcern = true
}
})
this.paramsSub = this.route.params.subscribe(routeParams => {
2019-03-13 13:18:58 +00:00
const videoId = routeParams[ 'videoId' ]
if (videoId) this.loadVideo(videoId)
2018-05-31 09:35:01 +00:00
2019-03-13 13:18:58 +00:00
const playlistId = routeParams[ 'playlistId' ]
if (playlistId) this.loadPlaylist(playlistId)
})
2019-03-13 13:18:58 +00:00
this.queryParamsSub = this.route.queryParams.subscribe(queryParams => {
const videoId = queryParams[ 'videoId' ]
if (videoId) this.loadVideo(videoId)
})
this.initHotkeys()
2019-06-11 14:26:48 +00:00
this.theaterEnabled = getStoredTheater()
2019-07-08 13:54:08 +00:00
2019-07-23 10:16:34 +00:00
this.hooks.runAction('action:video-watch.init', 'video-watch')
}
ngOnDestroy () {
2018-04-03 16:06:58 +00:00
this.flushPlayer()
2016-11-08 20:17:17 +00:00
// Unsubscribe subscriptions
2019-03-13 13:18:58 +00:00
if (this.paramsSub) this.paramsSub.unsubscribe()
if (this.queryParamsSub) this.queryParamsSub.unsubscribe()
// Unbind hotkeys
this.hotkeysService.remove(this.hotkeys)
2016-03-14 12:50:19 +00:00
}
2016-03-14 21:16:43 +00:00
setLike () {
if (this.isUserLoggedIn() === false) return
2019-05-31 09:48:28 +00:00
// Already liked this video
if (this.userRating === 'like') this.setRating('none')
else this.setRating('like')
2017-03-08 20:35:43 +00:00
}
setDislike () {
if (this.isUserLoggedIn() === false) return
2019-05-31 09:48:28 +00:00
// Already disliked this video
if (this.userRating === 'dislike') this.setRating('none')
else this.setRating('dislike')
2017-03-08 20:35:43 +00:00
}
getRatePopoverText () {
if (this.isUserLoggedIn()) return undefined
return this.i18n('You need to be connected to rate this content.')
}
showMoreDescription () {
if (this.completeVideoDescription === undefined) {
return this.loadCompleteDescription()
}
this.updateVideoDescription(this.completeVideoDescription)
this.completeDescriptionShown = true
}
showLessDescription () {
this.updateVideoDescription(this.shortVideoDescription)
this.completeDescriptionShown = false
}
loadCompleteDescription () {
this.descriptionLoading = true
this.videoService.loadCompleteDescription(this.video.descriptionPath)
.subscribe(
description => {
this.completeDescriptionShown = true
this.descriptionLoading = false
this.shortVideoDescription = this.video.description
this.completeVideoDescription = description
this.updateVideoDescription(this.completeVideoDescription)
},
error => {
this.descriptionLoading = false
this.notifier.error(error.message)
}
)
}
2018-02-20 15:13:05 +00:00
showSupportModal () {
2019-12-05 08:21:09 +00:00
this.pausePlayer()
2018-02-20 15:13:05 +00:00
this.videoSupportModal.show()
}
showShareModal () {
2019-12-05 08:21:09 +00:00
this.pausePlayer()
2019-03-07 16:06:00 +00:00
this.videoShareModal.show(this.currentTime)
2016-11-08 20:11:57 +00:00
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
2017-01-20 18:22:15 +00:00
}
2017-12-06 16:15:59 +00:00
getVideoTags () {
if (!this.video || Array.isArray(this.video.tags) === false) return []
return this.video.tags
2017-12-06 16:15:59 +00:00
}
onRecommendations (videos: Video[]) {
if (videos.length > 0) {
// Pick a random video until the recommendations are improved
this.nextVideoUuid = videos[randomInt(0,videos.length - 1)].uuid
}
}
2019-12-05 08:21:09 +00:00
onModalOpened () {
this.pausePlayer()
}
2019-04-05 08:52:27 +00:00
onVideoRemoved () {
this.redirectService.redirectToHomepage()
}
acceptedPrivacyConcern () {
peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
this.hasAlreadyAcceptedPrivacyConcern = true
}
isVideoToTranscode () {
return this.video && this.video.state.id === VideoState.TO_TRANSCODE
}
isVideoToImport () {
return this.video && this.video.state.id === VideoState.TO_IMPORT
}
hasVideoScheduledPublication () {
return this.video && this.video.scheduledUpdate !== undefined
}
2019-03-13 13:18:58 +00:00
isVideoBlur (video: Video) {
return video.isVideoNSFWForUser(this.user, this.serverService.getConfig())
}
isAutoPlayEnabled () {
return (
this.user && this.user.autoPlayNextVideo ||
peertubeSessionStorage.getItem(RecommendedVideosComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
)
}
isPlaylistAutoPlayEnabled () {
return (
this.user && this.user.autoPlayNextVideoPlaylist ||
peertubeSessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true'
)
}
2019-03-13 13:18:58 +00:00
private loadVideo (videoId: string) {
// Video did not change
if (this.video && this.video.uuid === videoId) return
if (this.player) this.player.pause()
2019-07-22 13:40:13 +00:00
const videoObs = this.hooks.wrapObsFun(
this.videoService.getVideo.bind(this.videoService),
{ videoId },
'video-watch',
'filter:api.video-watch.video.get.params',
'filter:api.video-watch.video.get.result'
)
2019-03-13 13:18:58 +00:00
// Video did change
2019-07-25 14:23:44 +00:00
forkJoin([
2019-07-22 13:40:13 +00:00
videoObs,
2019-03-13 13:18:58 +00:00
this.videoCaptionService.listCaptions(videoId)
2019-07-25 14:23:44 +00:00
])
2019-03-13 13:18:58 +00:00
.pipe(
// If 401, the video is private or blacklisted so redirect to 404
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ]))
)
.subscribe(([ video, captionsResult ]) => {
const queryParams = this.route.snapshot.queryParams
2019-05-31 09:48:28 +00:00
const urlOptions = {
startTime: queryParams.start,
stopTime: queryParams.stop,
2019-06-11 13:59:10 +00:00
muted: queryParams.muted,
loop: queryParams.loop,
2019-05-31 09:48:28 +00:00
subtitle: queryParams.subtitle,
2019-06-11 13:59:10 +00:00
playerMode: queryParams.mode,
peertubeLink: false
2019-05-31 09:48:28 +00:00
}
this.onVideoFetched(video, captionsResult.data, urlOptions)
2019-03-13 13:18:58 +00:00
.catch(err => this.handleError(err))
})
}
private loadPlaylist (playlistId: string) {
// Playlist did not change
if (this.playlist && this.playlist.uuid === playlistId) return
this.playlistService.getVideoPlaylist(playlistId)
.pipe(
// If 401, the video is private or blacklisted so redirect to 404
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ]))
)
.subscribe(playlist => {
this.playlist = playlist
const videoId = this.route.snapshot.queryParams['videoId']
this.videoWatchPlaylist.loadPlaylistElements(playlist, !videoId)
2019-03-13 13:18:58 +00:00
})
}
private updateVideoDescription (description: string) {
this.video.description = description
this.setVideoDescriptionHTML()
2019-05-31 09:48:28 +00:00
.catch(err => console.error(err))
}
2019-02-15 14:52:18 +00:00
private async setVideoDescriptionHTML () {
this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description)
}
2017-12-21 09:49:52 +00:00
private setVideoLikesBarTooltipText () {
this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', {
likesNumber: this.video.likes,
dislikesNumber: this.video.dislikes
})
2017-12-21 09:49:52 +00:00
}
2017-07-23 09:07:30 +00:00
private handleError (err: any) {
const errorMessage: string = typeof err === 'string' ? err : err.message
2018-02-26 08:55:23 +00:00
if (!errorMessage) return
// Display a message in the video player instead of a notification
if (errorMessage.indexOf('from xs param') !== -1) {
this.flushPlayer()
this.remoteServerDown = true
2018-07-17 15:06:34 +00:00
this.changeDetector.detectChanges()
return
2017-07-23 09:07:30 +00:00
}
this.notifier.error(errorMessage)
2017-07-23 09:07:30 +00:00
}
private checkUserRating () {
2017-03-08 20:35:43 +00:00
// Unlogged users do not have ratings
if (this.isUserLoggedIn() === false) return
2017-03-08 20:35:43 +00:00
this.videoService.getUserVideoRating(this.video.id)
.subscribe(
ratingObject => {
if (ratingObject) {
this.userRating = ratingObject.rating
}
},
err => this.notifier.error(err.message)
)
2017-03-08 20:35:43 +00:00
}
2019-02-07 14:56:17 +00:00
private async onVideoFetched (
video: VideoDetails,
videoCaptions: VideoCaption[],
2019-06-11 13:59:10 +00:00
urlOptions: CustomizationOptions & { playerMode: PlayerMode }
2019-02-07 14:56:17 +00:00
) {
this.video = video
2019-06-12 10:40:24 +00:00
this.videoCaptions = videoCaptions
2017-04-04 19:37:03 +00:00
// Re init attributes
this.descriptionLoading = false
this.completeDescriptionShown = false
this.remoteServerDown = false
2019-03-07 16:06:00 +00:00
this.currentTime = undefined
this.videoWatchPlaylist.updatePlaylistIndex(video)
2019-03-13 13:18:58 +00:00
if (this.isVideoBlur(this.video)) {
const res = await this.confirmService.confirm(
2018-05-31 16:12:15 +00:00
this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'),
this.i18n('Mature or explicit content')
)
if (res === false) return this.location.back()
2017-04-04 19:37:03 +00:00
}
2018-04-03 16:06:58 +00:00
// Flush old player if needed
this.flushPlayer()
2018-04-03 15:33:39 +00:00
// Build video element, because videojs removes it on dispose
2019-03-13 13:18:58 +00:00
const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper')
2018-04-03 15:33:39 +00:00
this.playerElement = document.createElement('video')
this.playerElement.className = 'video-js vjs-peertube-skin'
2018-05-22 07:16:05 +00:00
this.playerElement.setAttribute('playsinline', 'true')
2018-04-03 15:33:39 +00:00
playerElementWrapper.appendChild(this.playerElement)
2019-12-05 16:06:18 +00:00
const params = {
video: this.video,
videoCaptions,
urlOptions,
user: this.user
2018-06-06 12:23:40 +00:00
}
2019-12-05 16:06:18 +00:00
const { playerMode, playerOptions } = await this.hooks.wrapFun(
this.buildPlayerManagerOptions.bind(this),
params,
2019-12-05 16:26:58 +00:00
'video-watch',
'filter:internal.video-watch.player.build-options.params',
2019-12-05 16:06:18 +00:00
'filter:internal.video-watch.player.build-options.result'
)
2018-06-06 12:23:40 +00:00
this.zone.runOutsideAngular(async () => {
2019-12-05 16:06:18 +00:00
this.player = await PeertubePlayerManager.initialize(playerMode, playerOptions, player => this.player = player)
2019-11-28 10:06:02 +00:00
this.player.focus()
2019-03-18 09:26:53 +00:00
this.player.on('customError', ({ err }: { err: any }) => this.handleError(err))
2019-03-07 16:06:00 +00:00
this.player.on('timeupdate', () => {
this.currentTime = Math.floor(this.player.currentTime())
})
2019-03-13 13:18:58 +00:00
this.player.one('ended', () => {
if (this.playlist) {
if (this.isPlaylistAutoPlayEnabled()) this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo())
} else if (this.isAutoPlayEnabled()) {
this.zone.run(() => this.autoplayNext())
2019-03-13 13:18:58 +00:00
}
})
this.player.one('stopped', () => {
if (this.playlist) {
if (this.isPlaylistAutoPlayEnabled()) this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo())
2019-03-13 13:18:58 +00:00
}
})
2019-03-18 09:26:53 +00:00
this.player.on('theaterChange', (_: any, enabled: boolean) => {
this.zone.run(() => this.theaterEnabled = enabled)
})
this.hooks.runAction('action:video-watch.player.loaded', 'video-watch', { player: this.player })
2018-04-03 15:33:39 +00:00
})
this.setVideoDescriptionHTML()
this.setVideoLikesBarTooltipText()
this.setOpenGraphTags()
this.checkUserRating()
2019-07-22 13:40:13 +00:00
this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', { videojs })
2017-04-04 19:37:03 +00:00
}
private autoplayNext () {
if (this.nextVideoUuid) {
this.router.navigate([ '/videos/watch', this.nextVideoUuid ])
}
}
2018-11-14 14:01:28 +00:00
private setRating (nextRating: UserVideoRateType) {
2019-05-31 09:48:28 +00:00
const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = {
like: this.videoService.setVideoLike,
dislike: this.videoService.setVideoDislike,
none: this.videoService.unsetVideoLike
}
2019-05-31 09:48:28 +00:00
ratingMethods[nextRating].call(this.videoService, this.video.id)
.subscribe(
() => {
// Update the video like attribute
this.updateVideoRating(this.userRating, nextRating)
this.userRating = nextRating
},
(err: { message: string }) => this.notifier.error(err.message)
)
}
2018-11-14 14:01:28 +00:00
private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) {
let likesToIncrement = 0
let dislikesToIncrement = 0
2017-03-08 20:35:43 +00:00
if (oldRating) {
if (oldRating === 'like') likesToIncrement--
if (oldRating === 'dislike') dislikesToIncrement--
2017-03-08 20:35:43 +00:00
}
if (newRating === 'like') likesToIncrement++
if (newRating === 'dislike') dislikesToIncrement++
2017-03-08 20:35:43 +00:00
this.video.likes += likesToIncrement
this.video.dislikes += dislikesToIncrement
2018-02-28 08:49:40 +00:00
this.video.buildLikeAndDislikePercents()
2018-02-28 08:49:40 +00:00
this.setVideoLikesBarTooltipText()
2017-03-08 20:35:43 +00:00
}
private setOpenGraphTags () {
this.metaService.setTitle(this.video.name)
2017-03-10 09:33:36 +00:00
this.metaService.setTag('og:type', 'video')
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:title', this.video.name)
this.metaService.setTag('name', this.video.name)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:description', this.video.description)
this.metaService.setTag('description', this.video.description)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:image', this.video.previewPath)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:duration', this.video.duration.toString())
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:site_name', 'PeerTube')
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:url', window.location.href)
this.metaService.setTag('url', window.location.href)
2016-11-04 16:37:44 +00:00
}
2017-11-30 08:21:11 +00:00
private isAutoplay () {
// We'll jump to the thread id, so do not play the video
if (this.route.snapshot.params['threadId']) return false
// Otherwise true by default
if (!this.user) return true
// Be sure the autoPlay is set to false
return this.user.autoPlayVideo !== false
}
2018-04-03 16:06:58 +00:00
private flushPlayer () {
// Remove player if it exists
if (this.player) {
2019-05-16 14:55:34 +00:00
try {
this.player.dispose()
this.player = undefined
} catch (err) {
console.error('Cannot dispose player.', err)
}
2018-04-03 16:06:58 +00:00
}
}
2019-12-05 16:06:18 +00:00
private buildPlayerManagerOptions (params: {
video: VideoDetails,
videoCaptions: VideoCaption[],
urlOptions: CustomizationOptions & { playerMode: PlayerMode },
user?: AuthUser
}) {
const { video, videoCaptions, urlOptions, user } = params
const getStartTime = () => {
const byUrl = urlOptions.startTime !== undefined
const byHistory = video.userHistory && !this.playlist
if (byUrl) {
return timeToInt(urlOptions.startTime)
} else if (byHistory) {
return video.userHistory.currentTime
} else {
return 0
}
}
2019-12-05 16:06:18 +00:00
let startTime = getStartTime()
2019-12-05 16:06:18 +00:00
// If we are at the end of the video, reset the timer
if (video.duration - startTime <= 1) startTime = 0
const playerCaptions = videoCaptions.map(c => ({
label: c.language.label,
language: c.language.id,
src: environment.apiUrl + c.captionPath
}))
const options: PeertubePlayerManagerOptions = {
common: {
autoplay: this.isAutoplay(),
playerElement: this.playerElement,
onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,
videoDuration: video.duration,
enableHotkeys: true,
inactivityTimeout: 2500,
poster: video.previewUrl,
startTime,
stopTime: urlOptions.stopTime,
controls: urlOptions.controls,
muted: urlOptions.muted,
loop: urlOptions.loop,
subtitle: urlOptions.subtitle,
peertubeLink: urlOptions.peertubeLink,
theaterButton: true,
captions: videoCaptions.length !== 0,
videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
? this.videoService.getVideoViewUrl(video.uuid)
: null,
embedUrl: video.embedUrl,
language: this.localeId,
userWatching: user && user.videosHistoryEnabled === true ? {
url: this.videoService.getUserWatchingVideoUrl(video.uuid),
authorizationHeader: this.authService.getRequestHeaderValue()
} : undefined,
serverUrl: environment.apiUrl,
videoCaptions: playerCaptions
},
webtorrent: {
videoFiles: video.files
}
}
let mode: PlayerMode
if (urlOptions.playerMode) {
if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader'
else mode = 'webtorrent'
} else {
if (video.hasHlsPlaylist()) mode = 'p2p-media-loader'
else mode = 'webtorrent'
}
if (mode === 'p2p-media-loader') {
const hlsPlaylist = video.getHlsPlaylist()
const p2pMediaLoader = {
playlistUrl: hlsPlaylist.playlistUrl,
segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
trackerAnnounce: video.trackerUrls,
videoFiles: hlsPlaylist.files
} as P2PMediaLoaderOptions
Object.assign(options, { p2pMediaLoader })
}
return { playerMode: mode, playerOptions: options }
}
2019-12-05 08:21:09 +00:00
private pausePlayer () {
if (!this.player) return
this.player.pause()
}
2019-12-06 08:55:36 +00:00
private initHotkeys () {
this.hotkeys = [
// These hotkeys are managed by the player
new Hotkey('f', e => e, undefined, this.i18n('Enter/exit fullscreen (requires player focus)')),
new Hotkey('space', e => e, undefined, this.i18n('Play/Pause the video (requires player focus)')),
new Hotkey('m', e => e, undefined, this.i18n('Mute/unmute the video (requires player focus)')),
new Hotkey('0-9', e => e, undefined, this.i18n('Skip to a percentage of the video: 0 is 0% and 9 is 90% (requires player focus)')),
new Hotkey('up', e => e, undefined, this.i18n('Increase the volume (requires player focus)')),
new Hotkey('down', e => e, undefined, this.i18n('Decrease the volume (requires player focus)')),
new Hotkey('right', e => e, undefined, this.i18n('Seek the video forward (requires player focus)')),
new Hotkey('left', e => e, undefined, this.i18n('Seek the video backward (requires player focus)')),
new Hotkey('>', e => e, undefined, this.i18n('Increase playback rate (requires player focus)')),
new Hotkey('<', e => e, undefined, this.i18n('Decrease playback rate (requires player focus)')),
new Hotkey('.', e => e, undefined, this.i18n('Navigate in the video frame by frame (requires player focus)'))
]
if (this.isUserLoggedIn()) {
this.hotkeys = this.hotkeys.concat([
new Hotkey('shift+l', () => {
this.setLike()
return false
}, undefined, this.i18n('Like the video')),
new Hotkey('shift+d', () => {
this.setDislike()
return false
}, undefined, this.i18n('Dislike the video')),
new Hotkey('shift+s', () => {
this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe()
return false
}, undefined, this.i18n('Subscribe to the account'))
])
}
this.hotkeysService.add(this.hotkeys)
2019-12-06 08:55:36 +00:00
}
2016-03-14 12:50:19 +00:00
}