@livo-tv/blocks 1.9.1 → 1.11.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/dist/index.cjs CHANGED
@@ -3870,6 +3870,7 @@ function useHlsPlayback({
3870
3870
 
3871
3871
  // src/components/video/captions.ts
3872
3872
  var CAPTION_LOCALES = ["en", "es", "pt"];
3873
+ var LIVE_CUE_HOLD_MS = 4e3;
3873
3874
  function isCaptionLocale(value) {
3874
3875
  return CAPTION_LOCALES.includes(value);
3875
3876
  }
@@ -3908,8 +3909,22 @@ function parseCaptionVtt(text) {
3908
3909
  }
3909
3910
  return cues;
3910
3911
  }
3911
- function captionAtTime(cues, time) {
3912
- return cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3912
+ function captionAtTime(cues, time, options) {
3913
+ const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3914
+ if (matched) return matched;
3915
+ return null;
3916
+ }
3917
+ function captionAtLiveLag(cues, now, lagMs, holdMs = LIVE_CUE_HOLD_MS) {
3918
+ if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3919
+ let shown = null;
3920
+ for (const cue of cues) {
3921
+ if (!Number.isFinite(cue.receivedAt)) continue;
3922
+ if (now - cue.receivedAt < lagMs) continue;
3923
+ if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3924
+ }
3925
+ if (!shown) return null;
3926
+ if (now >= shown.receivedAt + lagMs + holdMs) return null;
3927
+ return shown;
3913
3928
  }
3914
3929
  async function loadCaptionVtt(url) {
3915
3930
  try {
@@ -3920,6 +3935,34 @@ async function loadCaptionVtt(url) {
3920
3935
  return [];
3921
3936
  }
3922
3937
  }
3938
+ async function loadLiveCaptions(url) {
3939
+ try {
3940
+ const response = await fetch(url, {
3941
+ cache: "no-store",
3942
+ headers: { Accept: "application/json" }
3943
+ });
3944
+ if (!response.ok) return [];
3945
+ const body = await response.json();
3946
+ if (!body || typeof body !== "object" || !("cues" in body)) {
3947
+ return [];
3948
+ }
3949
+ const rows = body.cues;
3950
+ if (!Array.isArray(rows)) return [];
3951
+ const cues = [];
3952
+ for (const row of rows) {
3953
+ if (!row || typeof row !== "object") continue;
3954
+ const record = row;
3955
+ if (typeof record.text !== "string" || !record.text.trim()) continue;
3956
+ if (typeof record.receivedAt !== "number" || !Number.isFinite(record.receivedAt)) {
3957
+ continue;
3958
+ }
3959
+ cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3960
+ }
3961
+ return cues;
3962
+ } catch {
3963
+ return [];
3964
+ }
3965
+ }
3923
3966
  var DEFAULT_LABELS3 = {
3924
3967
  play: "Play",
3925
3968
  pause: "Pause",
@@ -4057,6 +4100,8 @@ function VideoPlayer({
4057
4100
  const [openMenu, setOpenMenu] = React8__namespace.useState(null);
4058
4101
  const [captionLocale, setCaptionLocale] = React8__namespace.useState(defaultCaptionLocale ?? "off");
4059
4102
  const [captionCues, setCaptionCues] = React8__namespace.useState([]);
4103
+ const [liveCaptionCues, setLiveCaptionCues] = React8__namespace.useState([]);
4104
+ const [liveLagMs, setLiveLagMs] = React8__namespace.useState(null);
4060
4105
  const [behindLive, setBehindLive] = React8__namespace.useState(false);
4061
4106
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8__namespace.useState(false);
4062
4107
  const hideTimerRef = React8__namespace.useRef(null);
@@ -4072,7 +4117,9 @@ function VideoPlayer({
4072
4117
  }
4073
4118
  if (mode === "live" && video.seekable.length > 0) {
4074
4119
  const edge = video.seekable.end(video.seekable.length - 1);
4075
- setBehindLive(edge - video.currentTime > LIVE_EDGE_THRESHOLD_S);
4120
+ const lagSeconds = edge - video.currentTime;
4121
+ setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4122
+ if (liveCaptionsBaseUrl) setLiveLagMs(lagSeconds * 1e3);
4076
4123
  }
4077
4124
  };
4078
4125
  const onDuration = () => setDuration(video.duration);
@@ -4094,7 +4141,7 @@ function VideoPlayer({
4094
4141
  video.removeEventListener("volumechange", onVolume);
4095
4142
  video.removeEventListener("ratechange", onRate);
4096
4143
  };
4097
- }, [videoRef, active, mode]);
4144
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4098
4145
  React8__namespace.useEffect(() => {
4099
4146
  const video = videoRef.current;
4100
4147
  if (!paused || !video || !isPlaying) return;
@@ -4152,12 +4199,20 @@ function VideoPlayer({
4152
4199
  React8__namespace.useEffect(() => {
4153
4200
  if (captionLocale === "off") {
4154
4201
  setCaptionCues([]);
4202
+ setLiveCaptionCues([]);
4155
4203
  return;
4156
4204
  }
4157
4205
  let cancelled = false;
4158
4206
  const load = async () => {
4159
4207
  const liveBase = liveCaptionsBaseUrl?.trim();
4160
- const url = liveBase ? liveCaptionsUrl(liveBase, captionLocale) : captionsVttUrlFromPlayback(src, captionLocale);
4208
+ if (liveBase) {
4209
+ const cues2 = await loadLiveCaptions(
4210
+ liveCaptionsUrl(liveBase, captionLocale)
4211
+ );
4212
+ if (!cancelled) setLiveCaptionCues(cues2);
4213
+ return;
4214
+ }
4215
+ const url = captionsVttUrlFromPlayback(src, captionLocale);
4161
4216
  if (!url) {
4162
4217
  if (!cancelled) setCaptionCues([]);
4163
4218
  return;
@@ -4173,7 +4228,7 @@ function VideoPlayer({
4173
4228
  window.clearInterval(timer);
4174
4229
  };
4175
4230
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4176
- const activeCaption = captionAtTime(captionCues, currentTime);
4231
+ const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4177
4232
  function togglePlay() {
4178
4233
  const video = videoRef.current;
4179
4234
  if (!video) return;