@livo-tv/blocks 1.11.0 → 1.12.1

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
@@ -3592,7 +3592,7 @@ function useHlsPlayback({
3592
3592
  let mediaErrorRetries = 0;
3593
3593
  let playbackSource = "hls";
3594
3594
  const teardown = [];
3595
- media.muted = muted;
3595
+ applyMute(media);
3596
3596
  setState("loading");
3597
3597
  setError(null);
3598
3598
  setAutoplayState("none");
@@ -3623,13 +3623,25 @@ function useHlsPlayback({
3623
3623
  setError(described);
3624
3624
  setState("error");
3625
3625
  }
3626
+ function applyMute(el) {
3627
+ el.muted = muted;
3628
+ }
3629
+ function preserveMute(el, fn) {
3630
+ const wasMuted = el.muted;
3631
+ fn();
3632
+ el.muted = wasMuted;
3633
+ }
3626
3634
  function attemptAutoplay(el) {
3635
+ applyMute(el);
3627
3636
  void el.play().then(
3628
- () => setAutoplayState("playing"),
3637
+ () => {
3638
+ applyMute(el);
3639
+ setAutoplayState("playing");
3640
+ },
3629
3641
  () => {
3630
3642
  el.muted = true;
3631
3643
  void el.play().then(
3632
- () => setAutoplayState("muted"),
3644
+ () => setAutoplayState(muted ? "playing" : "muted"),
3633
3645
  () => setAutoplayState("blocked")
3634
3646
  );
3635
3647
  }
@@ -3716,6 +3728,7 @@ function useHlsPlayback({
3716
3728
  media.removeAttribute("src");
3717
3729
  media.load();
3718
3730
  media.src = src;
3731
+ applyMute(media);
3719
3732
  const onReady = () => {
3720
3733
  manifestParsed = true;
3721
3734
  clearRetryTimer();
@@ -3759,6 +3772,7 @@ function useHlsPlayback({
3759
3772
  hlsRef.current = hls;
3760
3773
  hls.loadSource(src);
3761
3774
  hls.attachMedia(media);
3775
+ applyMute(media);
3762
3776
  hls.on(Hls.Events.MANIFEST_PARSED, (_e, data) => {
3763
3777
  manifestParsed = true;
3764
3778
  clearRetryTimer();
@@ -3797,12 +3811,14 @@ function useHlsPlayback({
3797
3811
  }
3798
3812
  mediaErrorRetries += 1;
3799
3813
  if (mediaErrorRetries === 1) {
3800
- hls.recoverMediaError();
3814
+ preserveMute(media, () => hls.recoverMediaError());
3801
3815
  return;
3802
3816
  }
3803
3817
  if (mediaErrorRetries === 2) {
3804
- hls.swapAudioCodec();
3805
- hls.recoverMediaError();
3818
+ preserveMute(media, () => {
3819
+ hls.swapAudioCodec();
3820
+ hls.recoverMediaError();
3821
+ });
3806
3822
  return;
3807
3823
  }
3808
3824
  if (mediaErrorRetries < MAX_MEDIA_ERROR_RETRIES) {
@@ -3871,6 +3887,13 @@ function useHlsPlayback({
3871
3887
  // src/components/video/captions.ts
3872
3888
  var CAPTION_LOCALES = ["en", "es", "pt"];
3873
3889
  var LIVE_CUE_HOLD_MS = 4e3;
3890
+ var LIVE_LAG_SPIKE_MS = 2500;
3891
+ function stabilizeLiveLagMs(previous, next) {
3892
+ if (previous == null || !Number.isFinite(previous)) return next;
3893
+ if (next <= previous) return next;
3894
+ if (next - previous < LIVE_LAG_SPIKE_MS) return previous;
3895
+ return next;
3896
+ }
3874
3897
  function isCaptionLocale(value) {
3875
3898
  return CAPTION_LOCALES.includes(value);
3876
3899
  }
@@ -3914,18 +3937,27 @@ function captionAtTime(cues, time, options) {
3914
3937
  if (matched) return matched;
3915
3938
  return null;
3916
3939
  }
3917
- function captionAtLiveLag(cues, now, lagMs, holdMs = LIVE_CUE_HOLD_MS) {
3918
- if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3940
+ function newestDueCue(cues, now, lagMs) {
3919
3941
  let shown = null;
3920
3942
  for (const cue of cues) {
3921
3943
  if (!Number.isFinite(cue.receivedAt)) continue;
3922
3944
  if (now - cue.receivedAt < lagMs) continue;
3923
3945
  if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3924
3946
  }
3925
- if (!shown) return null;
3926
- if (now >= shown.receivedAt + lagMs + holdMs) return null;
3927
3947
  return shown;
3928
3948
  }
3949
+ function captionAtLiveLag(cues, now, lagMs, previous = null, holdMs = LIVE_CUE_HOLD_MS) {
3950
+ if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3951
+ const due = newestDueCue(cues, now, lagMs);
3952
+ const dueInHold = due && now < due.receivedAt + lagMs + holdMs ? due : null;
3953
+ if (dueInHold && (!previous || dueInHold.receivedAt > previous.cue.receivedAt)) {
3954
+ return { cue: dueInHold, shownAt: now, lagMs };
3955
+ }
3956
+ if (previous && now < previous.shownAt + holdMs) {
3957
+ return previous;
3958
+ }
3959
+ return null;
3960
+ }
3929
3961
  async function loadCaptionVtt(url) {
3930
3962
  try {
3931
3963
  const response = await fetch(url, { cache: "no-store" });
@@ -3941,13 +3973,13 @@ async function loadLiveCaptions(url) {
3941
3973
  cache: "no-store",
3942
3974
  headers: { Accept: "application/json" }
3943
3975
  });
3944
- if (!response.ok) return [];
3976
+ if (!response.ok) return { ok: false, cues: [] };
3945
3977
  const body = await response.json();
3946
3978
  if (!body || typeof body !== "object" || !("cues" in body)) {
3947
- return [];
3979
+ return { ok: false, cues: [] };
3948
3980
  }
3949
3981
  const rows = body.cues;
3950
- if (!Array.isArray(rows)) return [];
3982
+ if (!Array.isArray(rows)) return { ok: false, cues: [] };
3951
3983
  const cues = [];
3952
3984
  for (const row of rows) {
3953
3985
  if (!row || typeof row !== "object") continue;
@@ -3958,9 +3990,9 @@ async function loadLiveCaptions(url) {
3958
3990
  }
3959
3991
  cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3960
3992
  }
3961
- return cues;
3993
+ return { ok: true, cues };
3962
3994
  } catch {
3963
- return [];
3995
+ return { ok: false, cues: [] };
3964
3996
  }
3965
3997
  }
3966
3998
  var DEFAULT_LABELS3 = {
@@ -4089,6 +4121,8 @@ function VideoPlayer({
4089
4121
  });
4090
4122
  const [isPlaying, setIsPlaying] = React8__namespace.useState(false);
4091
4123
  const [isMuted, setIsMuted] = React8__namespace.useState(muted);
4124
+ const isMutedRef = React8__namespace.useRef(isMuted);
4125
+ isMutedRef.current = isMuted;
4092
4126
  const [volume, setVolume] = React8__namespace.useState(1);
4093
4127
  const [currentTime, setCurrentTime] = React8__namespace.useState(0);
4094
4128
  const [duration, setDuration] = React8__namespace.useState(0);
@@ -4102,6 +4136,7 @@ function VideoPlayer({
4102
4136
  const [captionCues, setCaptionCues] = React8__namespace.useState([]);
4103
4137
  const [liveCaptionCues, setLiveCaptionCues] = React8__namespace.useState([]);
4104
4138
  const [liveLagMs, setLiveLagMs] = React8__namespace.useState(null);
4139
+ const liveCaptionLatchRef = React8__namespace.useRef(null);
4105
4140
  const [behindLive, setBehindLive] = React8__namespace.useState(false);
4106
4141
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8__namespace.useState(false);
4107
4142
  const hideTimerRef = React8__namespace.useRef(null);
@@ -4119,7 +4154,9 @@ function VideoPlayer({
4119
4154
  const edge = video.seekable.end(video.seekable.length - 1);
4120
4155
  const lagSeconds = edge - video.currentTime;
4121
4156
  setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4122
- if (liveCaptionsBaseUrl) setLiveLagMs(lagSeconds * 1e3);
4157
+ if (liveCaptionsBaseUrl) {
4158
+ setLiveLagMs((prev) => stabilizeLiveLagMs(prev, lagSeconds * 1e3));
4159
+ }
4123
4160
  }
4124
4161
  };
4125
4162
  const onDuration = () => setDuration(video.duration);
@@ -4128,11 +4165,15 @@ function VideoPlayer({
4128
4165
  setVolume(video.volume);
4129
4166
  };
4130
4167
  const onRate = () => setPlaybackRate(video.playbackRate);
4168
+ const onPlaying = () => {
4169
+ if (isMutedRef.current && !video.muted) video.muted = true;
4170
+ };
4131
4171
  video.addEventListener("timeupdate", onTimeUpdate);
4132
4172
  video.addEventListener("durationchange", onDuration);
4133
4173
  video.addEventListener("loadedmetadata", onDuration);
4134
4174
  video.addEventListener("volumechange", onVolume);
4135
4175
  video.addEventListener("ratechange", onRate);
4176
+ video.addEventListener("playing", onPlaying);
4136
4177
  onVolume();
4137
4178
  return () => {
4138
4179
  video.removeEventListener("timeupdate", onTimeUpdate);
@@ -4140,8 +4181,13 @@ function VideoPlayer({
4140
4181
  video.removeEventListener("loadedmetadata", onDuration);
4141
4182
  video.removeEventListener("volumechange", onVolume);
4142
4183
  video.removeEventListener("ratechange", onRate);
4184
+ video.removeEventListener("playing", onPlaying);
4143
4185
  };
4144
4186
  }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4187
+ React8__namespace.useLayoutEffect(() => {
4188
+ const video = videoRef.current;
4189
+ if (video) video.muted = isMuted;
4190
+ }, [isMuted, active, videoRef]);
4145
4191
  React8__namespace.useEffect(() => {
4146
4192
  const video = videoRef.current;
4147
4193
  if (!paused || !video || !isPlaying) return;
@@ -4200,16 +4246,17 @@ function VideoPlayer({
4200
4246
  if (captionLocale === "off") {
4201
4247
  setCaptionCues([]);
4202
4248
  setLiveCaptionCues([]);
4249
+ liveCaptionLatchRef.current = null;
4203
4250
  return;
4204
4251
  }
4205
4252
  let cancelled = false;
4206
4253
  const load = async () => {
4207
4254
  const liveBase = liveCaptionsBaseUrl?.trim();
4208
4255
  if (liveBase) {
4209
- const cues2 = await loadLiveCaptions(
4256
+ const loaded = await loadLiveCaptions(
4210
4257
  liveCaptionsUrl(liveBase, captionLocale)
4211
4258
  );
4212
- if (!cancelled) setLiveCaptionCues(cues2);
4259
+ if (!cancelled && loaded.ok) setLiveCaptionCues(loaded.cues);
4213
4260
  return;
4214
4261
  }
4215
4262
  const url = captionsVttUrlFromPlayback(src, captionLocale);
@@ -4228,7 +4275,20 @@ function VideoPlayer({
4228
4275
  window.clearInterval(timer);
4229
4276
  };
4230
4277
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4231
- const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4278
+ const liveCaptionKey = `${captionLocale}:${liveCaptionsBaseUrl ?? ""}`;
4279
+ const liveCaptionKeyRef = React8__namespace.useRef(liveCaptionKey);
4280
+ if (liveCaptionKeyRef.current !== liveCaptionKey) {
4281
+ liveCaptionKeyRef.current = liveCaptionKey;
4282
+ liveCaptionLatchRef.current = null;
4283
+ }
4284
+ const liveLatch = liveCaptionsBaseUrl ? captionAtLiveLag(
4285
+ liveCaptionCues,
4286
+ Date.now(),
4287
+ liveLagMs,
4288
+ liveCaptionLatchRef.current
4289
+ ) : null;
4290
+ liveCaptionLatchRef.current = liveLatch;
4291
+ const activeCaption = liveCaptionsBaseUrl ? liveLatch?.cue ?? null : captionAtTime(captionCues, currentTime);
4232
4292
  function togglePlay() {
4233
4293
  const video = videoRef.current;
4234
4294
  if (!video) return;
@@ -4389,6 +4449,7 @@ function VideoPlayer({
4389
4449
  {
4390
4450
  ref: videoRef,
4391
4451
  playsInline: true,
4452
+ muted: isMuted,
4392
4453
  className: "h-full w-full bg-black object-contain",
4393
4454
  onClick: active ? togglePlay : void 0,
4394
4455
  poster
@@ -4462,7 +4523,7 @@ function VideoPlayer({
4462
4523
  {
4463
4524
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",
4464
4525
  "data-slot": "video-captions",
4465
- children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "max-w-[90%] rounded-sm bg-black/70 px-3 py-1.5 text-center text-sm font-medium text-white shadow-sm", children: activeCaption.text })
4526
+ children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "line-clamp-2 max-w-[90%] break-words rounded-sm bg-black/70 px-3 py-1.5 text-center text-sm font-medium text-white shadow-sm", children: activeCaption.text })
4466
4527
  }
4467
4528
  ),
4468
4529
  active && /* @__PURE__ */ jsxRuntime.jsxs(