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**\n * Structured error with a machine-readable code and human-readable\n * recovery hint. Consumers can switch on `error.code` for programmatic\n * handling and show `error.recovery` in UI.\n *\n * All codes use the `ERR_AVBRIDGE_` prefix to avoid collisions.\n */\nexport class AvbridgeError extends Error {\n  override name = \"AvbridgeError\";\n\n  constructor(\n    /** Machine-readable error code. */\n    public readonly code: string,\n    message: string,\n    /** Human-readable recovery suggestion. */\n    public readonly recovery?: string,\n    options?: ErrorOptions,\n  ) {\n    super(message, options);\n  }\n}\n\n// ── Error codes ────────────────────────────────────────────────────────\n\n// Probe\nexport const ERR_PROBE_FAILED = \"ERR_AVBRIDGE_PROBE_FAILED\";\nexport const ERR_PROBE_UNKNOWN_CONTAINER = \"ERR_AVBRIDGE_PROBE_UNKNOWN_CONTAINER\";\nexport const ERR_PROBE_FETCH_FAILED = \"ERR_AVBRIDGE_PROBE_FETCH_FAILED\";\n\n// Codec / strategy\nexport const ERR_CODEC_NOT_SUPPORTED = \"ERR_AVBRIDGE_CODEC_NOT_SUPPORTED\";\nexport const ERR_STRATEGY_FAILED = \"ERR_AVBRIDGE_STRATEGY_FAILED\";\nexport const ERR_ALL_STRATEGIES_EXHAUSTED = \"ERR_AVBRIDGE_ALL_STRATEGIES_EXHAUSTED\";\n\n// Player lifecycle\nexport const ERR_PLAYER_NOT_READY = \"ERR_AVBRIDGE_PLAYER_NOT_READY\";\n\n// Transport / network\nexport const ERR_RANGE_NOT_SUPPORTED = \"ERR_AVBRIDGE_RANGE_NOT_SUPPORTED\";\nexport const ERR_FETCH_FAILED = \"ERR_AVBRIDGE_FETCH_FAILED\";\n\n// libav\nexport const ERR_LIBAV_NOT_REACHABLE = \"ERR_AVBRIDGE_LIBAV_NOT_REACHABLE\";\n\n// MSE\nexport const ERR_MSE_NOT_SUPPORTED = \"ERR_AVBRIDGE_MSE_NOT_SUPPORTED\";\nexport const ERR_MSE_CODEC_NOT_SUPPORTED = \"ERR_AVBRIDGE_MSE_CODEC_NOT_SUPPORTED\";\n\n// Transcode\nexport const ERR_TRANSCODE_ABORTED = \"ERR_AVBRIDGE_TRANSCODE_ABORTED\";\nexport const ERR_TRANSCODE_UNSUPPORTED_COMBO = \"ERR_AVBRIDGE_TRANSCODE_UNSUPPORTED_COMBO\";\nexport const ERR_TRANSCODE_DECODE = \"ERR_AVBRIDGE_TRANSCODE_DECODE\";\nexport const ERR_CONTAINER_NOT_SUPPORTED = \"ERR_AVBRIDGE_CONTAINER_NOT_SUPPORTED\";\n","import type { ContainerKind, MediaInput, TransportConfig } from \"../types.js\";\nimport { mergeFetchInit, fetchWith } from \"./transport.js\";\nimport { AvbridgeError, ERR_PROBE_FETCH_FAILED } from \"../errors.js\";\n\n/**\n * Bytes needed by the sniffer to identify every container we recognize.\n * MPEG-TS needs the most: a sync byte at offset 0 *and* offset 188 (one TS\n * packet apart). Allow a little extra for the M2TS variant (offset 4/192).\n */\nconst SNIFF_BYTES_NEEDED = 380;\n\n/**\n * Bytes to fetch from a URL during the initial sniff. We grab a slightly\n * larger range than `SNIFF_BYTES_NEEDED` so the cache has some headroom for\n * the demuxer's first read after sniffing, in case it wants to look at\n * a few extra bytes (e.g. mp4 ftyp + first moov box).\n */\nconst URL_SNIFF_RANGE_BYTES = 32 * 1024;\n\n/**\n * `NormalizedSource` is a discriminated union: every consumer (probe,\n * strategies) decides what to do based on `kind`. URL sources are NOT\n * fetched eagerly; we only do a Range request for the first ~32 KB so the\n * sniffer has bytes to look at. The strategies are then handed the URL\n * directly so they can stream the rest via Range requests.\n *\n * For File / Blob / ArrayBuffer / Uint8Array sources, the bytes are\n * already in memory, so we wrap them as a `blob` variant.\n */\nexport type NormalizedSource =\n  | {\n      kind: \"blob\";\n      blob: Blob;\n      name?: string;\n      byteLength: number;\n      original: MediaInput;\n    }\n  | {\n      kind: \"url\";\n      url: string;\n      /** Bytes pulled via Range request for the sniffer. NOT the full file. */\n      sniffBytes: Uint8Array;\n      name?: string;\n      /** Total file size from Content-Length / Content-Range. May be undefined. */\n      byteLength: number | undefined;\n      original: MediaInput;\n    };\n\n/** True if this source carries the entire file's bytes (vs. streaming). */\nexport function isInMemorySource(source: NormalizedSource): source is Extract<NormalizedSource, { kind: \"blob\" }> {\n  return source.kind === \"blob\";\n}\n\n\n/**\n * Normalize a `MediaInput` for the probe + strategy layers. **Does not**\n * download URL sources in full — only fetches the first ~32 KB via a\n * Range request, which is enough for the sniffer to identify the\n * container. The strategies are then expected to stream the rest via\n * mediabunny's `UrlSource` (Range requests, prefetch, parallelism, cache).\n *\n * For non-URL inputs, the bytes are already in memory and we just wrap them.\n */\nexport async function normalizeSource(\n  source: MediaInput,\n  transport?: TransportConfig,\n): Promise<NormalizedSource> {\n  if (source instanceof File) {\n    return {\n      kind: \"blob\",\n      blob: source,\n      name: source.name,\n      byteLength: source.size,\n      original: source,\n    };\n  }\n  if (source instanceof Blob) {\n    return { kind: \"blob\", blob: source, byteLength: source.size, original: source };\n  }\n  if (source instanceof ArrayBuffer) {\n    const blob = new Blob([source]);\n    return { kind: \"blob\", blob, byteLength: blob.size, original: source };\n  }\n  if (source instanceof Uint8Array) {\n    const blob = new Blob([source as BlobPart]);\n    return { kind: \"blob\", blob, byteLength: blob.size, original: source };\n  }\n  if (typeof source === \"string\" || source instanceof URL) {\n    const url = source instanceof URL ? source.toString() : source;\n    return await fetchUrlForSniff(url, source, transport);\n  }\n  throw new TypeError(\"unsupported source type\");\n}\n\n/**\n * Fetch the first ~32 KB of a URL via a Range request. Falls back to a\n * full GET if the server doesn't support range requests, but in that case\n * we only read the first 32 KB and abort the rest of the response so we\n * don't accidentally buffer a large file.\n */\nasync function fetchUrlForSniff(\n  url: string,\n  originalSource: MediaInput,\n  transport?: TransportConfig,\n): Promise<NormalizedSource> {\n  const name = url.split(\"/\").pop()?.split(\"?\")[0] ?? undefined;\n  const doFetch = fetchWith(transport);\n\n  // First attempt: Range request for the sniff window.\n  let res: Response;\n  try {\n    res = await doFetch(url, mergeFetchInit(transport?.requestInit, {\n      headers: { Range: `bytes=0-${URL_SNIFF_RANGE_BYTES - 1}` },\n    })!);\n  } catch (err) {\n    throw new AvbridgeError(\n      ERR_PROBE_FETCH_FAILED,\n      `Failed to fetch source ${url}: ${(err as Error).message}`,\n      \"Check that the URL is reachable and CORS is configured. If the source requires authentication, pass requestInit with credentials/headers.\",\n    );\n  }\n  if (!res.ok && res.status !== 206) {\n    throw new AvbridgeError(\n      ERR_PROBE_FETCH_FAILED,\n      `Failed to fetch source ${url}: ${res.status} ${res.statusText}`,\n      res.status === 403 || res.status === 401\n        ? \"The server rejected the request. Pass requestInit with the required Authorization header or credentials.\"\n        : \"Check that the URL is correct and the server is reachable.\",\n    );\n  }\n\n  // Determine the total file size from Content-Range (preferred) or Content-Length.\n  let byteLength: number | undefined;\n  const contentRange = res.headers.get(\"content-range\");\n  if (contentRange) {\n    // \"bytes 0-32767/12345678\" — parse the part after the slash\n    const m = contentRange.match(/\\/(\\d+)$/);\n    if (m) byteLength = parseInt(m[1], 10);\n  }\n  if (byteLength === undefined) {\n    const cl = res.headers.get(\"content-length\");\n    if (cl) {\n      const n = parseInt(cl, 10);\n      if (Number.isFinite(n)) {\n        // If the server returned 200 (full body), Content-Length is the\n        // FILE size. If 206 (partial), it's the chunk size — only use it\n        // as a total if no Content-Range was present (server doesn't do\n        // ranges) AND the full response is smaller than our sniff window.\n        if (res.status === 200) byteLength = n;\n        else if (res.status === 206 && !contentRange) byteLength = n;\n      }\n    }\n  }\n\n  // Read the sniff bytes. If the server ignored the Range header and is\n  // streaming the full file, only read the first window and let the rest\n  // be GC'd. We use a reader so we can stop early.\n  const reader = res.body?.getReader();\n  if (!reader) {\n    // No streamed body (some test environments). Fall back to .arrayBuffer()\n    // and slice — this might pull more than we wanted, but only for the\n    // initial sniff, not the full file.\n    const buf = new Uint8Array(await res.arrayBuffer());\n    const sniffBytes = buf.slice(0, URL_SNIFF_RANGE_BYTES);\n    return { kind: \"url\", url, sniffBytes, name, byteLength, original: originalSource };\n  }\n\n  const chunks: Uint8Array[] = [];\n  let collected = 0;\n  while (collected < URL_SNIFF_RANGE_BYTES) {\n    const { done, value } = await reader.read();\n    if (done) break;\n    chunks.push(value);\n    collected += value.byteLength;\n  }\n  // Cancel the response so we don't keep downloading.\n  await reader.cancel().catch(() => { /* ignore */ });\n\n  // Concatenate up to URL_SNIFF_RANGE_BYTES.\n  const total = Math.min(collected, URL_SNIFF_RANGE_BYTES);\n  const sniffBytes = new Uint8Array(total);\n  let offset = 0;\n  for (const chunk of chunks) {\n    if (offset >= total) break;\n    const room = total - offset;\n    sniffBytes.set(chunk.subarray(0, Math.min(chunk.byteLength, room)), offset);\n    offset += chunk.byteLength;\n  }\n\n  return { kind: \"url\", url, sniffBytes, name, byteLength, original: originalSource };\n}\n\n/**\n * Identify the container family from a small byte buffer. Used by the\n * probe layer for both file (Blob → first 380 bytes) and URL (Range\n * request → first 32 KB) inputs.\n *\n * Sniffing intentionally does not trust file extensions.\n */\nexport function sniffContainerFromBytes(head: Uint8Array): ContainerKind {\n  // MPEG-TS: sync byte 0x47 every 188 bytes. Verify at least two sync\n  // bytes in the right places to avoid false positives. Some captures\n  // start with a few junk bytes — also try offsets 4 and 192 (M2TS).\n  if (head.length >= 376 && head[0] === 0x47 && head[188] === 0x47) {\n    return \"mpegts\";\n  }\n  if (head.length >= 380 && head[4] === 0x47 && head[192] === 0x47) {\n    return \"mpegts\"; // M2TS — 4-byte timestamp prefix per packet\n  }\n  // RIFF....AVI  →  AVI\n  if (\n    head[0] === 0x52 && head[1] === 0x49 && head[2] === 0x46 && head[3] === 0x46 &&\n    head[8] === 0x41 && head[9] === 0x56 && head[10] === 0x49\n  ) return \"avi\";\n  // RIFF....WAVE → WAV\n  if (\n    head[0] === 0x52 && head[1] === 0x49 && head[2] === 0x46 && head[3] === 0x46 &&\n    head[8] === 0x57 && head[9] === 0x41 && head[10] === 0x56 && head[11] === 0x45\n  ) return \"wav\";\n  // EBML start: 1A 45 DF A3 → MKV/WebM. Distinguish later via DocType.\n  if (head[0] === 0x1a && head[1] === 0x45 && head[2] === 0xdf && head[3] === 0xa3) {\n    return \"mkv\";\n  }\n  // ftyp at offset 4 → MP4 family\n  if (head[4] === 0x66 && head[5] === 0x74 && head[6] === 0x79 && head[7] === 0x70) {\n    // brand at bytes 8..11\n    const brand = String.fromCharCode(head[8], head[9], head[10], head[11]);\n    if (brand.startsWith(\"qt\")) return \"mov\";\n    return \"mp4\";\n  }\n  // ASF / WMV: 30 26 B2 75 8E 66 CF 11\n  if (\n    head[0] === 0x30 && head[1] === 0x26 && head[2] === 0xb2 && head[3] === 0x75 &&\n    head[4] === 0x8e && head[5] === 0x66 && head[6] === 0xcf && head[7] === 0x11\n  ) return \"asf\";\n  // FLV: 46 4C 56\n  if (head[0] === 0x46 && head[1] === 0x4c && head[2] === 0x56) return \"flv\";\n  // RealMedia (.rm / .rmvb): \".RMF\" — 2E 52 4D 46\n  if (head[0] === 0x2e && head[1] === 0x52 && head[2] === 0x4d && head[3] === 0x46) {\n    return \"rm\";\n  }\n  // OggS: 4F 67 67 53\n  if (head[0] === 0x4f && head[1] === 0x67 && head[2] === 0x67 && head[3] === 0x53) return \"ogg\";\n  // FLAC: 66 4C 61 43\n  if (head[0] === 0x66 && head[1] === 0x4c && head[2] === 0x61 && head[3] === 0x43) return \"flac\";\n  // ID3v2: 49 44 33  → MP3 (with id3)\n  if (head[0] === 0x49 && head[1] === 0x44 && head[2] === 0x33) return \"mp3\";\n  // MPEG audio frame sync: FF Fx\n  if (head[0] === 0xff && (head[1] & 0xe0) === 0xe0) {\n    // ADTS: FF F1 / FF F9\n    if ((head[1] & 0xf6) === 0xf0) return \"adts\";\n    return \"mp3\";\n  }\n  return \"unknown\";\n}\n\n/**\n * Convenience: sniff a `NormalizedSource` regardless of kind. For URL\n * sources, uses the pre-fetched `sniffBytes`. For blob sources, reads the\n * first 380 bytes.\n */\nexport async function sniffNormalizedSource(source: NormalizedSource): Promise<ContainerKind> {\n  if (source.kind === \"url\") {\n    return sniffContainerFromBytes(source.sniffBytes);\n  }\n  const buf = await readBlobBytes(source.blob, SNIFF_BYTES_NEEDED);\n  return sniffContainerFromBytes(new Uint8Array(buf));\n}\n\n/**\n * Backwards-compatible wrapper for code that still passes a Blob directly.\n * Prefer `sniffNormalizedSource` going forward.\n */\nexport async function sniffContainer(blob: Blob): Promise<ContainerKind> {\n  const buf = await readBlobBytes(blob, SNIFF_BYTES_NEEDED);\n  return sniffContainerFromBytes(new Uint8Array(buf));\n}\n\n/**\n * Read up to `limit` bytes from a Blob. Tries `Blob.arrayBuffer()` first\n * (modern browsers), then falls back to `FileReader` (works under jsdom).\n */\nasync function readBlobBytes(blob: Blob, limit: number): Promise<ArrayBuffer> {\n  const slice = blob.slice(0, limit);\n  if (typeof (slice as Blob & { arrayBuffer?: () => Promise<ArrayBuffer> }).arrayBuffer === \"function\") {\n    try {\n      return await (slice as Blob & { arrayBuffer: () => Promise<ArrayBuffer> }).arrayBuffer();\n    } catch {\n      /* fall through to FileReader */\n    }\n  }\n  return new Promise((resolve, reject) => {\n    const reader = new FileReader();\n    reader.onload = () => resolve(reader.result as ArrayBuffer);\n    reader.onerror = () => reject(reader.error ?? new Error(\"FileReader failed\"));\n    reader.readAsArrayBuffer(slice);\n  });\n}\n"]}