aboutsummaryrefslogtreecommitdiff
path: root/src/components/video_attachment/video_attachment.js
blob: 107b89855281aabac68603ef836330e625df5baa (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

const VideoAttachment = {
  props: ['attachment', 'controls'],
  data () {
    return {
      blocksSuspend: false,
      // Start from true because removing "loop" property seems buggy in Vue
      hasAudio: true
    }
  },
  computed: {
    loopVideo () {
      if (this.$store.getters.mergedConfig.loopVideoSilentOnly) {
        return !this.hasAudio
      }
      return this.$store.getters.mergedConfig.loopVideo
    }
  },
  methods: {
    onPlaying (e) {
      this.setHasAudio(e)
      if (this.loopVideo) {
        this.$emit('play', { looping: true })
        return
      }
      this.$emit('play')
    },
    onPaused (e) {
      this.$emit('pause')
    },
    setHasAudio (e) {
      const target = e.srcElement || e.target
      // If hasAudio is false, we've already marked this video to not have audio,
      // a video can't gain audio out of nowhere so don't bother checking again.
      if (!this.hasAudio) return
      if (typeof target.webkitAudioDecodedByteCount !== 'undefined') {
        // non-zero if video has audio track
        if (target.webkitAudioDecodedByteCount > 0) return
      }
      if (typeof target.mozHasAudio !== 'undefined') {
        // true if video has audio track
        if (target.mozHasAudio) return
      }
      if (typeof target.audioTracks !== 'undefined') {
        if (target.audioTracks.length > 0) return
      }
      this.hasAudio = false
    }
  }
}

export default VideoAttachment