]>
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 | |
69a01996 C |
47 | if (!player.canPlayType('application/vnd.apple.mpegurl')) { |
48 | const message = 'Cannot fallback to built-in HLS' | |
42b40636 | 49 | logger.warn(message) |
96cb4527 | 50 | |
69a01996 C |
51 | player.ready(() => player.trigger('error', new Error(message))) |
52 | return | |
53 | } | |
54 | } else { | |
55 | // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 | |
56 | (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => { | |
57 | this.hlsjs = hlsjs | |
58 | }) | |
3b6f205c | 59 | |
69a01996 C |
60 | initVideoJsContribHlsJsPlayer(player) |
61 | } | |
2adfc7ea | 62 | |
e2f01c47 C |
63 | this.startTime = timeToInt(options.startTime) |
64 | ||
2adfc7ea C |
65 | player.src({ |
66 | type: options.type, | |
67 | src: options.src | |
68 | }) | |
09209296 | 69 | |
69a01996 C |
70 | player.ready(() => { |
71 | this.initializeCore() | |
6ec0b75b | 72 | |
69a01996 C |
73 | if ((videojs as any).Html5Hlsjs) { |
74 | this.initializePlugin() | |
75 | } | |
76 | }) | |
2adfc7ea C |
77 | } |
78 | ||
3b6f205c | 79 | dispose () { |
6ec0b75b C |
80 | if (this.hlsjs) this.hlsjs.destroy() |
81 | if (this.p2pEngine) this.p2pEngine.destroy() | |
82 | ||
3b6f205c C |
83 | clearInterval(this.networkInfoInterval) |
84 | } | |
85 | ||
4e11d8f3 | 86 | getCurrentLevel () { |
3e254de8 | 87 | return this.hlsjs.levels[this.hlsjs.currentLevel] |
4e11d8f3 C |
88 | } |
89 | ||
90 | getLiveLatency () { | |
02b2e482 | 91 | return Math.round(this.hlsjs.latency) |
4e11d8f3 C |
92 | } |
93 | ||
6377a9f2 C |
94 | getHLSJS () { |
95 | return this.hlsjs | |
96 | } | |
97 | ||
69a01996 C |
98 | private initializeCore () { |
99 | this.player.one('play', () => { | |
100 | this.player.addClass('vjs-has-big-play-button-clicked') | |
101 | }) | |
102 | ||
103 | this.player.one('canplay', () => { | |
104 | if (this.startTime) { | |
105 | this.player.currentTime(this.startTime) | |
106 | } | |
107 | }) | |
108 | } | |
109 | ||
110 | private initializePlugin () { | |
09209296 C |
111 | initHlsJsPlayer(this.hlsjs) |
112 | ||
9597920e | 113 | this.p2pEngine = this.options.loader.getEngine() |
3b6f205c | 114 | |
da332417 | 115 | this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => { |
42b40636 | 116 | logger.error(`Segment ${segment.id} error.`, err) |
da332417 | 117 | |
b82df0a3 | 118 | this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl) |
09209296 C |
119 | }) |
120 | ||
da332417 | 121 | this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls() |
09209296 | 122 | |
3b6f205c | 123 | this.runStats() |
fd3c2e87 C |
124 | |
125 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engineResolutionChange')) | |
3b6f205c C |
126 | } |
127 | ||
128 | private runStats () { | |
3e254de8 | 129 | this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => { |
09209296 C |
130 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
131 | ||
3e254de8 C |
132 | elem.pendingDownload.push(bytes) |
133 | elem.totalDownload += bytes | |
3b6f205c C |
134 | }) |
135 | ||
3e254de8 | 136 | this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => { |
fd3c2e87 C |
137 | if (method !== 'p2p') { |
138 | logger.error(`Received upload from unknown method ${method}`) | |
139 | return | |
140 | } | |
09209296 | 141 | |
fd3c2e87 C |
142 | this.statsP2PBytes.pendingUpload.push(bytes) |
143 | this.statsP2PBytes.totalUpload += bytes | |
3b6f205c C |
144 | }) |
145 | ||
146 | this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) | |
147 | this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) | |
148 | ||
149 | this.networkInfoInterval = setInterval(() => { | |
09209296 C |
150 | const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) |
151 | const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) | |
152 | ||
153 | const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) | |
3b6f205c C |
154 | |
155 | this.statsP2PBytes.pendingDownload = [] | |
156 | this.statsP2PBytes.pendingUpload = [] | |
09209296 | 157 | this.statsHTTPBytes.pendingDownload = [] |
3b6f205c C |
158 | |
159 | return this.player.trigger('p2pInfo', { | |
17152837 | 160 | source: 'p2p-media-loader', |
09209296 C |
161 | http: { |
162 | downloadSpeed: httpDownloadSpeed, | |
fd3c2e87 | 163 | downloaded: this.statsHTTPBytes.totalDownload |
09209296 | 164 | }, |
3b6f205c | 165 | p2p: { |
09209296 C |
166 | downloadSpeed: p2pDownloadSpeed, |
167 | uploadSpeed: p2pUploadSpeed, | |
3b6f205c C |
168 | numPeers: this.statsP2PBytes.numPeers, |
169 | downloaded: this.statsP2PBytes.totalDownload, | |
170 | uploaded: this.statsP2PBytes.totalUpload | |
4e11d8f3 C |
171 | }, |
172 | bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8 | |
3b6f205c C |
173 | } as PlayerNetworkInfo) |
174 | }, this.CONSTANTS.INFO_SCHEDULER) | |
175 | } | |
09209296 C |
176 | |
177 | private arraySum (data: number[]) { | |
178 | return data.reduce((a: number, b: number) => a + b, 0) | |
179 | } | |
2adfc7ea C |
180 | } |
181 | ||
182 | videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) | |
183 | export { P2pMediaLoaderPlugin } |