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