diff options
Diffstat (limited to 'client/src/assets/player/peertube-videojs-plugin.ts')
-rw-r--r-- | client/src/assets/player/peertube-videojs-plugin.ts | 98 |
1 files changed, 93 insertions, 5 deletions
diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index c54d8b5ea..977455bff 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts | |||
@@ -2,9 +2,24 @@ | |||
2 | 2 | ||
3 | import videojs, { Player } from 'video.js' | 3 | import videojs, { Player } from 'video.js' |
4 | import * as WebTorrent from 'webtorrent' | 4 | import * as WebTorrent from 'webtorrent' |
5 | import { VideoFile } from '../../../../shared' | ||
5 | 6 | ||
6 | import { renderVideo } from './video-renderer' | 7 | import { renderVideo } from './video-renderer' |
7 | import { VideoFile } from '../../../../shared' | 8 | |
9 | // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts | ||
10 | // Don't import all Angular stuff, just copy the code with shame | ||
11 | const dictionaryBytes: Array<{max: number, type: string}> = [ | ||
12 | { max: 1024, type: 'B' }, | ||
13 | { max: 1048576, type: 'KB' }, | ||
14 | { max: 1073741824, type: 'MB' }, | ||
15 | { max: 1.0995116e12, type: 'GB' } | ||
16 | ] | ||
17 | function bytes (value) { | ||
18 | const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] | ||
19 | const calc = Math.floor(value / (format.max / 1024)).toString() | ||
20 | |||
21 | return [ calc, format.type ] | ||
22 | } | ||
8 | 23 | ||
9 | // videojs typings don't have some method we need | 24 | // videojs typings don't have some method we need |
10 | const videojsUntyped = videojs as any | 25 | const videojsUntyped = videojs as any |
@@ -62,6 +77,7 @@ const ResolutionMenuButton = videojsUntyped.extend(MenuButton, { | |||
62 | 77 | ||
63 | update: function () { | 78 | update: function () { |
64 | this.label.innerHTML = this.player_.getCurrentResolutionLabel() | 79 | this.label.innerHTML = this.player_.getCurrentResolutionLabel() |
80 | this.hide() | ||
65 | return MenuButton.prototype.update.call(this) | 81 | return MenuButton.prototype.update.call(this) |
66 | }, | 82 | }, |
67 | 83 | ||
@@ -74,8 +90,7 @@ MenuButton.registerComponent('ResolutionMenuButton', ResolutionMenuButton) | |||
74 | const Button = videojsUntyped.getComponent('Button') | 90 | const Button = videojsUntyped.getComponent('Button') |
75 | const PeertubeLinkButton = videojsUntyped.extend(Button, { | 91 | const PeertubeLinkButton = videojsUntyped.extend(Button, { |
76 | constructor: function (player) { | 92 | constructor: function (player) { |
77 | Button.apply(this, arguments) | 93 | Button.call(this, player) |
78 | this.player = player | ||
79 | }, | 94 | }, |
80 | 95 | ||
81 | createEl: function () { | 96 | createEl: function () { |
@@ -90,11 +105,78 @@ const PeertubeLinkButton = videojsUntyped.extend(Button, { | |||
90 | }, | 105 | }, |
91 | 106 | ||
92 | handleClick: function () { | 107 | handleClick: function () { |
93 | this.player.pause() | 108 | this.player_.pause() |
94 | } | 109 | } |
95 | }) | 110 | }) |
96 | Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton) | 111 | Button.registerComponent('PeerTubeLinkButton', PeertubeLinkButton) |
97 | 112 | ||
113 | const WebTorrentButton = videojsUntyped.extend(Button, { | ||
114 | constructor: function (player) { | ||
115 | Button.call(this, player) | ||
116 | }, | ||
117 | |||
118 | createEl: function () { | ||
119 | const div = document.createElement('div') | ||
120 | |||
121 | const downloadIcon = document.createElement('span') | ||
122 | downloadIcon.classList.add('icon', 'icon-download') | ||
123 | div.appendChild(downloadIcon) | ||
124 | |||
125 | const downloadSpeedText = document.createElement('span') | ||
126 | downloadSpeedText.classList.add('download-speed-text') | ||
127 | const downloadSpeedNumber = document.createElement('span') | ||
128 | downloadSpeedNumber.classList.add('download-speed-number') | ||
129 | const downloadSpeedUnit = document.createElement('span') | ||
130 | downloadSpeedText.appendChild(downloadSpeedNumber) | ||
131 | downloadSpeedText.appendChild(downloadSpeedUnit) | ||
132 | div.appendChild(downloadSpeedText) | ||
133 | |||
134 | const uploadIcon = document.createElement('span') | ||
135 | uploadIcon.classList.add('icon', 'icon-upload') | ||
136 | div.appendChild(uploadIcon) | ||
137 | |||
138 | const uploadSpeedText = document.createElement('span') | ||
139 | uploadSpeedText.classList.add('upload-speed-text') | ||
140 | const uploadSpeedNumber = document.createElement('span') | ||
141 | uploadSpeedNumber.classList.add('upload-speed-number') | ||
142 | const uploadSpeedUnit = document.createElement('span') | ||
143 | uploadSpeedText.appendChild(uploadSpeedNumber) | ||
144 | uploadSpeedText.appendChild(uploadSpeedUnit) | ||
145 | div.appendChild(uploadSpeedText) | ||
146 | |||
147 | const peersText = document.createElement('span') | ||
148 | peersText.textContent = ' peers' | ||
149 | peersText.classList.add('peers-text') | ||
150 | const peersNumber = document.createElement('span') | ||
151 | peersNumber.classList.add('peers-number') | ||
152 | div.appendChild(peersNumber) | ||
153 | div.appendChild(peersText) | ||
154 | |||
155 | div.className = 'vjs-webtorrent' | ||
156 | // Hide the stats before we get the info | ||
157 | div.style.display = 'none' | ||
158 | |||
159 | this.player_.on('torrentInfo', (event, data) => { | ||
160 | const downloadSpeed = bytes(data.downloadSpeed) | ||
161 | const uploadSpeed = bytes(data.uploadSpeed) | ||
162 | const numPeers = data.numPeers | ||
163 | |||
164 | downloadSpeedNumber.textContent = downloadSpeed[0] | ||
165 | downloadSpeedUnit.textContent = ' ' + downloadSpeed[1] | ||
166 | |||
167 | uploadSpeedNumber.textContent = uploadSpeed[0] | ||
168 | uploadSpeedUnit.textContent = ' ' + uploadSpeed[1] | ||
169 | |||
170 | peersNumber.textContent = numPeers | ||
171 | |||
172 | div.style.display = 'block' | ||
173 | }) | ||
174 | |||
175 | return div | ||
176 | } | ||
177 | }) | ||
178 | Button.registerComponent('WebTorrentButton', WebTorrentButton) | ||
179 | |||
98 | type PeertubePluginOptions = { | 180 | type PeertubePluginOptions = { |
99 | videoFiles: VideoFile[] | 181 | videoFiles: VideoFile[] |
100 | playerElement: HTMLVideoElement | 182 | playerElement: HTMLVideoElement |
@@ -223,6 +305,12 @@ const peertubePlugin = function (options: PeertubePluginOptions) { | |||
223 | } | 305 | } |
224 | } | 306 | } |
225 | 307 | ||
308 | const webTorrentButton = new WebTorrentButton(player) | ||
309 | controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el()) | ||
310 | controlBar.webTorrent.dispose = function () { | ||
311 | this.parentNode.removeChild(this) | ||
312 | } | ||
313 | |||
226 | if (options.autoplay === true) { | 314 | if (options.autoplay === true) { |
227 | player.updateVideoFile() | 315 | player.updateVideoFile() |
228 | } else { | 316 | } else { |
@@ -245,7 +333,7 @@ const peertubePlugin = function (options: PeertubePluginOptions) { | |||
245 | }, 1000) | 333 | }, 1000) |
246 | }) | 334 | }) |
247 | 335 | ||
248 | function handleError (err: Error|string) { | 336 | function handleError (err: Error | string) { |
249 | return player.trigger('customError', { err }) | 337 | return player.trigger('customError', { err }) |
250 | } | 338 | } |
251 | } | 339 | } |