aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/resolutions/peertube-resolutions-plugin.ts
blob: e7899ac71a0459c337e80c2d572604524ca4e1b1 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import videojs from 'video.js'
import { PeerTubeResolution } from '../../types'

const Plugin = videojs.getPlugin('plugin')

class PeerTubeResolutionsPlugin extends Plugin {
  private currentSelection: PeerTubeResolution
  private resolutions: PeerTubeResolution[] = []

  private autoResolutionChosenId: number
  private autoResolutionEnabled = true

  add (resolutions: PeerTubeResolution[]) {
    for (const r of resolutions) {
      this.resolutions.push(r)
    }

    this.currentSelection = this.getSelected()

    this.sort()
    this.trigger('resolutionsAdded')
  }

  getResolutions () {
    return this.resolutions
  }

  getSelected () {
    return this.resolutions.find(r => r.selected)
  }

  getAutoResolutionChosen () {
    return this.resolutions.find(r => r.id === this.autoResolutionChosenId)
  }

  select (options: {
    id: number
    byEngine: boolean
    autoResolutionChosenId?: number
  }) {
    const { id, autoResolutionChosenId, byEngine } = options

    if (this.currentSelection?.id === id && this.autoResolutionChosenId === autoResolutionChosenId) return

    this.autoResolutionChosenId = autoResolutionChosenId

    for (const r of this.resolutions) {
      r.selected = r.id === id

      if (r.selected) {
        this.currentSelection = r

        if (!byEngine) r.selectCallback()
      }
    }

    this.trigger('resolutionChanged')
  }

  disableAutoResolution () {
    this.autoResolutionEnabled = false
    this.trigger('autoResolutionEnabledChanged')
  }

  enabledAutoResolution () {
    this.autoResolutionEnabled = true
    this.trigger('autoResolutionEnabledChanged')
  }

  isAutoResolutionEnabeld () {
    return this.autoResolutionEnabled
  }

  private sort () {
    this.resolutions.sort((a, b) => {
      if (a.id === -1) return 1
      if (b.id === -1) return -1

      if (a.height > b.height) return -1
      if (a.height === b.height) return 0
      return 1
    })
  }

}

videojs.registerPlugin('peertubeResolutions', PeerTubeResolutionsPlugin)
export { PeerTubeResolutionsPlugin }