1 import { VideoFile } from '../../../../shared/models/videos'
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
12 return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)
15 function isSafari () {
16 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
19 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
20 // Don't import all Angular stuff, just copy the code with shame
21 const dictionaryBytes: Array<{max: number, type: string}> = [
22 { max: 1024, type: 'B' },
23 { max: 1048576, type: 'KB' },
24 { max: 1073741824, type: 'MB' },
25 { max: 1.0995116e12, type: 'GB' }
27 function bytes (value: number) {
28 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
29 const calc = Math.floor(value / (format.max / 1024)).toString()
31 return [ calc, format.type ]
34 function isMobile () {
35 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
38 function buildVideoLink (options: {
52 warningTitle?: boolean,
55 const { baseUrl } = options
59 : window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
61 const params = new URLSearchParams(window.location.search)
62 // Remove these unused parameters when we are on a playlist page
63 params.delete('videoId')
64 params.delete('resume')
66 if (options.startTime) {
67 const startTimeInt = Math.floor(options.startTime)
68 params.set('start', secondsToTime(startTimeInt))
71 if (options.stopTime) {
72 const stopTimeInt = Math.floor(options.stopTime)
73 params.set('stop', secondsToTime(stopTimeInt))
76 if (options.subtitle) params.set('subtitle', options.subtitle)
78 if (options.loop === true) params.set('loop', '1')
79 if (options.autoplay === true) params.set('autoplay', '1')
80 if (options.muted === true) params.set('muted', '1')
81 if (options.title === false) params.set('title', '0')
82 if (options.warningTitle === false) params.set('warningTitle', '0')
83 if (options.controls === false) params.set('controls', '0')
86 params.forEach(() => hasParams = true)
88 if (hasParams) return url + '?' + params.toString()
93 function timeToInt (time: number | string) {
95 if (typeof time === 'number') return time
97 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
98 const matches = time.match(reg)
100 if (!matches) return 0
102 const hours = parseInt(matches[2] || '0', 10)
103 const minutes = parseInt(matches[4] || '0', 10)
104 const seconds = parseInt(matches[6] || '0', 10)
106 return hours * 3600 + minutes * 60 + seconds
109 function secondsToTime (seconds: number, full = false, symbol?: string) {
112 const hourSymbol = (symbol || 'h')
113 const minuteSymbol = (symbol || 'm')
114 const secondsSymbol = full ? '' : 's'
116 const hours = Math.floor(seconds / 3600)
117 if (hours >= 1) time = hours + hourSymbol
118 else if (full) time = '0' + hourSymbol
121 const minutes = Math.floor(seconds / 60)
122 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
123 else if (minutes >= 1) time += minutes + minuteSymbol
124 else if (full) time += '00' + minuteSymbol
127 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
128 else if (seconds >= 1) time += seconds + secondsSymbol
129 else if (full) time += '00'
134 function buildVideoEmbed (embedUrl: string) {
135 return '<iframe width="560" height="315" ' +
136 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
137 'src="' + embedUrl + '" ' +
138 'frameborder="0" allowfullscreen>' +
142 function copyToClipboard (text: string) {
143 const el = document.createElement('textarea')
145 el.setAttribute('readonly', '')
146 el.style.position = 'absolute'
147 el.style.left = '-9999px'
148 document.body.appendChild(el)
150 document.execCommand('copy')
151 document.body.removeChild(el)
154 function videoFileMaxByResolution (files: VideoFile[]) {
157 for (let i = 1; i < files.length; i++) {
158 const file = files[i]
159 if (max.resolution.id < file.resolution.id) max = file
165 function videoFileMinByResolution (files: VideoFile[]) {
168 for (let i = 1; i < files.length; i++) {
169 const file = files[i]
170 if (min.resolution.id > file.resolution.id) min = file
176 function getRtcConfig () {
180 urls: 'stun:stun.stunprotocol.org'
183 urls: 'stun:stun.framasoft.org'
189 // ---------------------------------------------------------------------------
199 videoFileMaxByResolution,
200 videoFileMinByResolution,