@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.js CHANGED
@@ -3844,6 +3844,7 @@ function useHlsPlayback({
3844
3844
 
3845
3845
  // src/components/video/captions.ts
3846
3846
  var CAPTION_LOCALES = ["en", "es", "pt"];
3847
+ var LIVE_CUE_HOLD_MS = 4e3;
3847
3848
  function isCaptionLocale(value) {
3848
3849
  return CAPTION_LOCALES.includes(value);
3849
3850
  }
@@ -3882,8 +3883,22 @@ function parseCaptionVtt(text) {
3882
3883
  }
3883
3884
  return cues;
3884
3885
  }
3885
- function captionAtTime(cues, time) {
3886
- return cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3886
+ function captionAtTime(cues, time, options) {
3887
+ const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3888
+ if (matched) return matched;
3889
+ return null;
3890
+ }
3891
+ function captionAtLiveLag(cues, now, lagMs, holdMs = LIVE_CUE_HOLD_MS) {
3892
+ if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3893
+ let shown = null;
3894
+ for (const cue of cues) {
3895
+ if (!Number.isFinite(cue.receivedAt)) continue;
3896
+ if (now - cue.receivedAt < lagMs) continue;
3897
+ if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3898
+ }
3899
+ if (!shown) return null;
3900
+ if (now >= shown.receivedAt + lagMs + holdMs) return null;
3901
+ return shown;
3887
3902
  }
3888
3903
  async function loadCaptionVtt(url) {
3889
3904
  try {
@@ -3894,6 +3909,34 @@ async function loadCaptionVtt(url) {
3894
3909
  return [];
3895
3910
  }
3896
3911
  }
3912
+ async function loadLiveCaptions(url) {
3913
+ try {
3914
+ const response = await fetch(url, {
3915
+ cache: "no-store",
3916
+ headers: { Accept: "application/json" }
3917
+ });
3918
+ if (!response.ok) return [];
3919
+ const body = await response.json();
3920
+ if (!body || typeof body !== "object" || !("cues" in body)) {
3921
+ return [];
3922
+ }
3923
+ const rows = body.cues;
3924
+ if (!Array.isArray(rows)) return [];
3925
+ const cues = [];
3926
+ for (const row of rows) {
3927
+ if (!row || typeof row !== "object") continue;
3928
+ const record = row;
3929
+ if (typeof record.text !== "string" || !record.text.trim()) continue;
3930
+ if (typeof record.receivedAt !== "number" || !Number.isFinite(record.receivedAt)) {
3931
+ continue;
3932
+ }
3933
+ cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3934
+ }
3935
+ return cues;
3936
+ } catch {
3937
+ return [];
3938
+ }
3939
+ }
3897
3940
  var DEFAULT_LABELS3 = {
3898
3941
  play: "Play",
3899
3942
  pause: "Pause",
@@ -4031,6 +4074,8 @@ function VideoPlayer({
4031
4074
  const [openMenu, setOpenMenu] = React8.useState(null);
4032
4075
  const [captionLocale, setCaptionLocale] = React8.useState(defaultCaptionLocale ?? "off");
4033
4076
  const [captionCues, setCaptionCues] = React8.useState([]);
4077
+ const [liveCaptionCues, setLiveCaptionCues] = React8.useState([]);
4078
+ const [liveLagMs, setLiveLagMs] = React8.useState(null);
4034
4079
  const [behindLive, setBehindLive] = React8.useState(false);
4035
4080
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8.useState(false);
4036
4081
  const hideTimerRef = React8.useRef(null);
@@ -4046,7 +4091,9 @@ function VideoPlayer({
4046
4091
  }
4047
4092
  if (mode === "live" && video.seekable.length > 0) {
4048
4093
  const edge = video.seekable.end(video.seekable.length - 1);
4049
- setBehindLive(edge - video.currentTime > LIVE_EDGE_THRESHOLD_S);
4094
+ const lagSeconds = edge - video.currentTime;
4095
+ setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4096
+ if (liveCaptionsBaseUrl) setLiveLagMs(lagSeconds * 1e3);
4050
4097
  }
4051
4098
  };
4052
4099
  const onDuration = () => setDuration(video.duration);
@@ -4068,7 +4115,7 @@ function VideoPlayer({
4068
4115
  video.removeEventListener("volumechange", onVolume);
4069
4116
  video.removeEventListener("ratechange", onRate);
4070
4117
  };
4071
- }, [videoRef, active, mode]);
4118
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4072
4119
  React8.useEffect(() => {
4073
4120
  const video = videoRef.current;
4074
4121
  if (!paused || !video || !isPlaying) return;
@@ -4126,12 +4173,20 @@ function VideoPlayer({
4126
4173
  React8.useEffect(() => {
4127
4174
  if (captionLocale === "off") {
4128
4175
  setCaptionCues([]);
4176
+ setLiveCaptionCues([]);
4129
4177
  return;
4130
4178
  }
4131
4179
  let cancelled = false;
4132
4180
  const load = async () => {
4133
4181
  const liveBase = liveCaptionsBaseUrl?.trim();
4134
- const url = liveBase ? liveCaptionsUrl(liveBase, captionLocale) : captionsVttUrlFromPlayback(src, captionLocale);
4182
+ if (liveBase) {
4183
+ const cues2 = await loadLiveCaptions(
4184
+ liveCaptionsUrl(liveBase, captionLocale)
4185
+ );
4186
+ if (!cancelled) setLiveCaptionCues(cues2);
4187
+ return;
4188
+ }
4189
+ const url = captionsVttUrlFromPlayback(src, captionLocale);
4135
4190
  if (!url) {
4136
4191
  if (!cancelled) setCaptionCues([]);
4137
4192
  return;
@@ -4147,7 +4202,7 @@ function VideoPlayer({
4147
4202
  window.clearInterval(timer);
4148
4203
  };
4149
4204
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4150
- const activeCaption = captionAtTime(captionCues, currentTime);
4205
+ const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4151
4206
  function togglePlay() {
4152
4207
  const video = videoRef.current;
4153
4208
  if (!video) return;