Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions backend_py/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ sdbus==0.14.0
sdbus-networkmanager==2.0.0
pyserial
colorlog==6.10.1
# 2.x requires libjpeg-turbo 3, which Debian 12 and Ubuntu 22.04 do not have
PyTurboJPEG==1.8.3
2 changes: 2 additions & 0 deletions backend_py/src/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
StreamFormatModel,
StreamInfoModel,
StreamModel,
StreamScale,
StreamTypeEnum,
UVCControlModel,
V4LControlTypeEnum,
Expand Down Expand Up @@ -81,6 +82,7 @@
"StreamFormatModel",
"StreamInfoModel",
"StreamModel",
"StreamScale",
"StreamTypeEnum",
"UVCControlModel",
"V4LControlTypeEnum",
Expand Down
8 changes: 8 additions & 0 deletions backend_py/src/models/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

from enum import Enum, IntEnum
from typing import Literal

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -47,6 +48,11 @@ class StreamTypeEnum(str, Enum):
RECORDING = "RECORDING"


# Fraction of the recorded resolution a synchronized recording is streamed at
# (1 = full size, 2 = half, ...)
StreamScale = Literal[1, 2, 4, 8]


class H264Mode(IntEnum):
"""
H.264 Mode Enum
Expand Down Expand Up @@ -161,6 +167,7 @@ class StreamModel(BaseModel):
height: int
interval: IntervalModel
enabled: bool
stream_scale: StreamScale = 1

class Config:
from_attributes = True
Expand Down Expand Up @@ -230,6 +237,7 @@ class StreamInfoModel(BaseModel):
encode_type: StreamEncodeTypeEnum
enabled: bool
endpoints: list[StreamEndpointModel]
stream_scale: StreamScale = 1

class Config:
from_attributes = True
Expand Down
2 changes: 2 additions & 0 deletions backend_py/src/models/saved_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
IntervalModel,
StreamEncodeTypeEnum,
StreamEndpointModel,
StreamScale,
StreamTypeEnum,
)

Expand All @@ -35,6 +36,7 @@ class SavedStreamModel(BaseModel):
height: int
interval: IntervalModel
enabled: bool
stream_scale: StreamScale = 1

class Config:
# use_enum_values = True
Expand Down
13 changes: 7 additions & 6 deletions backend_py/src/services/cameras/device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ def configure_device_stream(self, stream_info: StreamInfoModel) -> bool:
encode_type: StreamEncodeTypeEnum = stream_info.encode_type
stream_type: StreamTypeEnum = stream_info.stream_type
endpoints = stream_info.endpoints
stream_scale = stream_info.stream_scale

# For DWVO streams only:
# Endpoints are read live while recording, so changing only them must not
# restart the stream and split the recording into a new file
only_endpoints_changed = (
# Endpoints and stream scale are read live while recording, so changing only
# them must not restart the stream and split the recording into a new file
only_live_settings_changed = (
device.stream_runner.started
and device.stream.stream_type == StreamTypeEnum.RECORDING
and stream_type == StreamTypeEnum.RECORDING
Expand All @@ -241,7 +242,7 @@ def configure_device_stream(self, stream_info: StreamInfoModel) -> bool:
)

device.configure_stream(
encode_type, width, height, interval, stream_type, endpoints
encode_type, width, height, interval, stream_type, endpoints, stream_scale
)

should_start = stream_info.enabled
Expand All @@ -261,8 +262,8 @@ def configure_device_stream(self, stream_info: StreamInfoModel) -> bool:

if should_start:
# We don't restart the stream if it's a dwvo stream that is also recording
# and only changed its endpoints
if not only_endpoints_changed:
# and only changed its endpoints or stream scale
if not only_live_settings_changed:
device.start_stream()
else:
device.stop_stream()
Expand Down
3 changes: 3 additions & 0 deletions backend_py/src/services/cameras/drivers/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def configure_stream(
interval: IntervalModel,
stream_type: StreamTypeEnum,
stream_endpoints: list[StreamEndpointModel] | None = None,
stream_scale: int = 1,
) -> None:
if stream_endpoints is None:
stream_endpoints = []
Expand Down Expand Up @@ -251,6 +252,7 @@ def configure_stream(
self.stream.height = height
self.stream.interval = interval
self.stream.endpoints = stream_endpoints
self.stream.stream_scale = stream_scale
self.stream.encode_type = encode_type
self.stream.stream_type = stream_type

Expand Down Expand Up @@ -321,6 +323,7 @@ def load_settings(self, saved_device: SavedDeviceModel) -> None:
saved_device.stream.interval,
saved_device.stream.stream_type,
saved_device.stream.endpoints,
saved_device.stream.stream_scale,
)

with self._configuration_lock:
Expand Down
2 changes: 2 additions & 0 deletions backend_py/src/services/cameras/stream_engines/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Stream:
default_factory=lambda: IntervalModel(numerator=1, denominator=30)
)
enabled: bool = False
# Synchronized recordings stream at 1/stream_scale of the recorded resolution
stream_scale: int = 1

# Configuration specific
software_h264_bitrate: int = 5000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DWVOVideoFrame,
DWVOWriter,
)
from ..synchronized_camera.jpeg_scaler import AutoJpegScaler
from .base_stream_engine import BaseStreamEngine

# How much video a recording can buffer while the disk catches up
Expand Down Expand Up @@ -72,9 +73,7 @@ def __init__(self, streams, error_callback) -> None:
self.emit_error(e.strerror)

# <AI-Assisted> Custom RTP improves performance compared to RTP class
def _send_frame(
self, frames: list[CopiedFrame], endpoint: StreamEndpointModel
) -> None:
def _send_frame(self, frames: list[bytes], endpoint: StreamEndpointModel) -> None:
# TODO: change protocol to handle more than two cameras
if len(frames) > 2:
self.logger.warning(
Expand All @@ -87,10 +86,10 @@ def _send_frame(
right_frame = frames[1]

# payload headers
frame_header = struct.pack("<QQ", len(left_frame.data), len(right_frame.data))
frame_header = struct.pack("<QQ", len(left_frame), len(right_frame))

# Copy everything into a payload
full_payload = frame_header + left_frame.data + right_frame.data
full_payload = frame_header + left_frame + right_frame
payload_view = memoryview(full_payload)
payload_size = len(payload_view)

Expand Down Expand Up @@ -222,20 +221,39 @@ def capture_loop_(self) -> None:
self.synchronized_camera.stop()

def stream_loop_(self) -> None:
while self._running:
try:
endpoint = self.streams[0].endpoints[0]
except IndexError:
time.sleep(1)
continue
# TODO: do not assume two
try:
(left, right) = self.stream_queue.popleft()
# Only created once a frame needs scaling, since the hardware scaler runs
# its own process
scaler: AutoJpegScaler | None = None
try:
while self._running:
try:
endpoint = self.streams[0].endpoints[0]
except IndexError:
time.sleep(1)
continue
# TODO: do not assume two
try:
(left, right) = self.stream_queue.popleft()
except IndexError:
time.sleep(1 / self.streams[0].interval.denominator)
continue

# Read live, so it can change without splitting the recording.
# Only the stream is scaled, the recording keeps the full frames
scale = self.streams[0].stream_scale if self.is_recording else 1
frames = [left.data, right.data]
if scale > 1:
if scaler is None:
scaler = AutoJpegScaler()
frames = [
scaler.scale(frame.data, frame.width, frame.height, scale)
for frame in (left, right)
]
# TODO: make less scuffed
self._send_frame([left, right], endpoint)
except IndexError:
time.sleep(1 / self.streams[0].interval.denominator)
continue
self._send_frame(frames, endpoint)
finally:
if scaler is not None:
scaler.close()

def record_loop_(self) -> None:
leader = self.streams[0]
Expand Down
Loading
Loading