utils.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { HTMLServerConfig, Video, VideoFile } from '@peertube/peertube-models'
  2. function toTitleCase (str: string) {
  3. return str.charAt(0).toUpperCase() + str.slice(1)
  4. }
  5. function isWebRTCDisabled () {
  6. return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
  7. }
  8. function isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) {
  9. if (video.isLocal && config.tracker.enabled === false) return false
  10. if (isWebRTCDisabled()) return false
  11. return userP2PEnabled
  12. }
  13. function isIOS () {
  14. if (/iPad|iPhone|iPod/.test(navigator.platform)) {
  15. return true
  16. }
  17. // Detect iPad Desktop mode
  18. return !!(navigator.maxTouchPoints &&
  19. navigator.maxTouchPoints > 2 &&
  20. navigator.platform.includes('MacIntel'))
  21. }
  22. function isSafari () {
  23. return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
  24. }
  25. // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
  26. // Don't import all Angular stuff, just copy the code with shame
  27. const dictionaryBytes: { max: number, type: string }[] = [
  28. { max: 1024, type: 'B' },
  29. { max: 1048576, type: 'KB' },
  30. { max: 1073741824, type: 'MB' },
  31. { max: 1.0995116e12, type: 'GB' }
  32. ]
  33. function bytes (value: number) {
  34. const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
  35. const calc = Math.floor(value / (format.max / 1024)).toString()
  36. return [ calc, format.type ]
  37. }
  38. function isMobile () {
  39. return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
  40. }
  41. function videoFileMaxByResolution (files: VideoFile[]) {
  42. let max = files[0]
  43. for (let i = 1; i < files.length; i++) {
  44. const file = files[i]
  45. if (max.resolution.id < file.resolution.id) max = file
  46. }
  47. return max
  48. }
  49. function videoFileMinByResolution (files: VideoFile[]) {
  50. let min = files[0]
  51. for (let i = 1; i < files.length; i++) {
  52. const file = files[i]
  53. if (min.resolution.id > file.resolution.id) min = file
  54. }
  55. return min
  56. }
  57. function getRtcConfig () {
  58. return {
  59. iceServers: [
  60. {
  61. urls: 'stun:stun.stunprotocol.org'
  62. },
  63. {
  64. urls: 'stun:stun.framasoft.org'
  65. }
  66. ]
  67. }
  68. }
  69. // ---------------------------------------------------------------------------
  70. export {
  71. getRtcConfig,
  72. toTitleCase,
  73. isWebRTCDisabled,
  74. isP2PEnabled,
  75. videoFileMaxByResolution,
  76. videoFileMinByResolution,
  77. isMobile,
  78. bytes,
  79. isIOS,
  80. isSafari
  81. }