Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(recording): stop orphaned capture streams and fade in mic-only recordings

Etienne Lescotf698bba6d531
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 169dad47f..8eb97c23c 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -1267,13 +1267,24 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
})()
: Promise.resolve(null);
- const [screenMediaStream, micMediaStream] = await Promise.all([screenCapture, micCapture]);
-
- if (!isCountdownRunActive(countdownRunToken)) {
- teardownMedia();
- return;
+ // Await both in-flight captures. If the screen capture rejects it would
+ // otherwise orphan a mic stream that resolved in parallel (leaving the
+ // screen/mic indicator on), so stop that stream before rethrowing.
+ let screenMediaStream: MediaStream;
+ try {
+ screenMediaStream = await screenCapture;
+ } catch (error) {
+ void micCapture
+ .then((micStream) => micStream?.getTracks().forEach((track) => track.stop()))
+ .catch(() => {
+ // Mic capture itself failed too; nothing left to stop.
+ });
+ throw error;
}
+ const micMediaStream = await micCapture;
+ // Assign the refs before the cancellation check below so teardownMedia() can
+ // stop the freshly acquired streams if the countdown was cancelled mid-capture.
screenStream.current = screenMediaStream;
microphoneStream.current = micMediaStream;
diff --git a/src/lib/audioMix.test.ts b/src/lib/audioMix.test.ts
index d997fbe24..d9ce11e78 100644
--- a/src/lib/audioMix.test.ts
+++ b/src/lib/audioMix.test.ts
@@ -80,11 +80,33 @@ describe("mixAudioTracks", () => {
expect(result.track).toBe(system);
});
- it("returns the mic track verbatim and no context when only mic is present", () => {
+ it("fades a mic-only recording in from 0 to unity gain through its own AudioContext, with no system source", () => {
+ const fakeCtx = stubAudioContext(0.5);
+
const mic = track("mic");
const result = mixAudioTracks({ systemAudioTrack: null, micAudioTrack: mic });
- expect(result.context).toBeNull();
- expect(result.track).toBe(mic);
+
+ expect(result.context).toBe(fakeCtx);
+ expect(fakeCtx.createGain).toHaveBeenCalledTimes(1);
+ // Only the mic is routed through a source node; there is no system audio.
+ expect(fakeCtx.createMediaStreamSource).toHaveBeenCalledTimes(1);
+ expect(fakeCtx.createMediaStreamDestination).toHaveBeenCalledTimes(1);
+
+ const [gain] = fakeCtx.createGain.mock.results.map((r) => r.value) as FakeGainNode[];
+ // Ramps to unity (1), not MIC_GAIN_BOOST, so mic-only loudness is unchanged.
+ expect(gain.gain.setValueAtTime).toHaveBeenCalledWith(0, 0.5);
+ expect(gain.gain.linearRampToValueAtTime).toHaveBeenCalledWith(1, 0.5 + MIC_FADE_IN_S);
+
+ const [micSource] = fakeCtx.createMediaStreamSource.mock.results.map(
+ (r) => r.value,
+ ) as FakeMediaStreamAudioSourceNode[];
+ const [destination] = fakeCtx.createMediaStreamDestination.mock.results.map(
+ (r) => r.value,
+ ) as FakeMediaStreamDestination[];
+ expect(micSource.connect).toHaveBeenCalledWith(gain);
+ expect(gain.connect).toHaveBeenCalledWith(destination);
+
+ expect(result.track).toEqual({ kind: "audio", id: "dest-track" });
});
it("returns null track and no context when neither track is present", () => {
diff --git a/src/lib/audioMix.ts b/src/lib/audioMix.ts
index f614a67bc..1329c59cd 100644
--- a/src/lib/audioMix.ts
+++ b/src/lib/audioMix.ts
@@ -27,30 +27,36 @@ export type MixAudioTracksResult = {
/**
* Combine system audio and the microphone into a single audio track for the recorder.
*
- * - Both tracks present: builds an AudioContext + GainNode + MediaStreamDestination
- * that mixes them, applying a short fade-in on the mic gain so the very first packet
- * doesn't click. Caller owns the returned `context` and must `.close()` it on teardown.
- * - Exactly one track present: returns it verbatim, no AudioContext created.
+ * Whenever a mic track is present it is routed through an AudioContext + GainNode with
+ * a short fade-in so its first packet doesn't click — mic-only recordings (a common
+ * mode) get the same anti-click treatment as mixed ones, so `context` is non-null
+ * whenever a mic track is present. The mic is boosted above unity only when it has to
+ * sit over system audio; on its own it rides at unity so mic-only loudness is unchanged.
+ * Caller owns the returned `context` and must `.close()` it on teardown.
+ *
+ * - System audio only: returned verbatim, no AudioContext created (no warm-up click).
* - Neither present: returns `{ context: null, track: null }`.
*/
export function mixAudioTracks({
systemAudioTrack,
micAudioTrack,
}: MixAudioTracksInput): MixAudioTracksResult {
- if (systemAudioTrack && micAudioTrack) {
- const context = new AudioContext();
+ if (!micAudioTrack) {
+ return { context: null, track: systemAudioTrack ?? null };
+ }
+
+ const context = new AudioContext();
+ const destination = context.createMediaStreamDestination();
+ if (systemAudioTrack) {
const systemSource = context.createMediaStreamSource(new MediaStream([systemAudioTrack]));
- const micSource = context.createMediaStreamSource(new MediaStream([micAudioTrack]));
- const micGain = context.createGain();
- micGain.gain.setValueAtTime(0, context.currentTime);
- micGain.gain.linearRampToValueAtTime(MIC_GAIN_BOOST, context.currentTime + MIC_FADE_IN_S);
- const destination = context.createMediaStreamDestination();
systemSource.connect(destination);
- micSource.connect(micGain).connect(destination);
- return { context, track: destination.stream.getAudioTracks()[0] };
}
- return {
- context: null,
- track: systemAudioTrack ?? micAudioTrack ?? null,
- };
+ // Unity when the mic is on its own; boosted only when competing with system audio.
+ const micTargetGain = systemAudioTrack ? MIC_GAIN_BOOST : 1;
+ const micSource = context.createMediaStreamSource(new MediaStream([micAudioTrack]));
+ const micGain = context.createGain();
+ micGain.gain.setValueAtTime(0, context.currentTime);
+ micGain.gain.linearRampToValueAtTime(micTargetGain, context.currentTime + MIC_FADE_IN_S);
+ micSource.connect(micGain).connect(destination);
+ return { context, track: destination.stream.getAudioTracks()[0] };
}