I have a Javascript web app using .getUserMedia()
, .applyConstraints()
, and related browser APIs to capture some video from mobile device web browsers and send it someplace with a websocket. The capture and send stuff is working well.
I have implemented a little user interface to let my users turn on and off the torch (the LED lamp to provide light for the camera.)
On most Android devices, this UI works fine to turn on, and then off, the torch. But not on the the Android Studio Mega (Build/O11019 running Android 8.1.0 and mobile Chrome 81.0.4044.138)
Note: Studio Mega is an Android model name, and not related to Android Studio.
On that device I can turn the torch on just fine, but it ignores my request to turn the torch off. I use code like this:
const torchstate = false /* works when torchstate = true */
const track = stream.getVideoTracks()[0]
if( track && typeof track.getCapabilities === 'function' ) {
const caps = track.getCapabilities()
if( caps.torch ) {
track.applyConstraints( { advanced: [{ torch: torchstate }] } )
.then( () => {
/* the torch should now be off, but it is not. */
} )
.catch( ( error ) => {
/* My code never hits this catch clause */
console.log( 'setTorch', torchstate ? 'on' : 'off', 'error', error )
} )
}
}
This Studio Mega device actually has two torches, one on the back camera and another on the selfie-camera. My UI turns on the one I want it to turn on (the one associated with the currently active camera). But not off.
Closing the stream
turns the torch off as expected (on all Androids I've tried).
Is there some other way to turn off torches? Or is this possibly a browser, WebRTC, or device bug?
(For what it's worth iOS / mobile Safari doesn't have the torch capability. So maybe I should consider this a talking-donkey problem--in which I should be amazed it works at all, not annoyed that it works strangely.)