camera_profiles.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Per-printer-model camera tuning knobs.
  2. Bambuddy talks to multiple Bambu Lab printer models that all expose a
  3. camera but in subtly different ways:
  4. - **Chamber image** (port 6000, proprietary binary protocol) — A1, A1
  5. Mini, P1P, P1S. Frame pacing and TLS quirks are firmware-driven and
  6. don't go through ffmpeg.
  7. - **RTSPS** (port 322) — X1 series, X2D, H2 series, P2S. Wrapped by a
  8. local TLS proxy + ffmpeg to MJPEG.
  9. The RTSPS path used to live with hard-coded module constants in
  10. ``camera.py``: a single ``-probesize 32 -analyzeduration 0`` tuned for
  11. X1/H2 fast startup. That breaks the P2S on firmware 01.02.00.00, whose
  12. RTSP keyframe pacing is slow enough that ffmpeg can't lock onto the
  13. stream within 32 bytes and gives up with "not enough frames to estimate
  14. rate" (#1395 follow-up — Tschipel's reproduction).
  15. This module replaces those module constants with per-model
  16. :class:`CameraProfile` entries. Defaults match the historical pre-fix
  17. behaviour, so existing models (X1, H2, X2D, X1E) keep their fast-
  18. startup tuning unchanged. Quirky models override the relevant fields
  19. only — the P2S entry below is the first example.
  20. Adding a new model's quirk is a config edit (an entry in ``_PROFILES``
  21. plus the alias for its internal SSDP code if needed), not another
  22. hard-coded global constant.
  23. """
  24. from __future__ import annotations
  25. from dataclasses import dataclass, field
  26. @dataclass(frozen=True)
  27. class CameraProfile:
  28. """Tuning knobs for one printer model's camera path.
  29. All defaults reflect the historical X1/H2 behaviour (fast startup,
  30. minimal probing). Models with quirky firmware override individual
  31. fields rather than re-defining the whole profile.
  32. """
  33. # --- RTSPS / ffmpeg path -------------------------------------------------
  34. # ffmpeg's `-probesize` (bytes). Smaller = lower startup latency but
  35. # less margin to lock onto a stream whose first keyframe is delayed
  36. # or whose container metadata is incomplete. P2S 01.02.00.00 needs a
  37. # full MB to lock; X1/H2 lock within ~32 bytes.
  38. probesize: int = 32
  39. # ffmpeg's `-analyzeduration` (microseconds). 0 = skip format
  40. # analysis entirely. Same trade-off as probesize.
  41. analyzeduration: int = 0
  42. # Max consecutive ffmpeg respawns when the printer drops the RTSP
  43. # session mid-stream. Some firmwares cut the stream after a few
  44. # seconds (originally noted on P2S), so we transparently respawn
  45. # to keep the MJPEG client alive.
  46. rtsp_reconnect_max: int = 30
  47. # Seconds between ffmpeg respawn attempts.
  48. rtsp_reconnect_delay: float = 0.2
  49. # --- Extra ffmpeg input args ---------------------------------------------
  50. # Hook for future per-model knobs (e.g. `-fflags` overrides) without
  51. # changing the dataclass shape. Tuple, not list, so the dataclass
  52. # stays hashable / frozen-friendly.
  53. extra_ffmpeg_input_args: tuple[str, ...] = field(default_factory=tuple)
  54. # ---------------------------------------------------------------------------
  55. # Profile registry
  56. # ---------------------------------------------------------------------------
  57. # Default profile = historical X1/H2 fast-startup behaviour. Used for
  58. # every RTSP-capable model that doesn't have an entry in ``_PROFILES``.
  59. DEFAULT_PROFILE = CameraProfile()
  60. # Per-model overrides. Keys are uppercase display names (e.g. "P2S")
  61. # AFTER alias normalisation, so internal SSDP codes ("N7") resolve via
  62. # ``_MODEL_ALIASES`` below.
  63. _PROFILES: dict[str, CameraProfile] = {
  64. # P2S firmware 01.02.00.00 RTSP keyframe pacing is slow enough that
  65. # ffmpeg's "32-byte probe + zero analyze" combo can't estimate the
  66. # frame rate. ffmpeg's own stderr literally says "consider increasing
  67. # probesize" (#1395 follow-up).
  68. "P2S": CameraProfile(
  69. probesize=1_000_000,
  70. analyzeduration=500_000,
  71. ),
  72. }
  73. # SSDP internal codes that should resolve to a display-name profile.
  74. # Display-name lookup is the canonical path; this just lets the camera
  75. # code pass through whatever ``Printer.model`` carries without each
  76. # call site needing to know the code→name map.
  77. _MODEL_ALIASES: dict[str, str] = {
  78. "N7": "P2S", # P2S internal SSDP code
  79. }
  80. def get_camera_profile(model: str | None) -> CameraProfile:
  81. """Return the :class:`CameraProfile` for *model*, or the default.
  82. ``model`` can be either a display name (e.g. ``"P2S"``) or an
  83. internal SSDP code (e.g. ``"N7"``). Unknown models fall back to
  84. :data:`DEFAULT_PROFILE` so the camera path is never blocked on a
  85. missing entry.
  86. """
  87. if not model:
  88. return DEFAULT_PROFILE
  89. key = model.upper().strip()
  90. key = _MODEL_ALIASES.get(key, key)
  91. return _PROFILES.get(key, DEFAULT_PROFILE)