]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/utils.ts
cbca1065f9131cbdf8d738dcf5d9001b6cf45ecc
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / 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 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 navigator.platform.includes('MacIntel'))
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 buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) {
46 const iframe = document.createElement('iframe')
47
48 iframe.title = embedTitle
49 iframe.src = embedUrl
50 iframe.frameBorder = '0'
51 iframe.allowFullscreen = true
52 iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups')
53
54 return iframe.outerHTML
55 }
56
57 function videoFileMaxByResolution (files: VideoFile[]) {
58 let max = files[0]
59
60 for (let i = 1; i < files.length; i++) {
61 const file = files[i]
62 if (max.resolution.id < file.resolution.id) max = file
63 }
64
65 return max
66 }
67
68 function videoFileMinByResolution (files: VideoFile[]) {
69 let min = files[0]
70
71 for (let i = 1; i < files.length; i++) {
72 const file = files[i]
73 if (min.resolution.id > file.resolution.id) min = file
74 }
75
76 return min
77 }
78
79 function getRtcConfig () {
80 return {
81 iceServers: [
82 {
83 urls: 'stun:stun.stunprotocol.org'
84 },
85 {
86 urls: 'stun:stun.framasoft.org'
87 }
88 ]
89 }
90 }
91
92 // ---------------------------------------------------------------------------
93
94 export {
95 getRtcConfig,
96 toTitleCase,
97 isWebRTCDisabled,
98
99 buildVideoOrPlaylistEmbed,
100 videoFileMaxByResolution,
101 videoFileMinByResolution,
102 isMobile,
103 bytes,
104 isIOS,
105 isSafari
106 }