aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/test-embed.ts
blob: 5f8e97562534589e7a75410228fe4c6a7e123455 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import './test-embed.scss'
import { PeerTubeResolution, PlayerEventType } from '../player/definitions'
import { PeerTubePlayer } from '../player/player'

window.addEventListener('load', async () => {
  const urlParts = window.location.href.split('/')
  const lastPart = urlParts[ urlParts.length - 1 ]

  const isPlaylist = window.location.pathname.startsWith('/video-playlists/')

  const elementId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]

  const iframe = document.createElement('iframe')
  iframe.src = isPlaylist
    ? `/videos/embed/${elementId}?api=1`
    : `/video-playlists/embed/${elementId}?api=1`

  const mainElement = document.querySelector('#host')
  mainElement.appendChild(iframe)

  console.log(`Document finished loading.`)
  const player = new PeerTubePlayer(document.querySelector('iframe'))

  window[ 'player' ] = player

  console.log(`Awaiting player ready...`)
  await player.ready
  console.log(`Player is ready.`)

  const monitoredEvents = [
    'pause',
    'play',
    'playbackStatusUpdate',
    'playbackStatusChange'
  ]

  monitoredEvents.forEach(e => {
    player.addEventListener(e as PlayerEventType, (param) => console.log(`PLAYER: event '${e}' received`, param))
    console.log(`PLAYER: now listening for event '${e}'`)

    player.getCurrentPosition()
      .then(position => document.getElementById('playlist-position').innerHTML = position + '')
  })

  let playbackRates: number[] = []
  let currentRate = await player.getPlaybackRate()

  const updateRates = async () => {
    const rateListEl = document.querySelector('#rate-list')
    rateListEl.innerHTML = ''

    playbackRates.forEach(rate => {
      if (currentRate === rate) {
        const itemEl = document.createElement('strong')
        itemEl.innerText = `${rate} (active)`
        itemEl.style.display = 'block'
        rateListEl.appendChild(itemEl)
      } else {
        const itemEl = document.createElement('a')
        itemEl.href = 'javascript:;'
        itemEl.innerText = rate.toString()
        itemEl.addEventListener('click', () => {
          player.setPlaybackRate(rate)
          currentRate = rate
          updateRates()
        })
        itemEl.style.display = 'block'
        rateListEl.appendChild(itemEl)
      }
    })
  }

  player.getPlaybackRates().then(rates => {
    playbackRates = rates
    updateRates()
  })

  const updateCaptions = async () => {
    const captions = await player.getCaptions()

    const captionEl = document.querySelector('#caption-list')
    captionEl.innerHTML = ''

    captions.forEach(c => {
      console.log(c)

      if (c.mode === 'showing') {
        const itemEl = document.createElement('strong')
        itemEl.innerText = `${c.label} (active)`
        itemEl.style.display = 'block'
        captionEl.appendChild(itemEl)
      } else {
        const itemEl = document.createElement('a')
        itemEl.href = 'javascript:;'
        itemEl.innerText = c.label
        itemEl.addEventListener('click', () => {
          player.setCaption(c.id)
          updateCaptions()
        })
        itemEl.style.display = 'block'
        captionEl.appendChild(itemEl)
      }
    })
  }

  updateCaptions()

  const updateResolutions = ((resolutions: PeerTubeResolution[]) => {
    const resolutionListEl = document.querySelector('#resolution-list')
    resolutionListEl.innerHTML = ''

    resolutions.forEach(resolution => {
      if (resolution.active) {
        const itemEl = document.createElement('strong')
        itemEl.innerText = `${resolution.label} (active)`
        itemEl.style.display = 'block'
        resolutionListEl.appendChild(itemEl)
      } else {
        const itemEl = document.createElement('a')
        itemEl.href = 'javascript:;'
        itemEl.innerText = resolution.label
        itemEl.addEventListener('click', () => {
          player.setResolution(resolution.id)
        })
        itemEl.style.display = 'block'
        resolutionListEl.appendChild(itemEl)
      }
    })
  })

  player.getResolutions().then(
    resolutions => updateResolutions(resolutions))
  player.addEventListener('resolutionUpdate',
    resolutions => updateResolutions(resolutions))

  const updateVolume = (volume: number) => {
    const volumeEl = document.getElementById('volume')
    volumeEl.innerText = (volume * 100) + '%'
  }

  player.getVolume().then(volume => updateVolume(volume))
  player.addEventListener('volumeChange', volume => updateVolume(volume))
})