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