@codenkay/video-nsgplayer-core 5.1.0 → 5.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Resume playback from `startPosition` after the media duration is known. A short opening buffer no longer pulls that seek back to the start.
8
+ - Keep a 30-second forward buffer, a 60-second ceiling, and a 20MB cap. Adaptive quality stays on the hls.js defaults.
9
+ - Refresh the playback token every 45 seconds by default (`DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS`). The next request uses the new token. A proactive refresh does not restart the loader. Pass `playback.refreshIntervalMs: 0` to refresh only as the credential expires. A refresh after an authorization failure still restarts the loader.
10
+
3
11
  ## 5.1.0
4
12
 
5
13
  ### Minor Changes
package/README.md CHANGED
@@ -19,13 +19,10 @@ const player = createVideoPlayer({
19
19
  videoId: "12345",
20
20
  mount: document.querySelector("video")!,
21
21
  auth: {
22
- getToken: async (ctx) => {
23
- // Call YOUR backend — never put clientSecret here in browser code
24
- return fetch("/api/auth-token", {
25
- method: "POST",
26
- body: JSON.stringify(ctx),
27
- }).then((r) => r.json());
28
- },
22
+ getToken: async () => ({
23
+ accessToken: "bff-managed",
24
+ expiresAt: Date.now() + 60 * 60 * 1000,
25
+ }),
29
26
  },
30
27
  playback: {
31
28
  getSource: async ({ videoId }) =>
@@ -34,6 +31,12 @@ const player = createVideoPlayer({
34
31
  fetch(`/api/videos/${videoId}/proxy-refresh`, { method: "POST" }).then((r) =>
35
32
  r.json(),
36
33
  ),
34
+ // Default is 45_000. The loader keeps its buffer and uses the new token
35
+ // on the next request. Pass 0 to refresh only as the credential expires.
36
+ },
37
+ streaming: {
38
+ mediaProxyUrlBuilder: ({ url }) =>
39
+ `${location.origin}/api/media?url=${encodeURIComponent(url)}`,
37
40
  },
38
41
  });
39
42
 
@@ -41,7 +44,7 @@ player.on("ready", () => console.log("ready"));
41
44
  await player.play();
42
45
  ```
43
46
 
44
- See repository `docs/` for configuration, authentication, and security.
47
+ Core **5.2.0** keeps a 30-second forward buffer and refreshes the playback token every 45 seconds. That refresh does not restart the loader. `streaming.mediaProxyUrlBuilder` arrived in 5.1.0, which is already on npm. See repository `docs/` for configuration, authentication, and security.
45
48
 
46
49
  ## Samples
47
50
 
package/dist/index.cjs CHANGED
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  AuthParamsStore: () => AuthParamsStore,
35
35
  DEFAULT_CONTROLS: () => DEFAULT_CONTROLS,
36
36
  DEFAULT_LIVE_EDGE_TOLERANCE_SECONDS: () => DEFAULT_LIVE_EDGE_TOLERANCE_SECONDS,
37
+ DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS: () => DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS,
37
38
  DEFAULT_PLAYER_BEHAVIOR: () => DEFAULT_PLAYER_BEHAVIOR,
38
39
  DEFAULT_PLAYER_CONFIG: () => DEFAULT_PLAYER_CONFIG,
39
40
  DEFAULT_THEME: () => DEFAULT_THEME,
@@ -215,7 +216,8 @@ var AuthManager = class {
215
216
 
216
217
  // src/config/defaults.ts
217
218
  var PLAYBACK_RATES = [0.5, 0.75, 1, 1.25, 1.5, 2];
218
- var PACKAGE_VERSION = "3.0.3";
219
+ var DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS = 45e3;
220
+ var PACKAGE_VERSION = "5.2.0";
219
221
  var DEFAULT_PLAYER_BEHAVIOR = {
220
222
  autoplay: true,
221
223
  startMuted: true,
@@ -287,7 +289,12 @@ var DEFAULT_PLAYER_CONFIG = {
287
289
  enableWorker: true,
288
290
  // VOD + AES-128: LL mode causes ABR thrash → bufferAppendError on MSE.
289
291
  lowLatencyMode: false,
290
- backBufferLength: 90,
292
+ // Match a smooth encrypted VOD: 30s forward target, 60s ceiling,
293
+ // 20MB cap, and a short back buffer so MSE does not stall when full.
294
+ backBufferLength: 30,
295
+ maxBufferLength: 30,
296
+ maxMaxBufferLength: 60,
297
+ maxBufferSize: 20 * 1024 * 1024,
291
298
  fatalRecoveryLimit: 3
292
299
  }
293
300
  },
@@ -705,6 +712,7 @@ var RefreshManager = class {
705
712
  constructor(options) {
706
713
  this.options = options;
707
714
  this.timer = null;
715
+ this.intervalTimer = null;
708
716
  this.coalesceTimer = null;
709
717
  this.inFlight = false;
710
718
  this.consecutiveFailures = 0;
@@ -713,6 +721,7 @@ var RefreshManager = class {
713
721
  }
714
722
  start(initialExpiresAt) {
715
723
  this.schedule(initialExpiresAt);
724
+ this.armInterval();
716
725
  }
717
726
  requestRenew(reason = "frag_error") {
718
727
  if (this.stopped || this.options.isDestroyed()) return;
@@ -728,8 +737,10 @@ var RefreshManager = class {
728
737
  stop() {
729
738
  this.stopped = true;
730
739
  if (this.timer) clearTimeout(this.timer);
740
+ if (this.intervalTimer) clearTimeout(this.intervalTimer);
731
741
  if (this.coalesceTimer) clearTimeout(this.coalesceTimer);
732
742
  this.timer = null;
743
+ this.intervalTimer = null;
733
744
  this.coalesceTimer = null;
734
745
  }
735
746
  schedule(expiresAt) {
@@ -743,13 +754,25 @@ var RefreshManager = class {
743
754
  void this.renew("proactive");
744
755
  }, delay);
745
756
  }
757
+ /** Rotate the token on a fixed cadence. The loader reads the store live. */
758
+ armInterval() {
759
+ const interval = this.options.refreshIntervalMs;
760
+ if (!interval || interval < 5e3 || this.stopped) return;
761
+ if (this.intervalTimer) clearTimeout(this.intervalTimer);
762
+ this.intervalTimer = setTimeout(() => {
763
+ this.intervalTimer = null;
764
+ if (this.stopped) return;
765
+ void this.renew("proactive");
766
+ this.armInterval();
767
+ }, interval);
768
+ }
746
769
  async renew(reason) {
747
770
  if (this.stopped || this.options.isDestroyed() || this.inFlight) {
748
771
  if (this.inFlight) this.pendingReason = reason;
749
772
  return;
750
773
  }
751
774
  this.inFlight = true;
752
- this.options.onRenewing?.();
775
+ this.options.onRenewing?.(reason);
753
776
  try {
754
777
  await this.options.authManager.ensureToken({
755
778
  reason: reason === "proactive" ? "expiring" : "unauthorized",
@@ -760,7 +783,7 @@ var RefreshManager = class {
760
783
  reason: reason === "proactive" ? "expiring" : reason === "manual" ? "manual" : "auth_error"
761
784
  });
762
785
  this.consecutiveFailures = 0;
763
- this.options.onRenewed?.(source.expiresAt);
786
+ this.options.onRenewed?.(source.expiresAt, reason);
764
787
  this.schedule(source.expiresAt);
765
788
  this.options.logger.debug("Playback source renewed", { reason });
766
789
  } catch (error) {
@@ -1426,20 +1449,20 @@ var HlsEngine = class {
1426
1449
  "Disabling hls.js worker on Firefox to avoid MSE bufferAppendError"
1427
1450
  );
1428
1451
  }
1452
+ const startPosition = this.options.startPosition;
1429
1453
  this.hls = new Hls({
1430
1454
  enableWorker,
1431
1455
  // LL mode on short encrypted VOD drives aggressive ABR thrash → MSE errors.
1432
1456
  lowLatencyMode: hlsConfig.lowLatencyMode,
1433
1457
  backBufferLength: hlsConfig.backBufferLength,
1434
- // Stabilize ABR: avoid first-frag-only bandwidth test that immediately
1435
- // hops levels on single-segment Appextech VODs (HAR showed truncated
1436
- // 720p body vs Content-Length during level switches).
1458
+ maxBufferLength: hlsConfig.maxBufferLength,
1459
+ maxMaxBufferLength: hlsConfig.maxMaxBufferLength,
1460
+ maxBufferSize: hlsConfig.maxBufferSize,
1461
+ startPosition: typeof startPosition === "number" && startPosition > 0 ? startPosition : -1,
1462
+ // A first-fragment bandwidth probe on short encrypted VODs truncates
1463
+ // the body and immediately hops levels. ABR itself stays on hls.js
1464
+ // defaults so a long VOD is not held on the lowest rung.
1437
1465
  testBandwidth: false,
1438
- abrEwmaDefaultEstimate: 5e5,
1439
- abrBandWidthFactor: 0.8,
1440
- abrBandWidthUpFactor: 0.5,
1441
- maxStarvationDelay: 4,
1442
- capLevelToPlayerSize: true,
1443
1466
  startLevel: -1,
1444
1467
  loader
1445
1468
  });
@@ -1547,35 +1570,35 @@ var HlsEngine = class {
1547
1570
  }
1548
1571
  }
1549
1572
  clampStartPosition(raw, videoEl) {
1550
- let t = Math.max(0, raw);
1573
+ const t = Math.max(0, raw);
1551
1574
  const duration = videoEl.duration;
1552
1575
  if (Number.isFinite(duration) && duration > 0) {
1553
- t = Math.min(t, Math.max(0, duration - 0.35));
1554
- }
1555
- if (videoEl.seekable.length > 0) {
1556
- const end = videoEl.seekable.end(videoEl.seekable.length - 1);
1557
- if (Number.isFinite(end)) {
1558
- t = Math.min(t, end);
1559
- }
1576
+ return Math.min(t, Math.max(0, duration - 0.35));
1560
1577
  }
1561
1578
  return t;
1562
1579
  }
1563
1580
  async applyStartPositionOnly() {
1564
1581
  if (this.destroyed || this.startPositionApplied) return;
1565
1582
  const { videoEl, startPosition } = this.options;
1583
+ if (typeof startPosition !== "number" || startPosition <= 0) {
1584
+ this.startPositionApplied = true;
1585
+ return;
1586
+ }
1587
+ const durationKnown = Number.isFinite(videoEl.duration) && videoEl.duration > 0;
1588
+ if (!durationKnown) {
1589
+ videoEl.addEventListener(
1590
+ "loadedmetadata",
1591
+ () => {
1592
+ void this.applyStartPositionOnly();
1593
+ },
1594
+ { once: true }
1595
+ );
1596
+ return;
1597
+ }
1566
1598
  this.startPositionApplied = true;
1567
- if (typeof startPosition !== "number" || startPosition <= 0) return;
1568
- const seek = () => {
1569
- if (this.destroyed) return;
1570
- try {
1571
- videoEl.currentTime = this.clampStartPosition(startPosition, videoEl);
1572
- } catch {
1573
- }
1574
- };
1575
- if (videoEl.seekable.length > 0 || Number.isFinite(videoEl.duration)) {
1576
- seek();
1577
- } else {
1578
- videoEl.addEventListener("loadedmetadata", seek, { once: true });
1599
+ try {
1600
+ videoEl.currentTime = this.clampStartPosition(startPosition, videoEl);
1601
+ } catch {
1579
1602
  }
1580
1603
  }
1581
1604
  async applyAutoplayOnly() {
@@ -1636,9 +1659,14 @@ var HlsEngine = class {
1636
1659
  keyProxyMaxFailures: this.options.keyProxyMaxFailures
1637
1660
  });
1638
1661
  }
1639
- resumeAfterAuthRenew() {
1662
+ /**
1663
+ * Auth params are already on the shared store, so the next request uses
1664
+ * the new token. Restart the loader only after a real auth failure.
1665
+ * A proactive refresh must not abort the buffered segments.
1666
+ */
1667
+ resumeAfterAuthRenew(restart = true) {
1640
1668
  this.networkRecoveryCount = 0;
1641
- this.hls?.startLoad();
1669
+ if (restart) this.hls?.startLoad();
1642
1670
  this.updateAuthParams();
1643
1671
  }
1644
1672
  setQuality(selection) {
@@ -2024,12 +2052,15 @@ function createVideoPlayer(options) {
2024
2052
  renewMaxRetries: options.playback.renewMaxRetries ?? 3,
2025
2053
  renewBackoffMs: options.playback.renewBackoffMs ?? 2e3,
2026
2054
  coalesceMs: options.playback.authRecoveryCoalesceMs ?? 1e3,
2055
+ refreshIntervalMs: options.playback.refreshIntervalMs ?? DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS,
2027
2056
  logger,
2028
2057
  isDestroyed: () => destroyed,
2029
- onRenewing: () => setState("renewing"),
2030
- onRenewed: (nextExpiresAt) => {
2031
- engine?.resumeAfterAuthRenew();
2032
- if (!destroyed) {
2058
+ onRenewing: (reason) => {
2059
+ if (reason !== "proactive") setState("renewing");
2060
+ },
2061
+ onRenewed: (nextExpiresAt, reason) => {
2062
+ engine?.resumeAfterAuthRenew(reason !== "proactive");
2063
+ if (reason !== "proactive" && !destroyed) {
2033
2064
  const el2 = videoEl;
2034
2065
  if (el2 && !el2.paused && !el2.ended) {
2035
2066
  setState("playing");
@@ -2089,8 +2120,13 @@ function createVideoPlayer(options) {
2089
2120
  },
2090
2121
  seek(time) {
2091
2122
  const el = ensureVideoElement();
2092
- const duration = Number.isFinite(el.duration) ? el.duration : time;
2093
- el.currentTime = Math.min(Math.max(0, time), duration);
2123
+ const requested = Math.max(0, time);
2124
+ const duration = el.duration;
2125
+ if (Number.isFinite(duration) && duration > 0) {
2126
+ el.currentTime = Math.min(requested, Math.max(0, duration - 0.05));
2127
+ } else {
2128
+ el.currentTime = requested;
2129
+ }
2094
2130
  },
2095
2131
  goToLive() {
2096
2132
  const el = ensureVideoElement();
@@ -2292,6 +2328,7 @@ function createVideoPlayer(options) {
2292
2328
  AuthParamsStore,
2293
2329
  DEFAULT_CONTROLS,
2294
2330
  DEFAULT_LIVE_EDGE_TOLERANCE_SECONDS,
2331
+ DEFAULT_PLAYBACK_REFRESH_INTERVAL_MS,
2295
2332
  DEFAULT_PLAYER_BEHAVIOR,
2296
2333
  DEFAULT_PLAYER_CONFIG,
2297
2334
  DEFAULT_THEME,