# HLS.js Integration

This page demonstrates how to authenticate and play ClearKey-encrypted videos via HLS.js using the getXhrSetup() function from the SDK to attach the Session Cookie to Key Requests.


# Integration Steps

# Step 1: Load HLS.js and the SDK

Add the Script Tags for HLS.js and the Passport ClearKey Web SDK to the HTML page.

<!-- Load HLS.js -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest/dist/hls.min.js"></script>
<!-- Load Passport ClearKey Web SDK -->
<script src="https://byteark-sdk.cdn.byteark.com/passport-clearkey/web-sdk/v0/passport-web-sdk.js"></script>
1
2
3
4
5

# Step 2: Authenticate with the Key Server

Call initAuth() to create a Session Cookie for video decryption.

<script>
  var sdk = window.PassportWebSDK;
  sdk.initAuth('YOUR_JWT_TOKEN', 'https://YOUR_KEY_SERVER_URL')
    .then(function(result) {
      console.log('Authentication successful:', result.message);
    });
</script>
1
2
3
4
5
6
7
8

# Step 3: Create an HLS.js Instance with xhrSetup

Use getXhrSetup() so that HLS.js automatically attaches the Session Cookie to Key Requests.

<script>
  var xhrSetup = sdk.getXhrSetup();
  var hls = new Hls({ xhrSetup: xhrSetup });
  var video = document.getElementById('video');
  hls.loadSource('https://example.com/video/playlist.m3u8');
  hls.attachMedia(video);
  hls.on(Hls.Events.MANIFEST_PARSED, function() {
    video.play();
  });
</script>
1
2
3
4
5
6
7
8
9
10
11
12

Note: getXhrSetup() sets withCredentials = true only for Key Requests (excluding .m3u8 and .ts requests) to avoid CORS issues with Manifest Requests.

# Full Source Code Example

A complete HTML example demonstrating authentication and ClearKey-encrypted video playback via HLS.js.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>ClearKey SDK — HLS.js Integration</title>
  </head>
  <body>
    <h1>Passport ClearKey Web SDK — HLS.js</h1>
    <video id="video" controls style="width:100%;"></video>
    <pre id="log">Waiting…</pre>
    <!-- Load HLS.js -->
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest/dist/hls.min.js"></script>
    <!-- Load Passport ClearKey Web SDK -->
    <script src="https://byteark-sdk.cdn.byteark.com/passport-clearkey/web-sdk/v0/passport-web-sdk.js"></script>
    <script>
      var sdk = window.PassportWebSDK;
      var logEl = document.getElementById('log');
      function log(msg) {
        logEl.textContent += '\n' + msg;
      }
      // 1. Authenticate
      sdk.initAuth('YOUR_JWT_TOKEN', 'https://YOUR_KEY_SERVER_URL', { logEnabled: true })
        .then(function(result) {
          log('Authentication successful: ' + result.message);
          // 2. Create HLS.js with xhrSetup
          var xhrSetup = sdk.getXhrSetup();
          var hls = new Hls({ xhrSetup: xhrSetup });
          var video = document.getElementById('video');
          hls.loadSource('YOUR_HLS_URL');
          hls.attachMedia(video);
          hls.on(Hls.Events.MANIFEST_PARSED, function() {
            log('Manifest parsed — starting playback');
            video.play();
          });
          hls.on(Hls.Events.ERROR, function(_event, data) {
            log('hls.js error: ' + data.type + ' / ' + data.details);
            if (data.fatal) {
              hls.destroy();
            }
          });
        })
        .catch(function(err) {
          log('✖ Error: ' + err.message);
        });
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56