aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/embed.html
blob: d4bb74263a576e49d8d8c432c61a1098d6436556 (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
145
146
147
148
149
<!DOCTYPE html>
<html>
  <head>
    <title>PeerTube</title>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="icon" type="image/png" href="/client/assets/favicon.png" />

    <link rel="stylesheet" href="/client/assets/video-js/video-js.min.css">
    <link rel="stylesheet" href="/client/assets/video-js/videojs-dock.css">

    <script src="/client/assets/webtorrent/webtorrent.min.js"></script>
    <script src="/client/assets/video-js/video.min.js"></script>
    <script src="/client/assets/video-js/videojs-dock.min.js"></script>

    <style>
      video {
        width: 99%;
      }

      /* fill the entire space */
      html, body {
        height: 100%;
        margin: 0;
      }

      .video-js {
        width: 100%;
        height: 100%;
      }

      .vjs-poster {
        background-size: 100% auto;
      }

      .vjs-peertube-link {
        color: white;
        text-decoration: none;
        font-size: 1.3em;
        line-height: 2.20;
        transition: all .4s;
      }

      .vjs-peertube-link:hover {
        text-shadow: 0 0 1em #fff;
      }

    </style>
  </head>

  <body>

  <video id="video-container" class="video-js vjs-default-skin vjs-big-play-centered">
  </video>


  <script>
  function loadVideoInfos (videoId, callback) {
    var xhttp = new XMLHttpRequest()
    xhttp.onreadystatechange = function () {
      if (this.readyState === 4 && this.status === 200) {
        var json = JSON.parse(this.responseText)
        return callback(json)
      }
    }

    var url = window.location.origin + '/api/v1/videos/' + videoId
    xhttp.open('GET', url, true)
    xhttp.send()
  }

  function loadVideoTorrent (magnetUri, player) {
    console.log('Loading video ' + videoId)
    var client = new window.WebTorrent()

    console.log('Adding magnet ' + magnetUri)
    client.add(magnetUri, function (torrent) {
      var file = torrent.files[0]

      file.renderTo('video', function (err) {
        if (err) {
          console.error(err)
          return
        }

        // Hack to "simulate" src link in video.js >= 6
        // If no, we can't play the video after pausing it
        // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
        player.src = function () { return true }

        player.play()
      })
    })
  }

  var urlParts = window.location.href.split('/')
  var videoId = urlParts[urlParts.length - 1]

  loadVideoInfos(videoId, function (videoInfos) {
    var magnetUri = videoInfos.magnetUri
    var videoContainer = document.getElementById('video-container')
    var previewUrl = window.location.protocol + '//' + videoInfos.podHost + videoInfos.previewPath
    videoContainer.poster = previewUrl

    videojs('video-container', { controls: true, autoplay: false }, function () {
      var player = this

      var Button = videojs.getComponent('Button')
      var peertubeLinkButton = videojs.extend(Button, {
        constructor: function () {
          Button.apply(this, arguments)
        },

        createEl: function () {
          var link = document.createElement('a')
          link.href = window.location.href.replace('embed', 'watch')
          link.innerHTML = 'PeerTube'
          link.title = 'Go to the video page'
          link.className = 'vjs-peertube-link'
          link.target = '_blank'

          return link
        },

        handleClick: function () {
          player.pause()
        }
      })
      videojs.registerComponent('PeerTubeLinkButton', peertubeLinkButton)

      var controlBar = player.getChild('controlBar')
      var addedLink = controlBar.addChild('PeerTubeLinkButton', {})
      controlBar.el().insertBefore(addedLink.el(), controlBar.fullscreenToggle.el())

      player.dock({
        title: videoInfos.name
      })

      document.querySelector('.vjs-big-play-button').addEventListener('click', function () {
        loadVideoTorrent(magnetUri, player)
      }, false)
    })
  })

  </script>
  </body>
</html