@livo-tv/blocks 1.19.0 → 1.21.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
@@ -3256,7 +3256,7 @@ function parseThumbnailsVtt(text, vttUrl) {
3256
3256
  return cues;
3257
3257
  }
3258
3258
  function cueAtTime(cues, time) {
3259
- if (cues.length === 0) return null;
3259
+ if (cues.length === 0 || !Number.isFinite(time)) return null;
3260
3260
  const hit = cues.find((cue) => time >= cue.start && time < cue.end);
3261
3261
  if (hit) return hit;
3262
3262
  const last = cues[cues.length - 1];
@@ -3264,10 +3264,18 @@ function cueAtTime(cues, time) {
3264
3264
  return cues[0] ?? null;
3265
3265
  }
3266
3266
  function hoverTimeFromClientX(clientX, rect, duration) {
3267
- if (rect.width <= 0 || duration <= 0) return 0;
3267
+ if (rect.width <= 0 || !Number.isFinite(duration) || duration <= 0) return 0;
3268
3268
  const ratio = Math.min(1, Math.max(0, (clientX - rect.left) / rect.width));
3269
3269
  return ratio * duration;
3270
3270
  }
3271
+ function finitePlaybackDuration(duration, seekable, mode) {
3272
+ if (Number.isFinite(duration) && duration > 0) return duration;
3273
+ if (mode === "vod" && seekable.length > 0) {
3274
+ const span = seekable.end(seekable.length - 1) - seekable.start(0);
3275
+ if (Number.isFinite(span) && span > 0) return span;
3276
+ }
3277
+ return 0;
3278
+ }
3271
3279
  async function loadThumbnailsVtt(src) {
3272
3280
  const vttUrl = thumbnailsVttUrlFromPlayback(src);
3273
3281
  if (!vttUrl) return [];
@@ -3311,7 +3319,7 @@ function SeekBar({
3311
3319
  },
3312
3320
  [duration]
3313
3321
  );
3314
- const cue = hoverTime !== null && cues && cues.length > 0 ? cueAtTime(cues, hoverTime) : null;
3322
+ const cue = hoverTime !== null && Number.isFinite(duration) && duration > 0 && cues && cues.length > 0 ? cueAtTime(cues, hoverTime) : null;
3315
3323
  const hoverPercent = hoverTime !== null && duration > 0 ? Math.min(100, Math.max(0, hoverTime / duration * 100)) : 0;
3316
3324
  return /* @__PURE__ */ jsxRuntime.jsxs(
3317
3325
  "div",
@@ -3436,7 +3444,8 @@ function createLiveHlsConfig() {
3436
3444
  }
3437
3445
  function createVodHlsConfig() {
3438
3446
  return {
3439
- enableWorker: true
3447
+ enableWorker: true,
3448
+ startPosition: 0
3440
3449
  };
3441
3450
  }
3442
3451
 
@@ -4245,6 +4254,7 @@ function LiveCaptionStack({
4245
4254
 
4246
4255
  // src/components/video/nativeCaptionTrack.ts
4247
4256
  var NATIVE_CAPTION_HOLD_S = 4;
4257
+ var NATIVE_SUBTITLE_KIND = "subtitles";
4248
4258
  function joinCaptionTexts(cues) {
4249
4259
  return cues.map((cue) => cue.text.trim()).filter(Boolean).join(" ");
4250
4260
  }
@@ -4256,12 +4266,46 @@ function clearNativeCaptionCues(track) {
4256
4266
  if (cue) track.removeCue(cue);
4257
4267
  }
4258
4268
  }
4259
- function ensureNativeCaptionTrack(video, locale, existing) {
4260
- if (existing && existing.language === locale) return existing;
4261
- if (existing) existing.mode = "disabled";
4262
- const track = video.addTextTrack("captions", "Livo", locale);
4263
- track.mode = "hidden";
4264
- return track;
4269
+ function subtitleTracksForLocales(video) {
4270
+ return Array.from(video.textTracks).filter(
4271
+ (track) => track.kind === NATIVE_SUBTITLE_KIND && isCaptionLocale(track.language)
4272
+ );
4273
+ }
4274
+ function findSubtitleTrack(video, locale) {
4275
+ return subtitleTracksForLocales(video).find(
4276
+ (track) => track.language === locale
4277
+ ) ?? null;
4278
+ }
4279
+ function showingCaptionLocale(video) {
4280
+ for (const track of subtitleTracksForLocales(video)) {
4281
+ if (track.mode === "showing" && isCaptionLocale(track.language)) {
4282
+ return track.language;
4283
+ }
4284
+ }
4285
+ return "off";
4286
+ }
4287
+ function applyCaptionTrackModes(video, locale, nativeVisible) {
4288
+ for (const track of subtitleTracksForLocales(video)) {
4289
+ if (locale !== "off" && track.language === locale) {
4290
+ track.mode = nativeVisible ? "showing" : "hidden";
4291
+ } else {
4292
+ track.mode = "disabled";
4293
+ }
4294
+ }
4295
+ }
4296
+ function ensureLiveSubtitleTracks(video, labels) {
4297
+ const existing = subtitleTracksForLocales(video);
4298
+ return CAPTION_LOCALES.map((locale) => {
4299
+ const found = existing.find((track2) => track2.language === locale);
4300
+ if (found) return found;
4301
+ const track = video.addTextTrack(
4302
+ NATIVE_SUBTITLE_KIND,
4303
+ labels[locale],
4304
+ locale
4305
+ );
4306
+ track.mode = "disabled";
4307
+ return track;
4308
+ });
4265
4309
  }
4266
4310
  function syncNativeCaptionCue(video, track, text) {
4267
4311
  clearNativeCaptionCues(track);
@@ -4270,17 +4314,45 @@ function syncNativeCaptionCue(video, track, text) {
4270
4314
  const start = Number.isFinite(video.currentTime) ? video.currentTime : 0;
4271
4315
  track.addCue(new VTTCue(start, start + NATIVE_CAPTION_HOLD_S, trimmed));
4272
4316
  }
4273
- function syncNativeVodCues(track, cues) {
4274
- clearNativeCaptionCues(track);
4275
- for (const cue of cues) {
4276
- const text = cue.text.trim();
4277
- if (!text) continue;
4278
- track.addCue(new VTTCue(cue.start, cue.end, text));
4317
+ function zeroAnchorNativeTrackCues(track) {
4318
+ const list = track.cues;
4319
+ if (!list || list.length === 0) return;
4320
+ let origin = Number.POSITIVE_INFINITY;
4321
+ for (let index = 0; index < list.length; index += 1) {
4322
+ const cue = list[index];
4323
+ if (cue && Number.isFinite(cue.startTime) && cue.startTime < origin) {
4324
+ origin = cue.startTime;
4325
+ }
4279
4326
  }
4327
+ if (!Number.isFinite(origin) || origin <= 0) return;
4328
+ const next = [];
4329
+ for (let index = 0; index < list.length; index += 1) {
4330
+ const cue = list[index];
4331
+ if (!(cue instanceof VTTCue)) continue;
4332
+ next.push(
4333
+ new VTTCue(
4334
+ Math.max(0, cue.startTime - origin),
4335
+ Math.max(0, cue.endTime - origin),
4336
+ cue.text
4337
+ )
4338
+ );
4339
+ }
4340
+ clearNativeCaptionCues(track);
4341
+ for (const cue of next) track.addCue(cue);
4280
4342
  }
4281
- function setNativeCaptionTrackVisible(track, showing) {
4282
- if (!track) return;
4283
- track.mode = showing ? "showing" : "hidden";
4343
+ function listenTextTracksChange(tracks, onChange) {
4344
+ if (typeof tracks.addEventListener === "function") {
4345
+ tracks.addEventListener("change", onChange);
4346
+ return () => {
4347
+ if (typeof tracks.removeEventListener === "function") {
4348
+ tracks.removeEventListener("change", onChange);
4349
+ }
4350
+ };
4351
+ }
4352
+ tracks.onchange = onChange;
4353
+ return () => {
4354
+ if (tracks.onchange === onChange) tracks.onchange = null;
4355
+ };
4284
4356
  }
4285
4357
  var DEFAULT_LABELS3 = {
4286
4358
  play: "Play",
@@ -4374,7 +4446,8 @@ function VideoPlayer({
4374
4446
  const t = { ...DEFAULT_LABELS3, ...labels };
4375
4447
  const containerRef = React9__namespace.useRef(null);
4376
4448
  const firstPlayFiredRef = React9__namespace.useRef(false);
4377
- const nativeTrackRef = React9__namespace.useRef(null);
4449
+ const applyingTrackModesRef = React9__namespace.useRef(false);
4450
+ const captionLocaleRef = React9__namespace.useRef("off");
4378
4451
  const cssFullscreenRef = React9__namespace.useRef(false);
4379
4452
  const webkitFsRef = React9__namespace.useRef(false);
4380
4453
  const [userStarted, setUserStarted] = React9__namespace.useState(false);
@@ -4435,8 +4508,10 @@ function VideoPlayer({
4435
4508
  const [embedded, setEmbedded] = React9__namespace.useState(false);
4436
4509
  const [cssFullscreen, setCssFullscreen] = React9__namespace.useState(false);
4437
4510
  const [appleMobile, setAppleMobile] = React9__namespace.useState(false);
4511
+ const [webkitNativeFs, setWebkitNativeFs] = React9__namespace.useState(false);
4438
4512
  const hideTimerRef = React9__namespace.useRef(null);
4439
4513
  cssFullscreenRef.current = cssFullscreen;
4514
+ captionLocaleRef.current = captionLocale;
4440
4515
  React9__namespace.useEffect(() => {
4441
4516
  const el = containerRef.current;
4442
4517
  if (!el || typeof ResizeObserver === "undefined") return;
@@ -4474,7 +4549,7 @@ function VideoPlayer({
4474
4549
  }
4475
4550
  }
4476
4551
  };
4477
- const onDuration = () => setDuration(video.duration);
4552
+ const onDuration = () => setDuration(finitePlaybackDuration(video.duration, video.seekable, mode));
4478
4553
  const onVolume = () => {
4479
4554
  setIsMuted(video.muted);
4480
4555
  setVolume(video.volume);
@@ -4527,17 +4602,42 @@ function VideoPlayer({
4527
4602
  document.addEventListener("fullscreenchange", onFsChange);
4528
4603
  return () => document.removeEventListener("fullscreenchange", onFsChange);
4529
4604
  }, []);
4605
+ function applyNativeTrackModes(video, locale, nativeVisible) {
4606
+ applyingTrackModesRef.current = true;
4607
+ try {
4608
+ applyCaptionTrackModes(video, locale, nativeVisible);
4609
+ } finally {
4610
+ applyingTrackModesRef.current = false;
4611
+ }
4612
+ }
4613
+ function onSidecarTrackLoad(event) {
4614
+ const track = event.currentTarget.track;
4615
+ if (!track) return;
4616
+ if (isLivePlaylistSrc(src)) {
4617
+ zeroAnchorNativeTrackCues(track);
4618
+ }
4619
+ const video = videoRef.current;
4620
+ if (video) {
4621
+ applyNativeTrackModes(
4622
+ video,
4623
+ captionLocaleRef.current,
4624
+ webkitFsRef.current
4625
+ );
4626
+ }
4627
+ }
4530
4628
  React9__namespace.useEffect(() => {
4531
4629
  const video = asWebkitVideo(videoRef.current);
4532
4630
  if (!video) return;
4533
4631
  const onBegin = () => {
4534
4632
  webkitFsRef.current = true;
4633
+ setWebkitNativeFs(true);
4535
4634
  setIsFullscreen(true);
4536
- setNativeCaptionTrackVisible(nativeTrackRef.current, true);
4635
+ applyNativeTrackModes(video, captionLocaleRef.current, true);
4537
4636
  };
4538
4637
  const onEnd = () => {
4539
4638
  webkitFsRef.current = false;
4540
- setNativeCaptionTrackVisible(nativeTrackRef.current, false);
4639
+ setWebkitNativeFs(false);
4640
+ applyNativeTrackModes(video, captionLocaleRef.current, false);
4541
4641
  if (!cssFullscreenRef.current) setIsFullscreen(false);
4542
4642
  };
4543
4643
  video.addEventListener("webkitbeginfullscreen", onBegin);
@@ -4634,41 +4734,56 @@ function VideoPlayer({
4634
4734
  const activeCaption = liveCaptionsBaseUrl || endedLiveReplay ? null : captionAtTime(captionCues, currentTime);
4635
4735
  React9__namespace.useEffect(() => {
4636
4736
  const video = videoRef.current;
4637
- if (!video || captionLocale === "off") {
4638
- if (nativeTrackRef.current) {
4639
- nativeTrackRef.current.mode = "disabled";
4640
- nativeTrackRef.current = null;
4641
- }
4642
- return;
4643
- }
4737
+ if (!video || !liveCaptionsBaseUrl) return;
4644
4738
  try {
4645
- nativeTrackRef.current = ensureNativeCaptionTrack(
4646
- video,
4647
- captionLocale,
4648
- nativeTrackRef.current
4649
- );
4650
- setNativeCaptionTrackVisible(nativeTrackRef.current, webkitFsRef.current);
4739
+ ensureLiveSubtitleTracks(video, {
4740
+ en: t.captionsEn,
4741
+ es: t.captionsEs,
4742
+ pt: t.captionsPt
4743
+ });
4651
4744
  } catch {
4652
- nativeTrackRef.current = null;
4745
+ return;
4653
4746
  }
4654
- }, [videoRef, active, captionLocale]);
4747
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4748
+ }, [
4749
+ videoRef,
4750
+ active,
4751
+ liveCaptionsBaseUrl,
4752
+ captionLocale,
4753
+ t.captionsEn,
4754
+ t.captionsEs,
4755
+ t.captionsPt
4756
+ ]);
4655
4757
  React9__namespace.useEffect(() => {
4656
- const track = nativeTrackRef.current;
4657
- if (!track || captionLocale === "off") return;
4658
- if (liveCaptionsBaseUrl || endedLiveReplay) return;
4659
- syncNativeVodCues(track, captionCues);
4660
- }, [captionCues, captionLocale, liveCaptionsBaseUrl, endedLiveReplay]);
4758
+ const video = videoRef.current;
4759
+ if (!video) return;
4760
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4761
+ }, [videoRef, active, captionLocale, src]);
4661
4762
  React9__namespace.useEffect(() => {
4662
4763
  const video = videoRef.current;
4663
- const track = nativeTrackRef.current;
4664
- if (!video || !track) return;
4665
- if (!(liveCaptionsBaseUrl || endedLiveReplay)) return;
4764
+ if (!video) return;
4765
+ const onChange = () => {
4766
+ if (applyingTrackModesRef.current) return;
4767
+ const showing = showingCaptionLocale(video);
4768
+ if (!webkitFsRef.current) {
4769
+ if (showing !== "off") setCaptionLocale(showing);
4770
+ return;
4771
+ }
4772
+ setCaptionLocale(showing);
4773
+ };
4774
+ return listenTextTracksChange(video.textTracks, onChange);
4775
+ }, [videoRef, active, src, liveCaptionsBaseUrl]);
4776
+ React9__namespace.useEffect(() => {
4777
+ const video = videoRef.current;
4778
+ if (!video || captionLocale === "off" || !liveCaptionsBaseUrl) return;
4779
+ const track = findSubtitleTrack(video, captionLocale);
4780
+ if (!track) return;
4666
4781
  syncNativeCaptionCue(
4667
4782
  video,
4668
4783
  track,
4669
4784
  lastCaptionLines(joinCaptionTexts(captionStackCues))
4670
4785
  );
4671
- }, [captionStackCues, liveCaptionsBaseUrl, endedLiveReplay]);
4786
+ }, [captionStackCues, captionLocale, liveCaptionsBaseUrl]);
4672
4787
  function togglePlay() {
4673
4788
  const video = videoRef.current;
4674
4789
  if (!video) return;
@@ -4853,7 +4968,22 @@ function VideoPlayer({
4853
4968
  muted: isMuted,
4854
4969
  className: "livo-video-player-media h-full w-full bg-black object-contain",
4855
4970
  onClick: active ? togglePlay : void 0,
4856
- poster
4971
+ poster,
4972
+ children: !liveCaptionsBaseUrl ? CAPTION_LOCALES.map((locale) => {
4973
+ const url = captionsVttUrlFromPlayback(src, locale);
4974
+ if (!url) return null;
4975
+ return /* @__PURE__ */ jsxRuntime.jsx(
4976
+ "track",
4977
+ {
4978
+ kind: "subtitles",
4979
+ srcLang: locale,
4980
+ label: captionLocaleLabel(locale, t),
4981
+ src: url,
4982
+ onLoad: onSidecarTrackLoad
4983
+ },
4984
+ `${locale}:${url}`
4985
+ );
4986
+ }) : null
4857
4987
  }
4858
4988
  ),
4859
4989
  showPoster && /* @__PURE__ */ jsxRuntime.jsx(
@@ -4919,7 +5049,7 @@ function VideoPlayer({
4919
5049
  ] }),
4920
5050
  typeof stats?.viewers === "number" && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "rounded bg-black/60 px-2 py-0.5 text-xs", children: t.formatViewers ? t.formatViewers(stats.viewers) : `\u{1F441} ${stats.viewers}` })
4921
5051
  ] }),
4922
- (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && /* @__PURE__ */ jsxRuntime.jsx(
5052
+ (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsxRuntime.jsx(
4923
5053
  LiveCaptionStack,
4924
5054
  {
4925
5055
  cues: captionStackCues,
@@ -4927,7 +5057,7 @@ function VideoPlayer({
4927
5057
  variant: appleMobile ? "window" : "scroll"
4928
5058
  }
4929
5059
  ),
4930
- !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsxRuntime.jsx(
5060
+ !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsxRuntime.jsx(
4931
5061
  "div",
4932
5062
  {
4933
5063
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",