]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/utils.ts
Better display redundancy pies
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
index b7cd40aa28e9155577ed49b1f270e1f3b92a33ec..7e25e3067d3ef9ba37357ff2daa59b0cd51d9187 100644 (file)
-import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
-import { VideoFile } from '../../../../shared/models/videos'
+import { HTMLServerConfig, Video, VideoFile } from '@shared/models'
 
 function toTitleCase (str: string) {
   return str.charAt(0).toUpperCase() + str.slice(1)
 }
 
-// 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
-const dictionaryBytes: Array<{max: number, type: string}> = [
-  { max: 1024, type: 'B' },
-  { max: 1048576, type: 'KB' },
-  { max: 1073741824, type: 'MB' },
-  { max: 1.0995116e12, type: 'GB' }
-]
-function bytes (value) {
-  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 ]
+function isWebRTCDisabled () {
+  return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
 }
 
-function getStoredVolume () {
-  const value = getLocalStorage('volume')
-  if (value !== null && value !== undefined) {
-    const valueNumber = parseFloat(value)
-    if (isNaN(valueNumber)) return undefined
-
-    return valueNumber
-  }
+function isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) {
+  if (video.isLocal && config.tracker.enabled === false) return false
+  if (isWebRTCDisabled()) return false
 
-  return undefined
+  return userP2PEnabled
 }
 
-function getStoredMute () {
-  const value = getLocalStorage('mute')
-  if (value !== null && value !== undefined) return value === 'true'
-
-  return undefined
-}
-
-function getAverageBandwidth () {
-  const value = getLocalStorage('average-bandwidth')
-  if (value !== null && value !== undefined) {
-    const valueNumber = parseInt(value, 10)
-    if (isNaN(valueNumber)) return undefined
-
-    return valueNumber
+function isIOS () {
+  if (/iPad|iPhone|iPod/.test(navigator.platform)) {
+    return true
   }
 
-  return undefined
-}
-
-function getStoredTheater () {
-  const value = getLocalStorage('theater-enabled')
-  if (value !== null && value !== undefined) return value === 'true'
-
-  return undefined
+  // Detect iPad Desktop mode
+  return !!(navigator.maxTouchPoints &&
+      navigator.maxTouchPoints > 2 &&
+      navigator.platform.includes('MacIntel'))
 }
 
-function saveVolumeInStore (value: number) {
-  return setLocalStorage('volume', value.toString())
+function isSafari () {
+  return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
 }
 
-function saveMuteInStore (value: boolean) {
-  return setLocalStorage('mute', value.toString())
-}
-
-function saveTheaterInStore (enabled: boolean) {
-  return setLocalStorage('theater-enabled', enabled.toString())
-}
+// 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
+const dictionaryBytes: Array<{max: number, type: string}> = [
+  { max: 1024, type: 'B' },
+  { max: 1048576, type: 'KB' },
+  { max: 1073741824, type: 'MB' },
+  { max: 1.0995116e12, type: 'GB' }
+]
+function bytes (value: number) {
+  const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
+  const calc = Math.floor(value / (format.max / 1024)).toString()
 
-function saveAverageBandwidth (value: number) {
-  return setLocalStorage('average-bandwidth', value.toString())
+  return [ calc, format.type ]
 }
 
 function isMobile () {
   return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
 }
 
-function buildVideoLink (time?: number) {
-  let href = window.location.href.replace('/embed/', '/watch/')
-  if (time) {
-    const timeInt = Math.floor(time)
-
-    if (window.location.search) href += '&start=' + timeInt
-    else href += '?start=' + timeInt
-  }
+function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) {
+  const iframe = document.createElement('iframe')
 
-  return href
-}
+  iframe.title = embedTitle
+  iframe.width = '560'
+  iframe.height = '315'
+  iframe.src = embedUrl
+  iframe.frameBorder = '0'
+  iframe.allowFullscreen = true
+  iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups')
 
-function buildVideoEmbed (embedUrl: string) {
-  return '<iframe width="560" height="315" ' +
-    'src="' + embedUrl + '" ' +
-    'frameborder="0" allowfullscreen>' +
-    '</iframe>'
-}
-
-function copyToClipboard (text: string) {
-  const el = document.createElement('textarea')
-  el.value = text
-  el.setAttribute('readonly', '')
-  el.style.position = 'absolute'
-  el.style.left = '-9999px'
-  document.body.appendChild(el)
-  el.select()
-  document.execCommand('copy')
-  document.body.removeChild(el)
+  return iframe.outerHTML
 }
 
 function videoFileMaxByResolution (files: VideoFile[]) {
@@ -131,39 +85,32 @@ function videoFileMinByResolution (files: VideoFile[]) {
   return min
 }
 
-export {
-  toTitleCase,
-  buildVideoLink,
-  getStoredVolume,
-  saveVolumeInStore,
-  saveAverageBandwidth,
-  getAverageBandwidth,
-  saveMuteInStore,
-  buildVideoEmbed,
-  getStoredMute,
-  videoFileMaxByResolution,
-  videoFileMinByResolution,
-  copyToClipboard,
-  getStoredTheater,
-  saveTheaterInStore,
-  isMobile,
-  bytes
+function getRtcConfig () {
+  return {
+    iceServers: [
+      {
+        urls: 'stun:stun.stunprotocol.org'
+      },
+      {
+        urls: 'stun:stun.framasoft.org'
+      }
+    ]
+  }
 }
 
 // ---------------------------------------------------------------------------
 
-const KEY_PREFIX = 'peertube-videojs-'
-
-function getLocalStorage (key: string) {
-  try {
-    return localStorage.getItem(KEY_PREFIX + key)
-  } catch {
-    return undefined
-  }
-}
+export {
+  getRtcConfig,
+  toTitleCase,
+  isWebRTCDisabled,
+  isP2PEnabled,
 
-function setLocalStorage (key: string, value: string) {
-  try {
-    localStorage.setItem(KEY_PREFIX + key, value)
-  } catch { /* empty */ }
+  buildVideoOrPlaylistEmbed,
+  videoFileMaxByResolution,
+  videoFileMinByResolution,
+  isMobile,
+  bytes,
+  isIOS,
+  isSafari
 }