aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/p2p-media-loader
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-08-03 11:51:49 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-08-05 10:57:49 +0200
commit3e254de8bef59e4e25b74d1b0fde07de29654ada (patch)
tree9496e386f499701e53b531f92994045f0eea93da /client/src/assets/player/p2p-media-loader
parentff4de38385049bf8f6e1d76d8511854fcfabc71c (diff)
downloadPeerTube-3e254de8bef59e4e25b74d1b0fde07de29654ada.tar.gz
PeerTube-3e254de8bef59e4e25b74d1b0fde07de29654ada.tar.zst
PeerTube-3e254de8bef59e4e25b74d1b0fde07de29654ada.zip
HLS v1 support
Diffstat (limited to 'client/src/assets/player/p2p-media-loader')
-rw-r--r--client/src/assets/player/p2p-media-loader/hls-plugin.ts16
-rw-r--r--client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts34
-rw-r--r--client/src/assets/player/p2p-media-loader/segment-url-builder.ts2
-rw-r--r--client/src/assets/player/p2p-media-loader/segment-validator.ts2
4 files changed, 19 insertions, 35 deletions
diff --git a/client/src/assets/player/p2p-media-loader/hls-plugin.ts b/client/src/assets/player/p2p-media-loader/hls-plugin.ts
index 53969a5a5..3050110cd 100644
--- a/client/src/assets/player/p2p-media-loader/hls-plugin.ts
+++ b/client/src/assets/player/p2p-media-loader/hls-plugin.ts
@@ -264,20 +264,16 @@ class Html5Hlsjs {
264 if (this.errorCounts[ data.type ]) this.errorCounts[ data.type ] += 1 264 if (this.errorCounts[ data.type ]) this.errorCounts[ data.type ] += 1
265 else this.errorCounts[ data.type ] = 1 265 else this.errorCounts[ data.type ] = 1
266 266
267 if (!data.fatal) { 267 if (data.fatal) console.warn(error.message)
268 console.warn(error.message) 268 else console.error(error.message, data)
269 return
270 }
271
272 console.error(error.message)
273 269
274 if (data.type === Hlsjs.ErrorTypes.NETWORK_ERROR) { 270 if (data.type === Hlsjs.ErrorTypes.NETWORK_ERROR) {
275 error.code = 2 271 error.code = 2
276 this._handleNetworkError(error) 272 this._handleNetworkError(error)
277 } else if (data.type === Hlsjs.ErrorTypes.MEDIA_ERROR && data.details !== 'manifestIncompatibleCodecsError') { 273 } else if (data.fatal && data.type === Hlsjs.ErrorTypes.MEDIA_ERROR && data.details !== 'manifestIncompatibleCodecsError') {
278 error.code = 3 274 error.code = 3
279 this._handleMediaError(error) 275 this._handleMediaError(error)
280 } else { 276 } else if (data.fatal) {
281 this.hls.destroy() 277 this.hls.destroy()
282 console.info('bubbling error up to VIDEOJS') 278 console.info('bubbling error up to VIDEOJS')
283 this.tech.error = () => error as any 279 this.tech.error = () => error as any
@@ -286,12 +282,12 @@ class Html5Hlsjs {
286 } 282 }
287 283
288 private switchQuality (qualityId: number) { 284 private switchQuality (qualityId: number) {
289 this.hls.nextLevel = qualityId 285 this.hls.currentLevel = qualityId
290 } 286 }
291 287
292 private _levelLabel (level: Hlsjs.Level) { 288 private _levelLabel (level: Hlsjs.Level) {
293 if (this.player.srOptions_.levelLabelHandler) { 289 if (this.player.srOptions_.levelLabelHandler) {
294 return this.player.srOptions_.levelLabelHandler(level) 290 return this.player.srOptions_.levelLabelHandler(level as any)
295 } 291 }
296 292
297 if (level.height) return level.height + 'p' 293 if (level.height) return level.height + 'p'
diff --git a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
index 2eb849d2b..093795e48 100644
--- a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
+++ b/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
@@ -1,7 +1,7 @@
1import * as Hlsjs from 'hls.js/dist/hls.light.js' 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'
4import videojs from 'video.js' 2import videojs from 'video.js'
3import { Events, Segment } from '@peertube/p2p-media-loader-core'
4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
5import { timeToInt } from '@shared/core-utils' 5import { timeToInt } from '@shared/core-utils'
6import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings' 6import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
7import { registerConfigPlugin, registerSourceHandler } from './hls-plugin' 7import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
@@ -36,9 +36,6 @@ class P2pMediaLoaderPlugin extends Plugin {
36 36
37 private networkInfoInterval: any 37 private networkInfoInterval: any
38 38
39 private hlsjsCurrentLevel: number
40 private hlsjsLevels: Hlsjs.Level[]
41
42 constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) { 39 constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
43 super(player) 40 super(player)
44 41
@@ -88,13 +85,12 @@ class P2pMediaLoaderPlugin extends Plugin {
88 } 85 }
89 86
90 getCurrentLevel () { 87 getCurrentLevel () {
91 return this.hlsjsLevels.find(l => l.level === this.hlsjsCurrentLevel) 88 return this.hlsjs.levels[this.hlsjs.currentLevel]
92 } 89 }
93 90
94 getLiveLatency () { 91 getLiveLatency () {
95 return undefined as number 92 // FIXME: typings
96 // FIXME: Use latency when hls >= V1 93 return Math.round((this.hlsjs as any).latency)
97 // return this.hlsjs.latency
98 } 94 }
99 95
100 getHLSJS () { 96 getHLSJS () {
@@ -140,31 +136,23 @@ class P2pMediaLoaderPlugin extends Plugin {
140 } 136 }
141 137
142 private runStats () { 138 private runStats () {
143 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => { 139 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
144 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes 140 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
145 141
146 elem.pendingDownload.push(size) 142 elem.pendingDownload.push(bytes)
147 elem.totalDownload += size 143 elem.totalDownload += bytes
148 }) 144 })
149 145
150 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => { 146 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
151 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes 147 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
152 148
153 elem.pendingUpload.push(size) 149 elem.pendingUpload.push(bytes)
154 elem.totalUpload += size 150 elem.totalUpload += bytes
155 }) 151 })
156 152
157 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) 153 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
158 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) 154 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
159 155
160 this.hlsjs.on(Hlsjs.Events.MANIFEST_PARSED, (_e, manifest) => {
161 this.hlsjsCurrentLevel = manifest.firstLevel
162 this.hlsjsLevels = manifest.levels
163 })
164 this.hlsjs.on(Hlsjs.Events.LEVEL_LOADED, (_e, level) => {
165 this.hlsjsCurrentLevel = level.levelId || (level as any).id
166 })
167
168 this.networkInfoInterval = setInterval(() => { 156 this.networkInfoInterval = setInterval(() => {
169 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) 157 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
170 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) 158 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
diff --git a/client/src/assets/player/p2p-media-loader/segment-url-builder.ts b/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
index 039777cea..ad0e460ae 100644
--- a/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
+++ b/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
@@ -1,4 +1,4 @@
1import { Segment } from 'p2p-media-loader-core' 1import { Segment } from '@peertube/p2p-media-loader-core'
2import { RedundancyUrlManager } from './redundancy-url-manager' 2import { RedundancyUrlManager } from './redundancy-url-manager'
3 3
4function segmentUrlBuilderFactory (redundancyUrlManager: RedundancyUrlManager) { 4function segmentUrlBuilderFactory (redundancyUrlManager: RedundancyUrlManager) {
diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts
index 4a0caec5e..a28474793 100644
--- a/client/src/assets/player/p2p-media-loader/segment-validator.ts
+++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts
@@ -1,5 +1,5 @@
1import { wait } from '@root-helpers/utils' 1import { wait } from '@root-helpers/utils'
2import { Segment } from 'p2p-media-loader-core' 2import { Segment } from '@peertube/p2p-media-loader-core'
3import { basename } from 'path' 3import { basename } from 'path'
4 4
5type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } } 5type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }