Skip to content

Commit

Permalink
Merge pull request #18 from slateteams/fix/cancel-stream
Browse files Browse the repository at this point in the history
fix: close and open another readAvPacket stream
  • Loading branch information
ForeverSc authored Oct 30, 2024
2 parents 2acb574 + e7e5262 commit 9fd30c0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/web-demuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export class WebDemuxer {
const queueingStrategy = new CountQueuingStrategy({ highWaterMark: 1 });
const msgId = this.msgId;
let pullCounter = 0;
let msgListener: (e: MessageEvent) => void;
let cancelResolver: () => void;

return new ReadableStream(
{
Expand All @@ -218,7 +220,7 @@ export class WebDemuxer {
controller.error("source is not loaded. call load() first");
return;
}
const msgListener = (e: MessageEvent) => {
msgListener = (e: MessageEvent) => {
const data = e.data;

if (
Expand All @@ -237,10 +239,16 @@ export class WebDemuxer {
data.type === FFMpegWorkerMessageType.AVPacketStream &&
data.msgId === msgId
) {
if (data.result) {
if (data.result && !cancelResolver) {
controller.enqueue(data.result);
} else {
controller.close();
this.ffmpegWorker.removeEventListener("message", msgListener);
// only close if the stream has not been cancelled from outside
if (cancelResolver) {
cancelResolver();
} else {
controller.close();
}
}
}
};
Expand All @@ -267,7 +275,10 @@ export class WebDemuxer {
pullCounter++;
},
cancel: () => {
this.post(FFMpegWorkerMessageType.StopReadAVPacket, undefined, msgId);
return new Promise((resolve) => {
cancelResolver = resolve;
this.post(FFMpegWorkerMessageType.StopReadAVPacket, undefined, msgId);
});
},
},
queueingStrategy,
Expand Down

0 comments on commit 9fd30c0

Please sign in to comment.