feat: harden video analysis and runtime resilience

This commit is contained in:
somew1nd committed 2026-08-25 20:58:25 -07:00
1 parent 5ada543a09
commit bb16991f2c
55 files changed
+5023 -437

No files matched your search

+28 -15
View File
@@ -78,7 +78,9 @@ class AndroidDeviceController(MobileDeviceController):
self.data_engine_start_time = data_engine_start_time
self.data_engine = data_engine
self._device: AdbDevice | None = None
self._segment_cache: dict[tuple[float, float | None], VideoRecordingResult] = {}
self._segment_cache: dict[
tuple[str, int, float, float], VideoRecordingResult
] = {}
@property
def device(self) -> AdbDevice:
@@ -734,19 +736,6 @@ class AndroidDeviceController(MobileDeviceController):
end_relative_time: float | None = None,
) -> VideoRecordingResult:
"""Get a video segment for a specific time range (relative to video start)."""
cache_key = (
round(start_relative_time, 1),
round(end_relative_time, 1) if end_relative_time is not None else None,
)
if cache_key in self._segment_cache:
cached_res = self._segment_cache[cache_key]
if cached_res.success and cached_res.video_path and cached_res.video_path.exists():
logger.info(
"Reusing cached trimmed video segment for range"
f" {cache_key[0]}s to {cache_key[1]}s"
)
return cached_res
session = get_active_session(self.device_id)
if not session:
return VideoRecordingResult(
@@ -754,6 +743,22 @@ class AndroidDeviceController(MobileDeviceController):
message=f"No active recording for device {self.device_id}",
)
cache_key = None
if end_relative_time is not None:
cache_key = (
str(session.video_id),
session.generation,
round(start_relative_time, 1),
round(end_relative_time, 1),
)
cached_res = self._segment_cache.get(cache_key)
if cached_res and cached_res.success and cached_res.video_path and cached_res.video_path.exists():
logger.info(
"Reusing generation-scoped trimmed video segment for range"
f" {cache_key[2]}s to {cache_key[3]}s"
)
return cached_res
try:
mkv_path = session.local_video_path
if not mkv_path or not mkv_path.exists():
@@ -776,6 +781,7 @@ class AndroidDeviceController(MobileDeviceController):
# Try to restart scrcpy
try:
session.android_segment_index += 1
session.generation = session.android_segment_index
output_dir = mkv_path.parent
new_video_path = output_dir / f"recording_{session.android_segment_index}.mkv"
@@ -890,8 +896,15 @@ class AndroidDeviceController(MobileDeviceController):
duration_seconds=round(duration, 2),
actual_start_relative_time=actual_start,
warning=truncation_warning,
video_id=session.video_id,
generation=session.generation,
sealed_until=session.sealed_until,
source_revision=(
f"{session.video_id}:{session.generation}:{round(actual_end, 3)}"
),
)
self._segment_cache[cache_key] = res
if cache_key is not None:
self._segment_cache[cache_key] = res
return res
except Exception as e: