]>
Commit | Line | Data |
---|---|---|
134006b0 | 1 | import Hlsjs from 'hls.js' |
512decf3 | 2 | import videojs from 'video.js' |
3e254de8 C |
3 | import { Events, Segment } from '@peertube/p2p-media-loader-core' |
4 | import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs' | |
fd3c2e87 | 5 | import { logger } from '@root-helpers/logger' |
15a7eafb | 6 | import { timeToInt } from '@shared/core-utils' |
57d65032 | 7 | import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types' |
83fcadac | 8 | import { registerConfigPlugin, registerSourceHandler } from './hls-plugin' |
2adfc7ea | 9 | |
83fcadac C |
10 | registerConfigPlugin(videojs) |
11 | registerSourceHandler(videojs) | |
2adfc7ea | 12 | |
f5fcd9f7 | 13 | const Plugin = videojs.getPlugin('plugin') |
2adfc7ea C |
14 | class P2pMediaLoaderPlugin extends Plugin { |
15 | ||
3b6f205c C |
16 | private readonly CONSTANTS = { |
17 | INFO_SCHEDULER: 1000 // Don't change this | |
18 | } | |
09209296 | 19 | private readonly options: P2PMediaLoaderPluginOptions |
3b6f205c | 20 | |
83fcadac | 21 | private hlsjs: Hlsjs |
3b6f205c C |
22 | private p2pEngine: Engine |
23 | private statsP2PBytes = { | |
24 | pendingDownload: [] as number[], | |
25 | pendingUpload: [] as number[], | |
26 | numPeers: 0, | |
27 | totalDownload: 0, | |
28 | totalUpload: 0 | |
29 | } | |
09209296 C |
30 | private statsHTTPBytes = { |
31 | pendingDownload: [] as number[], | |
fd3c2e87 | 32 | totalDownload: 0 |
09209296 | 33 | } |
e2f01c47 | 34 | private startTime: number |
3b6f205c C |
35 | |
36 | private networkInfoInterval: any | |
37 | ||
7e37e111 | 38 | constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) { |
f5fcd9f7 | 39 | super(player) |
2adfc7ea | 40 | |
09209296 C |
41 | this.options = options |
42 | ||
f5fcd9f7 C |
43 | // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 |
44 | if (!(videojs as any).Html5Hlsjs) { | |
42b40636 | 45 | logger.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.') |
96cb4527 | 46 | |
326f3692 | 47 | let message: string |
69a01996 | 48 | if (!player.canPlayType('application/vnd.apple.mpegurl')) { |
326f3692 C |
49 | message = 'Cannot fallback to built-in HLS' |
50 | } else if (options.requiresAuth) { | |
51 | message = 'Video requires auth which is not compatible to build-in HLS player' | |
52 | } | |
53 | ||
54 | if (message) { | |
42b40636 | 55 | logger.warn(message) |
96cb4527 | 56 | |
69a01996 C |
57 | player.ready(() => player.trigger('error', new Error(message))) |
58 | return | |
59 | } | |
60 | } else { | |
61 | // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 | |
62 | (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => { | |
63 | this.hlsjs = hlsjs | |
64 | }) | |
3b6f205c | 65 | |
69a01996 C |
66 | initVideoJsContribHlsJsPlayer(player) |
67 | } | |
2adfc7ea | 68 | |
e2f01c47 C |
69 | this.startTime = timeToInt(options.startTime) |
70 | ||
2adfc7ea C |
71 | player.src({ |
72 | type: options.type, | |
73 | src: options.src | |
74 | }) | |
09209296 | 75 | |
69a01996 C |
76 | player.ready(() => { |
77 | this.initializeCore() | |
6ec0b75b | 78 | |
69a01996 C |
79 | if ((videojs as any).Html5Hlsjs) { |
80 | this.initializePlugin() | |
81 | } | |
82 | }) | |
2adfc7ea C |
83 | } |
84 | ||
3b6f205c | 85 | dispose () { |
6ec0b75b C |
86 | if (this.hlsjs) this.hlsjs.destroy() |
87 | if (this.p2pEngine) this.p2pEngine.destroy() | |
88 | ||
3b6f205c C |
89 | clearInterval(this.networkInfoInterval) |
90 | } | |
91 | ||
4e11d8f3 | 92 | getCurrentLevel () { |
f2c29ced C |
93 | if (!this.hlsjs) return undefined |
94 | ||
3e254de8 | 95 | return this.hlsjs.levels[this.hlsjs.currentLevel] |
4e11d8f3 C |
96 | } |
97 | ||
98 | getLiveLatency () { | |
02b2e482 | 99 | return Math.round(this.hlsjs.latency) |
4e11d8f3 C |
100 | } |
101 | ||
6377a9f2 C |
102 | getHLSJS () { |
103 | return this.hlsjs | |
104 | } | |
105 | ||
69a01996 C |
106 | private initializeCore () { |
107 | this.player.one('play', () => { | |
108 | this.player.addClass('vjs-has-big-play-button-clicked') | |
109 | }) | |
110 | ||
111 | this.player.one('canplay', () => { | |
112 | if (this.startTime) { | |
113 | this.player.currentTime(this.startTime) | |
114 | } | |
115 | }) | |
116 | } | |
117 | ||
118 | private initializePlugin () { | |
09209296 C |
119 | initHlsJsPlayer(this.hlsjs) |
120 | ||
9597920e | 121 | this.p2pEngine = this.options.loader.getEngine() |
3b6f205c | 122 | |
da332417 | 123 | this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => { |
f2a16d93 | 124 | if (navigator.onLine === false) return |
125 | ||
42b40636 | 126 | logger.error(`Segment ${segment.id} error.`, err) |
da332417 | 127 | |
b82df0a3 | 128 | this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl) |
09209296 C |
129 | }) |
130 | ||
da332417 | 131 | this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls() |
09209296 | 132 | |
3b6f205c | 133 | this.runStats() |
fd3c2e87 C |
134 | |
135 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engineResolutionChange')) | |
3b6f205c C |
136 | } |
137 | ||
138 | private runStats () { | |
3e254de8 | 139 | this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => { |
09209296 C |
140 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
141 | ||
3e254de8 C |
142 | elem.pendingDownload.push(bytes) |
143 | elem.totalDownload += bytes | |
3b6f205c C |
144 | }) |
145 | ||
3e254de8 | 146 | this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => { |
fd3c2e87 C |
147 | if (method !== 'p2p') { |
148 | logger.error(`Received upload from unknown method ${method}`) | |
149 | return | |
150 | } | |
09209296 | 151 | |
fd3c2e87 C |
152 | this.statsP2PBytes.pendingUpload.push(bytes) |
153 | this.statsP2PBytes.totalUpload += bytes | |
3b6f205c C |
154 | }) |
155 | ||
156 | this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) | |
157 | this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) | |
158 | ||
159 | this.networkInfoInterval = setInterval(() => { | |
09209296 C |
160 | const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) |
161 | const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) | |
162 | ||
163 | const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) | |
3b6f205c C |
164 | |
165 | this.statsP2PBytes.pendingDownload = [] | |
166 | this.statsP2PBytes.pendingUpload = [] | |
09209296 | 167 | this.statsHTTPBytes.pendingDownload = [] |
3b6f205c C |
168 | |
169 | return this.player.trigger('p2pInfo', { | |
17152837 | 170 | source: 'p2p-media-loader', |
09209296 C |
171 | http: { |
172 | downloadSpeed: httpDownloadSpeed, | |
fd3c2e87 | 173 | downloaded: this.statsHTTPBytes.totalDownload |
09209296 | 174 | }, |
3b6f205c | 175 | p2p: { |
09209296 C |
176 | downloadSpeed: p2pDownloadSpeed, |
177 | uploadSpeed: p2pUploadSpeed, | |
3b6f205c C |
178 | numPeers: this.statsP2PBytes.numPeers, |
179 | downloaded: this.statsP2PBytes.totalDownload, | |
180 | uploaded: this.statsP2PBytes.totalUpload | |
4e11d8f3 C |
181 | }, |
182 | bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8 | |
3b6f205c C |
183 | } as PlayerNetworkInfo) |
184 | }, this.CONSTANTS.INFO_SCHEDULER) | |
185 | } | |
09209296 C |
186 | |
187 | private arraySum (data: number[]) { | |
188 | return data.reduce((a: number, b: number) => a + b, 0) | |
189 | } | |
2adfc7ea C |
190 | } |
191 | ||
192 | videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) | |
193 | export { P2pMediaLoaderPlugin } |