From 2c9af48cc722de4fb03608a93e9583b3d441fe4d Mon Sep 17 00:00:00 2001 From: FredTsang <17154608+FredZeng@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:56:58 +0800 Subject: [PATCH 1/3] feat: fire 'INIT_DEMUXER' event --- docs/api.md | 1 + src/core/transmuxer.js | 8 ++++++++ src/core/transmuxing-controller.js | 4 ++++ src/core/transmuxing-events.js | 1 + src/core/transmuxing-worker.js | 8 ++++++++ src/player/mse-player.js | 3 +++ src/player/player-events.js | 3 ++- 7 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 5b5d4d25..2210100b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -204,6 +204,7 @@ A series of constants that can be used with `Player.on()` / `Player.off()`. They | PES_PRIVATE_DATA_ARRIVED | Provides ISO/IEC 13818-1 PES packets containing private data (stream_type=0x06) callback | | STATISTICS_INFO | Provides playback statistics information like dropped frames, current speed, etc. | | DESTROYING | Fired when the player begins teardown | +| INIT_DEMUXER | Fired when demuxer first created | ### mpegts.ErrorTypes diff --git a/src/core/transmuxer.js b/src/core/transmuxer.js index bedeff03..6b3c01f7 100644 --- a/src/core/transmuxer.js +++ b/src/core/transmuxer.js @@ -54,6 +54,7 @@ class Transmuxer { if (this._controller) { let ctl = this._controller; ctl.on(TransmuxingEvents.IO_ERROR, this._onIOError.bind(this)); + ctl.on(TransmuxingEvents.INIT_DEMUXER, this._onInitDemuxer.bind(this)); ctl.on(TransmuxingEvents.DEMUX_ERROR, this._onDemuxError.bind(this)); ctl.on(TransmuxingEvents.INIT_SEGMENT, this._onInitSegment.bind(this)); ctl.on(TransmuxingEvents.MEDIA_SEGMENT, this._onMediaSegment.bind(this)); @@ -239,6 +240,12 @@ class Transmuxer { }); } + _onInitDemuxer(probeData) { + Promise.resolve().then(() => { + this._emitter.emit(TransmuxingEvents.INIT_DEMUXER, probeData); + }); + } + _onDemuxError(type, info) { Promise.resolve().then(() => { this._emitter.emit(TransmuxingEvents.DEMUX_ERROR, type, info); @@ -281,6 +288,7 @@ class Transmuxer { Object.setPrototypeOf(data, MediaInfo.prototype); this._emitter.emit(message.msg, data); break; + case TransmuxingEvents.INIT_DEMUXER: case TransmuxingEvents.METADATA_ARRIVED: case TransmuxingEvents.SCRIPTDATA_ARRIVED: case TransmuxingEvents.TIMED_ID3_METADATA_ARRIVED: diff --git a/src/core/transmuxing-controller.js b/src/core/transmuxing-controller.js index 696d9394..4030def4 100644 --- a/src/core/transmuxing-controller.js +++ b/src/core/transmuxing-controller.js @@ -264,6 +264,10 @@ class TransmuxingController { } } + if (this._demuxer) { + this._emitter.emit(TransmuxingEvents.INIT_DEMUXER, probeData); + } + if (!probeData.match && !probeData.needMoreData) { // Both probing as FLV / MPEG-TS failed, report error probeData = null; diff --git a/src/core/transmuxing-events.js b/src/core/transmuxing-events.js index 1ffa8966..796875d0 100644 --- a/src/core/transmuxing-events.js +++ b/src/core/transmuxing-events.js @@ -19,6 +19,7 @@ const TransmuxingEvents = { IO_ERROR: 'io_error', DEMUX_ERROR: 'demux_error', + INIT_DEMUXER: 'init_demuxer', INIT_SEGMENT: 'init_segment', MEDIA_SEGMENT: 'media_segment', LOADING_COMPLETE: 'loading_complete', diff --git a/src/core/transmuxing-worker.js b/src/core/transmuxing-worker.js index e93714f3..4a05b184 100644 --- a/src/core/transmuxing-worker.js +++ b/src/core/transmuxing-worker.js @@ -48,6 +48,7 @@ let TransmuxingWorker = function (self) { case 'init': controller = new TransmuxingController(e.data.param[0], e.data.param[1]); controller.on(TransmuxingEvents.IO_ERROR, onIOError.bind(this)); + controller.on(TransmuxingEvents.INIT_DEMUXER, onInitDemuxer.bind(this)); controller.on(TransmuxingEvents.DEMUX_ERROR, onDemuxError.bind(this)); controller.on(TransmuxingEvents.INIT_SEGMENT, onInitSegment.bind(this)); controller.on(TransmuxingEvents.MEDIA_SEGMENT, onMediaSegment.bind(this)); @@ -236,6 +237,13 @@ let TransmuxingWorker = function (self) { }); } + function onInitDemuxer(probeData) { + self.postMessage({ + msg: TransmuxingEvents.INIT_DEMUXER, + data: probeData, + }); + } + function onDemuxError(type, info) { self.postMessage({ msg: TransmuxingEvents.DEMUX_ERROR, diff --git a/src/player/mse-player.js b/src/player/mse-player.js index a175e185..b9ac3a3b 100644 --- a/src/player/mse-player.js +++ b/src/player/mse-player.js @@ -240,6 +240,9 @@ class MSEPlayer { this._transmuxer.on(TransmuxingEvents.IO_ERROR, (detail, info) => { this._emitter.emit(PlayerEvents.ERROR, ErrorTypes.NETWORK_ERROR, detail, info); }); + this._transmuxer.on(TransmuxingEvents.INIT_DEMUXER, (probeData) => { + this._emitter.emit(PlayerEvents.INIT_DEMUXER, probeData); + }); this._transmuxer.on(TransmuxingEvents.DEMUX_ERROR, (detail, info) => { this._emitter.emit(PlayerEvents.ERROR, ErrorTypes.MEDIA_ERROR, detail, {code: -1, msg: info}); }); diff --git a/src/player/player-events.js b/src/player/player-events.js index 4993430f..276a4d6f 100644 --- a/src/player/player-events.js +++ b/src/player/player-events.js @@ -31,7 +31,8 @@ const PlayerEvents = { PES_PRIVATE_DATA_DESCRIPTOR: 'pes_private_data_descriptor', PES_PRIVATE_DATA_ARRIVED: 'pes_private_data_arrived', STATISTICS_INFO: 'statistics_info', - DESTROYING: 'destroying' + DESTROYING: 'destroying', + INIT_DEMUXER: 'init_demuxer', }; export default PlayerEvents; \ No newline at end of file From a821b46c55c9d47f89c2208ee7ba23380d7fa57f Mon Sep 17 00:00:00 2001 From: FredTsang <17154608+FredZeng@users.noreply.github.com> Date: Thu, 18 Jan 2024 00:46:18 +0800 Subject: [PATCH 2/3] fix: merge error --- src/player/player-engine-main-thread.ts | 3 +++ src/player/player-engine-worker-msg-def.ts | 3 ++- src/player/player-engine-worker.ts | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/player/player-engine-main-thread.ts b/src/player/player-engine-main-thread.ts index 91e39bd8..ed632e4a 100644 --- a/src/player/player-engine-main-thread.ts +++ b/src/player/player-engine-main-thread.ts @@ -206,6 +206,9 @@ class PlayerEngineMainThread implements PlayerEngine { this._transmuxer.on(TransmuxingEvents.IO_ERROR, (detail: any, info: any) => { this._emitter.emit(PlayerEvents.ERROR, ErrorTypes.NETWORK_ERROR, detail, info); }); + this._transmuxer.on(TransmuxingEvents.INIT_DEMUXER, (probeData: any) => { + this._emitter.emit(PlayerEvents.INIT_DEMUXER, probeData); + }); this._transmuxer.on(TransmuxingEvents.DEMUX_ERROR, (detail: any, info: any) => { this._emitter.emit(PlayerEvents.ERROR, ErrorTypes.MEDIA_ERROR, detail, info); }); diff --git a/src/player/player-engine-worker-msg-def.ts b/src/player/player-engine-worker-msg-def.ts index 714d6e3f..ce4aa4a7 100644 --- a/src/player/player-engine-worker-msg-def.ts +++ b/src/player/player-engine-worker-msg-def.ts @@ -67,7 +67,8 @@ export type WorkerMessagePacketPlayerEventExtraData = WorkerMessagePacketPlayerE | PlayerEvents.SMPTE2038_METADATA_ARRIVED | PlayerEvents.SCTE35_METADATA_ARRIVED | PlayerEvents.PES_PRIVATE_DATA_DESCRIPTOR - | PlayerEvents.PES_PRIVATE_DATA_ARRIVED, + | PlayerEvents.PES_PRIVATE_DATA_ARRIVED + | PlayerEvents.INIT_DEMUXER, extraData: any, }; diff --git a/src/player/player-engine-worker.ts b/src/player/player-engine-worker.ts index 9e4da036..592353ae 100644 --- a/src/player/player-engine-worker.ts +++ b/src/player/player-engine-worker.ts @@ -222,6 +222,9 @@ const PlayerEngineWorker = (self: DedicatedWorkerGlobalScope) => { info: info, } as WorkerMessagePacketPlayerEventError); }); + transmuxer.on(TransmuxingEvents.INIT_DEMUXER, (probeData: any) => { + emitPlayerEventsExtraData(PlayerEvents.INIT_DEMUXER, probeData); + }); transmuxer.on(TransmuxingEvents.DEMUX_ERROR, (detail: any, info: any) => { self.postMessage({ msg: 'player_event', From fad5c3d1c61f6becccfee321d3ae46d6bcbff2c6 Mon Sep 17 00:00:00 2001 From: FredTsang <17154608+FredZeng@users.noreply.github.com> Date: Thu, 18 Jan 2024 00:55:43 +0800 Subject: [PATCH 3/3] feat: update d.ts --- d.ts/src/core/transmuxing-events.d.ts | 1 + d.ts/src/player/player-engine-worker-msg-def.d.ts | 2 +- d.ts/src/player/player-events.d.ts | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/d.ts/src/core/transmuxing-events.d.ts b/d.ts/src/core/transmuxing-events.d.ts index 82b2f678..52b9ed38 100644 --- a/d.ts/src/core/transmuxing-events.d.ts +++ b/d.ts/src/core/transmuxing-events.d.ts @@ -1,6 +1,7 @@ declare enum TransmuxingEvents { IO_ERROR = "io_error", DEMUX_ERROR = "demux_error", + INIT_DEMUXER = "init_demuxer", INIT_SEGMENT = "init_segment", MEDIA_SEGMENT = "media_segment", LOADING_COMPLETE = "loading_complete", diff --git a/d.ts/src/player/player-engine-worker-msg-def.d.ts b/d.ts/src/player/player-engine-worker-msg-def.d.ts index 4a1bc58c..521b67c3 100644 --- a/d.ts/src/player/player-engine-worker-msg-def.d.ts +++ b/d.ts/src/player/player-engine-worker-msg-def.d.ts @@ -26,7 +26,7 @@ export type WorkerMessagePacketPlayerEventError = WorkerMessagePacketPlayerEvent }; export type WorkerMessagePacketPlayerEventExtraData = WorkerMessagePacketPlayerEvent & { msg: 'player_event'; - event: PlayerEvents.METADATA_ARRIVED | PlayerEvents.SCRIPTDATA_ARRIVED | PlayerEvents.TIMED_ID3_METADATA_ARRIVED | PlayerEvents.SYNCHRONOUS_KLV_METADATA_ARRIVED | PlayerEvents.ASYNCHRONOUS_KLV_METADATA_ARRIVED | PlayerEvents.SMPTE2038_METADATA_ARRIVED | PlayerEvents.SCTE35_METADATA_ARRIVED | PlayerEvents.PES_PRIVATE_DATA_DESCRIPTOR | PlayerEvents.PES_PRIVATE_DATA_ARRIVED; + event: PlayerEvents.METADATA_ARRIVED | PlayerEvents.SCRIPTDATA_ARRIVED | PlayerEvents.TIMED_ID3_METADATA_ARRIVED | PlayerEvents.SYNCHRONOUS_KLV_METADATA_ARRIVED | PlayerEvents.ASYNCHRONOUS_KLV_METADATA_ARRIVED | PlayerEvents.SMPTE2038_METADATA_ARRIVED | PlayerEvents.SCTE35_METADATA_ARRIVED | PlayerEvents.PES_PRIVATE_DATA_DESCRIPTOR | PlayerEvents.PES_PRIVATE_DATA_ARRIVED | PlayerEvents.INIT_DEMUXER; extraData: any; }; export type WorkerMessagePacketTransmuxingEvent = WorkerMessagePacket & { diff --git a/d.ts/src/player/player-events.d.ts b/d.ts/src/player/player-events.d.ts index 704ec83f..3ab55c83 100644 --- a/d.ts/src/player/player-events.d.ts +++ b/d.ts/src/player/player-events.d.ts @@ -13,6 +13,7 @@ declare enum PlayerEvents { PES_PRIVATE_DATA_DESCRIPTOR = "pes_private_data_descriptor", PES_PRIVATE_DATA_ARRIVED = "pes_private_data_arrived", STATISTICS_INFO = "statistics_info", - DESTROYING = "destroying" + DESTROYING = "destroying", + INIT_DEMUXER = "init_demuxer" } export default PlayerEvents;