]>
Commit | Line | Data |
---|---|---|
15a7eafb | 1 | import { VideoFile } from '@shared/models' |
e945b184 | 2 | |
c6352f2c C |
3 | function toTitleCase (str: string) { |
4 | return str.charAt(0).toUpperCase() + str.slice(1) | |
5 | } | |
6 | ||
31b6ddf8 C |
7 | function isWebRTCDisabled () { |
8 | return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false | |
9 | } | |
10 | ||
3e2bc4ea | 11 | function isIOS () { |
b6a8cfc5 C |
12 | if (/iPad|iPhone|iPod/.test(navigator.platform)) { |
13 | return true | |
14 | } | |
15 | ||
16 | // Detect iPad Desktop mode | |
b12ce2b8 | 17 | return !!(navigator.maxTouchPoints && |
b6a8cfc5 | 18 | navigator.maxTouchPoints > 2 && |
9df52d66 | 19 | navigator.platform.includes('MacIntel')) |
3e2bc4ea C |
20 | } |
21 | ||
22 | function isSafari () { | |
23 | return /^((?!chrome|android).)*safari/i.test(navigator.userAgent) | |
24 | } | |
25 | ||
c6352f2c C |
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 | ] | |
c199c427 | 34 | function bytes (value: number) { |
c6352f2c C |
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 | ||
d1bd87e0 C |
41 | function isMobile () { |
42 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | |
43 | } | |
44 | ||
4097c6d6 | 45 | function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) { |
bdb1dfc1 C |
46 | const iframe = document.createElement('iframe') |
47 | ||
48 | iframe.title = embedTitle | |
926bf549 C |
49 | iframe.width = '560' |
50 | iframe.height = '315' | |
bdb1dfc1 C |
51 | iframe.src = embedUrl |
52 | iframe.frameBorder = '0' | |
53 | iframe.allowFullscreen = true | |
54 | iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups') | |
55 | ||
56 | return iframe.outerHTML | |
960a11e8 C |
57 | } |
58 | ||
6cca7360 C |
59 | function videoFileMaxByResolution (files: VideoFile[]) { |
60 | let max = files[0] | |
61 | ||
62 | for (let i = 1; i < files.length; i++) { | |
63 | const file = files[i] | |
64 | if (max.resolution.id < file.resolution.id) max = file | |
65 | } | |
66 | ||
67 | return max | |
68 | } | |
69 | ||
70 | function videoFileMinByResolution (files: VideoFile[]) { | |
71 | let min = files[0] | |
72 | ||
73 | for (let i = 1; i < files.length; i++) { | |
74 | const file = files[i] | |
75 | if (min.resolution.id > file.resolution.id) min = file | |
76 | } | |
77 | ||
78 | return min | |
79 | } | |
80 | ||
09209296 C |
81 | function getRtcConfig () { |
82 | return { | |
83 | iceServers: [ | |
84 | { | |
85 | urls: 'stun:stun.stunprotocol.org' | |
86 | }, | |
87 | { | |
88 | urls: 'stun:stun.framasoft.org' | |
89 | } | |
90 | ] | |
91 | } | |
92 | } | |
93 | ||
7b3a99d5 C |
94 | // --------------------------------------------------------------------------- |
95 | ||
c6352f2c | 96 | export { |
09209296 | 97 | getRtcConfig, |
c6352f2c | 98 | toTitleCase, |
31b6ddf8 | 99 | isWebRTCDisabled, |
9162fdd3 | 100 | |
951b582f | 101 | buildVideoOrPlaylistEmbed, |
6cca7360 C |
102 | videoFileMaxByResolution, |
103 | videoFileMinByResolution, | |
d1bd87e0 | 104 | isMobile, |
3e2bc4ea C |
105 | bytes, |
106 | isIOS, | |
107 | isSafari | |
c6352f2c | 108 | } |