Dough Git

clove/musicPlayer

web/player/src/player.ts

back to web/player/src

// Controller around a single <audio> element, with shuffle, repeat, volume.
// Works unchanged in a browser tab or a Tauri desktop webview — both give
// us a real HTMLAudioElement/localStorage, so there's no
// platform abstraction here beyond the injectable prefs storage.

import type { Song } from "@musicapp/shared";

import { Emitter } from "./events.ts";
import { createAudioElement, type MediaEl } from "./native-audio.ts";
import { localStoragePrefs, type Prefs, type PrefsStorage } from "./storage.ts";

export type RepeatMode = Prefs["repeat"];

type PlayerEvents = {
  change: [];
  trackchange: [Song];
  error: [string];
};

export class Player {
  readonly events = new Emitter<PlayerEvents>();

  // A real <audio> on web/desktop; a native Media3-backed shim inside the
  // Android shell (see native-audio.ts). The Player logic is identical either
  // way — only the backend differs.
  private audio: MediaEl = createAudioElement();
  private storage: PrefsStorage;
  private queue: Song[] = []; // playback order (shuffled or not)
  private originalOrder: Song[] = []; // order as given to playQueue, never shuffled
  private index = -1;
  private lastTrackChangeId: string | null = null;
  private playToken = 0; // bumped on every load(); see tryPlay()
  // Auto-recovery for a stream that drops mid-play (network blip, a transient
  // server hiccup, mobile dropping the media buffer). See recoverPlayback().
  private recoverAttempts = 0;
  private readonly maxRecoverAttempts = 4;
  private stallTimer: ReturnType<typeof setTimeout> | null = null;
  private prefs: Prefs;
  // iOS ignores HTMLMediaElement.volume (hardware-controlled). Detect so the
  // UI can hide the volume slider there.
  readonly volumeSupported = !isIOS();

  constructor(storage: PrefsStorage = localStoragePrefs) {
    this.storage = storage;
    this.prefs = { volume: 1, shuffle: false, repeat: "off", ...this.storage.load() };

    this.audio.volume = this.prefs.volume;
    // iOS needs these: inline playback + the element attached to the document.
    this.audio.preload = "metadata";
    this.audio.setAttribute("playsinline", "");
    this.audio.setAttribute("webkit-playsinline", "");
    this.audio.setAttribute("controlsList", "nodownload");
    if (typeof document !== "undefined") {
      this.audio.style.display = "none";
      // Only a real <audio> is a DOM node; the native shim isn't appended.
      if (this.audio instanceof HTMLElement) document.body.appendChild(this.audio);
    }

    this.audio.addEventListener("ended", () => this.onEnded());
    this.audio.addEventListener("timeupdate", () => this.events.emit("change"));
    this.audio.addEventListener("play", () => this.events.emit("change"));
    this.audio.addEventListener("pause", () => this.events.emit("change"));
    this.audio.addEventListener("volumechange", () => this.events.emit("change"));

    // Playback made progress -> the stream is healthy again: clear any pending
    // stall watchdog and reset the recovery counter.
    this.audio.addEventListener("playing", () => {
      this.recoverAttempts = 0;
      this.clearStallTimer();
    });
    this.audio.addEventListener("canplay", () => this.clearStallTimer());

    // `stalled`/`waiting` fire when the buffer starves. Usually the browser
    // recovers on its own, so we don't act immediately — we arm a watchdog and
    // only force a reload if it's still starved after a grace period.
    this.audio.addEventListener("stalled", () => this.armStallWatchdog());
    this.audio.addEventListener("waiting", () => this.armStallWatchdog());

    this.audio.addEventListener("error", () => {
      const err = this.audio.error;
      if (!err) return;
      // MEDIA_ERR_ABORTED (1) is us changing tracks on purpose — ignore.
      // NETWORK (2) / DECODE (3) / SRC_NOT_SUPPORTED (4, what a dropped or
      // 401'd stream often surfaces as) are recoverable: re-fetch and resume.
      if (err.code === 1) return;
      this.recoverPlayback();
    });

    this.setupMediaSession();
  }

  // Wire OS media controls (macOS media keys / Now Playing widget, Windows
  // SMTC, mobile lock-screen) to the player. Without this the hardware keys
  // don't reach the app at all.
  private setupMediaSession(): void {
    if (typeof navigator === "undefined" || !("mediaSession" in navigator)) return;
    const ms = navigator.mediaSession;
    try {
      ms.setActionHandler("play", () => this.tryPlay());
      ms.setActionHandler("pause", () => this.audio.pause());
      ms.setActionHandler("previoustrack", () => this.prev());
      ms.setActionHandler("nexttrack", () => this.next());
      ms.setActionHandler("seekto", (d) => {
        if (typeof d.seekTime === "number") this.seek(d.seekTime);
      });
    } catch {
      /* some actions aren't supported everywhere — ignore */
    }
    this.audio.addEventListener("play", () => {
      ms.playbackState = "playing";
    });
    this.audio.addEventListener("pause", () => {
      ms.playbackState = "paused";
    });
    this.audio.addEventListener("timeupdate", () => this.updatePositionState());
  }

  private updateMediaMetadata(song: Song): void {
    // Hand metadata to the native backend first (its OS notification needs it),
    // independently of whether the web MediaSession API is available. No-op on
    // a real <audio> element.
    this.audio.setMetadata?.({
      title: song.title,
      artist: song.artist,
      album: song.album ?? "",
      coverUrl: song.coverUrl ? absoluteUrl(song.coverUrl) : null,
      durationS: song.durationS,
    });

    if (typeof navigator === "undefined" || !("mediaSession" in navigator)) return;
    try {
      navigator.mediaSession.metadata = new MediaMetadata({
        title: song.title,
        artist: song.artist,
        album: song.album ?? "",
        artwork: song.coverUrl
          ? [{ src: absoluteUrl(song.coverUrl), sizes: "512x512", type: "image/jpeg" }]
          : [],
      });
    } catch {
      /* MediaMetadata unsupported — ignore */
    }
  }

  private updatePositionState(): void {
    if (typeof navigator === "undefined" || !("mediaSession" in navigator)) return;
    if (typeof navigator.mediaSession.setPositionState !== "function") return;
    const duration = this.audio.duration;
    if (!Number.isFinite(duration) || duration <= 0) return;
    try {
      navigator.mediaSession.setPositionState({
        duration,
        position: Math.min(this.audio.currentTime || 0, duration),
        playbackRate: this.audio.playbackRate || 1,
      });
    } catch {
      /* ignore invalid position states */
    }
  }

  get current(): Song | null {
    return this.queue[this.index] ?? null;
  }

  get playing(): boolean {
    return !this.audio.paused;
  }

  get volume(): number {
    return this.prefs.volume;
  }

  get shuffle(): boolean {
    return this.prefs.shuffle;
  }

  get repeat(): RepeatMode {
    return this.prefs.repeat;
  }

  get progress(): { current: number; duration: number } {
    return {
      current: this.audio.currentTime || 0,
      duration: this.audio.duration || 0,
    };
  }

  // Start a fresh queue. `startAt` indexes into `songs` as given; if shuffle is
  // on, that song plays first and the rest are shuffled after it.
  playQueue(songs: Song[], startAt = 0): void {
    this.originalOrder = songs.slice();
    if (this.prefs.shuffle) {
      const first = songs[startAt];
      const rest = songs.filter((_, i) => i !== startAt);
      this.queue = first ? [first, ...shuffleArray(rest)] : shuffleArray(rest);
      this.index = 0;
    } else {
      this.queue = songs.slice();
      this.index = startAt;
    }
    this.load();
  }

  toggle(): void {
    if (this.audio.paused) this.tryPlay();
    else this.audio.pause();
  }

  next(): void {
    if (this.index < this.queue.length - 1) {
      this.index++;
      this.load();
    } else if (this.prefs.repeat === "all" && this.queue.length > 0) {
      this.index = 0;
      this.load();
    }
  }

  prev(): void {
    // Restart the track if we're more than 3s in; otherwise go back one.
    if (this.audio.currentTime > 3) {
      this.audio.currentTime = 0;
      return;
    }
    if (this.index > 0) {
      this.index--;
      this.load();
    }
  }

  seek(seconds: number): void {
    this.audio.currentTime = seconds;
  }

  setVolume(v: number): void {
    this.prefs.volume = Math.min(1, Math.max(0, v));
    this.audio.volume = this.prefs.volume;
    this.save();
  }

  toggleShuffle(): void {
    this.prefs.shuffle = !this.prefs.shuffle;
    this.save();
    this.reshuffleQueue();
    this.events.emit("change");
  }

  cycleRepeat(): void {
    const order: RepeatMode[] = ["off", "all", "one"];
    const nextIdx = (order.indexOf(this.prefs.repeat) + 1) % order.length;
    this.prefs.repeat = order[nextIdx]!;
    this.save();
    this.events.emit("change");
  }

  private onEnded(): void {
    if (this.prefs.repeat === "one") {
      this.audio.currentTime = 0;
      this.tryPlay();
      const song = this.current;
      if (song) this.events.emit("trackchange", song); // same track, but a fresh listen
      return;
    }
    this.next();
  }

  // Re-derives the playback queue for the current shuffle preference. The
  // song that's currently playing stays right where it is (nothing skips or
  // restarts) — only the order of the *other* songs changes. This is what
  // was missing before: toggling the shuffle button used to only affect the
  // *next* playQueue() call, so it looked like shuffle "did nothing" if you
  // toggled it mid-playback.
  private reshuffleQueue(): void {
    const current = this.current;
    if (!current || this.originalOrder.length === 0) return;

    if (this.prefs.shuffle) {
      const rest = this.originalOrder.filter((s) => s.id !== current.id);
      this.queue = [current, ...shuffleArray(rest)];
    } else {
      this.queue = this.originalOrder.slice();
    }
    const idx = this.queue.findIndex((s) => s.id === current.id);
    this.index = idx >= 0 ? idx : 0;
  }

  private load(): void {
    const song = this.current;
    if (!song) return;
    const changed = song.id !== this.lastTrackChangeId;

    // Fresh track: drop any recovery state carried over from the last one.
    this.recoverAttempts = 0;
    this.clearStallTimer();

    // Each load supersedes any in-flight play(). This token lets tryPlay()
    // tell "my play() was interrupted because a newer track started" (an
    // expected AbortError we should ignore) apart from a real failure.
    const token = ++this.playToken;

    this.audio.src = song.streamUrl;
    this.audio.volume = this.prefs.volume;
    this.updateMediaMetadata(song);
    // NB: deliberately NO explicit audio.load() here. Assigning .src already
    // kicks off loading; calling .load() in the same tick as play() is the
    // canonical cause of "The play() request was interrupted by a call to
    // load()" — the AbortError users were seeing on every track change.
    this.tryPlay(token);

    this.events.emit("change");
    if (changed) {
      this.lastTrackChangeId = song.id;
      this.events.emit("trackchange", song);
    }
  }

  // Snapshot of the current session, for resume-after-reload.
  snapshot(): { ids: string[]; index: number; position: number } | null {
    if (!this.current) return null;
    return {
      ids: this.queue.map((s) => s.id),
      index: this.index,
      position: this.audio.currentTime || 0,
    };
  }

  // Load a saved queue WITHOUT autoplaying; seek once metadata is ready.
  restore(songs: Song[], index: number, position: number): void {
    if (!songs.length) return;
    this.queue = songs;
    this.originalOrder = songs.slice();
    this.index = Math.min(Math.max(index, 0), songs.length - 1);
    const song = this.current;
    if (!song) return;
    this.audio.src = song.streamUrl;
    this.updateMediaMetadata(song);
    this.audio.load();
    const onMeta = () => {
      try {
        this.audio.currentTime = position;
      } catch {
        /* ignore */
      }
      this.audio.removeEventListener("loadedmetadata", onMeta);
      this.events.emit("change");
    };
    this.audio.addEventListener("loadedmetadata", onMeta);
    this.events.emit("change");
  }

  // Play the current element and surface only *real* failures. `token` ties
  // this attempt to the load() that started it: if a newer track has since
  // loaded, the play() promise rejects with AbortError and we ignore it (it
  // was superseded on purpose). A genuine "element wasn't ready yet" abort is
  // retried once on `canplay`; autoplay-policy blocks get a friendlier note.
  private tryPlay(token = this.playToken, retried = false): void {
    const p = this.audio.play();
    if (!p || typeof p.catch !== "function") return;

    p.catch((err: unknown) => {
      // A newer track load already moved on — this rejection is expected.
      if (token !== this.playToken) return;

      const name = (err as { name?: string })?.name ?? "Error";

      if (name === "AbortError") {
        // The element wasn't ready when play() was called. Retry once as soon
        // as it can play, still guarded by the token so a later skip wins.
        if (retried) return;
        this.audio.addEventListener(
          "canplay",
          () => this.tryPlay(token, true),
          { once: true },
        );
        return;
      }

      if (name === "NotAllowedError") {
        // Autoplay policy blocked us (no user gesture yet) — nothing broke.
        this.events.emit("error", "Tap play to start");
        return;
      }

      this.events.emit("error", `Can't play (${name})`);
    });
  }

  // Arm a one-shot watchdog for a stall. If we're still supposed to be playing
  // but the buffer is starved after the grace period, force a recovery. No-op
  // if a timer is already pending (repeated stalled/waiting events coalesce).
  private armStallWatchdog(): void {
    if (this.stallTimer) return;
    this.stallTimer = setTimeout(() => {
      this.stallTimer = null;
      // readyState < HAVE_FUTURE_DATA (3) means it can't keep playing.
      if (!this.audio.paused && this.audio.readyState < 3) {
        this.recoverPlayback();
      }
    }, 8000);
  }

  private clearStallTimer(): void {
    if (this.stallTimer !== null) {
      clearTimeout(this.stallTimer);
      this.stallTimer = null;
    }
  }

  // Re-fetch the current stream and resume from where it dropped. Bounded and
  // backed off so a genuinely dead source can't loop forever, and guarded by
  // playToken so a track change during the wait cancels the retry cleanly.
  private recoverPlayback(): void {
    const song = this.current;
    if (!song) return;

    this.clearStallTimer();

    if (this.recoverAttempts >= this.maxRecoverAttempts) {
      this.recoverAttempts = 0;
      this.events.emit("error", "Playback stopped — tap play to retry");
      return;
    }

    const attempt = ++this.recoverAttempts;
    const wasPlaying = !this.audio.paused;
    const position = this.audio.currentTime || 0;
    const delay = Math.min(500 * 2 ** (attempt - 1), 4000); // 0.5s,1s,2s,4s
    const token = ++this.playToken; // supersede any in-flight play()

    setTimeout(() => {
      if (token !== this.playToken) return; // a real track change won the race
      // Reassigning src forces a fresh request for the same bytes.
      this.audio.src = song.streamUrl;
      const onMeta = () => {
        this.audio.removeEventListener("loadedmetadata", onMeta);
        try {
          if (position > 0 && Number.isFinite(this.audio.duration)) {
            this.audio.currentTime = position;
          }
        } catch {
          /* seeking may be unavailable until more buffers in — best effort */
        }
        if (wasPlaying) this.tryPlay(token);
      };
      this.audio.addEventListener("loadedmetadata", onMeta, { once: true });
      this.audio.load();
    }, delay);
  }

  private save(): void {
    this.storage.save(this.prefs);
  }
}

// Resolve a possibly-relative URL (e.g. "/api/songs/x/cover") to absolute, for
// MediaSession artwork which the OS fetches out-of-context.
function absoluteUrl(u: string): string {
  try {
    return new URL(u, typeof location !== "undefined" ? location.href : undefined).href;
  } catch {
    return u;
  }
}

function isIOS(): boolean {
  if (typeof navigator === "undefined") return false;
  const ua = navigator.userAgent;
  // iPhone/iPod/iPad, plus iPadOS 13+ which reports as Mac with touch.
  return (
    /iP(hone|od|ad)/.test(ua) ||
    (ua.includes("Macintosh") && "ontouchend" in document)
  );
}

function shuffleArray<T>(arr: T[]): T[] {
  const a = arr.slice();
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j]!, a[i]!];
  }
  return a;
}

export function formatTime(seconds: number): string {
  if (!Number.isFinite(seconds)) return "0:00";
  const m = Math.floor(seconds / 60);
  const s = Math.floor(seconds % 60);
  return `${m}:${s.toString().padStart(2, "0")}`;
}