@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.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) {
@@ -3844,6 +3860,14 @@ function useHlsPlayback({
3844
3860
 
3845
3861
  // src/components/video/captions.ts
3846
3862
  var CAPTION_LOCALES = ["en", "es", "pt"];
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
+ }
3847
3871
  function isCaptionLocale(value) {
3848
3872
  return CAPTION_LOCALES.includes(value);
3849
3873
  }
@@ -3885,8 +3909,26 @@ function parseCaptionVtt(text) {
3885
3909
  function captionAtTime(cues, time, options) {
3886
3910
  const matched = cues.find((cue) => time >= cue.start && time < cue.end) ?? null;
3887
3911
  if (matched) return matched;
3888
- if (options?.live && cues.length > 0) {
3889
- return cues[cues.length - 1] ?? null;
3912
+ return null;
3913
+ }
3914
+ function newestDueCue(cues, now, lagMs) {
3915
+ let shown = null;
3916
+ for (const cue of cues) {
3917
+ if (!Number.isFinite(cue.receivedAt)) continue;
3918
+ if (now - cue.receivedAt < lagMs) continue;
3919
+ if (!shown || cue.receivedAt > shown.receivedAt) shown = cue;
3920
+ }
3921
+ return shown;
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;
3890
3932
  }
3891
3933
  return null;
3892
3934
  }
@@ -3899,6 +3941,34 @@ async function loadCaptionVtt(url) {
3899
3941
  return [];
3900
3942
  }
3901
3943
  }
3944
+ async function loadLiveCaptions(url) {
3945
+ try {
3946
+ const response = await fetch(url, {
3947
+ cache: "no-store",
3948
+ headers: { Accept: "application/json" }
3949
+ });
3950
+ if (!response.ok) return { ok: false, cues: [] };
3951
+ const body = await response.json();
3952
+ if (!body || typeof body !== "object" || !("cues" in body)) {
3953
+ return { ok: false, cues: [] };
3954
+ }
3955
+ const rows = body.cues;
3956
+ if (!Array.isArray(rows)) return { ok: false, cues: [] };
3957
+ const cues = [];
3958
+ for (const row of rows) {
3959
+ if (!row || typeof row !== "object") continue;
3960
+ const record = row;
3961
+ if (typeof record.text !== "string" || !record.text.trim()) continue;
3962
+ if (typeof record.receivedAt !== "number" || !Number.isFinite(record.receivedAt)) {
3963
+ continue;
3964
+ }
3965
+ cues.push({ text: record.text.trim(), receivedAt: record.receivedAt });
3966
+ }
3967
+ return { ok: true, cues };
3968
+ } catch {
3969
+ return { ok: false, cues: [] };
3970
+ }
3971
+ }
3902
3972
  var DEFAULT_LABELS3 = {
3903
3973
  play: "Play",
3904
3974
  pause: "Pause",
@@ -4025,6 +4095,8 @@ function VideoPlayer({
4025
4095
  });
4026
4096
  const [isPlaying, setIsPlaying] = React8.useState(false);
4027
4097
  const [isMuted, setIsMuted] = React8.useState(muted);
4098
+ const isMutedRef = React8.useRef(isMuted);
4099
+ isMutedRef.current = isMuted;
4028
4100
  const [volume, setVolume] = React8.useState(1);
4029
4101
  const [currentTime, setCurrentTime] = React8.useState(0);
4030
4102
  const [duration, setDuration] = React8.useState(0);
@@ -4036,6 +4108,9 @@ function VideoPlayer({
4036
4108
  const [openMenu, setOpenMenu] = React8.useState(null);
4037
4109
  const [captionLocale, setCaptionLocale] = React8.useState(defaultCaptionLocale ?? "off");
4038
4110
  const [captionCues, setCaptionCues] = React8.useState([]);
4111
+ const [liveCaptionCues, setLiveCaptionCues] = React8.useState([]);
4112
+ const [liveLagMs, setLiveLagMs] = React8.useState(null);
4113
+ const liveCaptionLatchRef = React8.useRef(null);
4039
4114
  const [behindLive, setBehindLive] = React8.useState(false);
4040
4115
  const [webkitFullscreenSupported, setWebkitFullscreenSupported] = React8.useState(false);
4041
4116
  const hideTimerRef = React8.useRef(null);
@@ -4051,7 +4126,11 @@ function VideoPlayer({
4051
4126
  }
4052
4127
  if (mode === "live" && video.seekable.length > 0) {
4053
4128
  const edge = video.seekable.end(video.seekable.length - 1);
4054
- setBehindLive(edge - video.currentTime > LIVE_EDGE_THRESHOLD_S);
4129
+ const lagSeconds = edge - video.currentTime;
4130
+ setBehindLive(lagSeconds > LIVE_EDGE_THRESHOLD_S);
4131
+ if (liveCaptionsBaseUrl) {
4132
+ setLiveLagMs((prev) => stabilizeLiveLagMs(prev, lagSeconds * 1e3));
4133
+ }
4055
4134
  }
4056
4135
  };
4057
4136
  const onDuration = () => setDuration(video.duration);
@@ -4060,11 +4139,15 @@ function VideoPlayer({
4060
4139
  setVolume(video.volume);
4061
4140
  };
4062
4141
  const onRate = () => setPlaybackRate(video.playbackRate);
4142
+ const onPlaying = () => {
4143
+ if (isMutedRef.current && !video.muted) video.muted = true;
4144
+ };
4063
4145
  video.addEventListener("timeupdate", onTimeUpdate);
4064
4146
  video.addEventListener("durationchange", onDuration);
4065
4147
  video.addEventListener("loadedmetadata", onDuration);
4066
4148
  video.addEventListener("volumechange", onVolume);
4067
4149
  video.addEventListener("ratechange", onRate);
4150
+ video.addEventListener("playing", onPlaying);
4068
4151
  onVolume();
4069
4152
  return () => {
4070
4153
  video.removeEventListener("timeupdate", onTimeUpdate);
@@ -4072,8 +4155,13 @@ function VideoPlayer({
4072
4155
  video.removeEventListener("loadedmetadata", onDuration);
4073
4156
  video.removeEventListener("volumechange", onVolume);
4074
4157
  video.removeEventListener("ratechange", onRate);
4158
+ video.removeEventListener("playing", onPlaying);
4075
4159
  };
4076
- }, [videoRef, active, mode]);
4160
+ }, [videoRef, active, mode, liveCaptionsBaseUrl]);
4161
+ React8.useLayoutEffect(() => {
4162
+ const video = videoRef.current;
4163
+ if (video) video.muted = isMuted;
4164
+ }, [isMuted, active, videoRef]);
4077
4165
  React8.useEffect(() => {
4078
4166
  const video = videoRef.current;
4079
4167
  if (!paused || !video || !isPlaying) return;
@@ -4131,12 +4219,21 @@ function VideoPlayer({
4131
4219
  React8.useEffect(() => {
4132
4220
  if (captionLocale === "off") {
4133
4221
  setCaptionCues([]);
4222
+ setLiveCaptionCues([]);
4223
+ liveCaptionLatchRef.current = null;
4134
4224
  return;
4135
4225
  }
4136
4226
  let cancelled = false;
4137
4227
  const load = async () => {
4138
4228
  const liveBase = liveCaptionsBaseUrl?.trim();
4139
- const url = liveBase ? liveCaptionsUrl(liveBase, captionLocale) : captionsVttUrlFromPlayback(src, captionLocale);
4229
+ if (liveBase) {
4230
+ const loaded = await loadLiveCaptions(
4231
+ liveCaptionsUrl(liveBase, captionLocale)
4232
+ );
4233
+ if (!cancelled && loaded.ok) setLiveCaptionCues(loaded.cues);
4234
+ return;
4235
+ }
4236
+ const url = captionsVttUrlFromPlayback(src, captionLocale);
4140
4237
  if (!url) {
4141
4238
  if (!cancelled) setCaptionCues([]);
4142
4239
  return;
@@ -4152,9 +4249,20 @@ function VideoPlayer({
4152
4249
  window.clearInterval(timer);
4153
4250
  };
4154
4251
  }, [captionLocale, liveCaptionsBaseUrl, src]);
4155
- const activeCaption = captionAtTime(captionCues, currentTime, {
4156
- live: Boolean(liveCaptionsBaseUrl)
4157
- });
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);
4158
4266
  function togglePlay() {
4159
4267
  const video = videoRef.current;
4160
4268
  if (!video) return;
@@ -4315,6 +4423,7 @@ function VideoPlayer({
4315
4423
  {
4316
4424
  ref: videoRef,
4317
4425
  playsInline: true,
4426
+ muted: isMuted,
4318
4427
  className: "h-full w-full bg-black object-contain",
4319
4428
  onClick: active ? togglePlay : void 0,
4320
4429
  poster