From f27c3e117014b65977cf8db5fbd0313b049777d4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 19 May 2026 10:12:33 +0100 Subject: [PATCH] Delete Python OpenGL Spout path --- localcast/diagnostics/spout_output.py | 429 -------------------------- scripts/diagnostic_stream_spout.py | 178 ----------- 2 files changed, 607 deletions(-) delete mode 100644 localcast/diagnostics/spout_output.py delete mode 100644 scripts/diagnostic_stream_spout.py diff --git a/localcast/diagnostics/spout_output.py b/localcast/diagnostics/spout_output.py deleted file mode 100644 index a2d0e9f..0000000 --- a/localcast/diagnostics/spout_output.py +++ /dev/null @@ -1,429 +0,0 @@ -from __future__ import annotations - -import ctypes -import json -from pathlib import Path -import time - -import numpy as np - -from localcast.sensor_fusion.audio_overlay import AudioVisualSyncStatus, overlay_audio_events, with_remote_video_status -from localcast.sensor_fusion.media_artifacts import remote_video_artifact_for_present_time -from localcast.sensor_fusion.render_bridge import RenderFramePacket, RenderPointPacket -from localcast.diagnostics.render_frame_json import load_render_frame_json -from localcast.diagnostics.render_math import ( - SpoutOutputConfig, - camera_matrix, - camera_preset, - demo_render_frame, - frame_to_vertex_array, - frame_with_point_budget, - point_prefix_counts, - rasterize_frame_rgba, -) -from localcast.diagnostics.visual_cache import CultStreamStatus, get_live_render_frame, put_stream_status - - -class RenderFrameFileSource: - def __init__(self, path: Path, demo_if_missing: bool = False, demo_point_count: int = 4096) -> None: - self.path = path - self.demo_if_missing = demo_if_missing - self.demo_point_count = demo_point_count - self._mtime_ns: int | None = None - self._frame: RenderFramePacket | None = None - - def current_frame(self, now_s: float, config: SpoutOutputConfig) -> RenderFramePacket: - if self.path.exists(): - mtime_ns = self.path.stat().st_mtime_ns - if self._frame is None or mtime_ns != self._mtime_ns: - self._frame = load_render_frame_json(self.path) - self._mtime_ns = mtime_ns - return self._frame - if not self.demo_if_missing: - raise FileNotFoundError(self.path) - return demo_render_frame(now_s, config) - - -class CultCacheRenderFrameSource: - def __init__( - self, - path: Path, - demo_if_missing: bool = False, - demo_point_count: int = 4096, - audio_cache: Path | None = None, - audio_events_cache: Path | None = None, - audio_event_window_ns: int = 120_000_000, - remote_video_name: str | None = None, - remote_video_url: str | None = None, - remote_video_latency_ns: int = 120_000_000, - remote_video_tolerance_ns: int = 120_000_000, - ) -> None: - self.path = path - self.demo_if_missing = demo_if_missing - self.demo_point_count = demo_point_count - self.audio_cache = audio_cache - self.audio_events_cache = audio_events_cache - self.audio_event_window_ns = int(audio_event_window_ns) - self.remote_video_name = remote_video_name - self.remote_video_url = remote_video_url - self.remote_video_latency_ns = int(remote_video_latency_ns) - self.remote_video_tolerance_ns = int(remote_video_tolerance_ns) - self._mtime_ns: int | None = None - self._frame: RenderFramePacket | None = None - self._audio_mtime_ns: int | None = None - self._audio_frame = None - self._events_mtime_ns: int | None = None - self._events = None - self.last_sync_status: AudioVisualSyncStatus | None = None - - def current_frame(self, now_s: float, config: SpoutOutputConfig) -> RenderFramePacket: - if self.path.exists(): - mtime_ns = self.path.stat().st_mtime_ns - if self._frame is None or mtime_ns != self._mtime_ns: - self._frame = get_live_render_frame(self.path) - self._mtime_ns = mtime_ns - if self._frame is not None: - return self._with_audio_overlay(self._frame) - if not self.demo_if_missing: - raise FileNotFoundError(self.path) - return self._with_audio_overlay(demo_render_frame(now_s, config)) - - def _with_audio_overlay(self, frame: RenderFramePacket) -> RenderFramePacket: - if self.audio_cache is None and self.audio_events_cache is None: - self.last_sync_status = None - return frame - audio_frame = self._read_audio_frame() - events = self._read_audio_events() - augmented, status = overlay_audio_events( - frame, - events, - audio_frame, - window_ns=self.audio_event_window_ns, - ) - remote_video = None - if self.remote_video_name and self.remote_video_url: - remote_video = remote_video_artifact_for_present_time( - source_name=self.remote_video_name, - url=self.remote_video_url, - present_time_ns=frame.present_time_ns, - observed_time_ns=frame.source_time_max_ns, - expected_latency_ns=self.remote_video_latency_ns, - tolerance_ns=self.remote_video_tolerance_ns, - ) - status = with_remote_video_status(status, remote_video) - self.last_sync_status = status - return augmented - - def _read_audio_frame(self): - if self.audio_cache is None or not self.audio_cache.exists(): - return None - mtime_ns = self.audio_cache.stat().st_mtime_ns - if self._audio_frame is None or mtime_ns != self._audio_mtime_ns: - from audio_field.cultcache_audio import get_live_spatial_audio_frame - - self._audio_frame = get_live_spatial_audio_frame(self.audio_cache) - self._audio_mtime_ns = mtime_ns - return self._audio_frame - - def _read_audio_events(self): - if self.audio_events_cache is None or not self.audio_events_cache.exists(): - return None - mtime_ns = self.audio_events_cache.stat().st_mtime_ns - if self._events is None or mtime_ns != self._events_mtime_ns: - from audio_field.cultcache_audio import get_live_audio_source_events - - self._events = get_live_audio_source_events(self.audio_events_cache) - self._events_mtime_ns = mtime_ns - return self._events - - -def write_status( - path: Path, - *, - sender_name: str, - frames_sent: int, - point_count: int, - frame_path: Path | None, - last_error: str | None = None, - point_prefix_counts: dict[str, int] | None = None, -) -> None: - path.parent.mkdir(parents=True, exist_ok=True) - payload = { - "sender_name": sender_name, - "frames_sent": frames_sent, - "point_count": point_count, - "frame_path": None if frame_path is None else str(frame_path), - "updated_monotonic_ns": time.monotonic_ns(), - "last_error": last_error, - } - if point_prefix_counts is not None: - payload["point_prefix_counts"] = dict(sorted(point_prefix_counts.items())) - path.write_text(json.dumps(payload, indent=2), encoding="utf-8") - - -def write_cult_status( - path: Path, - *, - sender_name: str, - frames_sent: int, - point_count: int, - frame_source: str, - last_error: str | None = None, -) -> None: - put_stream_status( - path, - CultStreamStatus( - sender_name=sender_name, - frames_sent=frames_sent, - point_count=point_count, - frame_source=frame_source, - updated_monotonic_ns=time.monotonic_ns(), - last_error="" if last_error is None else last_error, - ), - ) - - -def write_sync_status(path: Path, status: AudioVisualSyncStatus | None) -> None: - if status is None: - return - path.parent.mkdir(parents=True, exist_ok=True) - path.write_text( - json.dumps( - { - "visual_frame_id": status.visual_frame_id, - "visual_present_time_ns": status.visual_present_time_ns, - "audio_frame_id": status.audio_frame_id, - "audio_time_ns": status.audio_time_ns, - "audio_delta_ns": status.audio_delta_ns, - "source_event_count": status.source_event_count, - "overlay_event_count": status.overlay_event_count, - "remote_video": None - if status.remote_video is None - else { - "source_name": status.remote_video.source_name, - "url": status.remote_video.url, - "expected_latency_ns": status.remote_video.expected_latency_ns, - "present_time_ns": status.remote_video.present_time_ns, - "delta_ns": status.remote_video.delta_ns, - "synchronized": status.remote_video.synchronized, - }, - "synchronized": status.synchronized, - "updated_monotonic_ns": time.monotonic_ns(), - }, - indent=2, - ), - encoding="utf-8", - ) - - -class OpenGLSpoutPointRenderer: - def __init__(self, config: SpoutOutputConfig) -> None: - self.config = config - self.sender = None - self.texture = None - self.fbo = None - self.program = None - self.vbo = None - self.vao = None - self.frames_sent = 0 - self.last_render_point_count = 0 - self.last_render_prefix_counts: dict[str, int] = {} - - def __enter__(self) -> "OpenGLSpoutPointRenderer": - self.open() - return self - - def __exit__(self, exc_type, exc, tb) -> None: - self.close() - - def open(self) -> None: - import SpoutGL - from OpenGL.GL import ( - GL_ARRAY_BUFFER, - GL_BLEND, - GL_COLOR_ATTACHMENT0, - GL_FRAMEBUFFER, - GL_RGBA, - GL_RGBA8, - GL_TEXTURE_2D, - GL_UNSIGNED_BYTE, - glBindBuffer, - glBindFramebuffer, - glBindTexture, - glBlendFunc, - glBufferData, - glEnable, - glEnableVertexAttribArray, - glFramebufferTexture2D, - glGenBuffers, - glGenFramebuffers, - glGenTextures, - glGenVertexArrays, - glBindVertexArray, - glTexImage2D, - glVertexAttribPointer, - GL_DYNAMIC_DRAW, - GL_FLOAT, - GL_ONE, - GL_ONE_MINUS_SRC_ALPHA, - GL_PROGRAM_POINT_SIZE, - ) - - from OpenGL.GL.shaders import compileProgram, compileShader - from OpenGL.GL import GL_FRAGMENT_SHADER, GL_VERTEX_SHADER - - self.sender = SpoutGL.SpoutSender() - self.sender.setSenderName(self.config.sender_name) - if not self.sender.createOpenGL(): - raise RuntimeError("SpoutGL could not create an OpenGL context") - - self.texture = glGenTextures(1) - glBindTexture(GL_TEXTURE_2D, self.texture) - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGBA8, - self.config.width, - self.config.height, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - None, - ) - self.fbo = glGenFramebuffers(1) - glBindFramebuffer(GL_FRAMEBUFFER, self.fbo) - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0) - - self.program = compileProgram( - compileShader(VERTEX_SHADER, GL_VERTEX_SHADER), - compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER), - ) - self.vao = glGenVertexArrays(1) - self.vbo = glGenBuffers(1) - glBindVertexArray(self.vao) - glBindBuffer(GL_ARRAY_BUFFER, self.vbo) - glBufferData(GL_ARRAY_BUFFER, 0, None, GL_DYNAMIC_DRAW) - stride = 8 * 4 - glEnableVertexAttribArray(0) - glVertexAttribPointer(0, 3, GL_FLOAT, False, stride, None) - glEnableVertexAttribArray(1) - glVertexAttribPointer(1, 4, GL_FLOAT, False, stride, ctypes.c_void_p(12)) - glEnableVertexAttribArray(2) - glVertexAttribPointer(2, 1, GL_FLOAT, False, stride, ctypes.c_void_p(28)) - glEnable(GL_BLEND) - glEnable(GL_PROGRAM_POINT_SIZE) - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) - - def render(self, frame: RenderFramePacket) -> bool: - from OpenGL.GL import ( - GL_ARRAY_BUFFER, - GL_COLOR_BUFFER_BIT, - GL_DEPTH_BUFFER_BIT, - GL_DYNAMIC_DRAW, - GL_FRAMEBUFFER, - GL_POINTS, - GL_RGBA, - GL_TEXTURE_2D, - GL_TRUE, - GL_UNSIGNED_BYTE, - glBindBuffer, - glBindFramebuffer, - glBindTexture, - glBindVertexArray, - glBufferData, - glClear, - glClearColor, - glDrawArrays, - glFlush, - glGetUniformLocation, - glUniform2f, - glUniformMatrix4fv, - glTexSubImage2D, - glUseProgram, - glViewport, - ) - - assert self.sender is not None - assert self.texture is not None - assert self.fbo is not None - assert self.program is not None - assert self.vbo is not None - assert self.vao is not None - - render_frame = frame_with_point_budget(frame, self.config.max_render_points, self.config.camera_preset) - self.last_render_point_count = len(render_frame.points) - self.last_render_prefix_counts = point_prefix_counts(render_frame.points) - vertices = frame_to_vertex_array(render_frame, self.config.point_scale) - glBindFramebuffer(GL_FRAMEBUFFER, self.fbo) - glViewport(0, 0, self.config.width, self.config.height) - glClearColor(0.025, 0.035, 0.048, 1.0) - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - glUseProgram(self.program) - camera = camera_preset(self.config.camera_preset, time.monotonic()) - view_proj = camera_matrix(time.monotonic(), self.config.width / max(1, self.config.height), camera) - glUniformMatrix4fv(glGetUniformLocation(self.program, "u_view_proj"), 1, GL_TRUE, view_proj.astype(np.float32)) - glUniform2f(glGetUniformLocation(self.program, "u_viewport"), float(self.config.width), float(self.config.height)) - glBindVertexArray(self.vao) - glBindBuffer(GL_ARRAY_BUFFER, self.vbo) - glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_DYNAMIC_DRAW) - glDrawArrays(GL_POINTS, 0, len(vertices)) - image = rasterize_frame_rgba(render_frame, self.config.width, self.config.height, self.config.point_scale, camera) - glBindTexture(GL_TEXTURE_2D, self.texture) - glTexSubImage2D( - GL_TEXTURE_2D, - 0, - 0, - 0, - self.config.width, - self.config.height, - GL_RGBA, - GL_UNSIGNED_BYTE, - image, - ) - glFlush() - ok = bool(self.sender.sendTexture(self.texture, GL_TEXTURE_2D, self.config.width, self.config.height, False, self.fbo)) - if ok: - self.frames_sent += 1 - return ok - - def close(self) -> None: - if self.sender is not None: - self.sender.releaseSender() - self.sender.closeOpenGL() - self.sender = None - - -VERTEX_SHADER = """ -#version 330 core -layout(location = 0) in vec3 in_pos; -layout(location = 1) in vec4 in_color; -layout(location = 2) in float in_radius; -uniform mat4 u_view_proj; -uniform vec2 u_viewport; -out vec4 v_color; - -void main() { - vec2 projected = vec2(in_pos.x * 0.58, (in_pos.z - 1.18) * 1.15 + in_pos.y * 0.16); - gl_Position = vec4(projected, 0.0, 1.0); - float radius_px = max(5.0, in_radius * u_viewport.y * 2.8); - gl_PointSize = radius_px; - v_color = in_color; -} -""" - - -FRAGMENT_SHADER = """ -#version 330 core -in vec4 v_color; -out vec4 out_color; - -void main() { - vec2 delta = gl_PointCoord * 2.0 - 1.0; - float radius2 = dot(delta, delta); - if (radius2 > 1.0) { - discard; - } - float alpha = (1.0 - smoothstep(0.45, 1.0, radius2)) * v_color.a; - out_color = vec4(v_color.rgb * alpha, alpha); -} -""" diff --git a/scripts/diagnostic_stream_spout.py b/scripts/diagnostic_stream_spout.py deleted file mode 100644 index 3a38225..0000000 --- a/scripts/diagnostic_stream_spout.py +++ /dev/null @@ -1,178 +0,0 @@ -import argparse -from pathlib import Path -import sys -import time -from typing import BinaryIO - -ROOT = Path(__file__).resolve().parents[1] -if str(ROOT) not in sys.path: - sys.path.insert(0, str(ROOT)) - -from localcast.diagnostics.spout_output import ( - CultCacheRenderFrameSource, - OpenGLSpoutPointRenderer, - RenderFrameFileSource, - SpoutOutputConfig, - write_cult_status, - write_sync_status, - write_status, -) - - -def acquire_runtime_lock(path: Path) -> BinaryIO | None: - import msvcrt - - path.parent.mkdir(parents=True, exist_ok=True) - handle = path.open("a+b") - handle.seek(0) - handle.write(b"\0") - handle.flush() - handle.seek(0) - try: - msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1) - except OSError: - handle.close() - return None - return handle - - -def main() -> None: - parser = argparse.ArgumentParser(description="Publish render-frame packets as a named Spout texture for OBS.") - parser.add_argument("--frame-json", default=str(ROOT / "calibration" / "runs" / "synthetic-render-frame.json")) - parser.add_argument("--frame-cache", default=str(ROOT / "calibration" / "runs" / "visual-state.msgpack")) - parser.add_argument("--sender-name", default="LocalCastBridge Point Cloud") - parser.add_argument("--width", type=int, default=1920) - parser.add_argument("--height", type=int, default=1080) - parser.add_argument("--fps", type=float, default=60.0) - parser.add_argument("--point-scale", type=float, default=1.0) - parser.add_argument("--camera-preset", choices=["kiyo-mid-deru", "orbit"], default="kiyo-mid-deru") - parser.add_argument("--max-render-points", type=int, default=5000) - parser.add_argument("--demo-if-missing", action="store_true") - parser.add_argument("--demo-point-count", type=int, default=4096) - parser.add_argument("--status", default=str(ROOT / "calibration" / "runs" / "stream-spout-status.json")) - parser.add_argument("--status-cache", default=str(ROOT / "calibration" / "runs" / "visual-stream-status.msgpack")) - parser.add_argument("--sync-status", default=str(ROOT / "calibration" / "runs" / "av-sync-status.json")) - parser.add_argument("--audio-cache", default=str(ROOT / "calibration" / "runs" / "audio-state.msgpack")) - parser.add_argument("--audio-events-cache", default=str(ROOT / "calibration" / "runs" / "audio-events.msgpack")) - parser.add_argument("--audio-event-window-ms", type=float, default=120.0) - parser.add_argument("--no-audio-overlay", action="store_true") - parser.add_argument("--remote-video-name", default="Neighbor PC - Video") - parser.add_argument("--remote-video-url", default="srt://0.0.0.0:5100?mode=listener&latency=120000&timeout=5000000") - parser.add_argument("--remote-video-latency-ms", type=float, default=250.0) - parser.add_argument("--remote-video-tolerance-ms", type=float, default=120.0) - parser.add_argument("--no-remote-video-sync", action="store_true") - parser.add_argument("--source", choices=["cultcache", "json"], default="cultcache") - parser.add_argument("--duration", type=float, help="Optional run duration in seconds for smoke tests.") - args = parser.parse_args() - - frame_path = Path(args.frame_json) - frame_cache_path = Path(args.frame_cache) - status_path = Path(args.status) - status_cache_path = Path(args.status_cache) - sync_status_path = Path(args.sync_status) - lock = acquire_runtime_lock(status_path.with_suffix(".sender.lock")) - if lock is None: - return - config = SpoutOutputConfig( - sender_name=args.sender_name, - width=args.width, - height=args.height, - fps=args.fps, - point_scale=args.point_scale, - demo_point_count=args.demo_point_count, - camera_preset=args.camera_preset, - max_render_points=args.max_render_points, - ) - if args.source == "cultcache": - source = CultCacheRenderFrameSource( - frame_cache_path, - demo_if_missing=args.demo_if_missing, - demo_point_count=args.demo_point_count, - audio_cache=None if args.no_audio_overlay else Path(args.audio_cache), - audio_events_cache=None if args.no_audio_overlay else Path(args.audio_events_cache), - audio_event_window_ns=int(float(args.audio_event_window_ms) * 1_000_000), - remote_video_name=None if args.no_remote_video_sync else args.remote_video_name, - remote_video_url=None if args.no_remote_video_sync else args.remote_video_url, - remote_video_latency_ns=int(float(args.remote_video_latency_ms) * 1_000_000), - remote_video_tolerance_ns=int(float(args.remote_video_tolerance_ms) * 1_000_000), - ) - frame_source_name = str(frame_cache_path) - else: - source = RenderFrameFileSource(frame_path, demo_if_missing=args.demo_if_missing, demo_point_count=args.demo_point_count) - frame_source_name = str(frame_path) - frame_interval = 1.0 / max(1.0, args.fps) - start = time.monotonic() - last_status = 0.0 - - with OpenGLSpoutPointRenderer(config) as renderer: - while True: - now = time.monotonic() - if args.duration is not None and now - start >= args.duration: - break - try: - frame = source.current_frame(now, config) - except FileNotFoundError: - if now - last_status >= 1.0: - write_status( - status_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=0, - frame_path=Path(frame_source_name), - last_error="waiting for visual frame cache", - ) - write_cult_status( - status_cache_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=0, - frame_source=frame_source_name, - last_error="waiting for visual frame cache", - ) - last_status = now - time.sleep(frame_interval) - continue - ok = renderer.render(frame) - if now - last_status >= 1.0: - write_status( - status_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=renderer.last_render_point_count, - frame_path=Path(frame_source_name), - last_error=None if ok else "SpoutGL sendTexture returned false", - point_prefix_counts=renderer.last_render_prefix_counts, - ) - write_cult_status( - status_cache_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=renderer.last_render_point_count, - frame_source=frame_source_name, - last_error=None if ok else "SpoutGL sendTexture returned false", - ) - write_sync_status(sync_status_path, getattr(source, "last_sync_status", None)) - last_status = now - sleep_for = frame_interval - (time.monotonic() - now) - if sleep_for > 0: - time.sleep(sleep_for) - write_status( - status_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=0, - frame_path=Path(frame_source_name), - last_error="stopped", - ) - write_cult_status( - status_cache_path, - sender_name=config.sender_name, - frames_sent=renderer.frames_sent, - point_count=0, - frame_source=frame_source_name, - last_error="stopped", - ) - - -if __name__ == "__main__": - main()