📎 Webclip
Fix Bad Lighting with JavaScript Webcam Exposure Controls
The article discusses how webcam image quality in WebRTC can be improved by adjusting exposure before the stream reaches the browser. It explains photographic exposure and the exposure triangle, then focuses on shutter speed as the only practically controllable parameter for webcams. The author shows how auto-exposure fails in uneven lighting scenarios like backlit windows or bright spotlights and compares this with limited post-processing. He then walks through controlling exposure via JavaScript MediaStreamTrack constraints where supported, and describes a fallback using USB Video Class (UVC) for native and Electron apps, plus notes on platform and browser support and a possible future AI-based approach.
Reading notes#
- The author has been building WebRTC apps for nearly a decade and used to accept webcam image quality as a fixed hardware limitation until a project required changing imaging parameters.
- He discovered it is possible to improve image quality by adjusting camera settings like exposure before the video stream enters the browser.
- Exposure is defined as the amount of light that reaches the camera sensor over time, where no light produces black images and too much light causes overexposed white areas with lost information.
- In photography, exposure is controlled by the exposure triangle: shutter speed, aperture, and ISO, but not all three matter equally for video communication.
- Shutter speed (exposure time) is the duration the shutter is open, with longer times increasing motion blur; for WebRTC it is the most important and usually the only practically controllable setting.
- Aperture controls how wide the lens opens and can influence background blur and focus, but it is not exposed in the Media Streams API and is often replaced by virtual background techniques for privacy.
- ISO represents sensor light sensitivity and higher ISO can introduce visible digital noise; Chrome advertises ISO support but the author has not seen webcams or apps that let users set ISO or even document ISO ranges.
- Each webcam uses its own auto-exposure algorithm and metering mode, which typically works under balanced lighting but struggles when parts of the scene are much brighter than others.
- In uneven lighting, the camera must pick which area to prioritize, leading to underexposed or overexposed zones, and webcams cannot match the dynamic range of the human eye.
- In typical video conferences, the subject is usually a static speaker in front of a monitor, which makes manual exposure adjustment manageable for users.
- Real-world problem cases include backlit situations where a participant sits in front of a bright window or lamp in locations like hotel rooms or fixed office setups.
- Another use case is spotlight scenarios such as stage performances, live conferences, or operating rooms where strong light focuses on a subject and manual exposure changes help avoid overexposure.
- Post-processing can shift color space on poorly exposed images, as in Google Meet’s “Adjust video for low light” feature, but cannot reliably restore overexposed areas because contours and details are often lost.
- The author recommends adjusting exposure when possible instead of relying on post-processing, especially to prevent information loss in bright regions.
- WebRTC apps can give users more control by applying advanced constraints to
MediaStreamTrackvia theapplyConstraintsAPI defined in the Media Streams spec. - When the author tried exposure constraints in March 2022, they did not behave as expected in Chrome, leading him to file a bug and temporarily switch to a UVC-based solution.
- Chrome version 101 fixed the initial issue with exposure constraints, allowing a return to the JavaScript-based method except on macOS, where the UVC workaround stays relevant.
- A live demo of exposure control is available in the official WebRTC GitHub samples with UI elements like buttons and sliders, though browser and OS compatibility may vary.
- The basic JavaScript flow is to call
navigator.mediaDevices.getUserMedia({video: true}), obtain the video track from the returnedMediaStream, and then inspect capabilities usingtrack.getCapabilities(). - Capabilities include minimum and maximum values for
exposureTime, which show what the connected device actually supports. - To use manual exposure, the track is first configured via
track.applyConstraints({ advanced: [{ exposureMode: 'manual' }] })to switch off auto-exposure. - After the mode change completes, the app can try setting a custom
exposureTimewith a separateapplyConstraintscall using an advanced constraint object. - The author stresses that real-world apps must respect
getCapabilitiesoutput, since otherwise they may request exposure values that the device cannot handle. - He notes that
exposureModeandexposureTimemust be applied in two distinct calls so manual mode activation finishes before time settings, or else browsers may ignore the requested exposure time. - Sample code chains promises to first log supported capabilities, then set manual mode, then set the target exposure time, logging the final settings and errors as appropriate.
- Exposure control support through
MediaStreamTrackdepends on operating system, browser, camera hardware, and the installed driver. - As of May 2022, manual exposure with constraints works on Chrome for Windows, Linux, and Android.
- Developers can call
navigator.mediaDevices.getSupportedConstraints()to see which constraint keys, such asexposureMode,exposureTime, andexposureCompensation, are recognized by the browser. - If desired constraints do not appear in
getSupportedConstraints, the browser has not implemented them, even before checking what the camera itself supports. - Even when constraints are listed,
track.getCapabilities()must still be used to see the actual ranges and values supported by the specific device. - Chrome 101 and later has relatively good exposure control support but relies on different platform-specific APIs such as IAMCameraControl on Windows, V4L2 on Linux, and IOUSBHost on macOS.
- Because Chrome’s media capability implementation uses these underlying OS APIs, supported exposure features vary across platforms.
- Safari currently does not list any exposure-related capabilities in
navigator.mediaDevices.getSupportedConstraints(). - Firefox does not yet implement
MediaStreamTrack.getCapabilities, and Mozilla’s documentation tracks this incomplete status. - As a fallback for platforms where the JavaScript approach fails, notably macOS, the author proposes using the USB Video Class (UVC) standard.
- UVC is an over-20-year-old, stable standard for USB video devices, but browsers cannot directly access the UVC API.
- The UVC method only works if the developer controls a native executable on the user’s machine, such as an Electron-based app.
- The author assumes Electron for his example and suggests searching npm for UVC libraries, highlighting
uvccwhich provides a simple command-line interface. - Using
uvcc, developers can install the tool globally, switch webcams to manual exposure mode, query the supported exposure time range, and set the desired exposure time. - Changes applied with
uvccaffect the underlying webcam settings and thus also influence WebRTC media tracks in any browser using that camera. - Many UVC helper libraries rely on
libusb, which introduces extra considerations, especially on Windows. - On Windows,
libusbrequires a WinUSB driver, and installing this driver for a webcam converts it into a generic USB device, removing its standard video capabilities. - For that reason, the author considers the UVC approach workable for macOS and Linux but problematic on Windows due to driver implications.
- On Android, Chrome already exposes some media capture capabilities as seen in the JavaScript approach, but these are implemented in Chromium’s media stack rather than in the WebRTC library.
- For native mobile applications, the recommended strategy is to use the platform’s native camera API (such as Android’s CameraX) to configure camera output.
- This native approach requires writing custom camera configuration code but opens the door to advanced features like Android’s face detection to inform exposure decisions, though the author has not tried that yet.
- The author suggests a future direction where machine learning enhances webcam exposure by identifying points of interest, such as faces, and computing whether they are over or underexposed.
- An ML or mathematical exposure metering algorithm could focus on these regions and adjust exposure automatically, aiming to improve results on low-budget webcams or under difficult lighting.
- Detailed exploration of AI-based exposure control is acknowledged as beyond the scope of the current article.
- The author invites readers to share bugs they find or novel techniques for enabling exposure control consistently across browsers and platforms in the comments.
