-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (xgplayer-flv) update download speed evaluate strategy
feat: (xgplayer-flv) export configuration for speed evaluate fix: (xgplayer-flv) seamless switch error case canplay、playing event emit again feat: (xgplayer-flv) export totalSize、 totalCost for speedInfo()
- Loading branch information
Showing
4 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 88 additions & 10 deletions
98
packages/xgplayer-streaming-shared/src/services/bandwidth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,115 @@ | ||
|
||
|
||
const SKIP_SMALL_CHUNK = 1000 | ||
const MAX_CHUNK_SAVE_SIZE = 50 | ||
const MAX_SEGMENT_SAVE_SIZE = 3 | ||
const LONGTIME_NO_RECEIVE = 3000 | ||
|
||
export class BandwidthService { | ||
_chunkSpeeds = [] // bps | ||
_chunkSpeed = 0 // bps | ||
_chunkCache = [] | ||
_speeds = [] // bps | ||
_totalSize = 0 | ||
_totalCost = 0 | ||
|
||
/** | ||
* @typedef {{ | ||
* chunkCountForSpeed?: number, | ||
* skipChunkSize?: number, | ||
* longtimeNoReceived?: number | ||
* }} Opts | ||
* | ||
* @param {Opts} opts | ||
*/ | ||
constructor (opts) { | ||
this._opts = opts || {} | ||
} | ||
|
||
addRecord (totalByte, ms) { | ||
if (!totalByte || !ms) return | ||
this._speeds.push(8000 * totalByte / ms) | ||
this._speeds = this._speeds.slice(-3) | ||
this._speeds = this._speeds.slice(-MAX_SEGMENT_SAVE_SIZE) | ||
} | ||
|
||
addChunkRecord (totalByte, ms) { | ||
if (!totalByte || !ms) return | ||
this._chunkSpeeds.push(8000 * totalByte / ms) | ||
this._chunkSpeeds = this._chunkSpeeds.slice(-100) | ||
if (!totalByte || !ms || totalByte < (this._opts?.skipChunkSize || SKIP_SMALL_CHUNK)) return | ||
|
||
this._totalSize += totalByte | ||
this._totalCost += ms | ||
|
||
this._chunkSpeed = 8000 * totalByte / ms | ||
this._chunkCache.push({ | ||
size: totalByte, | ||
duration: ms, | ||
timestamp: performance.now() | ||
}) | ||
|
||
const size = this._opts?.chunkCountForSpeed || MAX_CHUNK_SAVE_SIZE | ||
|
||
if (this._chunkCache.length > size) { | ||
this._chunkCache = this._chunkCache.slice(-size) | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @returns { number } | ||
*/ | ||
getAvgSpeed () { | ||
if (!this._chunkSpeeds.length && !this._speeds.length) return 0 | ||
if (!this._chunkCache.length && !this._speeds.length) return 0 | ||
if (this._speeds.length) { | ||
return this._speeds.reduce((a, c) => (a += c)) / this._speeds.length | ||
} | ||
return this._chunkSpeeds.reduce((a, c) => (a += c)) / this._chunkSpeeds.length | ||
|
||
// for long time no chunk received, append 0 size chunk | ||
const lastSample = this._chunkCache[this._chunkCache.length - 1] | ||
const cost = performance.now() - lastSample.timestamp | ||
if ( cost > (this._opts?.longtimeNoReceived || LONGTIME_NO_RECEIVE)) { | ||
this._chunkCache.push({ | ||
size: 0, | ||
duration: cost, | ||
timestamp: performance.now() | ||
}) | ||
} | ||
|
||
const totalSize = this._chunkCache.reduce((a, c) => a += c.size, 0) | ||
const totalDuration = this._chunkCache.reduce((a, c) => a += c.duration, 0) | ||
return 8000 * totalSize / totalDuration | ||
} | ||
|
||
/** | ||
* | ||
* @returns { number } | ||
*/ | ||
getLatestSpeed () { | ||
if (!this._chunkSpeeds.length && !this._speeds.length) return 0 | ||
if (!this._chunkCache.length && !this._speeds.length) return 0 | ||
if (this._speeds.length) { | ||
return this._speeds[this._speeds.length - 1] | ||
} | ||
return this._chunkSpeeds[this._chunkSpeeds.length - 1] | ||
return this._chunkSpeed | ||
} | ||
|
||
/** | ||
* | ||
* @returns { number } | ||
*/ | ||
getTotalSize () { | ||
return this._totalSize | ||
} | ||
|
||
/** | ||
* | ||
* @returns { number } | ||
*/ | ||
getTotalCost () { | ||
return this._totalCost | ||
} | ||
|
||
reset () { | ||
this._chunkSpeeds = [] | ||
this._chunkCache = [] | ||
this._speeds = [] | ||
this._totalSize = 0 | ||
this._totalCost = 0 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters