Tutorial

How To Access Front and Rear Cameras with JavaScript's getUserMedia()

Updated on May 27, 2020
Default avatar

By Chris Nwamba

English
How To Access Front and Rear Cameras with JavaScript's getUserMedia()

Introduction

With HTML5 came the introduction of APIs with access to device hardware, including the MediaDevices API. This API provides access to media input devices like audio and video.

With this API’s help, developers can access audio and video devices to stream and display live video feeds in the browser. In this tutorial, you’ll access the video feed from the user’s device and display it in the browser using the getUserMedia method.

The getUserMedia API makes use of the media input devices to produce a MediaStream. This MediaStream contains the requested media types, whether audio or video. Using the stream returned from the API, video feeds can be displayed on the browser, which is useful for real-time communication on the browser.

When used along with the MediaStream Recording API, you can record and store media data captured on the browser. This API only works on secure origins like the rest of the newly introduced APIs, but it also works on localhost and file URLs.

Prerequisites

This tutorial will first explain concepts and demonstrate examples with Codepen. In the final step, you will create a functioning video feed for the browser.

Step 1 — Checking Device Support

First, you will see how to check if the user’s browser supports the mediaDevices API. This API exists within the navigator interface and contains the current state and identity of the user agent. The check is performed with the following code that can be pasted into Codepen:

if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
  console.log("Let's get this party started")
}

First, this checks if the mediaDevices API exists within the navigator and then checks if the getUserMedia API is available within the mediaDevices. If this returns true, you can get started.

Step 2 — Requesting User Permission

After confirming the browser’s support for getUserMedia, you need to request permission to make use of the media input devices on the user agent. Typically, after a user grants permission, a Promise is returned which resolves to a media stream. This Promise isn’t returned when the permission is denied by the user, which blocks access to these devices.

Paste the following line into Codepen to request permission:

navigator.mediaDevices.getUserMedia({video: true})

The object provided as an argument for the getUserMedia method is called constraints. This determines which of the media input devices you are requesting permissions to access. For example, if the object contains audio: true, the user will be asked to grant access to the audio input device.

Step 3 — Understanding Media Constraints

This section will cover the general concept of contraints. The constraints object is a MediaStreamConstraints object that specifies the types of media to request and the requirements of each media type. You can specify requirements for the requested stream using the constraints object, like the resolution of the stream to use (front, back).

You must specify either audio or video when making the request. A NotFoundError will be returned if the requested media types can’t be found on the user’s browser.

If you intend to request a video stream of 1280 x 720 resolution, you can update the constraints object to look like this:

{
  video: {
    width: 1280,
    height: 720,
  }
}

With this update, the browser will try to match the specified quality settings for the stream. If the video device can’t deliver this resolution, the browser will return other available resolutions.

To ensure that the browser returns a resolution not lower than the one provided you will have to make use of the min property. Here is how you could update the constraints object to include the min property:

{
  video: {
    width: {
      min: 1280,
    },
    height: {
      min: 720,
    }
  }
}

This will ensure that the stream resolution returned will be at least 1280 x 720. If this minimum requirement can’t be met, the promise will be rejected with an OverconstrainedError.

In some cases you may be concerned about saving data and need the stream to not exceed a set resolution. This can come in handy when the user is on a limited plan. To enable this functionality, update the constraints object to contain a max field:

{
  video: {
    width: {
      min: 1280,
      max: 1920,
    },
    height: {
      min: 720,
      max: 1080
    }
  }
}

With these settings, the browser will ensure that the return stream doesn’t go below 1280 x 720 and doesn’t exceed 1920 x 1080.

Other terms that can be used includes exact and ideal. The ideal setting is typically used along with the min and max properties to find the best possible setting closest to the ideal values provided.

You can update the constraints to use the ideal keyword:

{
  video: {
    width: {
      min: 1280,
      ideal: 1920,
      max: 2560,
    },
    height: {
      min: 720,
      ideal: 1080,
      max: 1440
    }
  }
}

To tell the browser to make use of the front or back (on mobile) camera on devices, you can specify a facingMode property in the video object:

{
  video: {
    width: {
      min: 1280,
      ideal: 1920,
      max: 2560,
    },
    height: {
      min: 720,
      ideal: 1080,
      max: 1440
    },
    facingMode: 'user'
  }
}

This setting will make use of the front-facing camera at all times in all devices. To make use of the back camera on mobile devices, you can alter the facingMode property to environment.

{
  video: {
    ...
    facingMode: {
      exact: 'environment'
    }
  }
}

Step 4 — Using the enumerateDevices Method

When the enumerateDevices method is called, it returns all of the available input media devices available on the user’s PC.

With the method, you can provide the user options on which input media device to use for streaming audio or video content. This method returns a Promise resolved to a MediaDeviceInfo array containing information about each device.

An example of how to make a use of this method is shown in the snippet below:

async function getDevices() {
  const devices = await navigator.mediaDevices.enumerateDevices();
}

A sample response for each of the devices would look like the following:

{
  deviceId: "23e77f76e308d9b56cad920fe36883f30239491b8952ae36603c650fd5d8fbgj",
  groupId: "e0be8445bd846722962662d91c9eb04ia624aa42c2ca7c8e876187d1db3a3875",
  kind: "audiooutput",
  label: "",
}

Note: A label won’t be returned unless an available stream is available, or if the user has granted device access permissions.

Step 5 — Displaying the Video Stream on the Browser

You have gone through the process of requesting and getting access to the media devices, configured constraints to include required resolutions, and selected the camera you will need to record video.

After going through all these steps, you’ll at least want to see if the stream is delivering based on the configured settings. To ensure this, you will make use of the <video> element to display the video stream on the browser.

Like mentioned earlier, the getUserMedia method returns a Promise that can be resolved to a stream. The returned stream can be converted to an object URL using the createObjectURL method. This URL will be set as a video source.

You will create a short demo where we let the user choose from their available list of video devices. using the enumerateDevices method.

This is a navigator.mediaDevices method. It lists the available media devices, such as microphones and cameras. It returns a Promise resolvable to an array of objects detailing the available media devices.

Create an index.html file and update the contents with the code below:

index.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
<div class="display-cover">
    <video autoplay></video>
    <canvas class="d-none"></canvas>

    <div class="video-options">
        <select name="" id="" class="custom-select">
            <option value="">Select camera</option>
        </select>
    </div>

    <img class="screenshot-image d-none" alt="">

    <div class="controls">
        <button class="btn btn-danger play" title="Play"><i data-feather="play-circle"></i></button>
        <button class="btn btn-info pause d-none" title="Pause"><i data-feather="pause"></i></button>
        <button class="btn btn-outline-success screenshot d-none" title="ScreenShot"><i data-feather="image"></i></button>
    </div>
</div>

<script src="https://unpkg.com/feather-icons"></script>
<script src="script.js"></script>
</body>
</html>

In the snippet above, you have set up the elements you will need and a couple of controls for the video. Also included is a button for taking screenshots of the current video feed.

Now, let’s style up these components a bit.

Create a style.css file and copy the following styles into it. Bootstrap was included to reduce the amount of CSS you will need to write to get the components going.

style.css
.screenshot-image {
    width: 150px;
    height: 90px;
    border-radius: 4px;
    border: 2px solid whitesmoke;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.1);
    position: absolute;
    bottom: 5px;
    left: 10px;
    background: white;
}

.display-cover {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 70%;
    margin: 5% auto;
    position: relative;
}

video {
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
}

.video-options {
    position: absolute;
    left: 20px;
    top: 30px;
}

.controls {
    position: absolute;
    right: 20px;
    top: 20px;
    display: flex;
}

.controls > button {
    width: 45px;
    height: 45px;
    text-align: center;
    border-radius: 100%;
    margin: 0 6px;
    background: transparent;
}

.controls > button:hover svg {
    color: white !important;
}

@media (min-width: 300px) and (max-width: 400px) {
    .controls {
        flex-direction: column;
    }

    .controls button {
        margin: 5px 0 !important;
    }
}

.controls > button > svg {
    height: 20px;
    width: 18px;
    text-align: center;
    margin: 0 auto;
    padding: 0;
}

.controls button:nth-child(1) {
    border: 2px solid #D2002E;
}

.controls button:nth-child(1) svg {
    color: #D2002E;
}

.controls button:nth-child(2) {
    border: 2px solid #008496;
}

.controls button:nth-child(2) svg {
    color: #008496;
}

.controls button:nth-child(3) {
    border: 2px solid #00B541;
}

.controls button:nth-child(3) svg {
    color: #00B541;
}

.controls > button {
    width: 45px;
    height: 45px;
    text-align: center;
    border-radius: 100%;
    margin: 0 6px;
    background: transparent;
}

.controls > button:hover svg {
    color: white;
}

The next step is to add functionality to the demo. Using the enumerateDevices method, you will get the available video devices and set it as the options within the select element. Create a file called script.js and update it with the following snippet:

script.js
feather.replace();

const controls = document.querySelector('.controls');
const cameraOptions = document.querySelector('.video-options>select');
const video = document.querySelector('video');
const canvas = document.querySelector('canvas');
const screenshotImage = document.querySelector('img');
const buttons = [...controls.querySelectorAll('button')];
let streamStarted = false;

const [play, pause, screenshot] = buttons;

const constraints = {
  video: {
    width: {
      min: 1280,
      ideal: 1920,
      max: 2560,
    },
    height: {
      min: 720,
      ideal: 1080,
      max: 1440
    },
  }
};

const getCameraSelection = async () => {
  const devices = await navigator.mediaDevices.enumerateDevices();
  const videoDevices = devices.filter(device => device.kind === 'videoinput');
  const options = videoDevices.map(videoDevice => {
    return `<option value="${videoDevice.deviceId}">${videoDevice.label}</option>`;
  });
  cameraOptions.innerHTML = options.join('');
};

play.onclick = () => {
  if (streamStarted) {
    video.play();
    play.classList.add('d-none');
    pause.classList.remove('d-none');
    return;
  }
  if ('mediaDevices' in navigator && navigator.mediaDevices.getUserMedia) {
    const updatedConstraints = {
      ...constraints,
      deviceId: {
        exact: cameraOptions.value
      }
    };
    startStream(updatedConstraints);
  }
};

const startStream = async (constraints) => {
  const stream = await navigator.mediaDevices.getUserMedia(constraints);
  handleStream(stream);
};

const handleStream = (stream) => {
  video.srcObject = stream;
  play.classList.add('d-none');
  pause.classList.remove('d-none');
  screenshot.classList.remove('d-none');
  streamStarted = true;
};

getCameraSelection();

In the snippet above, there are a couple of things going on. Let’s break them down:

  1. feather.replace(): this method call instantiates feather, which is an icon set for web development.
  2. The constraints variable holds the initial configuration for the stream. This will be extended to include the media device the user chooses.
  3. getCameraSelection: this function calls the enumerateDevices method. Then, you filter through the array from the resolved Promise and select video input devices. From the filtered results, you create <option> for the <select> element.
  4. Calling the getUserMedia method happens within the onclick listener of the play button. Here, you will check if this method is supported by the user’s browser before starting the stream.
  5. Next, you will call the startStream function that takes a constraints argument. It calls the getUserMedia method with the provided constraints. handleStream is called using the stream from the resolved Promise. This method sets the returned stream to the video element’s srcObject.

Next, you will add click listeners to the button controls on the page to pause, stop, and take screenshots. Also, you will add a listener to the <select> element to update the stream constraints with the selected video device.

Update the script.js file with the code below:

script.js
...
cameraOptions.onchange = () => {
  const updatedConstraints = {
    ...constraints,
    deviceId: {
      exact: cameraOptions.value
    }
  };
  startStream(updatedConstraints);
};

const pauseStream = () => {
  video.pause();
  play.classList.remove('d-none');
  pause.classList.add('d-none');
};

const doScreenshot = () => {
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  canvas.getContext('2d').drawImage(video, 0, 0);
  screenshotImage.src = canvas.toDataURL('image/webp');
  screenshotImage.classList.remove('d-none');
};

pause.onclick = pauseStream;
screenshot.onclick = doScreenshot;

Now, when you open the index.html file in the browser, clicking the Play button will start the stream.

Here is a complete demo:

https://codepen.io/chrisbeast/pen/ebYwpX

Conclusion

This tutorial introduced the getUserMedia API. It is an interesting addition to HTML5 that eases the process of capturing media on the web.

The API takes a parameter (constraints) that can be used to configure the access to audio and video input devices. It can also be used to specify the video resolution required for your application.

You can extend the demo further to give the user an option to save the screenshots taken, as well as recording and storing video and audio data with the help of MediaStream Recording API.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Chris Nwamba

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This code as-is gives a black screen. It seems the issue is that the “deviceId” parameter is set outside of the “video” section in “updatedConstraints”. Putting “deviceId” in the video section fixes the camera selection and the black screen issue. “updatedConstraints” assignment needs to be re-written to include “deviceId” inside the “video” section. It would be best to just convert “constraints” to a variable and dynamically assign the “deviceId” as needed instead of trying to compose another constant.

This doesn not work on ios 14. The camera’s are recognized, but I get a black screen. Do you know why?

Hello, I can call the camera when I test locally, but I can’t find the media device when I upload it to the server. Do you know why?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel