getUserMedia - how to detect if the device actually has a camera
Asked Answered
C

6

16

I'm playing with the html5/javascript getUserMedia api to write a js app that will use the device's camera if available. I'm using Modernizr to detect the capability (of the browser) like this:

if (Modernizr.getusermedia) {

And within the true block:

navigator.getUserMedia(
    {   // we would like to use video but not audio
        // This object is browser API specific! - some implementations require boolean properties, others require strings!
        video: true, 
        audio: false
    },
    function(videoStream) {
        // 'success' callback - user has given permission to use the camera
        // my code to use the camera here ... 
    },
    function() {
        // 'no permission' call back
        console.log("user did not give access to the camera");
    }               
);

This works fine. But what I've found is that the Modernizer.getUserMedia call returns true based on the browser supporting the api, and not whether the device actually has a camera or not.

IE. on my MacBook with its iSight camera and a current version of Chrome, Modernizr.getUserMedia returns true, then navigator.getUserMedia(...) prompts for permission to use the camera. Excellent

However, on another machine without a camera but with a current version of Chrome, Modernizr.getUserMedia returns true, which means that navigator.getUserMedia(...) prompts for permission to use the camera which the device doesn't have. Not so excellent!

Does anyone know if its possible to detect the existance of a camera? Ideally I don't want to prompt the user for permission to access the camera if they dont have one!

Cheers

Nathan

Charitacharitable answered 14/10, 2012 at 19:48 Comment(2)
Which callback gets called (with which arguments) when you allow it on the device without a camera?Banky
The success callback is called, but I've not checked whether the arg (videoStream in my code above) is null or not. That's a good point, I should check that. Will do so shortly and let you know ...Charitacharitable
B
5

You can use MediaStreamTrack.getSources. This returns a list of video and audio devices connected to the PC. This does not require user permission.

You can then pass the ID to getUserMedia to get the desired media device.

Bruch answered 3/2, 2014 at 12:5 Comment(1)
MediaStreamTrack.getSources has been removed and is no longer supported.Gambado
S
3

This helped me:

function(videoStream) {
    // 'success' callback - user has given permission to use the camera
    if (videoStream.getVideoTracks().length > 0) {
        // my code to use the camera here ... 
    }
}
Semipalmate answered 5/3, 2014 at 8:21 Comment(0)
E
2

The navigator.mediaDevices apis have stabiliized in the last half-decade or so.

You can now do this from your browser javascript.

{
  navigator.mediaDevices.enumerateDevices()
  .then ( function (devices) {
      console.log(devices)
      const videoDevices = devices.filter(device => device.kind === 'videoinput')
      console.log(videoDevices)
  })
}

If your machine has any video devices, you'll get something back in the videoDevices array after filtering the devices array.

But, unless your program has already called getUserMedia() and obtained permission, you can't (at least on Google Chrome) tell how many webcams you have or what their label values (names) are. Because cybercreeps.

Esmerolda answered 29/9, 2020 at 18:25 Comment(0)
B
0

The getUserMedia API is still quite fresh out of the press and will have some bugs and things to be improved, like this problem.

But at the moment I don't see a way for you to check if the computer actually has a camera. Though you could use Flash :-( to detect it I think...

Brat answered 15/11, 2012 at 18:22 Comment(0)
S
0

You can use DetectRTC from Muaz Khan webrtc experiments: https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC

Use:

DetectRTC.audioInputDevices
DetectRTC.audioOutputDevices
DetectRTC.videoInputDevices

to get de devices.

Silicle answered 22/2, 2016 at 14:45 Comment(0)
S
0

In 2024, you can use navigator.mediaDevices.getUserMedia({ video: true })

(async () {
  const mediaStream = await navigator.mediaDevices.getUserMedia({ video: true });
  console.log(mediaStream.active);
})();
Schleswig answered 10/6 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.