@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.d.cts CHANGED
@@ -808,7 +808,7 @@ interface VideoPlayerProps {
808
808
  */
809
809
  fit?: "aspect-video" | "contain";
810
810
  className?: string;
811
- /** Default CC language from `livo-locale` / `?lang=`. Off when omitted. */
811
+ /** Optional initial CC language. Defaults to Off. UI locale / `?lang=` do not enable captions. */
812
812
  defaultCaptionLocale?: CaptionLocale;
813
813
  /** Live poll base, e.g. `https://api.livo.tv/public/streams/{id}/captions`. */
814
814
  liveCaptionsBaseUrl?: string | null;
package/dist/index.d.ts CHANGED
@@ -808,7 +808,7 @@ interface VideoPlayerProps {
808
808
  */
809
809
  fit?: "aspect-video" | "contain";
810
810
  className?: string;
811
- /** Default CC language from `livo-locale` / `?lang=`. Off when omitted. */
811
+ /** Optional initial CC language. Defaults to Off. UI locale / `?lang=` do not enable captions. */
812
812
  defaultCaptionLocale?: CaptionLocale;
813
813
  /** Live poll base, e.g. `https://api.livo.tv/public/streams/{id}/captions`. */
814
814
  liveCaptionsBaseUrl?: string | null;
package/dist/index.js CHANGED
@@ -3230,7 +3230,7 @@ function parseThumbnailsVtt(text, vttUrl) {
3230
3230
  return cues;
3231
3231
  }
3232
3232
  function cueAtTime(cues, time) {
3233
- if (cues.length === 0) return null;
3233
+ if (cues.length === 0 || !Number.isFinite(time)) return null;
3234
3234
  const hit = cues.find((cue) => time >= cue.start && time < cue.end);
3235
3235
  if (hit) return hit;
3236
3236
  const last = cues[cues.length - 1];
@@ -3238,10 +3238,18 @@ function cueAtTime(cues, time) {
3238
3238
  return cues[0] ?? null;
3239
3239
  }
3240
3240
  function hoverTimeFromClientX(clientX, rect, duration) {
3241
- if (rect.width <= 0 || duration <= 0) return 0;
3241
+ if (rect.width <= 0 || !Number.isFinite(duration) || duration <= 0) return 0;
3242
3242
  const ratio = Math.min(1, Math.max(0, (clientX - rect.left) / rect.width));
3243
3243
  return ratio * duration;
3244
3244
  }
3245
+ function finitePlaybackDuration(duration, seekable, mode) {
3246
+ if (Number.isFinite(duration) && duration > 0) return duration;
3247
+ if (mode === "vod" && seekable.length > 0) {
3248
+ const span = seekable.end(seekable.length - 1) - seekable.start(0);
3249
+ if (Number.isFinite(span) && span > 0) return span;
3250
+ }
3251
+ return 0;
3252
+ }
3245
3253
  async function loadThumbnailsVtt(src) {
3246
3254
  const vttUrl = thumbnailsVttUrlFromPlayback(src);
3247
3255
  if (!vttUrl) return [];
@@ -3285,7 +3293,7 @@ function SeekBar({
3285
3293
  },
3286
3294
  [duration]
3287
3295
  );
3288
- const cue = hoverTime !== null && cues && cues.length > 0 ? cueAtTime(cues, hoverTime) : null;
3296
+ const cue = hoverTime !== null && Number.isFinite(duration) && duration > 0 && cues && cues.length > 0 ? cueAtTime(cues, hoverTime) : null;
3289
3297
  const hoverPercent = hoverTime !== null && duration > 0 ? Math.min(100, Math.max(0, hoverTime / duration * 100)) : 0;
3290
3298
  return /* @__PURE__ */ jsxs(
3291
3299
  "div",
@@ -3410,7 +3418,8 @@ function createLiveHlsConfig() {
3410
3418
  }
3411
3419
  function createVodHlsConfig() {
3412
3420
  return {
3413
- enableWorker: true
3421
+ enableWorker: true,
3422
+ startPosition: 0
3414
3423
  };
3415
3424
  }
3416
3425
 
@@ -4219,6 +4228,7 @@ function LiveCaptionStack({
4219
4228
 
4220
4229
  // src/components/video/nativeCaptionTrack.ts
4221
4230
  var NATIVE_CAPTION_HOLD_S = 4;
4231
+ var NATIVE_SUBTITLE_KIND = "subtitles";
4222
4232
  function joinCaptionTexts(cues) {
4223
4233
  return cues.map((cue) => cue.text.trim()).filter(Boolean).join(" ");
4224
4234
  }
@@ -4230,12 +4240,46 @@ function clearNativeCaptionCues(track) {
4230
4240
  if (cue) track.removeCue(cue);
4231
4241
  }
4232
4242
  }
4233
- function ensureNativeCaptionTrack(video, locale, existing) {
4234
- if (existing && existing.language === locale) return existing;
4235
- if (existing) existing.mode = "disabled";
4236
- const track = video.addTextTrack("captions", "Livo", locale);
4237
- track.mode = "hidden";
4238
- return track;
4243
+ function subtitleTracksForLocales(video) {
4244
+ return Array.from(video.textTracks).filter(
4245
+ (track) => track.kind === NATIVE_SUBTITLE_KIND && isCaptionLocale(track.language)
4246
+ );
4247
+ }
4248
+ function findSubtitleTrack(video, locale) {
4249
+ return subtitleTracksForLocales(video).find(
4250
+ (track) => track.language === locale
4251
+ ) ?? null;
4252
+ }
4253
+ function showingCaptionLocale(video) {
4254
+ for (const track of subtitleTracksForLocales(video)) {
4255
+ if (track.mode === "showing" && isCaptionLocale(track.language)) {
4256
+ return track.language;
4257
+ }
4258
+ }
4259
+ return "off";
4260
+ }
4261
+ function applyCaptionTrackModes(video, locale, nativeVisible) {
4262
+ for (const track of subtitleTracksForLocales(video)) {
4263
+ if (locale !== "off" && track.language === locale) {
4264
+ track.mode = nativeVisible ? "showing" : "hidden";
4265
+ } else {
4266
+ track.mode = "disabled";
4267
+ }
4268
+ }
4269
+ }
4270
+ function ensureLiveSubtitleTracks(video, labels) {
4271
+ const existing = subtitleTracksForLocales(video);
4272
+ return CAPTION_LOCALES.map((locale) => {
4273
+ const found = existing.find((track2) => track2.language === locale);
4274
+ if (found) return found;
4275
+ const track = video.addTextTrack(
4276
+ NATIVE_SUBTITLE_KIND,
4277
+ labels[locale],
4278
+ locale
4279
+ );
4280
+ track.mode = "disabled";
4281
+ return track;
4282
+ });
4239
4283
  }
4240
4284
  function syncNativeCaptionCue(video, track, text) {
4241
4285
  clearNativeCaptionCues(track);
@@ -4244,17 +4288,45 @@ function syncNativeCaptionCue(video, track, text) {
4244
4288
  const start = Number.isFinite(video.currentTime) ? video.currentTime : 0;
4245
4289
  track.addCue(new VTTCue(start, start + NATIVE_CAPTION_HOLD_S, trimmed));
4246
4290
  }
4247
- function syncNativeVodCues(track, cues) {
4248
- clearNativeCaptionCues(track);
4249
- for (const cue of cues) {
4250
- const text = cue.text.trim();
4251
- if (!text) continue;
4252
- track.addCue(new VTTCue(cue.start, cue.end, text));
4291
+ function zeroAnchorNativeTrackCues(track) {
4292
+ const list = track.cues;
4293
+ if (!list || list.length === 0) return;
4294
+ let origin = Number.POSITIVE_INFINITY;
4295
+ for (let index = 0; index < list.length; index += 1) {
4296
+ const cue = list[index];
4297
+ if (cue && Number.isFinite(cue.startTime) && cue.startTime < origin) {
4298
+ origin = cue.startTime;
4299
+ }
4253
4300
  }
4301
+ if (!Number.isFinite(origin) || origin <= 0) return;
4302
+ const next = [];
4303
+ for (let index = 0; index < list.length; index += 1) {
4304
+ const cue = list[index];
4305
+ if (!(cue instanceof VTTCue)) continue;
4306
+ next.push(
4307
+ new VTTCue(
4308
+ Math.max(0, cue.startTime - origin),
4309
+ Math.max(0, cue.endTime - origin),
4310
+ cue.text
4311
+ )
4312
+ );
4313
+ }
4314
+ clearNativeCaptionCues(track);
4315
+ for (const cue of next) track.addCue(cue);
4254
4316
  }
4255
- function setNativeCaptionTrackVisible(track, showing) {
4256
- if (!track) return;
4257
- track.mode = showing ? "showing" : "hidden";
4317
+ function listenTextTracksChange(tracks, onChange) {
4318
+ if (typeof tracks.addEventListener === "function") {
4319
+ tracks.addEventListener("change", onChange);
4320
+ return () => {
4321
+ if (typeof tracks.removeEventListener === "function") {
4322
+ tracks.removeEventListener("change", onChange);
4323
+ }
4324
+ };
4325
+ }
4326
+ tracks.onchange = onChange;
4327
+ return () => {
4328
+ if (tracks.onchange === onChange) tracks.onchange = null;
4329
+ };
4258
4330
  }
4259
4331
  var DEFAULT_LABELS3 = {
4260
4332
  play: "Play",
@@ -4348,7 +4420,8 @@ function VideoPlayer({
4348
4420
  const t = { ...DEFAULT_LABELS3, ...labels };
4349
4421
  const containerRef = React9.useRef(null);
4350
4422
  const firstPlayFiredRef = React9.useRef(false);
4351
- const nativeTrackRef = React9.useRef(null);
4423
+ const applyingTrackModesRef = React9.useRef(false);
4424
+ const captionLocaleRef = React9.useRef("off");
4352
4425
  const cssFullscreenRef = React9.useRef(false);
4353
4426
  const webkitFsRef = React9.useRef(false);
4354
4427
  const [userStarted, setUserStarted] = React9.useState(false);
@@ -4409,8 +4482,10 @@ function VideoPlayer({
4409
4482
  const [embedded, setEmbedded] = React9.useState(false);
4410
4483
  const [cssFullscreen, setCssFullscreen] = React9.useState(false);
4411
4484
  const [appleMobile, setAppleMobile] = React9.useState(false);
4485
+ const [webkitNativeFs, setWebkitNativeFs] = React9.useState(false);
4412
4486
  const hideTimerRef = React9.useRef(null);
4413
4487
  cssFullscreenRef.current = cssFullscreen;
4488
+ captionLocaleRef.current = captionLocale;
4414
4489
  React9.useEffect(() => {
4415
4490
  const el = containerRef.current;
4416
4491
  if (!el || typeof ResizeObserver === "undefined") return;
@@ -4448,7 +4523,7 @@ function VideoPlayer({
4448
4523
  }
4449
4524
  }
4450
4525
  };
4451
- const onDuration = () => setDuration(video.duration);
4526
+ const onDuration = () => setDuration(finitePlaybackDuration(video.duration, video.seekable, mode));
4452
4527
  const onVolume = () => {
4453
4528
  setIsMuted(video.muted);
4454
4529
  setVolume(video.volume);
@@ -4501,17 +4576,42 @@ function VideoPlayer({
4501
4576
  document.addEventListener("fullscreenchange", onFsChange);
4502
4577
  return () => document.removeEventListener("fullscreenchange", onFsChange);
4503
4578
  }, []);
4579
+ function applyNativeTrackModes(video, locale, nativeVisible) {
4580
+ applyingTrackModesRef.current = true;
4581
+ try {
4582
+ applyCaptionTrackModes(video, locale, nativeVisible);
4583
+ } finally {
4584
+ applyingTrackModesRef.current = false;
4585
+ }
4586
+ }
4587
+ function onSidecarTrackLoad(event) {
4588
+ const track = event.currentTarget.track;
4589
+ if (!track) return;
4590
+ if (isLivePlaylistSrc(src)) {
4591
+ zeroAnchorNativeTrackCues(track);
4592
+ }
4593
+ const video = videoRef.current;
4594
+ if (video) {
4595
+ applyNativeTrackModes(
4596
+ video,
4597
+ captionLocaleRef.current,
4598
+ webkitFsRef.current
4599
+ );
4600
+ }
4601
+ }
4504
4602
  React9.useEffect(() => {
4505
4603
  const video = asWebkitVideo(videoRef.current);
4506
4604
  if (!video) return;
4507
4605
  const onBegin = () => {
4508
4606
  webkitFsRef.current = true;
4607
+ setWebkitNativeFs(true);
4509
4608
  setIsFullscreen(true);
4510
- setNativeCaptionTrackVisible(nativeTrackRef.current, true);
4609
+ applyNativeTrackModes(video, captionLocaleRef.current, true);
4511
4610
  };
4512
4611
  const onEnd = () => {
4513
4612
  webkitFsRef.current = false;
4514
- setNativeCaptionTrackVisible(nativeTrackRef.current, false);
4613
+ setWebkitNativeFs(false);
4614
+ applyNativeTrackModes(video, captionLocaleRef.current, false);
4515
4615
  if (!cssFullscreenRef.current) setIsFullscreen(false);
4516
4616
  };
4517
4617
  video.addEventListener("webkitbeginfullscreen", onBegin);
@@ -4608,41 +4708,56 @@ function VideoPlayer({
4608
4708
  const activeCaption = liveCaptionsBaseUrl || endedLiveReplay ? null : captionAtTime(captionCues, currentTime);
4609
4709
  React9.useEffect(() => {
4610
4710
  const video = videoRef.current;
4611
- if (!video || captionLocale === "off") {
4612
- if (nativeTrackRef.current) {
4613
- nativeTrackRef.current.mode = "disabled";
4614
- nativeTrackRef.current = null;
4615
- }
4616
- return;
4617
- }
4711
+ if (!video || !liveCaptionsBaseUrl) return;
4618
4712
  try {
4619
- nativeTrackRef.current = ensureNativeCaptionTrack(
4620
- video,
4621
- captionLocale,
4622
- nativeTrackRef.current
4623
- );
4624
- setNativeCaptionTrackVisible(nativeTrackRef.current, webkitFsRef.current);
4713
+ ensureLiveSubtitleTracks(video, {
4714
+ en: t.captionsEn,
4715
+ es: t.captionsEs,
4716
+ pt: t.captionsPt
4717
+ });
4625
4718
  } catch {
4626
- nativeTrackRef.current = null;
4719
+ return;
4627
4720
  }
4628
- }, [videoRef, active, captionLocale]);
4721
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4722
+ }, [
4723
+ videoRef,
4724
+ active,
4725
+ liveCaptionsBaseUrl,
4726
+ captionLocale,
4727
+ t.captionsEn,
4728
+ t.captionsEs,
4729
+ t.captionsPt
4730
+ ]);
4629
4731
  React9.useEffect(() => {
4630
- const track = nativeTrackRef.current;
4631
- if (!track || captionLocale === "off") return;
4632
- if (liveCaptionsBaseUrl || endedLiveReplay) return;
4633
- syncNativeVodCues(track, captionCues);
4634
- }, [captionCues, captionLocale, liveCaptionsBaseUrl, endedLiveReplay]);
4732
+ const video = videoRef.current;
4733
+ if (!video) return;
4734
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4735
+ }, [videoRef, active, captionLocale, src]);
4635
4736
  React9.useEffect(() => {
4636
4737
  const video = videoRef.current;
4637
- const track = nativeTrackRef.current;
4638
- if (!video || !track) return;
4639
- if (!(liveCaptionsBaseUrl || endedLiveReplay)) return;
4738
+ if (!video) return;
4739
+ const onChange = () => {
4740
+ if (applyingTrackModesRef.current) return;
4741
+ const showing = showingCaptionLocale(video);
4742
+ if (!webkitFsRef.current) {
4743
+ if (showing !== "off") setCaptionLocale(showing);
4744
+ return;
4745
+ }
4746
+ setCaptionLocale(showing);
4747
+ };
4748
+ return listenTextTracksChange(video.textTracks, onChange);
4749
+ }, [videoRef, active, src, liveCaptionsBaseUrl]);
4750
+ React9.useEffect(() => {
4751
+ const video = videoRef.current;
4752
+ if (!video || captionLocale === "off" || !liveCaptionsBaseUrl) return;
4753
+ const track = findSubtitleTrack(video, captionLocale);
4754
+ if (!track) return;
4640
4755
  syncNativeCaptionCue(
4641
4756
  video,
4642
4757
  track,
4643
4758
  lastCaptionLines(joinCaptionTexts(captionStackCues))
4644
4759
  );
4645
- }, [captionStackCues, liveCaptionsBaseUrl, endedLiveReplay]);
4760
+ }, [captionStackCues, captionLocale, liveCaptionsBaseUrl]);
4646
4761
  function togglePlay() {
4647
4762
  const video = videoRef.current;
4648
4763
  if (!video) return;
@@ -4827,7 +4942,22 @@ function VideoPlayer({
4827
4942
  muted: isMuted,
4828
4943
  className: "livo-video-player-media h-full w-full bg-black object-contain",
4829
4944
  onClick: active ? togglePlay : void 0,
4830
- poster
4945
+ poster,
4946
+ children: !liveCaptionsBaseUrl ? CAPTION_LOCALES.map((locale) => {
4947
+ const url = captionsVttUrlFromPlayback(src, locale);
4948
+ if (!url) return null;
4949
+ return /* @__PURE__ */ jsx(
4950
+ "track",
4951
+ {
4952
+ kind: "subtitles",
4953
+ srcLang: locale,
4954
+ label: captionLocaleLabel(locale, t),
4955
+ src: url,
4956
+ onLoad: onSidecarTrackLoad
4957
+ },
4958
+ `${locale}:${url}`
4959
+ );
4960
+ }) : null
4831
4961
  }
4832
4962
  ),
4833
4963
  showPoster && /* @__PURE__ */ jsx(
@@ -4893,7 +5023,7 @@ function VideoPlayer({
4893
5023
  ] }),
4894
5024
  typeof stats?.viewers === "number" && /* @__PURE__ */ 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}` })
4895
5025
  ] }),
4896
- (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && /* @__PURE__ */ jsx(
5026
+ (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsx(
4897
5027
  LiveCaptionStack,
4898
5028
  {
4899
5029
  cues: captionStackCues,
@@ -4901,7 +5031,7 @@ function VideoPlayer({
4901
5031
  variant: appleMobile ? "window" : "scroll"
4902
5032
  }
4903
5033
  ),
4904
- !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsx(
5034
+ !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsx(
4905
5035
  "div",
4906
5036
  {
4907
5037
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",