-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix webcam recording freeze and empty camera/mic device list #971
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
Open
finartcom
wants to merge
3
commits into
webadderallorg:main
Choose a base branch
from
finartcom:fix/webcam-freeze-and-device-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| const WEBCAM_WIDTH_IDEAL = 1280; | ||
| const WEBCAM_HEIGHT_IDEAL = 720; | ||
|
|
||
| interface WebcamAcquisition { | ||
| promise: Promise<MediaStream>; | ||
| deviceId: string | undefined; | ||
| refCount: number; | ||
| settled: boolean; | ||
| pendingStop: boolean; | ||
| } | ||
|
|
||
| const acquisitions = new Set<WebcamAcquisition>(); | ||
|
|
||
| function buildVideoConstraints(deviceId: string | undefined): MediaTrackConstraints { | ||
| return deviceId | ||
| ? { | ||
| deviceId: { exact: deviceId }, | ||
| width: { ideal: WEBCAM_WIDTH_IDEAL }, | ||
| height: { ideal: WEBCAM_HEIGHT_IDEAL }, | ||
| } | ||
| : { | ||
| width: { ideal: WEBCAM_WIDTH_IDEAL }, | ||
| height: { ideal: WEBCAM_HEIGHT_IDEAL }, | ||
| }; | ||
| } | ||
|
|
||
| /** An unspecified request may reuse any open camera; a specific device may only reuse a match. */ | ||
| function findCompatibleAcquisition(deviceId: string | undefined): WebcamAcquisition | undefined { | ||
| for (const acquisition of acquisitions) { | ||
| if (!deviceId || acquisition.deviceId === deviceId) { | ||
| return acquisition; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Many UVC webcams only support a single open handle at the OS/driver level, | ||
| * so two concurrent getUserMedia() calls for the same physical camera can | ||
| * freeze one another or silently fail to deliver frames. This coordinator | ||
| * dedupes concurrent webcam acquisitions across every consumer (the device | ||
| * picker's label-unlock probe, the HUD's live preview, and the recorder) so | ||
| * only one open request per distinct device is ever in flight, and a track is | ||
| * only stopped once every consumer holding it has released their reference | ||
| * *and* its getUserMedia() call has actually settled — a release that lands | ||
| * while the call is still pending just marks it for a deferred stop, so a new | ||
| * compatible acquire() in the meantime can cancel that and reuse the same | ||
| * in-flight request instead of starting a competing one. | ||
| */ | ||
| export function acquireSharedWebcamStream(deviceId?: string): Promise<MediaStream> { | ||
| const existing = findCompatibleAcquisition(deviceId); | ||
| if (existing) { | ||
| existing.refCount += 1; | ||
| existing.pendingStop = false; | ||
| return existing.promise; | ||
| } | ||
|
|
||
| const acquisition: WebcamAcquisition = { | ||
| deviceId, | ||
| refCount: 1, | ||
| settled: false, | ||
| pendingStop: false, | ||
| promise: null as unknown as Promise<MediaStream>, | ||
| }; | ||
| acquisition.promise = navigator.mediaDevices | ||
| .getUserMedia({ video: buildVideoConstraints(deviceId), audio: false }) | ||
| .then((stream) => { | ||
| acquisition.settled = true; | ||
| if (acquisition.pendingStop) { | ||
| acquisitions.delete(acquisition); | ||
| stream.getTracks().forEach((track) => track.stop()); | ||
| } | ||
| return stream; | ||
| }) | ||
| .catch((error: unknown) => { | ||
| acquisition.settled = true; | ||
| acquisitions.delete(acquisition); | ||
| throw error; | ||
| }); | ||
| acquisitions.add(acquisition); | ||
| return acquisition.promise; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** Releases one reference obtained from {@link acquireSharedWebcamStream}. */ | ||
| export function releaseSharedWebcamStream(acquisitionPromise: Promise<MediaStream>): void { | ||
| let target: WebcamAcquisition | undefined; | ||
| for (const acquisition of acquisitions) { | ||
| if (acquisition.promise === acquisitionPromise) { | ||
| target = acquisition; | ||
| break; | ||
| } | ||
| } | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| target.refCount -= 1; | ||
| if (target.refCount > 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (!target.settled) { | ||
| target.pendingStop = true; | ||
| return; | ||
| } | ||
|
|
||
| acquisitions.delete(target); | ||
| void target.promise | ||
| .then((stream) => { | ||
| stream.getTracks().forEach((track) => track.stop()); | ||
| }) | ||
| .catch(() => { | ||
| // Acquisition failed; nothing to stop. | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.