From a87316cb446cf2fdac4b67eb78199cc666f07290 Mon Sep 17 00:00:00 2001 From: wudechang Date: Mon, 31 Mar 2025 17:20:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81flv=E9=A2=84=E6=8B=89?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/xgplayer-flv/src/flv/index.js | 8 ++++---- packages/xgplayer-flv/src/plugin.js | 8 +++++++- packages/xgplayer-streaming-shared/src/net/fetch.js | 12 ++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/xgplayer-flv/src/flv/index.js b/packages/xgplayer-flv/src/flv/index.js index 3978ddad..919b92dc 100644 --- a/packages/xgplayer-flv/src/flv/index.js +++ b/packages/xgplayer-flv/src/flv/index.js @@ -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) diff --git a/packages/xgplayer-flv/src/plugin.js b/packages/xgplayer-flv/src/plugin.js index ff7bf763..6f00b560 100644 --- a/packages/xgplayer-flv/src/plugin.js +++ b/packages/xgplayer-flv/src/plugin.js @@ -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() diff --git a/packages/xgplayer-streaming-shared/src/net/fetch.js b/packages/xgplayer-streaming-shared/src/net/fetch.js index 00b37e02..6da61041 100644 --- a/packages/xgplayer-streaming-shared/src/net/fetch.js +++ b/packages/xgplayer-streaming-shared/src/net/fetch.js @@ -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