1 import { VideoFile } from '@shared/models'
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
7 function isWebRTCDisabled () {
8 return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
12 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
16 // Detect iPad Desktop mode
17 return !!(navigator.maxTouchPoints &&
18 navigator.maxTouchPoints > 2 &&
19 /MacIntel/.test(navigator.platform))
22 function isSafari () {
23 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
26 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
27 // Don't import all Angular stuff, just copy the code with shame
28 const dictionaryBytes: Array<{max: number, type: string}> = [
29 { max: 1024, type: 'B' },
30 { max: 1048576, type: 'KB' },
31 { max: 1073741824, type: 'MB' },
32 { max: 1.0995116e12, type: 'GB' }
34 function bytes (value: number) {
35 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
36 const calc = Math.floor(value / (format.max / 1024)).toString()
38 return [ calc, format.type ]
41 function isMobile () {
42 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
45 function buildVideoLink (options: {
59 warningTitle?: boolean
61 peertubeLink?: boolean
63 const { baseUrl } = options
67 : window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
69 const params = generateParams(window.location.search)
71 if (options.startTime !== undefined && options.startTime !== null) {
72 const startTimeInt = Math.floor(options.startTime)
73 params.set('start', secondsToTime(startTimeInt))
76 if (options.stopTime) {
77 const stopTimeInt = Math.floor(options.stopTime)
78 params.set('stop', secondsToTime(stopTimeInt))
81 if (options.subtitle) params.set('subtitle', options.subtitle)
83 if (options.loop === true) params.set('loop', '1')
84 if (options.autoplay === true) params.set('autoplay', '1')
85 if (options.muted === true) params.set('muted', '1')
86 if (options.title === false) params.set('title', '0')
87 if (options.warningTitle === false) params.set('warningTitle', '0')
88 if (options.controls === false) params.set('controls', '0')
89 if (options.peertubeLink === false) params.set('peertubeLink', '0')
91 return buildUrl(url, params)
94 function buildPlaylistLink (options: {
97 playlistPosition: number
99 const { baseUrl } = options
103 : window.location.origin + window.location.pathname.replace('/video-playlists/embed/', '/videos/watch/playlist/')
105 const params = generateParams(window.location.search)
107 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
109 return buildUrl(url, params)
112 function buildUrl (url: string, params: URLSearchParams) {
113 let hasParams = false
114 params.forEach(() => hasParams = true)
116 if (hasParams) return url + '?' + params.toString()
121 function generateParams (url: string) {
122 const params = new URLSearchParams(window.location.search)
123 // Unused parameters in embed
124 params.delete('videoId')
125 params.delete('resume')
130 function timeToInt (time: number | string) {
132 if (typeof time === 'number') return time
134 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
135 const matches = time.match(reg)
137 if (!matches) return 0
139 const hours = parseInt(matches[2] || '0', 10)
140 const minutes = parseInt(matches[4] || '0', 10)
141 const seconds = parseInt(matches[6] || '0', 10)
143 return hours * 3600 + minutes * 60 + seconds
146 function secondsToTime (seconds: number, full = false, symbol?: string) {
149 if (seconds === 0 && !full) return '0s'
151 const hourSymbol = (symbol || 'h')
152 const minuteSymbol = (symbol || 'm')
153 const secondsSymbol = full ? '' : 's'
155 const hours = Math.floor(seconds / 3600)
156 if (hours >= 1) time = hours + hourSymbol
157 else if (full) time = '0' + hourSymbol
160 const minutes = Math.floor(seconds / 60)
161 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
162 else if (minutes >= 1) time += minutes + minuteSymbol
163 else if (full) time += '00' + minuteSymbol
166 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
167 else if (seconds >= 1) time += seconds + secondsSymbol
168 else if (full) time += '00'
173 function buildVideoOrPlaylistEmbed (embedUrl: string) {
174 return '<iframe width="560" height="315" ' +
175 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
176 'src="' + embedUrl + '" ' +
177 'frameborder="0" allowfullscreen>' +
181 function videoFileMaxByResolution (files: VideoFile[]) {
184 for (let i = 1; i < files.length; i++) {
185 const file = files[i]
186 if (max.resolution.id < file.resolution.id) max = file
192 function videoFileMinByResolution (files: VideoFile[]) {
195 for (let i = 1; i < files.length; i++) {
196 const file = files[i]
197 if (min.resolution.id > file.resolution.id) min = file
203 function getRtcConfig () {
207 urls: 'stun:stun.stunprotocol.org'
210 urls: 'stun:stun.framasoft.org'
216 // ---------------------------------------------------------------------------
226 buildVideoOrPlaylistEmbed,
227 videoFileMaxByResolution,
228 videoFileMinByResolution,