@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.js CHANGED
@@ -3566,7 +3566,7 @@ function useHlsPlayback({
3566
3566
  let mediaErrorRetries = 0;
3567
3567
  let playbackSource = "hls";
3568
3568
  const teardown = [];
3569
- media.muted = muted;
3569
+ applyMute(media);
3570
3570
  setState("loading");
3571
3571
  setError(null);
3572
3572
  setAutoplayState("none");
@@ -3597,13 +3597,25 @@ function useHlsPlayback({
3597
3597
  setError(described);
3598
3598
  setState("error");
3599
3599
  }
3600
+ function applyMute(el) {
3601
+ el.muted = muted;
3602
+ }
3603
+ function preserveMute(el, fn) {
3604
+ const wasMuted = el.muted;
3605
+ fn();
3606
+ el.muted = wasMuted;
3607
+ }
3600
3608
  function attemptAutoplay(el) {
3609
+ applyMute(el);
3601
3610
  void el.play().then(
3602
- () => setAutoplayState("playing"),
3611
+ () => {
3612
+ applyMute(el);
3613
+ setAutoplayState("playing");
3614
+ },
3603
3615
  () => {
3604
3616
  el.muted = true;
3605
3617
  void el.play().then(
3606
- () => setAutoplayState("muted"),
3618
+ () => setAutoplayState(muted ? "playing" : "muted"),
3607
3619
  () => setAutoplayState("blocked")
3608
3620
  );
3609
3621
  }
@@ -3690,6 +3702,7 @@ function useHlsPlayback({
3690
3702
  media.removeAttribute("src");
3691
3703
  media.load();
3692
3704
  media.src = src;
3705
+ applyMute(media);
3693
3706
  const onReady = () => {
3694
3707
  manifestParsed = true;
3695
3708
  clearRetryTimer();
@@ -3733,6 +3746,7 @@ function useHlsPlayback({
3733
3746
  hlsRef.current = hls;
3734
3747
  hls.loadSource(src);
3735
3748
  hls.attachMedia(media);
3749
+ applyMute(media);
3736
3750
  hls.on(Hls.Events.MANIFEST_PARSED, (_e, data) => {
3737
3751
  manifestParsed = true;
3738
3752
  clearRetryTimer();
@@ -3771,12 +3785,14 @@ function useHlsPlayback({
3771
3785
  }
3772
3786
  mediaErrorRetries += 1;
3773
3787
  if (mediaErrorRetries === 1) {
3774
- hls.recoverMediaError();
3788
+ preserveMute(media, () => hls.recoverMediaError());
3775
3789
  return;
3776
3790
  }
3777
3791
  if (mediaErrorRetries === 2) {
3778
- hls.swapAudioCodec();
3779
- hls.recoverMediaError();
3792
+ preserveMute(media, () => {
3793
+ hls.swapAudioCodec();
3794
+ hls.recoverMediaError();
3795
+ });
3780
3796
  return;
3781
3797
  }
3782
3798
  if (mediaErrorRetries < MAX_MEDIA_ERROR_RETRIES) {
@@ -3845,6 +3861,13 @@ function useHlsPlayback({
3845
3861
  // src/components/video/captions.ts
3846
3862
  var CAPTION_LOCALES = ["en", "es", "pt"];
3847
3863
  var LIVE_CUE_HOLD_MS = 4e3;
3864
+ var LIVE_LAG_SPIKE_MS = 2500;
3865
+ function stabilizeLiveLagMs(previous, next) {
3866
+ if (previous == null || !Number.isFinite(previous)) return next;
3867
+ if (next <= previous) return next;
3868
+ if (next - previous < LIVE_LAG_SPIKE_MS) return previous;
3869
+ return next;
3870
+ }
3848
3871
  function isCaptionLocale(value) {
3849
3872
  return CAPTION_LOCALES.includes(value);
3850
3873
  }
@@ -3888,18 +3911,27 @@ function captionAtTime(cues, time, options) {
3888
3911
  if (matched) return matched;
3889
3912
  return null;
3890
3913
  }
3891
- function captionAtLiveLag(cues, now, lagMs, holdMs = LIVE_CUE_HOLD_MS) {
3892
- if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3914
+ function newestDueCue(cues, now, lagMs) {
3893
3915
  let shown = null;
3894
3916
  for (const cue of cues) {
3895
3917
  if (!Number.isFinite(cue.receivedAt)) continue;
3896
3918
  if (now - cue.receivedAt < lagMs) continue;
3897
3919
  if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3898
3920
  }
3899
- if (!shown) return null;
3900
- if (now >= shown.receivedAt + lagMs + holdMs) return null;
3901
3921
  return shown;
3902
3922
  }
3923
+ function captionAtLiveLag(cues, now, lagMs, previous = null, holdMs = LIVE_CUE_HOLD_MS) {
3924
+ if (lagMs == null || !Number.isFinite(lagMs) || lagMs < 0) return null;
3925
+ const due = newestDueCue(cues, now, lagMs);
3926
+ const dueInHold = due && now < due.receivedAt + lagMs + holdMs ? due : null;
3927
+ if (dueInHold && (!previous || dueInHold.receivedAt > previous.cue.receivedAt)) {
3928
+ return { cue: dueInHold, shownAt: now, lagMs };
3929
+ }
3930
+ if (previous && now < previous.shownAt + holdMs) {
3931
+ return previous;
3932
+ }
3933
+ return null;
3934
+ }
3903
3935
  async function loadCaptionVtt(url) {
3904
3936
  try {
3905
3937
  const response = await fetch(url, { cache: "no-store" });
@@ -3915,13 +3947,13 @@ async function loadLiveCaptions(url) {
3915
3947
  cache: "no-store",
3916
3948
  headers: { Accept: "application/json" }
3917
3949
  });
3918
- if (!response.ok) return [];
3950
+ if (!response.ok) return { ok: false, cues: [] };
3919
3951
  const body = await response.json();
3920
3952
  if (!body || typeof body !== "object" || !("cues" in body)) {
3921
- return [];
3953
+ return { ok: false, cues: [] };
3922
3954
  }
3923
3955
  const rows = body.cues;
3924
- if (!Array.isArray(rows)) return [];
3956
+ if (!Array.isArray(rows)) return { ok: false, cues: [] };
3925
3957
  const cues = [];
3926
3958
  for (const row of rows) {
3927
3959
  if (!row || typeof row !== "object") continue;
@@ -3932,9 +3964,9 @@ async function loadLiveCaptions(url) {
3932
3964
  }
3933
3965
  cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3934
3966
  }
3935
- return cues;
3967
+ return { ok: true, cues };
3936
3968
  } catch {
3937
- return [];
3969
+ return { ok: false, cues: [] };
3938
3970
  }
3939
3971
  }
3940
3972
  var DEFAULT_LABELS3 = {
@@ -4063,6 +4095,8 @@ function VideoPlayer({
4063
4095
  });
4064
4096
  const [isPlaying, setIsPlaying] = React8.useState(false);
4065
4097
  const [isMuted, setIsMuted] = React8.useState(muted);
4098
+ const isMutedRef = React8.useRef(isMuted);
4099
+ isMutedRef.current = isMuted;
4066
4100
  const [volume, setVolume] = React8.useState(1);
4067
4101
  const [currentTime, setCurrentTime] = React8.useState(0);
4068
4102
  const [duration, setDuration] = React8.useState(0);
@@ -4076,6 +4110,7 @@ function VideoPlayer({
4076
4110
  const [captionCues, setCaptionCues] = React8.useState([]);
4077
4111
  const [liveCaptionCues, setLiveCaptionCues] = React8.useState([]);
4078
4112
  const [liveLagMs, setLiveLagMs] = React8.useState(null);
4113
+ const liveCaptionLatchRef = React8.useRef(null);
4079
4114
  const [behindLive, setBehindLive] = React8.useState(false);
4080
4115
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8.useState(false);
4081
4116
  const hideTimerRef = React8.useRef(null);
@@ -4093,7 +4128,9 @@ function VideoPlayer({
4093
4128
  const edge = video.seekable.end(video.seekable.length - 1);
4094
4129
  const lagSeconds = edge - video.currentTime;
4095
4130
  setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4096
- if (liveCaptionsBaseUrl) setLiveLagMs(lagSeconds * 1e3);
4131
+ if (liveCaptionsBaseUrl) {
4132
+ setLiveLagMs((prev) => stabilizeLiveLagMs(prev, lagSeconds * 1e3));
4133
+ }
4097
4134
  }
4098
4135
  };
4099
4136
  const onDuration = () => setDuration(video.duration);
@@ -4102,11 +4139,15 @@ function VideoPlayer({
4102
4139
  setVolume(video.volume);
4103
4140
  };
4104
4141
  const onRate = () => setPlaybackRate(video.playbackRate);
4142
+ const onPlaying = () => {
4143
+ if (isMutedRef.current && !video.muted) video.muted = true;
4144
+ };
4105
4145
  video.addEventListener("timeupdate", onTimeUpdate);
4106
4146
  video.addEventListener("durationchange", onDuration);
4107
4147
  video.addEventListener("loadedmetadata", onDuration);
4108
4148
  video.addEventListener("volumechange", onVolume);
4109
4149
  video.addEventListener("ratechange", onRate);
4150
+ video.addEventListener("playing", onPlaying);
4110
4151
  onVolume();
4111
4152
  return () => {
4112
4153
  video.removeEventListener("timeupdate", onTimeUpdate);
@@ -4114,8 +4155,13 @@ function VideoPlayer({
4114
4155
  video.removeEventListener("loadedmetadata", onDuration);
4115
4156
  video.removeEventListener("volumechange", onVolume);
4116
4157
  video.removeEventListener("ratechange", onRate);
4158
+ video.removeEventListener("playing", onPlaying);
4117
4159
  };
4118
4160
  }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4161
+ React8.useLayoutEffect(() => {
4162
+ const video = videoRef.current;
4163
+ if (video) video.muted = isMuted;
4164
+ }, [isMuted, active, videoRef]);
4119
4165
  React8.useEffect(() => {
4120
4166
  const video = videoRef.current;
4121
4167
  if (!paused || !video || !isPlaying) return;
@@ -4174,16 +4220,17 @@ function VideoPlayer({
4174
4220
  if (captionLocale === "off") {
4175
4221
  setCaptionCues([]);
4176
4222
  setLiveCaptionCues([]);
4223
+ liveCaptionLatchRef.current = null;
4177
4224
  return;
4178
4225
  }
4179
4226
  let cancelled = false;
4180
4227
  const load = async () => {
4181
4228
  const liveBase = liveCaptionsBaseUrl?.trim();
4182
4229
  if (liveBase) {
4183
- const cues2 = await loadLiveCaptions(
4230
+ const loaded = await loadLiveCaptions(
4184
4231
  liveCaptionsUrl(liveBase, captionLocale)
4185
4232
  );
4186
- if (!cancelled) setLiveCaptionCues(cues2);
4233
+ if (!cancelled && loaded.ok) setLiveCaptionCues(loaded.cues);
4187
4234
  return;
4188
4235
  }
4189
4236
  const url = captionsVttUrlFromPlayback(src, captionLocale);
@@ -4202,7 +4249,20 @@ function VideoPlayer({
4202
4249
  window.clearInterval(timer);
4203
4250
  };
4204
4251
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4205
- const activeCaption = liveCaptionsBaseUrl ? captionAtLiveLag(liveCaptionCues, Date.now(), liveLagMs) : captionAtTime(captionCues, currentTime);
4252
+ const liveCaptionKey = `${captionLocale}:${liveCaptionsBaseUrl ?? ""}`;
4253
+ const liveCaptionKeyRef = React8.useRef(liveCaptionKey);
4254
+ if (liveCaptionKeyRef.current !== liveCaptionKey) {
4255
+ liveCaptionKeyRef.current = liveCaptionKey;
4256
+ liveCaptionLatchRef.current = null;
4257
+ }
4258
+ const liveLatch = liveCaptionsBaseUrl ? captionAtLiveLag(
4259
+ liveCaptionCues,
4260
+ Date.now(),
4261
+ liveLagMs,
4262
+ liveCaptionLatchRef.current
4263
+ ) : null;
4264
+ liveCaptionLatchRef.current = liveLatch;
4265
+ const activeCaption = liveCaptionsBaseUrl ? liveLatch?.cue ?? null : captionAtTime(captionCues, currentTime);
4206
4266
  function togglePlay() {
4207
4267
  const video = videoRef.current;
4208
4268
  if (!video) return;
@@ -4363,6 +4423,7 @@ function VideoPlayer({
4363
4423
  {
4364
4424
  ref: videoRef,
4365
4425
  playsInline: true,
4426
+ muted: isMuted,
4366
4427
  className: "h-full w-full bg-black object-contain",
4367
4428
  onClick: active ? togglePlay : void 0,
4368
4429
  poster
@@ -4436,7 +4497,7 @@ function VideoPlayer({
4436
4497
  {
4437
4498
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",
4438
4499
  "data-slot": "video-captions",
4439
- children: /* @__PURE__ */ 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 })
4500
+ children: /* @__PURE__ */ 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 })
4440
4501
  }
4441
4502
  ),
4442
4503
  active && /* @__PURE__ */ jsxs(