@livo-tv/blocks 1.10.0 → 1.12.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
@@ -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) {
@@ -3870,6 +3886,14 @@ function useHlsPlayback({
3870
3886
 
3871
3887
  // src/components/video/captions.ts
3872
3888
  var CAPTION_LOCALES = ["en", "es", "pt"];
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
+ }
3873
3897
  function isCaptionLocale(value) {
3874
3898
  return CAPTION_LOCALES.includes(value);
3875
3899
  }
@@ -3911,8 +3935,26 @@ function parseCaptionVtt(text) {
3911
3935
  function captionAtTime(cues, time, options) {
3912
3936
  const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3913
3937
  if (matched) return matched;
3914
- if (options?.live && cues.length > 0) {
3915
- return cues[cues.length - 1] ?? null;
3938
+ return null;
3939
+ }
3940
+ function newestDueCue(cues, now, lagMs) {
3941
+ let shown = null;
3942
+ for (const cue of cues) {
3943
+ if (!Number.isFinite(cue.receivedAt)) continue;
3944
+ if (now - cue.receivedAt < lagMs) continue;
3945
+ if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3946
+ }
3947
+ return shown;
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;
3916
3958
  }
3917
3959
  return null;
3918
3960
  }
@@ -3925,6 +3967,34 @@ async function loadCaptionVtt(url) {
3925
3967
  return [];
3926
3968
  }
3927
3969
  }
3970
+ async function loadLiveCaptions(url) {
3971
+ try {
3972
+ const response = await fetch(url, {
3973
+ cache: "no-store",
3974
+ headers: { Accept: "application/json" }
3975
+ });
3976
+ if (!response.ok) return { ok: false, cues: [] };
3977
+ const body = await response.json();
3978
+ if (!body || typeof body !== "object" || !("cues" in body)) {
3979
+ return { ok: false, cues: [] };
3980
+ }
3981
+ const rows = body.cues;
3982
+ if (!Array.isArray(rows)) return { ok: false, cues: [] };
3983
+ const cues = [];
3984
+ for (const row of rows) {
3985
+ if (!row || typeof row !== "object") continue;
3986
+ const record = row;
3987
+ if (typeof record.text !== "string" || !record.text.trim()) continue;
3988
+ if (typeof record.receivedAt !== "number" || !Number.isFinite(record.receivedAt)) {
3989
+ continue;
3990
+ }
3991
+ cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3992
+ }
3993
+ return { ok: true, cues };
3994
+ } catch {
3995
+ return { ok: false, cues: [] };
3996
+ }
3997
+ }
3928
3998
  var DEFAULT_LABELS3 = {
3929
3999
  play: "Play",
3930
4000
  pause: "Pause",
@@ -4051,6 +4121,8 @@ function VideoPlayer({
4051
4121
  });
4052
4122
  const [isPlaying, setIsPlaying] = React8__namespace.useState(false);
4053
4123
  const [isMuted, setIsMuted] = React8__namespace.useState(muted);
4124
+ const isMutedRef = React8__namespace.useRef(isMuted);
4125
+ isMutedRef.current = isMuted;
4054
4126
  const [volume, setVolume] = React8__namespace.useState(1);
4055
4127
  const [currentTime, setCurrentTime] = React8__namespace.useState(0);
4056
4128
  const [duration, setDuration] = React8__namespace.useState(0);
@@ -4062,6 +4134,9 @@ function VideoPlayer({
4062
4134
  const [openMenu, setOpenMenu] = React8__namespace.useState(null);
4063
4135
  const [captionLocale, setCaptionLocale] = React8__namespace.useState(defaultCaptionLocale ?? "off");
4064
4136
  const [captionCues, setCaptionCues] = React8__namespace.useState([]);
4137
+ const [liveCaptionCues, setLiveCaptionCues] = React8__namespace.useState([]);
4138
+ const [liveLagMs, setLiveLagMs] = React8__namespace.useState(null);
4139
+ const liveCaptionLatchRef = React8__namespace.useRef(null);
4065
4140
  const [behindLive, setBehindLive] = React8__namespace.useState(false);
4066
4141
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8__namespace.useState(false);
4067
4142
  const hideTimerRef = React8__namespace.useRef(null);
@@ -4077,7 +4152,11 @@ function VideoPlayer({
4077
4152
  }
4078
4153
  if (mode === "live" && video.seekable.length > 0) {
4079
4154
  const edge = video.seekable.end(video.seekable.length - 1);
4080
- setBehindLive(edge - video.currentTime > LIVE_EDGE_THRESHOLD_S);
4155
+ const lagSeconds = edge - video.currentTime;
4156
+ setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4157
+ if (liveCaptionsBaseUrl) {
4158
+ setLiveLagMs((prev) => stabilizeLiveLagMs(prev, lagSeconds * 1e3));
4159
+ }
4081
4160
  }
4082
4161
  };
4083
4162
  const onDuration = () => setDuration(video.duration);
@@ -4086,11 +4165,15 @@ function VideoPlayer({
4086
4165
  setVolume(video.volume);
4087
4166
  };
4088
4167
  const onRate = () => setPlaybackRate(video.playbackRate);
4168
+ const onPlaying = () => {
4169
+ if (isMutedRef.current && !video.muted) video.muted = true;
4170
+ };
4089
4171
  video.addEventListener("timeupdate", onTimeUpdate);
4090
4172
  video.addEventListener("durationchange", onDuration);
4091
4173
  video.addEventListener("loadedmetadata", onDuration);
4092
4174
  video.addEventListener("volumechange", onVolume);
4093
4175
  video.addEventListener("ratechange", onRate);
4176
+ video.addEventListener("playing", onPlaying);
4094
4177
  onVolume();
4095
4178
  return () => {
4096
4179
  video.removeEventListener("timeupdate", onTimeUpdate);
@@ -4098,8 +4181,13 @@ function VideoPlayer({
4098
4181
  video.removeEventListener("loadedmetadata", onDuration);
4099
4182
  video.removeEventListener("volumechange", onVolume);
4100
4183
  video.removeEventListener("ratechange", onRate);
4184
+ video.removeEventListener("playing", onPlaying);
4101
4185
  };
4102
- }, [videoRef, active, mode]);
4186
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4187
+ React8__namespace.useLayoutEffect(() => {
4188
+ const video = videoRef.current;
4189
+ if (video) video.muted = isMuted;
4190
+ }, [isMuted, active, videoRef]);
4103
4191
  React8__namespace.useEffect(() => {
4104
4192
  const video = videoRef.current;
4105
4193
  if (!paused || !video || !isPlaying) return;
@@ -4157,12 +4245,21 @@ function VideoPlayer({
4157
4245
  React8__namespace.useEffect(() => {
4158
4246
  if (captionLocale === "off") {
4159
4247
  setCaptionCues([]);
4248
+ setLiveCaptionCues([]);
4249
+ liveCaptionLatchRef.current = null;
4160
4250
  return;
4161
4251
  }
4162
4252
  let cancelled = false;
4163
4253
  const load = async () => {
4164
4254
  const liveBase = liveCaptionsBaseUrl?.trim();
4165
- const url = liveBase ? liveCaptionsUrl(liveBase, captionLocale) : captionsVttUrlFromPlayback(src, captionLocale);
4255
+ if (liveBase) {
4256
+ const loaded = await loadLiveCaptions(
4257
+ liveCaptionsUrl(liveBase, captionLocale)
4258
+ );
4259
+ if (!cancelled && loaded.ok) setLiveCaptionCues(loaded.cues);
4260
+ return;
4261
+ }
4262
+ const url = captionsVttUrlFromPlayback(src, captionLocale);
4166
4263
  if (!url) {
4167
4264
  if (!cancelled) setCaptionCues([]);
4168
4265
  return;
@@ -4178,9 +4275,20 @@ function VideoPlayer({
4178
4275
  window.clearInterval(timer);
4179
4276
  };
4180
4277
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4181
- const activeCaption = captionAtTime(captionCues, currentTime, {
4182
- live: Boolean(liveCaptionsBaseUrl)
4183
- });
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);
4184
4292
  function togglePlay() {
4185
4293
  const video = videoRef.current;
4186
4294
  if (!video) return;
@@ -4341,6 +4449,7 @@ function VideoPlayer({
4341
4449
  {
4342
4450
  ref: videoRef,
4343
4451
  playsInline: true,
4452
+ muted: isMuted,
4344
4453
  className: "h-full w-full bg-black object-contain",
4345
4454
  onClick: active ? togglePlay : void 0,
4346
4455
  poster