]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
Force videojs to not recreate <video>
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / shared / p2p-media-loader / p2p-media-loader-plugin.ts
CommitLineData
134006b0 1import Hlsjs from 'hls.js'
512decf3 2import videojs from 'video.js'
3e254de8
C
3import { Events, Segment } from '@peertube/p2p-media-loader-core'
4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
fd3c2e87 5import { logger } from '@root-helpers/logger'
15a7eafb 6import { timeToInt } from '@shared/core-utils'
57d65032 7import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types'
83fcadac 8import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
2adfc7ea 9
83fcadac
C
10registerConfigPlugin(videojs)
11registerSourceHandler(videojs)
2adfc7ea 12
f5fcd9f7 13const Plugin = videojs.getPlugin('plugin')
2adfc7ea
C
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
83fcadac 21 private hlsjs: Hlsjs
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[],
fd3c2e87 32 totalDownload: 0
09209296 33 }
e2f01c47 34 private startTime: number
3b6f205c
C
35
36 private networkInfoInterval: any
37
7e37e111 38 constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
f5fcd9f7 39 super(player)
2adfc7ea 40
09209296
C
41 this.options = options
42
f5fcd9f7
C
43 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
44 if (!(videojs as any).Html5Hlsjs) {
42b40636 45 logger.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.')
96cb4527 46
326f3692 47 let message: string
69a01996 48 if (!player.canPlayType('application/vnd.apple.mpegurl')) {
326f3692
C
49 message = 'Cannot fallback to built-in HLS'
50 } else if (options.requiresAuth) {
51 message = 'Video requires auth which is not compatible to build-in HLS player'
52 }
53
54 if (message) {
42b40636 55 logger.warn(message)
96cb4527 56
0b8e50aa
C
57 const error: MediaError = {
58 code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
59 message,
60 MEDIA_ERR_ABORTED: MediaError.MEDIA_ERR_ABORTED,
61 MEDIA_ERR_DECODE: MediaError.MEDIA_ERR_DECODE,
62 MEDIA_ERR_NETWORK: MediaError.MEDIA_ERR_NETWORK,
63 MEDIA_ERR_SRC_NOT_SUPPORTED: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED
64 }
65
66 player.ready(() => player.error(error))
69a01996
C
67 return
68 }
32f44a01
C
69
70 // Workaround to force video.js to not re create a video element
71 (this.player as any).playerElIngest_ = this.player.el().parentNode
69a01996
C
72 } else {
73 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
74 (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
75 this.hlsjs = hlsjs
76 })
3b6f205c 77
69a01996
C
78 initVideoJsContribHlsJsPlayer(player)
79 }
2adfc7ea 80
e2f01c47
C
81 this.startTime = timeToInt(options.startTime)
82
2adfc7ea
C
83 player.src({
84 type: options.type,
85 src: options.src
86 })
09209296 87
69a01996
C
88 player.ready(() => {
89 this.initializeCore()
6ec0b75b 90
69a01996
C
91 if ((videojs as any).Html5Hlsjs) {
92 this.initializePlugin()
93 }
94 })
2adfc7ea
C
95 }
96
3b6f205c 97 dispose () {
6ec0b75b
C
98 if (this.hlsjs) this.hlsjs.destroy()
99 if (this.p2pEngine) this.p2pEngine.destroy()
100
3b6f205c
C
101 clearInterval(this.networkInfoInterval)
102 }
103
4e11d8f3 104 getCurrentLevel () {
f2c29ced
C
105 if (!this.hlsjs) return undefined
106
3e254de8 107 return this.hlsjs.levels[this.hlsjs.currentLevel]
4e11d8f3
C
108 }
109
110 getLiveLatency () {
02b2e482 111 return Math.round(this.hlsjs.latency)
4e11d8f3
C
112 }
113
6377a9f2
C
114 getHLSJS () {
115 return this.hlsjs
116 }
117
69a01996
C
118 private initializeCore () {
119 this.player.one('play', () => {
120 this.player.addClass('vjs-has-big-play-button-clicked')
121 })
122
123 this.player.one('canplay', () => {
124 if (this.startTime) {
125 this.player.currentTime(this.startTime)
126 }
127 })
128 }
129
130 private initializePlugin () {
09209296
C
131 initHlsJsPlayer(this.hlsjs)
132
9597920e 133 this.p2pEngine = this.options.loader.getEngine()
3b6f205c 134
da332417 135 this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
f2a16d93 136 if (navigator.onLine === false) return
137
42b40636 138 logger.error(`Segment ${segment.id} error.`, err)
da332417 139
b82df0a3 140 this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
09209296
C
141 })
142
da332417 143 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
09209296 144
3b6f205c 145 this.runStats()
fd3c2e87
C
146
147 this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engineResolutionChange'))
3b6f205c
C
148 }
149
150 private runStats () {
3e254de8 151 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
09209296
C
152 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
153
3e254de8
C
154 elem.pendingDownload.push(bytes)
155 elem.totalDownload += bytes
3b6f205c
C
156 })
157
3e254de8 158 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
fd3c2e87
C
159 if (method !== 'p2p') {
160 logger.error(`Received upload from unknown method ${method}`)
161 return
162 }
09209296 163
fd3c2e87
C
164 this.statsP2PBytes.pendingUpload.push(bytes)
165 this.statsP2PBytes.totalUpload += bytes
3b6f205c
C
166 })
167
168 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
169 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
170
171 this.networkInfoInterval = setInterval(() => {
09209296
C
172 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
173 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
174
175 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
3b6f205c
C
176
177 this.statsP2PBytes.pendingDownload = []
178 this.statsP2PBytes.pendingUpload = []
09209296 179 this.statsHTTPBytes.pendingDownload = []
3b6f205c
C
180
181 return this.player.trigger('p2pInfo', {
17152837 182 source: 'p2p-media-loader',
09209296
C
183 http: {
184 downloadSpeed: httpDownloadSpeed,
fd3c2e87 185 downloaded: this.statsHTTPBytes.totalDownload
09209296 186 },
3b6f205c 187 p2p: {
09209296
C
188 downloadSpeed: p2pDownloadSpeed,
189 uploadSpeed: p2pUploadSpeed,
3b6f205c
C
190 numPeers: this.statsP2PBytes.numPeers,
191 downloaded: this.statsP2PBytes.totalDownload,
192 uploaded: this.statsP2PBytes.totalUpload
4e11d8f3
C
193 },
194 bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
3b6f205c
C
195 } as PlayerNetworkInfo)
196 }, this.CONSTANTS.INFO_SCHEDULER)
197 }
09209296
C
198
199 private arraySum (data: number[]) {
200 return data.reduce((a: number, b: number) => a + b, 0)
201 }
2adfc7ea
C
202}
203
204videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
205export { P2pMediaLoaderPlugin }