feat: 支持flv预拉流

This commit is contained in:
wudechang 2025-03-31 17:20:04 +08:00
parent 6851672144
commit a87316cb44
3 changed files with 21 additions and 7 deletions

View File

@ -163,11 +163,11 @@ export class Flv extends EventEmitter {
* @param {string} [url]
* @return {Promise}
*/
async load (url, reuseMse = false) {
async load (url, reuseMse = false, streamRes) {
if (!this._bufferService) return
await this._reset(reuseMse)
this._loadData(url, this._opts.isLive ? [] : [0, this._opts.defaultVodLoadSize])
this._loadData(url, this._opts.isLive ? [] : [0, this._opts.defaultVodLoadSize], streamRes)
clearTimeout(this._tickTimer)
this._tickTimer = setTimeout(this._tick, this._tickInterval)
@ -298,7 +298,7 @@ export class Flv extends EventEmitter {
await this._bufferService.reset(reuseMse)
}
async _loadData (url, range) {
async _loadData (url, range, streamRes) {
if (url) this._opts.url = url
let finnalUrl = (url = this._opts.url)
if (!url) throw new Error('Source url is missing')
@ -322,7 +322,7 @@ export class Flv extends EventEmitter {
this._loading = true
try {
await this._mediaLoader.load({ url: finnalUrl, range })
await this._mediaLoader.load({ url: finnalUrl, range, streamRes })
} catch (error) {
this._loading = false
return this._emitError(StreamingError.network(error), false)

View File

@ -114,7 +114,9 @@ export class FlvPlugin extends BasePlugin {
this._transCoreEvent(EVENT.SWITCH_URL_SUCCESS)
this._transCoreEvent(EVENT.SWITCH_URL_FAILED)
this.flv.load(config.url, true)
if (!flvOpts.manualLoad) {
this.loadSource(config.url)
}
}
/**
@ -124,6 +126,10 @@ export class FlvPlugin extends BasePlugin {
return this.flv?.getStats()
}
loadSource = (url = this.player.config.url, streamRes) => {
this.flv?.load(this.player.config.url, true, streamRes)
}
destroy = () => {
if (this.flv) {
this.flv.destroy()

View File

@ -49,7 +49,8 @@ export class FetchLoader extends EventEmitter {
referrer,
referrerPolicy,
onProcessMinLen,
priOptions
priOptions,
streamRes
}) {
this._logger = logger
this._aborted = false
@ -108,7 +109,14 @@ export class FetchLoader extends EventEmitter {
const startTime = Date.now()
this._logger.debug('[fetch load start], index,', index, ',range,', range)
return new Promise((resolve, reject) => {
fetch(request || url, request ? undefined : init).then(async (response) => {
const promise = streamRes
? new Promise(r => {
// const response = new Response(stream)
// Object.defineProperty(response, 'url', { value: url })
r(streamRes)
})
: fetch(request || url, request ? undefined : init)
promise.then(async (response) => {
clearTimeout(this._timeoutTimer)
this._response = response
if (this._aborted || !this._running) return