简体   繁体   中英

WebRTC switch back to front camera with gUM not streaming on Android/Chrome

On Samsung Galaxy S2 Android 6.0.1 + Chrome v55 , when I getUserMedia on page load, the video track acquired appears live .

在此输入图像描述

When I select the back camera from my camera select, I trigger another time my gUM with constraints to use that exact facing back cameraId , the video track is ended , I have a black rectangle instead of a stream.

在此输入图像描述

var constraints = {
    video: {
        deviceId: {
            exact: defaultVideoDeviceId
        }
    },
    audio: true
};

gUM wrapper

function gUM(constraints, callback) {
    console.debug("WebRTC constraints", constraints);

    // Stopping streaming before starting the new one
    if (window.streams.local) {
        window.streams.local.getTracks().forEach(function(track) {
          track.stop();
        });
    }

    navigator.mediaDevices.getUserMedia(constraints)
        .then(stream => {
            console.debug("New MediaStream > Tracks", stream.getTracks());
            window.streams.local = stream;
            callback && callback(stream);
        })
        .catch(err => {
            console.log("Raised error when capturing:", err);
        });
}

If I switch back to front, it acquires a new MediaStream and it plays the video.

I'm facing a similar problem too.

My test is a bit different since I'm trying to make a GUM of the back camera while I have a GUM a media stream active that is using the front camera.

I tested in several android devices and only the Xiaomi MI Mix 2 with the MIUI beta 875 with android 8.1 works. This can be because that rom uses the new camera2 android api, or because the Mi Mix 2 camera hardware allows the usage of both the back and the front camera at the same time.

The annoying thing is that sometimes, on certain devices, the GUM doesn't fail, but hangs indefinitely.

Maybe listening to the MediaStreamTrack.onended event after the track.stop() method call, can help to understand when resources are completely free so as you can try a new GUM with different constraints.

Please let me know if you discovered something.

Simone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM