# การใช้งานกับ HLS.js

หน้านี้แสดงการยืนยันตัวตนและเล่นวิดีโอที่เข้ารหัสแบบ ClearKey ผ่าน HLS.js โดยใช้ฟังก์ชัน getXhrSetup() จาก SDK เพื่อแนบ Session Cookie ไปกับ Key Request


# ขั้นตอนการใช้งาน

# ขั้นตอนที่ 1: โหลด HLS.js และ SDK

เพิ่ม Script Tag ของ HLS.js และ Passport ClearKey Web SDK ในหน้า HTML

<!-- 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

# ขั้นตอนที่ 2: ยืนยันตัวตนกับ Key Server

เรียก initAuth() เพื่อสร้าง Session Cookie สำหรับการถอดรหัสวิดีโอ

<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

# ขั้นตอนที่ 3: สร้าง HLS.js instance พร้อม xhrSetup

ใช้ getXhrSetup() เพื่อให้ HLS.js แนบ Session Cookie ไปกับ Key Request โดยอัตโนมัติ

<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

หมายเหตุ: ฟังก์ชัน getXhrSetup() จะตั้งค่า withCredentials = true เฉพาะ Key Request เท่านั้น (ไม่รวม .m3u8 และ .ts) เพื่อหลีกเลี่ยงปัญหา CORS กับ Manifest Request

# ตัวอย่างซอร์สโค้ด

ตัวอย่าง HTML ที่สมบูรณ์ แสดงการยืนยันตัวตนและเล่นวิดีโอที่เข้ารหัสแบบ ClearKey ผ่าน 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