1 import { HTMLServerConfig, Video, VideoFile } from '@shared/models'
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
7 function isWebRTCDisabled () {
8 return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
11 function isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) {
12 if (video.isLocal && config.tracker.enabled === false) return false
13 if (isWebRTCDisabled()) return false
19 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
23 // Detect iPad Desktop mode
24 return !!(navigator.maxTouchPoints &&
25 navigator.maxTouchPoints > 2 &&
26 navigator.platform.includes('MacIntel'))
29 function isSafari () {
30 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
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' }
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()
45 return [ calc, format.type ]
48 function isMobile () {
49 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
52 function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) {
53 const iframe = document.createElement('iframe')
55 iframe.title = embedTitle
59 iframe.frameBorder = '0'
60 iframe.allowFullscreen = true
61 iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups')
63 return iframe.outerHTML
66 function videoFileMaxByResolution (files: VideoFile[]) {
69 for (let i = 1; i < files.length; i++) {
71 if (max.resolution.id < file.resolution.id) max = file
77 function videoFileMinByResolution (files: VideoFile[]) {
80 for (let i = 1; i < files.length; i++) {
82 if (min.resolution.id > file.resolution.id) min = file
88 function getRtcConfig () {
92 urls: 'stun:stun.stunprotocol.org'
95 urls: 'stun:stun.framasoft.org'
101 // ---------------------------------------------------------------------------
109 buildVideoOrPlaylistEmbed,
110 videoFileMaxByResolution,
111 videoFileMinByResolution,