aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts')
-rw-r--r--client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts23
1 files changed, 11 insertions, 12 deletions
diff --git a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
index e5f099dea..54d87aea5 100644
--- a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
+++ b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
@@ -2,10 +2,10 @@ import Hlsjs from 'hls.js'
2import videojs from 'video.js' 2import videojs from 'video.js'
3import { Events, Segment } from '@peertube/p2p-media-loader-core' 3import { Events, Segment } from '@peertube/p2p-media-loader-core'
4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs' 4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
5import { logger } from '@root-helpers/logger'
5import { timeToInt } from '@shared/core-utils' 6import { timeToInt } from '@shared/core-utils'
6import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types' 7import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types'
7import { registerConfigPlugin, registerSourceHandler } from './hls-plugin' 8import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
8import { logger } from '@root-helpers/logger'
9 9
10registerConfigPlugin(videojs) 10registerConfigPlugin(videojs)
11registerSourceHandler(videojs) 11registerSourceHandler(videojs)
@@ -29,9 +29,7 @@ class P2pMediaLoaderPlugin extends Plugin {
29 } 29 }
30 private statsHTTPBytes = { 30 private statsHTTPBytes = {
31 pendingDownload: [] as number[], 31 pendingDownload: [] as number[],
32 pendingUpload: [] as number[], 32 totalDownload: 0
33 totalDownload: 0,
34 totalUpload: 0
35 } 33 }
36 private startTime: number 34 private startTime: number
37 35
@@ -123,6 +121,8 @@ class P2pMediaLoaderPlugin extends Plugin {
123 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls() 121 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
124 122
125 this.runStats() 123 this.runStats()
124
125 this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engineResolutionChange'))
126 } 126 }
127 127
128 private runStats () { 128 private runStats () {
@@ -134,10 +134,13 @@ class P2pMediaLoaderPlugin extends Plugin {
134 }) 134 })
135 135
136 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => { 136 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
137 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes 137 if (method !== 'p2p') {
138 logger.error(`Received upload from unknown method ${method}`)
139 return
140 }
138 141
139 elem.pendingUpload.push(bytes) 142 this.statsP2PBytes.pendingUpload.push(bytes)
140 elem.totalUpload += bytes 143 this.statsP2PBytes.totalUpload += bytes
141 }) 144 })
142 145
143 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) 146 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
@@ -148,20 +151,16 @@ class P2pMediaLoaderPlugin extends Plugin {
148 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) 151 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
149 152
150 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) 153 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
151 const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
152 154
153 this.statsP2PBytes.pendingDownload = [] 155 this.statsP2PBytes.pendingDownload = []
154 this.statsP2PBytes.pendingUpload = [] 156 this.statsP2PBytes.pendingUpload = []
155 this.statsHTTPBytes.pendingDownload = [] 157 this.statsHTTPBytes.pendingDownload = []
156 this.statsHTTPBytes.pendingUpload = []
157 158
158 return this.player.trigger('p2pInfo', { 159 return this.player.trigger('p2pInfo', {
159 source: 'p2p-media-loader', 160 source: 'p2p-media-loader',
160 http: { 161 http: {
161 downloadSpeed: httpDownloadSpeed, 162 downloadSpeed: httpDownloadSpeed,
162 uploadSpeed: httpUploadSpeed, 163 downloaded: this.statsHTTPBytes.totalDownload
163 downloaded: this.statsHTTPBytes.totalDownload,
164 uploaded: this.statsHTTPBytes.totalUpload
165 }, 164 },
166 p2p: { 165 p2p: {
167 downloadSpeed: p2pDownloadSpeed, 166 downloadSpeed: p2pDownloadSpeed,