Skip to content

Commit

Permalink
nice
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger committed Nov 7, 2024
1 parent 2aa1494 commit 7b0b10e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/convert/app/components/ConvertUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default function ConvertUI({src}: {readonly src: Source}) {
// eslint-disable-next-line no-alert
alert((e as Error).stack);
console.error(e);
setState({type: 'idle'});
});

return () => {
Expand Down
20 changes: 18 additions & 2 deletions packages/webcodecs/src/audio-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ export const createAudioDecoder = ({
const audioDecoder = new AudioDecoder({
output(inputFrame) {
ioSynchronizer.onOutput(inputFrame.timestamp);
const abortHandler = () => {
inputFrame.close();
};

signal.addEventListener('abort', abortHandler, {once: true});
outputQueue = outputQueue
.then(() => onFrame(inputFrame))
.then(() => {
if (signal.aborted) {
return;
}

return onFrame(inputFrame);
})
.then(() => {
ioSynchronizer.onProcessed();
signal.removeEventListener('abort', abortHandler);
return Promise.resolve();
})
.catch((err) => {
inputFrame.close();
onError(err);
});
},
error(error) {
Expand Down Expand Up @@ -68,7 +84,7 @@ export const createAudioDecoder = ({
return;
}

while (ioSynchronizer.getUnemittedKeyframes() > 1) {
while (ioSynchronizer.getUnemittedKeyframes() > 100) {
await ioSynchronizer.waitForOutput();
}

Expand Down
11 changes: 10 additions & 1 deletion packages/webcodecs/src/audio-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ export const createAudioEncoder = ({
output: (chunk) => {
ioSynchronizer.onOutput(chunk.timestamp);
prom = prom
.then(() => onChunk(chunk))
.then(() => {
if (signal.aborted) {
return;
}

return onChunk(chunk);
})
.then(() => {
ioSynchronizer.onProcessed();
return Promise.resolve();
})
.catch((err) => {
onError(err);
});
},
error(error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/webcodecs/src/on-video-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export const makeVideoTrackHandler =
codec: track.codecWithoutConfig,
codecPrivate: track.codecPrivate,
});
return (sample) => {
state.addSample(
return async (sample) => {
await state.addSample(
new EncodedVideoChunk(sample),
videoTrack.trackNumber,
true,
Expand Down
15 changes: 15 additions & 0 deletions packages/webcodecs/src/video-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ export const createVideoDecoder = ({
output(inputFrame) {
ioSynchronizer.onOutput(inputFrame.timestamp);

const abortHandler = () => {
inputFrame.close();
};

signal.addEventListener('abort', abortHandler, {once: true});

outputQueue = outputQueue
.then(() => {
if (signal.aborted) {
return;
}

return onFrame(inputFrame);
})
.then(() => {
ioSynchronizer.onProcessed();
signal.removeEventListener('abort', abortHandler);
return Promise.resolve();
})
.catch((err) => {
inputFrame.close();
onError(err);
});
},
error(error) {
Expand Down
7 changes: 7 additions & 0 deletions packages/webcodecs/src/video-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ export const createVideoEncoder = ({

outputQueue = outputQueue
.then(() => {
if (signal.aborted) {
return;
}

return onChunk(chunk);
})
.then(() => {
ioSynchronizer.onProcessed();
return Promise.resolve();
})
.catch((err) => {
onError(err);
});
},
});
Expand Down

0 comments on commit 7b0b10e

Please sign in to comment.