aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
blob: fb990a19dd44558bcfcb0ee72a89da16421967f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { basename } from 'path'
import { Segment } from 'p2p-media-loader-core'

function segmentUrlBuilderFactory (baseUrls: string[]) {
  return function segmentBuilder (segment: Segment) {
    const max = baseUrls.length + 1
    const i = getRandomInt(max)

    if (i === max - 1) return segment.url

    const newBaseUrl = baseUrls[i]
    const middlePart = newBaseUrl.endsWith('/') ? '' : '/'

    return newBaseUrl + middlePart + basename(segment.url)
  }
}

// ---------------------------------------------------------------------------

export {
  segmentUrlBuilderFactory
}

// ---------------------------------------------------------------------------

function getRandomInt (max: number) {
  return Math.floor(Math.random() * Math.floor(max))
}