@livo-tv/blocks 1.10.0 → 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
  }
@@ -3885,11 +3886,20 @@ function parseCaptionVtt(text) {
3885
3886
  function captionAtTime(cues, time, options) {
3886
3887
  const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3887
3888
  if (matched) return matched;
3888
- if (options?.live && cues.length > 0) {
3889
- return cues[cues.length - 1] ?? null;
3890
- }
3891
3889
  return null;
3892
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;
3902
+ }
3893
3903
  async function loadCaptionVtt(url) {
3894
3904
  try {
3895
3905
  const response = await fetch(url, { cache: "no-store" });
@@ -3899,6 +3909,34 @@ async function loadCaptionVtt(url) {
3899
3909
  return [];
3900
3910
  }
3901
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
+ }
3902
3940
  var DEFAULT_LABELS3 = {
3903
3941
  play: "Play",
3904
3942
  pause: "Pause",
@@ -4036,6 +4074,8 @@ function VideoPlayer({
4036
4074
  const [openMenu, setOpenMenu] = React8.useState(null);
4037
4075
  const [captionLocale, setCaptionLocale] = React8.useState(defaultCaptionLocale ?? "off");
4038
4076
  const [captionCues, setCaptionCues] = React8.useState([]);
4077
+ const [liveCaptionCues, setLiveCaptionCues] = React8.useState([]);
4078
+ const [liveLagMs, setLiveLagMs] = React8.useState(null);
4039
4079
  const [behindLive, setBehindLive] = React8.useState(false);
4040
4080
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8.useState(false);
4041
4081
  const hideTimerRef = React8.useRef(null);
@@ -4051,7 +4091,9 @@ function VideoPlayer({
4051
4091
  }
4052
4092
  if (mode === "live" && video.seekable.length > 0) {
4053
4093
  const edge = video.seekable.end(video.seekable.length - 1);
4054
- 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);
4055
4097
  }
4056
4098
  };
4057
4099
  const onDuration = () => setDuration(video.duration);
@@ -4073,7 +4115,7 @@ function VideoPlayer({
4073
4115
  video.removeEventListener("volumechange", onVolume);
4074
4116
  video.removeEventListener("ratechange", onRate);
4075
4117
  };
4076
- }, [videoRef, active, mode]);
4118
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4077
4119
  React8.useEffect(() => {
4078
4120
  const video = videoRef.current;
4079
4121
  if (!paused || !video || !isPlaying) return;
@@ -4131,12 +4173,20 @@ function VideoPlayer({
4131
4173
  React8.useEffect(() => {
4132
4174
  if (captionLocale === "off") {
4133
4175
  setCaptionCues([]);
4176
+ setLiveCaptionCues([]);
4134
4177
  return;
4135
4178
  }
4136
4179
  let cancelled = false;
4137
4180
  const load = async () => {
4138
4181
  const liveBase = liveCaptionsBaseUrl?.trim();
4139
- 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);
4140
4190
  if (!url) {
4141
4191
  if (!cancelled) setCaptionCues([]);
4142
4192
  return;
@@ -4152,9 +4202,7 @@ function VideoPlayer({
4152
4202
  window.clearInterval(timer);
4153
4203
  };
4154
4204
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4155
- const activeCaption = captionAtTime(captionCues, currentTime, {
4156
- live: Boolean(liveCaptionsBaseUrl)
4157
- });
4205
+ const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4158
4206
  function togglePlay() {
4159
4207
  const video = videoRef.current;
4160
4208
  if (!video) return;