]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/shared/common/utils.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / shared / common / utils.ts
1 import { VideoFile } from '@shared/models'
2
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
5 }
6
7 const dictionaryBytes = [
8 { max: 1024, type: 'B', decimals: 0 },
9 { max: 1048576, type: 'KB', decimals: 0 },
10 { max: 1073741824, type: 'MB', decimals: 0 },
11 { max: 1.0995116e12, type: 'GB', decimals: 1 }
12 ]
13 function bytes (value: number) {
14 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
15 const calc = (value / (format.max / 1024)).toFixed(format.decimals)
16
17 return [ calc, format.type ]
18 }
19
20 function videoFileMaxByResolution (files: VideoFile[]) {
21 let max = files[0]
22
23 for (let i = 1; i < files.length; i++) {
24 const file = files[i]
25 if (max.resolution.id < file.resolution.id) max = file
26 }
27
28 return max
29 }
30
31 function videoFileMinByResolution (files: VideoFile[]) {
32 let min = files[0]
33
34 for (let i = 1; i < files.length; i++) {
35 const file = files[i]
36 if (min.resolution.id > file.resolution.id) min = file
37 }
38
39 return min
40 }
41
42 function getRtcConfig () {
43 return {
44 iceServers: [
45 {
46 urls: 'stun:stun.stunprotocol.org'
47 },
48 {
49 urls: 'stun:stun.framasoft.org'
50 }
51 ]
52 }
53 }
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 getRtcConfig,
59 toTitleCase,
60
61 videoFileMaxByResolution,
62 videoFileMinByResolution,
63 bytes
64 }