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