]>
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' | |
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 | ||
7e37e111 | 39 | constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) { |
f5fcd9f7 | 40 | super(player) |
2adfc7ea | 41 | |
09209296 C |
42 | this.options = options |
43 | ||
f5fcd9f7 C |
44 | // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 |
45 | if (!(videojs as any).Html5Hlsjs) { | |
69a01996 | 46 | console.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.') |
96cb4527 | 47 | |
69a01996 C |
48 | if (!player.canPlayType('application/vnd.apple.mpegurl')) { |
49 | const message = 'Cannot fallback to built-in HLS' | |
50 | console.warn(message) | |
96cb4527 | 51 | |
69a01996 C |
52 | player.ready(() => player.trigger('error', new Error(message))) |
53 | return | |
54 | } | |
55 | } else { | |
56 | // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 | |
57 | (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => { | |
58 | this.hlsjs = hlsjs | |
59 | }) | |
3b6f205c | 60 | |
69a01996 C |
61 | initVideoJsContribHlsJsPlayer(player) |
62 | } | |
2adfc7ea | 63 | |
e2f01c47 C |
64 | this.startTime = timeToInt(options.startTime) |
65 | ||
2adfc7ea C |
66 | player.src({ |
67 | type: options.type, | |
68 | src: options.src | |
69 | }) | |
09209296 | 70 | |
69a01996 C |
71 | player.ready(() => { |
72 | this.initializeCore() | |
6ec0b75b | 73 | |
69a01996 C |
74 | if ((videojs as any).Html5Hlsjs) { |
75 | this.initializePlugin() | |
76 | } | |
77 | }) | |
2adfc7ea C |
78 | } |
79 | ||
3b6f205c | 80 | dispose () { |
6ec0b75b C |
81 | if (this.hlsjs) this.hlsjs.destroy() |
82 | if (this.p2pEngine) this.p2pEngine.destroy() | |
83 | ||
3b6f205c C |
84 | clearInterval(this.networkInfoInterval) |
85 | } | |
86 | ||
4e11d8f3 | 87 | getCurrentLevel () { |
3e254de8 | 88 | return this.hlsjs.levels[this.hlsjs.currentLevel] |
4e11d8f3 C |
89 | } |
90 | ||
91 | getLiveLatency () { | |
3e254de8 C |
92 | // FIXME: typings |
93 | return Math.round((this.hlsjs as any).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 | ||
f5fcd9f7 C |
115 | // FIXME: typings |
116 | const options = this.player.tech(true).options_ as any | |
117 | this.p2pEngine = options.hlsjsConfig.loader.getEngine() | |
3b6f205c | 118 | |
83fcadac | 119 | this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHING, (_: any, data: any) => { |
3b6f205c C |
120 | this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height }) |
121 | }) | |
122 | ||
ffacd78f C |
123 | this.hlsjs.on(Hlsjs.Events.MANIFEST_LOADED, (_: any, data: any) => { |
124 | this.trigger('resolutionsLoaded') | |
125 | }) | |
126 | ||
da332417 | 127 | this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => { |
09209296 | 128 | console.error('Segment error.', segment, err) |
da332417 | 129 | |
b82df0a3 | 130 | this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl) |
09209296 C |
131 | }) |
132 | ||
da332417 | 133 | this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls() |
09209296 | 134 | |
3b6f205c C |
135 | this.runStats() |
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) => { |
09209296 C |
147 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
148 | ||
3e254de8 C |
149 | elem.pendingUpload.push(bytes) |
150 | elem.totalUpload += bytes | |
3b6f205c C |
151 | }) |
152 | ||
153 | this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) | |
154 | this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) | |
155 | ||
156 | this.networkInfoInterval = setInterval(() => { | |
09209296 C |
157 | const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) |
158 | const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) | |
159 | ||
160 | const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) | |
161 | const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload) | |
3b6f205c C |
162 | |
163 | this.statsP2PBytes.pendingDownload = [] | |
164 | this.statsP2PBytes.pendingUpload = [] | |
09209296 C |
165 | this.statsHTTPBytes.pendingDownload = [] |
166 | this.statsHTTPBytes.pendingUpload = [] | |
3b6f205c C |
167 | |
168 | return this.player.trigger('p2pInfo', { | |
17152837 | 169 | source: 'p2p-media-loader', |
09209296 C |
170 | http: { |
171 | downloadSpeed: httpDownloadSpeed, | |
172 | uploadSpeed: httpUploadSpeed, | |
173 | downloaded: this.statsHTTPBytes.totalDownload, | |
174 | uploaded: this.statsHTTPBytes.totalUpload | |
175 | }, | |
3b6f205c | 176 | p2p: { |
09209296 C |
177 | downloadSpeed: p2pDownloadSpeed, |
178 | uploadSpeed: p2pUploadSpeed, | |
3b6f205c C |
179 | numPeers: this.statsP2PBytes.numPeers, |
180 | downloaded: this.statsP2PBytes.totalDownload, | |
181 | uploaded: this.statsP2PBytes.totalUpload | |
4e11d8f3 C |
182 | }, |
183 | bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8 | |
3b6f205c C |
184 | } as PlayerNetworkInfo) |
185 | }, this.CONSTANTS.INFO_SCHEDULER) | |
186 | } | |
09209296 C |
187 | |
188 | private arraySum (data: number[]) { | |
189 | return data.reduce((a: number, b: number) => a + b, 0) | |
190 | } | |
2adfc7ea C |
191 | } |
192 | ||
193 | videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) | |
194 | export { P2pMediaLoaderPlugin } |