@geekapps/silo-elements-nextjs 0.3.21 → 0.3.22

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
@@ -1550,9 +1550,8 @@ function deviceSupportsHdr() {
1550
1550
  function deviceSupportsOpus() {
1551
1551
  if (typeof window === "undefined") return false;
1552
1552
  try {
1553
- if (typeof MediaSource !== "undefined" && MediaSource.isTypeSupported) {
1554
- if (MediaSource.isTypeSupported('audio/mp4; codecs="opus"')) return true;
1555
- }
1553
+ if (typeof MediaSource !== "undefined" && MediaSource.isTypeSupported)
1554
+ return MediaSource.isTypeSupported('audio/mp4; codecs="opus"');
1556
1555
  const a = document.createElement("audio");
1557
1556
  return a.canPlayType('audio/ogg; codecs="opus"') !== "" || a.canPlayType('video/mp4; codecs="opus"') !== "";
1558
1557
  } catch {
@@ -2403,6 +2402,12 @@ function Video({
2403
2402
  const Hls = HlsModule.default;
2404
2403
  console.debug("[Silo/hls] Hls.isSupported()=", Hls.isSupported());
2405
2404
  if (Hls.isSupported()) {
2405
+ let audioCodecPriority2 = function(codec) {
2406
+ const c = codec.toLowerCase();
2407
+ const idx = AUDIO_CODEC_PRIORITY.findIndex((k) => c.includes(k));
2408
+ return idx === -1 ? 0 : idx;
2409
+ };
2410
+ var audioCodecPriority = audioCodecPriority2;
2406
2411
  const hls = new Hls({
2407
2412
  enableWorker: true,
2408
2413
  // Buffer ~20s ahead (≈1/6 of a 2min video). maxMaxBufferLength caps the
@@ -2464,32 +2469,36 @@ function Video({
2464
2469
  if (pinnedAudio >= 0) hls.audioTrack = pinnedAudio;
2465
2470
  setIsLoading(false);
2466
2471
  });
2472
+ const AUDIO_CODEC_PRIORITY = ["opus", "mp4a", "aac", "ac-3", "ac3", "dts", "ec-3", "eac3", "dts-hd", "dtshd", "truehd"];
2473
+ let audioUpgradeDone = false;
2474
+ let mappedAudioTracks = [];
2467
2475
  hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, (_, data) => {
2468
2476
  const tracks = data.audioTracks ?? [];
2469
2477
  console.debug("[Silo/hls] AUDIO_TRACKS_UPDATED", tracks.length);
2470
2478
  if (tracks.length > 0) {
2471
- const mapped = tracks.map((t, i) => {
2479
+ mappedAudioTracks = tracks.map((t, i) => {
2472
2480
  const codec = (t.attrs?.CODECS ?? t.codecSet ?? "").split(",")[0]?.trim() ?? "";
2473
2481
  const supported = deviceSupportsAudioCodec(codec);
2474
2482
  const label = friendlyAudioLabel(t.name, t.lang, codec, t.channels ?? t.attrs?.CHANNELS);
2475
- return { id: i, label, supported };
2483
+ return { id: i, label, supported, codec };
2476
2484
  });
2477
- const currentTrackIdx = hls.audioTrack ?? 0;
2478
- if (mapped[currentTrackIdx] && !mapped[currentTrackIdx].supported) {
2479
- const firstSupported = mapped.findIndex((t) => t.supported);
2480
- if (firstSupported >= 0) {
2481
- console.debug("[Silo/hls] current audio track unsupported \u2014 switching to track", firstSupported);
2482
- hls.audioTrack = firstSupported;
2483
- if (pinnedAudio === -1) setSelectedAudio(firstSupported);
2485
+ if (pinnedAudio === -1) {
2486
+ const opusTrack = mappedAudioTracks.find((t) => t.supported && /opus/i.test(t.codec));
2487
+ const fallback = mappedAudioTracks.find((t) => t.supported);
2488
+ const startTrack = opusTrack ?? fallback;
2489
+ if (startTrack) {
2490
+ hls.audioTrack = startTrack.id;
2491
+ setSelectedAudio(startTrack.id);
2492
+ console.debug("[Silo/hls] starting with audio track", startTrack.id, startTrack.codec);
2484
2493
  } else {
2485
- console.warn("[Silo/hls] no supported audio track found \u2014 disabling separate audio");
2494
+ console.warn("[Silo/hls] no supported audio track found \u2014 disabling");
2486
2495
  try {
2487
2496
  hls.audioTrack = -1;
2488
2497
  } catch {
2489
2498
  }
2490
2499
  }
2491
2500
  }
2492
- if (mapped.length > 1) setAudioTracks(mapped);
2501
+ if (mappedAudioTracks.length > 1) setAudioTracks(mappedAudioTracks.map(({ id, label, supported }) => ({ id, label, supported })));
2493
2502
  }
2494
2503
  if (pinnedAudio >= 0 && hls.audioTrack !== pinnedAudio) {
2495
2504
  hls.audioTrack = pinnedAudio;
@@ -2521,6 +2530,16 @@ function Video({
2521
2530
  });
2522
2531
  hls.on(Hls.Events.FRAG_LOADED, (_, data) => {
2523
2532
  console.debug("[Silo/hls] FRAG_LOADED", data.frag?.type, data.frag?.sn);
2533
+ if (!audioUpgradeDone && data.frag?.type === "audio" && data.frag?.sn === 0 && pinnedAudio === -1 && mappedAudioTracks.length > 1) {
2534
+ audioUpgradeDone = true;
2535
+ const supported = mappedAudioTracks.filter((t) => t.supported);
2536
+ const best = supported.reduce((a, b) => audioCodecPriority2(b.codec) > audioCodecPriority2(a.codec) ? b : a, supported[0]);
2537
+ if (best && best.id !== hls.audioTrack) {
2538
+ console.debug("[Silo/hls] upgrading audio track", hls.audioTrack, "\u2192", best.id, best.codec);
2539
+ hls.audioTrack = best.id;
2540
+ setSelectedAudio(best.id);
2541
+ }
2542
+ }
2524
2543
  });
2525
2544
  let mediaErrorAttempts = 0;
2526
2545
  let audioAppendErrors = 0;
@@ -2533,8 +2552,8 @@ function Video({
2533
2552
  const isAudioBufError = fragType === "audio" && (data.details === Hls.ErrorDetails.BUFFER_APPEND_ERROR || data.details === Hls.ErrorDetails.BUFFER_APPENDING_ERROR);
2534
2553
  if (isAudioBufError) {
2535
2554
  audioAppendErrors += 1;
2536
- if (audioAppendErrors >= 3) {
2537
- console.warn("[Silo/hls] repeated audio buffer errors \u2014 disabling separate audio tracks");
2555
+ if (audioAppendErrors >= 1) {
2556
+ console.warn("[Silo/hls] audio buffer append error \u2014 disabling separate audio tracks");
2538
2557
  setAudioTracks([]);
2539
2558
  try {
2540
2559
  hls.audioTrack = -1;