Bug 227933

Summary: applyConstraints recaptures camera and creates a lag
Product: WebKit Reporter: Manik <msach22>
Component: WebRTCAssignee: Nobody <webkit-unassigned>
Status: REOPENED ---    
Severity: Normal CC: eric.carlson, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: Safari 14   
Hardware: Mac (Intel)   
OS: macOS 11   

Description Manik 2021-07-13 17:41:51 PDT
When using the applyConstraints API on getUserMedia, it seems like Safari is recapturing from the camera. You will also see a lag in the video rendering each time applyConstraints is called.

I've set up a basic JSFiddle that helps repro this:
https://jsfiddle.net/3jztwxc6/

```

(async () => {

const stream = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
const videoElement = document.getElementById("gum");
videoElement.srcObject = stream;
const videoTrack = stream.getVideoTracks()[0];

const constraints = videoTrack.getConstraints();

constraints.frameRate = { max: 20, min: 20 };

setTimeout(() => {
videoTrack.applyConstraints(constraints);

}, 5000);

})();
```
Comment 1 Radar WebKit Bug Importer 2021-07-20 17:42:15 PDT
<rdar://problem/80868463>
Comment 2 youenn fablet 2021-08-02 02:13:16 PDT
Thanks for the report Manik, I think this is covered by https://bugs.webkit.org/show_bug.cgi?id=228634.
Comment 3 youenn fablet 2021-08-02 02:13:30 PDT

*** This bug has been marked as a duplicate of bug 228634 ***
Comment 4 Manik 2021-08-02 09:06:47 PDT
Thanks Youenn - appreciate the fix!
Comment 5 Manik 2021-08-03 10:23:52 PDT
@Youenn,

We reviewed the patch for the fix and it looks like it will only help if the new FPS constraints is the same as originally requested. I believe it does not fix our issue where we request an FPS os 30 (or default) and use applyConstraints to request 20, 15, etc thereafter.

I'm reopening the issue, please let me know what you think.
Comment 6 youenn fablet 2021-08-03 10:57:55 PDT
Well, we do change the camera setup to match the application frame rate.
The alternative would be to always go to high frame rate and do software frame rate decimation.

To prevent this optimization, you can use track clone to keep a track with high frame rate and another track with a lower frame rate. This should work fine with https://bugs.webkit.org/show_bug.cgi?id=228634 fix.

For instance: https://jsfiddle.net/6qLsm7jp/ (doTest1 should recalibrate, doTest2 should not).
Can you clarify whether you would like doTest1 to not recalibrate either?