I am implementing a flashlight toggle feature in a React web application using the WebRTC API. The code works correctly on most devices, but I have observed that on Samsung devices, the flashlight capability (torch
) is not detected, even though the device has a flashlight.
Here is the code snippet I’m using:
const handleFlashToggle = async (flashStatus: boolean) => {
if (isTogglingFlash || flashOn === flashStatus) return;
setIsTogglingFlash(true);
if (mediaStream) {
try {
const track: any = mediaStream.getVideoTracks()[0];
const capabilities: any = track.getCapabilities();
if (capabilities?.torch) {
await track.applyConstraints({
advanced: [{ torch: flashStatus }],
});
setFlashOn(flashStatus);
} else {
toast.error("Torch is not supported on this device.");
}
} catch (error) {
toast.error("Failed to toggle flash.");
} finally {
setIsTogglingFlash(false);
}
} else {
toast.error("MediaStream is not available.");
}
};
Steps to Reproduce:
- Use a Samsung device (tested on Samsung Galaxy models).
- Open the web application that includes the above code.
- Grant camera permissions and try to toggle the flashlight.
Expected Behavior:
The flashlight should toggle on and off if the device supports it.
Actual Behavior:
The getCapabilities()
method returns an object that does not include torch
as a capability, and the flashlight cannot be toggled.
- The same code works correctly on non-Samsung devices like (list examples, e.g., Google Pixel, iPhone).
Questions:
- Does Samsung restrict access to the
torch
capability via the WebRTC API? - Is there a workaround or a specific permission required to enable this?