Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor background filters #1415

Merged
merged 11 commits into from
Jul 2, 2024
Merged

fix: refactor background filters #1415

merged 11 commits into from
Jul 2, 2024

Conversation

myandrienko
Copy link
Contributor

No description provided.

}, [canvasRef]);
const start = useCallback(
(ms: MediaStream) => {
return new Promise<[MediaStream, () => void]>((resolve, reject) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the whole point of this PR: to rewrite background filter pipeline as a single linear function, instead of an effects-based logic.

Comment on lines 271 to 278
resolve([
outputStream,
() => {
renderer.dispose();
videoEl.srcObject = null;
disposeOfMediaStream(outputStream);
},
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new filter API, filter returns a stream and a cleanup function. The cleanup function will be called when the filter is unregistered or when the source stream is muted.

Comment on lines 160 to 177
const ready = withoutConcurrency(
this.filterRegistrationConcurrencyTag,
async () => {
this.filters.push(entry);
await this.applySettingsToStream();
},
);

return {
ready,
unregister: () =>
withoutConcurrency(this.filterRegistrationConcurrencyTag, async () => {
const cleanup = entry[1];
cleanup?.();
this.filters = this.filters.filter((f) => f !== entry);
await this.applySettingsToStream();
}),
};
Copy link
Contributor Author

@myandrienko myandrienko Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We move the logic preventing unregistering before finishing registering into the client. Note how both registering and unregistering logic is wrapped in withoutConcurrency.

It's convenient to return the unregister function immediately, so the method is no longer async. The ready promise is there if you want to await for registration.

This can be further optimized. Most of the times we unregister one filter to immediately register another. We can introduce a method called replaceFilter that will only call applySettingsToStream once. That way we won't get flickering when switching blur levels or background images.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the API with filters returning [stream, cleanup] tuple, leaving it in this draft, but will probably change before merging.

Comment on lines 142 to 145
const { ready: noiseCancellationRegistration, unregister } =
this.registerFilter(noiseCancellation.toFilter());
this.noiseCancellationRegistration = noiseCancellationRegistration.then(
() => unregister,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change things around the noise cancellation filter a bit, but I haven't looked to deep into it. Probably the code can be simplified in this file.

@myandrienko myandrienko marked this pull request as ready for review June 24, 2024 13:33
@myandrienko myandrienko changed the base branch from fix/bg-filter-cleanup to main June 28, 2024 09:44
@myandrienko myandrienko merged commit deb6da2 into main Jul 2, 2024
21 checks passed
@myandrienko myandrienko deleted the fix/bg-filter-refactor branch July 2, 2024 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants