@volcengine/veplayer-plugin 2.8.0-rc.1 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1287,12 +1287,25 @@ var KeySystem = /* @__PURE__ */ ((KeySystem2) => {
1287
1287
  KeySystem2["playready"] = "com.microsoft.playready";
1288
1288
  return KeySystem2;
1289
1289
  })(KeySystem || {});
1290
- function selectKeySystem() {
1290
+ var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
1291
+ ErrorCode2[ErrorCode2["UNSUPPORTED"] = 7101] = "UNSUPPORTED";
1292
+ ErrorCode2[ErrorCode2["RECORD"] = 7102] = "RECORD";
1293
+ ErrorCode2[ErrorCode2["NOMEDIAKEY"] = 7103] = "NOMEDIAKEY";
1294
+ ErrorCode2[ErrorCode2["NOKEYSESSION"] = 7104] = "NOKEYSESSION";
1295
+ ErrorCode2[ErrorCode2["GENERATEREQUESTFAIL"] = 7105] = "GENERATEREQUESTFAIL";
1296
+ ErrorCode2[ErrorCode2["LACKSERVERCERTIFICATEPATH"] = 7106] = "LACKSERVERCERTIFICATEPATH";
1297
+ ErrorCode2[ErrorCode2["LACKSERVERPROCESSSPCPATH"] = 7107] = "LACKSERVERPROCESSSPCPATH";
1298
+ ErrorCode2[ErrorCode2["LICENSEFAIL"] = 7108] = "LICENSEFAIL";
1299
+ ErrorCode2[ErrorCode2["KEYERROR"] = 7109] = "KEYERROR";
1300
+ ErrorCode2[ErrorCode2["CERTFAIL"] = 7110] = "CERTFAIL";
1301
+ return ErrorCode2;
1302
+ })(ErrorCode || {});
1303
+ function selectKeySystem(contentType) {
1291
1304
  const options = [
1292
1305
  {
1293
1306
  initDataTypes: ["keyids", "webm"],
1294
1307
  audioCapabilities: [{ contentType: 'audio/mp4; codecs="mp4a.40.2"' }],
1295
- videoCapabilities: [
1308
+ videoCapabilities: contentType ? [{ contentType }] : [
1296
1309
  { contentType: 'video/mp4; codecs="avc1.42c01e"' },
1297
1310
  { contentType: 'video/mp4; codecs="hev1.1.6.L120.90"' },
1298
1311
  { contentType: 'video/webm; codecs="vp8"' }
@@ -1303,7 +1316,8 @@ function selectKeySystem() {
1303
1316
  navigator.requestMediaKeySystemAccess(KeySystem.fairplay, options).then((access) => {
1304
1317
  resolve({
1305
1318
  access,
1306
- system: KeySystem.fairplay
1319
+ system: KeySystem.fairplay,
1320
+ systemName: "fairplay"
1307
1321
  });
1308
1322
  }).catch(() => {
1309
1323
  if (globalThis.WebKitMediaKeys && globalThis.WebKitMediaKeys.isTypeSupported(
@@ -1312,22 +1326,38 @@ function selectKeySystem() {
1312
1326
  )) {
1313
1327
  resolve({
1314
1328
  access: null,
1315
- system: KeySystem.fairplay
1329
+ system: KeySystem.fairplay,
1330
+ systemName: "fairplay"
1316
1331
  });
1317
1332
  return;
1318
1333
  }
1319
1334
  navigator.requestMediaKeySystemAccess(KeySystem.widevine, options).then((access) => {
1320
1335
  resolve({
1321
1336
  access,
1322
- system: KeySystem.widevine
1337
+ system: KeySystem.widevine,
1338
+ systemName: "widevine"
1323
1339
  });
1324
1340
  }).catch(() => {
1325
1341
  navigator.requestMediaKeySystemAccess(KeySystem.playready, options).then((access) => {
1326
1342
  resolve({
1327
1343
  access,
1328
- system: KeySystem.playready
1344
+ system: KeySystem.playready,
1345
+ systemName: "playready"
1329
1346
  });
1330
- }).catch(reject);
1347
+ }).catch((e) => {
1348
+ if (globalThis.MSMediaKeys && globalThis.MSMediaKeys.isTypeSupported(
1349
+ KeySystem.playready,
1350
+ "video/mp4"
1351
+ )) {
1352
+ resolve({
1353
+ access: null,
1354
+ system: KeySystem.playready,
1355
+ systemName: "playready"
1356
+ });
1357
+ } else {
1358
+ reject(e);
1359
+ }
1360
+ });
1331
1361
  });
1332
1362
  });
1333
1363
  });
@@ -1344,7 +1374,7 @@ function stringToArray(string) {
1344
1374
  }
1345
1375
  return array;
1346
1376
  }
1347
- class Mobile extends BasePlugin {
1377
+ class Drm extends BasePlugin {
1348
1378
  constructor(options) {
1349
1379
  super(options);
1350
1380
  this.unsupported = false;
@@ -1359,17 +1389,15 @@ class Mobile extends BasePlugin {
1359
1389
  event.target;
1360
1390
  let initData = event.initData;
1361
1391
  const initDataType = event.initDataType;
1362
- const contentId = this.extractContentId(initData);
1363
- if (this.keySystem === KeySystem.fairplay && !this.certificate) {
1364
- return this.suspend = () => {
1365
- this.afterInitDate(initData, contentId, initDataType);
1366
- };
1392
+ let contentId;
1393
+ if (this.keySystem === KeySystem.fairplay) {
1394
+ contentId = this.extractContentId(initData);
1367
1395
  }
1368
1396
  this.afterInitDate(initData, contentId, initDataType);
1369
1397
  };
1370
1398
  this.licenseRequestReady = (event) => {
1371
1399
  if (!this.serverProcessSPCPath) {
1372
- this.emitError("lack serverProcessSPCPath");
1400
+ this.emitError(ErrorCode.LACKSERVERPROCESSSPCPATH, "lack serverProcessSPCPath");
1373
1401
  return;
1374
1402
  }
1375
1403
  const message = event.message;
@@ -1379,10 +1407,12 @@ class Mobile extends BasePlugin {
1379
1407
  request.addEventListener("error", this.licenseRequestFailed(event), false);
1380
1408
  var params = new Uint8Array(message);
1381
1409
  request.open("POST", this.serverProcessSPCPath, true);
1382
- if (this.keySystem === KeySystem.playready) {
1383
- request.setRequestHeader("Content-type", "text/xml");
1384
- } else {
1385
- request.setRequestHeader("Content-type", "application/octet-stream");
1410
+ if (!this.config.ignoreLicenseHeader) {
1411
+ if (this.keySystem === KeySystem.playready) {
1412
+ request.setRequestHeader("Content-type", "text/xml; charset=utf-8");
1413
+ } else {
1414
+ request.setRequestHeader("Content-type", "application/octet-stream");
1415
+ }
1386
1416
  }
1387
1417
  request.send(params);
1388
1418
  };
@@ -1390,16 +1420,13 @@ class Mobile extends BasePlugin {
1390
1420
  const request = event.target;
1391
1421
  const arrayBuffer = request.response;
1392
1422
  let license;
1393
- if (this.keySystem === KeySystem.playready) {
1394
- license = this.parseServerResponse(arrayBuffer);
1395
- } else {
1396
- license = new Uint8Array(arrayBuffer);
1397
- }
1423
+ license = new Uint8Array(arrayBuffer);
1398
1424
  if (license) {
1399
1425
  this.keySession.update(license);
1400
1426
  } else {
1401
1427
  this.emitError(
1402
- `The license response no array buffer, licence: ${license}`
1428
+ ErrorCode.LICENSEFAIL,
1429
+ `The license response no array buffer, license: ${license}`
1403
1430
  );
1404
1431
  }
1405
1432
  };
@@ -1410,16 +1437,17 @@ class Mobile extends BasePlugin {
1410
1437
  } else {
1411
1438
  const request = event.target;
1412
1439
  this.emitError(
1440
+ ErrorCode.LICENSEFAIL,
1413
1441
  `The license request fail, status: ${request.status}, statusText: ${request.statusText}`
1414
1442
  );
1415
1443
  }
1416
1444
  };
1417
1445
  this.onKeyError = () => {
1418
- this.emitError("A decryption key error was encountered");
1446
+ this.emitError(ErrorCode.KEYERROR, "A decryption key error was encountered");
1419
1447
  };
1420
1448
  this.loadCertificate = () => {
1421
1449
  if (!this.serverCertificatePath) {
1422
- this.emitError("lack serverCertificatePath");
1450
+ this.emitError(ErrorCode.LACKSERVERCERTIFICATEPATH, "lack serverCertificatePath");
1423
1451
  return;
1424
1452
  }
1425
1453
  const request = new XMLHttpRequest();
@@ -1429,17 +1457,18 @@ class Mobile extends BasePlugin {
1429
1457
  request.open("GET", this.serverCertificatePath, true);
1430
1458
  request.send();
1431
1459
  };
1432
- this.emitError = (message) => {
1460
+ this.emitError = (errorCode, message) => {
1433
1461
  this.emit("error", {
1434
- errorCode: 7100,
1462
+ errorCode,
1435
1463
  message,
1436
- drm_sdk_version: "0.0.0-alpha.5"
1464
+ // @ts-ignore: Cannot find name '"0.0.0-alpha.13"'
1465
+ drm_sdk_version: "0.0.0-alpha.13"
1437
1466
  });
1438
1467
  };
1439
1468
  this.onCertificateLoaded = (event) => {
1440
1469
  const request = event.target;
1441
1470
  this.certificate = new Uint8Array(request.response);
1442
- this.suspend && this.suspend();
1471
+ this.loadSource();
1443
1472
  };
1444
1473
  this.onCertificateError = (event) => {
1445
1474
  if (this.certRetryTime > 0) {
@@ -1448,6 +1477,7 @@ class Mobile extends BasePlugin {
1448
1477
  } else {
1449
1478
  const request = event.target;
1450
1479
  this.emitError(
1480
+ ErrorCode.CERTFAIL,
1451
1481
  `get cert fail, status: ${request.status}, statusText: ${request.statusText}`
1452
1482
  );
1453
1483
  }
@@ -1457,12 +1487,13 @@ class Mobile extends BasePlugin {
1457
1487
  };
1458
1488
  this.reset();
1459
1489
  const config = options.config || {};
1460
- this.serverCertificatePath = config.serverCertificatePath;
1461
- this.serverProcessSPCPath = config.serverProcessSPCPath;
1490
+ this.config = config;
1491
+ this.contentType = this.config.contentType;
1462
1492
  if (typeof config.unRecord === "boolean") {
1463
1493
  this.unRecord = config.unRecord;
1464
1494
  }
1465
1495
  this.checkUnrecord();
1496
+ this.player.handleSource = false;
1466
1497
  }
1467
1498
  static get pluginName() {
1468
1499
  return "drm";
@@ -1473,8 +1504,6 @@ class Mobile extends BasePlugin {
1473
1504
  this._serverCertificatePath = x;
1474
1505
  if (this.keySystem === KeySystem.fairplay) {
1475
1506
  this.certificate = void 0;
1476
- this.suspend = () => {
1477
- };
1478
1507
  this.loadCertificate();
1479
1508
  }
1480
1509
  }
@@ -1484,9 +1513,11 @@ class Mobile extends BasePlugin {
1484
1513
  afterCreate() {
1485
1514
  this.media = this.player.video;
1486
1515
  this.media.addEventListener("webkitneedkey", this.onNeedKey, false);
1516
+ this.media.addEventListener("msneedkey", this.onNeedKey, false);
1487
1517
  this.media.addEventListener("needkey", this.onNeedKey, false);
1488
1518
  this.media.addEventListener("encrypted", this.onNeedKey);
1489
1519
  this.player.on("urlchange", this.restart);
1520
+ this.player.on("reset", this.restart);
1490
1521
  }
1491
1522
  async checkUnrecord() {
1492
1523
  if (this.unRecord) {
@@ -1503,60 +1534,101 @@ class Mobile extends BasePlugin {
1503
1534
  });
1504
1535
  this.unsupported = !powerEfficient;
1505
1536
  } catch (error) {
1506
- this.emitError(`check powerEfficient failed, ${error.message}`);
1537
+ this.emitError(ErrorCode.RECORD, `check powerEfficient failed, ${error.message}`);
1507
1538
  this.unsupported = true;
1508
1539
  }
1509
1540
  }
1510
1541
  if (this.unsupported)
1511
1542
  return;
1512
- selectKeySystem().then(({ access, system }) => {
1543
+ selectKeySystem(this.contentType).then(({ access, system, systemName }) => {
1513
1544
  this.keySystem = system;
1545
+ this.systemName = systemName;
1546
+ this.setRequestUrl(systemName);
1514
1547
  if (system === KeySystem.fairplay) {
1515
1548
  this.loadCertificate();
1549
+ } else {
1550
+ this.loadSource();
1516
1551
  }
1517
1552
  if (access) {
1518
1553
  this.keySystemAccess = access;
1519
1554
  }
1520
1555
  }).catch((error) => {
1521
- this.emitError(error.message);
1556
+ this.emitError(ErrorCode.UNSUPPORTED, error.message);
1522
1557
  this.unsupported = true;
1523
1558
  });
1524
1559
  }
1560
+ loadSource() {
1561
+ this.player.retry();
1562
+ }
1563
+ setRequestUrl(systemName) {
1564
+ const systemConfig = this.config[systemName] || this.config;
1565
+ this.serverCertificatePath = systemConfig.serverCertificatePath;
1566
+ this.serverProcessSPCPath = systemConfig.serverProcessSPCPath;
1567
+ }
1568
+ async updateConfig(config) {
1569
+ Object.assign(this.config, config);
1570
+ if (this.config.unRecord && this.contentType !== this.config.contentType) {
1571
+ this.contentType = this.config.contentType;
1572
+ await this.checkUnrecord();
1573
+ }
1574
+ this.setRequestUrl(this.systemName);
1575
+ }
1525
1576
  afterInitDate(initData, contentId, initDataType) {
1526
1577
  if (!this.keySystemAccess) {
1527
- const mediaKeys = new globalThis.WebKitMediaKeys(this.keySystem);
1528
- this.media.webkitSetMediaKeys(mediaKeys);
1529
- if (!this.media.webkitKeys) {
1530
- this.emitError("Could not create MediaKeys");
1531
- return;
1532
- }
1533
- const keySession = mediaKeys.createSession(
1534
- "video/mp4",
1535
- this.concatInitDataIdAndCertificate(initData, contentId)
1536
- );
1537
- this.keySession = keySession;
1538
- if (!keySession) {
1539
- this.emitError("Could not create key session");
1540
- return;
1578
+ if (this.keySystem === KeySystem.fairplay) {
1579
+ const mediaKeys = new globalThis.WebKitMediaKeys(this.keySystem);
1580
+ this.media.webkitSetMediaKeys(mediaKeys);
1581
+ if (!this.media.webkitKeys) {
1582
+ this.emitError(ErrorCode.NOMEDIAKEY, "Could not create MediaKeys");
1583
+ return;
1584
+ }
1585
+ const keySession = mediaKeys.createSession(
1586
+ "video/mp4",
1587
+ this.concatInitDataIdAndCertificate(initData, contentId)
1588
+ );
1589
+ this.keySession = keySession;
1590
+ if (!keySession) {
1591
+ this.emitError(ErrorCode.NOKEYSESSION, "Could not create key session");
1592
+ return;
1593
+ }
1594
+ keySession.addEventListener(
1595
+ "webkitkeymessage",
1596
+ this.licenseRequestReady
1597
+ );
1598
+ keySession.addEventListener("webkitkeyerror", this.onKeyError);
1599
+ } else if (this.keySystem === KeySystem.playready) {
1600
+ const mediaKeys = new globalThis.MSMediaKeys(this.keySystem);
1601
+ this.media.msSetMediaKeys(mediaKeys);
1602
+ const keySession = mediaKeys.createSession(
1603
+ "video/mp4",
1604
+ new Uint8Array(initData),
1605
+ null
1606
+ );
1607
+ this.keySession = keySession;
1608
+ if (!keySession) {
1609
+ this.emitError(ErrorCode.NOKEYSESSION, "Could not create key session");
1610
+ return;
1611
+ }
1612
+ keySession.addEventListener("mskeymessage", this.licenseRequestReady);
1613
+ keySession.addEventListener("mskeyerror", this.onKeyError);
1541
1614
  }
1542
- keySession.addEventListener("webkitkeymessage", this.licenseRequestReady);
1543
- keySession.addEventListener("webkitkeyerror", this.onKeyError);
1544
1615
  } else {
1545
1616
  this.keySystemAccess.createMediaKeys().then((mediaKeys) => {
1546
1617
  this.media.setMediaKeys(mediaKeys);
1547
1618
  if (this.media.mediaKeys) {
1548
- this.emitError("Could not create MediaKeys");
1619
+ this.emitError(ErrorCode.NOMEDIAKEY, "Could not create MediaKeys");
1549
1620
  return;
1550
1621
  }
1551
1622
  const keySession = mediaKeys.createSession();
1552
1623
  this.keySession = keySession;
1553
1624
  if (!keySession) {
1554
- this.emitError("Could not create key session");
1625
+ this.emitError(ErrorCode.NOKEYSESSION, "Could not create key session");
1555
1626
  return;
1556
1627
  }
1557
1628
  keySession.addEventListener("message", this.licenseRequestReady);
1558
- keySession.generateRequest(initDataType, initData).catch((error) => {
1629
+ keySession.generateRequest(initDataType || this.config.initDataType, initData).catch((error) => {
1559
1630
  this.emitError(
1631
+ ErrorCode.GENERATEREQUESTFAIL,
1560
1632
  `generateRequest failed, code: ${error.errorCode}, message: ${error.message}`
1561
1633
  );
1562
1634
  });
@@ -1630,20 +1702,32 @@ class Mobile extends BasePlugin {
1630
1702
  this.suspend = void 0;
1631
1703
  this.keySession = void 0;
1632
1704
  this.needKeyed = false;
1705
+ if (this.media) {
1706
+ try {
1707
+ this.media.src = "";
1708
+ if (this.keySystem === KeySystem.fairplay) {
1709
+ this.media.webkitSetMediaKeys(null);
1710
+ } else if (this.keySystem === KeySystem.playready) {
1711
+ this.media.msSetMediaKeys(null);
1712
+ } else {
1713
+ this.media.setMediaKeys(null);
1714
+ }
1715
+ } catch (error) {
1716
+ }
1717
+ }
1633
1718
  }
1634
1719
  destroy() {
1635
1720
  this.player.off("urlchange", this.restart);
1636
- }
1637
- }
1638
- class DrmPlugin extends Mobile {
1639
- updateDrmConfig(data) {
1640
- data.serverProcessSPCPath && (this.serverProcessSPCPath = data.serverProcessSPCPath);
1641
- data.serverCertificatePath && (this.serverCertificatePath = data.serverCertificatePath);
1721
+ this.player.off("reset", this.restart);
1722
+ this.media.removeEventListener("webkitneedkey", this.onNeedKey, false);
1723
+ this.media.removeEventListener("msneedkey", this.onNeedKey, false);
1724
+ this.media.removeEventListener("needkey", this.onNeedKey, false);
1725
+ this.media.removeEventListener("encrypted", this.onNeedKey);
1642
1726
  }
1643
1727
  }
1644
1728
  const DynamicModule = window["VePlayer"].DynamicModule;
1645
1729
  const __PLUGIN_NAME__ = DynamicModule.PluginDrm;
1646
1730
  export {
1647
- DrmPlugin,
1731
+ Drm as DrmPlugin,
1648
1732
  __PLUGIN_NAME__
1649
1733
  };