2

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.)

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 1
    filing a chrome bug might be your best bet. Two torches might be unusual enough this has never been tested before. – Philipp Hancke Jul 03 '20 at 11:44
  • That's good advice, @PhilippHancke, thanks. I have done so. – O. Jones Jul 03 '20 at 13:52
  • 1
    any luck on this? i know its old, but i experience the same problem when adding a torch button in my react-js app. its ok to turn it on, but it stays on after taking a picture, and since the stream is then lost, i have no way of deactivating it. It does work when i use 'false' though, but really reaaaaaaly slowly, so it makes a pretty bad UX. – Rmaxx May 24 '22 at 12:10

0 Answers0