2023-07-31 14:34:36 +02:00
|
|
|
import { HTMLServerConfig, Video, VideoFile } from '@peertube/peertube-models'
|
2018-06-06 14:23:40 +02:00
|
|
|
|
2018-03-30 17:40:00 +02:00
|
|
|
function toTitleCase (str: string) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:23:18 +02:00
|
|
|
function isWebRTCDisabled () {
|
|
|
|
return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
|
|
|
|
}
|
|
|
|
|
2021-12-15 15:58:10 +01:00
|
|
|
function isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) {
|
|
|
|
if (video.isLocal && config.tracker.enabled === false) return false
|
|
|
|
if (isWebRTCDisabled()) return false
|
|
|
|
|
|
|
|
return userP2PEnabled
|
|
|
|
}
|
|
|
|
|
2020-05-12 10:32:56 +02:00
|
|
|
function isIOS () {
|
2020-07-31 16:08:52 +02:00
|
|
|
if (/iPad|iPhone|iPod/.test(navigator.platform)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect iPad Desktop mode
|
2020-08-03 16:07:22 +02:00
|
|
|
return !!(navigator.maxTouchPoints &&
|
2020-07-31 16:08:52 +02:00
|
|
|
navigator.maxTouchPoints > 2 &&
|
2021-08-17 14:42:53 +02:00
|
|
|
navigator.platform.includes('MacIntel'))
|
2020-05-12 10:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSafari () {
|
|
|
|
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:40:00 +02:00
|
|
|
// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
|
|
|
|
// Don't import all Angular stuff, just copy the code with shame
|
2023-05-24 15:27:15 +02:00
|
|
|
const dictionaryBytes: { max: number, type: string }[] = [
|
2018-03-30 17:40:00 +02:00
|
|
|
{ max: 1024, type: 'B' },
|
|
|
|
{ max: 1048576, type: 'KB' },
|
|
|
|
{ max: 1073741824, type: 'MB' },
|
|
|
|
{ max: 1.0995116e12, type: 'GB' }
|
|
|
|
]
|
2018-10-18 14:35:31 +02:00
|
|
|
function bytes (value: number) {
|
2018-03-30 17:40:00 +02:00
|
|
|
const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
|
|
|
|
const calc = Math.floor(value / (format.max / 1024)).toString()
|
|
|
|
|
|
|
|
return [ calc, format.type ]
|
|
|
|
}
|
|
|
|
|
2018-05-22 16:02:29 +02:00
|
|
|
function isMobile () {
|
|
|
|
return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:50:33 +02:00
|
|
|
function videoFileMaxByResolution (files: VideoFile[]) {
|
|
|
|
let max = files[0]
|
|
|
|
|
|
|
|
for (let i = 1; i < files.length; i++) {
|
|
|
|
const file = files[i]
|
|
|
|
if (max.resolution.id < file.resolution.id) max = file
|
|
|
|
}
|
|
|
|
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
function videoFileMinByResolution (files: VideoFile[]) {
|
|
|
|
let min = files[0]
|
|
|
|
|
|
|
|
for (let i = 1; i < files.length; i++) {
|
|
|
|
const file = files[i]
|
|
|
|
if (min.resolution.id > file.resolution.id) min = file
|
|
|
|
}
|
|
|
|
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:37:25 +01:00
|
|
|
function getRtcConfig () {
|
|
|
|
return {
|
|
|
|
iceServers: [
|
|
|
|
{
|
|
|
|
urls: 'stun:stun.stunprotocol.org'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urls: 'stun:stun.framasoft.org'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 09:46:58 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-03-30 17:40:00 +02:00
|
|
|
export {
|
2019-01-29 08:37:25 +01:00
|
|
|
getRtcConfig,
|
2018-03-30 17:40:00 +02:00
|
|
|
toTitleCase,
|
2019-04-10 09:23:18 +02:00
|
|
|
isWebRTCDisabled,
|
2021-12-15 15:58:10 +01:00
|
|
|
isP2PEnabled,
|
2021-07-26 14:12:39 +02:00
|
|
|
|
2018-06-07 16:50:33 +02:00
|
|
|
videoFileMaxByResolution,
|
|
|
|
videoFileMinByResolution,
|
2018-05-22 16:02:29 +02:00
|
|
|
isMobile,
|
2020-05-12 10:32:56 +02:00
|
|
|
bytes,
|
|
|
|
isIOS,
|
|
|
|
isSafari
|
2018-03-30 17:40:00 +02:00
|
|
|
}
|