Fix/webcam fps clamp to camera rate - #31
Merged
Merged
Conversation
The requested fps went straight into the H264 encoder, the sample timestamps and the keyframe interval, even when the camera negotiated a lower rate. Asking for 60fps on a 30fps camera produced PTS advancing 16.6ms per frame while frames arrived every ~50ms, so the remote video ran fast and stuttered, and the IDR interval stretched from 2s to 6s. Clamp the stream fps to the rate the opened camera reports, and pace the capture loop against a deadline instead of sleeping a full interval on top of the already blocking frame grab.
The media type sent in MediaTypeListResponse carries the configured fps straight from WEBCAM_FPS, so a 60fps setting told the server it had a 60fps camera and the server duly asked for 60. Clamping the stream internally was not enough: the remote consumer still expected twice the samples the camera can produce. Record the highest frame rate the camera offers at the detected resolution during the proactive probe that already enumerates formats, and clamp the advertised fps to it. effective_fps moves next to it so both the advertised rate and the capture loop share one definition.
The target was computed as width * height * fps * 2 / 10, which both scales a per-second rate by the frame rate and, since the encoder runs with rate control off, never reaches OpenH264 at all: at 1080p60 it asked for ~20Mbps while the measured stream stayed around 2Mbps at every setting. Replace it with a nominal target per resolution weighted by quality, so the number in the config means something if rate control is ever turned on. Fixed QP behaviour is unchanged.
A single failed capture left the loop feeding generated frames for the rest of the session while logging the same error ten times a second: one log had 381 of them. The camera instance was also kept in its broken state, and Windows only releases the hardware once it is dropped, so recovery was impossible even when whatever held the device let go. Drop the camera on the first failure and reacquire it every two seconds for as long as the stream lasts. The error is logged once on the transition, the retries at debug level, and a successful reacquisition logs a single line. Mock frames keep flowing at the negotiated rate meanwhile, so the server never sees the channel stall.
Camera::frame_rate reports a placeholder rate of 1 under Media Foundation once a format has been applied, so clamping against it pinned every stream to 1fps: a 30fps camera logged "camera delivers 1fps" and the encoder was initialised at 1fps. set_camera_requset already returns the format the camera accepted and its value was being discarded. Return its frame rate from init_real_camera and carry it next to the camera instance, so the rate and the instance cannot drift apart.
Both StartStream and the SetFormat restart path opened a new camera while the previous instance was still alive: Rust evaluates the right-hand side of the assignment first, so init_real_camera ran before the old value was dropped. Media Foundation hands out most webcams exclusively, so the second open can come back as E_ACCESSDENIED or MF_E_HW_MFT_FAILED_START_STREAMING and the stream falls back to mock frames. Stopping the stream, as SetFormat did, does not help: the device stays claimed until the instance is dropped. Take the camera out of its slot first, so the drop happens before the new open is attempted. The mock branch no longer needs to clear the slot by hand.
The loop encoded a frame on every iteration but handed it over only when the server had an outstanding sample request. At 30fps against a server pulling around 15 samples a second, half of the encoded frames were dropped after the H264 encoder had already advanced its reference. The decoder then predicted from frames it never received, so the picture smeared and broke into blocks on any movement and only recovered at the next IDR, two seconds later. A still scene hid the problem because the missing references barely differed from the ones that arrived. Encode only when a sample is pending, so the reference chain matches what was transmitted. The camera is still drained every iteration to keep the latest frame fresh, and half the encoding work disappears.
aschumann-virtualcable
deleted the
fix/webcam-fps-clamp-to-camera-rate
branch
September 10, 2026 13:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix FPS for webcam on client