From 48eb62bddcd622846a4df28cdd27f5ce20bb405b Mon Sep 17 00:00:00 2001 From: gx Date: Thu, 21 May 2026 20:44:50 +0100 Subject: [PATCH] =?UTF-8?q?controller:=20UI-toggle=20audio=20output=20+=20?= =?UTF-8?q?astreamselect=20decouple=20(Phase=207=20=D0=92=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B0=D0=BD=D1=82=202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Web UI checkbox в Audio source карточке: «вывод аудио в стрим». Off → TV получает silence + downstream audio chain isolated от live-audio RTSP (нестабильный sidecar поток больше не блокирует video pipeline). Изменения: state.InstanceState: + audio_output_enabled (default True) config.InstanceCfg: + audio_output_volume_target (default volume@output_audio) + audio_input_select_target (default astreamselect@audio_input) dispatch.set_audio_output_enabled(enabled): enabled=False → astreamselect map=1 (anullsrc) + volume=0 enabled=True → astreamselect map=0 (live-audio) + volume=1 Двойная команда: select decouples upstream, volume гарантирует тишину на случай если в anullsrc что-то не так. http_api: + GET /audio-output/{instance}, POST /audio-output/{instance} static/index.html: + checkbox в Audio source header + loadAudioOut/toggleAudioOut ZMQ smoke test OK. HTTP roundtrip OK. Сопутствующий pipeline change: docker-compose.phase7.yml — amix заменён на astreamselect@audio_input (см. localhost-infra commit). Co-Authored-By: Claude Opus 4.7 --- controller/cuda_grid_controller/config.py | 11 +++++++ controller/cuda_grid_controller/dispatch.py | 33 +++++++++++++++++++ controller/cuda_grid_controller/http_api.py | 20 +++++++++++ controller/cuda_grid_controller/state.py | 14 ++++++++ .../cuda_grid_controller/static/index.html | 14 +++++++- 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/controller/cuda_grid_controller/config.py b/controller/cuda_grid_controller/config.py index 219d81f..bffd9cc 100644 --- a/controller/cuda_grid_controller/config.py +++ b/controller/cuda_grid_controller/config.py @@ -92,6 +92,17 @@ class InstanceCfg(BaseModel): default="volume@music", description="Target filter для управления громкостью music chain (Phase 5c ducking)", ) + audio_output_volume_target: str = Field( + default="volume@output_audio", + description="ZMQ target volume filter в video pipeline (после astreamselect) для UI-mute audio. " + "0 = mute, 1 = unmute. Phase 7 filter pipeline.", + ) + audio_input_select_target: str = Field( + default="astreamselect@audio_input", + description="ZMQ target astreamselect filter — выбор audio source в video pipeline. " + "map=0 → live-audio (sidecar), map=1 → anullsrc (decouple от jittery RTSP). " + "UI mute → map=1 + volume=0; unmute → map=0 + volume=1.", + ) intercom_volume_target: str = Field( default="volume@intercom", description="Target filter для управления громкостью intercom (Phase 5c)", diff --git a/controller/cuda_grid_controller/dispatch.py b/controller/cuda_grid_controller/dispatch.py index 0b99e4f..edc6221 100644 --- a/controller/cuda_grid_controller/dispatch.py +++ b/controller/cuda_grid_controller/dispatch.py @@ -218,6 +218,39 @@ class CommandDispatcher: использует cell 0 как main slot. Семантика идентична: cell_map 0 = cam_index.""" await self.set_main_cam(instance, cam_index) + async def set_audio_output_enabled(self, instance: str, enabled: bool) -> bool: + """Phase 7 Вариант 2: mute = astreamselect map=1 (anullsrc) + volume=0. + unmute = astreamselect map=0 (live-audio) + volume=1. astreamselect + decouples downstream от jittery live-audio RTSP — при mute frames + идут только через anullsrc chain, video не зависит от audio sidecar.""" + inst = self._find_instance(instance) + if inst is None: + return False + client = self._client(inst) + input_map = "0" if enabled else "1" + volume = "1.0" if enabled else "0.0" + try: + reply1 = await client.send_command( + inst.audio_input_select_target, "map", input_map + ) + reply2 = await client.send_command( + inst.audio_output_volume_target, "volume", volume + ) + log.info("dispatch.audio_output_set", + instance=instance, enabled=enabled, + input_map=input_map, volume=volume, + select_reply=reply1, volume_reply=reply2) + except (TimeoutError, Exception) as e: + log.warning("dispatch.audio_output_fail", + instance=instance, error=str(e)) + return False + await self.state.set_audio_output_enabled(instance, enabled) + if self.on_state_change: + await self.on_state_change(instance, "audio_output", "on" if enabled else "off") + if self.on_event: + await self.on_event(instance, "audio_output_changed", {"enabled": enabled}) + return True + async def _reload_icon(self, instance: str, icon_name: str) -> None: """Invalidate cached icon atlas во всех cuda_grid instances — каждый имеет свой кеш.""" diff --git a/controller/cuda_grid_controller/http_api.py b/controller/cuda_grid_controller/http_api.py index 2576688..5b04c84 100644 --- a/controller/cuda_grid_controller/http_api.py +++ b/controller/cuda_grid_controller/http_api.py @@ -34,6 +34,10 @@ class AutoLayoutReq(BaseModel): enabled: bool +class AudioOutputReq(BaseModel): + enabled: bool + + def create_app( cfg: Config, state: ControllerState, dispatcher: CommandDispatcher, snapshot_history: SnapshotHistory | None = None, @@ -80,6 +84,22 @@ def create_app( } return {"instances": out} + @app.get("/audio-output/{instance}") + async def audio_output_get(instance: str) -> dict[str, Any]: + _check_instance(instance) + return { + "instance": instance, + "enabled": await state.get_audio_output_enabled(instance), + } + + @app.post("/audio-output/{instance}") + async def audio_output_set(instance: str, req: AudioOutputReq) -> dict[str, Any]: + _check_instance(instance) + ok = await dispatcher.set_audio_output_enabled(instance, req.enabled) + if not ok: + raise HTTPException(500, "audio output ZMQ command failed (см. logs)") + return {"ok": True, "instance": instance, "enabled": req.enabled} + @app.post("/layout/{instance}/set") async def set_layout(instance: str, req: LayoutSetReq) -> dict[str, Any]: _check_instance(instance) diff --git a/controller/cuda_grid_controller/state.py b/controller/cuda_grid_controller/state.py index 94243ee..8cdff5c 100644 --- a/controller/cuda_grid_controller/state.py +++ b/controller/cuda_grid_controller/state.py @@ -13,6 +13,7 @@ class InstanceState: name: str active_layout: str overlays: dict[str, Overlay] = field(default_factory=dict) + audio_output_enabled: bool = True # Future: fps_out, dropped_frames, motion_cameras, last_event_ts @@ -66,6 +67,19 @@ class ControllerState: st = self.instances.get(instance) return list(st.overlays.values()) if st else [] + # ─── Audio output mute ────────────────────────────────────────── + + async def set_audio_output_enabled(self, instance: str, enabled: bool) -> None: + async with self._lock: + st = self.instances.get(instance) + if st is not None: + st.audio_output_enabled = enabled + + async def get_audio_output_enabled(self, instance: str) -> bool: + async with self._lock: + st = self.instances.get(instance) + return st.audio_output_enabled if st else True + async def clear_overlays(self, instance: str) -> int: async with self._lock: st = self.instances.get(instance) diff --git a/controller/cuda_grid_controller/static/index.html b/controller/cuda_grid_controller/static/index.html index 6468b91..a719c4c 100644 --- a/controller/cuda_grid_controller/static/index.html +++ b/controller/cuda_grid_controller/static/index.html @@ -57,7 +57,7 @@ pre { background:#0f0f12; padding:8px; border-radius:4px; font-size:11px; overfl
-

Audio source

+

Audio source

@@ -181,6 +181,18 @@ async function toggleAuto() { await api('POST', `/auto-layout/${INSTANCE}`, {enabled}, 'auto ' + (enabled?'ON':'OFF')); } +// ── Audio output toggle (Phase 7 volume@output_audio) ─ +async function loadAudioOut() { + const r = await fetch(`/audio-output/${INSTANCE}`); + if (!r.ok) return; + const d = await r.json(); + document.getElementById('audio-out-tog').checked = d.enabled; +} +async function toggleAudioOut() { + const enabled = document.getElementById('audio-out-tog').checked; + await api('POST', `/audio-output/${INSTANCE}`, {enabled}, 'audio out ' + (enabled?'ON':'OFF')); +} + // ── Layout buttons ──────────────────────────────────── async function loadLayouts() { const r = await fetch(`/layouts/${INSTANCE}`);