One camera, one stream
Why the camera kept blinking on an AI video interview platform, fixed by giving the media stream exactly one owner.
A candidate clicks “Join” to start their video interview. The camera light blinks off and on. The preview turns into a black box. Sometimes the browser even asks for camera permission a second time, as if it had forgotten the first answer.
We saw this in testing on an AI-led video interview platform.
Below I explain why the camera kept blinking, how giving the stream exactly one owner fixed it, and what still isn’t right.
The two screens
The candidate flow has two screens. The first is a device check. It shows a live camera preview and lets the candidate pick a camera, a microphone and a speaker. A small meter moves when they speak. When they are ready, they click “Join”.
The second screen is the interview. The candidate talks with an AI interviewer in real time. The app also records the session.
A teammate on the frontend built the interview: the real-time voice connection, the recording and the uploads. They also wrote the first version of the device-check code. When the move between the two screens started to misbehave, I took the media part.
A quick word on camera streams
To use the camera in a browser, you call navigator.mediaDevices.getUserMedia(). The browser asks the user for permission and then gives you a MediaStream.
A MediaStream is a container for tracks. A track is one source of media, like one camera or one microphone. A typical stream has one video track and one audio track.
To show the stream on the page, you set it as the srcObject of a <video> element. To release the camera, you call stop() on each track. Until every track is stopped, the camera stays on.
Why the camera kept blinking
Reading the device-check code and the interview code side by side turned up several problems. Four of them explain what candidates saw.
Two screens, two streams
The device check called getUserMedia when it loaded. Then the interview called getUserMedia again, with the chosen devices and a higher resolution.
Asking for a second stream from the same camera is what makes the light blink. The browser has to set up the camera again for the new settings. Depending on the browser and the kind of permission the user gave, it can also ask for permission again.
The preview was never stopped
The plan was to stop the preview stream before the interview started. The setup screen passed its stop function up to its parent, which saved it. Nothing ever called it.
There was a backup plan. When the setup screen was removed from the page, a cleanup function was supposed to stop the stream. But that cleanup function was created on the first render, and on the first render the stream was still null. A JavaScript function remembers the values that existed when it was created. So the cleanup always saw null and stopped nothing.
This is called a stale closure. Because of it, the preview’s tracks kept holding the camera, even after the interview opened its own stream. Sometimes the camera light stayed on after the interview ended.
Changing one device restarted everything
When a candidate picked a different camera, the code stopped every track, including the microphone, and asked for a completely new stream.
So changing the camera made the mic meter drop to zero and start again. Changing the microphone made the camera flicker.
The black box
The <video> element only appeared after loading finished, and the stream often arrived first. When the code tried to attach the stream, the video element did not exist yet, so it skipped that step.
In React, changing a ref does not cause a new render. So when the video element finally appeared, nothing tried to attach the stream again. The candidate saw a black box until something else caused a render.
What I tried first
My first fix stayed inside the existing device-check code and focused on devices that come and go.
If a candidate unplugs a USB headset, or their AirPods disconnect, the saved device ID points to a device that no longer exists. So I added a fallback: keep the user’s choice if that device still exists, otherwise use the device labelled “Default”, and otherwise use the first device in the list. Chrome shows a “Default” device, but Safari and Firefox do not, so the last step is needed.
Then I listened for the browser’s devicechange event. The browser fires this event for any change, even when a new device is plugged in. Restarting the stream on every event would cause the same flicker I wanted to remove. So the code only restarts when the device in use has actually disappeared.
This fixed the unplugging problem and part of the black box, but the real issue remained: two parts of the app each thought they owned the camera.
What actually worked
The second fix, later the same day, changed who owns the stream.
One stream for the interview route
The fix is a small React context that holds a single MediaStream. A context lets components share a value without passing it down through every level as props. This one has a function to save the stream and a function to stop all of its tracks.
The context also keeps the stream in a ref, so the stop function always sees the current stream instead of an old null. That removes the stale closure.
I placed the context only around the interview route, so the rest of the app cannot reach the stream.
The device check writes, the interview reads
Now the device check saves its stream in the context. When the device check screen goes away, it stops only the mic meter and leaves the stream alone, because the stream needs to live on into the interview.
When the interview starts, it takes the stream from the context and uses it for the webcam preview and the recording. It also sends the microphone track from the same stream to the AI voice session.
If the context has no stream, for example because someone opened the interview link directly, the interview falls back to a new getUserMedia call. Screen sharing still needs its own request, because the browser requires that.
Replace one track, keep the stream
To change a device, the code now replaces one track inside the same stream. In the simplified version below, the old track is stopped and removed, and the new track is added to the same MediaStream object:
async function swapTrack(stream: MediaStream, kind: "audio" | "video", deviceId: string) {
const fresh = await navigator.mediaDevices.getUserMedia({
[kind]: { deviceId: { exact: deviceId } },
});
const next = fresh.getTracks()[0];
const prev = stream.getTracks().find((t) => t.kind === kind);
if (prev) {
prev.stop(); // release the old device
stream.removeTrack(prev);
}
stream.addTrack(next); // same stream, new source
}
Now a camera change does not touch the microphone, so the meter keeps moving. And because the interview reads the mic track from this same stream, if the candidate changes the microphone during setup, the AI hears the new one without any extra code.
Make the video element’s arrival a render
For the black box, I replaced the plain ref on the video element with a callback ref that saves the element in React state. A callback ref is a function that React calls with the DOM element when the element appears. Because this function updates state, the element’s arrival causes a render.
One effect depends on both the stream and the video element, so whichever of the two arrives second triggers the attach. That got rid of the black box.
Small finishing touches
The interview screen now shows the webcam as soon as the interview starts loading, not only when the session is live. The stream already exists, so the candidate’s face never leaves the screen.
My teammate’s code already ended an interview carefully: stop the recorders, wait for the uploads, then tell the backend. One last step now stops the shared stream, so the camera and microphone are released. The old stop function that was saved but never called is gone.
The result
Before, the app asked for the camera at least twice between setup and interview, and often three times. Now it asks once, plus once for screen sharing. The blink and the second permission prompt at “Join” are gone. Changing the camera no longer affects the microphone.
What is still not perfect
The context stops the stream when the interview ends, but it does not clean up when it is removed from the page. If a candidate leaves the route in the middle of setup, the camera is released only when they close the tab. A cleanup step inside the context would fix that.
In development, React’s Strict Mode runs each effect twice to help find bugs. That can still start two camera requests at once. Saving the pending request in a ref and handing it to the second caller would fix it.
All of this was tested by hand with real devices, and there is no automated test yet. Chromium has fake media devices that work with Playwright, a tool that controls a browser from code, and I would like to add tests with them so this does not break again.
What I learned
Treat a camera stream as a resource with one owner. If two components can call getUserMedia, sooner or later both will, and neither will stop the other’s tracks.
Replace tracks instead of whole streams. When you change a track inside one MediaStream, everything that uses the stream keeps working: the video element, the mic meter and the voice connection.
If a DOM element can appear late, make its arrival a render. A callback ref that saves the element in state turns a timing problem into a normal dependency.
Check cleanup functions for stale values. A cleanup that reads state sees the value from the render where it was created. Read from a ref instead, or keep the resource somewhere that lives longer than the component.
Product names and some details are changed or left out. The engineering is real.