]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
Fix videojs typings
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / p2p-media-loader-plugin.ts
CommitLineData
7e37e111 1import videojs from 'video.js/dist/alt/video.core.js'
f5fcd9f7 2import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
09209296 3import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
da332417 4import { Events, Segment } from 'p2p-media-loader-core'
e2f01c47 5import { timeToInt } from '../utils'
83fcadac 6import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
fd69c52f 7import * as Hlsjs from 'hls.js/dist/hls.light.js'
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
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) {
96cb4527
C
46 const message = 'HLS.js does not seem to be supported.'
47 console.warn(message)
48
49 player.ready(() => player.trigger('error', new Error(message)))
50 return
51 }
52
f5fcd9f7
C
53 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
54 (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
3b6f205c 55 this.hlsjs = hlsjs
3b6f205c
C
56 })
57
58 initVideoJsContribHlsJsPlayer(player)
2adfc7ea 59
e2f01c47
C
60 this.startTime = timeToInt(options.startTime)
61
2adfc7ea
C
62 player.src({
63 type: options.type,
64 src: options.src
65 })
09209296 66
e2f01c47 67 player.one('play', () => {
6ec0b75b
C
68 player.addClass('vjs-has-big-play-button-clicked')
69 })
70
09209296 71 player.ready(() => this.initialize())
2adfc7ea
C
72 }
73
3b6f205c 74 dispose () {
6ec0b75b
C
75 if (this.hlsjs) this.hlsjs.destroy()
76 if (this.p2pEngine) this.p2pEngine.destroy()
77
3b6f205c
C
78 clearInterval(this.networkInfoInterval)
79 }
80
6377a9f2
C
81 getHLSJS () {
82 return this.hlsjs
83 }
84
3b6f205c 85 private initialize () {
09209296
C
86 initHlsJsPlayer(this.hlsjs)
87
f5fcd9f7
C
88 // FIXME: typings
89 const options = this.player.tech(true).options_ as any
90 this.p2pEngine = options.hlsjsConfig.loader.getEngine()
3b6f205c 91
83fcadac 92 this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHING, (_: any, data: any) => {
3b6f205c
C
93 this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
94 })
95
da332417 96 this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
09209296 97 console.error('Segment error.', segment, err)
da332417 98
b82df0a3 99 this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
09209296
C
100 })
101
da332417 102 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
09209296 103
3b6f205c 104 this.runStats()
e2f01c47 105
e028d983 106 this.player.one('canplay', () => {
dfe4294a
C
107 if (this.startTime) {
108 this.player.currentTime(this.startTime)
109 }
dfe4294a 110 })
3b6f205c
C
111 }
112
113 private runStats () {
114 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
09209296
C
115 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
116
117 elem.pendingDownload.push(size)
118 elem.totalDownload += size
3b6f205c
C
119 })
120
121 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
09209296
C
122 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
123
124 elem.pendingUpload.push(size)
125 elem.totalUpload += size
3b6f205c
C
126 })
127
128 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
129 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
130
131 this.networkInfoInterval = setInterval(() => {
09209296
C
132 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
133 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
134
135 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
136 const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
3b6f205c
C
137
138 this.statsP2PBytes.pendingDownload = []
139 this.statsP2PBytes.pendingUpload = []
09209296
C
140 this.statsHTTPBytes.pendingDownload = []
141 this.statsHTTPBytes.pendingUpload = []
3b6f205c
C
142
143 return this.player.trigger('p2pInfo', {
09209296
C
144 http: {
145 downloadSpeed: httpDownloadSpeed,
146 uploadSpeed: httpUploadSpeed,
147 downloaded: this.statsHTTPBytes.totalDownload,
148 uploaded: this.statsHTTPBytes.totalUpload
149 },
3b6f205c 150 p2p: {
09209296
C
151 downloadSpeed: p2pDownloadSpeed,
152 uploadSpeed: p2pUploadSpeed,
3b6f205c
C
153 numPeers: this.statsP2PBytes.numPeers,
154 downloaded: this.statsP2PBytes.totalDownload,
155 uploaded: this.statsP2PBytes.totalUpload
156 }
157 } as PlayerNetworkInfo)
158 }, this.CONSTANTS.INFO_SCHEDULER)
159 }
09209296
C
160
161 private arraySum (data: number[]) {
162 return data.reduce((a: number, b: number) => a + b, 0)
163 }
2adfc7ea
C
164}
165
166videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
167export { P2pMediaLoaderPlugin }