@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.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
  }
@@ -3911,11 +3912,20 @@ function parseCaptionVtt(text) {
3911
3912
  function captionAtTime(cues, time, options) {
3912
3913
  const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3913
3914
  if (matched) return matched;
3914
- if (options?.live && cues.length > 0) {
3915
- return cues[cues.length - 1] ?? null;
3916
- }
3917
3915
  return null;
3918
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;
3928
+ }
3919
3929
  async function loadCaptionVtt(url) {
3920
3930
  try {
3921
3931
  const response = await fetch(url, { cache: "no-store" });
@@ -3925,6 +3935,34 @@ async function loadCaptionVtt(url) {
3925
3935
  return [];
3926
3936
  }
3927
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
+ }
3928
3966
  var DEFAULT_LABELS3 = {
3929
3967
  play: "Play",
3930
3968
  pause: "Pause",
@@ -4062,6 +4100,8 @@ function VideoPlayer({
4062
4100
  const [openMenu, setOpenMenu] = React8__namespace.useState(null);
4063
4101
  const [captionLocale, setCaptionLocale] = React8__namespace.useState(defaultCaptionLocale ?? "off");
4064
4102
  const [captionCues, setCaptionCues] = React8__namespace.useState([]);
4103
+ const [liveCaptionCues, setLiveCaptionCues] = React8__namespace.useState([]);
4104
+ const [liveLagMs, setLiveLagMs] = React8__namespace.useState(null);
4065
4105
  const [behindLive, setBehindLive] = React8__namespace.useState(false);
4066
4106
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8__namespace.useState(false);
4067
4107
  const hideTimerRef = React8__namespace.useRef(null);
@@ -4077,7 +4117,9 @@ function VideoPlayer({
4077
4117
  }
4078
4118
  if (mode === "live" && video.seekable.length > 0) {
4079
4119
  const edge = video.seekable.end(video.seekable.length - 1);
4080
- 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);
4081
4123
  }
4082
4124
  };
4083
4125
  const onDuration = () => setDuration(video.duration);
@@ -4099,7 +4141,7 @@ function VideoPlayer({
4099
4141
  video.removeEventListener("volumechange", onVolume);
4100
4142
  video.removeEventListener("ratechange", onRate);
4101
4143
  };
4102
- }, [videoRef, active, mode]);
4144
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4103
4145
  React8__namespace.useEffect(() => {
4104
4146
  const video = videoRef.current;
4105
4147
  if (!paused || !video || !isPlaying) return;
@@ -4157,12 +4199,20 @@ function VideoPlayer({
4157
4199
  React8__namespace.useEffect(() => {
4158
4200
  if (captionLocale === "off") {
4159
4201
  setCaptionCues([]);
4202
+ setLiveCaptionCues([]);
4160
4203
  return;
4161
4204
  }
4162
4205
  let cancelled = false;
4163
4206
  const load = async () => {
4164
4207
  const liveBase = liveCaptionsBaseUrl?.trim();
4165
- 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);
4166
4216
  if (!url) {
4167
4217
  if (!cancelled) setCaptionCues([]);
4168
4218
  return;
@@ -4178,9 +4228,7 @@ function VideoPlayer({
4178
4228
  window.clearInterval(timer);
4179
4229
  };
4180
4230
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4181
- const activeCaption = captionAtTime(captionCues, currentTime, {
4182
- live: Boolean(liveCaptionsBaseUrl)
4183
- });
4231
+ const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4184
4232
  function togglePlay() {
4185
4233
  const video = videoRef.current;
4186
4234
  if (!video) return;