@livo-tv/blocks 1.19.0 → 1.20.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
@@ -4219,6 +4219,7 @@ function LiveCaptionStack({
4219
4219
 
4220
4220
  // src/components/video/nativeCaptionTrack.ts
4221
4221
  var NATIVE_CAPTION_HOLD_S = 4;
4222
+ var NATIVE_SUBTITLE_KIND = "subtitles";
4222
4223
  function joinCaptionTexts(cues) {
4223
4224
  return cues.map((cue) => cue.text.trim()).filter(Boolean).join(" ");
4224
4225
  }
@@ -4230,12 +4231,46 @@ function clearNativeCaptionCues(track) {
4230
4231
  if (cue) track.removeCue(cue);
4231
4232
  }
4232
4233
  }
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;
4234
+ function subtitleTracksForLocales(video) {
4235
+ return Array.from(video.textTracks).filter(
4236
+ (track) => track.kind === NATIVE_SUBTITLE_KIND && isCaptionLocale(track.language)
4237
+ );
4238
+ }
4239
+ function findSubtitleTrack(video, locale) {
4240
+ return subtitleTracksForLocales(video).find(
4241
+ (track) => track.language === locale
4242
+ ) ?? null;
4243
+ }
4244
+ function showingCaptionLocale(video) {
4245
+ for (const track of subtitleTracksForLocales(video)) {
4246
+ if (track.mode === "showing" && isCaptionLocale(track.language)) {
4247
+ return track.language;
4248
+ }
4249
+ }
4250
+ return "off";
4251
+ }
4252
+ function applyCaptionTrackModes(video, locale, nativeVisible) {
4253
+ for (const track of subtitleTracksForLocales(video)) {
4254
+ if (locale !== "off" && track.language === locale) {
4255
+ track.mode = nativeVisible ? "showing" : "hidden";
4256
+ } else {
4257
+ track.mode = "disabled";
4258
+ }
4259
+ }
4260
+ }
4261
+ function ensureLiveSubtitleTracks(video, labels) {
4262
+ const existing = subtitleTracksForLocales(video);
4263
+ return CAPTION_LOCALES.map((locale) => {
4264
+ const found = existing.find((track2) => track2.language === locale);
4265
+ if (found) return found;
4266
+ const track = video.addTextTrack(
4267
+ NATIVE_SUBTITLE_KIND,
4268
+ labels[locale],
4269
+ locale
4270
+ );
4271
+ track.mode = "disabled";
4272
+ return track;
4273
+ });
4239
4274
  }
4240
4275
  function syncNativeCaptionCue(video, track, text) {
4241
4276
  clearNativeCaptionCues(track);
@@ -4244,17 +4279,45 @@ function syncNativeCaptionCue(video, track, text) {
4244
4279
  const start = Number.isFinite(video.currentTime) ? video.currentTime : 0;
4245
4280
  track.addCue(new VTTCue(start, start + NATIVE_CAPTION_HOLD_S, trimmed));
4246
4281
  }
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));
4282
+ function zeroAnchorNativeTrackCues(track) {
4283
+ const list = track.cues;
4284
+ if (!list || list.length === 0) return;
4285
+ let origin = Number.POSITIVE_INFINITY;
4286
+ for (let index = 0; index < list.length; index += 1) {
4287
+ const cue = list[index];
4288
+ if (cue && Number.isFinite(cue.startTime) && cue.startTime < origin) {
4289
+ origin = cue.startTime;
4290
+ }
4253
4291
  }
4292
+ if (!Number.isFinite(origin) || origin <= 0) return;
4293
+ const next = [];
4294
+ for (let index = 0; index < list.length; index += 1) {
4295
+ const cue = list[index];
4296
+ if (!(cue instanceof VTTCue)) continue;
4297
+ next.push(
4298
+ new VTTCue(
4299
+ Math.max(0, cue.startTime - origin),
4300
+ Math.max(0, cue.endTime - origin),
4301
+ cue.text
4302
+ )
4303
+ );
4304
+ }
4305
+ clearNativeCaptionCues(track);
4306
+ for (const cue of next) track.addCue(cue);
4254
4307
  }
4255
- function setNativeCaptionTrackVisible(track, showing) {
4256
- if (!track) return;
4257
- track.mode = showing ? "showing" : "hidden";
4308
+ function listenTextTracksChange(tracks, onChange) {
4309
+ if (typeof tracks.addEventListener === "function") {
4310
+ tracks.addEventListener("change", onChange);
4311
+ return () => {
4312
+ if (typeof tracks.removeEventListener === "function") {
4313
+ tracks.removeEventListener("change", onChange);
4314
+ }
4315
+ };
4316
+ }
4317
+ tracks.onchange = onChange;
4318
+ return () => {
4319
+ if (tracks.onchange === onChange) tracks.onchange = null;
4320
+ };
4258
4321
  }
4259
4322
  var DEFAULT_LABELS3 = {
4260
4323
  play: "Play",
@@ -4348,7 +4411,8 @@ function VideoPlayer({
4348
4411
  const t = { ...DEFAULT_LABELS3, ...labels };
4349
4412
  const containerRef = React9.useRef(null);
4350
4413
  const firstPlayFiredRef = React9.useRef(false);
4351
- const nativeTrackRef = React9.useRef(null);
4414
+ const applyingTrackModesRef = React9.useRef(false);
4415
+ const captionLocaleRef = React9.useRef("off");
4352
4416
  const cssFullscreenRef = React9.useRef(false);
4353
4417
  const webkitFsRef = React9.useRef(false);
4354
4418
  const [userStarted, setUserStarted] = React9.useState(false);
@@ -4409,8 +4473,10 @@ function VideoPlayer({
4409
4473
  const [embedded, setEmbedded] = React9.useState(false);
4410
4474
  const [cssFullscreen, setCssFullscreen] = React9.useState(false);
4411
4475
  const [appleMobile, setAppleMobile] = React9.useState(false);
4476
+ const [webkitNativeFs, setWebkitNativeFs] = React9.useState(false);
4412
4477
  const hideTimerRef = React9.useRef(null);
4413
4478
  cssFullscreenRef.current = cssFullscreen;
4479
+ captionLocaleRef.current = captionLocale;
4414
4480
  React9.useEffect(() => {
4415
4481
  const el = containerRef.current;
4416
4482
  if (!el || typeof ResizeObserver === "undefined") return;
@@ -4501,17 +4567,42 @@ function VideoPlayer({
4501
4567
  document.addEventListener("fullscreenchange", onFsChange);
4502
4568
  return () => document.removeEventListener("fullscreenchange", onFsChange);
4503
4569
  }, []);
4570
+ function applyNativeTrackModes(video, locale, nativeVisible) {
4571
+ applyingTrackModesRef.current = true;
4572
+ try {
4573
+ applyCaptionTrackModes(video, locale, nativeVisible);
4574
+ } finally {
4575
+ applyingTrackModesRef.current = false;
4576
+ }
4577
+ }
4578
+ function onSidecarTrackLoad(event) {
4579
+ const track = event.currentTarget.track;
4580
+ if (!track) return;
4581
+ if (isLivePlaylistSrc(src)) {
4582
+ zeroAnchorNativeTrackCues(track);
4583
+ }
4584
+ const video = videoRef.current;
4585
+ if (video) {
4586
+ applyNativeTrackModes(
4587
+ video,
4588
+ captionLocaleRef.current,
4589
+ webkitFsRef.current
4590
+ );
4591
+ }
4592
+ }
4504
4593
  React9.useEffect(() => {
4505
4594
  const video = asWebkitVideo(videoRef.current);
4506
4595
  if (!video) return;
4507
4596
  const onBegin = () => {
4508
4597
  webkitFsRef.current = true;
4598
+ setWebkitNativeFs(true);
4509
4599
  setIsFullscreen(true);
4510
- setNativeCaptionTrackVisible(nativeTrackRef.current, true);
4600
+ applyNativeTrackModes(video, captionLocaleRef.current, true);
4511
4601
  };
4512
4602
  const onEnd = () => {
4513
4603
  webkitFsRef.current = false;
4514
- setNativeCaptionTrackVisible(nativeTrackRef.current, false);
4604
+ setWebkitNativeFs(false);
4605
+ applyNativeTrackModes(video, captionLocaleRef.current, false);
4515
4606
  if (!cssFullscreenRef.current) setIsFullscreen(false);
4516
4607
  };
4517
4608
  video.addEventListener("webkitbeginfullscreen", onBegin);
@@ -4608,41 +4699,56 @@ function VideoPlayer({
4608
4699
  const activeCaption = liveCaptionsBaseUrl || endedLiveReplay ? null : captionAtTime(captionCues, currentTime);
4609
4700
  React9.useEffect(() => {
4610
4701
  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
- }
4702
+ if (!video || !liveCaptionsBaseUrl) return;
4618
4703
  try {
4619
- nativeTrackRef.current = ensureNativeCaptionTrack(
4620
- video,
4621
- captionLocale,
4622
- nativeTrackRef.current
4623
- );
4624
- setNativeCaptionTrackVisible(nativeTrackRef.current, webkitFsRef.current);
4704
+ ensureLiveSubtitleTracks(video, {
4705
+ en: t.captionsEn,
4706
+ es: t.captionsEs,
4707
+ pt: t.captionsPt
4708
+ });
4625
4709
  } catch {
4626
- nativeTrackRef.current = null;
4710
+ return;
4627
4711
  }
4628
- }, [videoRef, active, captionLocale]);
4712
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4713
+ }, [
4714
+ videoRef,
4715
+ active,
4716
+ liveCaptionsBaseUrl,
4717
+ captionLocale,
4718
+ t.captionsEn,
4719
+ t.captionsEs,
4720
+ t.captionsPt
4721
+ ]);
4722
+ React9.useEffect(() => {
4723
+ const video = videoRef.current;
4724
+ if (!video) return;
4725
+ applyNativeTrackModes(video, captionLocale, webkitFsRef.current);
4726
+ }, [videoRef, active, captionLocale, src]);
4629
4727
  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]);
4728
+ const video = videoRef.current;
4729
+ if (!video) return;
4730
+ const onChange = () => {
4731
+ if (applyingTrackModesRef.current) return;
4732
+ const showing = showingCaptionLocale(video);
4733
+ if (!webkitFsRef.current) {
4734
+ if (showing !== "off") setCaptionLocale(showing);
4735
+ return;
4736
+ }
4737
+ setCaptionLocale(showing);
4738
+ };
4739
+ return listenTextTracksChange(video.textTracks, onChange);
4740
+ }, [videoRef, active, src, liveCaptionsBaseUrl]);
4635
4741
  React9.useEffect(() => {
4636
4742
  const video = videoRef.current;
4637
- const track = nativeTrackRef.current;
4638
- if (!video || !track) return;
4639
- if (!(liveCaptionsBaseUrl || endedLiveReplay)) return;
4743
+ if (!video || captionLocale === "off" || !liveCaptionsBaseUrl) return;
4744
+ const track = findSubtitleTrack(video, captionLocale);
4745
+ if (!track) return;
4640
4746
  syncNativeCaptionCue(
4641
4747
  video,
4642
4748
  track,
4643
4749
  lastCaptionLines(joinCaptionTexts(captionStackCues))
4644
4750
  );
4645
- }, [captionStackCues, liveCaptionsBaseUrl, endedLiveReplay]);
4751
+ }, [captionStackCues, captionLocale, liveCaptionsBaseUrl]);
4646
4752
  function togglePlay() {
4647
4753
  const video = videoRef.current;
4648
4754
  if (!video) return;
@@ -4827,7 +4933,22 @@ function VideoPlayer({
4827
4933
  muted: isMuted,
4828
4934
  className: "livo-video-player-media h-full w-full bg-black object-contain",
4829
4935
  onClick: active ? togglePlay : void 0,
4830
- poster
4936
+ poster,
4937
+ children: !liveCaptionsBaseUrl ? CAPTION_LOCALES.map((locale) => {
4938
+ const url = captionsVttUrlFromPlayback(src, locale);
4939
+ if (!url) return null;
4940
+ return /* @__PURE__ */ jsx(
4941
+ "track",
4942
+ {
4943
+ kind: "subtitles",
4944
+ srcLang: locale,
4945
+ label: captionLocaleLabel(locale, t),
4946
+ src: url,
4947
+ onLoad: onSidecarTrackLoad
4948
+ },
4949
+ `${locale}:${url}`
4950
+ );
4951
+ }) : null
4831
4952
  }
4832
4953
  ),
4833
4954
  showPoster && /* @__PURE__ */ jsx(
@@ -4893,7 +5014,7 @@ function VideoPlayer({
4893
5014
  ] }),
4894
5015
  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
5016
  ] }),
4896
- (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && /* @__PURE__ */ jsx(
5017
+ (liveCaptionsBaseUrl || endedLiveReplay) && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsx(
4897
5018
  LiveCaptionStack,
4898
5019
  {
4899
5020
  cues: captionStackCues,
@@ -4901,7 +5022,7 @@ function VideoPlayer({
4901
5022
  variant: appleMobile ? "window" : "scroll"
4902
5023
  }
4903
5024
  ),
4904
- !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && /* @__PURE__ */ jsx(
5025
+ !liveCaptionsBaseUrl && !endedLiveReplay && activeCaption && captionLocale !== "off" && !webkitNativeFs && /* @__PURE__ */ jsx(
4905
5026
  "div",
4906
5027
  {
4907
5028
  className: "pointer-events-none absolute inset-x-0 bottom-16 z-[1] flex justify-center px-4",