]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
Fix stuck hls player with bad redundancy
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
a9bfa85d 1import { HTMLServerConfig, Video, VideoFile } from '@shared/models'
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
a9bfa85d
C
11function 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
3e2bc4ea 18function isIOS () {
b6a8cfc5
C
19 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
20 return true
21 }
22
23 // Detect iPad Desktop mode
b12ce2b8 24 return !!(navigator.maxTouchPoints &&
b6a8cfc5 25 navigator.maxTouchPoints > 2 &&
9df52d66 26 navigator.platform.includes('MacIntel'))
3e2bc4ea
C
27}
28
29function isSafari () {
30 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
31}
32
c6352f2c
C
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
35const 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]
c199c427 41function bytes (value: number) {
c6352f2c
C
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
d1bd87e0
C
48function isMobile () {
49 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
50}
51
4097c6d6 52function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) {
bdb1dfc1
C
53 const iframe = document.createElement('iframe')
54
55 iframe.title = embedTitle
926bf549
C
56 iframe.width = '560'
57 iframe.height = '315'
bdb1dfc1
C
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
960a11e8
C
64}
65
6cca7360
C
66function 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
77function 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
09209296
C
88function 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
7b3a99d5
C
101// ---------------------------------------------------------------------------
102
c6352f2c 103export {
09209296 104 getRtcConfig,
c6352f2c 105 toTitleCase,
31b6ddf8 106 isWebRTCDisabled,
a9bfa85d 107 isP2PEnabled,
9162fdd3 108
951b582f 109 buildVideoOrPlaylistEmbed,
6cca7360
C
110 videoFileMaxByResolution,
111 videoFileMinByResolution,
d1bd87e0 112 isMobile,
3e2bc4ea
C
113 bytes,
114 isIOS,
115 isSafari
c6352f2c 116}