Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autotracker: add thread lock for move queues #7973

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions frigate/ptz/autotrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(
self.object_types: dict[str, object] = {}
self.required_zones: dict[str, object] = {}
self.move_queues: dict[str, object] = {}
self.move_queue_locks: dict[str, object] = {}
self.move_threads: dict[str, object] = {}
self.autotracker_init: dict[str, object] = {}
self.move_metrics: dict[str, object] = {}
Expand Down Expand Up @@ -196,6 +197,7 @@ def _autotracker_setup(self, cam, camera_name):
self.move_coefficients[camera_name] = []

self.move_queues[camera_name] = queue.Queue()
self.move_queue_locks[camera_name] = threading.Lock()

if not self.onvif.cams[camera_name]["init"]:
if not self.onvif._init_onvif(camera_name):
Expand Down Expand Up @@ -371,8 +373,9 @@ def _predict_movement_time(self, camera, pan, tilt):

def _process_move_queue(self, camera):
while True:
try:
move_data = self.move_queues[camera].get()
move_data = self.move_queues[camera].get()

with self.move_queue_locks[camera]:
frame_time, pan, tilt, zoom = move_data

# if we're receiving move requests during a PTZ move, ignore them
Expand Down Expand Up @@ -435,9 +438,6 @@ def _process_move_queue(self, camera):
# calculate new coefficients if we have enough data
self._calculate_move_coefficients(camera)

except queue.Empty:
continue

def _enqueue_move(self, camera, frame_time, pan, tilt, zoom):
def split_value(value):
clipped = np.clip(value, -1, 1)
Expand All @@ -446,7 +446,7 @@ def split_value(value):
if (
frame_time > self.ptz_metrics[camera]["ptz_start_time"].value
and frame_time > self.ptz_metrics[camera]["ptz_stop_time"].value
and self.move_queues[camera].qsize() == 0
and not self.move_queue_locks[camera].locked()
):
# don't make small movements
if abs(pan) < 0.02:
Expand Down