Skip to content

Commit

Permalink
prevent the cache from growing indefinitely
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeblackshear committed Sep 17, 2020
1 parent fdc8bbf commit 7880d24
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 11 additions & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ mqtt:
#################
# password: password # Optional

################
# Global configuration for saving clips
################
save_clips:
###########
# Maximum length of time to retain video during long events.
# If an object is being tracked for longer than this amount of time, the cache
# will begin to expire and the resulting clip will be the last x seconds of the event.
###########
max_seconds: 300

#################
# Default ffmpeg args. Optional and can be overwritten per camera.
# Should work with most RTSP cameras that send h264 video
Expand Down
2 changes: 1 addition & 1 deletion detect_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def on_connect(client, userdata, flags, rc):
camera_process['process'].start()
print(f"Camera_process started for {name}: {camera_process['process'].pid}")

event_processor = EventProcessor(CONFIG['cameras'], camera_processes, '/cache', '/clips', event_queue, stop_event)
event_processor = EventProcessor(CONFIG, camera_processes, '/cache', '/clips', event_queue, stop_event)
event_processor.start()

object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue, event_queue, stop_event)
Expand Down
13 changes: 11 additions & 2 deletions frigate/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def refresh_cache(self):
earliest_event = min(self.events_in_process.values(), key=lambda x:x['start_time'])['start_time']
else:
earliest_event = datetime.datetime.now().timestamp()

# if the earliest event exceeds the max seconds, cap it
max_seconds = self.config.get('save_clips', {}).get('max_seconds', 300)
if datetime.datetime.now().timestamp()-earliest_event > max_seconds:
earliest_event = datetime.datetime.now().timestamp()-max_seconds

for f, data in list(self.cached_clips.items()):
if earliest_event-90 > data['start_time']+data['duration']:
Expand Down Expand Up @@ -147,7 +152,11 @@ def run(self):

self.refresh_cache()

save_clips_config = self.config[camera].get('save_clips', {})
save_clips_config = self.config['cameras'][camera].get('save_clips', {})

# if save clips is not enabled for this camera, just continue
if not save_clips_config.get('enabled', False):
continue

# if specific objects are listed for this camera, only save clips for them
if 'objects' in save_clips_config:
Expand All @@ -158,7 +167,7 @@ def run(self):
self.events_in_process[event_data['id']] = event_data

if event_type == 'end':
if save_clips_config.get('enabled', False) and len(self.cached_clips) > 0 and not event_data['false_positive']:
if len(self.cached_clips) > 0 and not event_data['false_positive']:
self.create_clip(camera, event_data, save_clips_config.get('pre_capture', 30))
del self.events_in_process[event_data['id']]

Expand Down

0 comments on commit 7880d24

Please sign in to comment.