Skip to content

Commit ff81a9e

Browse files
committed
Reduce lock scope and refactor frame handling
Refactor _on_frame_captured to minimize time spent under _frame_lock and improve timing granularity. Introduces a frame_data local, renames timing labels (e.g. Multi.apply_transforms, Multi.store_latest, Multi.build_ordered, Multi.construct_frame_data), and moves frame_data construction inside measured blocks. The frame_ready emit is now performed outside the lock and guarded by a None check to avoid holding the lock during signal emission. Also small whitespace and cleanup changes.
1 parent 5cac636 commit ff81a9e

1 file changed

Lines changed: 37 additions & 32 deletions

File tree

dlclivegui/services/multi_camera_controller.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -469,52 +469,57 @@ def stop(self, wait: bool = True) -> None:
469469

470470
def _on_frame_captured(self, camera_id: str, frame: np.ndarray, timestamp: float) -> None:
471471
"""Handle a frame from one camera."""
472-
# Apply rotation if configured
473472
timing = self._timing_for_camera(camera_id)
473+
frame_data: MultiFrameData | None = None
474474

475475
with timing.measure("Multi.slot.total"):
476476
settings = self._settings.get(camera_id)
477-
with timing.measure("Multi.slot.apply_transforms"):
477+
478+
with timing.measure("Multi.apply_transforms"):
478479
if settings and settings.rotation:
479480
frame = MultiCameraController.apply_rotation(frame, settings.rotation)
480481

481-
# Apply cropping if configured
482482
if settings:
483483
crop_region = settings.get_crop_region()
484484
if crop_region:
485485
frame = MultiCameraController.apply_crop(frame, crop_region)
486-
with timing.measure("Multi.update_latest"):
487-
with self._frame_lock:
486+
487+
with self._frame_lock:
488+
with timing.measure("Multi.store_latest"):
488489
self._frames[camera_id] = frame
489490
self._timestamps[camera_id] = timestamp
490491

491-
# Emit frame data without tiling (tiling done in GUI for performance)
492-
if self._frames:
493-
ordered_frames: dict[str, np.ndarray] = {}
494-
ordered_timestamps: dict[str, float] = {}
495-
496-
for cam_id in self._camera_display_order:
497-
if cam_id in self._frames:
498-
ordered_frames[cam_id] = self._frames[cam_id]
499-
if cam_id in self._timestamps:
500-
ordered_timestamps[cam_id] = self._timestamps[cam_id]
501-
502-
# Any unexpected/legacy IDs, appended deterministically.
503-
for cam_id in self._frames:
504-
if cam_id not in ordered_frames:
505-
ordered_frames[cam_id] = self._frames[cam_id]
506-
for cam_id in self._timestamps:
507-
if cam_id not in ordered_timestamps:
508-
ordered_timestamps[cam_id] = self._timestamps[cam_id]
509-
510-
frame_data = MultiFrameData(
511-
frames=ordered_frames,
512-
timestamps=ordered_timestamps,
513-
source_camera_id=camera_id,
514-
tiled_frame=None,
515-
display_ids=dict(self._display_ids),
516-
)
517-
self.frame_ready.emit(frame_data)
492+
with timing.measure("Multi.build_ordered"):
493+
ordered_frames: dict[str, np.ndarray] = {}
494+
ordered_timestamps: dict[str, float] = {}
495+
496+
for cam_id in self._camera_display_order:
497+
if cam_id in self._frames:
498+
ordered_frames[cam_id] = self._frames[cam_id]
499+
if cam_id in self._timestamps:
500+
ordered_timestamps[cam_id] = self._timestamps[cam_id]
501+
502+
# Any unexpected/legacy IDs, appended deterministically.
503+
for cam_id in self._frames:
504+
if cam_id not in ordered_frames:
505+
ordered_frames[cam_id] = self._frames[cam_id]
506+
for cam_id in self._timestamps:
507+
if cam_id not in ordered_timestamps:
508+
ordered_timestamps[cam_id] = self._timestamps[cam_id]
509+
510+
with timing.measure("Multi.construct_frame_data"):
511+
frame_data = MultiFrameData(
512+
frames=ordered_frames,
513+
timestamps=ordered_timestamps,
514+
source_camera_id=camera_id,
515+
tiled_frame=None,
516+
display_ids=dict(self._display_ids),
517+
)
518+
519+
520+
if frame_data is not None:
521+
with timing.measure("Multi.emit.frame_ready"):
522+
self.frame_ready.emit(frame_data)
518523

519524
timing.note_frame()
520525
timing.maybe_log()

0 commit comments

Comments
 (0)