]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/assets/player/utils.ts
Add hls support on server
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
index 7a99dba1ae5c052982f28c88d819e430ad049515..8d87567c2b53dd75b412f8080c7c6c92d181571f 100644 (file)
@@ -1,3 +1,5 @@
+import { VideoFile } from '../../../../shared/models/videos'
+
 function toTitleCase (str: string) {
   return str.charAt(0).toUpperCase() + str.slice(1)
 }
@@ -10,63 +12,130 @@ const dictionaryBytes: Array<{max: number, type: string}> = [
   { max: 1073741824, type: 'MB' },
   { max: 1.0995116e12, type: 'GB' }
 ]
-function bytes (value) {
+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()
 
   return [ calc, format.type ]
 }
 
-function getStoredVolume () {
-  const value = getLocalStorage('volume')
-  if (value !== null && value !== undefined) {
-    const valueNumber = parseFloat(value)
-    if (isNaN(valueNumber)) return undefined
+function isMobile () {
+  return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
+}
+
+function buildVideoLink (time?: number, url?: string) {
+  if (!url) url = window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
+
+  if (time) {
+    const timeInt = Math.floor(time)
 
-    return valueNumber
+    const params = new URLSearchParams(window.location.search)
+    params.set('start', secondsToTime(timeInt))
+
+    return url + '?' + params.toString()
   }
 
-  return undefined
+  return url
 }
 
-function getStoredMute () {
-  const value = getLocalStorage('mute')
-  if (value !== null && value !== undefined) return value === 'true'
+function timeToInt (time: number | string) {
+  if (!time) return 0
+  if (typeof time === 'number') return time
+
+  const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/
+  const matches = time.match(reg)
+
+  if (!matches) return 0
+
+  const hours = parseInt(matches[2] || '0', 10)
+  const minutes = parseInt(matches[4] || '0', 10)
+  const seconds = parseInt(matches[6] || '0', 10)
 
-  return undefined
+  return hours * 3600 + minutes * 60 + seconds
 }
 
-function saveVolumeInStore (value: number) {
-  return setLocalStorage('volume', value.toString())
+function secondsToTime (seconds: number) {
+  let time = ''
+
+  let hours = Math.floor(seconds / 3600)
+  if (hours >= 1) time = hours + 'h'
+
+  seconds %= 3600
+  let minutes = Math.floor(seconds / 60)
+  if (minutes >= 1) time += minutes + 'm'
+
+  seconds %= 60
+  if (seconds >= 1) time += seconds + 's'
+
+  return time
 }
 
-function saveMuteInStore (value: boolean) {
-  return setLocalStorage('mute', value.toString())
+function buildVideoEmbed (embedUrl: string) {
+  return '<iframe width="560" height="315" ' +
+    'sandbox="allow-same-origin allow-scripts" ' +
+    'src="' + embedUrl + '" ' +
+    'frameborder="0" allowfullscreen>' +
+    '</iframe>'
 }
 
-export {
-  toTitleCase,
-  getStoredVolume,
-  saveVolumeInStore,
-  saveMuteInStore,
-  getStoredMute,
-  bytes
+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)
 }
 
-// ---------------------------------------------------------------------------
+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
+  }
 
-const KEY_PREFIX = 'peertube-videojs-'
+  return min
+}
 
-function getLocalStorage (key: string) {
-  try {
-    return localStorage.getItem(KEY_PREFIX + key)
-  } catch {
-    return undefined
+function getRtcConfig () {
+  return {
+    iceServers: [
+      {
+        urls: 'stun:stun.stunprotocol.org'
+      },
+      {
+        urls: 'stun:stun.framasoft.org'
+      }
+    ]
   }
 }
 
-function setLocalStorage (key: string, value: string) {
-  try {
-    localStorage.setItem(KEY_PREFIX + key, value)
-  } catch { /* empty */ }
+// ---------------------------------------------------------------------------
+
+export {
+  getRtcConfig,
+  toTitleCase,
+  timeToInt,
+  buildVideoLink,
+  buildVideoEmbed,
+  videoFileMaxByResolution,
+  videoFileMinByResolution,
+  copyToClipboard,
+  isMobile,
+  bytes
 }