]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
Cleanup stats for nerds
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / p2p-media-loader-plugin.ts
CommitLineData
4e11d8f3
C
1import * as Hlsjs from 'hls.js/dist/hls.light.js'
2import { Events, Segment } from 'p2p-media-loader-core'
3import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
512decf3 4import videojs from 'video.js'
f5fcd9f7 5import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
e2f01c47 6import { timeToInt } from '../utils'
83fcadac 7import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
2adfc7ea 8
83fcadac
C
9registerConfigPlugin(videojs)
10registerSourceHandler(videojs)
2adfc7ea 11
f5fcd9f7 12const Plugin = videojs.getPlugin('plugin')
2adfc7ea
C
13class 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
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 () {
139 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
09209296
C
140 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
141
142 elem.pendingDownload.push(size)
143 elem.totalDownload += size
3b6f205c
C
144 })
145
146 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
09209296
C
147 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
148
149 elem.pendingUpload.push(size)
150 elem.totalUpload += size
3b6f205c
C
151 })
152
153 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
154 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
155
4e11d8f3
C
156 this.hlsjs.on(Hlsjs.Events.MANIFEST_PARSED, (_e, manifest) => {
157 this.hlsjsCurrentLevel = manifest.firstLevel
158 this.hlsjsLevels = manifest.levels
159 })
160 this.hlsjs.on(Hlsjs.Events.LEVEL_LOADED, (_e, level) => {
161 this.hlsjsCurrentLevel = level.levelId || (level as any).id
162 })
163
3b6f205c 164 this.networkInfoInterval = setInterval(() => {
09209296
C
165 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
166 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
167
168 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
169 const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
3b6f205c
C
170
171 this.statsP2PBytes.pendingDownload = []
172 this.statsP2PBytes.pendingUpload = []
09209296
C
173 this.statsHTTPBytes.pendingDownload = []
174 this.statsHTTPBytes.pendingUpload = []
3b6f205c
C
175
176 return this.player.trigger('p2pInfo', {
17152837 177 source: 'p2p-media-loader',
09209296
C
178 http: {
179 downloadSpeed: httpDownloadSpeed,
180 uploadSpeed: httpUploadSpeed,
181 downloaded: this.statsHTTPBytes.totalDownload,
182 uploaded: this.statsHTTPBytes.totalUpload
183 },
3b6f205c 184 p2p: {
09209296
C
185 downloadSpeed: p2pDownloadSpeed,
186 uploadSpeed: p2pUploadSpeed,
3b6f205c
C
187 numPeers: this.statsP2PBytes.numPeers,
188 downloaded: this.statsP2PBytes.totalDownload,
189 uploaded: this.statsP2PBytes.totalUpload
4e11d8f3
C
190 },
191 bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
3b6f205c
C
192 } as PlayerNetworkInfo)
193 }, this.CONSTANTS.INFO_SCHEDULER)
194 }
09209296
C
195
196 private arraySum (data: number[]) {
197 return data.reduce((a: number, b: number) => a + b, 0)
198 }
2adfc7ea
C
199}
200
201videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
202export { P2pMediaLoaderPlugin }