Skip to content

Commit

Permalink
Merge pull request #4781 from remotion-dev/event-loop-break
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger authored Jan 22, 2025
2 parents 17b8209 + cb1b693 commit 030b467
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/convert/app/lib/use-thumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const useThumbnailAndWaveform = ({

const execute = useCallback(() => {
const abortController = new AbortController();

const hasEnoughData = () => {
onDone();
};

parseMedia({
signal: abortController.signal,
reader: src.type === 'file' ? webFileReader : fetchReader,
Expand Down Expand Up @@ -114,7 +119,7 @@ export const useThumbnailAndWaveform = ({
if (frames >= framesToGet) {
abortController.abort();
frame.close();
onDone();
hasEnoughData();
return;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/media-parser/src/parse-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ const internalParseMedia: InternalParseMedia = async function <
}
}

await state.eventLoop.eventLoopBreakIfNeeded();

const hasBigBuffer = iterator.bytesRemaining() > 100_000;

if (iterationWithThisOffset > 0 || !hasBigBuffer) {
Expand Down
18 changes: 18 additions & 0 deletions packages/media-parser/src/state/last-eventloop-break.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type {LogLevel} from '../log';
import {Log} from '../log';

export const eventLoopState = (logLevel: LogLevel) => {
let lastEventLoopBreak = Date.now();

const eventLoopBreakIfNeeded = async () => {
if (Date.now() - lastEventLoopBreak > 2_000) {
await new Promise<void>((resolve) => {
setTimeout(() => resolve(), 50);
});
Log.verbose(logLevel, '10ms event loop break');
lastEventLoopBreak = Date.now();
}
};

return {eventLoopBreakIfNeeded};
};
2 changes: 2 additions & 0 deletions packages/media-parser/src/state/parser-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {flacState} from './flac-state';
import {imagesState} from './images';
import {isoBaseMediaState} from './iso-base-media/iso-state';
import {keyframesState} from './keyframes';
import {eventLoopState} from './last-eventloop-break';
import {makeMp3State} from './mp3';
import {riffSpecificState} from './riff';
import {sampleCallback} from './sample-callbacks';
Expand Down Expand Up @@ -101,6 +102,7 @@ export const makeParserState = ({
videoSection: videoSectionState(),
logLevel,
iterator,
eventLoop: eventLoopState(logLevel),
};
};

Expand Down

0 comments on commit 030b467

Please sign in to comment.