Refactor audio to use internal zmq

This commit is contained in:
Nicolas Mowen 2025-03-11 16:51:19 -06:00
parent ad0df9c914
commit c04b2eec5a

View File

@ -2,17 +2,22 @@
import datetime import datetime
import logging import logging
import random
import string
import threading import threading
import time import time
from typing import Tuple from typing import Tuple
import numpy as np import numpy as np
import requests
import frigate.util as util import frigate.util as util
from frigate.camera import CameraMetrics from frigate.camera import CameraMetrics
from frigate.comms.config_updater import ConfigSubscriber from frigate.comms.config_updater import ConfigSubscriber
from frigate.comms.detections_updater import DetectionPublisher, DetectionTypeEnum from frigate.comms.detections_updater import DetectionPublisher, DetectionTypeEnum
from frigate.comms.event_metadata_updater import (
EventMetadataPublisher,
EventMetadataTypeEnum,
)
from frigate.comms.inter_process import InterProcessRequestor from frigate.comms.inter_process import InterProcessRequestor
from frigate.config import CameraConfig, CameraInput, FfmpegConfig from frigate.config import CameraConfig, CameraInput, FfmpegConfig
from frigate.const import ( from frigate.const import (
@ -21,7 +26,6 @@ from frigate.const import (
AUDIO_MAX_BIT_RANGE, AUDIO_MAX_BIT_RANGE,
AUDIO_MIN_CONFIDENCE, AUDIO_MIN_CONFIDENCE,
AUDIO_SAMPLE_RATE, AUDIO_SAMPLE_RATE,
FRIGATE_LOCALHOST,
) )
from frigate.ffmpeg_presets import parse_preset_input from frigate.ffmpeg_presets import parse_preset_input
from frigate.log import LogPipe from frigate.log import LogPipe
@ -139,6 +143,7 @@ class AudioEventMaintainer(threading.Thread):
f"config/enabled/{camera.name}", True f"config/enabled/{camera.name}", True
) )
self.detection_publisher = DetectionPublisher(DetectionTypeEnum.audio) self.detection_publisher = DetectionPublisher(DetectionTypeEnum.audio)
self.event_metadata_publisher = EventMetadataPublisher()
self.was_enabled = camera.enabled self.was_enabled = camera.enabled
@ -207,24 +212,33 @@ class AudioEventMaintainer(threading.Thread):
datetime.datetime.now().timestamp() datetime.datetime.now().timestamp()
) )
else: else:
now = datetime.datetime.now().timestamp()
rand_id = "".join(
random.choices(string.ascii_lowercase + string.digits, k=6)
)
event_id = f"{now}-{rand_id}"
self.requestor.send_data(f"{self.config.name}/audio/{label}", "ON") self.requestor.send_data(f"{self.config.name}/audio/{label}", "ON")
resp = requests.post( self.event_metadata_publisher.publish(
f"{FRIGATE_LOCALHOST}/api/events/{self.config.name}/{label}/create", EventMetadataTypeEnum.manual_event_end,
json={"duration": None, "score": score, "source_type": "audio"}, (
now,
self.config.name,
label,
event_id,
True,
score,
None,
None,
"audio",
{},
),
) )
if resp.status_code == 200:
event_id = resp.json()["event_id"]
self.detections[label] = { self.detections[label] = {
"id": event_id, "id": event_id,
"label": label, "label": label,
"last_detection": datetime.datetime.now().timestamp(), "last_detection": now,
} }
else:
self.logger.warning(
f"Failed to create audio event with status code {resp.status_code}"
)
def expire_detections(self) -> None: def expire_detections(self) -> None:
now = datetime.datetime.now().timestamp() now = datetime.datetime.now().timestamp()
@ -241,17 +255,11 @@ class AudioEventMaintainer(threading.Thread):
f"{self.config.name}/audio/{detection['label']}", "OFF" f"{self.config.name}/audio/{detection['label']}", "OFF"
) )
resp = requests.put( self.event_metadata_publisher.publish(
f"{FRIGATE_LOCALHOST}/api/events/{detection['id']}/end", EventMetadataTypeEnum.manual_event_end,
json={"end_time": detection["last_detection"]}, (detection["id"], detection["last_detection"]),
) )
if resp.status_code == 200:
self.detections[detection["label"]] = None self.detections[detection["label"]] = None
else:
self.logger.warning(
f"Failed to end audio event {detection['id']} with status code {resp.status_code}"
)
def expire_all_detections(self) -> None: def expire_all_detections(self) -> None:
"""Immediately end all current detections""" """Immediately end all current detections"""
@ -259,16 +267,11 @@ class AudioEventMaintainer(threading.Thread):
for label, detection in list(self.detections.items()): for label, detection in list(self.detections.items()):
if detection: if detection:
self.requestor.send_data(f"{self.config.name}/audio/{label}", "OFF") self.requestor.send_data(f"{self.config.name}/audio/{label}", "OFF")
resp = requests.put( self.event_metadata_publisher.publish(
f"{FRIGATE_LOCALHOST}/api/events/{detection['id']}/end", EventMetadataTypeEnum.manual_event_end,
json={"end_time": now}, (detection["id"], now),
) )
if resp.status_code == 200:
self.detections[label] = None self.detections[label] = None
else:
self.logger.warning(
f"Failed to end audio event {detection['id']} with status code {resp.status_code}"
)
def start_or_restart_ffmpeg(self) -> None: def start_or_restart_ffmpeg(self) -> None:
self.audio_listener = start_or_restart_ffmpeg( self.audio_listener = start_or_restart_ffmpeg(