]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
Improve HLS redundancy
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / p2p-media-loader-plugin.ts
CommitLineData
2adfc7ea
C
1// FIXME: something weird with our path definition in tsconfig and typings
2// @ts-ignore
3import * as videojs from 'video.js'
09209296
C
4import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo, VideoJSComponentInterface } from '../peertube-videojs-typings'
5import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
da332417 6import { Events, Segment } from 'p2p-media-loader-core'
e2f01c47 7import { timeToInt } from '../utils'
2adfc7ea
C
8
9// videojs-hlsjs-plugin needs videojs in window
10window['videojs'] = videojs
4348a27d 11require('@streamroot/videojs-hlsjs-plugin')
2adfc7ea 12
2adfc7ea
C
13const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
14class 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
4348a27d 21 private hlsjs: any // Don't type hlsjs to not bundle the module
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[],
32 pendingUpload: [] as number[],
33 totalDownload: 0,
34 totalUpload: 0
35 }
e2f01c47 36 private startTime: number
3b6f205c
C
37
38 private networkInfoInterval: any
39
2adfc7ea
C
40 constructor (player: videojs.Player, options: P2PMediaLoaderPluginOptions) {
41 super(player, options)
42
09209296
C
43 this.options = options
44
96cb4527
C
45 if (!videojs.Html5Hlsjs) {
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
4348a27d 53 videojs.Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
3b6f205c 54 this.hlsjs = hlsjs
3b6f205c
C
55 })
56
57 initVideoJsContribHlsJsPlayer(player)
2adfc7ea 58
e2f01c47
C
59 this.startTime = timeToInt(options.startTime)
60
2adfc7ea
C
61 player.src({
62 type: options.type,
63 src: options.src
64 })
09209296 65
e2f01c47 66 player.one('play', () => {
6ec0b75b
C
67 player.addClass('vjs-has-big-play-button-clicked')
68 })
69
09209296 70 player.ready(() => this.initialize())
2adfc7ea
C
71 }
72
3b6f205c 73 dispose () {
6ec0b75b
C
74 if (this.hlsjs) this.hlsjs.destroy()
75 if (this.p2pEngine) this.p2pEngine.destroy()
76
3b6f205c
C
77 clearInterval(this.networkInfoInterval)
78 }
79
80 private initialize () {
09209296
C
81 initHlsJsPlayer(this.hlsjs)
82
6ec0b75b
C
83 const tech = this.player.tech_
84 this.p2pEngine = tech.options_.hlsjsConfig.loader.getEngine()
3b6f205c 85
4348a27d
C
86 // Avoid using constants to not import hls.hs
87 // https://github.com/video-dev/hls.js/blob/master/src/events.js#L37
88 this.hlsjs.on('hlsLevelSwitching', (_: any, data: any) => {
3b6f205c
C
89 this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
90 })
91
da332417 92 this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
09209296 93 console.error('Segment error.', segment, err)
da332417
C
94
95 this.options.redundancyUrlManager.removeByOriginUrl(segment.url)
09209296
C
96 })
97
da332417 98 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
09209296 99
3b6f205c 100 this.runStats()
e2f01c47 101
e028d983 102 this.player.one('canplay', () => {
dfe4294a
C
103 if (this.startTime) {
104 this.player.currentTime(this.startTime)
105 }
dfe4294a 106 })
3b6f205c
C
107 }
108
109 private runStats () {
110 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
09209296
C
111 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
112
113 elem.pendingDownload.push(size)
114 elem.totalDownload += size
3b6f205c
C
115 })
116
117 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
09209296
C
118 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
119
120 elem.pendingUpload.push(size)
121 elem.totalUpload += size
3b6f205c
C
122 })
123
124 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
125 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
126
127 this.networkInfoInterval = setInterval(() => {
09209296
C
128 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
129 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
130
131 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
132 const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
3b6f205c
C
133
134 this.statsP2PBytes.pendingDownload = []
135 this.statsP2PBytes.pendingUpload = []
09209296
C
136 this.statsHTTPBytes.pendingDownload = []
137 this.statsHTTPBytes.pendingUpload = []
3b6f205c
C
138
139 return this.player.trigger('p2pInfo', {
09209296
C
140 http: {
141 downloadSpeed: httpDownloadSpeed,
142 uploadSpeed: httpUploadSpeed,
143 downloaded: this.statsHTTPBytes.totalDownload,
144 uploaded: this.statsHTTPBytes.totalUpload
145 },
3b6f205c 146 p2p: {
09209296
C
147 downloadSpeed: p2pDownloadSpeed,
148 uploadSpeed: p2pUploadSpeed,
3b6f205c
C
149 numPeers: this.statsP2PBytes.numPeers,
150 downloaded: this.statsP2PBytes.totalDownload,
151 uploaded: this.statsP2PBytes.totalUpload
152 }
153 } as PlayerNetworkInfo)
154 }, this.CONSTANTS.INFO_SCHEDULER)
155 }
09209296
C
156
157 private arraySum (data: number[]) {
158 return data.reduce((a: number, b: number) => a + b, 0)
159 }
2adfc7ea
C
160}
161
162videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
163export { P2pMediaLoaderPlugin }