]>
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 () { |
f2c29ced C |
87 | if (!this.hlsjs) return undefined |
88 | ||
3e254de8 | 89 | return this.hlsjs.levels[this.hlsjs.currentLevel] |
4e11d8f3 C |
90 | } |
91 | ||
92 | getLiveLatency () { | |
02b2e482 | 93 | return Math.round(this.hlsjs.latency) |
4e11d8f3 C |
94 | } |
95 | ||
6377a9f2 C |
96 | getHLSJS () { |
97 | return this.hlsjs | |
98 | } | |
99 | ||
69a01996 C |
100 | private initializeCore () { |
101 | this.player.one('play', () => { | |
102 | this.player.addClass('vjs-has-big-play-button-clicked') | |
103 | }) | |
104 | ||
105 | this.player.one('canplay', () => { | |
106 | if (this.startTime) { | |
107 | this.player.currentTime(this.startTime) | |
108 | } | |
109 | }) | |
110 | } | |
111 | ||
112 | private initializePlugin () { | |
09209296 C |
113 | initHlsJsPlayer(this.hlsjs) |
114 | ||
9597920e | 115 | this.p2pEngine = this.options.loader.getEngine() |
3b6f205c | 116 | |
da332417 | 117 | this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => { |
f2a16d93 | 118 | if (navigator.onLine === false) return |
119 | ||
42b40636 | 120 | logger.error(`Segment ${segment.id} error.`, err) |
da332417 | 121 | |
b82df0a3 | 122 | this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl) |
09209296 C |
123 | }) |
124 | ||
da332417 | 125 | this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls() |
09209296 | 126 | |
3b6f205c | 127 | this.runStats() |
fd3c2e87 C |
128 | |
129 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engineResolutionChange')) | |
3b6f205c C |
130 | } |
131 | ||
132 | private runStats () { | |
3e254de8 | 133 | this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => { |
09209296 C |
134 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
135 | ||
3e254de8 C |
136 | elem.pendingDownload.push(bytes) |
137 | elem.totalDownload += bytes | |
3b6f205c C |
138 | }) |
139 | ||
3e254de8 | 140 | this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => { |
fd3c2e87 C |
141 | if (method !== 'p2p') { |
142 | logger.error(`Received upload from unknown method ${method}`) | |
143 | return | |
144 | } | |
09209296 | 145 | |
fd3c2e87 C |
146 | this.statsP2PBytes.pendingUpload.push(bytes) |
147 | this.statsP2PBytes.totalUpload += bytes | |
3b6f205c C |
148 | }) |
149 | ||
150 | this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) | |
151 | this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) | |
152 | ||
153 | this.networkInfoInterval = setInterval(() => { | |
09209296 C |
154 | const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) |
155 | const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) | |
156 | ||
157 | const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) | |
3b6f205c C |
158 | |
159 | this.statsP2PBytes.pendingDownload = [] | |
160 | this.statsP2PBytes.pendingUpload = [] | |
09209296 | 161 | this.statsHTTPBytes.pendingDownload = [] |
3b6f205c C |
162 | |
163 | return this.player.trigger('p2pInfo', { | |
17152837 | 164 | source: 'p2p-media-loader', |
09209296 C |
165 | http: { |
166 | downloadSpeed: httpDownloadSpeed, | |
fd3c2e87 | 167 | downloaded: this.statsHTTPBytes.totalDownload |
09209296 | 168 | }, |
3b6f205c | 169 | p2p: { |
09209296 C |
170 | downloadSpeed: p2pDownloadSpeed, |
171 | uploadSpeed: p2pUploadSpeed, | |
3b6f205c C |
172 | numPeers: this.statsP2PBytes.numPeers, |
173 | downloaded: this.statsP2PBytes.totalDownload, | |
174 | uploaded: this.statsP2PBytes.totalUpload | |
4e11d8f3 C |
175 | }, |
176 | bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8 | |
3b6f205c C |
177 | } as PlayerNetworkInfo) |
178 | }, this.CONSTANTS.INFO_SCHEDULER) | |
179 | } | |
09209296 C |
180 | |
181 | private arraySum (data: number[]) { | |
182 | return data.reduce((a: number, b: number) => a + b, 0) | |
183 | } | |
2adfc7ea C |
184 | } |
185 | ||
186 | videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) | |
187 | export { P2pMediaLoaderPlugin } |