]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
Upgrade to angular 10
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
583eb04b 1import { VideoFile } from '@shared/models'
e945b184 2
c6352f2c
C
3function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
5}
6
31b6ddf8
C
7function isWebRTCDisabled () {
8 return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
9}
10
3e2bc4ea 11function isIOS () {
b6a8cfc5
C
12 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
13 return true
14 }
15
16 // Detect iPad Desktop mode
b12ce2b8 17 return !!(navigator.maxTouchPoints &&
b6a8cfc5 18 navigator.maxTouchPoints > 2 &&
b12ce2b8 19 /MacIntel/.test(navigator.platform))
3e2bc4ea
C
20}
21
22function isSafari () {
23 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
24}
25
c6352f2c
C
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
28const 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' }
33]
c199c427 34function bytes (value: number) {
c6352f2c
C
35 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
36 const calc = Math.floor(value / (format.max / 1024)).toString()
37
38 return [ calc, format.type ]
39}
40
d1bd87e0
C
41function isMobile () {
42 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
43}
44
2f4c784a
C
45function buildVideoLink (options: {
46 baseUrl?: string,
1f6824c9 47
2f4c784a
C
48 startTime?: number,
49 stopTime?: number,
960a11e8 50
2f4c784a 51 subtitle?: string,
1f6824c9 52
2f4c784a
C
53 loop?: boolean,
54 autoplay?: boolean,
55 muted?: boolean,
56
57 // Embed options
58 title?: boolean,
59 warningTitle?: boolean,
60 controls?: boolean
61} = {}) {
62 const { baseUrl } = options
63
64 const url = baseUrl
65 ? baseUrl
66 : window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
67
68 const params = new URLSearchParams(window.location.search)
52891376 69 // Remove these unused parameters when we are on a playlist page
3a1fed11 70 params.delete('videoId')
52891376 71 params.delete('resume')
2f4c784a
C
72
73 if (options.startTime) {
74 const startTimeInt = Math.floor(options.startTime)
75 params.set('start', secondsToTime(startTimeInt))
76 }
77
78 if (options.stopTime) {
79 const stopTimeInt = Math.floor(options.stopTime)
80 params.set('stop', secondsToTime(stopTimeInt))
960a11e8
C
81 }
82
2f4c784a
C
83 if (options.subtitle) params.set('subtitle', options.subtitle)
84
85 if (options.loop === true) params.set('loop', '1')
86 if (options.autoplay === true) params.set('autoplay', '1')
87 if (options.muted === true) params.set('muted', '1')
88 if (options.title === false) params.set('title', '0')
89 if (options.warningTitle === false) params.set('warningTitle', '0')
90 if (options.controls === false) params.set('controls', '0')
91
92 let hasParams = false
93 params.forEach(() => hasParams = true)
94
95 if (hasParams) return url + '?' + params.toString()
96
11b8762f 97 return url
1f6824c9
C
98}
99
100function timeToInt (time: number | string) {
3b019808 101 if (!time) return 0
1f6824c9
C
102 if (typeof time === 'number') return time
103
f0a39880 104 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
1f6824c9
C
105 const matches = time.match(reg)
106
107 if (!matches) return 0
108
109 const hours = parseInt(matches[2] || '0', 10)
110 const minutes = parseInt(matches[4] || '0', 10)
111 const seconds = parseInt(matches[6] || '0', 10)
112
113 return hours * 3600 + minutes * 60 + seconds
114}
115
f0a39880 116function secondsToTime (seconds: number, full = false, symbol?: string) {
1f6824c9
C
117 let time = ''
118
f0a39880
C
119 const hourSymbol = (symbol || 'h')
120 const minuteSymbol = (symbol || 'm')
121 const secondsSymbol = full ? '' : 's'
122
c4710631 123 const hours = Math.floor(seconds / 3600)
f0a39880
C
124 if (hours >= 1) time = hours + hourSymbol
125 else if (full) time = '0' + hourSymbol
1f6824c9
C
126
127 seconds %= 3600
c4710631 128 const minutes = Math.floor(seconds / 60)
f0a39880
C
129 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
130 else if (minutes >= 1) time += minutes + minuteSymbol
131 else if (full) time += '00' + minuteSymbol
1f6824c9
C
132
133 seconds %= 60
f0a39880
C
134 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
135 else if (seconds >= 1) time += seconds + secondsSymbol
136 else if (full) time += '00'
1f6824c9
C
137
138 return time
960a11e8
C
139}
140
141function buildVideoEmbed (embedUrl: string) {
142 return '<iframe width="560" height="315" ' +
f2aa2c3c 143 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
960a11e8
C
144 'src="' + embedUrl + '" ' +
145 'frameborder="0" allowfullscreen>' +
146 '</iframe>'
147}
148
149function copyToClipboard (text: string) {
150 const el = document.createElement('textarea')
151 el.value = text
152 el.setAttribute('readonly', '')
153 el.style.position = 'absolute'
154 el.style.left = '-9999px'
155 document.body.appendChild(el)
156 el.select()
157 document.execCommand('copy')
158 document.body.removeChild(el)
159}
160
6cca7360
C
161function videoFileMaxByResolution (files: VideoFile[]) {
162 let max = files[0]
163
164 for (let i = 1; i < files.length; i++) {
165 const file = files[i]
166 if (max.resolution.id < file.resolution.id) max = file
167 }
168
169 return max
170}
171
172function videoFileMinByResolution (files: VideoFile[]) {
173 let min = files[0]
174
175 for (let i = 1; i < files.length; i++) {
176 const file = files[i]
177 if (min.resolution.id > file.resolution.id) min = file
178 }
179
180 return min
181}
182
09209296
C
183function getRtcConfig () {
184 return {
185 iceServers: [
186 {
187 urls: 'stun:stun.stunprotocol.org'
188 },
189 {
190 urls: 'stun:stun.framasoft.org'
191 }
192 ]
193 }
194}
195
7b3a99d5
C
196// ---------------------------------------------------------------------------
197
c6352f2c 198export {
09209296 199 getRtcConfig,
c6352f2c 200 toTitleCase,
1f6824c9 201 timeToInt,
f0a39880 202 secondsToTime,
31b6ddf8 203 isWebRTCDisabled,
960a11e8 204 buildVideoLink,
960a11e8 205 buildVideoEmbed,
6cca7360
C
206 videoFileMaxByResolution,
207 videoFileMinByResolution,
960a11e8 208 copyToClipboard,
d1bd87e0 209 isMobile,
3e2bc4ea
C
210 bytes,
211 isIOS,
212 isSafari
c6352f2c 213}