]>
Commit | Line | Data |
---|---|---|
1 | import { HTMLServerConfig, Video, 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 isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) { | |
12 | if (video.isLocal && config.tracker.enabled === false) return false | |
13 | if (isWebRTCDisabled()) return false | |
14 | ||
15 | return userP2PEnabled | |
16 | } | |
17 | ||
18 | function isIOS () { | |
19 | if (/iPad|iPhone|iPod/.test(navigator.platform)) { | |
20 | return true | |
21 | } | |
22 | ||
23 | // Detect iPad Desktop mode | |
24 | return !!(navigator.maxTouchPoints && | |
25 | navigator.maxTouchPoints > 2 && | |
26 | navigator.platform.includes('MacIntel')) | |
27 | } | |
28 | ||
29 | function isSafari () { | |
30 | return /^((?!chrome|android).)*safari/i.test(navigator.userAgent) | |
31 | } | |
32 | ||
33 | // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts | |
34 | // Don't import all Angular stuff, just copy the code with shame | |
35 | const dictionaryBytes: Array<{max: number, type: string}> = [ | |
36 | { max: 1024, type: 'B' }, | |
37 | { max: 1048576, type: 'KB' }, | |
38 | { max: 1073741824, type: 'MB' }, | |
39 | { max: 1.0995116e12, type: 'GB' } | |
40 | ] | |
41 | function bytes (value: number) { | |
42 | const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] | |
43 | const calc = Math.floor(value / (format.max / 1024)).toString() | |
44 | ||
45 | return [ calc, format.type ] | |
46 | } | |
47 | ||
48 | function isMobile () { | |
49 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | |
50 | } | |
51 | ||
52 | function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) { | |
53 | const iframe = document.createElement('iframe') | |
54 | ||
55 | iframe.title = embedTitle | |
56 | iframe.width = '560' | |
57 | iframe.height = '315' | |
58 | iframe.src = embedUrl | |
59 | iframe.frameBorder = '0' | |
60 | iframe.allowFullscreen = true | |
61 | iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups') | |
62 | ||
63 | return iframe.outerHTML | |
64 | } | |
65 | ||
66 | function videoFileMaxByResolution (files: VideoFile[]) { | |
67 | let max = files[0] | |
68 | ||
69 | for (let i = 1; i < files.length; i++) { | |
70 | const file = files[i] | |
71 | if (max.resolution.id < file.resolution.id) max = file | |
72 | } | |
73 | ||
74 | return max | |
75 | } | |
76 | ||
77 | function videoFileMinByResolution (files: VideoFile[]) { | |
78 | let min = files[0] | |
79 | ||
80 | for (let i = 1; i < files.length; i++) { | |
81 | const file = files[i] | |
82 | if (min.resolution.id > file.resolution.id) min = file | |
83 | } | |
84 | ||
85 | return min | |
86 | } | |
87 | ||
88 | function getRtcConfig () { | |
89 | return { | |
90 | iceServers: [ | |
91 | { | |
92 | urls: 'stun:stun.stunprotocol.org' | |
93 | }, | |
94 | { | |
95 | urls: 'stun:stun.framasoft.org' | |
96 | } | |
97 | ] | |
98 | } | |
99 | } | |
100 | ||
101 | // --------------------------------------------------------------------------- | |
102 | ||
103 | export { | |
104 | getRtcConfig, | |
105 | toTitleCase, | |
106 | isWebRTCDisabled, | |
107 | isP2PEnabled, | |
108 | ||
109 | buildVideoOrPlaylistEmbed, | |
110 | videoFileMaxByResolution, | |
111 | videoFileMinByResolution, | |
112 | isMobile, | |
113 | bytes, | |
114 | isIOS, | |
115 | isSafari | |
116 | } |