Track remote video as synchronized media artifact
This commit is contained in:
parent
aa8b5f926e
commit
d6c712d5eb
9 changed files with 158 additions and 7 deletions
|
|
@ -13,6 +13,7 @@ from .core import (
|
|||
)
|
||||
from .render_bridge import RenderBridgeConfig, RenderFramePacket, RenderPointPacket, lower_points_to_render_frame
|
||||
from .audio_overlay import AudioVisualSyncStatus, overlay_audio_events
|
||||
from .media_artifacts import RemoteVideoArtifact, remote_video_artifact_for_present_time
|
||||
from .cultcache_docs import (
|
||||
LIVE_RENDER_FRAME_KEY,
|
||||
CultRenderFrame,
|
||||
|
|
@ -37,6 +38,7 @@ __all__ = [
|
|||
"RenderFramePacket",
|
||||
"RenderPointPacket",
|
||||
"AudioVisualSyncStatus",
|
||||
"RemoteVideoArtifact",
|
||||
"LIVE_RENDER_FRAME_KEY",
|
||||
"CultRenderFrame",
|
||||
"CultRenderPoint",
|
||||
|
|
@ -48,5 +50,6 @@ __all__ = [
|
|||
"lower_frame_to_screen_brushes",
|
||||
"lower_points_to_render_frame",
|
||||
"overlay_audio_events",
|
||||
"remote_video_artifact_for_present_time",
|
||||
"put_live_render_frame",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import sys
|
|||
import numpy as np
|
||||
|
||||
from .render_bridge import RenderFramePacket, RenderPointPacket
|
||||
from .media_artifacts import RemoteVideoArtifact
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
|
@ -25,6 +26,7 @@ class AudioVisualSyncStatus:
|
|||
audio_delta_ns: int | None
|
||||
source_event_count: int
|
||||
overlay_event_count: int
|
||||
remote_video: RemoteVideoArtifact | None
|
||||
synchronized: bool
|
||||
|
||||
|
||||
|
|
@ -38,7 +40,7 @@ def overlay_audio_events(
|
|||
max_events: int = 128,
|
||||
) -> tuple[RenderFramePacket, AudioVisualSyncStatus]:
|
||||
if events is None:
|
||||
return frame, sync_status(frame, None, 0, 0, False)
|
||||
return frame, sync_status(frame, None, 0, 0, None, False)
|
||||
overlay = select_events_for_visual_frame(frame, events, audio_frame, int(window_ns), int(max_events))
|
||||
points = list(frame.points)
|
||||
for event in overlay:
|
||||
|
|
@ -70,7 +72,7 @@ def overlay_audio_events(
|
|||
target_height=frame.target_height,
|
||||
points=tuple(points),
|
||||
),
|
||||
sync_status(frame, audio_frame, len(events.events), len(overlay), True),
|
||||
sync_status(frame, audio_frame, len(events.events), len(overlay), None, True),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -114,9 +116,11 @@ def sync_status(
|
|||
audio_frame: CultSpatialAudioFrame | None,
|
||||
source_event_count: int,
|
||||
overlay_event_count: int,
|
||||
remote_video: RemoteVideoArtifact | None,
|
||||
synchronized: bool,
|
||||
) -> AudioVisualSyncStatus:
|
||||
audio_delta_ns = None if audio_frame is None else frame.audio_alignment_time_ns - audio_frame.audio_time_ns
|
||||
remote_ok = True if remote_video is None else remote_video.synchronized
|
||||
return AudioVisualSyncStatus(
|
||||
visual_frame_id=frame.frame_id,
|
||||
visual_present_time_ns=frame.present_time_ns,
|
||||
|
|
@ -125,6 +129,23 @@ def sync_status(
|
|||
audio_delta_ns=audio_delta_ns,
|
||||
source_event_count=source_event_count,
|
||||
overlay_event_count=overlay_event_count,
|
||||
synchronized=bool(synchronized and audio_frame is not None),
|
||||
remote_video=remote_video,
|
||||
synchronized=bool(synchronized and audio_frame is not None and remote_ok),
|
||||
)
|
||||
|
||||
|
||||
def with_remote_video_status(status: AudioVisualSyncStatus | None, remote_video: RemoteVideoArtifact | None) -> AudioVisualSyncStatus | None:
|
||||
if status is None:
|
||||
return None
|
||||
remote_ok = True if remote_video is None else remote_video.synchronized
|
||||
return AudioVisualSyncStatus(
|
||||
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=remote_video,
|
||||
synchronized=bool(status.synchronized and remote_ok),
|
||||
)
|
||||
|
|
|
|||
35
localcast/sensor_fusion/media_artifacts.py
Normal file
35
localcast/sensor_fusion/media_artifacts.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RemoteVideoArtifact:
|
||||
source_name: str
|
||||
url: str
|
||||
expected_latency_ns: int
|
||||
present_time_ns: int
|
||||
delta_ns: int
|
||||
synchronized: bool
|
||||
|
||||
|
||||
def remote_video_artifact_for_present_time(
|
||||
*,
|
||||
source_name: str,
|
||||
url: str,
|
||||
present_time_ns: int,
|
||||
observed_time_ns: int,
|
||||
expected_latency_ns: int,
|
||||
tolerance_ns: int = 120_000_000,
|
||||
) -> RemoteVideoArtifact:
|
||||
media_present_time = int(observed_time_ns) + int(expected_latency_ns)
|
||||
delta_ns = int(present_time_ns) - media_present_time
|
||||
return RemoteVideoArtifact(
|
||||
source_name=str(source_name),
|
||||
url=str(url),
|
||||
expected_latency_ns=int(expected_latency_ns),
|
||||
present_time_ns=media_present_time,
|
||||
delta_ns=delta_ns,
|
||||
synchronized=abs(delta_ns) <= int(tolerance_ns),
|
||||
)
|
||||
|
||||
|
|
@ -10,7 +10,8 @@ import time
|
|||
|
||||
import numpy as np
|
||||
|
||||
from .audio_overlay import AudioVisualSyncStatus, overlay_audio_events
|
||||
from .audio_overlay import AudioVisualSyncStatus, overlay_audio_events, with_remote_video_status
|
||||
from .media_artifacts import remote_video_artifact_for_present_time
|
||||
from .render_bridge import RenderFramePacket, RenderPointPacket, load_render_frame
|
||||
from .cultcache_docs import CultStreamStatus, get_live_render_frame, put_stream_status
|
||||
|
||||
|
|
@ -62,6 +63,10 @@ class CultCacheRenderFrameSource:
|
|||
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
|
||||
|
|
@ -69,6 +74,10 @@ class CultCacheRenderFrameSource:
|
|||
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
|
||||
|
|
@ -101,6 +110,17 @@ class CultCacheRenderFrameSource:
|
|||
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
|
||||
|
||||
|
|
@ -285,6 +305,16 @@ def write_sync_status(path: Path, status: AudioVisualSyncStatus | None) -> None:
|
|||
"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(),
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue