]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/utils.ts
81fe689514e057183b344fa75d7d1785378e1050
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
1 import { VideoFile } from '../../../../shared/models/videos'
2
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
5 }
6
7 function isWebRTCDisabled () {
8 return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
9 }
10
11 function isIOS () {
12 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
13 return true
14 }
15
16 // Detect iPad Desktop mode
17 return navigator.maxTouchPoints &&
18 navigator.maxTouchPoints > 2 &&
19 /MacIntel/.test(navigator.platform)
20 }
21
22 function isSafari () {
23 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
24 }
25
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' }
33 ]
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()
37
38 return [ calc, format.type ]
39 }
40
41 function isMobile () {
42 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
43 }
44
45 function buildVideoLink (options: {
46 baseUrl?: string,
47
48 startTime?: number,
49 stopTime?: number,
50
51 subtitle?: string,
52
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)
69 // Remove these unused parameters when we are on a playlist page
70 params.delete('videoId')
71 params.delete('resume')
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))
81 }
82
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
97 return url
98 }
99
100 function timeToInt (time: number | string) {
101 if (!time) return 0
102 if (typeof time === 'number') return time
103
104 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
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
116 function secondsToTime (seconds: number, full = false, symbol?: string) {
117 let time = ''
118
119 const hourSymbol = (symbol || 'h')
120 const minuteSymbol = (symbol || 'm')
121 const secondsSymbol = full ? '' : 's'
122
123 const hours = Math.floor(seconds / 3600)
124 if (hours >= 1) time = hours + hourSymbol
125 else if (full) time = '0' + hourSymbol
126
127 seconds %= 3600
128 const minutes = Math.floor(seconds / 60)
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
132
133 seconds %= 60
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'
137
138 return time
139 }
140
141 function buildVideoEmbed (embedUrl: string) {
142 return '<iframe width="560" height="315" ' +
143 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
144 'src="' + embedUrl + '" ' +
145 'frameborder="0" allowfullscreen>' +
146 '</iframe>'
147 }
148
149 function 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
161 function 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
172 function 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
183 function 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
196 // ---------------------------------------------------------------------------
197
198 export {
199 getRtcConfig,
200 toTitleCase,
201 timeToInt,
202 secondsToTime,
203 isWebRTCDisabled,
204 buildVideoLink,
205 buildVideoEmbed,
206 videoFileMaxByResolution,
207 videoFileMinByResolution,
208 copyToClipboard,
209 isMobile,
210 bytes,
211 isIOS,
212 isSafari
213 }