Skip to content
Merged
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
18 changes: 17 additions & 1 deletion backend_py/src/services/cameras/device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ def configure_device_stream(self, stream_info: StreamInfoModel) -> bool:
stream_type: StreamTypeEnum = stream_info.stream_type
endpoints = stream_info.endpoints

# 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 = (
device.stream_runner.started
and device.stream.stream_type == StreamTypeEnum.RECORDING
and stream_type == StreamTypeEnum.RECORDING
and device.stream.encode_type == encode_type
and device.stream.width == width
and device.stream.height == height
and device.stream.interval == interval
)

device.configure_stream(
encode_type, width, height, interval, stream_type, endpoints
)
Expand All @@ -247,7 +260,10 @@ def configure_device_stream(self, stream_info: StreamInfoModel) -> bool:
device.emit("updated")

if should_start:
device.start_stream()
# 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:
device.start_stream()
else:
device.stop_stream()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ def __init__(self, streams, error_callback) -> None:
self.is_recording = streams[0].stream_type == StreamTypeEnum.RECORDING

self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# A live stream only wants the newest frames, but a recording has to keep
# every frame it can
self.frame_queue: collections.deque[tuple[CopiedFrame, CopiedFrame]] = (
# Two queues, one for streaming and one for recording
# Recording prioritizes saving every frame possible
self.stream_queue: collections.deque[tuple[CopiedFrame, CopiedFrame]] = (
collections.deque(maxlen=2)
)
self.record_queue: collections.deque[tuple[CopiedFrame, CopiedFrame]] = (
collections.deque(
maxlen=(
streams[0].interval.denominator * RECORDING_BUFFER_SECONDS
if self.is_recording
else 2
)
maxlen=streams[0].interval.denominator * RECORDING_BUFFER_SECONDS
)
)

Expand All @@ -47,6 +46,7 @@ def __init__(self, streams, error_callback) -> None:
self.sequence_number = 1

self.stream_thread: threading.Thread | None = None
self.record_thread: threading.Thread | None = None
self.capture_thread: threading.Thread | None = None
self._running = False

Expand Down Expand Up @@ -160,11 +160,14 @@ def start(self) -> None:
self.capture_thread.start()

# We cannot handle more than 2 synchronized streams yet in the protocol
self.stream_thread = threading.Thread(
target=self.record_loop_ if self.is_recording else self.stream_loop_
)
# Streaming always runs, so a recording can be watched live
self.stream_thread = threading.Thread(target=self.stream_loop_)
self.stream_thread.start()

if self.is_recording:
self.record_thread = threading.Thread(target=self.record_loop_)
self.record_thread.start()

self.monitor_thread = threading.Thread(target=self.monitor_)
self.monitor_thread.start()

Expand All @@ -182,6 +185,8 @@ def stop(self) -> None:
self.capture_thread.join()
if self.stream_thread:
self.stream_thread.join()
if self.record_thread:
self.record_thread.join()

self.exit_event.set()
if self.monitor_thread:
Expand All @@ -207,10 +212,13 @@ def capture_loop_(self) -> None:
time.sleep(1 / self.streams[0].interval.denominator)
continue

if self.is_recording and len(self.frame_queue) == self.frame_queue.maxlen:
# The writer fell behind, so the oldest unsaved frame is lost
self.emit("frame_drop")
self.frame_queue.append((frames[0], frames[1]))
pair = (frames[0], frames[1])
if self.is_recording:
if len(self.record_queue) == self.record_queue.maxlen:
# The writer fell behind, so the oldest unsaved frame is lost
self.emit("frame_drop")
self.record_queue.append(pair)
self.stream_queue.append(pair)
self.synchronized_camera.stop()

def stream_loop_(self) -> None:
Expand All @@ -222,7 +230,7 @@ def stream_loop_(self) -> None:
continue
# TODO: do not assume two
try:
(left, right) = self.frame_queue.popleft()
(left, right) = self.stream_queue.popleft()
# TODO: make less scuffed
self._send_frame([left, right], endpoint)
except IndexError:
Expand Down Expand Up @@ -255,9 +263,9 @@ def record_loop_(self) -> None:

# Capture stops first on shutdown, so keep going until everything
# it queued has been written
while self._is_capturing() or self.frame_queue:
while self._is_capturing() or self.record_queue:
try:
frames = self.frame_queue.popleft()
frames = self.record_queue.popleft()
except IndexError:
time.sleep(1 / leader.interval.denominator)
continue
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/dwe/cameras/stream/stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const CameraStream = ({ bus_id }: { bus_id: string }) => {
const controls = device.controls;

const isManaged = device.is_managed || device.is_externally_managed;
const isSynchronized = canLead(device) && device.followers.length > 0;

const configureStream = useDeviceStore((state) => state.configureStream);
const isStreamLoading = useDeviceStore(
Expand Down Expand Up @@ -164,9 +165,11 @@ export const CameraStream = ({ bus_id }: { bus_id: string }) => {
</div>
</div>

{!isManaged && device.stream.stream_type === "UDP" && (
<EndpointList bus_id={bus_id} />
)}
{/* Synchronized cameras can be streamed while they record */}
{!isManaged &&
(device.stream.stream_type === "UDP" || isSynchronized) && (
<EndpointList bus_id={bus_id} />
)}

{!isManaged && (
<Button
Expand Down
Loading