aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/mobile/peertube-mobile-plugin.ts
blob: 95864c817546e483a27ff2ced4c82e87644be527 (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
144
import { PeerTubeMobileButtons } from './peertube-mobile-buttons'
import videojs from 'video.js'
import debug from 'debug'

const logger = debug('peertube:player:mobile')

const Plugin = videojs.getPlugin('plugin')

class PeerTubeMobilePlugin extends Plugin {
  private static readonly DOUBLE_TAP_DELAY_MS = 250
  private static readonly SET_CURRENT_TIME_DELAY = 1000

  private peerTubeMobileButtons: PeerTubeMobileButtons

  private seekAmount = 0

  private lastTapEvent: TouchEvent
  private tapTimeout: ReturnType<typeof setTimeout>
  private newActiveState: boolean

  private setCurrentTimeTimeout: ReturnType<typeof setTimeout>

  constructor (player: videojs.Player, options: videojs.PlayerOptions) {
    super(player, options)

    this.peerTubeMobileButtons = player.addChild('PeerTubeMobileButtons') as PeerTubeMobileButtons

    if (videojs.browser.IS_ANDROID && screen.orientation) {
      this.handleFullscreenRotation()
    }

    if (!this.player.options_.userActions) this.player.options_.userActions = {};

    // FIXME: typings
    (this.player.options_.userActions as any).click = false
    this.player.options_.userActions.doubleClick = false

    this.player.one('play', () => {
      this.initTouchStartEvents()
    })
  }

  private handleFullscreenRotation () {
    this.player.on('fullscreenchange', () => {
      if (!this.player.isFullscreen() || this.isPortraitVideo()) return

      screen.orientation.lock('landscape')
        .catch(err => console.error('Cannot lock screen to landscape.', err))
    })
  }

  private isPortraitVideo () {
    return this.player.videoWidth() < this.player.videoHeight()
  }

  private initTouchStartEvents () {
    this.player.on('touchstart', (event: TouchEvent) => {
      event.stopPropagation()

      if (this.tapTimeout) {
        clearTimeout(this.tapTimeout)
        this.tapTimeout = undefined
      }

      if (this.lastTapEvent && event.timeStamp - this.lastTapEvent.timeStamp < PeerTubeMobilePlugin.DOUBLE_TAP_DELAY_MS) {
        logger('Detected double tap')

        this.lastTapEvent = undefined
        this.onDoubleTap(event)
        return
      }

      this.newActiveState = !this.player.userActive()

      this.tapTimeout = setTimeout(() => {
        logger('No double tap detected, set user active state to %s.', this.newActiveState)

        this.player.userActive(this.newActiveState)
      }, PeerTubeMobilePlugin.DOUBLE_TAP_DELAY_MS)

      this.lastTapEvent = event
    })
  }

  private onDoubleTap (event: TouchEvent) {
    const playerWidth = this.player.currentWidth()

    const rect = this.findPlayerTarget((event.target as HTMLElement)).getBoundingClientRect()
    const offsetX = event.targetTouches[0].pageX - rect.left

    logger('Calculating double tap zone (player width: %d, offset X: %d)', playerWidth, offsetX)

    if (offsetX > 0.66 * playerWidth) {
      if (this.seekAmount < 0) this.seekAmount = 0

      this.seekAmount += 10

      logger('Will forward %d seconds', this.seekAmount)
    } else if (offsetX < 0.33 * playerWidth) {
      if (this.seekAmount > 0) this.seekAmount = 0

      this.seekAmount -= 10
      logger('Will rewind %d seconds', this.seekAmount)
    }

    this.peerTubeMobileButtons.displayFastSeek(this.seekAmount)

    this.scheduleSetCurrentTime()

  }

  private findPlayerTarget (target: HTMLElement): HTMLElement {
    if (target.classList.contains('video-js')) return target

    return this.findPlayerTarget(target.parentElement)
  }

  private scheduleSetCurrentTime () {
    this.player.pause()
    this.player.addClass('vjs-fast-seeking')

    if (this.setCurrentTimeTimeout) clearTimeout(this.setCurrentTimeTimeout)

    this.setCurrentTimeTimeout = setTimeout(() => {
      let newTime = this.player.currentTime() + this.seekAmount
      this.seekAmount = 0

      newTime = Math.max(0, newTime)
      newTime = Math.min(this.player.duration(), newTime)

      this.player.currentTime(newTime)
      this.seekAmount = 0
      this.peerTubeMobileButtons.displayFastSeek(0)

      this.player.removeClass('vjs-fast-seeking')
      this.player.userActive(false)

      this.player.play()
    }, PeerTubeMobilePlugin.SET_CURRENT_TIME_DELAY)
  }
}

videojs.registerPlugin('peertubeMobile', PeerTubeMobilePlugin)
export { PeerTubeMobilePlugin }