Fix round robin (#15035)

* Move camera SHM frame creation to main process

* Don't reset frame index

* Don't fail if shm exists

* Set more types
This commit is contained in:
Nicolas Mowen 2024-11-17 10:25:49 -07:00 committed by GitHub
parent 45e9030358
commit 5b1b6b5be0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 16 deletions

View File

@ -68,7 +68,7 @@ from frigate.stats.util import stats_init
from frigate.storage import StorageMaintainer from frigate.storage import StorageMaintainer
from frigate.timeline import TimelineProcessor from frigate.timeline import TimelineProcessor
from frigate.util.builtin import empty_and_close_queue from frigate.util.builtin import empty_and_close_queue
from frigate.util.image import UntrackedSharedMemory from frigate.util.image import SharedMemoryFrameManager, UntrackedSharedMemory
from frigate.util.object import get_camera_regions_grid from frigate.util.object import get_camera_regions_grid
from frigate.version import VERSION from frigate.version import VERSION
from frigate.video import capture_camera, track_camera from frigate.video import capture_camera, track_camera
@ -426,12 +426,18 @@ class FrigateApp:
def start_camera_capture_processes(self) -> None: def start_camera_capture_processes(self) -> None:
shm_frame_count = self.shm_frame_count() shm_frame_count = self.shm_frame_count()
frame_manager = SharedMemoryFrameManager()
for name, config in self.config.cameras.items(): for name, config in self.config.cameras.items():
if not self.config.cameras[name].enabled: if not self.config.cameras[name].enabled:
logger.info(f"Capture process not started for disabled camera {name}") logger.info(f"Capture process not started for disabled camera {name}")
continue continue
# pre-create shms
for i in range(shm_frame_count):
frame_size = config.frame_shape_yuv[0] * config.frame_shape_yuv[1]
frame_manager.create(f"{config.name}{i}", frame_size)
capture_process = util.Process( capture_process = util.Process(
target=capture_camera, target=capture_camera,
name=f"camera_capture:{name}", name=f"camera_capture:{name}",

View File

@ -790,11 +790,15 @@ class SharedMemoryFrameManager(FrameManager):
self.shm_store: dict[str, UntrackedSharedMemory] = {} self.shm_store: dict[str, UntrackedSharedMemory] = {}
def create(self, name: str, size) -> AnyStr: def create(self, name: str, size) -> AnyStr:
shm = UntrackedSharedMemory( try:
name=name, shm = UntrackedSharedMemory(
create=True, name=name,
size=size, create=True,
) size=size,
)
except FileExistsError:
shm = UntrackedSharedMemory(name=name)
self.shm_store[name] = shm self.shm_store[name] = shm
return shm.buf return shm.buf

View File

@ -94,7 +94,8 @@ def capture_frames(
ffmpeg_process, ffmpeg_process,
config: CameraConfig, config: CameraConfig,
shm_frame_count: int, shm_frame_count: int,
frame_shape, frame_index: int,
frame_shape: tuple[int, int],
frame_manager: FrameManager, frame_manager: FrameManager,
frame_queue, frame_queue,
fps: mp.Value, fps: mp.Value,
@ -108,12 +109,6 @@ def capture_frames(
skipped_eps = EventsPerSecond() skipped_eps = EventsPerSecond()
skipped_eps.start() skipped_eps.start()
# pre-create shms
for i in range(shm_frame_count):
frame_manager.create(f"{config.name}{i}", frame_size)
frame_index = 0
while True: while True:
fps.value = frame_rate.eps() fps.value = frame_rate.eps()
skipped_fps.value = skipped_eps.eps() skipped_fps.value = skipped_eps.eps()
@ -159,7 +154,7 @@ class CameraWatchdog(threading.Thread):
camera_name, camera_name,
config: CameraConfig, config: CameraConfig,
shm_frame_count: int, shm_frame_count: int,
frame_queue, frame_queue: mp.Queue,
camera_fps, camera_fps,
skipped_fps, skipped_fps,
ffmpeg_pid, ffmpeg_pid,
@ -181,6 +176,7 @@ class CameraWatchdog(threading.Thread):
self.frame_shape = self.config.frame_shape_yuv self.frame_shape = self.config.frame_shape_yuv
self.frame_size = self.frame_shape[0] * self.frame_shape[1] self.frame_size = self.frame_shape[0] * self.frame_shape[1]
self.fps_overflow_count = 0 self.fps_overflow_count = 0
self.frame_index = 0
self.stop_event = stop_event self.stop_event = stop_event
self.sleeptime = self.config.ffmpeg.retry_interval self.sleeptime = self.config.ffmpeg.retry_interval
@ -302,6 +298,7 @@ class CameraWatchdog(threading.Thread):
self.capture_thread = CameraCapture( self.capture_thread = CameraCapture(
self.config, self.config,
self.shm_frame_count, self.shm_frame_count,
self.frame_index,
self.ffmpeg_detect_process, self.ffmpeg_detect_process,
self.frame_shape, self.frame_shape,
self.frame_queue, self.frame_queue,
@ -342,9 +339,10 @@ class CameraCapture(threading.Thread):
self, self,
config: CameraConfig, config: CameraConfig,
shm_frame_count: int, shm_frame_count: int,
frame_index: int,
ffmpeg_process, ffmpeg_process,
frame_shape, frame_shape: tuple[int, int],
frame_queue, frame_queue: mp.Queue,
fps, fps,
skipped_fps, skipped_fps,
stop_event, stop_event,
@ -353,6 +351,7 @@ class CameraCapture(threading.Thread):
self.name = f"capture:{config.name}" self.name = f"capture:{config.name}"
self.config = config self.config = config
self.shm_frame_count = shm_frame_count self.shm_frame_count = shm_frame_count
self.frame_index = frame_index
self.frame_shape = frame_shape self.frame_shape = frame_shape
self.frame_queue = frame_queue self.frame_queue = frame_queue
self.fps = fps self.fps = fps
@ -368,6 +367,7 @@ class CameraCapture(threading.Thread):
self.ffmpeg_process, self.ffmpeg_process,
self.config, self.config,
self.shm_frame_count, self.shm_frame_count,
self.frame_index,
self.frame_shape, self.frame_shape,
self.frame_manager, self.frame_manager,
self.frame_queue, self.frame_queue,