summaryrefslogtreecommitdiff
path: root/frontend/app/views/tile/handles/tile.video.js
blob: 77640efe1c0c945a99a94cc7b7626d4a8054305e (plain)
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
import React, { Component } from 'react'

import { generateTransform, generateVideoStyle, pickCursor } from 'app/views/tile/tile.utils'
import { timestampToSeconds } from 'app/utils'

export default class TileVideo extends Component {
  state = {
    ready: false,
  }

  constructor(props) {
    super(props)
    this.videoRef = React.createRef()
    this.handleTimeUpdate = this.handleTimeUpdate.bind(this)
    this.handleEnded = this.handleEnded.bind(this)
  }
  
  componentDidMount() {
    this.bind()
    setTimeout(() => {
      this.setState({ ready: true })
    }, 1)
  }

  componentDidUpdate() {
    this.unbind()
    this.bind()
  }

  componentWillUnmount() {
    this.unbind()
  }

  bind() {
    if (!this.videoRef.current) return
    this.el = this.videoRef.current
    this.el.addEventListener('ended', this.handleEnded)
    this.el.addEventListener('timeupdate', this.handleTimeUpdate)
    const muted = this.props.viewing
      ? this.props.tile.settings.muted
      : true
    const volume = muted
      ? 0.0
      : ('volume' in this.props.tile.settings)
        ? this.props.tile.settings.volume
        : 1.0
    this.el.volume = volume
  }

  unbind() {
    if (!this.el) return
    this.el.removeEventListener('timeupdate', this.handleTimeUpdate)
    this.el.removeEventListener('ended', this.handleEnded)
  }

  handleTimeUpdate() {
    if (this.props.tile.settings.loop && this.props.tile.settings.loop_section) {
      const loop_start = timestampToSeconds(this.props.tile.settings.loop_start) || 0
      const loop_end = timestampToSeconds(this.props.tile.settings.loop_end) || this.videoRef.current.duration
      if (this.videoRef.current.currentTime > loop_end) {
        this.videoRef.current.currentTime = loop_start
      }
    }
  }

  handleEnded() {
    this.props.onPlaybackEnded(this.props.tile)
    if (this.props.tile.settings.loop && this.props.tile.settings.loop_section) {
      const loop_start = timestampToSeconds(this.props.tile.settings.loop_start) || 0
      this.videoRef.current.currentTime = loop_start
    }
  }

  render() {
    let { tile, box, bounds, audio, videoBounds, cursors, viewing, onMouseDown, onDoubleClick, onMouseEnter, onMouseLeave } = this.props

    if (!tile.settings.url) {
      return null
    }

    // console.log(tile)
    const style = {
      transform: generateTransform(tile, box, bounds, videoBounds),
      opacity: !this.state.ready ? 0 : tile.settings.opacity,
      transition: "opacity 0.2s",
    }

    let className = ['tile', tile.type].join(' ')

    let [cursorClass, cursorStyle] = pickCursor(tile, cursors, viewing)
    if (cursorClass) {
      className += " " + cursorClass
    }
    if (cursorStyle) {
      style.cursor = cursorStyle
    }

    // console.log(tile.settings)
    className += ' ' + tile.settings.align
    const muted = viewing
      ? (tile.settings.muted || audio.muted)
      : true
    return (
      <div
        className={className}
        onMouseDown={onMouseDown}
        onDoubleClick={onDoubleClick}
        onMouseEnter={onMouseEnter}
        onMouseLeave={onMouseLeave}
        style={style}
      >
        <video
          ref={this.videoRef}
          src={tile.settings.url}
          autoPlay={true}
          controls={false}
          disablePictureInPicture={true}
          loop={tile.settings.loop}
          muted={muted}
          playsInline={true}
          style={generateVideoStyle(tile, bounds)}
        />
      </div>
    )
  }
}