fix(server): initialize COM for Windows camera discovery - #99
Open
amrltqt wants to merge 1 commit into
Open
Conversation
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.
Context
I use LeLab to test and iterate on my robot configurations. While setting it up on Windows, I ran into this issue when trying to use the camera configuration flow in the web interface.
The problem was reproducible with two USB webcams and occurred during camera discovery, before recording started. After initializing COM in the worker thread, the cameras were detected normally and their DirectShow names were returned correctly.
I hope this makes the Windows setup experience a little smoother for other LeLab users as well.
Background
On Windows, LeLab鈥檚
/available-camerasendpoint runs_windows_cameras()as a synchronous FastAPI route. Starlette executes synchronous routes in a worker thread.The Windows implementation uses
pygrabberto enumerate DirectShow devices and return their real friendly names. These names are required by the frontend to match the server-side camera index with the browser鈥檚MediaDeviceInfo.deviceId.Root cause
COM initialization is apartment/thread-local on Windows. Initializing COM in the main Python thread does not initialize it in a FastAPI worker thread.
pygrabbercalls DirectShow from that worker thread without first initializing COM, which raises:WinError -2147221008: CoInitialize has not been calledLeLab then falls back to probing camera indices with OpenCV using the DirectShow backend (
CAP_DSHOW). That fallback runs in the same uninitialized thread, so OpenCV also fails with warnings such as:VIDEOIO(DSHOW): backend is generally available but can't be used to capture by indexThis caused LeLab to lose the real camera names and prevented reliable camera discovery from the web UI.
Fix
Initialize COM for the current worker thread before calling
pygrabberor probing DirectShow cameras, and always callCoUninitialize()when the operation finishes.The initialization is kept in a small context manager so that:
finally;comtypesis not installed or COM initialization fails.