Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Video

Authentication

To receive video streams from NVR, HTTP Basic / Digest and RTSP Digest authentication is required. The user must have permission to view cameras.

HLS Live

To receive live video stream with HLS protocol, use following HTTP path on NVR:

/live/videoN.m3u8

the N in videoN.m3u8 is camera number which starts from 1.

You can specify the format or profile parameter like:

/live/video1.m3u8?format=FORMAT

the FORMAT must be the one of mpegts, fmp4. (default: mpegts)

/live/video1.m3u8?profile=PROFILE

the PROFILE must be the one of high, normal, low. (default: high)

To use Internet browser with hls.js:

<!doctype html>
<html>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.7.4/dist/hls.min.js"></script>
<video id="video" width="320" height="240" autoplay controls>
  Your browser does not support the video tag.
</video>
<script>
  var user = 'USER';
  var password = 'PASSWORD';
  var videoSrc = '/live/videoN.m3u8';
  var video = document.getElementById('video');

  async function sha256(text) {
    var hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
    return Array.from(new Uint8Array(hash), (byte) => byte.toString(16).padStart(2, '0')).join('');
  }

  // Reads the SHA-256 Digest challenge from the 401 response of a request without credentials.
  async function getChallenge(url) {
    var response = await fetch(url, { credentials: 'omit', cache: 'no-store' });
    var header = response.headers.get('WWW-Authenticate');

    // Firefox drops a WWW-Authenticate header that is sent several times from the fetch response.
    // The Authorization header keeps Firefox from showing its login dialog for the 401 response.
    if (header === null) {
      header = await new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.setRequestHeader('Authorization', 'Digest');
        xhr.onload = () => resolve(xhr.getResponseHeader('WWW-Authenticate') || '');
        xhr.onerror = reject;
        xhr.send();
      });
    }

    var digest = header.split(/(?=\bDigest\s)/).find((value) => /algorithm=SHA-256\b/i.test(value));
    if (!digest)
      throw new Error('NVR does not offer SHA-256 Digest authentication');

    var challenge = { count: 0, time: Date.now() };
    digest.replace(/(\w+)=(?:"([^"]*)"|([^,\s]*))/g, function (match, key, quoted, token) {
      challenge[key] = quoted !== undefined ? quoted : token;
    });
    return challenge;
  }

  async function authorization(challenge, url) {
    var target = new URL(url, location.href);
    var uri = target.pathname + target.search;
    var nc = (++challenge.count).toString(16).padStart(8, '0');
    var cnonce = crypto.randomUUID().replace(/-/g, '');
    var ha1 = await sha256(user + ':' + challenge.realm + ':' + password);
    var ha2 = await sha256('GET:' + uri);
    var response = await sha256([ha1, challenge.nonce, nc, cnonce, 'auth', ha2].join(':'));

    return 'Digest username="' + user + '", realm="' + challenge.realm + '", nonce="' + challenge.nonce +
      '", uri="' + uri + '", algorithm=SHA-256, response="' + response + '", opaque="' + challenge.opaque +
      '", qop=auth, nc=' + nc + ', cnonce="' + cnonce + '"';
  }

  async function play() {
    var path = new URL(videoSrc, location.href).pathname;
    var challenge = await getChallenge(videoSrc);
    var hls = new Hls({
      lowLatencyMode: MODE,

      // NVR authenticates only the first playlist path, also when a live playlist is reloaded.
      xhrSetup: async function (xhr, url) {
        if (new URL(url, location.href).pathname !== path)
          return;

        // NVR may accept a nonce only for 30 seconds, so get a new challenge every 15 seconds.
        if (Date.now() - challenge.time > 15000)
          challenge = await getChallenge(videoSrc);

        var value = await authorization(challenge, url);
        xhr.open('GET', url, true);
        xhr.setRequestHeader('Authorization', value);
      }
    });

    hls.loadSource(videoSrc);
    hls.attachMedia(video);
  }

  if (Hls.isSupported())
    play().catch((error) => console.error(error));
</script>
</html>

the N in videoN.m3u8 is camera number which starts from 1. the USER and PASSWORD are NVR user accounts. the MODE is the supported low latency HLS set in NVR, and it is true or false.

The example sends the user account in a Digest Authorization header, not in the URL, and has these requirements:

  • The page must be opened from the same origin as NVR, for example through a reverse proxy. NVR does not answer CORS preflight requests, so a page on another origin cannot send the Authorization header to NVR.
  • The page must be opened with https, because crypto.subtle works only in a secure context.
  • NVR offers SHA-256 Digest only when every NVR user has a password for HTTP. NVR also offers MD5 Digest unless the NVR administrator turns it off.
  • The example uses SHA-256 only, because the Web Crypto API (crypto.subtle) of browsers has no MD5. To use MD5 Digest, calculate the same values with an MD5 function instead of sha256 and send algorithm=MD5. If NVR does not offer SHA-256 Digest, the example stops with an error.
  • The browser must support hls.js. The native HLS player of Safari cannot send the Authorization header, so the example does not use it.
  • NVR may accept a nonce only for 30 seconds after it sends the challenge. The example gets a new challenge every 15 seconds while it reloads a live playlist.

MP4 Live

To receive live video stream in fragmented MP4 format, use following HTTP path on NVR:

/live/videoN.mp4

the N in videoN.mp4 is camera number which starts from 1.

You can specify the profile parameter like:

/live/video1.mp4?profile=PROFILE

the PROFILE must be the one of high, normal, low. (default: high)

MJPEG Live

To receive live video stream in Motion JPEG format, use following HTTP path on NVR:

/live/videoN.mjpg

the N in videoN.mjpg is camera number which starts from 1.

JPEG Snapshot

To receive live video snapshot, use following HTTP path on NVR:

/live/videoN.jpg

the N in videoN.jpg is camera number which starts from 1.

MP4 Download

To download recorded video streams in fragmented MP4 format, use following HTTP path on NVR:

/download/videoN.mp4?start=START&end=END

or

/download/videoN.mp4?start=START&duration=DURATION

the N in videoN.mp4 is camera number which starts from 1. the START / END are ISO 8601 format like 2018-09-06T15:14:03. DURATION is the playback time from the start time. end time regards as start time + duration minutes. If end / duration parameter is missing, end time regards as start time + 10 minutes. You can also use length or len instead of duration.

To download recorded video streams in normal indexed MP4 format, add index=1 parameter like:

/download/videoN.mp4?start=START&end=END&index=1

Note that the response time may be slow becauses MP4 files must be generated in the recording disk. You can specify index=2 parameter to generate MP4 files in the memory, but it’s not recommended in most case.

When index parameter is set to 1 or 2, you can use timestamp parameter:

  • 0: Do not display recording time
  • 1: Embed recording time as subtitle (default)

RTSP Live

To receive live video stream with RTSP protocol, use following RTSP path on NVR:

/videoN

the N in videoN is camera number which starts from 1. For example, full URL may be rtsp://demo.emstone.com/video1.

You can specify the profile parameter like:

/video1?profile=PROFILE

the PROFILE must be the one of high, normal, low. (default: high)

RTSP Playback

To receive playback video stream with RTSP protocol, use following RTSP path on NVR:

/playback/videoN?start=START&end=END&speed=SPEED

The N in videoN.mp4 is camera number which starts from 1. The START / END are ISO 8601 format like 2021-01-20T15:14:03. If START or END is not in this format, the server answers 400 Bad Request. The SPEED is playback speed. 1.0 means 1x speed, 2.0 means 2x speed.

For example, play Camera 1 from 2021-01-20 15:14:00 to 2021-01-20 15:24:00 by 1x speed like:

/playback/videoN?start=2021-01-20T15:14:00&end=2021-01-20T15:24:00&speed=1.0

MJPEG / JPEG (Legacy)

To receive live video stream in MJPEG format, use following HTTP path on NVR:

/mjpeg.cgi?channel=N

the N in channel=N is camera number which starts from 0.

To receive live video snapshot image in JPEG format, add snapshot=on parameter like:

/mjpeg.cgi?channel=0&snapshot=on

HLS Playback

To playback recorded video streams with HLS protocol, use following HTTP path on NVR:

/playback/videoN.m3u8?start=START&end=END

or

/playback/videoN.m3u8?start=START&duration=DURATION

To use Internet browser with hls.js, use the page of HLS Live with these changes:

  var videoSrc = '/playback/videoN.m3u8?start=START&end=END';
  ...
    var hls = new Hls({
      xhrSetup: ...
    });
  ...
  video.playbackRate = SPEED;

The requirements of the HLS Live example apply too. The header is sent only to the requests of the first playlist path; the playlist that the first playlist points to needs no authentication.

the N in videoN.m3u8 is camera number which starts from 1. the START / END are ISO 8601 format like 2018-09-06T15:14:03. DURATION is the playback time from the start time. end time regards as start time + duration minutes. If end / duration parameter is missing, end time regards as start time + 10 minutes. You can also use length or len instead of duration.

You can specify the format parameter like:

/playback/videoN.m3u8?format=FORMAT

the FORMAT must be the one of mpegts, fmp4. (default: mpegts) the SPEED indicates the current playback speed of the video. (HTML DOM Video playbackRate Property)

MP4 Playback

To playback recorded video streams in fragmented MP4 format, use following HTTP path on NVR:

/playback/videoN.mp4?start=START&end=END

or

/playback/videoN.mp4?start=START&duration=DURATION

the N in videoN.mp4 is camera number which starts from 1. the START / END are ISO 8601 format like 2018-09-06T15:14:03. DURATION is the playback time from the start time. end time regards as start time + duration minutes. If end / duration parameter is missing, end time regards as start time + 10 minutes. You can also use length or len instead of duration.

HTTP Response Code

NVR replies error status with following HTTP response code:

CodeDescription
400 Bad RequestBad request (ex: wrong format of channels or profile)
401 UnauthorizedAuthentication failure
403 ForbiddenThere is no streamable or no permission.
404 Not FoundThere is unused channel.
502 Bad GatewayThere is no video signal.