From 9e3bba7b1f54526c3721f45fcc7eb890e1258086 Mon Sep 17 00:00:00 2001 From: Teng Wei Date: Sun, 1 Nov 2026 09:48:14 -0800 Subject: [PATCH 1/7] Update camera_streamer example Signed-off-by: Teng Wei --- examples/camera_streamer/AGENTS.md | 53 +- examples/camera_streamer/README.md | 503 ++++-------------- .../camera_streamer/argus_sender/.gitignore | 7 - .../argus_sender/CMakeLists.txt | 79 ++- .../camera_streamer/argus_sender/README.md | 328 ++++++++++++ .../camera_streamer/argus_sender/TESTING.md | 61 +++ .../argus_sender/WIRE_FORMAT.md | 8 +- .../camera_streamer/argus_sender/build.sh | 58 ++ .../argus_sender/cmake/Tests.cmake | 103 +++- .../argus_sender/cmake/YamlCpp.cmake | 15 - .../argus_sender/configs/cameras.yaml | 75 +++ .../configs/mixed_streams.example.yaml | 47 ++ .../deploy/10-camera-persistent-journal.conf | 18 + .../argus_sender/deploy/README.md | 110 ++++ .../deploy/camera-argus-sender.service.in | 30 ++ .../deploy/camera-chrony-agx.conf.in | 16 + .../deploy/camera-chrony-host.conf.in | 18 + .../deploy/camera-time-sync.service.in | 24 + .../deploy/camera-vi-error-trace.service.in | 27 + .../deploy/deploy_argus_sender.sh | 182 +++++++ .../argus_sender/deploy/deploy_platform.sh | 36 ++ .../argus_sender/deploy/deployment_state.sh | 137 +++++ .../deploy/lock_jetson_camera_clocks.sh | 81 +++ .../remaps/.gitattributes} | 2 +- .../argus_sender/remaps/README.md | 51 ++ .../argus_sender/remaps/cam4.remap | 3 + .../argus_sender/remaps/cam4.remap.license | 2 + .../argus_sender/remaps/cam5.remap | 3 + .../argus_sender/remaps/cam5.remap.license | 2 + .../remaps/example_calibration.json | 111 ++++ .../remaps/example_calibration.json.license | 2 + .../argus_sender/remaps/generate_example.py | 103 ++++ .../argus_sender/remaps/manifest.json | 98 ++++ .../argus_sender/remaps/manifest.json.license | 2 + examples/camera_streamer/argus_sender/run.sh | 33 ++ .../argus_sender/src/Avs1Wire.cpp | 30 +- .../argus_sender/src/Avs1Wire.h | 2 +- .../argus_sender/src/CMakeLists.txt | 57 -- .../argus_sender/src/CameraCatalog.cpp | 2 +- .../argus_sender/src/CameraCatalog.h | 2 +- .../argus_sender/src/CaptureCoordinator.cpp | 2 +- .../argus_sender/src/CaptureCoordinator.h | 2 +- .../argus_sender/src/CaptureDomainPlan.cpp | 2 +- .../argus_sender/src/CaptureDomainPlan.h | 2 +- .../argus_sender/src/CaptureManager.cpp | 84 ++- .../argus_sender/src/CaptureManager.h | 2 +- .../src/CapturePriorityPolicy.cpp | 2 +- .../argus_sender/src/CapturePriorityPolicy.h | 2 +- .../argus_sender/src/Config.cpp | 9 +- .../camera_streamer/argus_sender/src/Config.h | 2 +- .../argus_sender/src/EGLGlobal.cpp | 33 +- .../argus_sender/src/EGLGlobal.h | 6 +- .../argus_sender/src/EncodedFrameSink.h | 2 +- .../camera_streamer/argus_sender/src/Error.h | 6 +- .../argus_sender/src/FrameAlignmentPolicy.cpp | 2 +- .../argus_sender/src/FrameAlignmentPolicy.h | 2 +- .../argus_sender/src/FrameBundleAssembler.cpp | 13 +- .../argus_sender/src/FrameBundleAssembler.h | 2 +- .../argus_sender/src/FrameCopyWorker.cpp | 2 +- .../argus_sender/src/FrameCopyWorker.h | 2 +- .../argus_sender/src/HevcEncoder.cpp | 2 +- .../argus_sender/src/HevcEncoder.h | 2 +- .../argus_sender/src/MixedRateScheduler.cpp | 2 +- .../argus_sender/src/MixedRateScheduler.h | 2 +- .../argus_sender/src/NvBufSurfaceLite.cpp | 6 +- .../argus_sender/src/NvRemap.cpp | 2 +- .../argus_sender/src/NvRemap.h | 4 +- .../argus_sender/src/OutputGeometry.cpp | 8 +- .../argus_sender/src/OutputGeometry.h | 2 +- .../argus_sender/src/PacketSink.h | 2 +- .../argus_sender/src/PriorityFrameHandoff.h | 2 +- .../argus_sender/src/Runtime.cpp | 2 +- .../argus_sender/src/Runtime.h | 2 +- .../argus_sender/src/SenderSession.cpp | 2 +- .../argus_sender/src/SenderSession.h | 2 +- .../src/StereoAlignmentPolicy.cpp | 2 +- .../argus_sender/src/StereoAlignmentPolicy.h | 2 +- .../argus_sender/src/StereoTiming.cpp | 2 +- .../argus_sender/src/StereoTiming.h | 2 +- .../argus_sender/src/StreamProcessor.cpp | 2 +- .../argus_sender/src/StreamProcessor.h | 2 +- .../argus_sender/src/TcpTransport.cpp | 2 +- .../argus_sender/src/TcpTransport.h | 2 +- .../camera_streamer/argus_sender/src/main.cpp | 2 +- .../cpp => argus_sender/tests}/TestHarness.h | 2 +- .../tests/hardware/avs1_smoke.py | 36 +- .../tests}/test_alignment_policy.cpp | 2 +- .../tests}/test_avs1_wire.cpp | 100 +--- .../tests}/test_bundle_assembler.cpp | 95 +--- .../tests}/test_capture_domain_plan.cpp | 2 +- .../tests}/test_capture_priority_policy.cpp | 2 +- .../tests}/test_config.cpp | 25 +- .../argus_sender/tests/test_deployment.py | 329 ++++++++++++ .../tests}/test_frame_alignment_policy.cpp | 2 +- .../tests}/test_geometry_scheduler.cpp | 30 +- .../tests}/test_nv_remap.cpp | 2 +- .../tests}/test_priority_frame_handoff.cpp | 2 +- .../tests}/test_rate_limiter.cpp | 2 +- .../tests}/test_tcp_transport.cpp | 2 +- .../camera_receiver/.gitignore | 3 - .../camera_streamer/camera_receiver/README.md | 211 ++++++++ .../camera_receiver/avs_wire.py | 9 +- .../camera_receiver/camera_receiver.py | 101 +--- .../camera_receiver/codec/CMakeLists.txt | 2 + .../camera_receiver/codec/__init__.py | 4 +- .../camera_receiver/codec/build.sh | 60 +++ .../camera_receiver/codec/codec_bindings.cpp | 135 ++++- .../camera_receiver/codec/cuda_buffers.hpp | 155 ------ .../codec/{ => inc}/nv_video_decoder.hpp | 3 - .../codec/nv_video_decoder.cpp | 80 +-- .../camera_receiver/configs/receiver.yaml | 58 ++ .../camera_receiver/cuda_ipc.py | 60 +-- .../camera_receiver/nv_video_decode.py | 8 +- .../camera_streamer/camera_receiver/run.sh | 25 + .../camera_streamer/camera_receiver/setup.sh | 86 +++ .../camera_receiver/slot_guard.py | 52 -- .../tests}/conftest.py | 7 +- .../tests}/test_nv_video_decode.py | 0 .../tests}/test_reconnect.py | 5 +- .../tests}/test_wire_format.py | 2 +- .../configs/s56-mono-1080p30.yaml | 45 -- .../configs/s56-mono-1080p60.yaml | 45 -- .../configs/s56-mono-2400x1440-30.yaml | 45 -- .../configs/s56-mono-2560x1984-30.yaml | 45 -- .../configs/s56-mono-720p30.yaml | 45 -- .../configs/s56-mono-720p60.yaml | 45 -- .../configs/s56-stereo-1080p60.yaml | 48 -- .../configs/s56-stereo-2560x1984-30.yaml | 48 -- .../configs/shw5g-mono-2400x1440-60.yaml | 47 -- .../configs/shw5g-mono-2560x1984-30.yaml | 47 -- .../cuda_ipc_consumer/.gitignore | 3 - .../cuda_ipc_consumer/README.md | 114 ++++ .../cuda_ipc_consumer/consumer.py | 153 ++---- .../cuda_ipc_consumer/latency_check.py | 106 ++++ .../camera_streamer/cuda_ipc_consumer/run.sh | 18 + .../cuda_ipc_consumer/setup.sh | 57 ++ examples/camera_streamer/pyproject.toml | 20 - .../tests/cpp/alignment_policy/CMakeLists.txt | 8 - .../tests/cpp/avs1_wire/CMakeLists.txt | 7 - .../tests/cpp/bundle_assembler/CMakeLists.txt | 11 - .../cpp/capture_domain_plan/CMakeLists.txt | 7 - .../capture_priority_policy/CMakeLists.txt | 7 - .../tests/cpp/config/CMakeLists.txt | 11 - .../cpp/frame_alignment_policy/CMakeLists.txt | 7 - .../cpp/geometry_scheduler/CMakeLists.txt | 8 - .../tests/cpp/nv_remap/CMakeLists.txt | 7 - .../cpp/priority_frame_handoff/CMakeLists.txt | 6 - .../tests/cpp/rate_limiter/CMakeLists.txt | 8 - .../tests/cpp/tcp_transport/CMakeLists.txt | 10 - examples/camera_streamer/tests/pyproject.toml | 13 - .../tests/python/test_avs1_validation.py | 72 --- .../tests/python/test_backpressure.py | 131 ----- .../tests/python/test_consumer_cleanup.py | 114 ---- .../tests/python/test_cuda_buffers.py | 77 --- .../tests/python/test_decoder_dimensions.py | 79 --- .../tests/python/test_shared_config.py | 88 --- .../tests/python/test_slot_guard.py | 94 ---- .../tests/python/test_smoke_config.py | 41 -- .../tests/python/test_worker_stream.py | 70 --- 159 files changed, 3374 insertions(+), 2892 deletions(-) create mode 100644 examples/camera_streamer/argus_sender/README.md create mode 100644 examples/camera_streamer/argus_sender/TESTING.md create mode 100755 examples/camera_streamer/argus_sender/build.sh delete mode 100644 examples/camera_streamer/argus_sender/cmake/YamlCpp.cmake create mode 100644 examples/camera_streamer/argus_sender/configs/cameras.yaml create mode 100644 examples/camera_streamer/argus_sender/configs/mixed_streams.example.yaml create mode 100644 examples/camera_streamer/argus_sender/deploy/10-camera-persistent-journal.conf create mode 100644 examples/camera_streamer/argus_sender/deploy/README.md create mode 100644 examples/camera_streamer/argus_sender/deploy/camera-argus-sender.service.in create mode 100644 examples/camera_streamer/argus_sender/deploy/camera-chrony-agx.conf.in create mode 100644 examples/camera_streamer/argus_sender/deploy/camera-chrony-host.conf.in create mode 100644 examples/camera_streamer/argus_sender/deploy/camera-time-sync.service.in create mode 100644 examples/camera_streamer/argus_sender/deploy/camera-vi-error-trace.service.in create mode 100755 examples/camera_streamer/argus_sender/deploy/deploy_argus_sender.sh create mode 100755 examples/camera_streamer/argus_sender/deploy/deploy_platform.sh create mode 100755 examples/camera_streamer/argus_sender/deploy/deployment_state.sh create mode 100755 examples/camera_streamer/argus_sender/deploy/lock_jetson_camera_clocks.sh rename examples/camera_streamer/{__init__.py => argus_sender/remaps/.gitattributes} (70%) create mode 100644 examples/camera_streamer/argus_sender/remaps/README.md create mode 100644 examples/camera_streamer/argus_sender/remaps/cam4.remap create mode 100644 examples/camera_streamer/argus_sender/remaps/cam4.remap.license create mode 100644 examples/camera_streamer/argus_sender/remaps/cam5.remap create mode 100644 examples/camera_streamer/argus_sender/remaps/cam5.remap.license create mode 100644 examples/camera_streamer/argus_sender/remaps/example_calibration.json create mode 100644 examples/camera_streamer/argus_sender/remaps/example_calibration.json.license create mode 100755 examples/camera_streamer/argus_sender/remaps/generate_example.py create mode 100644 examples/camera_streamer/argus_sender/remaps/manifest.json create mode 100644 examples/camera_streamer/argus_sender/remaps/manifest.json.license create mode 100755 examples/camera_streamer/argus_sender/run.sh delete mode 100644 examples/camera_streamer/argus_sender/src/CMakeLists.txt rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/TestHarness.h (94%) rename examples/camera_streamer/{ => argus_sender}/tests/hardware/avs1_smoke.py (85%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_alignment_policy.cpp (96%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_avs1_wire.cpp (62%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_bundle_assembler.cpp (72%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_capture_domain_plan.cpp (96%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_capture_priority_policy.cpp (92%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_config.cpp (90%) create mode 100644 examples/camera_streamer/argus_sender/tests/test_deployment.py rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_frame_alignment_policy.cpp (97%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_geometry_scheduler.cpp (58%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_nv_remap.cpp (98%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_priority_frame_handoff.cpp (96%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_rate_limiter.cpp (97%) rename examples/camera_streamer/{tests/cpp => argus_sender/tests}/test_tcp_transport.cpp (98%) create mode 100644 examples/camera_streamer/camera_receiver/README.md create mode 100755 examples/camera_streamer/camera_receiver/codec/build.sh delete mode 100644 examples/camera_streamer/camera_receiver/codec/cuda_buffers.hpp rename examples/camera_streamer/camera_receiver/codec/{ => inc}/nv_video_decoder.hpp (97%) create mode 100644 examples/camera_streamer/camera_receiver/configs/receiver.yaml create mode 100755 examples/camera_streamer/camera_receiver/run.sh create mode 100755 examples/camera_streamer/camera_receiver/setup.sh delete mode 100644 examples/camera_streamer/camera_receiver/slot_guard.py rename examples/camera_streamer/{tests/python => camera_receiver/tests}/conftest.py (54%) rename examples/camera_streamer/{tests/python => camera_receiver/tests}/test_nv_video_decode.py (100%) rename examples/camera_streamer/{tests/python => camera_receiver/tests}/test_reconnect.py (96%) rename examples/camera_streamer/{tests/python => camera_receiver/tests}/test_wire_format.py (99%) delete mode 100644 examples/camera_streamer/configs/s56-mono-1080p30.yaml delete mode 100644 examples/camera_streamer/configs/s56-mono-1080p60.yaml delete mode 100644 examples/camera_streamer/configs/s56-mono-2400x1440-30.yaml delete mode 100644 examples/camera_streamer/configs/s56-mono-2560x1984-30.yaml delete mode 100644 examples/camera_streamer/configs/s56-mono-720p30.yaml delete mode 100644 examples/camera_streamer/configs/s56-mono-720p60.yaml delete mode 100644 examples/camera_streamer/configs/s56-stereo-1080p60.yaml delete mode 100644 examples/camera_streamer/configs/s56-stereo-2560x1984-30.yaml delete mode 100644 examples/camera_streamer/configs/shw5g-mono-2400x1440-60.yaml delete mode 100644 examples/camera_streamer/configs/shw5g-mono-2560x1984-30.yaml create mode 100644 examples/camera_streamer/cuda_ipc_consumer/README.md create mode 100755 examples/camera_streamer/cuda_ipc_consumer/latency_check.py create mode 100755 examples/camera_streamer/cuda_ipc_consumer/run.sh create mode 100755 examples/camera_streamer/cuda_ipc_consumer/setup.sh delete mode 100644 examples/camera_streamer/pyproject.toml delete mode 100644 examples/camera_streamer/tests/cpp/alignment_policy/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/avs1_wire/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/bundle_assembler/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/capture_domain_plan/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/capture_priority_policy/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/config/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/frame_alignment_policy/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/geometry_scheduler/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/nv_remap/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/priority_frame_handoff/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/rate_limiter/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/cpp/tcp_transport/CMakeLists.txt delete mode 100644 examples/camera_streamer/tests/pyproject.toml delete mode 100644 examples/camera_streamer/tests/python/test_avs1_validation.py delete mode 100644 examples/camera_streamer/tests/python/test_backpressure.py delete mode 100644 examples/camera_streamer/tests/python/test_consumer_cleanup.py delete mode 100644 examples/camera_streamer/tests/python/test_cuda_buffers.py delete mode 100644 examples/camera_streamer/tests/python/test_decoder_dimensions.py delete mode 100644 examples/camera_streamer/tests/python/test_shared_config.py delete mode 100644 examples/camera_streamer/tests/python/test_slot_guard.py delete mode 100644 examples/camera_streamer/tests/python/test_smoke_config.py delete mode 100644 examples/camera_streamer/tests/python/test_worker_stream.py diff --git a/examples/camera_streamer/AGENTS.md b/examples/camera_streamer/AGENTS.md index 62bff46128..b8459275f0 100644 --- a/examples/camera_streamer/AGENTS.md +++ b/examples/camera_streamer/AGENTS.md @@ -3,45 +3,16 @@ SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All SPDX-License-Identifier: Apache-2.0 --> -# Standalone GMSL/Argus streamer +# Standalone camera streamer -- Keep this example scoped to GMSL/Argus capture, AVS1/TCP, NVDEC and CUDA IPC. - Do not restore unrelated camera wrappers, USB senders, RTP/recording paths or - rig-specific deployment assets without an explicit scope change. -- Preserve upstream notices when adding SPDX headers; missing SPDX does not mean - a file lacks a license. Keep third-party exceptions separate from Apache-2.0 code. -- Keep the native sender and standalone receiver independent of robot control - software and the Isaac Capture runtime. Use the isaaccapture logger tree without importing it. -- Document Install, Preview, Send, Receive and Viewer stages with explicit - commands, without a launcher wrapper. Keep operator instructions in this README - rather than duplicating them across subdirectories. Local Argus preview is a separate application - and must release the sensor before network capture starts. -- Use HOST_IP/ROBOT_IP discovered for the installation, never developer addresses. - Explain source transfer before referencing robot deployment directories. -- Select sensor modes from --list-modes and match PWM to capture rate. Record - hardware validation evidence in the review; keep operator mode tables focused on configuration. -- Deploy directory renames with documentation and rebuild stale CMake - caches. Do not transfer virtual environments, builds or caches between machines. -- Receiver tests must establish the standalone import path. Check runtime imports - and native/receiver tests after trimming dependencies, not just formatting. -- Preserve encoded frame dependencies across disconnects. CUDA IPC handles stay - local to the receiving machine; they are not network transport handles. -- Preview loops must handle window-close events before rendering another frame. -- CuPy pixel diagnostics need matching NVRTC; exclude first-use JIT from timing. -- Normalize final newlines and imported executable permissions before staging. -- Describe camera support generically; separate upstream device testing from local validation. -- Use one shared profile per device/resolution; define wire geometry and port once. -- Keep tests under the example-level tests/ directory, split by language and hardware needs. -- Public setup instructions target main; describe pre-merge branch overrides separately. -- Do not infer Argus mode indices from vendor specifications or another model; keep unknown modes explicit in profiles. -- Keep camera YAMLs directly under configs/ with model-prefixed filenames so profile selection needs no subdirectory. -- Use matching columns for all camera profile tables so resolution, frame rate and capture constraints are directly comparable. - Keep profile alternatives in those tables; show PROFILE assignments in fenced code blocks with an inline selection hint. -- Preserve HEVC ordering through TCP enqueue and bound receiver work queues; backpressure must remain interruptible during shutdown. -- Verify unresolved review findings against the current PR head and regression tests; - an open thread can describe code already fixed in an earlier commit. -- Concurrency regression tests must synchronize inside the operation under test; - a pre-call flag and a fixed sleep do not establish lock contention. -- Keep portable tests buildable without Jetson dependencies and each review independently buildable. -- Keep argus_sender, camera_receiver and cuda_ipc_consumer directly under camera_streamer; - do not group standalone transport components under the TeleViz visualization name. +- Preserve the standalone Argus → AVS1/TCP → NVDEC → CUDA IPC boundary; these tools must not import robot control packages or the Isaac Capture runtime. +- Deploy to a driver-ready Orin/Thor without assuming a vendor directory or existing camera services. Snapshot and restore camera-streamer artifacts and the platform service state changed for camera ownership; leave driver configuration to the BSP. +- Keep sender-only calibration assets, remaps and deployment helpers under argus_sender/. Resolve remap paths relative to the sender config so copying the sender includes its calibration assets. +- Treat the shipped calibration as an approximate viewing example. Preserve the 2x wrist lookup grid and box filtering when moving or regenerating it. +- Keep the sender build's Argus linkage consistent with the launcher's daemon ownership checks. The build wrapper selects single-process Argus; direct CMake builds also support multiprocess mode. +- Standalone Python loggers use the isaaccapture namespace without importing the runtime. +- The imported Argus sample helpers `Error.h` and `EGLGlobal.h` retain BSD-3-Clause notices and SPDX identifiers. Do not assign the example's Apache default to those files. +- Preserve upstream notices. Downloaded SDK sources remain outside this source export and retain their own license. +- Preserve the source layout when exporting standalone components; keep each component's tests inside its own folder. Reserve example-level tests/ for checks spanning components. +- Minimize differences from the source export: preserve CMake target definitions, include/header locations and flat component tests. This standalone import is an exception to the repository's CMake/include conventions; C++ formatting follows the repository's clang-format-14 configuration. Do not add build options or refactor working code solely for local verification. +- Normalize imported executable modes and final newlines before staging. diff --git a/examples/camera_streamer/README.md b/examples/camera_streamer/README.md index 113fc39652..ba72c67d6d 100644 --- a/examples/camera_streamer/README.md +++ b/examples/camera_streamer/README.md @@ -3,442 +3,161 @@ SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All SPDX-License-Identifier: Apache-2.0 --> -# Isaac Camera Streamer — GMSL / Argus +# Camera streamer -Stream Argus-compatible GMSL stereo video from a Jetson Orin robot computer to an NVIDIA -workstation: **Argus → hardware HEVC → AVS1/TCP → NVDEC → CUDA IPC viewer**. -This standalone example runs independently of robot control software. +Standalone camera streaming for a trusted local LAN. Capture runs on an NVIDIA +Jetson AGX Orin or Thor with an Argus-compatible camera driver. The reference +setup has been used with Sensing SHW5G GMSL cameras; other cameras need their own +driver, sensor-mode selection, and calibration. The receiver and CUDA IPC +consumer run on a Linux host with an NVIDIA GPU, using Ubuntu 22.04 or 24.04. -| Camera | Robot Install | Robot Preview | Robot Send | Host Receive | Host Viewer | -|---|---|---|---|---|---| -| Sensing SHW5G / S56 (GMSL) | Required: driver + native build | Optional: external Argus app | Jetson HEVC/TCP | NVDEC receiver | Stereo desktop view | - -Complete common setup once, then follow the five GMSL sections below. -Network streaming uses three terminals: Robot Send, Host Receive, Host Viewer. - -## Common setup — once per computer - -### Identify the two network addresses - -On **both computers**, run: - -```bash -ip -brief -4 address -``` - -Choose the IPv4 address of the Ethernet, Wi-Fi, or VPN interface that connects -these two sites. Use the address without its `/prefix` suffix. Do not select -loopback (`127.0.0.1`) or a Docker-only interface. For a shared LAN, choose the -addresses on that LAN; for routed sites, choose mutually reachable addresses. - -Replace the placeholders below and export both values in each terminal where -you run the network commands: - -```bash -export HOST_IP="" -export ROBOT_IP="" -``` - -`HOST_IP` always means the workstation and `ROBOT_IP` always means the Orin, -regardless of which computer runs the command. Exports do not carry into a new -terminal or SSH session; repeat them there. You can check routing from each side: - -```bash -# On the Distributed Cloud workstation: -ip route get "${ROBOT_IP:?Set ROBOT_IP first}" -# On the Robot Site: -ip route get "${HOST_IP:?Set HOST_IP first}" -``` - -The receiver connects to the sender at `ROBOT_IP:5000`. The sender listens on -all local interfaces by default, so it does not need `HOST_IP` as an argument. - -### Put the source on both computers - -`~/isaac-camera-streamer` is a deployment directory. The copy commands below -create it explicitly; the robot does not need a separate repository there. -Choose **one** of the following approaches. - -**Recommended for a disk-constrained robot: clone on the host, copy the example.** -The robot does not need a full Isaac Capture clone. Start with a host -checkout containing `examples/camera_streamer` (Git and Git LFS required). -The commands use `main` for the merged example. Before merge, reviewers must -substitute the pull request's head branch for `main`. - -```bash -# On the Distributed Cloud workstation; skip cloning if you have this checkout. -export CAMERA_REPO_URL="https://github.com/NVIDIA/IsaacCapture.git" -export CAMERA_BRANCH="main" -GIT_LFS_SKIP_SMUDGE=1 git clone --single-branch --branch "$CAMERA_BRANCH" \ - "$CAMERA_REPO_URL" "$HOME/IsaacCapture" -cd ~/IsaacCapture -git lfs pull --include='examples/camera_streamer/**' --exclude='' -``` - -With SSH access configured and `rsync` installed on both computers, run this on -the **host**. Replace the username with the account you use on the robot: - -```bash -export ROBOT_USER="" -ssh "${ROBOT_USER}@${ROBOT_IP:?Set ROBOT_IP first}" \ - 'mkdir -p "$HOME/isaac-camera-streamer/camera_streamer"' -rsync -av --exclude='.venv*/' --exclude='build/' \ - --exclude='__pycache__/' --exclude='.pytest_cache/' \ - --exclude='*.egg-info/' --exclude='*.so' \ - "$HOME/IsaacCapture/examples/camera_streamer/" \ - "${ROBOT_USER}@${ROBOT_IP}:isaac-camera-streamer/camera_streamer/" -``` - -The trailing source slash copies the example's contents into the destination. -Run this transfer again to deploy source updates; it overwrites matching files, -so keep robot-specific profiles outside that directory or copy edits back first. -Virtual environments and native builds stay on their own machine: workstation -binaries cannot be reused on the ARM64 Orin. - -**Alternative: clone directly on both computers.** On the robot, run the same -clone and LFS commands above using the same branch/revision as the host. No SSH -file transfer or `isaac-camera-streamer` directory is then needed. Use -`~/IsaacCapture/examples/camera_streamer` instead of the deployment path in the -Robot Site commands below. This approach uses more disk space for Git history -and the rest of the checkout. - -### Test a clean robot deployment - -Stop the sender and any local preview, and save custom profiles or captures outside -`~/isaac-camera-streamer` before deleting that directory. Repeating the source -transfer and Robot Install steps then builds the sender from scratch. This resets -the example's source, build products and any environments inside the directory; -it does not uninstall JetPack, camera drivers, system packages or PWM setup. - -For a fuller check, use fresh terminals, enumerate cameras with `--list-modes`, -validate the shared profile with `--check-config`, run the native tests, then -start sender, receiver and viewer. Check both eyes, stop/restart the receiver to -exercise reconnect, and switch profiles after stopping all three processes. -A reboot followed by the documented vendor driver/PWM setup checks cold-start -behavior. A clean OS installation is a separate test of system prerequisites. - -### Install the shared host receiver and viewer - -Do this once, for any supported GMSL mode. The host needs Linux, an NVIDIA NVDEC-capable -GPU, CUDA development tools, CMake, a C++ compiler and `uv`. Argus and the camera vendor drivers are needed only on the robot. The native build downloads a pinned NVIDIA Video -Codec SDK archive from the source-configured NVIDIA artifact server. - -```bash -cd ~/IsaacCapture/examples/camera_streamer -uv venv .venv --python 3.12 -uv pip install --python .venv/bin/python -e '.[receiver,test]' -cmake -S camera_receiver/codec -B camera_receiver/codec/build \ - -DCMAKE_BUILD_TYPE=Release -DPython3_EXECUTABLE="$PWD/.venv/bin/python" -cmake --build camera_receiver/codec/build --parallel 2 -``` - -Create the environment only once. Reinstall dependencies/rebuild after relevant -source updates. Keep host and robot environments separate even when both are -named `.venv`. - -### How to select and switch modes - -All profiles live directly in `configs/`, with model-prefixed filenames. -Each device/resolution has one shared YAML. Set `PROFILE` to the filename without -`.yaml`, for example: - -```bash -PROFILE=s56-stereo-2560x1984-30 # Choose your resolution +```text +AGX Orin / Thor Linux GPU host +Argus cameras → argus_sender ─TCP─→ camera_receiver ─CUDA IPC─→ cuda_ipc_consumer + HEVC or raw NV12 NVDEC / GPU rings display or metadata ``` -Then `configs/${PROFILE}.yaml` selects the file. Copy the same file to both computers and select the **same PROFILE on both computers**. -`network.port` and `streams[].id/type/output` define the common wire format. -`cameras`, `capture`, `runtime` and `alignment` control capture; `receiver`, `ipc` -and `streams[].receiver` control decoding and local IPC. Stream order determines -the AVS1 index. The receiver derives dimensions from `output` without a second copy. -Set `PROFILE` again in each new terminal; shell variables are not shared. -No Python copy/modify script is needed for the supplied profiles. - -Before switching mode, stop the viewer, receiver and sender with -Ctrl+C. Then choose a new profile and restart those three processes. Stop local -previews before sending: they compete for camera ownership. All profiles use -TCP port 5000 and host IPC socket `/tmp/isaac-camera-streamer.sock`, so run one -pipeline at a time. TCP is unauthenticated; use a trusted LAN or VPN. - -A sensor's maximum rate is not a guarantee of the complete capture/encode/network/display -rate. Link bandwidth, lighting, driver modes and hardware sync can limit it. - -## GMSL cameras: Sensing - -Stereo GMSL2 → Argus → Jetson hardware HEVC → AVS1/TCP. Dimensions are per eye. -The sender uses Argus device IDs and sensor modes for Sensing cameras, including -SHW5G and S56. Device models are not hard-coded in the sender. - -### S56 stereo profiles - -The supplied stereo profiles use sensor indices 0/1 on the S56 adapter. -Resolution and frame rate below apply to each eye. - -| PROFILE | Output resolution | Frame rate | Capture mode / notes | -|---|---|---|---| -| `s56-stereo-2560x1984-30` | 2560×1984 | 30 fps | Mode 1, native dimensions; highest resolution exposed by this driver | -| `s56-stereo-1080p60` | 1920×1080 | 60 fps | Mode 0, native dimensions; highest frame rate exposed by this driver | - -### S56 mono profiles - -Use one S56 sensor to exercise the mono capture, HEVC, network and viewer path. -Here, mono means one color camera stream, not grayscale. The supplied profiles -select device 0; select device 1 instead to use the other eye. - -| PROFILE | Output resolution | Frame rate | Capture mode / notes | -|---|---|---|---| -| `s56-mono-2560x1984-30` | 2560×1984 | 30 fps | Mode 1, native dimensions | -| `s56-mono-1080p30` | 1920×1080 | 30 fps | Mode 0, native dimensions | -| `s56-mono-1080p60` | 1920×1080 | 60 fps | Mode 0, native dimensions | -| `s56-mono-720p30` | 1280×720 | 30 fps | Mode 0, scaled from 1920×1080 | -| `s56-mono-720p60` | 1280×720 | 60 fps | Mode 0, scaled from 1920×1080 | -| `s56-mono-2400x1440-30` | 2400×1440 | 30 fps | Mode 1, center-cropped/scaled from 2560×1984 | +This transport is designed for a local LAN and is **not suitable for the public +internet**. AVS1/TCP has no authentication, encryption, NAT traversal, or adaptive +congestion control. Keep the sender port on a trusted camera network. CUDA IPC +works between processes on the receiving host and requires compatible access to +the same physical GPU; its handles cannot be sent to another machine. -Set `PROFILE` to the same mono profile in the Robot Send and Host Receive commands -below. The viewer automatically displays the single `head` stream. No runtime -code or separate receiver configuration is needed. All mono profiles use 30 Mbps. +## Layout -### SHW5G mono profiles +| Folder | Purpose | +| --- | --- | +| [argus_sender](argus_sender/README.md) | Argus capture, stereo SOF pairing, mixed-rate streams, HEVC/NV12, optional sender remapping, TCP streaming and wire dumps | +| [camera_receiver](camera_receiver/README.md) | Reconnecting TCP receiver, parallel stream processing, NVDEC decode, presentation rotation and GPU ring publication | +| [cuda_ipc_consumer](cuda_ipc_consumer/README.md) | Stream subscription, NV12/RGBA preview, finite headless runs and latency CSV output | +| [argus_sender/remaps](argus_sender/remaps/README.md) | Bundled example wrist remaps, calibration, manifest and a standalone generator | +| [tests](tests/) | Checks spanning sender and receiver configurations; component tests live inside their own folders | -SENSING lists SHW5G as a monocular camera with 2560×1984 at 30 fps and -2400×1440 at 60 fps ([SHW5G specifications](https://www.sgwrd.com/products/3mp-global-shutter-monocular-camera-shw5g)). -The [S56 specifications](https://wiki.sensing-world.com/docs/6_1_Camera/Binocular_Camera/S56) -list 2560×1984 at 30 fps per eye. Argus modes depend on the installed driver; -vendor specifications do not define their numeric indices. +These tools run without the Isaac Capture Python package or robot control +software. They are built and launched separately from the main repository. -`configs/` contains one shared YAML for each published SHW5G mode: +## Environment -| PROFILE | Output resolution | Frame rate | Capture mode / notes | -|---|---|---|---| -| `shw5g-mono-2560x1984-30` | 2560×1984 | 30 fps | Set mode from `--list-modes`; highest published resolution | -| `shw5g-mono-2400x1440-60` | 2400×1440 | 60 fps | Set mode from `--list-modes`; highest published frame rate | +**Sender:** the board's supported JetPack/L4T image, working Argus camera driver, +Jetson Multimedia API, CUDA headers, EGL/DRM development packages, CMake and a +C++14 compiler. Install the camera vendor's matching BSP and initialize its +serializer/deserializer as required before capture. Confirm the camera works in +the vendor's Argus sample first. A sensor can have only one capture owner. -Edit the desired profile in `configs/`: set `cameras[].device` for your rig, -and replace `mode: SET_FROM_LIST_MODES` with the matching Argus mode index. -Then copy the edited YAML to both computers and use its filename without `.yaml` as `PROFILE` -(for example, `shw5g-mono-2560x1984-30`). -The placeholder intentionally prevents starting a sender with a guessed mode. +**Host:** Ubuntu 22.04 or 24.04, an NVIDIA GPU supporting HEVC NVDEC and CUDA IPC, +a compatible NVIDIA driver and CUDA 12 toolkit including `nvcc`, CMake, a C++17 +compiler, and Python 3.10–3.12 with `venv` support. Install the driver and CUDA +toolkit for your GPU using NVIDIA's installation instructions. The component +requirements retain NumPy 1.x and CuPy 13.x; use Python 3.10–3.12 for those pins. -The supplied S56 driver's 60 fps mode is 1920×1080, not 2400×1440. Its -2400×1440 mono profile uses the full-resolution sensor mode at 30 fps; it -exercises that output geometry without claiming the SHW5G 60 fps capture mode. -Smaller scaled outputs are not additional native sensor modes. Use -`--list-modes` as the source of truth for a particular camera/driver combination. +The receiver build downloads the public NVIDIA HoloHub Video Codec SDK bundle +used by the source implementation. It requires internet access during the first +build; its sources and license remain in the build directory. -Stereo profiles use 30 Mbps per eye. At 60 fps, a hardware-triggered rig must also have its -vendor PWM/sync configured for 60 Hz; changing YAML alone does not change PWM. +Use a wired LAN with enough capacity for the selected profiles. Raw NV12 streams +can consume substantial bandwidth. Sender and receiver configs must agree on +stream order, mono/stereo type, dimensions, and port. Synchronize both machines +with chrony or another common time source for cross-host wall-clock latency. +Stereo SOF TSC deltas and local processing durations use their own clock domains. -### Robot Install +## Transfer the example -Complete the common source transfer first. Install the matching JetPack -Multimedia API/CUDA development headers, CMake and a C++ compiler. Install the -Sensing adapter's vendor driver and configure its PWM/sync for the selected rate. -These are rig-specific steps; the sender does not install drivers or change PWM. +Fetch Git LFS assets before copying; the remaps must be binary RMAP files rather +than pointer text: ```bash -cd ~/isaac-camera-streamer/camera_streamer -cmake -S argus_sender -B argus_sender/build \ - -DCMAKE_BUILD_TYPE=Release -cmake --build argus_sender/build --parallel 2 -argus_sender/build/argus_sender --list-modes +# From the repository root on the machine that holds this checkout: +git lfs pull --include='examples/camera_streamer/argus_sender/remaps/*.remap' +export SENDER_SSH=user@camera-board # Replace with your SSH target. +rsync -az --exclude='build/' --exclude='.venv/' --exclude='__pycache__/' \ + examples/camera_streamer/ "$SENDER_SSH:camera_streamer/" ``` -Verify both eye devices and mode indices against the chosen shared YAML. The -build downloads pinned yaml-cpp. Build initially and after source changes; after -moving the source directory, use a fresh build directory rather than an old -CMake cache. Do not assume supplied rig calibration maps fit your camera. +Remaps are bundled inside `argus_sender/remaps/`. Run the commands below +from the example directory on each machine. The supplied four-camera example +uses Argus device IDs 0, 1, 4, and 5, sensor mode 0, a full-resolution HEVC stereo +stream, and two remapped 640x480 raw NV12 streams at 60 FPS. Those device IDs and +mode numbers are installation-specific: inspect your board and edit the configs. -### Robot Preview - -Optional, with the sender stopped, in a terminal on the **Orin desktop**: +## Build and send on the board ```bash -argus_camera -d 0 -``` - -Exit it, then use `argus_camera -d 1` to inspect the other eye. This is NVIDIA's -separate Argus sample app; install/build it from the Jetson Multimedia API Argus -samples if absent. It shows one sensor locally and does not use the sender YAML -or transmit to the host. Its GUI controls select preview settings independently. -Exit the preview before sending. No host process is needed for this check. +cd ~/camera_streamer +./argus_sender/build.sh -### Robot Send - -```bash -cd ~/isaac-camera-streamer/camera_streamer -PROFILE=s56-stereo-2560x1984-30 # Choose your resolution -argus_sender/build/argus_sender \ - --config "configs/${PROFILE}.yaml" --check-config -argus_sender/build/argus_sender \ - --config "configs/${PROFILE}.yaml" +# The build wrapper selects single-process Argus. Release any other camera owner. +sudo systemctl stop nvargus-daemon.service +./argus_sender/build/argus_sender --list-modes +./argus_sender/build/argus_sender --check-config \ + --config argus_sender/configs/cameras.yaml +./argus_sender/run.sh ``` -The sender opens no preview and waits for the receiver before capturing. -`--check-config` validates configuration; it is not an end-to-end hardware test. +`build.sh` installs missing Ubuntu build dependencies using sudo and accepts extra +CMake options. `run.sh` checks daemon ownership and starts the default config; +pass `--config /path/to/config.yaml` to select another config. Direct CMake builds +also support multiprocess Argus; in that mode use the binary directly and keep +`nvargus-daemon` running. See the sender README for capture and codec options. -### Host Receive +The example maps are approximate viewing calibration. Replace them for your +camera unit before metric use, and preserve the declared source and output +geometry. Full-resolution head frames are not undistorted by this receiver. -After the shared host installation, run in the first host terminal: +## Receive on the Linux host ```bash -cd ~/IsaacCapture/examples/camera_streamer -PROFILE=s56-stereo-2560x1984-30 # Choose your resolution -.venv/bin/python camera_receiver/camera_receiver.py \ - --config "configs/${PROFILE}.yaml" \ - --host "${ROBOT_IP:?Set ROBOT_IP first}" +# Install basic build/Python support; install the NVIDIA driver and CUDA 12 first. +sudo apt-get update +sudo apt-get install -y build-essential cmake python3-dev python3-venv +cd /path/to/IsaacTeleop/examples/camera_streamer +./camera_receiver/setup.sh +export SENDER_IP=camera-board # Replace with the board's LAN IP or hostname. +./camera_receiver/run.sh --host "$SENDER_IP" --port 5000 ``` -This decodes the configured mono or stereo stream using NVDEC and publishes GPU frames through local CUDA -IPC. It does not open a display window. +The default receiver endpoint is loopback so the example contains no deployment +address. Use `--host`, `CAMERA_RECEIVER_HOST`, or edit +`camera_receiver/configs/receiver.yaml`. The receiver creates +`/tmp/camera_receiver_avs1_cuda_ipc.sock`; leave it running for consumers. -### Host Viewer +## Preview or consume on the same host -In a second host terminal: +In another terminal on the receiving host: ```bash -cd ~/IsaacCapture/examples/camera_streamer -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock -``` - -Expect `head.left` and `head.right` for stereo, or `head` for mono. This command is not a -CloudXR launcher; headset display needs a compatible AVS1 CUDA IPC source in -camera_viz. -Close the window, press q/Esc with the window focused, or Ctrl+C in its terminal. - -#### Display size and stream selection - -`--scale` controls each image's display dimensions. The default is `1.0`, which -preserves the decoded resolution before window display. A 2560×1984 stream -produces a 2560×1984 image at `1.0`, or 1280×992 at `0.5`. This affects only the -local preview, not capture, encoding, transmission or the decoded CUDA IPC frames. -The desktop may fit the window to your screen; full-resolution stereo can exceed -its available space. Restart only the viewer to change these options. - -`--streams` selects a comma-separated list of IPC stream names. Omit it to display -all streams. For the supplied profiles, stereo names are `head.left` and -`head.right`; mono uses `head`. +cd /path/to/IsaacTeleop/examples/camera_streamer +./cuda_ipc_consumer/setup.sh +./cuda_ipc_consumer/run.sh --streams head.left,head.right -```bash -# Show both eyes at half size to fit a smaller screen. -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock \ - --scale 0.5 --streams head.left,head.right - -# Inspect one eye at full resolution (1.0 is also the default). -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock \ - --scale 1.0 --streams head.left - -# Display the single stream from a mono profile. -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock --streams head -``` - -## Tuning and diagnostics - -To add a camera mode, copy one shared YAML under a new device/resolution profile name. -Change `streams[].output` and choose the matching `cameras[].mode` from -`--list-modes`. Dimensions are shared; adjust the receiver FPS warning threshold and -configure hardware PWM for the capture rate. Save changes on the host and -transfer profiles before restarting the sender, receiver and viewer. - -`bitrate_mbps` is Mbps per eye; `gop` is in frames. Sensor timestamps and timing -statistics are not by themselves a photon-to-photon latency measurement. - -On a CUDA 13-only host, the optional CuPy pixel-mean diagnostic needs NVRTC 12: - -```bash -cd ~/IsaacCapture/examples/camera_streamer -uv pip install --python .venv/bin/python nvidia-cuda-nvrtc-cu12 -export LD_LIBRARY_PATH="$PWD/.venv/lib/python3.12/site-packages/nvidia/cuda_nvrtc/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock --no-display --frames 120 --sample-mean +# Finite headless check, or dump latency metadata: +./cuda_ipc_consumer/run.sh --streams wrist1 --no-display --frames 120 +./cuda_ipc_consumer/.venv/bin/python cuda_ipc_consumer/latency_check.py \ + --streams head.left --frames 120 --output latency.csv ``` -First-use kernel compilation can pause this diagnostic. Omit `--sample-mean` -when measuring steady frame delivery. +OpenCV preview requires a graphical desktop. Use `--no-display` for headless +operation. Consult the consumer README for CSV columns and GPU pixel checks. -To record frame timing without opening a window, use the same consumer: - -```bash -.venv/bin/python cuda_ipc_consumer/consumer.py \ - --socket /tmp/isaac-camera-streamer.sock --no-display \ - --frames 120 --latency-csv /tmp/camera_latency.csv -``` +For optional Orin/Thor service deployment, persistent journals, clock pinning, +time synchronization, and rollback, see [deployment](argus_sender/deploy/README.md). +The target must already have a working Argus camera driver and camera initialization. ## Validation -Portable protocol/configuration tests can run on a workstation without Jetson libraries: - -```bash -cd ~/IsaacCapture/examples/camera_streamer -cmake -S argus_sender -B /tmp/camera-protocol-build -DARGUS_SENDER_CPU_TESTS_ONLY=ON -cmake --build /tmp/camera-protocol-build --parallel 2 -ctest --test-dir /tmp/camera-protocol-build --output-on-failure -``` - -```bash -cd ~/IsaacCapture/examples/camera_streamer -uv pip install --python .venv/bin/python -e ".[test]" -.venv/bin/python -m pytest tests -``` - -On the robot, run the native sender tests where the sender was built. The default -suite checks configuration, scheduling, alignment and AVS1/TCP behavior without -opening a camera: +See [sender testing](argus_sender/TESTING.md) for Jetson and hardware +commands. Component tests stay under `argus_sender/tests/` and +`camera_receiver/tests/`; only checks spanning components live under `tests/`. +From the example root, run the Python checks with pytest and PyYAML installed: ```bash -cd ~/isaac-camera-streamer/camera_streamer -ctest --test-dir argus_sender/build --output-on-failure +python3 -m pytest argus_sender/tests camera_receiver/tests tests -q ``` -For an opt-in stereo hardware test, stop the sender and local preview first. -Check the devices, sensor modes and PWM rate against the smoke-test YAML before -running it; pass a different YAML path if your camera setup differs. Install -PyYAML for the interpreter selected by CMake (`Python3_EXECUTABLE`) before this test: +These tests cover configuration and protocol behavior; live capture, +NVENC/NVDEC, and CUDA IPC still require the target NVIDIA hardware and driver. -```bash -cd ~/isaac-camera-streamer/camera_streamer/argus_sender -cmake -S . -B build \ - -DARGUS_SENDER_HARDWARE_TESTS=ON \ - -DARGUS_SENDER_HARDWARE_CONFIG="$PWD/configs/hardware_stereo_smoke.yaml" -cmake --build build --parallel 2 -ctest --test-dir build -R '^avs1_hardware_smoke$' --output-on-failure -V -``` +## Licensing -This receives 120 stereo bundles and checks sequence continuity, nonempty HEVC -payloads, increasing sensor timestamps when available, initial HEVC parameter -sets/keyframes, and clean sender exit. The receiver reads `network.port` from -the same YAML as the sender (default 5000); change the YAML to select another port. -Configuration checks alone do not validate a camera mode on hardware. - -## Source layout and provenance - -| Directory | Purpose | -|---|---| -| `argus_sender` | Native Argus capture, hardware HEVC and AVS1/TCP | -| `camera_receiver` | AVS1 parsing, NVDEC extension, GPU rings and CUDA IPC | -| `cuda_ipc_consumer` | Standalone viewer and timing diagnostics | -| `tests` | C++ unit tests, Python receiver/config tests and opt-in hardware tests | -| `configs` | Shared device/resolution profiles for sender and receiver | - -The GMSL/Argus pipeline was adapted from NVIDIA camera sources at commit -`1fdb06d91ddb02ad527563cbacfc894a76d09126`. The original extraction and hardware-validation history are retained in -[PR #1358](https://github.com/NVIDIA/IsaacCapture/pull/1358). - -This example is Apache-2.0 licensed. Existing upstream notices and external SDK -licenses remain in effect. The Argus-derived `EGLGlobal.h`, `EGLGlobal.cpp` and -`Error.h` retain their BSD-3-Clause license and notices. This example is not included in Isaac Capture wheels. - -## Rebuilding after a source-directory move - -CMake caches absolute source paths. After updating an existing deployment, configure -and build the native sender and decoder again at the paths shown above. Do not move -or reuse an old build directory from a different source location. - -CUDA IPC protocol 3 requires consumers to hold each slot's shared file lock and -verify `slot_generation` before reading pixels, then finish GPU reads before -unlocking. The receiver holds an exclusive lock while writing. Delayed notifications -are discarded; a slow active read backpressures slot reuse rather than corrupting -pixels. Update receiver and consumer together; protocol 2 consumers are rejected. +Project code uses Apache-2.0. The imported Argus sample helpers `Error.h` and +`EGLGlobal.h` retain their BSD-3-Clause notices. Jetson Multimedia API and Video +Codec SDK dependencies retain their own licenses and are supplied separately. diff --git a/examples/camera_streamer/argus_sender/.gitignore b/examples/camera_streamer/argus_sender/.gitignore index 8cf2ed8f5f..6097314d0b 100644 --- a/examples/camera_streamer/argus_sender/.gitignore +++ b/examples/camera_streamer/argus_sender/.gitignore @@ -1,6 +1,3 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - # CMake build trees and generated files /build/ /build-*/ @@ -41,7 +38,3 @@ core.* *.swo *~ .DS_Store - -# Deployed wrist remap LUTs — generated by setup_video_streaming.sh from the -# calibration tree, rsynced to the AGX with the source tree. -/remaps/ diff --git a/examples/camera_streamer/argus_sender/CMakeLists.txt b/examples/camera_streamer/argus_sender/CMakeLists.txt index 541a2f9c53..58b60a4685 100644 --- a/examples/camera_streamer/argus_sender/CMakeLists.txt +++ b/examples/camera_streamer/argus_sender/CMakeLists.txt @@ -10,15 +10,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) include(CTest) -option(ARGUS_SENDER_CPU_TESTS_ONLY "Build only portable protocol/configuration tests" OFF) -if(ARGUS_SENDER_CPU_TESTS_ONLY) - find_package(Threads REQUIRED) - include(cmake/YamlCpp.cmake) - set(ARGUS_SENDER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") - include(cmake/Tests.cmake) - return() -endif() - option(DISABLE_MULTIPROCESS "Link the single-process Argus implementation instead of nvargus-daemon's socket client" OFF) @@ -61,7 +52,19 @@ find_library(CUDA_DRIVER_LIBRARY NAMES cuda find_package(Threads REQUIRED) -include(cmake/YamlCpp.cmake) +include(FetchContent) +set(YAML_CPP_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(YAML_CPP_BUILD_TOOLS OFF CACHE BOOL "" FORCE) +set(YAML_CPP_BUILD_CONTRIB OFF CACHE BOOL "" FORCE) +set(YAML_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) +FetchContent_Declare( + yaml_cpp + URL + https://github.com/jbeder/yaml-cpp/releases/download/yaml-cpp-0.9.0/yaml-cpp-yaml-cpp-0.9.0.tar.gz + URL_HASH + SHA256=298593d9c440fd9034b8b193d96318b76d49bc97c6ceadb7b0836edf0b6d7539 +) +FetchContent_MakeAvailable(yaml_cpp) set(REQUIRED_PATHS ARGUS_INCLUDE_DIR @@ -120,13 +123,65 @@ foreach(source_file IN LISTS MMAPI_CLASS_SOURCES) endif() endforeach() -set(ARGUS_SENDER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") -add_subdirectory(src) +add_executable(argus_sender + src/main.cpp + src/Config.cpp + src/Avs1Wire.cpp + src/OutputGeometry.cpp + src/MixedRateScheduler.cpp + src/CapturePriorityPolicy.cpp + src/CaptureDomainPlan.cpp + src/CaptureManager.cpp + src/FrameBundleAssembler.cpp + src/StreamProcessor.cpp + src/CaptureCoordinator.cpp + src/SenderSession.cpp + src/Runtime.cpp + src/CameraCatalog.cpp + src/FrameCopyWorker.cpp + src/NvRemap.cpp + src/FrameAlignmentPolicy.cpp + src/StereoAlignmentPolicy.cpp + src/TcpTransport.cpp + src/HevcEncoder.cpp + src/StereoTiming.cpp + src/EGLGlobal.cpp + src/NvBufSurfaceLite.cpp + ${MMAPI_CLASS_SOURCES} +) + +if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(argus_sender PRIVATE -Wall -Wextra) + set_source_files_properties(${MMAPI_CLASS_SOURCES} + PROPERTIES COMPILE_OPTIONS -w) +endif() + +target_compile_definitions(argus_sender PRIVATE NVMMAPI_SUPPORTED) + +target_include_directories(argus_sender PRIVATE + src + ${ARGUS_INCLUDE_DIR} + ${MMAPI_INCLUDE_DIR} + ${MMAPI_CLASS_DIR} + ${CUDA_INCLUDE_DIR} + ${DRM_INCLUDE_DIR} +) + +target_link_libraries(argus_sender PRIVATE + ${ARGUS_LINK_LIBRARY} + ${EGL_LIBRARY} + ${NVV4L2_LIBRARY} + ${NVBUFSURFACE_LIBRARY} + ${CUDA_DRIVER_LIBRARY} + yaml-cpp::yaml-cpp + Threads::Threads +) if(BUILD_TESTING) include(cmake/Tests.cmake) endif() +install(TARGETS argus_sender DESTINATION bin) message(STATUS "Argus include: ${ARGUS_INCLUDE_DIR}") message(STATUS "Argus library (${ARGUS_LINK_MODE}): ${ARGUS_LINK_LIBRARY}") diff --git a/examples/camera_streamer/argus_sender/README.md b/examples/camera_streamer/argus_sender/README.md new file mode 100644 index 0000000000..dea875962e --- /dev/null +++ b/examples/camera_streamer/argus_sender/README.md @@ -0,0 +1,328 @@ + + +# argus_sender + +Standalone, headless NVIDIA Argus camera sender for Jetson Thor and Orin. One +YAML file can describe any combination of mono and stereo logical streams. The +sender captures them through Argus, optionally resizes and rotates each stream, +produces HEVC or tight NV12, and sends every stream over one TCP connection in +the AVS1 format. + +The project builds only `argus_sender`; it does not configure NVIDIA's full +`argus_camera` sample and does not require the legacy `libnvmm_jpeg.so` used by +JPEG sample modules. CMake downloads the pinned yaml-cpp 0.9.0 release archive, +verifies its SHA-256 digest, and builds it statically. + +## Build + +```bash +cd /path/to/argus_sender +./build.sh +``` + +`build.sh` selects single-process Argus and is intended for AGX Orin or Thor. It installs any missing Ubuntu, +Jetson Multimedia API, CUDA, DRM, and EGL build dependencies, then configures a +Release build and builds `build/argus_sender`. Already-installed packages are +left untouched. + +The first configure requires network access to GitHub. CMake caches yaml-cpp +under `build/_deps`; later builds reuse it until the build directory is removed. +For an offline build, provide a local source checkout: + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ + -DFETCHCONTENT_SOURCE_DIR_YAML_CPP=/path/to/yaml-cpp +``` + +If the Jetson Multimedia API is installed somewhere else: + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ + -DMMAPI_ROOT=/usr/src/jetson_multimedia_api +``` + +List the Argus device and mode indices visible on the current board: + +```bash +build/argus_sender --list-modes +``` + +Validate a production config without binding TCP or opening any cameras: + +```bash +build/argus_sender --check-config --config configs/cameras.yaml +``` + +## Run + +```bash +./run.sh +``` + +`run.sh` uses `configs/cameras.yaml` and may be launched from any working +directory. Additional command-line arguments are passed to `argus_sender`. + +By default, runtime logging is concise: it reports aggregate FPS and global TSC +span, stereo-eye TSC delta, and per-stream capture-to-TCP-sent latency. Enable +the detailed capture, FIFO-selection, stereo-repair, encoder, TCP-stage, and +per-stream diagnostics when troubleshooting. The verbose capture summary also +reports the latest selected frame's sensor exposure, sensor analog gain, ISP +digital gain, AWB mode, AWB color temperature, and per-Bayer-channel AWB gains +for each camera: + +```bash +./run.sh --verbose +``` + +All periodic summaries use `runtime.log_interval_s` (default: 1 second). Set it +to `0` to disable periodic summaries without suppressing warnings, errors, or +one-time lifecycle messages. + +The sender is the TCP server. It waits for one receiver, creates fresh Argus and +encoder pipelines after the connection is accepted, and tears the entire +pipeline down after a disconnect. It then listens again. This makes each new +receiver start with fresh HEVC parameter sets and an IDR frame while keeping the +steady-state path queue-free except for the TCP sender's depth-one handoff. + +## Configuration + +```yaml +version: 1 + +network: + bind: 0.0.0.0 + port: 5000 + reconnect_ms: 1000 + dump_wire: "" # optional exact TCP-record dump + +runtime: + frames: 0 # 0 = run until stopped/disconnected + kpi: true + log_interval_s: 1 # 0 disables periodic summaries + bench_encode: false + +alignment: + warmup_frames: 60 + interval_frames: 60 # 0 disables periodic checks + +streams: + - id: front_stereo + type: stereo # mono | stereo + cameras: # one camera for mono, left then right for stereo + - {device: 0, mode: 0} + - {device: 1, mode: 0} + output: + fps: 60 # 0 = native; see Mixed FPS + codec: hevc # hevc | raw_nv12 + width: 1280 # optional + height: 992 # optional + resize_mode: strict # strict | center_crop | stretch + rotation: 180 # 0 | 180 + bitrate_mbps: 25 # per camera, HEVC only + gop: 60 # 0 defaults to output FPS +``` + +Stream descriptors and samples always follow `streams` and `cameras` order. +Stream IDs are local configuration names; the wire uses their stable zero-based +indices. A physical Argus device may appear only once in a config. + +`capture.mode` selects the sender's secondary-stream consumption policy: +`mailbox` (the default) is demand-driven, while `fifo` continuously acquires +into a two-frame application candidate window. For raw NV12 streams, it also +configures the Argus EGL stream as mailbox or FIFO; set +`capture.fifo_length: 3` or greater for EGL FIFO. For HEVC, Argus captures +directly into client-owned DMABUFs, so no EGL mailbox/FIFO mode is set; +`capture.mode` still selects the secondary consumption policy. The configured +mode and direct-buffer/EGL stream type appear separately in the startup log. +`capture.fifo_length` is not applied to a direct-buffer stream. A priority +stream must use the default mailbox policy regardless of codec; FIFO priority +capture is rejected. + +### Size and aspect ratio + +- Omit `width` and `height`: use the selected sensor mode's native dimensions. +- Specify one dimension: derive the other as an even number while preserving + the source aspect ratio. +- Specify both with `strict`: reject any aspect-ratio change greater than 0.1%. +- Specify both with `center_crop`: crop equally around the source center, then + resize to the requested dimensions. +- Specify both with `stretch`: resize the full source to the requested dimensions. + +All resolved NV12 dimensions must be nonzero and even. For HEVC, Argus writes +the resolved ISP output directly into client-owned block-linear DMABUFs that are +queued unchanged to NVENC. Raw/remapped streams still use the sender's GPU/VIC +processing path. `rotation` supports 0 and 180 degrees as receiver-side +presentation metadata; the sender does not rotate pixels. + +### Mixed FPS + +The highest configured stream FPS is the AVS1 bundle cadence. A lower-rate +stream is marked absent in bundles where it is not due; its descriptor remains +present, but its `PRESENT` bit is clear and its samples have zero payload length. +Frames are never duplicated. For a 60 FPS base cadence, a 30 FPS stream appears +in 30 bundles per second and a 45 FPS stream appears in 45 bundles per second. + +The sender leaves Argus capture at the camera/trigger rate and selects output +frames with a deterministic fractional scheduler. The priority stereo domain +is drained continuously by independent eye workers; HEVC uses direct Argus +buffers rather than an EGL stream. A mailbox-policy secondary is consumed +once per base tick through a bounded one-permit gate. A FIFO-policy secondary is +consumed continuously and retains two acquired frame handles or DMABUFs; each +base tick +selects the candidate whose SOF TSC is closest to the synchronized priority +reference. Only that selected frame is retained for processing. HEVC retains +its client-owned capture DMABUF through NVENC; raw streams copy the selection +into their processing DMABUF. Frames for a lower-rate stream are discarded on +absent ticks. + +Use `fps: 0` to send every native capture. Native and explicit rates cannot be +mixed in one config. When every stream uses `fps: 0`, their resolved native rates +must match. Explicit rates may be mixed, including rates such as 60, 45, and 30. + +### Codec behavior + +`hevc` uses one `NvVideoEncoder` instance per camera. Stereo eyes therefore +encode independently and in parallel. Each encoder consumes the same +client-owned block-linear DMABUF that Argus filled; there is no intermediate +raw-frame copy. Payloads are Annex-B HEVC access units. The sender requests AUD +insertion and repeated SPS/PPS at IDR frames when the platform accepts those +controls. + +`raw_nv12` sends tightly packed NV12: `width * height` Y bytes followed by +`width * height / 2` interleaved UV bytes. Surface pitch is removed before the +wire payload is built. Full-resolution raw streams can exceed 1 GbE; the sender +prints a bandwidth warning when the configured raw payload estimate exceeds +0.9 Gbps. + +## Capture and latency model + +Capture is organized into Argus domains. The priority head stereo stream owns +two independent single-device sessions and continuously acquires each eye in +its own worker. A one-frame slot per eye retains the newest acquired frame; +SOF-based pairing replaces only the older eye while retaining the newer eye. +An event worker still monitors Argus errors. A secondary stereo stream owns one +two-device session; each mono stream owns a single-device session. Mailbox-policy +secondary domains are capture-complete-driven with one pending frame-set slot; +FIFO-policy secondaries continuously fill a two-candidate frame-set window. + +The coordinator chooses a maximum-rate stereo HEVC stream as the priority when +one is available; otherwise it uses the first maximum-rate stream. At startup, +only that domain runs during its warm-up and SOF-TSC synchronization check. +Secondary domains then start with their configured consumption policy and warm +up while the priority domain continues to drain. The priority domain is checked +again after secondary startup. A stereo priority domain is considered associated +after three consecutive frame sets have a TSC span no greater than half its +sensor period. Failure to associate within 120 frame sets is fatal. At runtime, +the older eye is selectively reacquired, up to four times, when a pair is more +than half a frame period apart. The retained newer frame is not processed until +the final pair is selected; direct-buffer HEVC frames are not copied into NVENC. +Runtime bundles apply the same half-period span check before computing the +priority midpoint used for FIFO selection. + +On normal bundles, the coordinator consumes the priority domain first. Its HEVC +DMABUFs are already ready for NVENC because Argus captured directly into them. +Secondary-domain permits are released together after 42 percent of the priority +sensor period (7 ms at 60 Hz), and the coordinator then consumes the selected +secondary frames. After all due frames have been acquired, every HEVC stream is +queued into NVENC before raw transforms begin. Raw scaling and packing overlap +asynchronous HEVC encoding. This prioritizes head latency without adding a +frame queue or a sender-side HEVC pixel copy. + +The startup synchronization check is intentionally scoped to the priority +domain. Secondary FIFO streams may select an adjacent trigger epoch without +disturbing the stereo head pair. While any continuous FIFO domain is active, +per-bundle closest-candidate selection replaces periodic global reacquisition and +`alignment.interval_frames` is reported as inactive. With mailbox-only capture, +a positive interval retains the periodic global SOF-TSC check and reacquires a +domain more than half its sensor frame interval behind the newest frame, up to +four drops. Reacquiring a stereo domain replaces both eyes together. +Periodic diagnostics report per-stream capture FPS, right-minus-left TSC delta, +per-stream processing FPS/bitrate, aggregate AVS1/TCP rates, and the average/max +`global_tsc_span_us` from the earliest to latest camera SOF in each bundle. +`argus_sender[capture:]` counts delivered frames, including HEVC +frames whose DMABUF ownership has already moved to the encoder path. Its +right-minus-left TSC delta appears only when both SOF timestamps are valid. +`argus_sender[sof-acquire:]` prints each camera's sensor-SOF-to- +acquire average/max and selected-frame gaps once per `runtime.log_interval_s` +(one second by default), even with `runtime.kpi: false`; set the interval to +zero to disable it. The SOF latency is `n/a` when comparable TSC metadata is +unavailable. With KPI enabled, `[head-handoff:]` also reports +priority slot overwrites and alignment drops once per interval. A priority +camera exits after 10 seconds without its first new frame or 1 second without +a subsequent new frame, including repeated acquisitions of the same frame +number; the service can then restart it. + +With KPI enabled, `argus_sender[sof-to-sent]` reports each present camera's +`sensor_sof_to_tcp_sent_ms(avg/max)` for frames with valid SOF TSC. It starts +at sensor start-of-frame, not photon arrival, and ends after the complete AVS1 +record has been written to the local TCP socket. It excludes network transit, +receiver processing, and display. + +The TCP KPI line reports `acquire_spread_ms(avg/max)`, +`capture_to_encode_start_ms(avg/max)`, +`capture_to_encode_done_ms(avg/max)`, and `capture_to_tcp_sent_ms(avg/max)` +once per interval. The three capture-to-* measurements start when the earliest +camera worker's `acquireFrame()` or `acquireBuffer()` returns for a final frame +included in the bundle. `acquire_spread_ms` is the latest-minus-earliest +acquisition completion among all present cameras in that bundle. This is capture +acquisition start, not physical sensor SOF. Frames rejected during stereo +alignment are excluded. Encode start is the first HEVC encoder submission among +the bundle samples; for raw NV12 it is the start of payload packing. Encode +done is the latest encoder callback among the bundle samples; for raw NV12 it +is payload packing completion. +TCP sent ends when `sendAll()` has handed the complete record to the local TCP +stack. It does not include network transit, receiver decode, or display latency. + +The TCP interval is followed by one `argus_sender[latency:]` line +for every stream present during that interval. Each line uses that stream's +earliest `acquireFrame()` completion as its capture anchor. HEVC streams report +acquisition offset from the bundle's earliest stream, within-stream acquisition +spread, copy completion, encoder submission, encoded-payload readiness, the +wait from encoded readiness through complete-bundle TCP transmission, and total +stream capture-to-send latency. For HEVC, `copy_done` denotes direct-capture +buffer readiness and should be approximately zero from the acquisition anchor; +raw streams report the corresponding copy, raw-payload readiness, ready-to-send +wait, and total latency. Copy completion is timestamped by the copy worker for +raw streams immediately after its transform returns. It is not the later time +at which the coordinator waits for that worker. These metrics are diagnostic +metadata only and do not alter the AVS1 wire format. + +## Wire dump + +Set `network.dump_wire` to save the exact length-prefixed AVS1 records that were +successfully sent over TCP: + +```yaml +network: + dump_wire: /tmp/cameras.avs1 +``` + +The first connection truncates the file; reconnects in the same sender process +append complete records from new sessions. Dump I/O occurs in the TCP sender +thread and can apply backpressure if storage cannot sustain the stream bitrate. +See [WIRE_FORMAT.md](WIRE_FORMAT.md) for the byte-level format. + +## Source layout + +- `Config.*`: YAML parsing and validation. +- `CaptureManager.*` / `CaptureDomainPlan.*` / `PriorityFrameHandoff.h`: independent priority-eye acquisition and latest-frame slots, event-driven secondary Argus domains, configurable EGL mailbox/FIFO consumers, and timing. +- `CapturePriorityPolicy.*`: priority-stream selection, startup synchronization tolerance, and secondary release timing. +- `CaptureCoordinator.*`: mixed-rate scheduling, priority-first capture, periodic global alignment, and pipelined cross-domain orchestration. +- `StreamProcessor.*`: direct Argus-DMABUF HEVC submission plus raw processing. +- `FrameBundleAssembler.*` / `Avs1Wire.*`: ordered asynchronous bundle assembly. +- `TcpTransport.*` / `SenderSession.*`: depth-one TCP sending and reconnects. +- `OutputGeometry.*` / `MixedRateScheduler.*`: pure geometry/rate policy. +- `tests/`: host unit tests and the opt-in 120-frame camera smoke test. + +The matching receiver is [camera_receiver](../camera_receiver/README.md). +See the [example setup](../README.md) for sender, receiver and consumer commands. + +## Remap assets + +The default config uses bundled [example remaps](remaps/README.md). Paths resolve +relative to the YAML file. The maps preserve the 1280x992 capture, 1280x960 lookup +grid and 2x box downsampling to 640x480 wrist output. Replace the sample calibration +for your own camera unit. diff --git a/examples/camera_streamer/argus_sender/TESTING.md b/examples/camera_streamer/argus_sender/TESTING.md new file mode 100644 index 0000000000..3a8e0a20c9 --- /dev/null +++ b/examples/camera_streamer/argus_sender/TESTING.md @@ -0,0 +1,61 @@ + + +# Testing argus_sender + +## Unit and loopback tests + +Run these commands on a Jetson with the build dependencies installed. The tests +use the Jetson SDK even when they do not open a camera. + +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build -j$(nproc) +ctest --test-dir build --output-on-failure +``` + +The default suite does not open a camera. It covers YAML capture-mode parsing and validation, +output geometry, mixed-rate scheduling, capture-priority selection and release +timing, stereo alignment policy, AVS1 serialization/parsing, asynchronous bundle +assembly and ordering, plus TCP send/dump/disconnect and bounded stalled-receiver +drain behavior. + +The matching receiver parser tests run from `camera_receiver/`: + +```bash +python3 -m pytest tests -q +``` + +The deployment snapshot/restore checks run from `argus_sender/`: + +```bash +python3 -m pytest tests/test_deployment.py -q +``` + +## 120-frame hardware smoke test + +The opt-in test opens the devices in +`configs/hardware_stereo_smoke.yaml`, creates two HEVC encoders, and runs a local +AVS1 receiver: + +```bash +cmake -S . -B build \ + -DARGUS_SENDER_HARDWARE_TESTS=ON \ + -DARGUS_SENDER_HARDWARE_CONFIG="$PWD/configs/hardware_stereo_smoke.yaml" +cmake --build build -j$(nproc) +ctest --test-dir build -R '^avs1_hardware_smoke$' --output-on-failure -V +``` + +The test receives exactly 120 bundles and verifies: + +- contiguous AVS1 bundle sequence numbers; +- a present stereo HEVC stream with nonempty left/right payloads; +- monotonically increasing per-eye SOF TSC timestamps when available; +- VPS, SPS, PPS, and an IRAP NAL in each first access unit; +- clean sender exit with no reconnecting process left behind. + +Override the loopback port with `-DARGUS_SENDER_TEST_PORT=PORT`. To use other +devices, modes, dimensions, or rates, copy the hardware YAML and pass its path +through `ARGUS_SENDER_HARDWARE_CONFIG`. diff --git a/examples/camera_streamer/argus_sender/WIRE_FORMAT.md b/examples/camera_streamer/argus_sender/WIRE_FORMAT.md index 3095b25812..f0cdefd57d 100644 --- a/examples/camera_streamer/argus_sender/WIRE_FORMAT.md +++ b/examples/camera_streamer/argus_sender/WIRE_FORMAT.md @@ -189,8 +189,8 @@ overlap, gaps, unknown flags, unsupported versions, or inconsistent presence. ## Minimal Python framing -The standalone parser is -`camera_receiver/avs_wire.py` relative to the example root. This fragment shows the outer framing +The IsaacCapture implementation is +`examples/camera_viz/sources/avs_tcp.py`. This fragment shows the outer framing and fixed descriptor structs: ```python @@ -213,10 +213,8 @@ def recv_exact(sock, size): return bytes(data) -def recv_record(sock, max_record_bytes=256 * 1024 * 1024): +def recv_record(sock): (record_length,) = struct.unpack("!I", recv_exact(sock, 4)) - if not 0 < record_length <= max_record_bytes: - raise ValueError(f"invalid AVS1 record length: {record_length}") record = recv_exact(sock, record_length) header = HEADER.unpack_from(record) if header[0] != b"AVS1" or header[1] != 1 or header[3] != HEADER.size: diff --git a/examples/camera_streamer/argus_sender/build.sh b/examples/camera_streamer/argus_sender/build.sh new file mode 100755 index 0000000000..beeffb0b57 --- /dev/null +++ b/examples/camera_streamer/argus_sender/build.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +build_dir="${project_dir}/build" + +if [[ "$(uname -m)" != "aarch64" || ! -f /etc/nv_tegra_release ]]; then + echo "error: build.sh must run on an NVIDIA Jetson (AGX Orin or Thor)." >&2 + exit 1 +fi + +if ! command -v apt-get >/dev/null 2>&1 || ! command -v dpkg-query >/dev/null 2>&1; then + echo "error: this script requires the Ubuntu apt package manager." >&2 + exit 1 +fi + +packages=( + build-essential + ca-certificates + cmake + libdrm-dev + libegl-dev + nvidia-l4t-jetson-multimedia-api +) + +missing_packages=() +for package in "${packages[@]}"; do + if [[ "$(dpkg-query -W -f='${Status}' "${package}" 2>/dev/null || true)" != \ + "install ok installed" ]]; then + missing_packages+=("${package}") + fi +done + +if ((${#missing_packages[@]})); then + if ((EUID == 0)); then + apt_prefix=() + elif command -v sudo >/dev/null 2>&1; then + apt_prefix=(sudo) + else + echo "error: installing dependencies requires root or sudo." >&2 + exit 1 + fi + + echo "Installing missing build dependencies: ${missing_packages[*]}" + "${apt_prefix[@]}" apt-get update + "${apt_prefix[@]}" apt-get install -y --no-install-recommends \ + "${missing_packages[@]}" +else + echo "All build dependencies are already installed." +fi + +cmake -S "${project_dir}" -B "${build_dir}" -DCMAKE_BUILD_TYPE=Release -DDISABLE_MULTIPROCESS=ON "$@" +cmake --build "${build_dir}" --target argus_sender -j "$(nproc)" + +echo "Built ${build_dir}/argus_sender" diff --git a/examples/camera_streamer/argus_sender/cmake/Tests.cmake b/examples/camera_streamer/argus_sender/cmake/Tests.cmake index 18eeda3c34..a120f16aa8 100644 --- a/examples/camera_streamer/argus_sender/cmake/Tests.cmake +++ b/examples/camera_streamer/argus_sender/cmake/Tests.cmake @@ -1,12 +1,9 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -get_filename_component(CAMERA_STREAMER_ROOT "${ARGUS_SENDER_ROOT}" DIRECTORY) -set(CAMERA_STREAMER_TEST_ROOT "${CAMERA_STREAMER_ROOT}/tests") - set(ARGUS_SENDER_TEST_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src - ${CAMERA_STREAMER_TEST_ROOT}/cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tests ${ARGUS_INCLUDE_DIR} ) @@ -28,23 +25,86 @@ function(argus_sender_add_cpp_test target test_name) ) endfunction() -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/config ${CMAKE_CURRENT_BINARY_DIR}/tests/config) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/geometry_scheduler ${CMAKE_CURRENT_BINARY_DIR}/tests/geometry_scheduler) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/priority_frame_handoff ${CMAKE_CURRENT_BINARY_DIR}/tests/priority_frame_handoff) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/frame_alignment_policy ${CMAKE_CURRENT_BINARY_DIR}/tests/frame_alignment_policy) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/capture_domain_plan ${CMAKE_CURRENT_BINARY_DIR}/tests/capture_domain_plan) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/capture_priority_policy ${CMAKE_CURRENT_BINARY_DIR}/tests/capture_priority_policy) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/avs1_wire ${CMAKE_CURRENT_BINARY_DIR}/tests/avs1_wire) +add_executable(argus_sender_test_config + tests/test_config.cpp + src/Config.cpp +) +argus_sender_add_cpp_test(argus_sender_test_config config) +target_link_libraries(argus_sender_test_config PRIVATE yaml-cpp::yaml-cpp) -if(ARGUS_SENDER_CPU_TESTS_ONLY) - return() -endif() +add_executable(argus_sender_test_geometry_scheduler + tests/test_geometry_scheduler.cpp + src/OutputGeometry.cpp + src/MixedRateScheduler.cpp +) +argus_sender_add_cpp_test(argus_sender_test_geometry_scheduler geometry_scheduler) + +add_executable(argus_sender_test_nv_remap + tests/test_nv_remap.cpp + src/NvRemap.cpp +) +argus_sender_add_cpp_test(argus_sender_test_nv_remap nv_remap) + +add_executable(argus_sender_test_rate_limiter + tests/test_rate_limiter.cpp + src/StereoTiming.cpp + src/MixedRateScheduler.cpp +) +argus_sender_add_cpp_test(argus_sender_test_rate_limiter rate_limiter) + +add_executable(argus_sender_test_alignment_policy + tests/test_alignment_policy.cpp + src/StereoAlignmentPolicy.cpp + src/StereoTiming.cpp +) +argus_sender_add_cpp_test(argus_sender_test_alignment_policy alignment_policy) + +add_executable(argus_sender_test_priority_frame_handoff + tests/test_priority_frame_handoff.cpp +) +argus_sender_add_cpp_test(argus_sender_test_priority_frame_handoff priority_frame_handoff) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/nv_remap ${CMAKE_CURRENT_BINARY_DIR}/tests/nv_remap) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/rate_limiter ${CMAKE_CURRENT_BINARY_DIR}/tests/rate_limiter) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/alignment_policy ${CMAKE_CURRENT_BINARY_DIR}/tests/alignment_policy) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/bundle_assembler ${CMAKE_CURRENT_BINARY_DIR}/tests/bundle_assembler) -add_subdirectory(${CAMERA_STREAMER_TEST_ROOT}/cpp/tcp_transport ${CMAKE_CURRENT_BINARY_DIR}/tests/tcp_transport) +add_executable(argus_sender_test_frame_alignment_policy + tests/test_frame_alignment_policy.cpp + src/FrameAlignmentPolicy.cpp +) +argus_sender_add_cpp_test(argus_sender_test_frame_alignment_policy frame_alignment_policy) + +add_executable(argus_sender_test_capture_domain_plan + tests/test_capture_domain_plan.cpp + src/CaptureDomainPlan.cpp +) +argus_sender_add_cpp_test(argus_sender_test_capture_domain_plan capture_domain_plan) + +add_executable(argus_sender_test_capture_priority_policy + tests/test_capture_priority_policy.cpp + src/CapturePriorityPolicy.cpp +) +argus_sender_add_cpp_test(argus_sender_test_capture_priority_policy capture_priority_policy) + +add_executable(argus_sender_test_avs1_wire + tests/test_avs1_wire.cpp + src/Avs1Wire.cpp +) +argus_sender_add_cpp_test(argus_sender_test_avs1_wire avs1_wire) + +add_executable(argus_sender_test_bundle_assembler + tests/test_bundle_assembler.cpp + src/FrameBundleAssembler.cpp + src/Avs1Wire.cpp + src/StereoTiming.cpp + src/Runtime.cpp +) +argus_sender_add_cpp_test(argus_sender_test_bundle_assembler bundle_assembler) + +add_executable(argus_sender_test_tcp_transport + tests/test_tcp_transport.cpp + src/TcpTransport.cpp + src/StereoTiming.cpp + src/Runtime.cpp +) +argus_sender_add_cpp_test(argus_sender_test_tcp_transport tcp_transport) +set_tests_properties(tcp_transport PROPERTIES LABELS "loopback") option(ARGUS_SENDER_HARDWARE_TESTS "Enable tests that capture from physical stereo cameras" @@ -52,16 +112,19 @@ option(ARGUS_SENDER_HARDWARE_TESTS set(ARGUS_SENDER_HARDWARE_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/configs/hardware_stereo_smoke.yaml" CACHE FILEPATH "Config used by the hardware smoke test") +set(ARGUS_SENDER_TEST_PORT "55000" CACHE STRING + "Loopback TCP port used by hardware tests") if(ARGUS_SENDER_HARDWARE_TESTS) find_package(Python3 REQUIRED COMPONENTS Interpreter) add_test( NAME avs1_hardware_smoke COMMAND ${Python3_EXECUTABLE} - ${CAMERA_STREAMER_TEST_ROOT}/hardware/avs1_smoke.py + ${CMAKE_CURRENT_SOURCE_DIR}/tests/hardware/avs1_smoke.py --sender $ --config ${ARGUS_SENDER_HARDWARE_CONFIG} --frames 120 + --port ${ARGUS_SENDER_TEST_PORT} ) set_tests_properties(avs1_hardware_smoke PROPERTIES LABELS "hardware" diff --git a/examples/camera_streamer/argus_sender/cmake/YamlCpp.cmake b/examples/camera_streamer/argus_sender/cmake/YamlCpp.cmake deleted file mode 100644 index 51418e22e1..0000000000 --- a/examples/camera_streamer/argus_sender/cmake/YamlCpp.cmake +++ /dev/null @@ -1,15 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -include(FetchContent) -set(YAML_CPP_BUILD_TESTS OFF CACHE BOOL "" FORCE) -set(YAML_CPP_BUILD_TOOLS OFF CACHE BOOL "" FORCE) -set(YAML_CPP_BUILD_CONTRIB OFF CACHE BOOL "" FORCE) -set(YAML_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) -FetchContent_Declare( - yaml_cpp - URL - https://github.com/jbeder/yaml-cpp/releases/download/yaml-cpp-0.9.0/yaml-cpp-yaml-cpp-0.9.0.tar.gz - URL_HASH - SHA256=298593d9c440fd9034b8b193d96318b76d49bc97c6ceadb7b0836edf0b6d7539 -) -FetchContent_MakeAvailable(yaml_cpp) diff --git a/examples/camera_streamer/argus_sender/configs/cameras.yaml b/examples/camera_streamer/argus_sender/configs/cameras.yaml new file mode 100644 index 0000000000..b2249e93df --- /dev/null +++ b/examples/camera_streamer/argus_sender/configs/cameras.yaml @@ -0,0 +1,75 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +version: 1 + +network: + bind: 0.0.0.0 + port: 5000 + reconnect_ms: 1000 + dump_wire: "" + +runtime: + frames: 0 + kpi: true + log_interval_s: 5 + bench_encode: false + +alignment: + warmup_frames: 60 + interval_frames: 60 + +streams: + - id: head + type: stereo + cameras: + - device: 0 + mode: 0 + - device: 1 + mode: 0 + output: + fps: 60 + codec: hevc + rotation: 180 + resize_mode: strict + bitrate_mbps: 50 + gop: 300 + # Example wrist maps: 1280x992 source, 1280x960 lookup grid, + # then 2x box-downsampled to 640x480. Replace with your calibration. + # Paths resolve relative to this config, independent of the working directory. + - id: wrist1 + type: mono + capture: + mode: fifo + fifo_length: 3 + cameras: + - {device: 4, mode: 0} + output: + fps: 60 + codec: raw_nv12 + width: 640 + height: 480 + resize_mode: strict + rotation: 0 + remap: + file: ../remaps/cam4.remap + capture_width: 1280 + capture_height: 992 + - id: wrist2 + type: mono + capture: + mode: fifo + fifo_length: 3 + cameras: + - {device: 5, mode: 0} + output: + fps: 60 + codec: raw_nv12 + width: 640 + height: 480 + resize_mode: strict + rotation: 0 + remap: + file: ../remaps/cam5.remap + capture_width: 1280 + capture_height: 992 diff --git a/examples/camera_streamer/argus_sender/configs/mixed_streams.example.yaml b/examples/camera_streamer/argus_sender/configs/mixed_streams.example.yaml new file mode 100644 index 0000000000..681522211d --- /dev/null +++ b/examples/camera_streamer/argus_sender/configs/mixed_streams.example.yaml @@ -0,0 +1,47 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +version: 1 + +network: + bind: 0.0.0.0 + port: 5000 + reconnect_ms: 1000 + +runtime: + frames: 0 + kpi: true + +alignment: + warmup_frames: 60 + interval_frames: 60 + +streams: + - id: front_stereo + type: stereo + cameras: + - {device: 0, mode: 0} + - {device: 1, mode: 0} + output: + fps: 60 + codec: hevc + width: 1280 + resize_mode: strict + rotation: 180 + bitrate_mbps: 25 + gop: 60 + + - id: wrist + type: mono + capture: + mode: fifo + fifo_length: 3 + cameras: + - {device: 2, mode: 0} + output: + fps: 30 + codec: raw_nv12 + width: 640 + height: 480 + resize_mode: center_crop + rotation: 0 diff --git a/examples/camera_streamer/argus_sender/deploy/10-camera-persistent-journal.conf b/examples/camera_streamer/argus_sender/deploy/10-camera-persistent-journal.conf new file mode 100644 index 0000000000..2dd2504bcc --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/10-camera-persistent-journal.conf @@ -0,0 +1,18 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Installed verbatim by deploy_argus_sender.sh into +# /etc/systemd/journald.conf.d/ during reference camera AGX provisioning. +# +# The reference camera AGXs ship with volatile journald (Storage=auto with no +# /var/log/journal), so `journalctl` history is wiped on every reboot. After a +# real spontaneous camera fault that forces a reboot there is then no journal +# left to post-mortem -- exactly when the argus_sender / nvargus logs matter +# most. Storage=persistent keeps the journal on disk across reboots. +# +# Capped so a chatty AGX cannot fill the (small) eMMC: SystemMaxUse bounds the +# on-disk journal, SystemKeepFree keeps headroom for everything else. +[Journal] +Storage=persistent +SystemMaxUse=500M +SystemKeepFree=1G diff --git a/examples/camera_streamer/argus_sender/deploy/README.md b/examples/camera_streamer/argus_sender/deploy/README.md new file mode 100644 index 0000000000..fe5d784d87 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/README.md @@ -0,0 +1,110 @@ + + +# Optional Orin/Thor service deployment + +This deployment targets a clean Orin or Thor with a working Argus camera driver. +The installed board support package owns camera initialization, including any +serializer/deserializer setup needed at boot. Confirm that the cameras can +capture through Argus before deploying. The script installs the camera-streamer +services and configuration; it does not modify driver or vendor camera settings. + +All deployment templates and helpers are local to this folder. The script copies +the sender, remaps and tests into a staging directory on the board, builds the +sender, runs its unit tests, and validates the config before replacing the current +installation. It installs: + +- `camera-argus-sender.service`: single-process Argus capture, bounded restart + attempts, clock pinning before start, and exclusive camera ownership. +- `camera-time-sync.service`: bounded chrony synchronization before stamping frames. +- `camera-vi-error-trace.service`: a dedicated VI error trace instance, active when + the BSP exposes the matching tracepoint. +- A capped persistent journald drop-in, a chrony client drop-in, and the clock helper. + +## Requirements + +Run deployment from a Linux host with SSH, rsync, and the usual shell tools. +Configure key-based SSH access and the sudo permissions needed for service setup. +The script does not provision credentials or change sudo policy. Bare IP addresses +use the `nvidia` account unless `--user` is provided; SSH aliases use their configured +account. A target is always required. + +Install the Jetson Multimedia API, CMake, a C++ compiler and chrony on the board. +The chrony configuration must include `/etc/chrony/conf.d/`; the standard Ubuntu +configuration normally provides this. The script checks that include and writes +only its own `camera-agx.conf` drop-in. Stop any manually launched sender before +using service deployment. + +The host must serve a common time source over the camera LAN. The +`camera-chrony-host.conf.in` template contains `@ROBOT_LAN_SUBNET@`; render it for +your LAN and include it in the host chrony configuration. Deployment configures +the board to use the host IP passed below. + +## Deploy, inspect, or restore + +From this folder, with the bundled remaps present: + +```bash +export SENDER_SSH=user@camera-board +export HOST_LAN_IP=host-camera-lan-address # Replace with the actual host LAN IP. +./deploy_argus_sender.sh "$SENDER_SSH" --host-lan-ip "$HOST_LAN_IP" + +# Read-only service, clock, binary, configuration and synchronization audit: +./deploy_argus_sender.sh "$SENDER_SSH" --host-lan-ip "$HOST_LAN_IP" --check + +# Restore the snapshot taken immediately before the latest deployment: +./deploy_argus_sender.sh "$SENDER_SSH" --rollback +# --restore is an alias for --rollback. +``` + +The target is `~/camera_streamer/argus_sender`, including its `remaps/` directory. +Its tests are included in `~/camera_streamer/argus_sender/tests`. The deployment +leaves the other components and example-level tests in place. Launch the receiver +and consumers on the host using the main example README. + +Deployment always builds in a fresh staging directory. To rebuild manually in the +installed source tree, remove its `build/` directory before running `build.sh`; +the transferred CMake cache contains the staging paths. + +## Rollback behavior + +Before activation, the script snapshots the previous sender tree, including its +tests, its owned service/configuration files, and the enabled/running states of its services, +`nvargus-daemon`, and chrony. The snapshot is under +`~/camera_streamer/.deployment-backup`. Each deployment replaces this restore point +only after the staged build and snapshot have succeeded. + +If activation fails, the script automatically restores that snapshot. An explicit +`--rollback` does the same: + +- For a first installation, remove the newly installed sender directory, service + files and drop-ins, and restore the recorded platform service state. +- For an update, restore the previous binary, remaps, configuration and service + state, including whether the previous sender was running or stopped. + +Rollback does not restart unrelated camera services or modify camera drivers. +Existing receiver/consumer directories and journal logs are retained. Installed +prerequisites and runtime clock pins are not removed or reset. A repeated restore +of the same snapshot does nothing. If SSH is interrupted during activation, retry +`--rollback` after reconnecting. + +## Diagnostics + +The sender service conflicts with `nvargus-daemon`, because single-process Argus +must own the cameras exclusively. Deployment saves the daemon's prior state before +disabling it; rollback restores that state. Do not run another Argus owner +concurrently. Capture retries are bounded by `StartLimitIntervalSec=200`, +`StartLimitBurst=6`, and `RestartSec=15`. + +```bash +ssh "$SENDER_SSH" 'journalctl -u camera-argus-sender -b --no-pager -n 100' +ssh "$SENDER_SSH" 'systemctl show -p ActiveState,NRestarts camera-argus-sender' +ssh "$SENDER_SSH" 'journalctl -u camera-vi-error-trace -f -o cat' +``` + +The clock helper pins runtime camera clocks and reports the current power mode. +Review the board's supported power/thermal configuration before changing it. +Wall-clock latency requires both hosts to be synchronized. The time-sync wait is +bounded, so inspect chrony state if the source was unavailable at boot. diff --git a/examples/camera_streamer/argus_sender/deploy/camera-argus-sender.service.in b/examples/camera_streamer/argus_sender/deploy/camera-argus-sender.service.in new file mode 100644 index 0000000000..ccd123eac7 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/camera-argus-sender.service.in @@ -0,0 +1,30 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# Installed by deploy_argus_sender.sh; camera driver initialization is owned by the BSP. +[Unit] +Description=AVS1 Argus camera sender (:5000) +# A bounded chrony wait improves frame timestamps while allowing an offline boot. +Wants=chrony.service camera-time-sync.service camera-vi-error-trace.service +After=nvargus-daemon.service nvpmodel.service chrony.service camera-time-sync.service camera-vi-error-trace.service +Conflicts=nvargus-daemon.service +# Bound retries while allowing transient camera startup failures to recover. +StartLimitIntervalSec=200 +StartLimitBurst=6 + +[Service] +User=@AGX_USER@ +WorkingDirectory=@SENDER_DIR@ +ExecStartPre=-+/bin/bash /usr/local/lib/camera/lock_jetson_camera_clocks.sh +# Conflicts enforces exclusive ownership; these also tolerate BSPs without a daemon unit. +ExecStartPre=-+/usr/bin/systemctl stop nvargus-daemon.service +ExecStartPre=-+/usr/bin/systemctl reset-failed nvargus-daemon.service +ExecStartPre=/bin/sleep 2 +ExecStart=@SENDER_DIR@/build/argus_sender --config @SENDER_DIR@/configs/cameras.yaml +Restart=always +RestartSec=15 +TimeoutStopSec=45 +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target diff --git a/examples/camera_streamer/argus_sender/deploy/camera-chrony-agx.conf.in b/examples/camera_streamer/argus_sender/deploy/camera-chrony-agx.conf.in new file mode 100644 index 0000000000..4730a74999 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/camera-chrony-agx.conf.in @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# chrony drop-in for the robot AGX — installed at /etc/chrony/conf.d/camera-agx.conf +# deploy time. The same deploy installs camera-time-sync.service so camera-argus-sender +# starts only after this source has disciplined the clock (see both unit files). +# +# The AGX has no internet; its only time source is the host PC over the robot +# LAN, which is internet-disciplined and re-serves. Software timestamping over the +# sub-ms link is ~10-50 us — far below the 33 ms (30fps) / 16 ms (60fps) frame +# budget. iburst = fast lock; short poll = tight tracking on the quiet link. +server @HOST_LAN_IP@ iburst minpoll 2 maxpoll 4 + +# Step (not slew) a large initial offset, but only in the first few updates after +# chronyd starts. Do the first big correction in a coordinated no-recording window. +makestep 1.0 3 diff --git a/examples/camera_streamer/argus_sender/deploy/camera-chrony-host.conf.in b/examples/camera_streamer/argus_sender/deploy/camera-chrony-host.conf.in new file mode 100644 index 0000000000..a0b50373f7 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/camera-chrony-host.conf.in @@ -0,0 +1,18 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# chrony drop-in for the host PC — installed at /etc/chrony/conf.d/camera-server.conf +# +# The host PC is disciplined to the internet (pool lines in the base chrony.conf) +# and re-serves that time to the robot AGX over the robot LAN. The robot-LAN NIC +# has no PHC, so this is software timestamping (~10-50 us over the sub-ms link). + +# Clients allowed to query this server (the robot LAN). +allow @ROBOT_LAN_SUBNET@ + +# Keep serving even when the internet upstream is unreachable, so the AGX stays +# disciplined to this host regardless of WAN state (offline-island mode). +local stratum 10 + +# Record the host<->internet clock relationship for dataset alignment / audit. +log tracking measurements statistics diff --git a/examples/camera_streamer/argus_sender/deploy/camera-time-sync.service.in b/examples/camera_streamer/argus_sender/deploy/camera-time-sync.service.in new file mode 100644 index 0000000000..c75081a180 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/camera-time-sync.service.in @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Installed on the camera board by deploy_argus_sender.sh. +# +# The AGX RTC does not hold time across a reboot: chrony steps CLOCK_REALTIME by +# minutes once the host answers (camera-chrony-agx.conf: iburst, makestep). Every +# AVS1 frame carries CLOCK_REALTIME stamps, so services that stamp frames must not +# start before that step. This oneshot blocks until chrony reports the clock within +# 0.1 s of its source (up to 60 tries x 2 s); on timeout it fails and dependents +# that only Want= it start anyway, with the failure visible in the journal. +[Unit] +Description=Wait for chrony to discipline the AGX clock before frame-stamping services start +After=chrony.service network-online.target +Wants=chrony.service +Before=time-sync.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/chronyc waitsync 60 0.1 0.0 2 +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target diff --git a/examples/camera_streamer/argus_sender/deploy/camera-vi-error-trace.service.in b/examples/camera_streamer/argus_sender/deploy/camera-vi-error-trace.service.in new file mode 100644 index 0000000000..b1c6d4b639 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/camera-vi-error-trace.service.in @@ -0,0 +1,27 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Capture only VI firmware error notifications. A private tracefs instance keeps +# this collector from consuming or clearing trace data owned by other tools. +[Unit] +Description=reference camera VI capture-error trace collector +Wants=sys-kernel-debug.mount +After=systemd-journald.service sys-kernel-debug.mount +Before=camera-argus-sender.service +ConditionPathExists=/sys/kernel/debug/tracing/events/tegra_rtcpu/rtcpu_vinotify_error/format + +[Service] +Type=simple +ExecStartPre=/bin/mkdir -p /sys/kernel/debug/tracing/instances/camera_vi_errors +ExecStartPre=/bin/sh -ec ': > /sys/kernel/debug/tracing/instances/camera_vi_errors/trace; echo 1 > /sys/kernel/debug/tracing/instances/camera_vi_errors/events/tegra_rtcpu/rtcpu_vinotify_error/enable; echo 1 > /sys/kernel/debug/tracing/instances/camera_vi_errors/tracing_on' +ExecStart=/bin/cat /sys/kernel/debug/tracing/instances/camera_vi_errors/trace_pipe +ExecStopPost=/bin/sh -c 'test ! -e /sys/kernel/debug/tracing/instances/camera_vi_errors/events/tegra_rtcpu/rtcpu_vinotify_error/enable || echo 0 > /sys/kernel/debug/tracing/instances/camera_vi_errors/events/tegra_rtcpu/rtcpu_vinotify_error/enable' +Restart=on-failure +RestartSec=1 +TimeoutStopSec=5 +StandardOutput=journal +StandardError=journal +SyslogIdentifier=camera-vi-error-trace + +[Install] +WantedBy=multi-user.target diff --git a/examples/camera_streamer/argus_sender/deploy/deploy_argus_sender.sh b/examples/camera_streamer/argus_sender/deploy/deploy_argus_sender.sh new file mode 100755 index 0000000000..e5b7e7ef2e --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/deploy_argus_sender.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Build and deploy argus_sender to an Orin/Thor with a working Argus camera driver. +# Usage: ./deploy_argus_sender.sh host [--host-lan-ip IP] [--user USER] +# [--check | --rollback | --restore] [--verbose] +# --check is read-only. --rollback/--restore restores the preceding deployment +# snapshot, or removes a first installation. Driver setup is owned by the BSP. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SENDER_SRC_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" +HOST="" +AGX_USER="nvidia" +HOST_LAN_IP="" +CHECK_ONLY=0 +ROLLBACK=0 +VERBOSE=0 +DEPLOYMENT_STARTED=0 +STAGE_DIR="" + +usage() { sed -n '5,9s/^# //p' "${BASH_SOURCE[0]}"; } +while [[ $# -gt 0 ]]; do + case "$1" in + --check) CHECK_ONLY=1; shift ;; + --rollback|--restore) ROLLBACK=1; shift ;; + --verbose|-v) VERBOSE=1; shift ;; + --user|--host-lan-ip) + [[ $# -ge 2 && -n "$2" ]] || { echo "$1 requires a value" >&2; exit 2; } + if [[ "$1" == --user ]]; then AGX_USER="$2"; else HOST_LAN_IP="$2"; fi + shift 2 + ;; + -h|--help) usage; exit 0 ;; + -*) echo "Unknown option: $1" >&2; exit 2 ;; + *) [[ -z "$HOST" ]] || { echo "Unexpected argument: $1" >&2; exit 2; }; HOST="$1"; shift ;; + esac +done +[[ -n "$HOST" && "$HOST" != -* ]] || { echo "A target SSH host is required" >&2; exit 2; } +[[ $CHECK_ONLY -eq 0 || $ROLLBACK -eq 0 ]] || { echo "--check and --rollback are mutually exclusive" >&2; exit 2; } +[[ "$AGX_USER" =~ ^[a-zA-Z0-9_-]+$ ]] || { echo "Invalid SSH user" >&2; exit 2; } +if [[ "$HOST" != *@* && "$HOST" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then HOST="${AGX_USER}@${HOST}"; fi +SSH=(ssh -o BatchMode=yes -o ConnectTimeout=10) +RSYNC_SSH="ssh -o BatchMode=yes -o ConnectTimeout=10" +RSYNC_ARGS=(-az) +[[ $VERBOSE -eq 0 ]] || RSYNC_ARGS+=(-v) +info() { printf ' [ .. ] %s\n' "$*"; } +ok() { printf ' [ OK ] %s\n' "$*"; } +fail() { printf ' [FAIL] %s\n' "$*" >&2; } +die() { fail "$*"; exit 1; } +remote() { "${SSH[@]}" "$HOST" "$@"; } +deployment_state() { + remote "sudo -n bash -s -- '$1' '$INSTALL_DIR' '${2:-}'" < "$SCRIPT_DIR/deployment_state.sh" +} +install_remote_file() { + local content="$1" destination="$2" current tmp + current="$(remote "sudo -n cat '$destination' 2>/dev/null" || true)" + [[ "$current" != "$content" ]] || return 0 + tmp="/tmp/camera_avs1_$$_$(basename "$destination")" + printf '%s\n' "$content" | remote "cat > '$tmp' && sudo -n install -D -m 0644 '$tmp' '$destination' && rm -f '$tmp'" +} +render() { + local file="$1"; shift + local -a replacements=() + while [[ $# -gt 0 ]]; do replacements+=(-e "s|@$1@|$2|g"); shift 2; done + sed "${replacements[@]}" "$file" +} +on_exit() { + local rc=$? + trap - EXIT + if [[ $rc -ne 0 && $DEPLOYMENT_STARTED -eq 1 ]]; then + fail "deployment failed; restoring the camera-streamer snapshot" + deployment_state restore || fail "automatic restore failed; retry with --rollback" + fi + if [[ -n "$STAGE_DIR" ]]; then remote "rm -rf -- '$STAGE_DIR'" || true; fi + exit "$rc" +} +trap on_exit EXIT +trap 'exit 130' INT +trap 'exit 143' TERM + +remote true >/dev/null 2>&1 || die "configure key-based SSH access to $HOST before deployment" +remote 'sudo -n true' >/dev/null 2>&1 || die "sudo access is required on $HOST" +FACTS="$(remote 'bash -s' <<'REMOTE' +printf 'USER=%s\nHOME=%s\n' "$(id -un)" "$HOME" +REMOTE +)" +fact() { awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' <<< "$FACTS"; } +AGX_HOME="$(fact HOME)" +REMOTE_USER="$(fact USER)" +[[ "$AGX_HOME" =~ ^/[a-zA-Z0-9_./-]+$ && "$REMOTE_USER" =~ ^[a-zA-Z0-9_-]+$ ]] || die "unsupported target home/user" +INSTALL_DIR="$AGX_HOME/camera_streamer" +SENDER_DIR="$INSTALL_DIR/argus_sender" +if [[ $ROLLBACK -eq 1 ]]; then + deployment_state restore + exit 0 +fi +if [[ -z "$HOST_LAN_IP" ]]; then + ROBOT_IP="$(ssh -G "$HOST" 2>/dev/null | awk '/^hostname / {print $2; exit}' || true)" + HOST_LAN_IP="$(ip -o route get "${ROBOT_IP:-${HOST#*@}}" 2>/dev/null | sed -n 's/.*src \([0-9.]*\).*/\1/p' | head -1 || true)" +fi +[[ "$HOST_LAN_IP" =~ ^[a-zA-Z0-9._:-]+$ ]] || die "pass this host's camera-LAN address with --host-lan-ip" + +check_deployment() { + remote "test -x '$SENDER_DIR/build/argus_sender'" + remote "'$SENDER_DIR/build/argus_sender' --check-config --config '$SENDER_DIR/configs/cameras.yaml'" + remote "ldd '$SENDER_DIR/build/argus_sender' | grep -Eq 'libnvargus\\.so([[:space:]]|$)' && ! ldd '$SENDER_DIR/build/argus_sender' | grep -q libnvargus_socketclient" + remote 'systemctl is-enabled --quiet camera-argus-sender.service && systemctl is-active --quiet camera-argus-sender.service' + remote "systemctl show -p Conflicts --value camera-argus-sender.service | grep -qw nvargus-daemon.service" + remote "systemctl show -p After --value camera-argus-sender.service | grep -qw camera-time-sync.service" + remote 'systemctl is-enabled --quiet camera-time-sync.service' + remote 'if systemctl is-active --quiet nvargus-daemon.service; then exit 1; fi' + remote "ss -tan | grep -Eq '[:.]5000[[:space:]]'" + remote "test -d /var/log/journal && grep -qs '^Storage=persistent' /etc/systemd/journald.conf.d/10-camera-persistent-journal.conf" + remote "grep -Fqs 'server $HOST_LAN_IP ' /etc/chrony/conf.d/camera-agx.conf" + remote 'chronyc waitsync 1 0.05 0.0 1' + remote 'if test -f /sys/kernel/debug/tracing/events/tegra_rtcpu/rtcpu_vinotify_error/format; then systemctl is-active --quiet camera-vi-error-trace.service && test "$(sudo -n cat /sys/kernel/debug/tracing/instances/camera_vi_errors/events/tegra_rtcpu/rtcpu_vinotify_error/enable)" = 1; fi' + bash "$SCRIPT_DIR/deploy_platform.sh" "$HOST" --jetson-clocks --check + ok "sender, configuration, time synchronization and supported diagnostics are healthy" +} +if [[ $CHECK_ONLY -eq 1 ]]; then + check_deployment + exit 0 +fi + +remote 'bash -s' <<'REMOTE' +set -euo pipefail +test -d /usr/src/jetson_multimedia_api +command -v cmake >/dev/null +command -v g++ >/dev/null +command -v chronyc >/dev/null +grep -Eqs '^[[:space:]]*(confdir|include).*conf\.d' /etc/chrony/chrony.conf || { + echo "Enable /etc/chrony/conf.d in chrony.conf before deploying" >&2 + exit 1 +} +main_pid="$(systemctl show -p MainPID --value camera-argus-sender.service 2>/dev/null || true)" +for pid in $(pgrep -x argus_sender 2>/dev/null || true); do + [[ "$pid" == "${main_pid:-0}" ]] || { echo "Unmanaged argus_sender PID $pid" >&2; exit 1; } +done +REMOTE + +candidate="$(remote "mktemp -d '$AGX_HOME/.camera-streamer-stage.XXXXXX'")" +[[ "$candidate" == "$AGX_HOME"/.camera-streamer-stage.* && "$candidate" =~ ^/[a-zA-Z0-9_./-]+$ ]] || die "invalid staging directory" +STAGE_DIR="$candidate" +info "staging sender, remaps and tests on $HOST" +rsync "${RSYNC_ARGS[@]}" --exclude build/ --exclude .git/ --exclude __pycache__/ --exclude .DS_Store \ + -e "$RSYNC_SSH" "$SENDER_SRC_DIR/" "$HOST:$STAGE_DIR/argus_sender/" +info "building and validating before changing the running deployment" +remote "cmake -S '$STAGE_DIR/argus_sender' -B '$STAGE_DIR/argus_sender/build' -DCMAKE_BUILD_TYPE=Release -DDISABLE_MULTIPROCESS=ON && cmake --build '$STAGE_DIR/argus_sender/build' -j2 && ctest --test-dir '$STAGE_DIR/argus_sender/build' --output-on-failure && '$STAGE_DIR/argus_sender/build/argus_sender' --check-config --config '$STAGE_DIR/argus_sender/configs/cameras.yaml'" +deployment_state snapshot +DEPLOYMENT_STARTED=1 +deployment_state activate "$STAGE_DIR" + +info "installing camera-streamer configuration and services" +install_remote_file "$(cat "$SCRIPT_DIR/10-camera-persistent-journal.conf")" /etc/systemd/journald.conf.d/10-camera-persistent-journal.conf +remote 'sudo -n mkdir -p /var/log/journal && sudo -n systemd-tmpfiles --create --prefix /var/log/journal && sudo -n systemctl restart systemd-journald && sudo -n journalctl --flush' +install_remote_file "$(render "$SCRIPT_DIR/camera-chrony-agx.conf.in" HOST_LAN_IP "$HOST_LAN_IP")" /etc/chrony/conf.d/camera-agx.conf +remote 'sudo -n systemctl restart chrony.service' +install_remote_file "$(cat "$SCRIPT_DIR/lock_jetson_camera_clocks.sh")" /usr/local/lib/camera/lock_jetson_camera_clocks.sh +install_remote_file "$(cat "$SCRIPT_DIR/camera-time-sync.service.in")" /etc/systemd/system/camera-time-sync.service +install_remote_file "$(cat "$SCRIPT_DIR/camera-vi-error-trace.service.in")" /etc/systemd/system/camera-vi-error-trace.service +install_remote_file "$(render "$SCRIPT_DIR/camera-argus-sender.service.in" AGX_USER "$REMOTE_USER" SENDER_DIR "$SENDER_DIR")" /etc/systemd/system/camera-argus-sender.service +remote 'sudo -n systemctl daemon-reload' +remote 'sudo -n systemctl enable camera-time-sync.service camera-vi-error-trace.service camera-argus-sender.service' +remote 'if test "$(systemctl show -p LoadState --value nvargus-daemon.service)" != not-found; then sudo -n systemctl disable --now nvargus-daemon.service; fi' +remote 'sudo -n systemctl reset-failed camera-argus-sender.service && sudo -n systemctl restart camera-argus-sender.service' + +# Argus capture startup can take tens of seconds on a cold board. +ready=0 +for _ in $(seq 1 90); do + if remote "systemctl is-active --quiet camera-argus-sender.service && ss -tan | grep -Eq '[:.]5000[[:space:]]'"; then + ready=1 + break + fi + sleep 1 +done +if [[ $ready -ne 1 ]]; then + remote 'journalctl -u camera-argus-sender -n 40 --no-pager' || true + die "sender did not become ready" +fi +DEPLOYMENT_STARTED=0 +ok "camera-argus-sender is deployed; use --rollback to restore the preceding snapshot" diff --git a/examples/camera_streamer/argus_sender/deploy/deploy_platform.sh b/examples/camera_streamer/argus_sender/deploy/deploy_platform.sh new file mode 100755 index 0000000000..7e581d8055 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/deploy_platform.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# Deploy only the clock helper needed by the camera-streamer service setup. +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +host="${1:?An SSH target is required}" +shift +check=0 +for arg in "$@"; do + case "$arg" in + --check) check=1 ;; + --jetson-clocks|--verbose) ;; + *) echo "Unknown argument: $arg" >&2; exit 2 ;; + esac +done +ssh_args=(-o BatchMode=yes -o ConnectTimeout=10) +if [[ "$check" -eq 1 ]]; then + ssh "${ssh_args[@]}" "$host" 'sudo -n bash -s' <<'REMOTE' +set -euo pipefail +command -v jetson_clocks >/dev/null +sudo -n jetson_clocks --show +for name in vi isp nvcsi vic emc; do + base="/sys/kernel/debug/bpmp/debug/clk/$name" + [[ -d "$base" ]] || continue + [[ "$(cat "$base/mrq_rate_locked")" == 1 ]] + [[ "$(cat "$base/rate")" == "$(cat "$base/max_rate")" ]] +done +REMOTE + exit 0 +fi +ssh "${ssh_args[@]}" "$host" 'sudo -n mkdir -p /usr/local/lib/camera' +ssh "${ssh_args[@]}" "$host" 'sudo -n tee /usr/local/lib/camera/lock_jetson_camera_clocks.sh >/dev/null' \ + < "$here/lock_jetson_camera_clocks.sh" +ssh "${ssh_args[@]}" "$host" 'sudo -n bash /usr/local/lib/camera/lock_jetson_camera_clocks.sh' diff --git a/examples/camera_streamer/argus_sender/deploy/deployment_state.sh b/examples/camera_streamer/argus_sender/deploy/deployment_state.sh new file mode 100755 index 0000000000..8140b434a0 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/deployment_state.sh @@ -0,0 +1,137 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# Snapshot and restore only the camera-streamer installation and its service state. +set -euo pipefail + +action="${1:?snapshot, activate or restore is required}" +root="${2:?The camera_streamer installation directory is required}" +[[ "$root" == /*/camera_streamer && "$root" != *'/../'* ]] || { + echo "Invalid camera-streamer installation directory: $root" >&2 + exit 2 +} +backup="$root/.deployment-backup" +# Prefix system paths for isolated filesystem tests; deployment runs with sudo. +system_root="${CAMERA_DEPLOY_SYSTEM_ROOT:-}" +owned_directories=(argus_sender) +owned_units=(camera-argus-sender camera-time-sync camera-vi-error-trace) +saved_units=(nvargus-daemon chrony camera-time-sync camera-vi-error-trace camera-argus-sender) +owned_files=( + /etc/systemd/system/camera-argus-sender.service + /etc/systemd/system/camera-time-sync.service + /etc/systemd/system/camera-vi-error-trace.service + /etc/chrony/conf.d/camera-agx.conf + /etc/systemd/journald.conf.d/10-camera-persistent-journal.conf + /usr/local/lib/camera/lock_jetson_camera_clocks.sh +) + +unit_exists() { + local load_state + load_state="$(systemctl show -p LoadState --value "$1.service" 2>/dev/null || true)" + [[ -n "$load_state" && "$load_state" != not-found ]] +} + +stop_owned_units() { + local unit + for unit in "${owned_units[@]}"; do + if unit_exists "$unit"; then + systemctl stop "$unit.service" + systemctl disable "$unit.service" + fi + done +} + +case "$action" in + snapshot) + mkdir -p "$root" + pending="$(mktemp -d "$root/.deployment-backup.XXXXXX")" + chmod 700 "$pending" + trap 'rm -rf -- "$pending"' EXIT + for directory in "${owned_directories[@]}"; do + if [[ -e "$root/$directory" ]]; then + [[ ! -L "$root/$directory" ]] || { + echo "Refusing to replace a symlink: $root/$directory" >&2 + exit 1 + } + cp -a -- "$root/$directory" "$pending/$directory" + fi + done + for file in "${owned_files[@]}"; do + if [[ -e "$system_root$file" || -L "$system_root$file" ]]; then + mkdir -p "$(dirname "$pending/files$file")" + cp -a -- "$system_root$file" "$pending/files$file" + fi + done + for unit in "${saved_units[@]}"; do + enabled="$(systemctl show -p UnitFileState --value "$unit.service" 2>/dev/null || true)" + active="$(systemctl show -p ActiveState --value "$unit.service" 2>/dev/null || true)" + printf '%s|%s|%s\n' "$unit" "${enabled:-not-found}" "${active:-inactive}" >> "$pending/services" + done + touch "$pending/complete" + rm -rf -- "$backup" + mv -- "$pending" "$backup" + trap - EXIT + ;; + activate) + stage="${3:?A staged build directory is required}" + # Bash 3 does not apply errexit to [[ ... ]]; activation guards must exit explicitly. + [[ "$stage" == "${root%/camera_streamer}"/.camera-streamer-stage.* ]] || { + echo "Invalid staged deployment path: $stage" >&2 + exit 1 + } + [[ -f "$backup/complete" && -x "$stage/argus_sender/build/argus_sender" && -d "$stage/argus_sender/tests" ]] || { + echo "Activation requires a completed snapshot and a validated staged build" >&2 + exit 1 + } + stop_owned_units + for directory in "${owned_directories[@]}"; do + rm -rf -- "$root/$directory" + mv -- "$stage/$directory" "$root/$directory" + done + ;; + restore) + [[ -f "$backup/complete" ]] || { + echo "No completed camera-streamer deployment snapshot at $backup" >&2 + exit 1 + } + if [[ -f "$backup/restored" ]]; then + echo "Camera-streamer deployment snapshot is already restored" + exit 0 + fi + stop_owned_units + for file in "${owned_files[@]}"; do + rm -f -- "$system_root$file" + if [[ -e "$backup/files$file" || -L "$backup/files$file" ]]; then + mkdir -p "$(dirname "$system_root$file")" + cp -a -- "$backup/files$file" "$system_root$file" + fi + done + for directory in "${owned_directories[@]}"; do + rm -rf -- "$root/$directory" + if [[ -d "$backup/$directory" ]]; then + cp -a -- "$backup/$directory" "$root/$directory" + fi + done + systemctl daemon-reload + while IFS='|' read -r unit enabled active; do + if ! unit_exists "$unit"; then + continue + fi + case "$enabled" in + enabled) systemctl enable "$unit.service" ;; + enabled-runtime) systemctl enable --runtime "$unit.service" ;; + disabled) systemctl disable "$unit.service" ;; + esac + systemctl reset-failed "$unit.service" || true + if [[ "$active" == active ]]; then + systemctl restart "$unit.service" + else + systemctl stop "$unit.service" + fi + done < "$backup/services" + systemctl try-restart systemd-journald.service + touch "$backup/restored" + echo "Restored the camera-streamer deployment snapshot" + ;; + *) echo "Unknown deployment state action: $action" >&2; exit 2 ;; +esac diff --git a/examples/camera_streamer/argus_sender/deploy/lock_jetson_camera_clocks.sh b/examples/camera_streamer/argus_sender/deploy/lock_jetson_camera_clocks.sh new file mode 100755 index 0000000000..9b083ac222 --- /dev/null +++ b/examples/camera_streamer/argus_sender/deploy/lock_jetson_camera_clocks.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Lock the Jetson clocks the Argus capture path depends on. +# +# This matches the Sensing SHW5G/S56 latency-test condition: +# nvpmodel MAXN (ceiling) + jetson_clocks (pin at ceiling) +# + VI/ISP/NVCSI/VIC/EMC mrq_rate_locked + max_rate. +# +# The sender service runs this helper before capture. It can also be run +# manually with sudo. Clock pins are runtime state and do not survive a reboot. + +set -euo pipefail + +if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then + exec sudo -E "$0" "$@" +fi + +# ── power mode ──────────────────────────────────────────────────────── +# nvpmodel sets the frequency CEILING that jetson_clocks then pins against, and +# unlike jetson_clocks it IS persistent. If it has drifted off MAXN, pinning +# still "succeeds" but clamps everything to a reduced ceiling — the failure mode +# is a robot that looks healthy and quietly runs slow, so say so explicitly. +# +# MAXN's mode id is not guaranteed to be 0 on every board/BSP, so resolve it +# from nvpmodel.conf rather than hardcoding. +maxn_id() { + sed -n 's/^[[:space:]]*<[[:space:]]*POWER_MODEL[[:space:]]\+ID=\([0-9]\+\)[[:space:]]\+NAME=MAXN.*/\1/p' \ + /etc/nvpmodel.conf 2>/dev/null | head -1 +} + +if command -v nvpmodel >/dev/null 2>&1; then + cur_name="$(nvpmodel -q 2>/dev/null | sed -n 's/^NV Power Mode: *//p' | head -1 || true)" + cur_id="$(nvpmodel -q 2>/dev/null | sed -n '/^NV Power Mode/{n;s/^[[:space:]]*\([0-9]\+\).*/\1/p;}' | head -1 || true)" + want_id="$(maxn_id)" + printf "nvpmodel: mode=%s id=%s (MAXN id=%s)\n" \ + "${cur_name:-?}" "${cur_id:-?}" "${want_id:-?}" + if [[ "$cur_name" != MAXN* ]]; then + echo "WARNING: power mode is '${cur_name:-unknown}', not MAXN — clocks will pin" >&2 + echo " to a REDUCED ceiling. Fix persistently with:" >&2 + echo " sudo nvpmodel -m ${want_id:-0} # then reboot, or re-run this script" >&2 + fi +else + echo "nvpmodel not found; skipping power-mode check" >&2 +fi + +# ── pin at the ceiling ──────────────────────────────────────────────── +# Runtime sysfs only: per-CPU min frequency raised to max, CPU idle states +# disabled, GPU rail and EMC pinned. Undone by a reboot or any `nvpmodel -m`. +if command -v jetson_clocks >/dev/null 2>&1; then + jetson_clocks + echo "jetson_clocks applied" +else + echo "jetson_clocks not found; skipping CPU/GPU/EMC pinning" >&2 +fi + +# ── camera-specific clocks ──────────────────────────────────────────── +lock_clk() { + local name="$1" + local base="/sys/kernel/debug/bpmp/debug/clk/$name" + if [[ ! -d "$base" ]]; then + echo "missing $base; skipping $name" >&2 + return 0 + fi + + echo 1 > "$base/mrq_rate_locked" + cat "$base/max_rate" > "$base/rate" + + printf "%-5s locked=%s rate=%s max=%s\n" \ + "$name" \ + "$(cat "$base/mrq_rate_locked")" \ + "$(cat "$base/rate")" \ + "$(cat "$base/max_rate")" +} + +lock_clk vi +lock_clk isp +lock_clk nvcsi +lock_clk vic +lock_clk emc diff --git a/examples/camera_streamer/__init__.py b/examples/camera_streamer/argus_sender/remaps/.gitattributes similarity index 70% rename from examples/camera_streamer/__init__.py rename to examples/camera_streamer/argus_sender/remaps/.gitattributes index 544e2d4224..d6a04a506e 100644 --- a/examples/camera_streamer/__init__.py +++ b/examples/camera_streamer/argus_sender/remaps/.gitattributes @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Standalone Sensing/Argus camera streaming example.""" +*.remap filter=lfs diff=lfs merge=lfs -text diff --git a/examples/camera_streamer/argus_sender/remaps/README.md b/examples/camera_streamer/argus_sender/remaps/README.md new file mode 100644 index 0000000000..063380398b --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/README.md @@ -0,0 +1,51 @@ + + +# Example remaps + +`cam4.remap` and `cam5.remap` are example sender-side wrist undistortion maps for +the default camera config. They use approximate SHW5G fisheye intrinsics from a +reference rig; the wrist values reuse averaged head intrinsics. They are suitable +for demonstrating the viewing pipeline, and have no per-unit calibration accuracy +or stereo extrinsics. Calibrate your own cameras for metric work. + +The files are copied as real assets into this example and tracked with Git LFS. +The sender keeps its calibration assets under `argus_sender/remaps/`. The config resolves +`../remaps/cam4.remap` relative to `argus_sender/configs/cameras.yaml`. + +| Property | Example value | +| --- | --- | +| Calibration image size | 640x496 | +| Captured NV12 source | 1280x992, unrotated | +| Remap lookup grid | 1280x960 | +| Sender output | 640x480 NV12 | +| Filtering | Bilinear lookup followed by 2x box downsampling | +| Projection | Wrist-left balance=0 pinhole target, shared by both wrist maps | + +The generator follows the reference setup's projection: compute a balance=0 +undistorted 640x496 target, center-crop its principal point to 640x480, then scale +the target K and lookup grid by 2. Each camera's source K is also scaled by 2; +fisheye D is unchanged. The sender performs the box downsampling. Changing this +to a 640x480 lookup grid changes the filtering and image quality. + +To regenerate on a host with Python, NumPy and OpenCV: + +```bash +python3 -m venv /tmp/camera-remaps +/tmp/camera-remaps/bin/pip install 'numpy>=1.26,<2' 'opencv-python-headless>=4.9,<4.12' +/tmp/camera-remaps/bin/python argus_sender/remaps/generate_example.py +``` + +Pass `--calibration /path/to/calibration.json --output /path/to/remaps` for another +calibration. The example generator expects `image_size: [640, 496]` and +`cameras.wrist_left` / `cameras.wrist_right`, each with a 3x3 `K` and four-element +fisheye `D`. For different sensor geometry, generate compatible RMAPs and update +the sender config together. Device IDs in this example are 4 and 5. + +`manifest.json` records calibration provenance, geometry and SHA256 checksums. +The RMAP v1 binary layout is little endian: `RMAP`, uint32 grid width, uint32 grid +height, float32 balance, then row-major float32 X and Y arrays. Coordinates address +the captured source; the file size is `16 + width * height * 8` bytes. The sender +checks geometry and rejects maps with excessive out-of-source coordinates. diff --git a/examples/camera_streamer/argus_sender/remaps/cam4.remap b/examples/camera_streamer/argus_sender/remaps/cam4.remap new file mode 100644 index 0000000000..9b97859de6 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/cam4.remap @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3956ccdc9dceab5d7cb873d68a14ebb7bb8c41d15c85bf3f7f1a4e82c39aca0b +size 9830416 diff --git a/examples/camera_streamer/argus_sender/remaps/cam4.remap.license b/examples/camera_streamer/argus_sender/remaps/cam4.remap.license new file mode 100644 index 0000000000..817df24424 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/cam4.remap.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +SPDX-License-Identifier: Apache-2.0 diff --git a/examples/camera_streamer/argus_sender/remaps/cam5.remap b/examples/camera_streamer/argus_sender/remaps/cam5.remap new file mode 100644 index 0000000000..9b97859de6 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/cam5.remap @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3956ccdc9dceab5d7cb873d68a14ebb7bb8c41d15c85bf3f7f1a4e82c39aca0b +size 9830416 diff --git a/examples/camera_streamer/argus_sender/remaps/cam5.remap.license b/examples/camera_streamer/argus_sender/remaps/cam5.remap.license new file mode 100644 index 0000000000..817df24424 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/cam5.remap.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +SPDX-License-Identifier: Apache-2.0 diff --git a/examples/camera_streamer/argus_sender/remaps/example_calibration.json b/examples/camera_streamer/argus_sender/remaps/example_calibration.json new file mode 100644 index 0000000000..34cf3894aa --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/example_calibration.json @@ -0,0 +1,111 @@ +{ + "model": "fisheye", + "example_only": true, + "notes": "Approximate SHW5G intrinsics borrowed from a reference camera rig. Wrist intrinsics reuse the average head intrinsics. Viewing example only; no stereo extrinsics or per-unit accuracy. Calibrate your own cameras for metric use.", + "image_size": [ + 640, + 496 + ], + "cameras": { + "head_left": { + "K": [ + [ + 242.6581728514154, + 0.0, + 329.77843583722023 + ], + [ + 0.0, + 242.7556582093197, + 245.19517263911524 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.2326861408824948, + -0.0021612075891944285, + -0.12838685858963805, + 0.04584376532081385 + ] + }, + "head_right": { + "K": [ + [ + 242.07198075308452, + 0.0, + 327.2839795901617 + ], + [ + 0.0, + 242.26977718701468, + 250.3546880193839 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.23554618045873357, + -0.008756543168586194, + -0.11903091472005982, + 0.04190893303328575 + ] + }, + "wrist_left": { + "K": [ + [ + 242.36507680224997, + 0.0, + 328.53120771369095 + ], + [ + 0.0, + 242.5127176981672, + 247.77493032924957 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.2341161606706142, + -0.005458875378890312, + -0.12370888665484894, + 0.0438763491770498 + ] + }, + "wrist_right": { + "K": [ + [ + 242.36507680224997, + 0.0, + 328.53120771369095 + ], + [ + 0.0, + 242.5127176981672, + 247.77493032924957 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.2341161606706142, + -0.005458875378890312, + -0.12370888665484894, + 0.0438763491770498 + ] + } + } +} diff --git a/examples/camera_streamer/argus_sender/remaps/example_calibration.json.license b/examples/camera_streamer/argus_sender/remaps/example_calibration.json.license new file mode 100644 index 0000000000..817df24424 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/example_calibration.json.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +SPDX-License-Identifier: Apache-2.0 diff --git a/examples/camera_streamer/argus_sender/remaps/generate_example.py b/examples/camera_streamer/argus_sender/remaps/generate_example.py new file mode 100755 index 0000000000..197aefc8dd --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/generate_example.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +"""Generate the example wrist RMAPs from a standalone fisheye calibration JSON.""" + +from __future__ import annotations + +import argparse +import hashlib +import json +from pathlib import Path +import struct + +import cv2 +import numpy as np + +HEADER = struct.Struct("<4sIIf") +CAMERAS = {"wrist_left": 4, "wrist_right": 5} + + +def generate(calibration: Path, output: Path) -> dict: + calib = json.loads(calibration.read_text()) + width, height = calib["image_size"] + if (width, height) != (640, 496): + raise ValueError("This example requires intrinsics at 640x496") + left = calib["cameras"]["wrist_left"] + target = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify( + np.array(left["K"], dtype=np.float64), + np.array(left["D"], dtype=np.float64).reshape(4, 1), + (width, height), + np.eye(3), + balance=0.0, + new_size=(width, height), + fov_scale=1.0, + ) + target[1, 2] -= (height - 480) // 2 + scale = np.diag([2.0, 2.0, 1.0]) + target = scale @ target + manifest = { + "format": "RMAP v1", + "example_only": True, + "source_calibration": calibration.name, + "source_size": [1280, 992], + "dest_size": [1280, 960], + "output_size": [640, 480], + "supersample": 2, + "target_K": target.tolist(), + "rotation": 0, + "cameras": [], + } + output.mkdir(parents=True, exist_ok=True) + for role, device in CAMERAS.items(): + entry = calib["cameras"][role] + mx, my = cv2.fisheye.initUndistortRectifyMap( + scale @ np.array(entry["K"], dtype=np.float64), + np.array(entry["D"], dtype=np.float64).reshape(4, 1), + np.eye(3), + target, + (1280, 960), + cv2.CV_32FC1, + ) + outside = (mx < 0) | (mx >= 1279) | (my < 0) | (my >= 991) + if not np.isfinite(mx).all() or not np.isfinite(my).all(): + raise ValueError(f"Non-finite map coordinates for {role}") + if outside.mean() > 0.01: + raise ValueError(f"More than 1% out-of-source coordinates for {role}") + data = ( + HEADER.pack(b"RMAP", 1280, 960, 0.0) + + mx.astype(" None: + parser = argparse.ArgumentParser(description=__doc__) + here = Path(__file__).resolve().parent + parser.add_argument( + "--calibration", type=Path, default=here / "example_calibration.json" + ) + parser.add_argument("--output", type=Path, default=here) + args = parser.parse_args() + manifest = generate(args.calibration, args.output) + for camera in manifest["cameras"]: + print(f"{camera['file']}: {camera['bytes']} bytes, SHA256 {camera['sha256']}") + + +if __name__ == "__main__": + main() diff --git a/examples/camera_streamer/argus_sender/remaps/manifest.json b/examples/camera_streamer/argus_sender/remaps/manifest.json new file mode 100644 index 0000000000..d6ecadea24 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/manifest.json @@ -0,0 +1,98 @@ +{ + "format": "RMAP v1", + "example_only": true, + "source_calibration": "example_calibration.json", + "source_size": [ + 1280, + 992 + ], + "dest_size": [ + 1280, + 960 + ], + "output_size": [ + 640, + 480 + ], + "supersample": 2, + "target_K": [ + [ + 395.1962525199233, + 0.0, + 687.1392303278519 + ], + [ + 0.0, + 395.4369931808922, + 479.3134227344447 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "rotation": 0, + "cameras": [ + { + "role": "wrist_left", + "argus_id": 4, + "file": "cam4.remap", + "bytes": 9830416, + "sha256": "3956ccdc9dceab5d7cb873d68a14ebb7bb8c41d15c85bf3f7f1a4e82c39aca0b", + "K": [ + [ + 242.36507680224997, + 0.0, + 328.53120771369095 + ], + [ + 0.0, + 242.5127176981672, + 247.77493032924957 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.2341161606706142, + -0.005458875378890312, + -0.12370888665484894, + 0.0438763491770498 + ] + }, + { + "role": "wrist_right", + "argus_id": 5, + "file": "cam5.remap", + "bytes": 9830416, + "sha256": "3956ccdc9dceab5d7cb873d68a14ebb7bb8c41d15c85bf3f7f1a4e82c39aca0b", + "K": [ + [ + 242.36507680224997, + 0.0, + 328.53120771369095 + ], + [ + 0.0, + 242.5127176981672, + 247.77493032924957 + ], + [ + 0.0, + 0.0, + 1.0 + ] + ], + "D": [ + 0.2341161606706142, + -0.005458875378890312, + -0.12370888665484894, + 0.0438763491770498 + ] + } + ] +} diff --git a/examples/camera_streamer/argus_sender/remaps/manifest.json.license b/examples/camera_streamer/argus_sender/remaps/manifest.json.license new file mode 100644 index 0000000000..817df24424 --- /dev/null +++ b/examples/camera_streamer/argus_sender/remaps/manifest.json.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +SPDX-License-Identifier: Apache-2.0 diff --git a/examples/camera_streamer/argus_sender/run.sh b/examples/camera_streamer/argus_sender/run.sh new file mode 100755 index 0000000000..1bf2657be8 --- /dev/null +++ b/examples/camera_streamer/argus_sender/run.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +binary="${project_dir}/build/argus_sender" +config="${project_dir}/configs/cameras.yaml" + +if [[ ! -x "${binary}" ]]; then + echo "error: ${binary} does not exist; run ./build.sh first." >&2 + exit 1 +fi + +if command -v systemctl >/dev/null 2>&1 \ + && systemctl is-active --quiet nvargus-daemon.service; then + echo "stopping nvargus-daemon before starting single-process argus_sender" + if ! sudo -n systemctl stop nvargus-daemon.service; then + echo "error: could not stop nvargus-daemon; passwordless sudo is required." >&2 + exit 1 + fi + sudo -n systemctl reset-failed nvargus-daemon.service 2>/dev/null || true + sleep 2 +fi + +if command -v systemctl >/dev/null 2>&1 \ + && systemctl is-active --quiet nvargus-daemon.service; then + echo "error: nvargus-daemon is still active; refusing competing Argus ownership." >&2 + exit 1 +fi + +exec "${binary}" --config "${config}" "$@" diff --git a/examples/camera_streamer/argus_sender/src/Avs1Wire.cpp b/examples/camera_streamer/argus_sender/src/Avs1Wire.cpp index 34006849e5..da36189eca 100644 --- a/examples/camera_streamer/argus_sender/src/Avs1Wire.cpp +++ b/examples/camera_streamer/argus_sender/src/Avs1Wire.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -121,11 +121,7 @@ bool validateStream( if (stream.fpsDenominator == 0) return fail(error, "AVS1 FPS denominator is zero"); - if (stream.rotation != 0 && stream.rotation != 180) - return fail(error, "unsupported AVS1 rotation"); const bool streamPresent = (stream.flags & AVS1_STREAM_PRESENT) != 0; - if (streamPresent != (stream.sequence != 0)) - return fail(error, "AVS1 stream sequence must be nonzero only when the stream is present"); for (size_t i = 0; i < stream.samples.size(); ++i) { const Avs1SampleRecord& sample = stream.samples[i]; @@ -317,12 +313,6 @@ bool parseAvs1WireRecord(const uint8_t* data, size_t size, Avs1Bundle* bundle, c { return fail(error, "unsupported AVS1 header"); } - if (parsed.flags & ~kKnownBundleFlags) - return fail(error, "unknown AVS1 bundle flags"); - if ((parsed.flags & AVS1_BUNDLE_SENDER_CLOCK_TAI) && !(parsed.flags & AVS1_BUNDLE_SENDER_CLOCK_VALID)) - return fail(error, "AVS1 TAI flag requires a valid sender clock"); - if (parsed.sequence == 0 || parsed.sessionId == 0) - return fail(error, "AVS1 sequence and session ID must be nonzero"); if (!validateSenderTsc(parsed.flags, parsed.senderCaptureReadyTscNs, error)) { return false; @@ -343,7 +333,6 @@ bool parseAvs1WireRecord(const uint8_t* data, size_t size, Avs1Bundle* bundle, c std::vector firstSamples(streamCount); std::vector streamSampleCounts(streamCount); parsed.streams.resize(streamCount); - uint32_t flatCursor = 0; for (uint16_t i = 0; i < streamCount; ++i) { uint16_t streamIndex = 0; @@ -364,19 +353,14 @@ bool parseAvs1WireRecord(const uint8_t* data, size_t size, Avs1Bundle* bundle, c { return fail(error, "short AVS1 stream descriptor"); } - // Validate counts before resize so malformed tables cannot amplify allocations. - const uint16_t expectedCount = (stream.flags & AVS1_STREAM_STEREO) ? 2 : 1; - if (streamIndex != i || reserved32 != 0 || streamSampleCounts[i] != expectedCount || - firstSamples[i] != flatCursor || flatCursor + streamSampleCounts[i] > sampleCount) + if (streamIndex != i || reserved32 != 0 || + static_cast(firstSamples[i]) + streamSampleCounts[i] > sampleCount) { return fail(error, "invalid AVS1 stream descriptor index"); } - flatCursor += streamSampleCounts[i]; stream.samples.resize(streamSampleCounts[i]); } - if (flatCursor != sampleCount) - return fail(error, "AVS1 sample descriptors are not fully referenced"); std::vector locations(sampleCount); for (uint16_t flat = 0; flat < sampleCount; ++flat) { @@ -423,14 +407,6 @@ bool parseAvs1WireRecord(const uint8_t* data, size_t size, Avs1Bundle* bundle, c sample.senderProcessSubmitNs = processSubmitNs; sample.senderProcessDoneNs = processDoneNs; } - // A size sum alone cannot detect overlapping ranges with a gap elsewhere. - uint64_t expectedOffset = 0; - for (const auto& location : locations) - { - if (location.offset != expectedOffset) - return fail(error, "AVS1 payload ranges overlap or have gaps"); - expectedOffset += location.size; - } const size_t payloadStart = kAvs1LengthPrefixBytes + headerBytes + descriptorBytes; for (uint16_t streamIndex = 0; streamIndex < streamCount; ++streamIndex) { diff --git a/examples/camera_streamer/argus_sender/src/Avs1Wire.h b/examples/camera_streamer/argus_sender/src/Avs1Wire.h index fca710746c..57678bab4f 100644 --- a/examples/camera_streamer/argus_sender/src/Avs1Wire.h +++ b/examples/camera_streamer/argus_sender/src/Avs1Wire.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CMakeLists.txt b/examples/camera_streamer/argus_sender/src/CMakeLists.txt deleted file mode 100644 index a004b98abb..0000000000 --- a/examples/camera_streamer/argus_sender/src/CMakeLists.txt +++ /dev/null @@ -1,57 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender - main.cpp - Config.cpp - Avs1Wire.cpp - OutputGeometry.cpp - MixedRateScheduler.cpp - CapturePriorityPolicy.cpp - CaptureDomainPlan.cpp - CaptureManager.cpp - FrameBundleAssembler.cpp - StreamProcessor.cpp - CaptureCoordinator.cpp - SenderSession.cpp - Runtime.cpp - CameraCatalog.cpp - FrameCopyWorker.cpp - NvRemap.cpp - FrameAlignmentPolicy.cpp - StereoAlignmentPolicy.cpp - TcpTransport.cpp - HevcEncoder.cpp - StereoTiming.cpp - EGLGlobal.cpp - NvBufSurfaceLite.cpp - ${MMAPI_CLASS_SOURCES} -) - -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - target_compile_options(argus_sender PRIVATE -Wall -Wextra) - set_source_files_properties(${MMAPI_CLASS_SOURCES} - PROPERTIES COMPILE_OPTIONS -w) -endif() - -target_compile_definitions(argus_sender PRIVATE NVMMAPI_SUPPORTED) - -target_include_directories(argus_sender PRIVATE - ${ARGUS_INCLUDE_DIR} - ${MMAPI_INCLUDE_DIR} - ${MMAPI_CLASS_DIR} - ${CUDA_INCLUDE_DIR} - ${DRM_INCLUDE_DIR} -) - -target_link_libraries(argus_sender PRIVATE - ${ARGUS_LINK_LIBRARY} - ${EGL_LIBRARY} - ${NVV4L2_LIBRARY} - ${NVBUFSURFACE_LIBRARY} - ${CUDA_DRIVER_LIBRARY} - yaml-cpp::yaml-cpp - Threads::Threads -) - -set_target_properties(argus_sender PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") -install(TARGETS argus_sender DESTINATION bin) diff --git a/examples/camera_streamer/argus_sender/src/CameraCatalog.cpp b/examples/camera_streamer/argus_sender/src/CameraCatalog.cpp index d8538a179b..9912abf877 100644 --- a/examples/camera_streamer/argus_sender/src/CameraCatalog.cpp +++ b/examples/camera_streamer/argus_sender/src/CameraCatalog.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CameraCatalog.h b/examples/camera_streamer/argus_sender/src/CameraCatalog.h index 633800e502..df8eec773b 100644 --- a/examples/camera_streamer/argus_sender/src/CameraCatalog.h +++ b/examples/camera_streamer/argus_sender/src/CameraCatalog.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CaptureCoordinator.cpp b/examples/camera_streamer/argus_sender/src/CaptureCoordinator.cpp index 2d87d7e98a..8961daa08b 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureCoordinator.cpp +++ b/examples/camera_streamer/argus_sender/src/CaptureCoordinator.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CaptureCoordinator.h b/examples/camera_streamer/argus_sender/src/CaptureCoordinator.h index bbc3b291c5..8dcc30a951 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureCoordinator.h +++ b/examples/camera_streamer/argus_sender/src/CaptureCoordinator.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.cpp b/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.cpp index 863beb982a..5ac40d2748 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.cpp +++ b/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.h b/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.h index e948c19b17..641a807276 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.h +++ b/examples/camera_streamer/argus_sender/src/CaptureDomainPlan.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CaptureManager.cpp b/examples/camera_streamer/argus_sender/src/CaptureManager.cpp index 54754161b9..10644fe2dc 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureManager.cpp +++ b/examples/camera_streamer/argus_sender/src/CaptureManager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -193,6 +193,7 @@ class CaptureManager::Impl struct CaptureSessionState { + size_t index = 0; std::vector cameraIndices; Argus::UniqueObj session; Argus::ICaptureSession* iSession = NULL; @@ -415,6 +416,7 @@ class CaptureManager::Impl for (size_t sessionIndex = 0; sessionIndex < sessionCameraGroups.size(); ++sessionIndex) { std::unique_ptr captureSession(new CaptureSessionState()); + captureSession->index = sessionIndex; captureSession->cameraIndices = sessionCameraGroups[sessionIndex]; std::vector devices; for (size_t member = 0; member < captureSession->cameraIndices.size(); ++member) @@ -1405,9 +1407,10 @@ class CaptureManager::Impl for (size_t sessionIndex = 0; sessionIndex < domain.sessions.size(); ++sessionIndex) { CaptureSessionState& session = *domain.sessions[sessionIndex]; - if (session.iSession->repeat(session.request.get()) != Argus::STATUS_OK) + const Argus::Status status = session.iSession->repeat(session.request.get()); + if (status != Argus::STATUS_OK) { - return failDomain(&domain, "failed to start repeating capture"); + return failSession(&domain, &session, "failed to start repeating capture", status); } session.repeating = true; } @@ -1421,14 +1424,59 @@ class CaptureManager::Impl } bool failDomain(DomainState* domain, const char* message, Argus::Status status = Argus::STATUS_OK) + { + return failCapture(domain, std::string(), message, status); + } + + std::string describeSession(const CaptureSessionState* session) const + { + std::ostringstream description; + description << "session " << session->index << " cameras=["; + for (size_t member = 0; member < session->cameraIndices.size(); ++member) + { + if (member != 0) + description << ","; + const CameraState& camera = *m_cameras[session->cameraIndices[member]]; + description << camera.label << "(device=" << camera.config.device << ")"; + } + description << "]"; + return description.str(); + } + + std::string describeCamera(const CameraState* camera) const + { + std::ostringstream description; + description << "camera " << camera->label << "(device=" << camera->config.device << ")"; + return description.str(); + } + + bool failSession(DomainState* domain, + const CaptureSessionState* session, + const char* message, + Argus::Status status = Argus::STATUS_OK) + { + return failCapture(domain, describeSession(session), message, status); + } + + bool failCamera(DomainState* domain, + const CameraState* camera, + const char* message, + Argus::Status status = Argus::STATUS_OK) + { + return failCapture(domain, describeCamera(camera), message, status); + } + + bool failCapture(DomainState* domain, const std::string& scope, const char* message, Argus::Status status) { if (status == Argus::STATUS_OK) { - std::fprintf(stderr, "capture domain %zu: %s\n", domain->index, message); + std::fprintf(stderr, "capture domain %zu%s%s: %s\n", domain->index, scope.empty() ? "" : " ", scope.c_str(), + message); } else { - std::fprintf(stderr, "capture domain %zu: %s (status %d)\n", domain->index, message, status); + std::fprintf(stderr, "capture domain %zu%s%s: %s (status %d)\n", domain->index, scope.empty() ? "" : " ", + scope.c_str(), message, status); } { std::lock_guard lock(domain->mutex); @@ -1459,7 +1507,7 @@ class CaptureManager::Impl { if (expectedStop && expectedStop->load()) return false; - return failDomain(domain, "failed waiting for capture event", status); + return failSession(domain, session, "failed waiting for capture event", status); } uint64_t completeCount = 0; const Argus::Event* event = NULL; @@ -1483,13 +1531,19 @@ class CaptureManager::Impl const bool correctable = eventName && std::strstr(eventName, "CORR") && !std::strstr(eventName, "UNCORR"); if (correctable) { + const std::string sessionDescription = describeSession(session); std::fprintf( stderr, - "Warning: capture domain %zu received correctable Argus event %s (status %d); continuing\n", - domain->index, eventName, iError->getStatus()); + "Warning: capture domain %zu %s received correctable Argus event %s capture_id=%u (status %d); continuing\n", + domain->index, sessionDescription.c_str(), eventName, iEvent->getCaptureId(), + iError->getStatus()); continue; } - return failDomain(domain, "received Argus capture error", iError->getStatus()); + std::ostringstream message; + message << "received Argus capture error event=" << (eventName ? eventName : "unknown") + << " capture_id=" << iEvent->getCaptureId(); + const std::string eventMessage = message.str(); + return failSession(domain, session, eventMessage.c_str(), iError->getStatus()); } if (completeCount > 0) { @@ -1554,7 +1608,7 @@ class CaptureManager::Impl if (!dmaBuffer || !dmaBuffer->markArgusAcquired(argusBuffer)) { camera.iBufferStream->releaseBuffer(argusBuffer); - failDomain(domain, "failed to resolve acquired direct DMABUF"); + failCamera(domain, &camera, "failed to resolve acquired direct DMABUF"); return std::unique_ptr(); } sample->dmaBuffer.reset(dmaBuffer); @@ -1574,7 +1628,7 @@ class CaptureManager::Impl std::fprintf(stderr, "Priority camera %s produced no frame for %.1f s (%s)\n", camera.label.c_str(), static_cast(limitNs) / 1e9, camera.hasLastFrameNumber ? "steady-state drought" : "startup timeout"); - failDomain(domain, "priority camera frame drought"); + failCamera(domain, &camera, "priority camera frame drought"); return std::unique_ptr(); } continue; @@ -1587,8 +1641,8 @@ class CaptureManager::Impl { return std::unique_ptr(); } - failDomain(domain, camera.iBufferStream ? "failed to acquire direct buffer" : "failed to acquire EGL frame", - status); + failCamera(domain, &camera, + camera.iBufferStream ? "failed to acquire direct buffer" : "failed to acquire EGL frame", status); return std::unique_ptr(); } const uint64_t acquiredNs = steadyNowNs(); @@ -1598,7 +1652,7 @@ class CaptureManager::Impl readFrameTiming(sample->frame.get(), camera.label.c_str(), &sample->timing); if (!timingRead) { - failDomain(domain, "failed to read frame metadata"); + failCamera(domain, &camera, "failed to read frame metadata"); return std::unique_ptr(); } sample->timing.acquiredSteadyNs = acquiredNs; @@ -1633,7 +1687,7 @@ class CaptureManager::Impl stderr, "Priority camera %s produced no new frame for %.1f s (repeated frame number %llu)\n", m_cameras[flatIndex]->label.c_str(), static_cast(progress.droughtLimitNs()) / 1e9, static_cast(frameNumber)); - failDomain(domain, "priority camera new-frame drought"); + failCamera(domain, m_cameras[flatIndex].get(), "priority camera new-frame drought"); break; } std::this_thread::sleep_for(std::chrono::microseconds(100)); diff --git a/examples/camera_streamer/argus_sender/src/CaptureManager.h b/examples/camera_streamer/argus_sender/src/CaptureManager.h index 487089b22f..0beb231f71 100644 --- a/examples/camera_streamer/argus_sender/src/CaptureManager.h +++ b/examples/camera_streamer/argus_sender/src/CaptureManager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.cpp b/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.cpp index d2d9dfa168..c159be80a3 100644 --- a/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.cpp +++ b/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.h b/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.h index c4486a01b7..a6102da2b1 100644 --- a/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.h +++ b/examples/camera_streamer/argus_sender/src/CapturePriorityPolicy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/Config.cpp b/examples/camera_streamer/argus_sender/src/Config.cpp index 9dfe7cdc04..2c6e4c6de7 100644 --- a/examples/camera_streamer/argus_sender/src/Config.cpp +++ b/examples/camera_streamer/argus_sender/src/Config.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -183,11 +183,9 @@ bool parseStream(const YAML::Node& node, size_t index, StreamConfig* stream, std std::ostringstream pathBuilder; pathBuilder << "streams[" << index << "]"; const std::string path = pathBuilder.str(); - const std::set allowed = { "id", "type", "cameras", "capture", "output", "receiver" }; + const std::set allowed = { "id", "type", "cameras", "capture", "output" }; if (!rejectUnknown(node, allowed, path, error)) return false; - if (node["receiver"] && !node["receiver"].IsMap()) - return fail(error, path + ".receiver must be a mapping"); if (!node["id"] || !node["type"] || !node["cameras"] || !node["output"]) { return fail(error, path + " requires id, type, cameras, and output"); @@ -304,8 +302,7 @@ bool loadConfigFile(const std::string& path, AppConfig* config, std::string* err try { const YAML::Node root = YAML::LoadFile(path); - const std::set rootAllowed = { "version", "network", "runtime", "alignment", - "streams", "receiver", "ipc" }; + const std::set rootAllowed = { "version", "network", "runtime", "alignment", "streams" }; if (!rejectUnknown(root, rootAllowed, "config", error)) return false; if (!readUint32(root, "version", "config", &config->version, error)) diff --git a/examples/camera_streamer/argus_sender/src/Config.h b/examples/camera_streamer/argus_sender/src/Config.h index fe001253f8..d574ddcf7a 100644 --- a/examples/camera_streamer/argus_sender/src/Config.h +++ b/examples/camera_streamer/argus_sender/src/Config.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp b/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp index 569768b0e8..438982f4c6 100644 --- a/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp +++ b/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp @@ -1,34 +1,5 @@ -/* - * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NVIDIA CORPORATION nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 /* * Minimal headless EGL helper for argus_sender, derived from NVIDIA's diff --git a/examples/camera_streamer/argus_sender/src/EGLGlobal.h b/examples/camera_streamer/argus_sender/src/EGLGlobal.h index fdab944f18..47690e89f6 100644 --- a/examples/camera_streamer/argus_sender/src/EGLGlobal.h +++ b/examples/camera_streamer/argus_sender/src/EGLGlobal.h @@ -1,9 +1,7 @@ /* * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. + * + * SPDX-FileCopyrightText: Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions diff --git a/examples/camera_streamer/argus_sender/src/EncodedFrameSink.h b/examples/camera_streamer/argus_sender/src/EncodedFrameSink.h index 3a413b2f4c..a8df29ce7f 100644 --- a/examples/camera_streamer/argus_sender/src/EncodedFrameSink.h +++ b/examples/camera_streamer/argus_sender/src/EncodedFrameSink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/Error.h b/examples/camera_streamer/argus_sender/src/Error.h index e7c866c1ab..b94199b2e5 100644 --- a/examples/camera_streamer/argus_sender/src/Error.h +++ b/examples/camera_streamer/argus_sender/src/Error.h @@ -1,9 +1,7 @@ /* * SPDX-License-Identifier: BSD-3-Clause - */ - -/* - * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. + * + * SPDX-FileCopyrightText: Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions diff --git a/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.cpp b/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.cpp index 92da9dee90..122132e5a8 100644 --- a/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.cpp +++ b/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.h b/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.h index ccb4f61133..7fddce0a81 100644 --- a/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.h +++ b/examples/camera_streamer/argus_sender/src/FrameAlignmentPolicy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp index ea3555c19e..d6d89b60f8 100644 --- a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp +++ b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -21,10 +21,6 @@ namespace argus_sender { -#ifdef ARGUS_SENDER_TEST_SUBMISSION_HOOK -void observeSubmissionLock(std::mutex& mutex, uint64_t bundleSequence); -#endif - namespace { @@ -216,7 +212,6 @@ class FrameBundleAssembler::Impl } std::vector ready; - std::lock_guard sendLock(m_sendMutex); { std::lock_guard lock(m_mutex); if (m_pending.count(bundleSequence) != 0) @@ -333,10 +328,6 @@ class FrameBundleAssembler::Impl return setError("empty AVS1 sample payload"); const uint64_t payloadDoneSteadyNs = steadyNowNs(); std::vector ready; -#ifdef ARGUS_SENDER_TEST_SUBMISSION_HOOK - observeSubmissionLock(m_sendMutex, bundleSequence); -#endif - std::lock_guard sendLock(m_sendMutex); { std::lock_guard lock(m_mutex); std::map::iterator it = m_pending.find(bundleSequence); @@ -453,8 +444,6 @@ class FrameBundleAssembler::Impl uint64_t m_sessionId; std::vector m_streamTemplates; std::vector>> m_sinks; - // Hold across ordered collection and enqueue. Lock order: send, then state. - std::mutex m_sendMutex; mutable std::mutex m_mutex; std::map m_pending; uint64_t m_nextSendSequence; diff --git a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.h b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.h index 8800f4555c..fad11421a2 100644 --- a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.h +++ b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/FrameCopyWorker.cpp b/examples/camera_streamer/argus_sender/src/FrameCopyWorker.cpp index 07b0510877..777b060a19 100644 --- a/examples/camera_streamer/argus_sender/src/FrameCopyWorker.cpp +++ b/examples/camera_streamer/argus_sender/src/FrameCopyWorker.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/FrameCopyWorker.h b/examples/camera_streamer/argus_sender/src/FrameCopyWorker.h index 4dd1dfb6cc..a82e7901bf 100644 --- a/examples/camera_streamer/argus_sender/src/FrameCopyWorker.h +++ b/examples/camera_streamer/argus_sender/src/FrameCopyWorker.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/HevcEncoder.cpp b/examples/camera_streamer/argus_sender/src/HevcEncoder.cpp index d2bce4f9a3..3d995eb058 100644 --- a/examples/camera_streamer/argus_sender/src/HevcEncoder.cpp +++ b/examples/camera_streamer/argus_sender/src/HevcEncoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/HevcEncoder.h b/examples/camera_streamer/argus_sender/src/HevcEncoder.h index 25bed4e446..cb827c3d24 100644 --- a/examples/camera_streamer/argus_sender/src/HevcEncoder.h +++ b/examples/camera_streamer/argus_sender/src/HevcEncoder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/MixedRateScheduler.cpp b/examples/camera_streamer/argus_sender/src/MixedRateScheduler.cpp index b8965feb02..afcb416223 100644 --- a/examples/camera_streamer/argus_sender/src/MixedRateScheduler.cpp +++ b/examples/camera_streamer/argus_sender/src/MixedRateScheduler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/MixedRateScheduler.h b/examples/camera_streamer/argus_sender/src/MixedRateScheduler.h index b67b005540..1c2240dc56 100644 --- a/examples/camera_streamer/argus_sender/src/MixedRateScheduler.h +++ b/examples/camera_streamer/argus_sender/src/MixedRateScheduler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/NvBufSurfaceLite.cpp b/examples/camera_streamer/argus_sender/src/NvBufSurfaceLite.cpp index 90b1a21192..eb9cd896f7 100644 --- a/examples/camera_streamer/argus_sender/src/NvBufSurfaceLite.cpp +++ b/examples/camera_streamer/argus_sender/src/NvBufSurfaceLite.cpp @@ -1,7 +1,5 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 /* * Minimal NvBufSurf wrapper for argus_sender. diff --git a/examples/camera_streamer/argus_sender/src/NvRemap.cpp b/examples/camera_streamer/argus_sender/src/NvRemap.cpp index 9b0d953bbf..d446ffbb6b 100644 --- a/examples/camera_streamer/argus_sender/src/NvRemap.cpp +++ b/examples/camera_streamer/argus_sender/src/NvRemap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/NvRemap.h b/examples/camera_streamer/argus_sender/src/NvRemap.h index cb978ef12f..04793156c2 100644 --- a/examples/camera_streamer/argus_sender/src/NvRemap.h +++ b/examples/camera_streamer/argus_sender/src/NvRemap.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -18,7 +18,7 @@ namespace argus_sender // Precomputed NV12 lookup-table remap. // // Loads the RMAP binaries emitted by -// robotics/hardware_sdk/camera/calib/gen_remap_maps.py (little-endian +// remaps/generate_example.py (little-endian // header struct(<4sIIf) = [magic "RMAP", dest width u32, dest height u32, // balance f32], then map_x[H*W] and map_y[H*W] float32 row-major: the SOURCE // pixel each destination pixel samples, cv2.remap CV_32FC1 semantics). diff --git a/examples/camera_streamer/argus_sender/src/OutputGeometry.cpp b/examples/camera_streamer/argus_sender/src/OutputGeometry.cpp index 94d449989b..c1101d8243 100644 --- a/examples/camera_streamer/argus_sender/src/OutputGeometry.cpp +++ b/examples/camera_streamer/argus_sender/src/OutputGeometry.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -111,15 +111,11 @@ bool resolveOutputGeometry(const OutputConfig& output, resolved->captureHeight = output.remap.enabled() ? output.remap.captureHeight : height; const double targetAspect = static_cast(resolved->captureWidth) / resolved->captureHeight; - // An inferred dimension preserves the reference aspect before NV12 even-pixel rounding. - // Remap capture dimensions remain an explicit aspect contract. - const bool inferredDimension = (output.width == 0) != (output.height == 0); - const double strictAspect = inferredDimension && !output.remap.enabled() ? referenceAspect : targetAspect; resolved->clips.reserve(sources.size()); for (size_t i = 0; i < sources.size(); ++i) { const double sourceAspect = static_cast(sources[i].width) / sources[i].height; - if (output.resizeMode == RESIZE_STRICT && !aspectMatches(sourceAspect, strictAspect)) + if (output.resizeMode == RESIZE_STRICT && !aspectMatches(sourceAspect, targetAspect)) { std::ostringstream message; message << "strict resize rejects source " << sources[i].width << "x" << sources[i].height << " -> " diff --git a/examples/camera_streamer/argus_sender/src/OutputGeometry.h b/examples/camera_streamer/argus_sender/src/OutputGeometry.h index 4f4b522541..1cb22145c1 100644 --- a/examples/camera_streamer/argus_sender/src/OutputGeometry.h +++ b/examples/camera_streamer/argus_sender/src/OutputGeometry.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/PacketSink.h b/examples/camera_streamer/argus_sender/src/PacketSink.h index d6ea6c0abc..3dc51f9afe 100644 --- a/examples/camera_streamer/argus_sender/src/PacketSink.h +++ b/examples/camera_streamer/argus_sender/src/PacketSink.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/PriorityFrameHandoff.h b/examples/camera_streamer/argus_sender/src/PriorityFrameHandoff.h index c28b91c7fc..ca8d6023dc 100644 --- a/examples/camera_streamer/argus_sender/src/PriorityFrameHandoff.h +++ b/examples/camera_streamer/argus_sender/src/PriorityFrameHandoff.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/Runtime.cpp b/examples/camera_streamer/argus_sender/src/Runtime.cpp index 716e87d03b..b605714b24 100644 --- a/examples/camera_streamer/argus_sender/src/Runtime.cpp +++ b/examples/camera_streamer/argus_sender/src/Runtime.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/Runtime.h b/examples/camera_streamer/argus_sender/src/Runtime.h index 36aef73130..a12ceb9014 100644 --- a/examples/camera_streamer/argus_sender/src/Runtime.h +++ b/examples/camera_streamer/argus_sender/src/Runtime.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/SenderSession.cpp b/examples/camera_streamer/argus_sender/src/SenderSession.cpp index 57078b245b..df459fe79d 100644 --- a/examples/camera_streamer/argus_sender/src/SenderSession.cpp +++ b/examples/camera_streamer/argus_sender/src/SenderSession.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/SenderSession.h b/examples/camera_streamer/argus_sender/src/SenderSession.h index cab3debbaa..0893161ab2 100644 --- a/examples/camera_streamer/argus_sender/src/SenderSession.h +++ b/examples/camera_streamer/argus_sender/src/SenderSession.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.cpp b/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.cpp index be8c9b5e27..0f59d41f1c 100644 --- a/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.cpp +++ b/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.h b/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.h index d446904273..08fe6bc3c2 100644 --- a/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.h +++ b/examples/camera_streamer/argus_sender/src/StereoAlignmentPolicy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StereoTiming.cpp b/examples/camera_streamer/argus_sender/src/StereoTiming.cpp index 4ef97bdf44..4769406be5 100644 --- a/examples/camera_streamer/argus_sender/src/StereoTiming.cpp +++ b/examples/camera_streamer/argus_sender/src/StereoTiming.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StereoTiming.h b/examples/camera_streamer/argus_sender/src/StereoTiming.h index 894d1b9a47..f6a8f4a60b 100644 --- a/examples/camera_streamer/argus_sender/src/StereoTiming.h +++ b/examples/camera_streamer/argus_sender/src/StereoTiming.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StreamProcessor.cpp b/examples/camera_streamer/argus_sender/src/StreamProcessor.cpp index 3dc4b925c7..5c059f0054 100644 --- a/examples/camera_streamer/argus_sender/src/StreamProcessor.cpp +++ b/examples/camera_streamer/argus_sender/src/StreamProcessor.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/StreamProcessor.h b/examples/camera_streamer/argus_sender/src/StreamProcessor.h index 2f9195de57..c02e4dd7af 100644 --- a/examples/camera_streamer/argus_sender/src/StreamProcessor.h +++ b/examples/camera_streamer/argus_sender/src/StreamProcessor.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/TcpTransport.cpp b/examples/camera_streamer/argus_sender/src/TcpTransport.cpp index 527eb35820..6377b85e53 100644 --- a/examples/camera_streamer/argus_sender/src/TcpTransport.cpp +++ b/examples/camera_streamer/argus_sender/src/TcpTransport.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/TcpTransport.h b/examples/camera_streamer/argus_sender/src/TcpTransport.h index d70d7a5b10..1da1d69d49 100644 --- a/examples/camera_streamer/argus_sender/src/TcpTransport.h +++ b/examples/camera_streamer/argus_sender/src/TcpTransport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/argus_sender/src/main.cpp b/examples/camera_streamer/argus_sender/src/main.cpp index 06a18b231d..4b315528bd 100644 --- a/examples/camera_streamer/argus_sender/src/main.cpp +++ b/examples/camera_streamer/argus_sender/src/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/TestHarness.h b/examples/camera_streamer/argus_sender/tests/TestHarness.h similarity index 94% rename from examples/camera_streamer/tests/cpp/TestHarness.h rename to examples/camera_streamer/argus_sender/tests/TestHarness.h index c51c30c736..58787ea33d 100644 --- a/examples/camera_streamer/tests/cpp/TestHarness.h +++ b/examples/camera_streamer/argus_sender/tests/TestHarness.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/hardware/avs1_smoke.py b/examples/camera_streamer/argus_sender/tests/hardware/avs1_smoke.py similarity index 85% rename from examples/camera_streamer/tests/hardware/avs1_smoke.py rename to examples/camera_streamer/argus_sender/tests/hardware/avs1_smoke.py index 9af9c8acc0..0463ac098c 100755 --- a/examples/camera_streamer/tests/hardware/avs1_smoke.py +++ b/examples/camera_streamer/argus_sender/tests/hardware/avs1_smoke.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 - """Bounded AVS1 stereo hardware smoke test.""" from __future__ import annotations @@ -12,7 +11,6 @@ import subprocess import sys import time -from pathlib import Path HEADER = struct.Struct("!4sHHIHHQQQQIIQ") @@ -103,43 +101,13 @@ def connect_with_retry( raise TimeoutError("timed out connecting to argus_sender") -def configured_port(config_path: str) -> int: - import yaml - - config = yaml.safe_load(Path(config_path).read_text()) - port = config.get("network", {}).get("port", 5000) - if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535: - raise ValueError("network.port must be an integer in 1..65535") - return port - - -def validate_frame_limit(config_path: str, frames: int) -> None: - import yaml - - config = yaml.safe_load(Path(config_path).read_text()) - limit = config.get("runtime", {}).get("frames", 0) - # The sender must exit after the last bundle rather than wait for a reconnect. - if ( - isinstance(limit, bool) - or not isinstance(limit, int) - or limit <= 0 - or frames <= 0 - or limit != frames - ): - raise ValueError("runtime.frames must be positive and equal to --frames") - - def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--sender", required=True) parser.add_argument("--config", required=True) parser.add_argument("--frames", type=int, default=120) + parser.add_argument("--port", type=int, default=55000) args = parser.parse_args() - try: - port = configured_port(args.config) - validate_frame_limit(args.config, args.frames) - except ValueError as exc: - parser.error(str(exc)) process = subprocess.Popen( [args.sender, "--config", args.config], @@ -149,7 +117,7 @@ def main() -> int: ) output = "" try: - sock = connect_with_retry(port, process, 15.0) + sock = connect_with_retry(args.port, process, 15.0) first_nals: list[list[int]] | None = None last_tsc: list[int | None] = [None, None] max_delta_us = 0.0 diff --git a/examples/camera_streamer/tests/cpp/test_alignment_policy.cpp b/examples/camera_streamer/argus_sender/tests/test_alignment_policy.cpp similarity index 96% rename from examples/camera_streamer/tests/cpp/test_alignment_policy.cpp rename to examples/camera_streamer/argus_sender/tests/test_alignment_policy.cpp index 8dd7c361cc..225032d197 100644 --- a/examples/camera_streamer/tests/cpp/test_alignment_policy.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_alignment_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_avs1_wire.cpp b/examples/camera_streamer/argus_sender/tests/test_avs1_wire.cpp similarity index 62% rename from examples/camera_streamer/tests/cpp/test_avs1_wire.cpp rename to examples/camera_streamer/argus_sender/tests/test_avs1_wire.cpp index 8216cd4926..6f633728e5 100644 --- a/examples/camera_streamer/tests/cpp/test_avs1_wire.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_avs1_wire.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -66,109 +66,11 @@ argus_sender::Avs1Bundle mixedBundle() return bundle; } -void testStreamValidation(TestContext& test) -{ - const char* error = nullptr; - argus_sender::Avs1Bundle parsed; - std::vector wire; - for (uint16_t rotation : { 0, 180 }) - { - auto bundle = mixedBundle(); - bundle.streams[0].rotation = rotation; - TEST_CHECK(test, argus_sender::buildAvs1WireRecord(bundle, &wire, &error)); - TEST_CHECK(test, argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - } - auto invalid = mixedBundle(); - invalid.streams[0].rotation = 90; - TEST_CHECK(test, !argus_sender::buildAvs1WireRecord(invalid, &wire, &error)); - for (size_t stream = 0; stream < 2; ++stream) - { - invalid = mixedBundle(); - invalid.streams[stream].sequence = stream == 0 ? 0 : 1; - TEST_CHECK(test, !argus_sender::buildAvs1WireRecord(invalid, &wire, &error)); - } - - std::vector valid; - TEST_CHECK(test, argus_sender::buildAvs1WireRecord(mixedBundle(), &valid, &error)); - const size_t firstStream = argus_sender::kAvs1LengthPrefixBytes + argus_sender::kAvs1HeaderBytes; - wire = valid; - wire[firstStream + 7] = 90; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - wire = valid; - wire[firstStream + 31] = 0; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - wire = valid; - wire[firstStream + argus_sender::kAvs1StreamDescriptorBytes + 31] = 1; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); -} - -void testBundleValidation(TestContext& test) -{ - const char* error = nullptr; - argus_sender::Avs1Bundle parsed; - auto bundle = mixedBundle(); - bundle.flags = argus_sender::AVS1_BUNDLE_SENDER_CLOCK_VALID; - bundle.senderCaptureReadyTscNs = 0; - std::vector valid; - TEST_CHECK(test, argus_sender::buildAvs1WireRecord(bundle, &valid, &error)); - // Mutate the on-wire fields independently of the builder's checks. - for (uint8_t flags : { 0x80, 0x02 }) - { - auto wire = valid; - wire[4 + 7] = flags; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - } - for (size_t field : { 16, 24 }) - { - auto wire = valid; - for (size_t byte = 0; byte < 8; ++byte) - wire[4 + field + byte] = 0; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - } -} - -void testPayloadCoverage(TestContext& test) -{ - const char* error = nullptr; - argus_sender::Avs1Bundle parsed; - std::vector valid; - TEST_CHECK(test, argus_sender::buildAvs1WireRecord(mixedBundle(), &valid, &error)); - const size_t secondSample = argus_sender::kAvs1LengthPrefixBytes + argus_sender::kAvs1HeaderBytes + - 2 * argus_sender::kAvs1StreamDescriptorBytes + argus_sender::kAvs1SampleDescriptorBytes; - // Two nonempty ranges still sum to five bytes when the second overlaps the first. - for (uint8_t offset : { 0, 2, 4 }) - { - auto wire = valid; - wire[secondSample + 67] = offset; - TEST_CHECK(test, !argus_sender::parseAvs1WireRecord(wire.data(), wire.size(), &parsed, &error)); - } -} - } // namespace int main() { TestContext test("avs1_wire"); - testStreamValidation(test); - testBundleValidation(test); - testPayloadCoverage(test); - { - const char* descriptorError = nullptr; - std::vector validRecord; - argus_sender::Avs1Bundle result; - TEST_CHECK(test, argus_sender::buildAvs1WireRecord(mixedBundle(), &validRecord, &descriptorError)); - const size_t firstStream = argus_sender::kAvs1LengthPrefixBytes + argus_sender::kAvs1HeaderBytes; - // Oversized stereo count and overlapping mono range must fail at the descriptor, - // before allocating sample vectors or reading sample payloads. - for (size_t field : { firstStream + 35, firstStream + argus_sender::kAvs1StreamDescriptorBytes + 33 }) - { - auto malformed = validRecord; - malformed[field] = field == firstStream + 35 ? 3 : 0; - TEST_CHECK( - test, !argus_sender::parseAvs1WireRecord(malformed.data(), malformed.size(), &result, &descriptorError)); - TEST_CHECK_EQ(test, std::string(descriptorError), std::string("invalid AVS1 stream descriptor index")); - } - } argus_sender::Avs1Bundle input = mixedBundle(); std::vector wire; const char* error = 0; diff --git a/examples/camera_streamer/tests/cpp/test_bundle_assembler.cpp b/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp similarity index 72% rename from examples/camera_streamer/tests/cpp/test_bundle_assembler.cpp rename to examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp index db66886a3c..4060db28d2 100644 --- a/examples/camera_streamer/tests/cpp/test_bundle_assembler.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,24 +9,14 @@ #include "PacketSink.h" #include "TestHarness.h" -#include #include -#include #include -#include -#include #include #include namespace { -std::mutex submissionMutex; -std::condition_variable submissionChanged; -bool observeSecondSubmission = false; -bool secondEntered = false; -bool secondContended = false; - class FakePacketSink : public argus_sender::PacketSink { public: @@ -52,26 +42,6 @@ class FakePacketSink : public argus_sender::PacketSink std::vector timings; }; -class BlockingPacketSink : public FakePacketSink -{ -public: - bool enqueueFramedWithTiming(std::vector&& packet, const argus_sender::PacketTiming& timing) override - { - std::unique_lock lock(mutex); - if (!entered) - { - entered = true; - changed.notify_all(); - changed.wait(lock, [this] { return released; }); - } - return FakePacketSink::enqueueFramedWithTiming(std::move(packet), timing); - } - std::mutex mutex; - std::condition_variable changed; - bool entered = false; - bool released = false; -}; - std::vector templates() { std::vector result(2); @@ -108,24 +78,6 @@ argus_sender::FrameTiming timing(uint64_t frame, uint64_t tsc) } // namespace -namespace argus_sender -{ - -void observeSubmissionLock(std::mutex& mutex, uint64_t bundleSequence) -{ - std::lock_guard lock(submissionMutex); - if (!observeSecondSubmission || bundleSequence != 2) - return; - // Observe contention inside submitOwned, while the first enqueue is still blocked. - secondContended = !mutex.try_lock(); - if (!secondContended) - mutex.unlock(); - secondEntered = true; - submissionChanged.notify_all(); -} - -} // namespace argus_sender - int main() { TestContext test("bundle_assembler"); @@ -229,50 +181,5 @@ int main() TEST_CHECK(test, !(parsed.flags & argus_sender::AVS1_BUNDLE_SENDER_CLOCK_VALID)); TEST_CHECK(test, !(parsed.flags & argus_sender::AVS1_BUNDLE_SENDER_TSC_VALID)); TEST_CHECK_EQ(test, parsed.senderCaptureReadyTscNs, 0ULL); - // Hold the first enqueue while another encoder callback completes the next bundle. - BlockingPacketSink blockedSink; - argus_sender::FrameBundleAssembler concurrent(&blockedSink, 88, templates()); - TEST_CHECK(test, concurrent.beginBundle(1, bundle3, 6000, 6100, argus_sender::SENDER_CLOCK_UNKNOWN, 0)); - TEST_CHECK(test, concurrent.beginBundle(2, bundle3, 6000, 6100, argus_sender::SENDER_CLOCK_UNKNOWN, 0)); - std::atomic firstOk(false), secondOk(false); - observeSecondSubmission = true; - std::thread first( - [&] - { - firstOk = - concurrent.submitRaw(1, 0, 1, std::vector(12, 0x80), 6200, argus_sender::SENDER_CLOCK_REALTIME); - }); - { - std::unique_lock lock(blockedSink.mutex); - TEST_CHECK( - test, blockedSink.changed.wait_for(lock, std::chrono::seconds(2), [&] { return blockedSink.entered; })); - } - std::thread second( - [&] - { - secondOk = - concurrent.submitRaw(1, 0, 2, std::vector(12, 0x80), 6200, argus_sender::SENDER_CLOCK_REALTIME); - }); - { - std::unique_lock lock(submissionMutex); - TEST_CHECK(test, submissionChanged.wait_for(lock, std::chrono::seconds(2), [] { return secondEntered; })); - TEST_CHECK(test, secondContended); - } - TEST_CHECK(test, !secondOk); - { - std::lock_guard lock(blockedSink.mutex); - blockedSink.released = true; - } - blockedSink.changed.notify_all(); - first.join(); - second.join(); - TEST_CHECK(test, firstOk && secondOk); - TEST_CHECK_EQ(test, blockedSink.packets.size(), static_cast(2)); - for (size_t i = 0; i < blockedSink.packets.size(); ++i) - { - TEST_CHECK(test, argus_sender::parseAvs1WireRecord( - blockedSink.packets[i].data(), blockedSink.packets[i].size(), &parsed, &error)); - TEST_CHECK_EQ(test, parsed.sequence, i + 1); - } return test.finish(); } diff --git a/examples/camera_streamer/tests/cpp/test_capture_domain_plan.cpp b/examples/camera_streamer/argus_sender/tests/test_capture_domain_plan.cpp similarity index 96% rename from examples/camera_streamer/tests/cpp/test_capture_domain_plan.cpp rename to examples/camera_streamer/argus_sender/tests/test_capture_domain_plan.cpp index d4ca7da472..3fd6588e70 100644 --- a/examples/camera_streamer/tests/cpp/test_capture_domain_plan.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_capture_domain_plan.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_capture_priority_policy.cpp b/examples/camera_streamer/argus_sender/tests/test_capture_priority_policy.cpp similarity index 92% rename from examples/camera_streamer/tests/cpp/test_capture_priority_policy.cpp rename to examples/camera_streamer/argus_sender/tests/test_capture_priority_policy.cpp index e5f004e7d6..6fd0cdf043 100644 --- a/examples/camera_streamer/tests/cpp/test_capture_priority_policy.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_capture_priority_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_config.cpp b/examples/camera_streamer/argus_sender/tests/test_config.cpp similarity index 90% rename from examples/camera_streamer/tests/cpp/test_config.cpp rename to examples/camera_streamer/argus_sender/tests/test_config.cpp index b178461ba4..97c3c47d9e 100644 --- a/examples/camera_streamer/tests/cpp/test_config.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_config.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -90,36 +90,17 @@ int main() { char program[] = "argus_sender"; char configOption[] = "--config"; - char configPath[] = "configs/camera.yaml"; + char configPath[] = "configs/cameras.yaml"; char verboseOption[] = "--verbose"; char checkOption[] = "--check-config"; char* arguments[] = { program, configOption, configPath, verboseOption, checkOption }; argus_sender::CommandLine commandLine; TEST_CHECK(test, argus_sender::parseCommandLine(5, arguments, &commandLine)); - TEST_CHECK_EQ(test, commandLine.configPath, std::string("configs/camera.yaml")); + TEST_CHECK_EQ(test, commandLine.configPath, std::string("configs/cameras.yaml")); TEST_CHECK(test, commandLine.verbose); TEST_CHECK(test, commandLine.checkConfig); } - for (const char* profile : { "s56-stereo-2560x1984-30.yaml", "s56-stereo-1080p60.yaml", - "s56-mono-2560x1984-30.yaml", "s56-mono-1080p30.yaml", "s56-mono-1080p60.yaml", - "s56-mono-720p30.yaml", "s56-mono-720p60.yaml", "s56-mono-2400x1440-30.yaml" }) - { - argus_sender::AppConfig shared; - TEST_CHECK(test, argus_sender::loadConfigFile( - std::string(CAMERA_STREAMER_CONFIG_DIR) + "/" + profile, &shared, &error)); - TEST_CHECK_EQ(test, shared.network.port, 5000U); - TEST_CHECK_EQ(test, shared.streams.size(), static_cast(1)); - TEST_CHECK_EQ(test, shared.streams[0].id, std::string("head")); - } - - for (const char* profile : { "shw5g-mono-2560x1984-30.yaml", "shw5g-mono-2400x1440-60.yaml" }) - { - argus_sender::AppConfig unresolved; - TEST_CHECK(test, !argus_sender::loadConfigFile( - std::string(CAMERA_STREAMER_CONFIG_DIR) + "/" + profile, &unresolved, &error)); - } - argus_sender::AppConfig invalid; TEST_CHECK(test, !loadText("version: 1\nstreams: []\nunknown: true\n", &invalid, &error)); TEST_CHECK(test, error.find("unknown key") != std::string::npos); diff --git a/examples/camera_streamer/argus_sender/tests/test_deployment.py b/examples/camera_streamer/argus_sender/tests/test_deployment.py new file mode 100644 index 0000000000..5f5a4d470c --- /dev/null +++ b/examples/camera_streamer/argus_sender/tests/test_deployment.py @@ -0,0 +1,329 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import json +import os +from pathlib import Path +import subprocess + +import pytest + +DEPLOY = Path(__file__).resolve().parents[1] / "deploy" +OWNED_UNITS = ( + "camera-argus-sender", + "camera-time-sync", + "camera-vi-error-trace", +) +OWNED_FILES = ( + "etc/systemd/system/camera-argus-sender.service", + "etc/systemd/system/camera-time-sync.service", + "etc/systemd/system/camera-vi-error-trace.service", + "etc/chrony/conf.d/camera-agx.conf", + "etc/systemd/journald.conf.d/10-camera-persistent-journal.conf", + "usr/local/lib/camera/lock_jetson_camera_clocks.sh", +) + + +def executable(path, contents): + path.write_text(contents) + path.chmod(0o755) + + +@pytest.fixture +def board(tmp_path): + install = tmp_path / "board/camera_streamer" + system = tmp_path / "system" + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + state_path = tmp_path / "services.json" + log_path = tmp_path / "systemctl.log" + state = { + unit: {"load": "not-found", "enabled": "not-found", "active": "inactive"} + for unit in OWNED_UNITS + } + for unit in ("nvargus-daemon", "chrony"): + state[unit] = {"load": "loaded", "enabled": "enabled", "active": "active"} + state_path.write_text(json.dumps(state)) + executable( + bin_dir / "systemctl", + """#!/usr/bin/env python3 +import json, os, pathlib, sys +path = pathlib.Path(os.environ['FAKE_SERVICES']) +state = json.loads(path.read_text()) +args = sys.argv[1:] +with open(os.environ['FAKE_SYSTEMCTL_LOG'], 'a') as log: + log.write(json.dumps(args) + '\\n') +units = [arg.removesuffix('.service') for arg in args if arg.endswith('.service')] +if args[0] == 'show': + unit = units[0] + value = state.get(unit, {'load': 'not-found', 'enabled': '', 'active': 'inactive'}) + prop = args[args.index('-p') + 1] + print(value[{'LoadState': 'load', 'UnitFileState': 'enabled', 'ActiveState': 'active'}[prop]]) +else: + for unit in units: + value = state.setdefault(unit, {'load': 'loaded', 'enabled': 'disabled', 'active': 'inactive'}) + if args[0] in ('start', 'restart', 'try-restart'): value['active'] = 'active' + if args[0] == 'stop': value['active'] = 'inactive' + if args[0] == 'enable': value['enabled'] = 'enabled' + if args[0] == 'disable': value['enabled'] = 'disabled' + path.write_text(json.dumps(state)) +""", + ) + env = { + **os.environ, + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "CAMERA_DEPLOY_SYSTEM_ROOT": str(system), + "FAKE_SERVICES": str(state_path), + "FAKE_SYSTEMCTL_LOG": str(log_path), + } + + def run(action, *args, check=True): + return subprocess.run( + ["bash", str(DEPLOY / "deployment_state.sh"), action, str(install), *args], + env=env, + capture_output=True, + text=True, + check=check, + ) + + return install, system, state_path, log_path, run, bin_dir + + +def write_owned_files(system, prefix): + for name in OWNED_FILES: + path = system / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(f"{prefix}: {name}\n") + + +def test_restore_first_install_removes_only_deployment_files(board): + install, system, state_path, log_path, run, _ = board + driver = system / "etc/camera-driver.json" + driver.parent.mkdir(parents=True) + driver.write_text("installed driver configuration") + run("snapshot") + write_owned_files(system, "new") + (install / "argus_sender").mkdir() + (install / "argus_sender/binary").write_text("new") + state = json.loads(state_path.read_text()) + state["nvargus-daemon"].update(enabled="disabled", active="inactive") + for unit in OWNED_UNITS: + state[unit].update(load="loaded", enabled="enabled", active="active") + state_path.write_text(json.dumps(state)) + + run("restore") + + assert not (install / "argus_sender").exists() + assert all(not (system / name).exists() for name in OWNED_FILES) + assert driver.read_text() == "installed driver configuration" + state = json.loads(state_path.read_text()) + assert state["nvargus-daemon"]["enabled"] == "enabled" + assert state["nvargus-daemon"]["active"] == "active" + assert all(state[unit]["active"] == "inactive" for unit in OWNED_UNITS) + assert "camstream" not in log_path.read_text() + assert "cam-init" not in log_path.read_text() + previous_log = log_path.read_text() + run("restore") + assert log_path.read_text() == previous_log + + +@pytest.mark.parametrize("previous_active", ["active", "inactive"]) +def test_restore_previous_sender_files_and_service_state(board, previous_active): + install, system, state_path, _, run, _ = board + for name in ( + "argus_sender/build/argus_sender", + "argus_sender/remaps/cam4.remap", + "argus_sender/tests/test.py", + ): + path = install / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("previous release") + independent_test = install / "tests/integration.py" + independent_test.parent.mkdir() + independent_test.write_text("independent integration test") + write_owned_files(system, "previous") + state = json.loads(state_path.read_text()) + state["nvargus-daemon"].update(enabled="disabled", active="inactive") + for unit in OWNED_UNITS: + state[unit].update(load="loaded", enabled="disabled", active=previous_active) + state_path.write_text(json.dumps(state)) + run("snapshot") + (install / "argus_sender/build/argus_sender").write_text("new binary") + (install / "argus_sender/remaps/cam4.remap").write_text("new calibration") + independent_test.write_text("independent work after deployment") + write_owned_files(system, "new") + state = json.loads(state_path.read_text()) + for unit in OWNED_UNITS: + state[unit].update(enabled="enabled", active="active") + state_path.write_text(json.dumps(state)) + + run("restore") + + assert ( + install / "argus_sender/build/argus_sender" + ).read_text() == "previous release" + assert ( + install / "argus_sender/remaps/cam4.remap" + ).read_text() == "previous release" + assert (install / "argus_sender/tests/test.py").read_text() == "previous release" + assert independent_test.read_text() == "independent work after deployment" + assert all( + (system / name).read_text() == f"previous: {name}\n" for name in OWNED_FILES + ) + state = json.loads(state_path.read_text()) + assert state["nvargus-daemon"]["active"] == "inactive" + assert all(state[unit]["active"] == previous_active for unit in OWNED_UNITS) + assert all(state[unit]["enabled"] == "disabled" for unit in OWNED_UNITS) + + +def test_activate_swaps_the_staged_release_and_can_restore_it(board): + install, _, _, _, run, _ = board + old_binary = install / "argus_sender/build/argus_sender" + old_binary.parent.mkdir(parents=True) + old_binary.write_text("previous binary") + stage = install.parent / ".camera-streamer-stage.TEST" + new_binary = stage / "argus_sender/build/argus_sender" + new_binary.parent.mkdir(parents=True) + executable(new_binary, "#!/usr/bin/env bash\nexit 0\n") + (stage / "argus_sender/tests").mkdir() + result = run("activate", str(stage), check=False) + assert result.returncode != 0 + assert old_binary.read_text() == "previous binary" + run("snapshot") + run("activate", str(stage)) + assert old_binary.read_text() == "#!/usr/bin/env bash\nexit 0\n" + assert not new_binary.exists() + run("restore") + assert old_binary.read_text() == "previous binary" + + +def test_restore_without_snapshot_does_not_touch_services(board): + _, _, _, log_path, run, _ = board + result = run("restore", check=False) + assert result.returncode != 0 + assert not log_path.exists() + + +def test_failed_snapshot_preserves_existing_restore_point(board): + install, _, _, _, run, bin_dir = board + (install / "argus_sender").mkdir(parents=True) + (install / "argus_sender/binary").write_text("original") + run("snapshot") + executable(bin_dir / "cp", "#!/usr/bin/env bash\nexit 42\n") + result = run("snapshot", check=False) + assert result.returncode == 42 + assert ( + install / ".deployment-backup/argus_sender/binary" + ).read_text() == "original" + assert (install / ".deployment-backup/complete").exists() + + +@pytest.fixture +def remote_deploy(tmp_path): + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + log_path = tmp_path / "ssh.log" + executable( + bin_dir / "ssh", + """#!/usr/bin/env python3 +import json, os, sys +cmd = sys.argv[-1] +payload = sys.stdin.read() +with open(os.environ['FAKE_SSH_LOG'], 'a') as log: + log.write(json.dumps({'command': cmd, 'input': payload}) + '\\n') +if cmd == 'bash -s' and "printf 'USER=" in payload: + print('USER=nvidia\\nHOME=/home/nvidia') +elif cmd.startswith('mktemp '): + print('/home/nvidia/.camera-streamer-stage.TEST') +elif cmd.startswith('sudo -n cat '): + sys.exit(1) +elif cmd.startswith('cmake ') and os.environ.get('FAIL_AT') == 'build': + sys.exit(7) +elif 'systemctl reset-failed camera-argus-sender.service &&' in cmd and os.environ.get('FAIL_AT') == 'activation': + sys.exit(17) +""", + ) + executable(bin_dir / "rsync", "#!/usr/bin/env bash\nexit 0\n") + + def run(*args, fail_at=""): + result = subprocess.run( + [ + "bash", + str(DEPLOY / "deploy_argus_sender.sh"), + "camera-board", + "--host-lan-ip", + "192.0.2.10", + *args, + ], + env={ + **os.environ, + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "FAKE_SSH_LOG": str(log_path), + "FAIL_AT": fail_at, + }, + check=False, + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + ) + calls = [json.loads(line) for line in log_path.read_text().splitlines()] + return result, calls + + return run + + +@pytest.mark.parametrize( + "fail_at,expected", [("", 0), ("build", 7), ("activation", 17)] +) +def test_deployment_stages_build_and_restores_after_activation_failure( + remote_deploy, fail_at, expected +): + result, calls = remote_deploy(fail_at=fail_at) + assert result.returncode == expected, result.stderr + commands = [call["command"] for call in calls] + actions = [ + command.split(" -- ", 1)[1].split()[0].strip("'") + for command in commands + if command.startswith("sudo -n bash -s -- ") + ] + if fail_at == "build": + assert actions == [] + elif fail_at == "activation": + assert actions == ["snapshot", "activate", "restore"] + else: + assert actions == ["snapshot", "activate"] + assert all( + "camstream" not in command and "cam-init" not in command for command in commands + ) + assert not any("rb_camera.sh" in call["input"] for call in calls) + + +@pytest.mark.parametrize("flag", ["--rollback", "--restore"]) +def test_explicit_restore_does_not_require_build_or_driver_layout(remote_deploy, flag): + result, calls = remote_deploy(flag) + assert result.returncode == 0, result.stderr + commands = [call["command"] for call in calls] + assert any(" -- 'restore' " in command for command in commands) + assert not any(command.startswith(("cmake ", "mktemp ")) for command in commands) + + +def test_deployment_check_is_read_only(remote_deploy): + result, calls = remote_deploy("--check") + assert result.returncode == 0, result.stderr + forbidden = ( + "systemctl restart ", + "systemctl stop ", + "systemctl enable ", + "systemctl disable ", + "sudo -n install ", + "mktemp ", + ) + assert not any(any(word in call["command"] for word in forbidden) for call in calls) + assert not any("bash -s --" in call["command"] for call in calls) + + +def test_sender_unit_requires_only_platform_camera_ownership(): + unit = (DEPLOY / "camera-argus-sender.service.in").read_text() + assert "cam-init" not in unit + assert "camstream" not in unit + assert "Conflicts=nvargus-daemon.service" in unit diff --git a/examples/camera_streamer/tests/cpp/test_frame_alignment_policy.cpp b/examples/camera_streamer/argus_sender/tests/test_frame_alignment_policy.cpp similarity index 97% rename from examples/camera_streamer/tests/cpp/test_frame_alignment_policy.cpp rename to examples/camera_streamer/argus_sender/tests/test_frame_alignment_policy.cpp index 2eda922803..072515acfa 100644 --- a/examples/camera_streamer/tests/cpp/test_frame_alignment_policy.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_frame_alignment_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_geometry_scheduler.cpp b/examples/camera_streamer/argus_sender/tests/test_geometry_scheduler.cpp similarity index 58% rename from examples/camera_streamer/tests/cpp/test_geometry_scheduler.cpp rename to examples/camera_streamer/argus_sender/tests/test_geometry_scheduler.cpp index 0526a217de..1db6b70b9c 100644 --- a/examples/camera_streamer/tests/cpp/test_geometry_scheduler.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_geometry_scheduler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ @@ -41,34 +41,6 @@ int main() TEST_CHECK(test, argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); TEST_CHECK(test, std::fabs(geometry.clips[0].top) < 0.0001F); - output = argus_sender::OutputConfig(); - output.height = 480; - TEST_CHECK(test, argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - TEST_CHECK_EQ(test, geometry.width, 620U); - TEST_CHECK_EQ(test, geometry.height, 480U); - - output.width = 620; - TEST_CHECK(test, !argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - output.height = 0; - TEST_CHECK(test, argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - TEST_CHECK_EQ(test, geometry.height, 482U); - - output.width = 0; - output.height = 480; - sources.push_back(sources[0]); - TEST_CHECK(test, argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - sources[1].width = 1920; - sources[1].height = 1080; - TEST_CHECK(test, !argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - sources.resize(1); - output.remap.file = "test.rmap"; - output.remap.captureWidth = 620; - output.remap.captureHeight = 480; - TEST_CHECK(test, !argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - output.remap.captureWidth = 2560; - output.remap.captureHeight = 1984; - TEST_CHECK(test, argus_sender::resolveOutputGeometry(output, sources, &geometry, &error)); - argus_sender::MixedRateScheduler scheduler({ 60, 30, 45 }); TEST_CHECK_EQ(test, scheduler.baseFps(), 60U); uint32_t counts[3] = { 0, 0, 0 }; diff --git a/examples/camera_streamer/tests/cpp/test_nv_remap.cpp b/examples/camera_streamer/argus_sender/tests/test_nv_remap.cpp similarity index 98% rename from examples/camera_streamer/tests/cpp/test_nv_remap.cpp rename to examples/camera_streamer/argus_sender/tests/test_nv_remap.cpp index 3a3dde9085..552adf17c6 100644 --- a/examples/camera_streamer/tests/cpp/test_nv_remap.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_nv_remap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_priority_frame_handoff.cpp b/examples/camera_streamer/argus_sender/tests/test_priority_frame_handoff.cpp similarity index 96% rename from examples/camera_streamer/tests/cpp/test_priority_frame_handoff.cpp rename to examples/camera_streamer/argus_sender/tests/test_priority_frame_handoff.cpp index 4b4a93df13..e98553b173 100644 --- a/examples/camera_streamer/tests/cpp/test_priority_frame_handoff.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_priority_frame_handoff.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_rate_limiter.cpp b/examples/camera_streamer/argus_sender/tests/test_rate_limiter.cpp similarity index 97% rename from examples/camera_streamer/tests/cpp/test_rate_limiter.cpp rename to examples/camera_streamer/argus_sender/tests/test_rate_limiter.cpp index efb3d76eab..d9d4a48f8f 100644 --- a/examples/camera_streamer/tests/cpp/test_rate_limiter.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_rate_limiter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/tests/cpp/test_tcp_transport.cpp b/examples/camera_streamer/argus_sender/tests/test_tcp_transport.cpp similarity index 98% rename from examples/camera_streamer/tests/cpp/test_tcp_transport.cpp rename to examples/camera_streamer/argus_sender/tests/test_tcp_transport.cpp index 7cf310f8a5..4b7fc7de27 100644 --- a/examples/camera_streamer/tests/cpp/test_tcp_transport.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_tcp_transport.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/examples/camera_streamer/camera_receiver/.gitignore b/examples/camera_streamer/camera_receiver/.gitignore index 216bcdfefa..0878781cb5 100644 --- a/examples/camera_streamer/camera_receiver/.gitignore +++ b/examples/camera_streamer/camera_receiver/.gitignore @@ -1,6 +1,3 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - # Python environment and caches .venv/ __pycache__/ diff --git a/examples/camera_streamer/camera_receiver/README.md b/examples/camera_streamer/camera_receiver/README.md new file mode 100644 index 0000000000..d1706c4c64 --- /dev/null +++ b/examples/camera_streamer/camera_receiver/README.md @@ -0,0 +1,211 @@ + + +# camera_receiver + +Standalone AVS1 multi-stream receiver for the two-process camera path: + +1. Thor `argus_sender` sends AVS1 bundles over TCP. +2. `camera_receiver` connects to the sender, decodes HEVC streams or copies raw + NV12 streams into GPU-resident rings. +3. Same-host consumers open selected streams through CUDA IPC. + +## Setup + +From this directory: + +```bash +./setup.sh +``` + +The setup script creates: + +```text +.venv/ +codec/_camera_receiver_codec*.so +``` + +It installs the Python dependencies from `requirements.txt` and builds the +local native decoder from `codec/`. + +## Run + +Start the receiver: + +```bash +./run.sh --host --port 5000 +``` + +The default endpoint is `127.0.0.1:5000`. Override `receiver.host` with +`--host` or `CAMERA_RECEIVER_HOST`. Run this process on the receiving Linux GPU +host; see the [environment and quick start](../README.md). Stop the foreground +receiver with Ctrl-C/SIGTERM. Setup does not create a receiver system service. + +The default CUDA IPC socket is: + +```text +/tmp/camera_receiver_avs1_cuda_ipc.sock +``` + +`receiver.gpu_id` is an explicit CUDA device ID. `0` means CUDA device 0. To use +the current CuPy/CUDA device instead, omit `receiver.gpu_id` from the config or +set it to `null`. + +## Stream Config + +The default config matches a sender with one stereo `head` stream and two mono +raw-NV12 wrist streams: + +```yaml +streams: + - index: 0 + name: head + type: stereo + width: 2560 + height: 1984 + output_format: nv12 + ring_depth: 6 + ipc: + left: head.left + right: head.right + + - index: 1 + name: wrist1 + type: mono + width: 640 + height: 480 + output_format: nv12 + ring_depth: 6 + ipc: + mono: wrist1 +``` + +`index` maps to the AVS1 stream descriptor index. AVS1 descriptors do not carry +the sender YAML `id`, so the receiver config supplies user-facing names and IPC +stream names. + +`output_format` can be: + +```text +nv12 -> ((height * 3) / 2, width) uint8 +rgba8 -> (height, width, 4) uint8 +``` + +For raw AVS1 NV12 streams, `nv12` avoids color conversion and copies the TCP +payload into the GPU IPC ring. For HEVC streams, `nv12` asks NVDEC to write its +decoded NV12 surface into the IPC ring; `rgba8` converts to RGBA before publish. +The receiver applies the AVS1 stream descriptor's 0/180-degree presentation +rotation during that existing output write. HEVC NV12 rotation replaces the +normal NVDEC-to-ring device copy with a rotation kernel; RGBA rotation is fused +into color conversion, so neither path adds a second full-frame pass. + +`ring_depth` controls how many slots each physical IPC stream owns. Frame +notifications include `slot_generation`; consumers can detect if a slot was +overwritten before they consumed it. + +## Downstream consumers + +Consumers subscribe to the GPU rings through the local Unix socket. The +[example consumer](../cuda_ipc_consumer/README.md) supports display, headless +checks and latency CSV output. Other applications can use its `CudaIpcClient`. + +The default head frames remain distorted at 2560x1984; presentation rotation is +applied while writing the IPC output. The default wrist frames arrive at 640x480, +already remapped by the sender, so consumers must not apply the wrist remap again. +See the bundled [remap geometry and calibration](../argus_sender/remaps/README.md). + +Notifications include frame sequence, slot generation, source timing and local +publish timing. Validate frame identity and generation when associating frames +with metadata. The rings are bounded live buffers, not a durable recording queue. + +## Latency + +The receiver prints rolling decode/copy and IPC publish stats every five +seconds by default. Configure the interval under `receiver`: + +```yaml +stats_log_interval_s: 5.0 +``` + +Set `stats_log_interval_s: 0` to disable only this recurring stats summary. +Startup, connection, warning, error, and other one-time logs are unaffected. +The equivalent command-line override is `--stats-log-interval-s`; for example, +`--stats-log-interval-s 10` prints the summary every ten seconds. + +Low ingress FPS uses an independent warning-level monitor. It counts each +configured logical AVS stream after a complete bundle is received and parsed, +before decode/copy work is queued. Configure its threshold and window under +`receiver`: + +```yaml +receive_fps_warning_threshold: 55.0 +receive_fps_warning_interval_s: 10.0 +``` + +Every warning interval, streams below the target are reported individually, +for example `head=52.4fps`. A connected stream that receives no complete frames +is reported as `0.0fps`. Disconnect time is excluded because the existing +connection/retry logs cover that state. Set `receive_fps_warning_threshold: 0` +to disable these warnings. This monitor remains active when +`stats_log_interval_s` is `0` because its output is warning-level, not part of +the periodic INFO summary. + +The receiver processes logical AVS streams independently: + +- the TCP reader must receive a complete AVS1 bundle first; +- each present logical stream is queued immediately after parse; +- different logical streams process in parallel; +- stereo eyes within one stream process in parallel; +- a stream is published as soon as its own samples are ready. + +AVS1 sender wall-clock timing fields are used for capture/process/TCP latency +when sender and receiver clocks are synchronized and report the same clock +domain. AVS1 SOF TSC values are Tegra TSC-domain values, not wall-clock +timestamps; `sof_delta` is meaningful for stereo alignment, but absolute +SOF-to-consumer latency needs an additional sender TSC-to-wall-clock +calibration path. + +## Overrides + +Use another config: + +```bash +./run.sh configs/receiver.yaml +``` + +Pass receiver flags after the optional config path: + +```bash +./run.sh configs/receiver.yaml --log-level DEBUG --stats-log-interval-s 10 +``` + +Override the sender endpoint without editing YAML: + +```bash +CAMERA_RECEIVER_HOST= CAMERA_RECEIVER_PORT=5000 ./run.sh +``` + +Use a different Python interpreter: + +```bash +CAMERA_RECEIVER_PYTHON=/path/to/python ./run.sh +``` + +Use a different bootstrap Python when creating the venv: + +```bash +CAMERA_RECEIVER_BOOTSTRAP_PYTHON=/path/to/python3 ./setup.sh +``` + +## Tests + +From this component directory, run the protocol, reconnect and mocked decoder +tests with pytest and PyYAML installed: + +```bash +python3 -m pytest tests -q +``` + +These tests require neither a GPU nor the sender or consumer source folders. diff --git a/examples/camera_streamer/camera_receiver/avs_wire.py b/examples/camera_streamer/camera_receiver/avs_wire.py index 8058afb554..edca77d1c6 100644 --- a/examples/camera_streamer/camera_receiver/avs_wire.py +++ b/examples/camera_streamer/camera_receiver/avs_wire.py @@ -195,7 +195,6 @@ def parse_avs1_record(record: bytes | bytearray | memoryview) -> Avs1Bundle: offset = header_bytes stream_headers: list[tuple] = [] - flat_cursor = 0 for expected_index in range(stream_count): fields = STREAM.unpack_from(packet, offset) offset += STREAM.size @@ -229,10 +228,7 @@ def parse_avs1_record(record: bytes | bytearray | memoryview) -> Avs1Bundle: expected_sample_count = 2 if stream_flags & STREAM_STEREO else 1 if stream_sample_count != expected_sample_count: raise ValueError("AVS1 stream sample count does not match mono/stereo") - if first_sample != flat_cursor: - raise ValueError("AVS1 stream sample range is not contiguous") - flat_cursor += stream_sample_count - if flat_cursor > sample_count: + if first_sample + stream_sample_count > sample_count: raise ValueError("AVS1 stream sample range exceeds descriptor table") if stream_flags & STREAM_PRESENT: if stream_sequence == 0: @@ -241,9 +237,6 @@ def parse_avs1_record(record: bytes | bytearray | memoryview) -> Avs1Bundle: raise ValueError("absent AVS1 stream has nonzero stream_sequence") stream_headers.append(fields) - if flat_cursor != sample_count: - raise ValueError("AVS1 sample descriptors are not fully referenced") - raw_samples = [] for _flat_index in range(sample_count): fields = SAMPLE.unpack_from(packet, offset) diff --git a/examples/camera_streamer/camera_receiver/camera_receiver.py b/examples/camera_streamer/camera_receiver/camera_receiver.py index df04461704..8568f20aa5 100755 --- a/examples/camera_streamer/camera_receiver/camera_receiver.py +++ b/examples/camera_streamer/camera_receiver/camera_receiver.py @@ -23,7 +23,7 @@ import struct import threading import time -from concurrent.futures import Future, ThreadPoolExecutor, wait +from concurrent.futures import Future, ThreadPoolExecutor from dataclasses import dataclass from pathlib import Path from typing import Any, Callable, Iterable, Optional @@ -44,7 +44,7 @@ from cuda_ipc import CudaIpcFramePublisher from nv_video_decode import NvVideoDecoder -logger = logging.getLogger("isaaccapture.camera_streamer.camera_receiver") +logger = logging.getLogger("isaaccapture.camera_streamer.receiver") _SOCK_TIMEOUT_S = 0.5 # If no bytes arrive for this long the link is treated as dead and torn down so @@ -399,7 +399,6 @@ def __init__(self, config: StreamConfig, gpu_id: int) -> None: max_workers=1, thread_name_prefix=f"camera_receiver_stream_{config.name}", ) - self.inflight = threading.BoundedSemaphore(config.ring_depth) self.block = (16, 16, 1) self.grid = ( (config.width + 15) // 16, @@ -453,23 +452,6 @@ def reset_decoders(self) -> None: ) self.decoders[index] = None - def submit(self, stop: threading.Event, fn, *args) -> bool: - """Bound decode and reset work while allowing shutdown to interrupt waiting.""" - while not stop.is_set(): - if not self.inflight.acquire(timeout=0.1): - continue - if stop.is_set(): - self.inflight.release() - return False - try: - future = self.executor.submit(fn, *args) - except BaseException: - self.inflight.release() - raise - future.add_done_callback(lambda _future: self.inflight.release()) - return True - return False - def shutdown(self) -> None: self.executor.shutdown(wait=True) self.reset_decoders() @@ -495,7 +477,7 @@ def __init__( import cupy as cp except ImportError as e: raise RuntimeError( - "camera_receiver requires CuPy. Install the receiver extra as described in examples/camera_streamer/README.md." + "camera_receiver requires CuPy. Run ./setup.sh from camera_receiver." ) from e if not streams: raise ValueError("camera_receiver requires at least one AVS stream") @@ -589,8 +571,8 @@ def stop(self) -> None: if self._conn is not None: try: self._conn.close() - except OSError: - logger.debug("Failed to close receiver socket", exc_info=True) + except Exception: + pass if self._thread is not None: self._thread.join(timeout=5.0) if self._thread.is_alive(): @@ -636,14 +618,14 @@ def _produce_loop(self) -> None: finally: try: conn.close() - except OSError: - logger.debug("Failed to close sender connection", exc_info=True) + except Exception: + pass self._conn = None if self._receive_fps_monitor is not None: self._receive_fps_monitor.stop() self._session_id = None for runtime in self._runtimes.values(): - runtime.submit(self._stop, runtime.reset_decoders) + runtime.executor.submit(runtime.reset_decoders) def _connect(self) -> Optional[socket.socket]: next_wait_log_at = 0.0 @@ -727,7 +709,7 @@ def _dispatch_bundle(self, received: ReceivedRecord, bundle: Avs1Bundle) -> None bundle.session_id, ) for runtime in self._runtimes.values(): - runtime.submit(self._stop, runtime.reset_decoders) + runtime.executor.submit(runtime.reset_decoders) self._session_id = bundle.session_id dispatch_items: list[tuple[Avs1Stream, _StreamRuntime]] = [] @@ -741,8 +723,7 @@ def _dispatch_bundle(self, received: ReceivedRecord, bundle: Avs1Bundle) -> None runtime.config.name for _, runtime in dispatch_items ) for stream, runtime in dispatch_items: - runtime.submit( - self._stop, + runtime.executor.submit( self._process_stream_safe, received, bundle, @@ -774,6 +755,8 @@ def _process_stream( stream: Avs1Stream, runtime: _StreamRuntime, ) -> None: + import cupy as cp + self._validate_stream_descriptor(stream, runtime.config) slot = runtime.write_index t0 = time.monotonic() @@ -786,14 +769,16 @@ def _process_stream( ) for sample in stream.samples ] - # Keep the slot and decoders owned until both eyes finish, even on failure. - wait(futures) - results = [future.result() for future in futures] - if not all(results): + if not all(future.result() for future in futures): return receiver_process_done_mono_ns = time.monotonic_ns() receiver_process_done_ns, _ = _read_wall_clock_ns() + # The native decoder is synchronous. Raw NV12 copies / conversion kernels + # run on CuPy's current stream, so sync before announcing IPC visibility. + with cp.cuda.Device(self._gpu_device_id): + cp.cuda.get_current_stream().synchronize() + timestamp_ns = time.monotonic_ns() receiver_ipc_publish_mono_ns = timestamp_ns receiver_ipc_publish_ns, _ = _read_wall_clock_ns() @@ -843,20 +828,7 @@ def _process_stream( latencies, ) - def _process_sample(self, runtime, stream, sample, slot) -> bool: - import cupy as cp - - name = runtime.config.ipc_names[sample.camera_index] - with self._ipc_publisher.write_slot(name, slot, self._stop): - with cp.cuda.Device(self._gpu_device_id): - try: - return self._decode_sample(runtime, stream, sample, slot) - finally: - # CuPy's per-thread default stream belongs to this worker. - # Complete reads/writes even on failure before releasing the slot. - cp.cuda.get_current_stream().synchronize() - - def _decode_sample( + def _process_sample( self, runtime: _StreamRuntime, stream: Avs1Stream, @@ -1065,7 +1037,7 @@ def _frame_metadata( # None when the sender TSC anchor is absent or the SOF->ready lag is # out of range. "sensor_sof_ns": sample_sof_ns, - # Same value under the key the CUDA-IPC consumer tools and TeleViz + # Same value under the key the CUDA IPC consumer tools and other viewers # already read for their "sof_to_consumer_ms" column. "sof_wall_clock_ns": sample_sof_ns, "sensor_sof_lag_ms": ( @@ -1345,42 +1317,11 @@ def _load_config(path: Path) -> dict: cfg = yaml.safe_load(f) if not isinstance(cfg, dict): raise ValueError(f"config must be a YAML mapping: {path}") - if "network" in cfg: - # AVS1 stream indices follow sender list order; geometry and port are shared. - receiver = dict(cfg.get("receiver", {})) - if "port" in receiver: - raise ValueError("shared config uses network.port, not receiver.port") - receiver["port"] = cfg["network"].get("port", 5000) - streams = [] - for index, stream in enumerate(cfg.get("streams", [])): - settings = dict(stream.get("receiver", {})) - if set(settings) & {"index", "name", "type", "width", "height"}: - raise ValueError( - "shared stream identity and geometry belong in id/type/output" - ) - output = stream["output"] - if int(output.get("width", 0)) <= 0 or int(output.get("height", 0)) <= 0: - raise ValueError( - "shared config requires explicit positive output width and height" - ) - streams.append( - dict( - settings, - index=index, - name=stream["id"], - type=stream["type"], - width=output["width"], - height=output["height"], - ) - ) - cfg = dict(cfg, receiver=receiver, streams=streams) return cfg def main(argv: Optional[list[str]] = None) -> int: - default_config = ( - Path(__file__).resolve().parents[1] / "configs" / "s56-stereo-2560x1984-30.yaml" - ) + default_config = Path(__file__).resolve().parent / "configs" / "receiver.yaml" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--config", default=str(default_config)) parser.add_argument("--host", help="override receiver.host or CAMERA_RECEIVER_HOST") diff --git a/examples/camera_streamer/camera_receiver/codec/CMakeLists.txt b/examples/camera_streamer/camera_receiver/codec/CMakeLists.txt index c659529f60..fd88bac5ab 100644 --- a/examples/camera_streamer/camera_receiver/codec/CMakeLists.txt +++ b/examples/camera_streamer/camera_receiver/codec/CMakeLists.txt @@ -98,6 +98,8 @@ pybind11_add_module(_camera_receiver_codec MODULE ) target_include_directories(_camera_receiver_codec PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/inc ${_nvc_sdk_dir} ${_nvc_sdk_dir}/NvDecoder ) diff --git a/examples/camera_streamer/camera_receiver/codec/__init__.py b/examples/camera_streamer/camera_receiver/codec/__init__.py index f404495c38..ff176d374e 100644 --- a/examples/camera_streamer/camera_receiver/codec/__init__.py +++ b/examples/camera_streamer/camera_receiver/codec/__init__.py @@ -12,8 +12,8 @@ ) except ImportError as e: raise ImportError( - "camera_receiver native codec not built. Follow " - "the host receiver build instructions in examples/camera_streamer/README.md." + "camera_receiver native codec not built. Run " + "`codec/build.sh` from the camera_receiver directory." ) from e __all__ = [ diff --git a/examples/camera_streamer/camera_receiver/codec/build.sh b/examples/camera_streamer/camera_receiver/codec/build.sh new file mode 100755 index 0000000000..2414d3908e --- /dev/null +++ b/examples/camera_streamer/camera_receiver/codec/build.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Builds the native NVDEC decoder. The .so lands next to +# __init__.py so the package imports straight from the source tree. +set -euo pipefail + +here="$(cd "$(dirname "$0")" && pwd)" +project_dir="$(cd "${here}/.." && pwd)" +build_dir="${here}/build" +venv_dir="${CAMERA_RECEIVER_VENV:-${project_dir}/.venv}" +python_exe="${CAMERA_RECEIVER_PYTHON:-${venv_dir}/bin/python}" + +if [[ ! -x "$python_exe" ]]; then + echo "build.sh: no Python at $python_exe" >&2 + echo "build.sh: override with CAMERA_RECEIVER_VENV=/path/to/venv or CAMERA_RECEIVER_PYTHON=/path/to/python" >&2 + exit 1 +fi + +export VIRTUAL_ENV="$venv_dir" +export PATH="${venv_dir}/bin:${PATH}" + +# Prefer /usr/local/cuda's nvcc — distro nvcc is often too old for +# modern compute capabilities. +if [[ -z "${CUDACXX:-}" && -x /usr/local/cuda/bin/nvcc ]]; then + export CUDACXX=/usr/local/cuda/bin/nvcc + export CUDA_PATH=/usr/local/cuda + export PATH="/usr/local/cuda/bin:${PATH}" +fi + +if ! "$python_exe" -c "import pybind11" 2>/dev/null; then + echo "build.sh: installing pybind11 into venv..." >&2 + "$python_exe" -m pip install pybind11 +fi + +cuda_architectures="${CMAKE_CUDA_ARCHITECTURES:-}" +if [[ -z "$cuda_architectures" ]] && command -v nvidia-smi >/dev/null 2>&1; then + cuda_architectures="$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader 2>/dev/null \ + | head -n1 | tr -d '[:space:].')" +fi +if [[ -z "$cuda_architectures" ]]; then + cuda_architectures="80;86;87;89;90" +fi +echo "build.sh: Python=${python_exe}" +echo "build.sh: CMAKE_CUDA_ARCHITECTURES=${cuda_architectures}" + +generator=() +if command -v ninja >/dev/null 2>&1; then + generator=(-G Ninja) +fi + +cmake -S "${here}" -B "${build_dir}" "${generator[@]}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CUDA_ARCHITECTURES="${cuda_architectures}" \ + -DPython3_EXECUTABLE="${python_exe}" +cmake --build "${build_dir}" --parallel + +echo +echo "build.sh: decoder built → ${here}/_camera_receiver_codec*.so" diff --git a/examples/camera_streamer/camera_receiver/codec/codec_bindings.cpp b/examples/camera_streamer/camera_receiver/codec/codec_bindings.cpp index 3a409dab76..48774a994e 100644 --- a/examples/camera_streamer/camera_receiver/codec/codec_bindings.cpp +++ b/examples/camera_streamer/camera_receiver/codec/codec_bindings.cpp @@ -3,7 +3,6 @@ // // pybind11 bindings — public surface is ``camera_receiver.codec``. -#include "cuda_buffers.hpp" #include "nv_video_decoder.hpp" #include @@ -18,6 +17,134 @@ using camera_receiver::codec::Codec; using camera_receiver::codec::DecoderConfig; using camera_receiver::codec::NvVideoDecoder; +namespace +{ + +// Extracts device pointer + row pitch from an object exposing +// __cuda_array_interface__. Expects HxWx4 RGBA8 matching the configured +// (width, height). +struct CudaBuf +{ + std::uintptr_t ptr; + std::size_t row_pitch_bytes; +}; + +CudaBuf extract_cuda_buffer(const py::object& obj, std::uint32_t expected_width, std::uint32_t expected_height) +{ + if (!py::hasattr(obj, "__cuda_array_interface__")) + { + throw std::runtime_error("expected object exposing __cuda_array_interface__"); + } + py::dict iface = obj.attr("__cuda_array_interface__").cast(); + if (!iface.contains("shape") || !iface.contains("data")) + { + throw std::runtime_error("__cuda_array_interface__ missing required keys (shape/data)"); + } + + py::tuple shape = iface["shape"].cast(); + if (shape.size() != 3) + { + throw std::runtime_error("expected 3D array (H, W, 4); got rank " + std::to_string(shape.size())); + } + const auto h = shape[0].cast(); + const auto w = shape[1].cast(); + const auto c = shape[2].cast(); + if (c != 4) + { + throw std::runtime_error("expected 4-channel RGBA; got " + std::to_string(c)); + } + if (h != expected_height || w != expected_width) + { + throw std::runtime_error("frame size " + std::to_string(w) + "x" + std::to_string(h) + + " does not match configured " + std::to_string(expected_width) + "x" + + std::to_string(expected_height)); + } + + py::tuple data = iface["data"].cast(); + const auto ptr = data[0].cast(); + + // Default to tightly packed RGBA8. + std::size_t row_pitch = static_cast(w) * 4u; + if (iface.contains("strides") && !iface["strides"].is_none()) + { + py::tuple strides = iface["strides"].cast(); + if (strides.size() != 3) + { + throw std::runtime_error("strides rank does not match shape rank"); + } + const auto row_stride = strides[0].cast(); + const auto pixel_stride = strides[1].cast(); + const auto chan_stride = strides[2].cast(); + if (pixel_stride != 4 || chan_stride != 1) + { + throw std::runtime_error("non-contiguous-per-pixel RGBA layout not supported"); + } + if (row_stride < static_cast(w * 4u)) + { + throw std::runtime_error("row stride smaller than W*4"); + } + row_pitch = static_cast(row_stride); + } + + return CudaBuf{ ptr, row_pitch }; +} + +CudaBuf extract_nv12_buffer(const py::object& obj, std::uint32_t expected_width, std::uint32_t expected_height) +{ + if (!py::hasattr(obj, "__cuda_array_interface__")) + { + throw std::runtime_error("expected object exposing __cuda_array_interface__"); + } + py::dict iface = obj.attr("__cuda_array_interface__").cast(); + if (!iface.contains("shape") || !iface.contains("data")) + { + throw std::runtime_error("__cuda_array_interface__ missing required keys (shape/data)"); + } + + py::tuple shape = iface["shape"].cast(); + if (shape.size() != 2) + { + throw std::runtime_error("expected 2D NV12 array ((H*3)/2, W); got rank " + std::to_string(shape.size())); + } + const auto h = shape[0].cast(); + const auto w = shape[1].cast(); + const auto expected_rows = expected_height * 3u / 2u; + if (h != expected_rows || w != expected_width) + { + throw std::runtime_error("NV12 frame size " + std::to_string(w) + "x" + std::to_string(h) + + " does not match configured " + std::to_string(expected_width) + "x" + + std::to_string(expected_rows)); + } + + py::tuple data = iface["data"].cast(); + const auto ptr = data[0].cast(); + + std::size_t row_pitch = expected_width; + if (iface.contains("strides") && !iface["strides"].is_none()) + { + py::tuple strides = iface["strides"].cast(); + if (strides.size() != 2) + { + throw std::runtime_error("NV12 strides rank does not match shape rank"); + } + const auto row_stride = strides[0].cast(); + const auto col_stride = strides[1].cast(); + if (col_stride != 1) + { + throw std::runtime_error("non-contiguous NV12 columns are not supported"); + } + if (row_stride < static_cast(expected_width)) + { + throw std::runtime_error("NV12 row stride smaller than width"); + } + row_pitch = static_cast(row_stride); + } + + return CudaBuf{ ptr, row_pitch }; +} + +} // namespace + PYBIND11_MODULE(_camera_receiver_codec, m) { m.doc() = "Native NVDEC H.264/HEVC decoder for camera_receiver."; @@ -48,9 +175,6 @@ PYBIND11_MODULE(_camera_receiver_codec, m) { throw std::runtime_error("packet must be a uint8 buffer"); } - // Destination capacity must match the decoder, not caller-supplied dimensions. - if (width != self.width() || height != self.height()) - throw std::runtime_error("width/height do not match the configured decoder size"); const CudaBuf buf = extract_cuda_buffer(rgba_out, width, height); bool produced = false; { @@ -76,9 +200,6 @@ PYBIND11_MODULE(_camera_receiver_codec, m) { throw std::runtime_error("packet must be a uint8 buffer"); } - // Destination capacity must match the decoder, not caller-supplied dimensions. - if (width != self.width() || height != self.height()) - throw std::runtime_error("width/height do not match the configured decoder size"); const CudaBuf buf = extract_nv12_buffer(nv12_out, width, height); bool produced = false; { diff --git a/examples/camera_streamer/camera_receiver/codec/cuda_buffers.hpp b/examples/camera_streamer/camera_receiver/codec/cuda_buffers.hpp deleted file mode 100644 index 936a84bf4a..0000000000 --- a/examples/camera_streamer/camera_receiver/codec/cuda_buffers.hpp +++ /dev/null @@ -1,155 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include - -#include -#include -#include - -namespace py = pybind11; - -namespace -{ - -// Extracts device pointer + row pitch from an object exposing -// __cuda_array_interface__. Expects HxWx4 RGBA8 matching the configured -// (width, height). -struct CudaBuf -{ - std::uintptr_t ptr; - std::size_t row_pitch_bytes; -}; - -inline CudaBuf extract_cuda_buffer(const py::object& obj, std::uint32_t expected_width, std::uint32_t expected_height) -{ - if (!py::hasattr(obj, "__cuda_array_interface__")) - { - throw std::runtime_error("expected object exposing __cuda_array_interface__"); - } - py::dict iface = obj.attr("__cuda_array_interface__").cast(); - if (!iface.contains("shape") || !iface.contains("data")) - { - throw std::runtime_error("__cuda_array_interface__ missing required keys (shape/data)"); - } - - if (!iface.contains("typestr") || iface["typestr"].cast() != "|u1") - throw std::runtime_error("output buffer must have uint8 dtype (|u1)"); - py::tuple output_data = iface["data"].cast(); - if (output_data.size() != 2 || output_data[1].cast()) - throw std::runtime_error("output buffer must be writable"); - if (output_data[0].cast() == 0) - throw std::runtime_error("output buffer pointer is null"); - - py::tuple shape = iface["shape"].cast(); - if (shape.size() != 3) - { - throw std::runtime_error("expected 3D array (H, W, 4); got rank " + std::to_string(shape.size())); - } - const auto h = shape[0].cast(); - const auto w = shape[1].cast(); - const auto c = shape[2].cast(); - if (c != 4) - { - throw std::runtime_error("expected 4-channel RGBA; got " + std::to_string(c)); - } - if (h != expected_height || w != expected_width) - { - throw std::runtime_error("frame size " + std::to_string(w) + "x" + std::to_string(h) + - " does not match configured " + std::to_string(expected_width) + "x" + - std::to_string(expected_height)); - } - - py::tuple data = iface["data"].cast(); - const auto ptr = data[0].cast(); - - // Default to tightly packed RGBA8. - std::size_t row_pitch = static_cast(w) * 4u; - if (iface.contains("strides") && !iface["strides"].is_none()) - { - py::tuple strides = iface["strides"].cast(); - if (strides.size() != 3) - { - throw std::runtime_error("strides rank does not match shape rank"); - } - const auto row_stride = strides[0].cast(); - const auto pixel_stride = strides[1].cast(); - const auto chan_stride = strides[2].cast(); - if (pixel_stride != 4 || chan_stride != 1) - { - throw std::runtime_error("non-contiguous-per-pixel RGBA layout not supported"); - } - if (row_stride < static_cast(w * 4u)) - { - throw std::runtime_error("row stride smaller than W*4"); - } - row_pitch = static_cast(row_stride); - } - - return CudaBuf{ ptr, row_pitch }; -} - -inline CudaBuf extract_nv12_buffer(const py::object& obj, std::uint32_t expected_width, std::uint32_t expected_height) -{ - if (!py::hasattr(obj, "__cuda_array_interface__")) - { - throw std::runtime_error("expected object exposing __cuda_array_interface__"); - } - py::dict iface = obj.attr("__cuda_array_interface__").cast(); - if (!iface.contains("shape") || !iface.contains("data")) - { - throw std::runtime_error("__cuda_array_interface__ missing required keys (shape/data)"); - } - - if (!iface.contains("typestr") || iface["typestr"].cast() != "|u1") - throw std::runtime_error("output buffer must have uint8 dtype (|u1)"); - py::tuple output_data = iface["data"].cast(); - if (output_data.size() != 2 || output_data[1].cast()) - throw std::runtime_error("output buffer must be writable"); - if (output_data[0].cast() == 0) - throw std::runtime_error("output buffer pointer is null"); - - py::tuple shape = iface["shape"].cast(); - if (shape.size() != 2) - { - throw std::runtime_error("expected 2D NV12 array ((H*3)/2, W); got rank " + std::to_string(shape.size())); - } - const auto h = shape[0].cast(); - const auto w = shape[1].cast(); - const auto expected_rows = expected_height * 3u / 2u; - if (h != expected_rows || w != expected_width) - { - throw std::runtime_error("NV12 frame size " + std::to_string(w) + "x" + std::to_string(h) + - " does not match configured " + std::to_string(expected_width) + "x" + - std::to_string(expected_rows)); - } - - py::tuple data = iface["data"].cast(); - const auto ptr = data[0].cast(); - - std::size_t row_pitch = expected_width; - if (iface.contains("strides") && !iface["strides"].is_none()) - { - py::tuple strides = iface["strides"].cast(); - if (strides.size() != 2) - { - throw std::runtime_error("NV12 strides rank does not match shape rank"); - } - const auto row_stride = strides[0].cast(); - const auto col_stride = strides[1].cast(); - if (col_stride != 1) - { - throw std::runtime_error("non-contiguous NV12 columns are not supported"); - } - if (row_stride < static_cast(expected_width)) - { - throw std::runtime_error("NV12 row stride smaller than width"); - } - row_pitch = static_cast(row_stride); - } - - return CudaBuf{ ptr, row_pitch }; -} - -} // namespace diff --git a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.hpp b/examples/camera_streamer/camera_receiver/codec/inc/nv_video_decoder.hpp similarity index 97% rename from examples/camera_streamer/camera_receiver/codec/nv_video_decoder.hpp rename to examples/camera_streamer/camera_receiver/codec/inc/nv_video_decoder.hpp index a0f3a06c3a..a6268db281 100644 --- a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.hpp +++ b/examples/camera_streamer/camera_receiver/codec/inc/nv_video_decoder.hpp @@ -42,9 +42,6 @@ class NvVideoDecoder NvVideoDecoder(NvVideoDecoder&&) = delete; NvVideoDecoder& operator=(NvVideoDecoder&&) = delete; - uint32_t width() const; - uint32_t height() const; - // Feed one Annex-B access unit. Returns true iff a frame was // produced and converted into the caller's RGBA8 buffer. // ``rgba_out_device_ptr`` points to the top-left of a HxWx4 GPU diff --git a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp b/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp index dbf8dd4589..eaa8188e0a 100644 --- a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp +++ b/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -55,7 +54,6 @@ struct NvVideoDecoder::Impl CUcontext cu_context = nullptr; cudaStream_t stream = nullptr; std::unique_ptr decoder; - std::mutex decoder_mutex; bool warned_size_mismatch = false; Impl(const DecoderConfig& c) : cfg(c) @@ -121,7 +119,6 @@ struct NvVideoDecoder::Impl bool decode_one( const uint8_t* packet, std::size_t packet_size, uintptr_t rgba_out, std::size_t rgba_row_bytes, int rotation) { - std::lock_guard lock(decoder_mutex); if (!decoder) { throw std::runtime_error("NvVideoDecoder: decoder unavailable"); @@ -178,25 +175,11 @@ struct NvVideoDecoder::Impl return false; } - cudaError_t sync_err = cudaSuccess; - try - { - if (decoder->GetOutputFormat() != cudaVideoSurfaceFormat_NV12) - throw std::runtime_error("NvVideoDecoder: only 8-bit NV12 decoded surfaces are supported"); - launch_nv12_to_rgba(nv12, nv12 + luma_size, pitch, pitch, w, h, reinterpret_cast(rgba_out), - static_cast(rgba_row_bytes), cfg.full_range, rotation, stream); - // UnlockFrame returns the surface to NVDEC's pool, so the - // kernel reading it must finish first. - sync_err = cudaStreamSynchronize(stream); - } - catch (...) - { - // Complete any submitted reads before returning the surface to NVDEC. - cudaStreamSynchronize(stream); - decoder->UnlockFrame(&nv12); - cuCtxPopCurrent(nullptr); - throw; - } + launch_nv12_to_rgba(nv12, nv12 + luma_size, pitch, pitch, w, h, reinterpret_cast(rgba_out), + static_cast(rgba_row_bytes), cfg.full_range, rotation, stream); + // UnlockFrame returns the surface to NVDEC's pool, so the + // kernel reading it must finish first. + const cudaError_t sync_err = cudaStreamSynchronize(stream); decoder->UnlockFrame(&nv12); check_cu(cuCtxPopCurrent(nullptr), "cuCtxPopCurrent"); if (sync_err != cudaSuccess) @@ -211,7 +194,6 @@ struct NvVideoDecoder::Impl bool decode_one_nv12( const uint8_t* packet, std::size_t packet_size, uintptr_t nv12_out, std::size_t nv12_row_bytes, int rotation) { - std::lock_guard lock(decoder_mutex); if (!decoder) { throw std::runtime_error("NvVideoDecoder: decoder unavailable"); @@ -270,37 +252,22 @@ struct NvVideoDecoder::Impl return false; } - cudaError_t sync_err = cudaSuccess; - try + auto* dst_y = reinterpret_cast(nv12_out); + auto* dst_uv = dst_y + nv12_row_bytes * static_cast(h); + if (rotation == 180) { - if (decoder->GetOutputFormat() != cudaVideoSurfaceFormat_NV12) - throw std::runtime_error("NvVideoDecoder: only 8-bit NV12 decoded surfaces are supported"); - auto* dst_y = reinterpret_cast(nv12_out); - auto* dst_uv = dst_y + nv12_row_bytes * static_cast(h); - if (rotation == 180) - { - launch_nv12_rotate_180(nv12, nv12 + luma_size, src_pitch, src_pitch, w, h, dst_y, - static_cast(nv12_row_bytes), stream); - } - else - { - check_rt( - cudaMemcpy2DAsync(dst_y, nv12_row_bytes, nv12, src_pitch, w, h, cudaMemcpyDeviceToDevice, stream), - "cudaMemcpy2DAsync NV12 Y"); - check_rt(cudaMemcpy2DAsync(dst_uv, nv12_row_bytes, nv12 + luma_size, src_pitch, w, h / 2, - cudaMemcpyDeviceToDevice, stream), - "cudaMemcpy2DAsync NV12 UV"); - } - sync_err = cudaStreamSynchronize(stream); + launch_nv12_rotate_180( + nv12, nv12 + luma_size, src_pitch, src_pitch, w, h, dst_y, static_cast(nv12_row_bytes), stream); } - catch (...) + else { - // The Y copy may already be queued when the UV copy fails. - cudaStreamSynchronize(stream); - decoder->UnlockFrame(&nv12); - cuCtxPopCurrent(nullptr); - throw; + check_rt(cudaMemcpy2DAsync(dst_y, nv12_row_bytes, nv12, src_pitch, w, h, cudaMemcpyDeviceToDevice, stream), + "cudaMemcpy2DAsync NV12 Y"); + check_rt(cudaMemcpy2DAsync(dst_uv, nv12_row_bytes, nv12 + luma_size, src_pitch, w, h / 2, + cudaMemcpyDeviceToDevice, stream), + "cudaMemcpy2DAsync NV12 UV"); } + const cudaError_t sync_err = cudaStreamSynchronize(stream); decoder->UnlockFrame(&nv12); check_cu(cuCtxPopCurrent(nullptr), "cuCtxPopCurrent"); if (sync_err != cudaSuccess) @@ -314,7 +281,8 @@ struct NvVideoDecoder::Impl void reset_decoder() { - std::lock_guard lock(decoder_mutex); + if (!decoder) + return; check_cu(cuCtxPushCurrent(cu_context), "cuCtxPushCurrent"); decoder.reset(); try @@ -338,16 +306,6 @@ NvVideoDecoder::NvVideoDecoder(const DecoderConfig& cfg) : impl_(std::make_uniqu NvVideoDecoder::~NvVideoDecoder() = default; -uint32_t NvVideoDecoder::width() const -{ - return impl_->cfg.width; -} - -uint32_t NvVideoDecoder::height() const -{ - return impl_->cfg.height; -} - bool NvVideoDecoder::decode(const uint8_t* packet, std::size_t packet_size, uintptr_t rgba_out_device_ptr, diff --git a/examples/camera_streamer/camera_receiver/configs/receiver.yaml b/examples/camera_streamer/camera_receiver/configs/receiver.yaml new file mode 100644 index 0000000000..aedbf0839f --- /dev/null +++ b/examples/camera_streamer/camera_receiver/configs/receiver.yaml @@ -0,0 +1,58 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Standalone AVS1 receiver for argus_sender multi-stream bundles. +# Override receiver.host with --host or CAMERA_RECEIVER_HOST. + +version: 1 + +receiver: + host: 127.0.0.1 + port: 5000 + # Explicit CUDA device. Use null or omit this field to use the current device. + gpu_id: 0 + max_record_bytes: 268435456 + # Seconds between periodic decode/copy and IPC publish summaries. 0 disables + # only these recurring stats; startup, connection, warning, and error logs remain. + stats_log_interval_s: 0 + # Warn separately when any configured logical AVS stream is received below + # this FPS over the warning interval. Set the threshold to 0 to disable. + receive_fps_warning_threshold: 58.0 + receive_fps_warning_interval_s: 10.0 + +ipc: + socket_path: /tmp/camera_receiver_avs1_cuda_ipc.sock + owner_name: camera_receiver_avs1 + queue_size: 1024 + +streams: + - index: 0 + name: head + type: stereo + width: 2560 + height: 1984 + output_format: nv12 + ring_depth: 6 + ipc: + left: head.left + right: head.right + + - index: 1 + name: wrist1 + type: mono + width: 640 + height: 480 + output_format: nv12 + ring_depth: 6 + ipc: + mono: wrist1 + + - index: 2 + name: wrist2 + type: mono + width: 640 + height: 480 + output_format: nv12 + ring_depth: 6 + ipc: + mono: wrist2 diff --git a/examples/camera_streamer/camera_receiver/cuda_ipc.py b/examples/camera_streamer/camera_receiver/cuda_ipc.py index d75f0e9ed3..40b1bffea6 100644 --- a/examples/camera_streamer/camera_receiver/cuda_ipc.py +++ b/examples/camera_streamer/camera_receiver/cuda_ipc.py @@ -2,14 +2,12 @@ # SPDX-License-Identifier: Apache-2.0 """CUDA IPC frame publisher for camera_receiver. -Protocol v3 uses a newline-delimited JSON control socket: +Protocol v2 uses a newline-delimited JSON control socket: 1. Publisher -> client: ``hello`` stream catalog, no CUDA handles yet. 2. Client -> publisher: ``subscribe`` with stream names, or omitted/empty for all. 3. Publisher -> client: ``subscribed`` with CUDA IPC handles for selected streams. 4. Publisher -> client: ``frame`` notifications for selected streams only. -5. Client locks the slot's guard file, checks its generation, and completes GPU - reads before unlocking. Stale notifications are discarded. Image bytes stay in same-host CUDA IPC memory. The socket only carries control messages and per-frame metadata. @@ -25,14 +23,11 @@ import signal import socket import threading -import tempfile from dataclasses import dataclass from pathlib import Path from typing import Any, Optional -from slot_guard import commit_slot, read_slot, write_slot - -logger = logging.getLogger("isaaccapture.camera_streamer." + __name__) +logger = logging.getLogger("isaaccapture.camera_streamer.ipc") _CLIENT_SEND_TIMEOUT_S = 2.0 _CLIENT_SUBSCRIBE_TIMEOUT_S = 5.0 @@ -47,7 +42,6 @@ class _SlotInfo: nbytes: int shape: tuple[int, ...] strides: tuple[int, ...] - guard_path: str @dataclass(frozen=True) @@ -88,7 +82,6 @@ def __init__( self._accept_thread: Optional[threading.Thread] = None self._broadcast_thread: Optional[threading.Thread] = None self._started = False - self._guard_dir = tempfile.TemporaryDirectory(prefix="camera-ipc-slots-") @property def socket_path(self) -> str: @@ -126,14 +119,9 @@ def register_stream( raise TypeError( f"CUDA IPC stream {name!r} slot {idx} has no strides" ) - guard_path = str( - Path(self._guard_dir.name) / f"{len(self._streams)}-{idx}" - ) - Path(guard_path).write_bytes(bytes(8)) slots.append( _SlotInfo( slot=idx, - guard_path=guard_path, handle_b64=base64.b64encode(handle).decode("ascii"), nbytes=int(buf.nbytes), shape=tuple(int(v) for v in iface["shape"]), @@ -195,8 +183,8 @@ def stop(self) -> None: if self._server is not None: try: self._server.close() - except OSError: - logger.debug("CUDA IPC: socket close failed", exc_info=True) + except Exception: + pass self._server = None with self._lock: clients = list(self._clients) @@ -205,8 +193,8 @@ def stop(self) -> None: for client in clients: try: client.sock.close() - except OSError: - logger.debug("CUDA IPC: socket close failed", exc_info=True) + except Exception: + pass for thread in (self._accept_thread, self._broadcast_thread): if thread is not None: thread.join(timeout=2.0) @@ -220,15 +208,6 @@ def stop(self) -> None: "CUDA IPC: failed to unlink %s", self._socket_path, exc_info=True ) - def write_slot(self, stream_name: str, slot: int, stop: threading.Event): - return write_slot(self._streams[stream_name].slots[slot].guard_path, stop) - - def _current_frame(self, msg): - return read_slot( - self._streams[msg["stream"]].slots[msg["slot"]].guard_path, - msg["slot_generation"], - ) - def publish( self, stream_name: str, @@ -240,7 +219,6 @@ def publish( ) -> None: if not self._started: return - commit_slot(self._streams[stream_name].slots[slot].guard_path, slot_generation) msg = { "type": "frame", "stream": stream_name, @@ -257,7 +235,6 @@ def publish( try: self._queue.get_nowait() except queue.Empty: - # The broadcaster already drained the notification queue. pass try: self._queue.put_nowait(msg) @@ -284,7 +261,6 @@ def _open_server(self) -> None: srv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) srv.bind(self._socket_path) - os.chmod(self._socket_path, 0o600) srv.listen(8) srv.settimeout(0.5) self._server = srv @@ -306,7 +282,6 @@ def _stream_with_slots(self, stream: _StreamInfo) -> dict[str, Any]: msg["slots"] = [ { "slot": slot.slot, - "guard_path": slot.guard_path, "handle": slot.handle_b64, "nbytes": slot.nbytes, "shape": list(slot.shape), @@ -321,10 +296,9 @@ def _hello_message(self) -> dict[str, Any]: streams = list(self._streams.values()) return { "type": "hello", - "version": 3, + "version": 2, "features": [ "subscriptions", - "slot_read_locks", "frame_metadata", "avs1_metadata", "latency_metadata", @@ -349,7 +323,7 @@ def _subscribed_message( return ( { "type": "subscribed", - "version": 3, + "version": 2, "streams": [ self._stream_with_slots(all_streams[name]) for name in selected_names @@ -374,10 +348,6 @@ def _accept_loop(self) -> None: subscribe = self._recv_json(client) if subscribe.get("type") != "subscribe": raise ValueError(f"expected subscribe message, got {subscribe!r}") - if subscribe.get("version") != 3: - raise ValueError( - "CUDA IPC requires protocol 3 slot read locks; update the consumer" - ) requested_raw = subscribe.get("streams") or [] if not isinstance(requested_raw, list): raise ValueError("subscribe.streams must be a list") @@ -389,10 +359,8 @@ def _accept_loop(self) -> None: logger.info("CUDA IPC: rejecting consumer (%s)", exc) try: self._send_json(client, {"type": "error", "error": str(exc)}) - except OSError: - logger.debug( - "CUDA IPC: cannot notify rejected client", exc_info=True - ) + except Exception: + pass client.close() continue with self._lock: @@ -408,9 +376,6 @@ def _broadcast_loop(self) -> None: msg = self._queue.get(timeout=0.5) except queue.Empty: continue - with self._current_frame(msg) as current: - if not current: - continue stream_name = str(msg.get("stream", "")) with self._lock: clients = list(self._clients) @@ -436,8 +401,8 @@ def _broadcast_loop(self) -> None: for client in dead: try: client.sock.close() - except OSError: - logger.debug("CUDA IPC: client close failed", exc_info=True) + except Exception: + pass @staticmethod def _send_json(sock: socket.socket, msg: dict[str, Any]) -> None: @@ -468,5 +433,4 @@ def _ignore_sigpipe() -> None: try: signal.signal(sigpipe, signal.SIG_IGN) except (ValueError, OSError): - # Worker threads cannot change signal handlers; sends use MSG_NOSIGNAL. pass diff --git a/examples/camera_streamer/camera_receiver/nv_video_decode.py b/examples/camera_streamer/camera_receiver/nv_video_decode.py index 60b9627aa7..6da2aa9500 100644 --- a/examples/camera_streamer/camera_receiver/nv_video_decode.py +++ b/examples/camera_streamer/camera_receiver/nv_video_decode.py @@ -6,7 +6,7 @@ import logging -logger = logging.getLogger("isaaccapture.camera_streamer." + __name__) +logger = logging.getLogger("isaaccapture.camera_streamer.decode") class NvVideoDecoder: @@ -45,8 +45,8 @@ def _ensure_initialized(self) -> None: import codec except ImportError as e: raise RuntimeError( - "NvVideoDecoder requires the native codec. Follow " - "the host receiver build instructions in examples/camera_streamer/README.md." + "NvVideoDecoder requires the native codec. Run " + "`codec/build.sh` from the camera_receiver directory." ) from e cfg = codec.DecoderConfig() @@ -59,7 +59,7 @@ def _ensure_initialized(self) -> None: except AttributeError as e: raise RuntimeError( "The native codec module is stale (no Codec enum). Rebuild it " - "using the host receiver build instructions in examples/camera_streamer/README.md." + "with `codec/build.sh` from the camera_receiver directory." ) from e self._decoder = codec.NvVideoDecoder(cfg) diff --git a/examples/camera_streamer/camera_receiver/run.sh b/examples/camera_streamer/camera_receiver/run.sh new file mode 100755 index 0000000000..61ad916ea7 --- /dev/null +++ b/examples/camera_streamer/camera_receiver/run.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [[ -n "${CAMERA_RECEIVER_PYTHON:-}" ]]; then + PYTHON="$CAMERA_RECEIVER_PYTHON" +elif [[ -x "${HERE}/.venv/bin/python" ]]; then + PYTHON="${HERE}/.venv/bin/python" +else + echo "run.sh: no camera_receiver venv found. Run ./setup.sh first." >&2 + echo "run.sh: or set CAMERA_RECEIVER_PYTHON=/path/to/python" >&2 + exit 1 +fi + +if [[ $# -gt 0 && "$1" != -* ]]; then + CONFIG="$1" + shift +else + CONFIG="${HERE}/configs/receiver.yaml" +fi + +exec "$PYTHON" "${HERE}/camera_receiver.py" --config "$CONFIG" "$@" diff --git a/examples/camera_streamer/camera_receiver/setup.sh b/examples/camera_streamer/camera_receiver/setup.sh new file mode 100755 index 0000000000..b6a71c7372 --- /dev/null +++ b/examples/camera_streamer/camera_receiver/setup.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Create camera_receiver's local Python environment and build its local decoder. +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VENV="${CAMERA_RECEIVER_VENV:-${HERE}/.venv}" +BOOTSTRAP_PYTHON="${CAMERA_RECEIVER_BOOTSTRAP_PYTHON:-${PYTHON:-python3}}" + +if ! command -v "$BOOTSTRAP_PYTHON" >/dev/null 2>&1; then + echo "setup.sh: Python not found: $BOOTSTRAP_PYTHON" >&2 + echo "setup.sh: set CAMERA_RECEIVER_BOOTSTRAP_PYTHON=/path/to/python3" >&2 + exit 1 +fi + +if ! command -v cmake >/dev/null 2>&1; then + echo "setup.sh: cmake is required to build codec/" >&2 + exit 1 +fi + +if [[ -z "${CUDACXX:-}" && ! -x /usr/local/cuda/bin/nvcc ]] && ! command -v nvcc >/dev/null 2>&1; then + echo "setup.sh: nvcc not found. Install CUDA toolkit or set CUDACXX=/path/to/nvcc" >&2 + exit 1 +fi + +if [[ ! -x "${VENV}/bin/python" ]]; then + echo "setup.sh: creating venv at ${VENV}" + if command -v uv >/dev/null 2>&1; then + uv venv "${VENV}" --python "$BOOTSTRAP_PYTHON" + else + "$BOOTSTRAP_PYTHON" -m venv "${VENV}" + fi +fi + +PYTHON_EXE="${VENV}/bin/python" +export VIRTUAL_ENV="$VENV" +export PATH="${VENV}/bin:${PATH}" + +echo "setup.sh: installing Python dependencies" +if command -v uv >/dev/null 2>&1; then + uv pip install --python "$PYTHON_EXE" -r "${HERE}/requirements.txt" + uv pip check --python "$PYTHON_EXE" +else + "$PYTHON_EXE" -m pip install --upgrade pip + "$PYTHON_EXE" -m pip install -r "${HERE}/requirements.txt" + "$PYTHON_EXE" -m pip check +fi + +echo "setup.sh: verifying NumPy/CuPy runtime" +"$PYTHON_EXE" - <<'PY' +import cupy as cp +import numpy as np + +device_count = cp.cuda.runtime.getDeviceCount() +if device_count < 1: + raise RuntimeError("camera_receiver requires at least one CUDA device") +probe = cp.cuda.alloc(1) +cp.cuda.runtime.memset(probe.ptr, 0, 1) +cp.cuda.runtime.deviceSynchronize() +print( + f"setup.sh: NumPy {np.__version__}; CuPy {cp.__version__}; " + f"CUDA devices={device_count}" +) +PY + +export CAMERA_RECEIVER_VENV="$VENV" +export CAMERA_RECEIVER_PYTHON="$PYTHON_EXE" + +if [[ "${CAMERA_RECEIVER_CLEAN_BUILD:-1}" != "0" ]]; then + echo "setup.sh: cleaning codec/build" + rm -rf "${HERE}/codec/build" +fi + +echo "setup.sh: building native decoder" +"${HERE}/codec/build.sh" + +echo "setup.sh: verifying receiver config" +"$PYTHON_EXE" "${HERE}/camera_receiver.py" \ + --config "${HERE}/configs/receiver.yaml" \ + --dry-run + +echo +echo "setup.sh: ready" +echo "setup.sh: run with ./run.sh" diff --git a/examples/camera_streamer/camera_receiver/slot_guard.py b/examples/camera_streamer/camera_receiver/slot_guard.py deleted file mode 100644 index 97ff93295c..0000000000 --- a/examples/camera_streamer/camera_receiver/slot_guard.py +++ /dev/null @@ -1,52 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -"""Cross-process slot ownership; locks cover GPU work, not just submission.""" - -import fcntl -import os -import struct -from contextlib import contextmanager - -_GENERATION = struct.Struct("!Q") - - -@contextmanager -def write_slot(path, stop): - """Invalidate old notifications before writing; wait interruptibly for readers.""" - with open(path, "r+b", buffering=0) as guard: - while True: - if stop.is_set(): - raise InterruptedError("CUDA IPC slot write cancelled") - try: - fcntl.flock(guard, fcntl.LOCK_EX | fcntl.LOCK_NB) - break - except BlockingIOError: - stop.wait(0.01) - guard.write(_GENERATION.pack(0)) - yield - - -def commit_slot(path, generation): - if generation <= 0: - raise ValueError("slot generation must be positive") - with open(path, "r+b", buffering=0) as guard: - fcntl.flock(guard, fcntl.LOCK_EX) - guard.write(_GENERATION.pack(generation)) - - -@contextmanager -def read_slot(path, generation): - """Yield False for stale/busy slots; hold a shared lock through a valid read.""" - try: - guard = open(path, "rb", buffering=0) - except FileNotFoundError: - yield False - return - with guard: - try: - fcntl.flock(guard, fcntl.LOCK_SH | fcntl.LOCK_NB) - except BlockingIOError: - yield False - return - current = os.pread(guard.fileno(), _GENERATION.size, 0) - yield generation > 0 and current == _GENERATION.pack(generation) diff --git a/examples/camera_streamer/tests/python/conftest.py b/examples/camera_streamer/camera_receiver/tests/conftest.py similarity index 54% rename from examples/camera_streamer/tests/python/conftest.py rename to examples/camera_streamer/camera_receiver/tests/conftest.py index f2adb71797..e4da3573b3 100644 --- a/examples/camera_streamer/tests/python/conftest.py +++ b/examples/camera_streamer/camera_receiver/tests/conftest.py @@ -1,7 +1,8 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -import sys + from pathlib import Path +import sys -# The receiver is also supported as a standalone script directory. -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "camera_receiver")) +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) diff --git a/examples/camera_streamer/tests/python/test_nv_video_decode.py b/examples/camera_streamer/camera_receiver/tests/test_nv_video_decode.py similarity index 100% rename from examples/camera_streamer/tests/python/test_nv_video_decode.py rename to examples/camera_streamer/camera_receiver/tests/test_nv_video_decode.py diff --git a/examples/camera_streamer/tests/python/test_reconnect.py b/examples/camera_streamer/camera_receiver/tests/test_reconnect.py similarity index 96% rename from examples/camera_streamer/tests/python/test_reconnect.py rename to examples/camera_streamer/camera_receiver/tests/test_reconnect.py index 2be07e8ff4..c3d3d1867b 100755 --- a/examples/camera_streamer/tests/python/test_reconnect.py +++ b/examples/camera_streamer/camera_receiver/tests/test_reconnect.py @@ -18,7 +18,7 @@ from pathlib import Path from unittest.mock import patch -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "camera_receiver")) +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import camera_receiver # noqa: E402 from camera_receiver import Avs1TcpReceiver # noqa: E402 @@ -30,7 +30,7 @@ def _bare_receiver() -> Avs1TcpReceiver: recv = Avs1TcpReceiver.__new__(Avs1TcpReceiver) recv._stop = threading.Event() recv._receive_fps_monitor = None - recv._host = "6.6.7.100" + recv._host = "127.0.0.1" recv._port = 5000 recv._link_warm = True # warmed stream by default; cold-start tests flip this return recv @@ -107,7 +107,6 @@ def test_cold_start_does_not_give_up_before_first_bundle(self): recv._link_warm = False # no bundle received yet # times out a few times (~4s elapsed, past _STALL_TIMEOUT_S=3 but under # _COLD_START_TIMEOUT_S=15), then the first bytes arrive. - _DataThenBlockSocket([]) # will raise timeout first class _WarmupSock: def __init__(self): diff --git a/examples/camera_streamer/tests/python/test_wire_format.py b/examples/camera_streamer/camera_receiver/tests/test_wire_format.py similarity index 99% rename from examples/camera_streamer/tests/python/test_wire_format.py rename to examples/camera_streamer/camera_receiver/tests/test_wire_format.py index 4d3f3e047b..21755df14c 100755 --- a/examples/camera_streamer/tests/python/test_wire_format.py +++ b/examples/camera_streamer/camera_receiver/tests/test_wire_format.py @@ -11,7 +11,7 @@ from typing import Optional from unittest.mock import patch -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "camera_receiver")) +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from avs_wire import ( # noqa: E402 BUNDLE_SENDER_CLOCK_VALID, diff --git a/examples/camera_streamer/configs/s56-mono-1080p30.yaml b/examples/camera_streamer/configs/s56-mono-1080p30.yaml deleted file mode 100644 index eb8a2f51e6..0000000000 --- a/examples/camera_streamer/configs/s56-mono-1080p30.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 0 - output: - width: 1920 - height: 1080 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 24.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-mono-1080p60.yaml b/examples/camera_streamer/configs/s56-mono-1080p60.yaml deleted file mode 100644 index f919f9b023..0000000000 --- a/examples/camera_streamer/configs/s56-mono-1080p60.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 0 - output: - width: 1920 - height: 1080 - fps: 60 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 60 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 48.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-mono-2400x1440-30.yaml b/examples/camera_streamer/configs/s56-mono-2400x1440-30.yaml deleted file mode 100644 index 68e6049141..0000000000 --- a/examples/camera_streamer/configs/s56-mono-2400x1440-30.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 1 - output: - width: 2400 - height: 1440 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: center_crop - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 24.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-mono-2560x1984-30.yaml b/examples/camera_streamer/configs/s56-mono-2560x1984-30.yaml deleted file mode 100644 index 7ea8622fb8..0000000000 --- a/examples/camera_streamer/configs/s56-mono-2560x1984-30.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 1 - output: - width: 2560 - height: 1984 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 24.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-mono-720p30.yaml b/examples/camera_streamer/configs/s56-mono-720p30.yaml deleted file mode 100644 index bb1b19ddd5..0000000000 --- a/examples/camera_streamer/configs/s56-mono-720p30.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 0 - output: - width: 1280 - height: 720 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 24.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-mono-720p60.yaml b/examples/camera_streamer/configs/s56-mono-720p60.yaml deleted file mode 100644 index 49093fcfb4..0000000000 --- a/examples/camera_streamer/configs/s56-mono-720p60.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: 0 - output: - width: 1280 - height: 720 - fps: 60 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 60 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 48.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-stereo-1080p60.yaml b/examples/camera_streamer/configs/s56-stereo-1080p60.yaml deleted file mode 100644 index 7ea6950a36..0000000000 --- a/examples/camera_streamer/configs/s56-stereo-1080p60.yaml +++ /dev/null @@ -1,48 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: stereo - cameras: - - device: 0 - mode: 0 - - device: 1 - mode: 0 - output: - width: 1920 - height: 1080 - fps: 60 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 60 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - left: head.left - right: head.right -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 48.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/s56-stereo-2560x1984-30.yaml b/examples/camera_streamer/configs/s56-stereo-2560x1984-30.yaml deleted file mode 100644 index 198861ba14..0000000000 --- a/examples/camera_streamer/configs/s56-stereo-2560x1984-30.yaml +++ /dev/null @@ -1,48 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: stereo - cameras: - - device: 0 - mode: 1 - - device: 1 - mode: 1 - output: - width: 2560 - height: 1984 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - left: head.left - right: head.right -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 25 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/shw5g-mono-2400x1440-60.yaml b/examples/camera_streamer/configs/shw5g-mono-2400x1440-60.yaml deleted file mode 100644 index e1528b72c3..0000000000 --- a/examples/camera_streamer/configs/shw5g-mono-2400x1440-60.yaml +++ /dev/null @@ -1,47 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -# Replace mode using this SHW5G driver's --list-modes output. -# Sensor modes are platform-specific; this profile intentionally requires editing. -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: SET_FROM_LIST_MODES - output: - width: 2400 - height: 1440 - fps: 60 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 60 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 48.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/configs/shw5g-mono-2560x1984-30.yaml b/examples/camera_streamer/configs/shw5g-mono-2560x1984-30.yaml deleted file mode 100644 index 4284943abc..0000000000 --- a/examples/camera_streamer/configs/shw5g-mono-2560x1984-30.yaml +++ /dev/null @@ -1,47 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -# Replace mode using this SHW5G driver's --list-modes output. -# Sensor modes are platform-specific; this profile intentionally requires editing. -version: 1 -network: - bind: 0.0.0.0 - port: 5000 - reconnect_ms: 1000 -runtime: - frames: 0 - kpi: true - log_interval_s: 5 -alignment: - warmup_frames: 30 - interval_frames: 30 -streams: -- id: head - type: mono - cameras: - - device: 0 - mode: SET_FROM_LIST_MODES - output: - width: 2560 - height: 1984 - fps: 30 - codec: hevc - rotation: 0 - resize_mode: strict - bitrate_mbps: 30 - gop: 30 - receiver: - output_format: nv12 - ring_depth: 6 - ipc: - mono: head -receiver: - host: 127.0.0.1 - gpu_id: 0 - max_record_bytes: 268435456 - stats_log_interval_s: 5 - receive_fps_warning_threshold: 24.0 - receive_fps_warning_interval_s: 10 -ipc: - socket_path: /tmp/isaac-camera-streamer.sock - owner_name: isaac_camera_streamer - queue_size: 64 diff --git a/examples/camera_streamer/cuda_ipc_consumer/.gitignore b/examples/camera_streamer/cuda_ipc_consumer/.gitignore index 13c4cad133..4b8ba00621 100644 --- a/examples/camera_streamer/cuda_ipc_consumer/.gitignore +++ b/examples/camera_streamer/cuda_ipc_consumer/.gitignore @@ -1,6 +1,3 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - # Python environment and caches .venv/ __pycache__/ diff --git a/examples/camera_streamer/cuda_ipc_consumer/README.md b/examples/camera_streamer/cuda_ipc_consumer/README.md new file mode 100644 index 0000000000..9d79f5fffc --- /dev/null +++ b/examples/camera_streamer/cuda_ipc_consumer/README.md @@ -0,0 +1,114 @@ + + +# camera_receiver CUDA IPC consumer + +This example connects to `camera_receiver`'s AVS1 CUDA IPC socket, subscribes to +selected streams, opens the shared GPU buffers with CuPy, and optionally +displays them with OpenCV. + +## Setup + +Set up `camera_receiver` separately from its directory: + +```bash +cd ../camera_receiver +./setup.sh +``` + +Set up this consumer from this directory: + +```bash +cd ../cuda_ipc_consumer +./setup.sh +``` + +Each directory has its own `.venv`; the consumer does not use the +`camera_receiver` Python environment. + +## Run + +Start the receiver: + +```bash +cd ../camera_receiver +./run.sh --host --port 5000 +``` + +Run the consumer from this directory: + +```bash +./run.sh --scale 0.35 +``` + +Subscribe to only a subset of streams: + +```bash +./run.sh --streams head.left,head.right --scale 0.35 +``` + +For a headless smoke test: + +```bash +./run.sh --no-display --frames 120 +``` + +To collect per-frame latency metadata as CSV while still printing the normal +one-second stats: + +```bash +./run.sh --no-display --latency-csv /tmp/camera_latency.csv +``` + +For a CSV-only latency capture: + +```bash +.venv/bin/python latency_check.py --streams head.left --output /tmp/camera_latency.csv +``` + +The default CUDA IPC socket is: + +```text +/tmp/camera_receiver_avs1_cuda_ipc.sock +``` + +Override it with: + +```bash +./run.sh --socket /tmp/camera_receiver_avs1_cuda_ipc.sock +``` + +The shared buffers are GPU-resident `uint8` arrays. Supported layouts are: + +```text +rgba8: (height, width, 4) +nv12: ((height * 3) / 2, width) +``` + +The example copies frames to host memory only for OpenCV display. CUDA-capable +consumers can run kernels directly on the opened CuPy arrays. Frame +notifications include `slot_generation`; consumers can detect that a slot was +overwritten if a generation value skips. + +Frame notifications include AVS1 metadata from the sender, including +`bundle_sequence`, `stream_sequence`, `frame_number`, SOF TSC values, sender +timing fields, receiver timing fields, and rolling latency values when the +sender and receiver clocks are comparable. `sof_delta` is signed +`right_sof_tsc_ns - left_sof_tsc_ns` for stereo streams in the sender's Tegra +TSC clock domain. + +## Overrides + +Use a different Python interpreter: + +```bash +CUDA_IPC_CONSUMER_PYTHON=/path/to/python ./run.sh +``` + +Use a different bootstrap Python when creating the consumer venv: + +```bash +CUDA_IPC_CONSUMER_BOOTSTRAP_PYTHON=/path/to/python3 ./setup.sh +``` diff --git a/examples/camera_streamer/cuda_ipc_consumer/consumer.py b/examples/camera_streamer/cuda_ipc_consumer/consumer.py index b5c6a4645f..f14e261a76 100755 --- a/examples/camera_streamer/cuda_ipc_consumer/consumer.py +++ b/examples/camera_streamer/cuda_ipc_consumer/consumer.py @@ -12,27 +12,17 @@ import select import signal import socket -import sys import time -from contextlib import contextmanager from dataclasses import dataclass -from pathlib import Path from typing import Optional -# Support direct execution without installing the example as a package. -sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "camera_receiver")) -from slot_guard import read_slot # noqa: E402 - - @dataclass class IpcSlot: stream: str slot: int array: object owner: "IpcMemory" - guard_path: str - gpu_id: int class IpcMemory: @@ -52,7 +42,6 @@ def __del__(self) -> None: try: self.close() except Exception: - # CUDA/Python modules may already be torn down during finalization. pass @@ -68,66 +57,34 @@ def __init__(self, socket_path: str, streams: Optional[list[str]] = None) -> Non def connect(self) -> dict: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(self.socket_path) self.sock = sock - try: - sock.connect(self.socket_path) - hello = self._read_json_blocking() - if hello is None or hello.get("type") != "hello": - raise RuntimeError(f"expected hello message, got {hello!r}") - if hello.get("version") != 3: - raise RuntimeError( - "CUDA IPC requires protocol 3 slot read locks; update the receiver" - ) - self._send_json( - {"type": "subscribe", "version": 3, "streams": self.requested_streams} - ) - subscribed = self._read_json_blocking() - if subscribed is None: - raise RuntimeError("publisher disconnected before subscription") - if subscribed.get("type") == "error": - raise RuntimeError(str(subscribed.get("error", "subscription failed"))) - if subscribed.get("type") != "subscribed": - raise RuntimeError(f"expected subscribed message, got {subscribed!r}") - self._open_streams(subscribed) - sock.setblocking(False) - return {"hello": hello, "subscribed": subscribed} - except Exception: - self.close() - raise + hello = self._read_json_blocking() + if hello is None or hello.get("type") != "hello": + raise RuntimeError(f"expected hello message, got {hello!r}") + self._send_json({"type": "subscribe", "streams": self.requested_streams}) + subscribed = self._read_json_blocking() + if subscribed is None: + raise RuntimeError("publisher disconnected before subscription") + if subscribed.get("type") == "error": + raise RuntimeError(str(subscribed.get("error", "subscription failed"))) + if subscribed.get("type") != "subscribed": + raise RuntimeError(f"expected subscribed message, got {subscribed!r}") + self._open_streams(subscribed) + sock.setblocking(False) + return {"hello": hello, "subscribed": subscribed} def close(self) -> None: if self.sock is not None: try: self.sock.close() - except OSError: - # A socket close failure must not prevent releasing CUDA mappings. + except Exception: pass self.sock = None for slot in self.slots.values(): slot.owner.close() self.slots.clear() self.recv_buf.clear() - self.stream_order.clear() - self.stream_info.clear() - - @contextmanager - def read_frame(self, msg): - """Hold ownership until all GPU reads finish; yield None for stale frames.""" - import cupy as cp - - slot = self.slots.get((str(msg["stream"]), int(msg["slot"]))) - if slot is None: - yield None - return - with read_slot(slot.guard_path, int(msg["slot_generation"])) as valid: - if not valid: - yield None - return - with cp.cuda.Device(slot.gpu_id): - try: - yield slot.array - finally: - cp.cuda.get_current_stream().synchronize() def read_messages(self, timeout_s: float = 0.05) -> Optional[list[dict]]: """Return all currently available messages, or None on socket EOF.""" @@ -197,27 +154,17 @@ def _open_streams(self, subscribed: dict) -> None: handle, cp.cuda.runtime.cudaIpcMemLazyEnablePeerAccess ) owner = IpcMemory(ptr) - try: - mem = cp.cuda.UnownedMemory(ptr, int(slot["nbytes"]), owner) - memptr = cp.cuda.MemoryPointer(mem, 0) - arr = cp.ndarray( - tuple(slot["shape"]), - dtype=cp.uint8, - memptr=memptr, - strides=tuple(slot["strides"]), - ) - self.slots[(name, int(slot["slot"]))] = IpcSlot( - stream=name, - slot=int(slot["slot"]), - array=arr, - owner=owner, - guard_path=str(slot["guard_path"]), - gpu_id=gpu_id, - ) - except Exception: - # A mapping is owned before its array can be registered in slots. - owner.close() - raise + mem = cp.cuda.UnownedMemory(ptr, int(slot["nbytes"]), owner) + memptr = cp.cuda.MemoryPointer(mem, 0) + arr = cp.ndarray( + tuple(slot["shape"]), + dtype=cp.uint8, + memptr=memptr, + strides=tuple(slot["strides"]), + ) + self.slots[(name, int(slot["slot"]))] = IpcSlot( + stream=name, slot=int(slot["slot"]), array=arr, owner=owner + ) def _to_bgr_cpu(array, stream_info: dict): @@ -563,25 +510,7 @@ def on_sigint(_signum, _frame): csv_writer.writeheader() try: - if cv2 is not None: - cv2.namedWindow(args.window_name, cv2.WINDOW_NORMAL) - print( - "Close a preview window, press 'q' or Esc in the preview, or Ctrl+C in this terminal to quit.", - flush=True, - ) while not stop: - if cv2 is not None: - # Process window-close events before displaying another frame. - if cv2.waitKey(1) & 0xFF in (ord("q"), 27): - break - try: - if ( - cv2.getWindowProperty(args.window_name, cv2.WND_PROP_VISIBLE) - < 1 - ): - break - except cv2.error: - break messages = client.read_messages() if messages is None: print("publisher disconnected", flush=True) @@ -621,15 +550,11 @@ def on_sigint(_signum, _frame): if slot is None: print(f"unknown frame slot: {stream}[{slot_id}]", flush=True) continue + updated_display = True if cv2 is not None: - with client.read_frame(msg) as array: - if array is None: - overwritten += 1 - continue - latest_cpu[stream] = _to_bgr_cpu( - array, client.stream_info.get(stream, {}) - ) - updated_display = True + latest_cpu[stream] = _to_bgr_cpu( + slot.array, client.stream_info.get(stream, {}) + ) if cv2 is None: for msg in sampled_messages: @@ -642,11 +567,7 @@ def on_sigint(_signum, _frame): metadata_text = _format_stream_metadata(msg.get("metadata")) mean_text = "" if args.sample_mean: - with client.read_frame(msg) as array: - if array is None: - overwritten += 1 - continue - mean = float(cp.mean(array).get()) + mean = float(cp.mean(slot.array).get()) mean_text = f" mean={mean:.2f}" print( f"{stream}[{slot_id}] gen={msg.get('slot_generation')} " @@ -665,6 +586,9 @@ def on_sigint(_signum, _frame): canvas = _compose_canvas(images) if canvas is not None: cv2.imshow(args.window_name, canvas) + key = cv2.waitKey(1) & 0xFF + if key in (ord("q"), 27): + break now = time.monotonic() if now - last_report >= 1.0: @@ -706,12 +630,7 @@ def main() -> int: "--streams", help="comma-separated IPC stream names to subscribe to; default subscribes to all", ) - ap.add_argument( - "--scale", - type=float, - default=1.0, - help="OpenCV display scale (default: 1.0, full resolution)", - ) + ap.add_argument("--scale", type=float, default=0.35, help="OpenCV display scale") ap.add_argument("--window-name", default="camera_receiver CUDA IPC") ap.add_argument("--no-display", action="store_true", help="print stats only") ap.add_argument( diff --git a/examples/camera_streamer/cuda_ipc_consumer/latency_check.py b/examples/camera_streamer/cuda_ipc_consumer/latency_check.py new file mode 100755 index 0000000000..c5e2bd3fed --- /dev/null +++ b/examples/camera_streamer/cuda_ipc_consumer/latency_check.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +"""Dump camera_receiver CUDA IPC frame latency metadata as CSV.""" + +from __future__ import annotations + +import argparse +import csv +import signal +import sys +from typing import Optional + +from consumer import ( + CudaIpcClient, + _LATENCY_CSV_FIELDS, + _annotate_consumer_receive, + _csv_row, + _parse_stream_filter, +) + + +def run(args: argparse.Namespace) -> int: + client = CudaIpcClient(args.socket, _parse_stream_filter(args.streams)) + stop = False + + def on_sigint(_signum, _frame): + nonlocal stop + stop = True + + signal.signal(signal.SIGINT, on_sigint) + + output = sys.stdout + close_output = False + if args.output != "-": + output = open(args.output, "w", newline="", encoding="utf-8") + close_output = True + + frames_seen = 0 + try: + handshake = client.connect() + subscribed = handshake["subscribed"] + print( + "connected to " + f"{args.socket}: " + + ", ".join( + f"{s['name']} {s['width']}x{s['height']} " + f"{s.get('pixel_format', 'unknown')} slots={len(s['slots'])}" + for s in subscribed.get("streams", []) + ), + file=sys.stderr, + flush=True, + ) + writer = csv.DictWriter(output, fieldnames=_LATENCY_CSV_FIELDS) + writer.writeheader() + output.flush() + + while not stop: + messages = client.read_messages(timeout_s=args.poll_timeout) + if messages is None: + print("publisher disconnected", file=sys.stderr, flush=True) + break + for msg in messages: + if msg.get("type") != "frame": + continue + msg = _annotate_consumer_receive(msg) + writer.writerow(_csv_row(msg)) + frames_seen += 1 + if args.frames > 0 and frames_seen >= args.frames: + return 0 + output.flush() + finally: + client.close() + if close_output: + output.close() + return 0 + + +def main(argv: Optional[list[str]] = None) -> int: + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument( + "--socket", + default="/tmp/camera_receiver_avs1_cuda_ipc.sock", + help="Unix socket path exposed by camera_receiver ipc.socket_path", + ) + ap.add_argument( + "--streams", + help="comma-separated IPC stream names to subscribe to; default subscribes to all", + ) + ap.add_argument( + "--output", + default="-", + help="CSV output path, or '-' for stdout", + ) + ap.add_argument( + "--frames", + type=int, + default=0, + help="exit after this many frame notifications; 0 means run until stopped", + ) + ap.add_argument("--poll-timeout", type=float, default=0.05) + return run(ap.parse_args(argv)) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/camera_streamer/cuda_ipc_consumer/run.sh b/examples/camera_streamer/cuda_ipc_consumer/run.sh new file mode 100755 index 0000000000..ff7d70c9fe --- /dev/null +++ b/examples/camera_streamer/cuda_ipc_consumer/run.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [[ -n "${CUDA_IPC_CONSUMER_PYTHON:-}" ]]; then + PYTHON="$CUDA_IPC_CONSUMER_PYTHON" +elif [[ -x "${HERE}/.venv/bin/python" ]]; then + PYTHON="${HERE}/.venv/bin/python" +else + echo "run.sh: no cuda_ipc_consumer venv found. Run ./setup.sh first." >&2 + echo "run.sh: or set CUDA_IPC_CONSUMER_PYTHON=/path/to/python" >&2 + exit 1 +fi + +exec "$PYTHON" "${HERE}/consumer.py" "$@" diff --git a/examples/camera_streamer/cuda_ipc_consumer/setup.sh b/examples/camera_streamer/cuda_ipc_consumer/setup.sh new file mode 100755 index 0000000000..82e16c5485 --- /dev/null +++ b/examples/camera_streamer/cuda_ipc_consumer/setup.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VENV="${CUDA_IPC_CONSUMER_VENV:-${HERE}/.venv}" +BOOTSTRAP_PYTHON="${CUDA_IPC_CONSUMER_BOOTSTRAP_PYTHON:-${PYTHON:-python3}}" + +if ! command -v "$BOOTSTRAP_PYTHON" >/dev/null 2>&1; then + echo "setup.sh: Python not found: $BOOTSTRAP_PYTHON" >&2 + echo "setup.sh: set CUDA_IPC_CONSUMER_BOOTSTRAP_PYTHON=/path/to/python3" >&2 + exit 1 +fi + +if [[ ! -x "${VENV}/bin/python" ]]; then + echo "setup.sh: creating venv at ${VENV}" + if command -v uv >/dev/null 2>&1; then + uv venv "${VENV}" --python "$BOOTSTRAP_PYTHON" + else + "$BOOTSTRAP_PYTHON" -m venv "${VENV}" + fi +fi + +PYTHON_EXE="${VENV}/bin/python" +export VIRTUAL_ENV="$VENV" +export PATH="${VENV}/bin:${PATH}" + +echo "setup.sh: installing Python dependencies" +if command -v uv >/dev/null 2>&1; then + uv pip install --python "$PYTHON_EXE" -r "${HERE}/requirements.txt" + echo "setup.sh: checking Python dependency compatibility" + uv pip check --python "$PYTHON_EXE" +else + if ! "$PYTHON_EXE" -m pip --version >/dev/null 2>&1; then + "$PYTHON_EXE" -m ensurepip --upgrade + fi + "$PYTHON_EXE" -m pip install --upgrade pip + "$PYTHON_EXE" -m pip install -r "${HERE}/requirements.txt" + echo "setup.sh: checking Python dependency compatibility" + "$PYTHON_EXE" -m pip check +fi + +echo "setup.sh: verifying imports" +"$PYTHON_EXE" - <<'PY' +import cupy +import cv2 +import numpy + +print(f"numpy={numpy.__version__}") +print(f"cupy={cupy.__version__}") +print(f"opencv={cv2.__version__}") +PY + +echo +echo "setup.sh: ready" +echo "setup.sh: run with ./run.sh" diff --git a/examples/camera_streamer/pyproject.toml b/examples/camera_streamer/pyproject.toml deleted file mode 100644 index 4639ce6b23..0000000000 --- a/examples/camera_streamer/pyproject.toml +++ /dev/null @@ -1,20 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -[build-system] -requires = ["setuptools>=68"] -build-backend = "setuptools.build_meta" - -[project] -name = "isaac-camera-streamer-example" -version = "0.1.0" -requires-python = ">=3.10" -dependencies = ["numpy>=1.24,<2", "PyYAML>=6", "opencv-python>=4.8"] - -[project.optional-dependencies] -receiver = ["cupy-cuda12x>=13,<14", "pybind11>=2.12", "ninja>=1.11"] -test = ["pytest>=8"] - -[tool.setuptools] -packages = ["camera_streamer"] -package-dir = {camera_streamer = "."} diff --git a/examples/camera_streamer/tests/cpp/alignment_policy/CMakeLists.txt b/examples/camera_streamer/tests/cpp/alignment_policy/CMakeLists.txt deleted file mode 100644 index 4948a438b2..0000000000 --- a/examples/camera_streamer/tests/cpp/alignment_policy/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_alignment_policy - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_alignment_policy.cpp - ${ARGUS_SENDER_ROOT}/src/StereoAlignmentPolicy.cpp - ${ARGUS_SENDER_ROOT}/src/StereoTiming.cpp -) -argus_sender_add_cpp_test(argus_sender_test_alignment_policy alignment_policy) diff --git a/examples/camera_streamer/tests/cpp/avs1_wire/CMakeLists.txt b/examples/camera_streamer/tests/cpp/avs1_wire/CMakeLists.txt deleted file mode 100644 index 62ff559bca..0000000000 --- a/examples/camera_streamer/tests/cpp/avs1_wire/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_avs1_wire - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_avs1_wire.cpp - ${ARGUS_SENDER_ROOT}/src/Avs1Wire.cpp -) -argus_sender_add_cpp_test(argus_sender_test_avs1_wire avs1_wire) diff --git a/examples/camera_streamer/tests/cpp/bundle_assembler/CMakeLists.txt b/examples/camera_streamer/tests/cpp/bundle_assembler/CMakeLists.txt deleted file mode 100644 index 460075ec9d..0000000000 --- a/examples/camera_streamer/tests/cpp/bundle_assembler/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_bundle_assembler - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_bundle_assembler.cpp - ${ARGUS_SENDER_ROOT}/src/FrameBundleAssembler.cpp - ${ARGUS_SENDER_ROOT}/src/Avs1Wire.cpp - ${ARGUS_SENDER_ROOT}/src/StereoTiming.cpp - ${ARGUS_SENDER_ROOT}/src/Runtime.cpp -) -argus_sender_add_cpp_test(argus_sender_test_bundle_assembler bundle_assembler) -target_compile_definitions(argus_sender_test_bundle_assembler PRIVATE ARGUS_SENDER_TEST_SUBMISSION_HOOK) diff --git a/examples/camera_streamer/tests/cpp/capture_domain_plan/CMakeLists.txt b/examples/camera_streamer/tests/cpp/capture_domain_plan/CMakeLists.txt deleted file mode 100644 index 913efccc8c..0000000000 --- a/examples/camera_streamer/tests/cpp/capture_domain_plan/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_capture_domain_plan - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_capture_domain_plan.cpp - ${ARGUS_SENDER_ROOT}/src/CaptureDomainPlan.cpp -) -argus_sender_add_cpp_test(argus_sender_test_capture_domain_plan capture_domain_plan) diff --git a/examples/camera_streamer/tests/cpp/capture_priority_policy/CMakeLists.txt b/examples/camera_streamer/tests/cpp/capture_priority_policy/CMakeLists.txt deleted file mode 100644 index abc235c9dd..0000000000 --- a/examples/camera_streamer/tests/cpp/capture_priority_policy/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_capture_priority_policy - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_capture_priority_policy.cpp - ${ARGUS_SENDER_ROOT}/src/CapturePriorityPolicy.cpp -) -argus_sender_add_cpp_test(argus_sender_test_capture_priority_policy capture_priority_policy) diff --git a/examples/camera_streamer/tests/cpp/config/CMakeLists.txt b/examples/camera_streamer/tests/cpp/config/CMakeLists.txt deleted file mode 100644 index 9b80ade13c..0000000000 --- a/examples/camera_streamer/tests/cpp/config/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_config - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_config.cpp - ${ARGUS_SENDER_ROOT}/src/Config.cpp -) -argus_sender_add_cpp_test(argus_sender_test_config config) -target_link_libraries(argus_sender_test_config PRIVATE yaml-cpp::yaml-cpp) -target_compile_definitions(argus_sender_test_config PRIVATE - CAMERA_STREAMER_CONFIG_DIR="${CAMERA_STREAMER_ROOT}/configs" -) diff --git a/examples/camera_streamer/tests/cpp/frame_alignment_policy/CMakeLists.txt b/examples/camera_streamer/tests/cpp/frame_alignment_policy/CMakeLists.txt deleted file mode 100644 index 154af9c368..0000000000 --- a/examples/camera_streamer/tests/cpp/frame_alignment_policy/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_frame_alignment_policy - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_frame_alignment_policy.cpp - ${ARGUS_SENDER_ROOT}/src/FrameAlignmentPolicy.cpp -) -argus_sender_add_cpp_test(argus_sender_test_frame_alignment_policy frame_alignment_policy) diff --git a/examples/camera_streamer/tests/cpp/geometry_scheduler/CMakeLists.txt b/examples/camera_streamer/tests/cpp/geometry_scheduler/CMakeLists.txt deleted file mode 100644 index 3dc4b95f08..0000000000 --- a/examples/camera_streamer/tests/cpp/geometry_scheduler/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_geometry_scheduler - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_geometry_scheduler.cpp - ${ARGUS_SENDER_ROOT}/src/OutputGeometry.cpp - ${ARGUS_SENDER_ROOT}/src/MixedRateScheduler.cpp -) -argus_sender_add_cpp_test(argus_sender_test_geometry_scheduler geometry_scheduler) diff --git a/examples/camera_streamer/tests/cpp/nv_remap/CMakeLists.txt b/examples/camera_streamer/tests/cpp/nv_remap/CMakeLists.txt deleted file mode 100644 index 7eadff47fe..0000000000 --- a/examples/camera_streamer/tests/cpp/nv_remap/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_nv_remap - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_nv_remap.cpp - ${ARGUS_SENDER_ROOT}/src/NvRemap.cpp -) -argus_sender_add_cpp_test(argus_sender_test_nv_remap nv_remap) diff --git a/examples/camera_streamer/tests/cpp/priority_frame_handoff/CMakeLists.txt b/examples/camera_streamer/tests/cpp/priority_frame_handoff/CMakeLists.txt deleted file mode 100644 index 67d3a4554b..0000000000 --- a/examples/camera_streamer/tests/cpp/priority_frame_handoff/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_priority_frame_handoff - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_priority_frame_handoff.cpp -) -argus_sender_add_cpp_test(argus_sender_test_priority_frame_handoff priority_frame_handoff) diff --git a/examples/camera_streamer/tests/cpp/rate_limiter/CMakeLists.txt b/examples/camera_streamer/tests/cpp/rate_limiter/CMakeLists.txt deleted file mode 100644 index 8cbbe0c519..0000000000 --- a/examples/camera_streamer/tests/cpp/rate_limiter/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_rate_limiter - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_rate_limiter.cpp - ${ARGUS_SENDER_ROOT}/src/StereoTiming.cpp - ${ARGUS_SENDER_ROOT}/src/MixedRateScheduler.cpp -) -argus_sender_add_cpp_test(argus_sender_test_rate_limiter rate_limiter) diff --git a/examples/camera_streamer/tests/cpp/tcp_transport/CMakeLists.txt b/examples/camera_streamer/tests/cpp/tcp_transport/CMakeLists.txt deleted file mode 100644 index c01f25f4a4..0000000000 --- a/examples/camera_streamer/tests/cpp/tcp_transport/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -add_executable(argus_sender_test_tcp_transport - ${CAMERA_STREAMER_TEST_ROOT}/cpp/test_tcp_transport.cpp - ${ARGUS_SENDER_ROOT}/src/TcpTransport.cpp - ${ARGUS_SENDER_ROOT}/src/StereoTiming.cpp - ${ARGUS_SENDER_ROOT}/src/Runtime.cpp -) -argus_sender_add_cpp_test(argus_sender_test_tcp_transport tcp_transport) -set_tests_properties(tcp_transport PROPERTIES LABELS "loopback") diff --git a/examples/camera_streamer/tests/pyproject.toml b/examples/camera_streamer/tests/pyproject.toml deleted file mode 100644 index f45714fa26..0000000000 --- a/examples/camera_streamer/tests/pyproject.toml +++ /dev/null @@ -1,13 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -[project] -name = "camera-streamer-tests" -version = "0.0.0" -requires-python = ">=3.10" - -[project.optional-dependencies] -dev = ["pytest>=8", "pybind11>=2.13", "numpy>=1.24,<3", "PyYAML>=6"] - -[tool.pytest.ini_options] -testpaths = ["python"] diff --git a/examples/camera_streamer/tests/python/test_avs1_validation.py b/examples/camera_streamer/tests/python/test_avs1_validation.py deleted file mode 100644 index cc77cef61b..0000000000 --- a/examples/camera_streamer/tests/python/test_avs1_validation.py +++ /dev/null @@ -1,72 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Protocol-only regressions runnable without the GPU receiver or pytest.""" - -import sys -import unittest -from pathlib import Path - -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "camera_receiver")) - -from avs_wire import HEADER, SAMPLE, STREAM, parse_avs1_record # noqa: E402 - - -def record(first_samples=(0, 1), extra_sample=False): - streams = b"".join( - STREAM.pack(i, 1, 1, 1, 0, 1280, 720, 30, 1, 1, first, 1, 0) - for i, first in enumerate(first_samples) - ) - samples = b"".join( - SAMPLE.pack(i, 0, 0, 1, 0, i, 0, 1, 0, 0, 0, 0, 0, i, 1) for i in range(2) - ) - if extra_sample: - samples += bytes(SAMPLE.size) - header = HEADER.pack( - b"AVS1", - 1, - 0, - HEADER.size, - 2, - 2 + extra_sample, - 1, - 1, - 0, - 0, - len(streams) + len(samples), - 2, - 0, - ) - return header + streams + samples + b"LR" - - -class DescriptorCoverageTests(unittest.TestCase): - def test_valid_mono_streams(self): - bundle = parse_avs1_record(record()) - self.assertEqual( - [bytes(stream.samples[0].payload) for stream in bundle.streams], - [b"L", b"R"], - ) - - def test_rejects_unreferenced_descriptor(self): - with self.assertRaisesRegex(ValueError, "not fully referenced"): - parse_avs1_record(record(extra_sample=True)) - - def test_rejects_noncontiguous_stream_ranges(self): - for first_samples in [(1, 0), (0, 0), (0, 2)]: - with self.subTest(first_samples=first_samples): - with self.assertRaisesRegex(ValueError, "not contiguous"): - parse_avs1_record(record(first_samples)) - - def test_rejects_range_past_descriptor_table(self): - packet = bytearray(record()) - fields = list(STREAM.unpack_from(packet, HEADER.size + STREAM.size)) - fields[1] |= 2 # Stereo requires two samples, but only one remains. - fields[11] = 2 - STREAM.pack_into(packet, HEADER.size + STREAM.size, *fields) - with self.assertRaisesRegex(ValueError, "exceeds descriptor table"): - parse_avs1_record(packet) - - -if __name__ == "__main__": - unittest.main() diff --git a/examples/camera_streamer/tests/python/test_backpressure.py b/examples/camera_streamer/tests/python/test_backpressure.py deleted file mode 100644 index 01cd8ed45d..0000000000 --- a/examples/camera_streamer/tests/python/test_backpressure.py +++ /dev/null @@ -1,131 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -import threading -from concurrent.futures import ThreadPoolExecutor - -import pytest - -from camera_receiver import _StreamRuntime -from cuda_ipc import CudaIpcFramePublisher - - -@pytest.fixture -def runtime(): - runtime = _StreamRuntime.__new__(_StreamRuntime) - runtime.inflight = threading.BoundedSemaphore(1) - runtime.executor = ThreadPoolExecutor(max_workers=1) - yield runtime - runtime.executor.shutdown(wait=True) - - -def test_full_queue_blocks_and_preserves_order(runtime): - stop = threading.Event() - release = threading.Event() - submitted = threading.Event() - order = [] - - def first(): - release.wait(5) - order.append(1) - - assert runtime.submit(stop, first) - - def submit_second(): - runtime.submit(stop, lambda: order.append(2)) - submitted.set() - - thread = threading.Thread(target=submit_second) - thread.start() - try: - assert not submitted.wait(0.1) - finally: - release.set() - thread.join(2) - assert submitted.is_set() - runtime.executor.shutdown(wait=True) - assert order == [1, 2] - - -def test_stop_interrupts_full_queue(runtime): - stop = threading.Event() - runtime.inflight.acquire() - result = [] - thread = threading.Thread( - target=lambda: result.append(runtime.submit(stop, lambda: None)) - ) - thread.start() - stop.set() - thread.join(2) - runtime.inflight.release() - assert not thread.is_alive() - assert result == [False] - - -def test_submit_failure_returns_capacity(runtime): - runtime.executor.shutdown(wait=True) - with pytest.raises(RuntimeError): - runtime.submit(threading.Event(), lambda: None) - assert runtime.inflight.acquire(blocking=False) - - -def test_worker_failure_returns_capacity(runtime): - def fail(): - raise ValueError("decode failed") - - assert runtime.submit(threading.Event(), fail) - assert runtime.inflight.acquire(timeout=2) - - -def test_ipc_socket_is_owner_only(tmp_path): - path = tmp_path / "frames.sock" - publisher = CudaIpcFramePublisher(str(path)) - try: - publisher._open_server() - assert path.stat().st_mode & 0o777 == 0o600 - finally: - publisher.stop() - - -@pytest.mark.parametrize("raises", [False, True]) -def test_stream_waits_for_other_eye_after_failure(monkeypatch, raises): - import sys - from concurrent.futures import Future, wait - from types import SimpleNamespace - - import camera_receiver as module - - first, second = Future(), Future() - if raises: - first.set_exception(RuntimeError("left decode failed")) - else: - first.set_result(False) - futures = iter([first, second]) - receiver = module.Avs1TcpReceiver.__new__(module.Avs1TcpReceiver) - receiver._sample_pool = SimpleNamespace(submit=lambda *args: next(futures)) - receiver._validate_stream_descriptor = lambda *args: None - runtime = SimpleNamespace(write_index=0, config=SimpleNamespace(name="head")) - waiting, finished = threading.Event(), threading.Event() - - def observed_wait(tasks): - waiting.set() - return wait(tasks) - - def process(): - try: - receiver._process_stream_safe( - None, None, SimpleNamespace(samples=[object(), object()]), runtime - ) - finally: - finished.set() - - monkeypatch.setitem(sys.modules, "cupy", SimpleNamespace()) - monkeypatch.setattr(module, "wait", observed_wait) - thread = threading.Thread(target=process) - thread.start() - try: - assert waiting.wait(2) - assert not finished.is_set() - finally: - second.set_result(False) - thread.join(2) - assert finished.is_set() diff --git a/examples/camera_streamer/tests/python/test_consumer_cleanup.py b/examples/camera_streamer/tests/python/test_consumer_cleanup.py deleted file mode 100644 index 4f74b2492e..0000000000 --- a/examples/camera_streamer/tests/python/test_consumer_cleanup.py +++ /dev/null @@ -1,114 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -import base64 -import contextlib -import sys -from pathlib import Path -from types import SimpleNamespace -from unittest.mock import Mock - -import pytest - -sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "cuda_ipc_consumer")) -from consumer import CudaIpcClient # noqa: E402 - - -@pytest.mark.parametrize("failure", ["second_handle", "array"]) -def test_connect_failure_closes_socket_and_partial_mappings(monkeypatch, failure): - import consumer - - runtime = SimpleNamespace( - cudaIpcMemLazyEnablePeerAccess=1, - ipcOpenMemHandle=Mock(side_effect=[123, RuntimeError("mapping failed")]), - ipcCloseMemHandle=Mock(), - ) - cp = SimpleNamespace( - cuda=SimpleNamespace( - runtime=runtime, - Device=lambda _: contextlib.nullcontext(), - UnownedMemory=Mock(), - MemoryPointer=Mock(), - ), - uint8=object(), - ndarray=Mock( - side_effect=RuntimeError("array failed") if failure == "array" else None - ), - ) - sock = Mock() - monkeypatch.setitem(sys.modules, "cupy", cp) - monkeypatch.setattr(consumer.socket, "socket", Mock(return_value=sock)) - slots = [ - dict( - slot=i, - guard_path="/unused", - handle=base64.b64encode(b"handle").decode(), - nbytes=4, - shape=[2, 2], - strides=[2, 1], - ) - for i in range(2) - ] - client = CudaIpcClient("/unused.sock") - client._read_json_blocking = Mock( - side_effect=[ - {"type": "hello", "version": 3}, - {"type": "subscribed", "streams": [{"name": "head.left", "slots": slots}]}, - ] - ) - with pytest.raises(RuntimeError, match="failed"): - client.connect() - runtime.ipcCloseMemHandle.assert_called_once_with(123) - sock.close.assert_called_once() - assert client.sock is None - assert not client.slots and not client.stream_order and not client.stream_info - client.close() - runtime.ipcCloseMemHandle.assert_called_once_with(123) - - -@pytest.mark.parametrize("fails", [False, True]) -def test_read_frame_holds_lock_through_gpu_completion(monkeypatch, tmp_path, fails): - import fcntl - import consumer - from slot_guard import commit_slot - - path = tmp_path / "guard" - path.write_bytes(bytes(8)) - commit_slot(path, 7) - synced = [] - - def sync(): - with path.open("rb") as probe: - with pytest.raises(BlockingIOError): - fcntl.flock(probe, fcntl.LOCK_EX | fcntl.LOCK_NB) - synced.append(True) - - monkeypatch.setitem( - sys.modules, - "cupy", - SimpleNamespace( - cuda=SimpleNamespace( - Device=lambda _: contextlib.nullcontext(), - get_current_stream=lambda: SimpleNamespace(synchronize=sync), - ) - ), - ) - client = consumer.CudaIpcClient("unused") - array = object() - client.slots[("eye", 0)] = SimpleNamespace( - array=array, guard_path=str(path), gpu_id=0 - ) - msg = dict(stream="eye", slot=0, slot_generation=7) - try: - with client.read_frame(msg) as actual: - assert actual is array - if fails: - raise RuntimeError("conversion failed") - except RuntimeError: - assert fails - assert synced == [True] - with path.open("rb") as probe: - fcntl.flock(probe, fcntl.LOCK_EX | fcntl.LOCK_NB) - msg["slot_generation"] = 6 - with client.read_frame(msg) as actual: - assert actual is None - assert synced == [True] diff --git a/examples/camera_streamer/tests/python/test_cuda_buffers.py b/examples/camera_streamer/tests/python/test_cuda_buffers.py deleted file mode 100644 index 015415b3b9..0000000000 --- a/examples/camera_streamer/tests/python/test_cuda_buffers.py +++ /dev/null @@ -1,77 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -"""Exercise the native buffer checks without a CUDA device or toolkit.""" - -import importlib.util -import subprocess -import sysconfig -from pathlib import Path -from types import SimpleNamespace - -import pybind11 -import pytest - - -@pytest.fixture(scope="module") -def validators(tmp_path_factory): - path = tmp_path_factory.mktemp("cuda_buffers") - source = path / "bindings.cpp" - source.write_text("""#include -PYBIND11_MODULE(_test_cuda_buffers, m) { - m.def("nv12", [](py::object o) { return extract_nv12_buffer(o, 4, 2).row_pitch_bytes; }); - m.def("rgba", [](py::object o) { return extract_cuda_buffer(o, 4, 2).row_pitch_bytes; }); -} -""") - extension = path / ("_test_cuda_buffers" + sysconfig.get_config_var("EXT_SUFFIX")) - codec = Path(__file__).resolve().parents[2] / "camera_receiver" / "codec" - subprocess.run( - [ - "c++", - "-shared", - "-fPIC", - "-std=c++17", - str(source), - "-o", - str(extension), - "-I" + str(codec), - "-I" + pybind11.get_include(), - "-I" + sysconfig.get_paths()["include"], - ], - check=True, - ) - spec = importlib.util.spec_from_file_location("_test_cuda_buffers", extension) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - -@pytest.mark.parametrize( - "kind,shape,pitch", [("nv12", (3, 4), 4), ("rgba", (2, 4, 4), 16)] -) -@pytest.mark.parametrize( - "typestr,readonly,error", - [ - ("|u1", False, None), - (" -namespace camera_receiver::codec { -struct NvVideoDecoder::Impl { DecoderConfig cfg; }; -NvVideoDecoder::NvVideoDecoder(const DecoderConfig& cfg) : impl_(new Impl{cfg}) {} -NvVideoDecoder::~NvVideoDecoder() = default; -uint32_t NvVideoDecoder::width() const { return impl_->cfg.width; } -uint32_t NvVideoDecoder::height() const { return impl_->cfg.height; } -bool NvVideoDecoder::decode(const uint8_t*, std::size_t, uintptr_t, std::size_t, int) { return true; } -bool NvVideoDecoder::decode_nv12(const uint8_t*, std::size_t, uintptr_t, std::size_t, int) { return true; } -void NvVideoDecoder::reset() {} -} -""") - codec = Path(__file__).resolve().parents[2] / "camera_receiver" / "codec" - extension = path / ( - "_camera_receiver_codec" + sysconfig.get_config_var("EXT_SUFFIX") - ) - subprocess.run( - [ - "c++", - "-shared", - "-fPIC", - "-std=c++17", - str(codec / "codec_bindings.cpp"), - str(stub), - "-o", - str(extension), - "-I" + str(codec), - "-I" + pybind11.get_include(), - "-I" + sysconfig.get_paths()["include"], - ], - check=True, - ) - spec = importlib.util.spec_from_file_location("_camera_receiver_codec", extension) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - -@pytest.mark.parametrize("method", ["decode", "decode_nv12"]) -@pytest.mark.parametrize( - "width,height", [(1280, 720), (640, 720), (1280, 360), (640, 360), (1920, 1080)] -) -def test_binding_checks_dimensions_against_decoder(bindings, method, width, height): - config = bindings.DecoderConfig() - config.width, config.height = 1280, 720 - decoder = bindings.NvVideoDecoder(config) - shape = (height, width, 4) if method == "decode" else (height * 3 // 2, width) - output = SimpleNamespace( - __cuda_array_interface__={ - "shape": shape, - "data": (1234, False), - "typestr": "|u1", - "strides": None, - } - ) - call = getattr(decoder, method) - if (width, height) == (config.width, config.height): - assert call(b"au", output, width, height) - else: - with pytest.raises(RuntimeError, match="configured decoder size"): - call(b"au", output, width, height) diff --git a/examples/camera_streamer/tests/python/test_shared_config.py b/examples/camera_streamer/tests/python/test_shared_config.py deleted file mode 100644 index b527a4308f..0000000000 --- a/examples/camera_streamer/tests/python/test_shared_config.py +++ /dev/null @@ -1,88 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -from pathlib import Path - -import pytest -import yaml - -from camera_receiver import _load_config, _parse_stream_config - - -PROFILES = Path(__file__).resolve().parents[2] / "configs" - - -@pytest.mark.parametrize( - "path", sorted(PROFILES.glob("s56-*.yaml")), ids=lambda p: p.stem -) -def test_shared_profiles(path): - source = yaml.safe_load(path.read_text()) - receiver = _load_config(path) - assert receiver["receiver"]["port"] == source["network"]["port"] - for index, (sent, received) in enumerate( - zip(source["streams"], receiver["streams"], strict=True) - ): - stream = _parse_stream_config(received) - assert (stream.index, stream.name) == (index, sent["id"]) - assert (stream.width, stream.height) == ( - sent["output"]["width"], - sent["output"]["height"], - ) - assert stream.stereo == (sent["type"] == "stereo") - assert stream.ipc_names == ( - ("head.left", "head.right") if stream.stereo else ("head",) - ) - assert len(sent["cameras"]) == (2 if stream.stereo else 1) - - -def test_multiple_streams_share_order_geometry_and_port(tmp_path): - cfg = yaml.safe_load((PROFILES / "s56-stereo-1080p60.yaml").read_text()) - cfg["network"]["port"] = 54321 - cfg["streams"].append( - dict( - id="aux", - type="mono", - cameras=[dict(device=2)], - output=dict(width=640, height=480, fps=30, codec="hevc"), - ) - ) - path = tmp_path / "shared.yaml" - path.write_text(yaml.safe_dump(cfg)) - received = _load_config(path) - assert received["receiver"]["port"] == 54321 - aux = _parse_stream_config(received["streams"][1]) - assert (aux.index, aux.width, aux.height, aux.ipc_names) == (1, 640, 480, ("aux",)) - - -@pytest.mark.parametrize("conflict", ["port", "width", "missing_dimensions"]) -def test_shared_config_rejects_ambiguous_geometry_or_port(tmp_path, conflict): - cfg = yaml.safe_load((PROFILES / "s56-stereo-1080p60.yaml").read_text()) - if conflict == "port": - cfg["receiver"]["port"] = 6000 - elif conflict == "width": - cfg["streams"][0]["receiver"]["width"] = 640 - else: - del cfg["streams"][0]["output"]["width"] - path = tmp_path / "invalid.yaml" - path.write_text(yaml.safe_dump(cfg)) - with pytest.raises(ValueError): - _load_config(path) - - -def test_legacy_receiver_profile(tmp_path): - cfg = dict( - receiver=dict(port=6000), - streams=[dict(index=0, name="old", type="mono", width=640, height=480)], - ) - path = tmp_path / "legacy.yaml" - path.write_text(yaml.safe_dump(cfg)) - assert _load_config(path) == cfg - - -def test_default_profile_is_independent_of_working_directory( - tmp_path, monkeypatch, capsys -): - from camera_receiver import main - - monkeypatch.chdir(tmp_path) - assert main(["--dry-run", "--host", "127.0.0.1"]) == 0 - assert "config ok" in capsys.readouterr().out diff --git a/examples/camera_streamer/tests/python/test_slot_guard.py b/examples/camera_streamer/tests/python/test_slot_guard.py deleted file mode 100644 index 1fcfb17b82..0000000000 --- a/examples/camera_streamer/tests/python/test_slot_guard.py +++ /dev/null @@ -1,94 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -import threading -from types import SimpleNamespace - -from cuda_ipc import CudaIpcFramePublisher -from slot_guard import commit_slot, read_slot, write_slot - - -def test_delayed_broadcast_discards_frames_after_multiple_ring_wraps(tmp_path): - publisher = CudaIpcFramePublisher(str(tmp_path / "ipc.sock")) - paths = [tmp_path / str(i) for i in range(3)] - for path in paths: - path.write_bytes(bytes(8)) - publisher._streams["eye"] = SimpleNamespace( - slots=[SimpleNamespace(guard_path=str(p)) for p in paths] - ) - publisher._started = True - pixels = [None] * 3 - for seq in range(1, 8): - slot = (seq - 1) % 3 - with publisher.write_slot("eye", slot, publisher._stop): - pixels[slot] = seq - publisher.publish("eye", slot, (seq - 1) // 3 + 1, seq, seq) - sent = [] - publisher._clients = [SimpleNamespace(streams={"eye"}, sock=None)] - - def send(sock, msg): - with publisher._current_frame(msg) as valid: - assert valid - assert pixels[msg["slot"]] == msg["seq"] - sent.append(msg["seq"]) - if msg["seq"] == 7: - publisher._stop.set() - - publisher._send_json = send - publisher._broadcast_loop() - assert sent == [5, 6, 7] - # A message already in the socket must also be checked at consumption time. - with read_slot(paths[0], 1) as valid: - assert not valid - publisher._guard_dir.cleanup() - - -def test_reader_holds_pixels_until_read_finishes(tmp_path): - path = tmp_path / "slot" - path.write_bytes(bytes(8)) - commit_slot(path, 1) - waiting, finished = threading.Event(), threading.Event() - stop = threading.Event() - original_wait = stop.wait - - def observed_wait(timeout): - waiting.set() - return original_wait(timeout) - - stop.wait = observed_wait - - def overwrite(): - with write_slot(path, stop): - finished.set() - - with read_slot(path, 1) as valid: - assert valid - worker = threading.Thread(target=overwrite) - worker.start() - assert waiting.wait(2) - assert not finished.is_set() - worker.join(2) - assert finished.is_set() - with read_slot(path, 1) as valid: - assert not valid - - -def test_stop_interrupts_writer_waiting_on_reader(tmp_path): - path = tmp_path / "slot" - path.write_bytes(bytes(8)) - commit_slot(path, 1) - stop, cancelled = threading.Event(), threading.Event() - - def write(): - try: - with write_slot(path, stop): - raise AssertionError("must not overwrite a reader") - except InterruptedError: - cancelled.set() - - with read_slot(path, 1): - worker = threading.Thread(target=write) - worker.start() - stop.set() - worker.join(2) - assert cancelled.is_set() diff --git a/examples/camera_streamer/tests/python/test_smoke_config.py b/examples/camera_streamer/tests/python/test_smoke_config.py deleted file mode 100644 index 743cd03784..0000000000 --- a/examples/camera_streamer/tests/python/test_smoke_config.py +++ /dev/null @@ -1,41 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -import sys -import tempfile -import unittest -from pathlib import Path - -sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "hardware")) -from avs1_smoke import configured_port, validate_frame_limit # noqa: E402 - - -class SmokeConfigTests(unittest.TestCase): - def test_configured_port_and_sender_default(self): - with tempfile.TemporaryDirectory() as root: - path = Path(root) / "camera.yaml" - for text, expected in [("network: {port: 54321}", 54321), ("{}", 5000)]: - path.write_text(text) - self.assertEqual(configured_port(str(path)), expected) - for value in [0, 65536, True, "not-a-port"]: - path.write_text(f"network: {{port: {value}}}") - with self.assertRaises(ValueError): - configured_port(str(path)) - - def test_frame_limit_matches_requested_bundles(self): - with tempfile.TemporaryDirectory() as root: - path = Path(root) / "camera.yaml" - path.write_text("runtime: {frames: 120}") - validate_frame_limit(str(path), 120) - for text, requested in [ - ("{}", 120), - ("runtime: {frames: 0}", 120), - ("runtime: {frames: -1}", 120), - ("runtime: {frames: true}", 1), - ("runtime: {frames: 120}", 60), - ("runtime: {frames: 60}", 120), - ("runtime: {frames: 120}", 0), - ]: - with self.subTest(text=text, requested=requested): - path.write_text(text) - with self.assertRaisesRegex(ValueError, "runtime.frames"): - validate_frame_limit(str(path), requested) diff --git a/examples/camera_streamer/tests/python/test_worker_stream.py b/examples/camera_streamer/tests/python/test_worker_stream.py deleted file mode 100644 index 43c3dfbead..0000000000 --- a/examples/camera_streamer/tests/python/test_worker_stream.py +++ /dev/null @@ -1,70 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -import contextlib -import sys -import threading -from concurrent.futures import ThreadPoolExecutor -from types import SimpleNamespace - -import pytest - -from camera_receiver import Avs1TcpReceiver, CODEC_RAW_NV12 - - -@pytest.mark.parametrize( - "pixel_format,rotation", [("nv12", 0), ("nv12", 180), ("rgba8", 0), ("rgba8", 180)] -) -@pytest.mark.parametrize("fails", [False, True]) -def test_raw_work_completes_on_sample_thread_before_slot_release( - monkeypatch, pixel_format, rotation, fails -): - pending = threading.local() - coordinator = threading.get_ident() - sync_threads = [] - - def submit(*args): - pending.busy = True - if fails: - raise RuntimeError("submitted work failed") - - def synchronize(): - sync_threads.append(threading.get_ident()) - pending.busy = False - - @contextlib.contextmanager - def slot(*args): - try: - yield - finally: - assert not pending.busy - - cp = SimpleNamespace( - cuda=SimpleNamespace( - Device=lambda _: contextlib.nullcontext(), - get_current_stream=lambda: SimpleNamespace(synchronize=synchronize), - ) - ) - monkeypatch.setitem(sys.modules, "cupy", cp) - output = SimpleNamespace(shape=(3, 4), set=submit) - runtime = SimpleNamespace( - config=SimpleNamespace(ipc_names=["eye"], output_format=pixel_format), - output_buffers=[[output]], - raw_buffers=[SimpleNamespace(set=submit)], - raw_kernel=submit, - grid=None, - block=None, - ) - receiver = Avs1TcpReceiver.__new__(Avs1TcpReceiver) - receiver._gpu_device_id = 0 - receiver._stop = threading.Event() - receiver._ipc_publisher = SimpleNamespace(write_slot=slot) - sample = SimpleNamespace(camera_index=0, payload=bytes(12)) - stream = SimpleNamespace(codec=CODEC_RAW_NV12, width=4, height=2, rotation=rotation) - with ThreadPoolExecutor(max_workers=1) as pool: - future = pool.submit(receiver._process_sample, runtime, stream, sample, 0) - if fails: - with pytest.raises(RuntimeError, match="submitted"): - future.result() - else: - assert future.result() - assert len(sync_threads) == 1 and sync_threads[0] != coordinator From f207cffb940a74a6825cd2dd2bfb7d84aa299e75 Mon Sep 17 00:00:00 2001 From: Teng Wei Date: Tue, 10 Nov 2026 16:50:49 -0800 Subject: [PATCH 2/7] Address review comments --- .../argus_sender/src/FrameBundleAssembler.cpp | 4 + .../tests/test_bundle_assembler.cpp | 129 ++++++++++++++++++ .../camera_receiver/camera_receiver.py | 4 +- .../tests/test_sample_completion.py | 66 +++++++++ 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 examples/camera_streamer/camera_receiver/tests/test_sample_completion.py diff --git a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp index d6d89b60f8..bdceb2f9d2 100644 --- a/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp +++ b/examples/camera_streamer/argus_sender/src/FrameBundleAssembler.cpp @@ -211,6 +211,7 @@ class FrameBundleAssembler::Impl pending.timing.acquireSpreadNs = latestAcquiredSteadyNs - earliestAcquiredSteadyNs; } + std::lock_guard sendLock(m_sendMutex); std::vector ready; { std::lock_guard lock(m_mutex); @@ -327,6 +328,7 @@ class FrameBundleAssembler::Impl if (payload.empty()) return setError("empty AVS1 sample payload"); const uint64_t payloadDoneSteadyNs = steadyNowNs(); + std::lock_guard sendLock(m_sendMutex); std::vector ready; { std::lock_guard lock(m_mutex); @@ -444,6 +446,8 @@ class FrameBundleAssembler::Impl uint64_t m_sessionId; std::vector m_streamTemplates; std::vector>> m_sinks; + // Hold across ordered collection and enqueue. Lock order: send, then state. + std::mutex m_sendMutex; mutable std::mutex m_mutex; std::map m_pending; uint64_t m_nextSendSequence; diff --git a/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp b/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp index 4060db28d2..9ef014a59f 100644 --- a/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_bundle_assembler.cpp @@ -10,7 +10,10 @@ #include "TestHarness.h" #include +#include #include +#include +#include #include #include @@ -42,6 +45,60 @@ class FakePacketSink : public argus_sender::PacketSink std::vector timings; }; +class BlockingPacketSink : public argus_sender::PacketSink +{ +public: + bool enqueueFramed(std::vector&& packet) override + { + argus_sender::Avs1Bundle bundle; + const char* error = NULL; + if (!argus_sender::parseAvs1WireRecord(packet.data(), packet.size(), &bundle, &error)) + return false; + std::unique_lock lock(m_mutex); + if (bundle.sequence == 1) + { + m_firstEntered = true; + m_cv.notify_all(); + m_cv.wait(lock, [this] { return m_releaseFirst; }); + } + sequences.push_back(bundle.sequence); + m_cv.notify_all(); + return true; + } + + bool hasError() const override + { + return false; + } + + bool waitForFirst() + { + std::unique_lock lock(m_mutex); + return m_cv.wait_for(lock, std::chrono::seconds(2), [this] { return m_firstEntered; }); + } + + bool waitForOvertake() + { + std::unique_lock lock(m_mutex); + return m_cv.wait_for(lock, std::chrono::milliseconds(100), [this] { return !sequences.empty(); }); + } + + void releaseFirst() + { + std::lock_guard lock(m_mutex); + m_releaseFirst = true; + m_cv.notify_all(); + } + + std::vector sequences; + +private: + std::mutex m_mutex; + std::condition_variable m_cv; + bool m_firstEntered = false; + bool m_releaseFirst = false; +}; + std::vector templates() { std::vector result(2); @@ -76,6 +133,76 @@ argus_sender::FrameTiming timing(uint64_t frame, uint64_t tsc) return result; } +void checkConcurrentEnqueueOrder(TestContext& test, bool emptyBundles) +{ + BlockingPacketSink sink; + std::vector streams = templates(); + streams[1].codec = argus_sender::AVS1_CODEC_HEVC; + argus_sender::FrameBundleAssembler assembler(&sink, 77, streams); + std::vector inputs(2); + const uint8_t payload[] = { 0, 0, 1, 0x26 }; + if (!emptyBundles) + { + inputs[0].present = true; + inputs[0].sequence = 1; + inputs[0].timings.push_back(timing(10, 1000)); + inputs[0].timings.push_back(timing(11, 1004)); + inputs[1].present = true; + inputs[1].sequence = 1; + inputs[1].timings.push_back(timing(20, 1000)); + TEST_CHECK(test, assembler.beginBundle(1, inputs, 2000, 2100, argus_sender::SENDER_CLOCK_REALTIME, 0)); + inputs[0].sequence = 2; + inputs[1].present = false; + inputs[1].sequence = 0; + TEST_CHECK(test, assembler.beginBundle(2, inputs, 3000, 3100, argus_sender::SENDER_CLOCK_REALTIME, 0)); + for (size_t camera = 0; camera < 2; ++camera) + { + TEST_CHECK(test, assembler.encodedSink(0, camera)->enqueueEncoded( + payload, sizeof(payload), 1, 2200, argus_sender::SENDER_CLOCK_REALTIME)); + } + } + + bool firstOk = false; + bool secondOk = false; + std::thread first( + [&] + { + firstOk = emptyBundles ? + assembler.beginBundle(1, inputs, 2000, 2100, argus_sender::SENDER_CLOCK_REALTIME, 0) : + assembler.encodedSink(1, 0)->enqueueEncoded( + payload, sizeof(payload), 1, 2200, argus_sender::SENDER_CLOCK_REALTIME); + }); + TEST_CHECK(test, sink.waitForFirst()); + std::thread second( + [&] + { + if (emptyBundles) + { + secondOk = assembler.beginBundle(2, inputs, 3000, 3100, argus_sender::SENDER_CLOCK_REALTIME, 0); + } + else + { + secondOk = assembler.encodedSink(0, 0)->enqueueEncoded( + payload, sizeof(payload), 2, 3200, argus_sender::SENDER_CLOCK_REALTIME); + secondOk = assembler.encodedSink(0, 1)->enqueueEncoded( + payload, sizeof(payload), 2, 3200, argus_sender::SENDER_CLOCK_REALTIME) && + secondOk; + } + }); + TEST_CHECK(test, !sink.waitForOvertake()); + sink.releaseFirst(); + first.join(); + second.join(); + TEST_CHECK(test, firstOk); + TEST_CHECK(test, secondOk); + TEST_CHECK_EQ(test, sink.sequences.size(), static_cast(2)); + if (sink.sequences.size() == 2) + { + TEST_CHECK_EQ(test, sink.sequences[0], 1ULL); + TEST_CHECK_EQ(test, sink.sequences[1], 2ULL); + } +} + } // namespace int main() @@ -181,5 +308,7 @@ int main() TEST_CHECK(test, !(parsed.flags & argus_sender::AVS1_BUNDLE_SENDER_CLOCK_VALID)); TEST_CHECK(test, !(parsed.flags & argus_sender::AVS1_BUNDLE_SENDER_TSC_VALID)); TEST_CHECK_EQ(test, parsed.senderCaptureReadyTscNs, 0ULL); + checkConcurrentEnqueueOrder(test, false); + checkConcurrentEnqueueOrder(test, true); return test.finish(); } diff --git a/examples/camera_streamer/camera_receiver/camera_receiver.py b/examples/camera_streamer/camera_receiver/camera_receiver.py index 8568f20aa5..c1f5672745 100755 --- a/examples/camera_streamer/camera_receiver/camera_receiver.py +++ b/examples/camera_streamer/camera_receiver/camera_receiver.py @@ -23,7 +23,7 @@ import struct import threading import time -from concurrent.futures import Future, ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor, wait from dataclasses import dataclass from pathlib import Path from typing import Any, Callable, Iterable, Optional @@ -769,6 +769,8 @@ def _process_stream( ) for sample in stream.samples ] + # Finish every eye before a failure can allow slot reuse or decoder reset. + wait(futures) if not all(future.result() for future in futures): return receiver_process_done_mono_ns = time.monotonic_ns() diff --git a/examples/camera_streamer/camera_receiver/tests/test_sample_completion.py b/examples/camera_streamer/camera_receiver/tests/test_sample_completion.py new file mode 100644 index 0000000000..52b9b8a38c --- /dev/null +++ b/examples/camera_streamer/camera_receiver/tests/test_sample_completion.py @@ -0,0 +1,66 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +from concurrent.futures import ThreadPoolExecutor +import sys +import threading +from types import ModuleType, SimpleNamespace +from unittest.mock import Mock + +import pytest + +from camera_receiver import Avs1TcpReceiver + + +@pytest.mark.parametrize("failure_index", [0, 1]) +@pytest.mark.parametrize("raises", [False, True]) +def test_failed_stereo_bundle_finishes_both_eyes_before_next_operation( + monkeypatch, failure_index, raises +): + monkeypatch.setitem(sys.modules, "cupy", ModuleType("cupy")) + blocked_eye_started = threading.Event() + release_eye = threading.Event() + next_operation = threading.Event() + receiver = Avs1TcpReceiver.__new__(Avs1TcpReceiver) + receiver._validate_stream_descriptor = Mock() + receiver._ipc_publisher = Mock() + runtime = SimpleNamespace(write_index=0, config=SimpleNamespace(name="head")) + stream = SimpleNamespace( + samples=tuple(SimpleNamespace(camera_index=i) for i in range(2)) + ) + + def process_sample(_runtime, _stream, sample, _slot): + if sample.camera_index == failure_index: + if raises: + raise RuntimeError("decode failed") + return False + blocked_eye_started.set() + if not release_eye.wait(timeout=5): + raise TimeoutError("test did not release the other eye") + return True + + receiver._process_sample = process_sample + with ( + ThreadPoolExecutor(max_workers=2) as sample_pool, + ThreadPoolExecutor(max_workers=1) as stream_pool, + ): + receiver._sample_pool = sample_pool + processing = stream_pool.submit( + receiver._process_stream, None, None, stream, runtime + ) + following = stream_pool.submit(next_operation.set) + try: + assert blocked_eye_started.wait(timeout=2) + assert not next_operation.wait(timeout=0.1) + assert not processing.done() + finally: + release_eye.set() + if raises: + with pytest.raises(RuntimeError, match="decode failed"): + processing.result(timeout=2) + else: + assert processing.result(timeout=2) is None + following.result(timeout=2) + + assert runtime.write_index == 0 + receiver._ipc_publisher.publish.assert_not_called() From 7ad430a0729f6745831785987f8ec6064d85f56f Mon Sep 17 00:00:00 2001 From: Teng Wei Date: Tue, 10 Nov 2026 17:06:18 -0800 Subject: [PATCH 3/7] Correct file license --- .../argus_sender/src/EGLGlobal.cpp | 33 +++++++++++++++++-- .../argus_sender/src/EGLGlobal.h | 6 ++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp b/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp index 438982f4c6..569768b0e8 100644 --- a/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp +++ b/examples/camera_streamer/argus_sender/src/EGLGlobal.cpp @@ -1,5 +1,34 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 +/* + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of NVIDIA CORPORATION nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ /* * Minimal headless EGL helper for argus_sender, derived from NVIDIA's diff --git a/examples/camera_streamer/argus_sender/src/EGLGlobal.h b/examples/camera_streamer/argus_sender/src/EGLGlobal.h index 47690e89f6..fdab944f18 100644 --- a/examples/camera_streamer/argus_sender/src/EGLGlobal.h +++ b/examples/camera_streamer/argus_sender/src/EGLGlobal.h @@ -1,7 +1,9 @@ /* * SPDX-License-Identifier: BSD-3-Clause - * - * SPDX-FileCopyrightText: Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. + */ + +/* + * Copyright (c) 2016-2018, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions From afdf8e848c54b0de4670eefee13ddbf8fd316428 Mon Sep 17 00:00:00 2001 From: Teng Wei Date: Tue, 10 Nov 2026 17:20:40 -0800 Subject: [PATCH 4/7] Address exception cleanup and 8-bit format validation --- .../codec/nv_video_decoder.cpp | 63 +++- .../tests/test_decoder_cleanup.py | 290 ++++++++++++++++++ 2 files changed, 336 insertions(+), 17 deletions(-) create mode 100644 examples/camera_streamer/camera_receiver/tests/test_decoder_cleanup.py diff --git a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp b/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp index eaa8188e0a..85c527d7f3 100644 --- a/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp +++ b/examples/camera_streamer/camera_receiver/codec/nv_video_decoder.cpp @@ -175,11 +175,25 @@ struct NvVideoDecoder::Impl return false; } - launch_nv12_to_rgba(nv12, nv12 + luma_size, pitch, pitch, w, h, reinterpret_cast(rgba_out), - static_cast(rgba_row_bytes), cfg.full_range, rotation, stream); - // UnlockFrame returns the surface to NVDEC's pool, so the - // kernel reading it must finish first. - const cudaError_t sync_err = cudaStreamSynchronize(stream); + cudaError_t sync_err = cudaSuccess; + try + { + if (decoder->GetOutputFormat() != cudaVideoSurfaceFormat_NV12) + throw std::runtime_error("NvVideoDecoder: only 8-bit NV12 decoded surfaces are supported"); + launch_nv12_to_rgba(nv12, nv12 + luma_size, pitch, pitch, w, h, reinterpret_cast(rgba_out), + static_cast(rgba_row_bytes), cfg.full_range, rotation, stream); + // UnlockFrame returns the surface to NVDEC's pool, so the + // kernel reading it must finish first. + sync_err = cudaStreamSynchronize(stream); + } + catch (...) + { + // Complete any submitted reads before returning the surface to NVDEC. + cudaStreamSynchronize(stream); + decoder->UnlockFrame(&nv12); + cuCtxPopCurrent(nullptr); + throw; + } decoder->UnlockFrame(&nv12); check_cu(cuCtxPopCurrent(nullptr), "cuCtxPopCurrent"); if (sync_err != cudaSuccess) @@ -252,22 +266,37 @@ struct NvVideoDecoder::Impl return false; } - auto* dst_y = reinterpret_cast(nv12_out); - auto* dst_uv = dst_y + nv12_row_bytes * static_cast(h); - if (rotation == 180) + cudaError_t sync_err = cudaSuccess; + try { - launch_nv12_rotate_180( - nv12, nv12 + luma_size, src_pitch, src_pitch, w, h, dst_y, static_cast(nv12_row_bytes), stream); + if (decoder->GetOutputFormat() != cudaVideoSurfaceFormat_NV12) + throw std::runtime_error("NvVideoDecoder: only 8-bit NV12 decoded surfaces are supported"); + auto* dst_y = reinterpret_cast(nv12_out); + auto* dst_uv = dst_y + nv12_row_bytes * static_cast(h); + if (rotation == 180) + { + launch_nv12_rotate_180(nv12, nv12 + luma_size, src_pitch, src_pitch, w, h, dst_y, + static_cast(nv12_row_bytes), stream); + } + else + { + check_rt( + cudaMemcpy2DAsync(dst_y, nv12_row_bytes, nv12, src_pitch, w, h, cudaMemcpyDeviceToDevice, stream), + "cudaMemcpy2DAsync NV12 Y"); + check_rt(cudaMemcpy2DAsync(dst_uv, nv12_row_bytes, nv12 + luma_size, src_pitch, w, h / 2, + cudaMemcpyDeviceToDevice, stream), + "cudaMemcpy2DAsync NV12 UV"); + } + sync_err = cudaStreamSynchronize(stream); } - else + catch (...) { - check_rt(cudaMemcpy2DAsync(dst_y, nv12_row_bytes, nv12, src_pitch, w, h, cudaMemcpyDeviceToDevice, stream), - "cudaMemcpy2DAsync NV12 Y"); - check_rt(cudaMemcpy2DAsync(dst_uv, nv12_row_bytes, nv12 + luma_size, src_pitch, w, h / 2, - cudaMemcpyDeviceToDevice, stream), - "cudaMemcpy2DAsync NV12 UV"); + // The Y copy may already be queued when the UV copy fails. + cudaStreamSynchronize(stream); + decoder->UnlockFrame(&nv12); + cuCtxPopCurrent(nullptr); + throw; } - const cudaError_t sync_err = cudaStreamSynchronize(stream); decoder->UnlockFrame(&nv12); check_cu(cuCtxPopCurrent(nullptr), "cuCtxPopCurrent"); if (sync_err != cudaSuccess) diff --git a/examples/camera_streamer/camera_receiver/tests/test_decoder_cleanup.py b/examples/camera_streamer/camera_receiver/tests/test_decoder_cleanup.py new file mode 100644 index 0000000000..2593bbb79f --- /dev/null +++ b/examples/camera_streamer/camera_receiver/tests/test_decoder_cleanup.py @@ -0,0 +1,290 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +from pathlib import Path +import shutil +import subprocess + +import pytest + + +_STATE = r""" +#pragma once +#include +#include +#include +#include +#include + +struct TestState { + std::string fault; + std::vector events; + int context_depth = 0; + int copy_calls = 0; + bool locked = false; + uint8_t surface[12] = {}; +}; +inline TestState state; +""" + +_CUDA_DRIVER = r""" +#pragma once +#include "test_state.h" +using CUdevice = int; +using CUcontext = void*; +using CUresult = int; +constexpr CUresult CUDA_SUCCESS = 0; +inline CUresult cuInit(unsigned) { return CUDA_SUCCESS; } +inline CUresult cuDeviceGet(CUdevice* device, int id) { *device = id; return CUDA_SUCCESS; } +inline CUresult cuDevicePrimaryCtxRetain(CUcontext* context, CUdevice) { + *context = reinterpret_cast(1); + return CUDA_SUCCESS; +} +inline CUresult cuDevicePrimaryCtxRelease(CUdevice) { return CUDA_SUCCESS; } +inline CUresult cuGetErrorString(CUresult, const char** error) { + *error = "injected driver error"; + return CUDA_SUCCESS; +} +inline CUresult cuCtxPushCurrent(CUcontext) { + ++state.context_depth; + state.events.push_back("push"); + return CUDA_SUCCESS; +} +inline CUresult cuCtxPopCurrent(CUcontext*) { + --state.context_depth; + state.events.push_back("pop"); + return CUDA_SUCCESS; +} +""" + +_CUDA_RUNTIME = r""" +#pragma once +#include +#include "test_state.h" +using cudaStream_t = void*; +using cudaError_t = int; +constexpr cudaError_t cudaSuccess = 0; +constexpr unsigned cudaStreamNonBlocking = 1; +constexpr int cudaMemcpyDeviceToDevice = 0; +inline const char* cudaGetErrorString(cudaError_t) { return "injected CUDA error"; } +inline cudaError_t cudaStreamCreateWithFlags(cudaStream_t* stream, unsigned) { + *stream = reinterpret_cast(2); + return cudaSuccess; +} +inline cudaError_t cudaStreamDestroy(cudaStream_t) { return cudaSuccess; } +inline cudaError_t cudaStreamSynchronize(cudaStream_t) { + state.events.push_back("sync"); + return state.fault.find("sync") != std::string::npos ? 1 : cudaSuccess; +} +inline cudaError_t cudaMemcpy2DAsync(void*, std::size_t, const void*, std::size_t, + std::size_t, std::size_t, int, cudaStream_t) { + const std::string operation = ++state.copy_calls == 1 ? "copy_y" : "copy_uv"; + state.events.push_back(operation); + return state.fault.find(operation) == 0 ? 1 : cudaSuccess; +} +""" + +_NVDEC = r""" +#pragma once +#include "cuda.h" +enum cudaVideoCodec { cudaVideoCodec_H264, cudaVideoCodec_HEVC }; +enum cudaVideoSurfaceFormat { cudaVideoSurfaceFormat_NV12, cudaVideoSurfaceFormat_P016 }; +class NvDecoder { +public: + template explicit NvDecoder(Args...) {} + int Decode(const uint8_t*, int) { + state.events.push_back("decode"); + if (state.fault == "decode") throw std::runtime_error("injected decode error"); + return state.fault == "no_frame" ? 0 : 1; + } + uint8_t* GetLockedFrame() { + if (state.fault == "null_frame") return nullptr; + if (state.locked) throw std::runtime_error("frame still locked"); + state.locked = true; + state.events.push_back("lock"); + return state.surface; + } + void UnlockFrame(uint8_t** frame) { + state.events.push_back("unlock"); + state.locked = false; + *frame = nullptr; + } + int GetWidth() { return state.fault == "size_mismatch" ? 8 : 4; } + int GetHeight() { return 2; } + int GetDeviceFramePitch() { return 4; } + int GetLumaPlaneSize() { return 8; } + cudaVideoSurfaceFormat GetOutputFormat() { + return state.fault == "format" ? cudaVideoSurfaceFormat_P016 : cudaVideoSurfaceFormat_NV12; + } +}; +""" + +_HARNESS = r""" +#include "nv_video_decoder.hpp" +#include "nv12_to_rgba.cuh" +#include "test_state.h" +#include + +namespace camera_receiver::codec { +void launch_nv12_to_rgba(const uint8_t*, const uint8_t*, int, int, int, int, + uint8_t*, int, bool, int, cudaStream_t) { + state.events.push_back("rgba"); + if (state.fault.find("launch") == 0) throw std::runtime_error("injected launch error"); +} +void launch_nv12_rotate_180(const uint8_t*, const uint8_t*, int, int, int, int, + uint8_t*, int, cudaStream_t) { + state.events.push_back("rotate"); + if (state.fault.find("launch") == 0) throw std::runtime_error("injected launch error"); +} +} + +int main(int argc, char** argv) { + if (argc != 3) return 2; + using namespace camera_receiver::codec; + const std::string mode = argv[1]; + const std::string fault = argv[2]; + DecoderConfig config; + config.width = 4; + config.height = 2; + NvVideoDecoder decoder(config); + state.events.clear(); + state.fault = fault; + uint8_t output[32] = {}; + const uint8_t packet[] = {0, 0, 1}; + auto decode = [&] { + return mode == "rgba" + ? decoder.decode(packet, sizeof(packet), reinterpret_cast(output), 16) + : decoder.decode_nv12(packet, sizeof(packet), reinterpret_cast(output), + 4, mode == "nv12_rotate" ? 180 : 0); + }; + std::string error; + bool produced = false; + try { produced = decode(); } + catch (const std::exception& exception) { error = exception.what(); } + + bool failed = false; + auto require = [&](bool condition, const char* message) { + if (!condition) { std::cerr << message << '\n'; failed = true; } + }; + const bool drops = fault == "no_frame" || fault == "null_frame" || fault == "size_mismatch"; + const bool throws = fault != "none" && !drops; + require(!error.empty() == throws, "unexpected exception outcome"); + require(produced == (fault == "none"), "unexpected frame result"); + require(state.context_depth == 0, "CUDA context push/pop is unbalanced"); + require(!state.locked, "NVDEC frame remains locked"); + auto count = [&](const std::string& event) { + return std::count(state.events.begin(), state.events.end(), event); + }; + auto position = [&](const std::string& event) { + return std::find(state.events.begin(), state.events.end(), event) - state.events.begin(); + }; + const bool frame_locked = fault != "decode" && fault != "no_frame" && fault != "null_frame"; + require(count("unlock") == (frame_locked ? 1 : 0), "frame must be unlocked exactly once"); + require(count("pop") == 1, "context must be popped exactly once"); + if (frame_locked) require(position("unlock") < position("pop"), "unlock must precede context pop"); + const bool submitted = count("rgba") || count("rotate") || count("copy_y"); + if (submitted) { + require(count("sync") == 1, "submitted GPU work must synchronize exactly once"); + require(position("sync") < position("unlock"), "GPU reads must finish before frame unlock"); + } + if (fault == "format") { + require(!submitted, "unsupported format reached an 8-bit GPU operation"); + require(error.find("only 8-bit NV12") != std::string::npos, "missing format error"); + } + if (fault.find("launch") == 0) + require(error == "injected launch error", "cleanup replaced the launch exception"); + if (fault.find("copy_uv") == 0) { + require(count("copy_y") == 1 && count("copy_uv") == 1, "UV failure did not follow Y copy"); + require(error.find("NV12 UV") != std::string::npos, "cleanup replaced the UV copy exception"); + } + if (!failed) { + state.fault = "none"; + state.copy_calls = 0; + require(decode(), "decoder did not accept the next packet"); + require(state.context_depth == 0 && !state.locked, "next packet leaked decoder resources"); + } + return failed ? 1 : 0; +} +""" + + +@pytest.fixture(scope="module") +def native_decoder_probe(tmp_path_factory): + compiler = shutil.which("c++") + if compiler is None: + pytest.skip("native decoder cleanup tests require a C++17 compiler") + build = tmp_path_factory.mktemp("decoder_cleanup") + for name, content in { + "test_state.h": _STATE, + "cuda.h": _CUDA_DRIVER, + "cuda_runtime.h": _CUDA_RUNTIME, + "NvDecoder/NvDecoder.h": _NVDEC, + "probe.cpp": _HARNESS, + }.items(): + target = build / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(content) + codec = Path(__file__).resolve().parents[1] / "codec" + executable = build / "decoder_cleanup" + subprocess.run( + [ + compiler, + "-std=c++17", + "-Wall", + "-Wextra", + "-Werror", + f"-I{build}", + f"-I{codec}", + f"-I{codec / 'inc'}", + str(codec / "nv_video_decoder.cpp"), + str(build / "probe.cpp"), + "-o", + str(executable), + ], + check=True, + capture_output=True, + text=True, + timeout=30, + ) + return executable + + +@pytest.mark.parametrize("mode", ["rgba", "nv12", "nv12_rotate"]) +@pytest.mark.parametrize( + "fault", + ["none", "format", "sync", "decode", "no_frame", "null_frame", "size_mismatch"], +) +def test_native_decoder_resources_and_format(native_decoder_probe, mode, fault): + result = subprocess.run( + [str(native_decoder_probe), mode, fault], + check=False, + capture_output=True, + text=True, + timeout=5, + ) + assert result.returncode == 0, result.stdout + result.stderr + + +@pytest.mark.parametrize( + "mode,fault", + [ + ("rgba", "launch"), + ("rgba", "launch_cleanup_sync"), + ("nv12_rotate", "launch"), + ("nv12_rotate", "launch_cleanup_sync"), + ("nv12", "copy_y"), + ("nv12", "copy_uv"), + ("nv12", "copy_uv_cleanup_sync"), + ], +) +def test_native_decoder_cleans_up_gpu_submission_errors( + native_decoder_probe, mode, fault +): + result = subprocess.run( + [str(native_decoder_probe), mode, fault], + check=False, + capture_output=True, + text=True, + timeout=5, + ) + assert result.returncode == 0, result.stdout + result.stderr From 18871abff490b2d73bc4be67715d48c6b2b9d6f2 Mon Sep 17 00:00:00 2001 From: Gareth Morgan Date: Tue, 10 Nov 2026 22:55:07 -0500 Subject: [PATCH 5/7] camera_streamer: answer the clock exchange over UDP on the sender's CLOCK_MONOTONIC ClockResponder is a stateless, unauthenticated UDP responder that lives as long as the sender process, whether or not a receiver is connected. A datagram of exactly 13 bytes ('Q', u32 seq, i64 t1, little-endian) is answered to its source with 29 bytes ('R', seq, t1 echoed, t2, t3) where t2 is read as soon as the receive returns and t3 immediately before the send, both on CLOCK_MONOTONIC; anything else is ignored. The requester matches replies to its outstanding (seq, t1) and resets on a media session change, so the responder keeps no session state. network.clock_port (0 = off, the default) turns it on. Tests cover the payload encoding, a loopback round trip, bursts, malformed datagrams, concurrent clients, prompt shutdown, bind errors and the config key. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Gareth Morgan --- .../argus_sender/CMakeLists.txt | 2 + .../argus_sender/cmake/Tests.cmake | 14 + .../argus_sender/src/ClockExchange.cpp | 60 ++++ .../argus_sender/src/ClockExchange.h | 35 +++ .../argus_sender/src/ClockResponder.cpp | 113 +++++++ .../argus_sender/src/ClockResponder.h | 50 +++ .../argus_sender/src/Config.cpp | 7 +- .../camera_streamer/argus_sender/src/Config.h | 2 + .../argus_sender/src/SenderSession.cpp | 22 ++ .../tests/test_clock_exchange.cpp | 167 ++++++++++ .../tests/test_clock_responder.cpp | 284 ++++++++++++++++++ .../argus_sender/tests/test_config.cpp | 11 + 12 files changed, 766 insertions(+), 1 deletion(-) create mode 100644 examples/camera_streamer/argus_sender/src/ClockExchange.cpp create mode 100644 examples/camera_streamer/argus_sender/src/ClockExchange.h create mode 100644 examples/camera_streamer/argus_sender/src/ClockResponder.cpp create mode 100644 examples/camera_streamer/argus_sender/src/ClockResponder.h create mode 100644 examples/camera_streamer/argus_sender/tests/test_clock_exchange.cpp create mode 100644 examples/camera_streamer/argus_sender/tests/test_clock_responder.cpp diff --git a/examples/camera_streamer/argus_sender/CMakeLists.txt b/examples/camera_streamer/argus_sender/CMakeLists.txt index 58b60a4685..bc19ba4221 100644 --- a/examples/camera_streamer/argus_sender/CMakeLists.txt +++ b/examples/camera_streamer/argus_sender/CMakeLists.txt @@ -135,6 +135,8 @@ add_executable(argus_sender src/FrameBundleAssembler.cpp src/StreamProcessor.cpp src/CaptureCoordinator.cpp + src/ClockExchange.cpp + src/ClockResponder.cpp src/SenderSession.cpp src/Runtime.cpp src/CameraCatalog.cpp diff --git a/examples/camera_streamer/argus_sender/cmake/Tests.cmake b/examples/camera_streamer/argus_sender/cmake/Tests.cmake index a120f16aa8..6a620c895c 100644 --- a/examples/camera_streamer/argus_sender/cmake/Tests.cmake +++ b/examples/camera_streamer/argus_sender/cmake/Tests.cmake @@ -106,6 +106,20 @@ add_executable(argus_sender_test_tcp_transport argus_sender_add_cpp_test(argus_sender_test_tcp_transport tcp_transport) set_tests_properties(tcp_transport PROPERTIES LABELS "loopback") +add_executable(argus_sender_test_clock_exchange + tests/test_clock_exchange.cpp + src/ClockExchange.cpp +) +argus_sender_add_cpp_test(argus_sender_test_clock_exchange clock_exchange) + +add_executable(argus_sender_test_clock_responder + tests/test_clock_responder.cpp + src/ClockExchange.cpp + src/ClockResponder.cpp +) +argus_sender_add_cpp_test(argus_sender_test_clock_responder clock_responder) +set_tests_properties(clock_responder PROPERTIES LABELS "loopback") + option(ARGUS_SENDER_HARDWARE_TESTS "Enable tests that capture from physical stereo cameras" OFF) diff --git a/examples/camera_streamer/argus_sender/src/ClockExchange.cpp b/examples/camera_streamer/argus_sender/src/ClockExchange.cpp new file mode 100644 index 0000000000..f204da70ec --- /dev/null +++ b/examples/camera_streamer/argus_sender/src/ClockExchange.cpp @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "ClockExchange.h" + +#include + +namespace argus_sender +{ +namespace +{ + +uint64_t readLe(const uint8_t* data, int bytes) +{ + uint64_t value = 0; + for (int i = bytes - 1; i >= 0; --i) + value = (value << 8) | data[i]; + return value; +} + +void appendLe(std::string* out, uint64_t value, int bytes) +{ + for (int i = 0; i < bytes; ++i) + out->push_back(static_cast((value >> (8 * i)) & 0xFF)); +} + +} // namespace + +bool parseClockRequest(const uint8_t* data, size_t size, uint32_t* seq, int64_t* t1) +{ + if (!data || size != kClockRequestBytes || data[0] != 'Q') + return false; + *seq = static_cast(readLe(data + 1, 4)); + *t1 = static_cast(readLe(data + 5, 8)); + return true; +} + +std::string buildClockReply(uint32_t seq, int64_t t1, int64_t t2, int64_t t3) +{ + std::string reply; + reply.reserve(kClockReplyBytes); + reply.push_back('R'); + appendLe(&reply, seq, 4); + appendLe(&reply, static_cast(t1), 8); + appendLe(&reply, static_cast(t2), 8); + appendLe(&reply, static_cast(t3), 8); + return reply; +} + +int64_t monotonicClockNs() +{ + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return static_cast(ts.tv_sec) * 1000000000LL + ts.tv_nsec; +} + +} // namespace argus_sender diff --git a/examples/camera_streamer/argus_sender/src/ClockExchange.h b/examples/camera_streamer/argus_sender/src/ClockExchange.h new file mode 100644 index 0000000000..b3150cdc6e --- /dev/null +++ b/examples/camera_streamer/argus_sender/src/ClockExchange.h @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ARGUS_SENDER_CLOCK_EXCHANGE_H +#define ARGUS_SENDER_CLOCK_EXCHANGE_H + +// The four-timestamp clock exchange payloads, little-endian: a request is 'Q', u32 seq, i64 t1 (the requester's send +// time) and the reply is 'R', u32 seq, i64 t1 echoed, i64 t2 (the responder's receive time) and i64 t3 (the responder's +// send time). The responder's times are on its CLOCK_MONOTONIC. + +#include +#include +#include + +namespace argus_sender +{ + +const size_t kClockRequestBytes = 13; +const size_t kClockReplyBytes = 29; + +// Parses a request; false unless `data` is exactly one well-formed request. +bool parseClockRequest(const uint8_t* data, size_t size, uint32_t* seq, int64_t* t1); + +// Builds the reply to a request received at t2 and answered at t3. +std::string buildClockReply(uint32_t seq, int64_t t1, int64_t t2, int64_t t3); + +// CLOCK_MONOTONIC in nanoseconds. +int64_t monotonicClockNs(); + +} // namespace argus_sender + +#endif // ARGUS_SENDER_CLOCK_EXCHANGE_H diff --git a/examples/camera_streamer/argus_sender/src/ClockResponder.cpp b/examples/camera_streamer/argus_sender/src/ClockResponder.cpp new file mode 100644 index 0000000000..0843568abf --- /dev/null +++ b/examples/camera_streamer/argus_sender/src/ClockResponder.cpp @@ -0,0 +1,113 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "ClockResponder.h" + +#include "ClockExchange.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace argus_sender +{ + +struct ClockResponder::Impl +{ + int fd = -1; + uint16_t port = 0; + std::atomic stop{ false }; + std::atomic answered{ 0 }; + std::thread thread; + + void run() + { + // One byte more than a request, so an oversized datagram is seen as oversized instead of truncated to look + // valid. + uint8_t buffer[kClockRequestBytes + 1]; + while (!stop.load()) + { + sockaddr_in source; + socklen_t sourceLength = sizeof(source); + const ssize_t received = + ::recvfrom(fd, buffer, sizeof(buffer), 0, reinterpret_cast(&source), &sourceLength); + // As close to the arrival as the receive call allows. + const int64_t t2 = monotonicClockNs(); + if (received < 0) + continue; // the receive timeout, or a signal: look at the stop flag again + uint32_t seq = 0; + int64_t t1 = 0; + if (!parseClockRequest(buffer, static_cast(received), &seq, &t1)) + continue; + const std::string reply = buildClockReply(seq, t1, t2, monotonicClockNs()); + if (::sendto(fd, reply.data(), reply.size(), 0, reinterpret_cast(&source), sourceLength) == + static_cast(reply.size())) + { + ++answered; + } + } + } +}; + +ClockResponder::ClockResponder(const ClockResponderConfig& config) : m_impl(new Impl()) +{ + m_impl->fd = ::socket(AF_INET, SOCK_DGRAM, 0); + if (m_impl->fd < 0) + throw std::runtime_error(std::string("clock responder: cannot create a socket: ") + std::strerror(errno)); + sockaddr_in address; + std::memset(&address, 0, sizeof(address)); + address.sin_family = AF_INET; + address.sin_port = htons(config.port); + if (::inet_pton(AF_INET, config.bindHost.c_str(), &address.sin_addr) != 1) + { + ::close(m_impl->fd); + throw std::runtime_error("clock responder: \"" + config.bindHost + "\" is not an IPv4 address"); + } + if (::bind(m_impl->fd, reinterpret_cast(&address), sizeof(address)) != 0) + { + const std::string why = std::strerror(errno); + ::close(m_impl->fd); + throw std::runtime_error("clock responder: cannot bind " + config.bindHost + ":" + std::to_string(config.port) + + ": " + why); + } + socklen_t length = sizeof(address); + ::getsockname(m_impl->fd, reinterpret_cast(&address), &length); + m_impl->port = ntohs(address.sin_port); + // The thread looks at the stop flag between datagrams; the timeout bounds how long that takes. + timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 100000; + ::setsockopt(m_impl->fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + m_impl->thread = std::thread(&Impl::run, m_impl.get()); +} + +ClockResponder::~ClockResponder() +{ + m_impl->stop = true; + if (m_impl->thread.joinable()) + m_impl->thread.join(); + ::close(m_impl->fd); +} + +uint16_t ClockResponder::port() const +{ + return m_impl->port; +} + +uint64_t ClockResponder::answered() const +{ + return m_impl->answered.load(); +} + +} // namespace argus_sender diff --git a/examples/camera_streamer/argus_sender/src/ClockResponder.h b/examples/camera_streamer/argus_sender/src/ClockResponder.h new file mode 100644 index 0000000000..1b6f607cb2 --- /dev/null +++ b/examples/camera_streamer/argus_sender/src/ClockResponder.h @@ -0,0 +1,50 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ARGUS_SENDER_CLOCK_RESPONDER_H +#define ARGUS_SENDER_CLOCK_RESPONDER_H + +// Answers the clock exchange (see ClockExchange.h) over UDP on the sender's CLOCK_MONOTONIC, for as long as the object +// lives. It is stateless and unauthenticated, like the AVS1 stream it sits beside, and exposes nothing but the sender's +// own clock readings. Anything that is not exactly one request is ignored. The requester matches replies to its +// outstanding (seq, t1), so a late reply from before a restart is rejected by the client. + +#include +#include +#include + +namespace argus_sender +{ + +struct ClockResponderConfig +{ + std::string bindHost = "0.0.0.0"; + // 0 picks a free port; read it back with port(). + uint16_t port = 0; +}; + +class ClockResponder +{ +public: + // Starts answering. Throws std::runtime_error with the reason if the address cannot be bound. + explicit ClockResponder(const ClockResponderConfig& config); + ~ClockResponder(); + + ClockResponder(const ClockResponder&) = delete; + ClockResponder& operator=(const ClockResponder&) = delete; + + uint16_t port() const; + // Requests answered so far. + uint64_t answered() const; + +private: + struct Impl; + std::unique_ptr m_impl; +}; + +} // namespace argus_sender + +#endif // ARGUS_SENDER_CLOCK_RESPONDER_H diff --git a/examples/camera_streamer/argus_sender/src/Config.cpp b/examples/camera_streamer/argus_sender/src/Config.cpp index 2c6e4c6de7..f2908f5e1f 100644 --- a/examples/camera_streamer/argus_sender/src/Config.cpp +++ b/examples/camera_streamer/argus_sender/src/Config.cpp @@ -311,15 +311,17 @@ bool loadConfigFile(const std::string& path, AppConfig* config, std::string* err if (root["network"]) { const YAML::Node network = root["network"]; - const std::set allowed = { "bind", "port", "reconnect_ms", "dump_wire" }; + const std::set allowed = { "bind", "port", "reconnect_ms", "dump_wire", "clock_port" }; if (!rejectUnknown(network, allowed, "network", error) || !readOptional(network, "bind", "network", &config->network.bindHost, error)) { return false; } uint32_t port = config->network.port; + uint32_t clockPort = config->network.clockPort; if (!readUint32(network, "port", "network", &port, error) || !readUint32(network, "reconnect_ms", "network", &config->network.reconnectDelayMs, error) || + !readUint32(network, "clock_port", "network", &clockPort, error) || !readOptional(network, "dump_wire", "network", &config->network.dumpWirePath, error)) { return false; @@ -327,6 +329,9 @@ bool loadConfigFile(const std::string& path, AppConfig* config, std::string* err if (port > std::numeric_limits::max()) return fail(error, "network.port must be in 1..65535"); config->network.port = static_cast(port); + if (clockPort > std::numeric_limits::max()) + return fail(error, "network.clock_port must be 0 (off) or in 1..65535"); + config->network.clockPort = static_cast(clockPort); } if (root["runtime"]) diff --git a/examples/camera_streamer/argus_sender/src/Config.h b/examples/camera_streamer/argus_sender/src/Config.h index d574ddcf7a..70a205871e 100644 --- a/examples/camera_streamer/argus_sender/src/Config.h +++ b/examples/camera_streamer/argus_sender/src/Config.h @@ -103,6 +103,8 @@ struct NetworkConfig uint16_t port = 5000; uint32_t reconnectDelayMs = 1000; std::string dumpWirePath; + // UDP port answering the clock exchange for the life of the process (see ClockResponder.h); 0 turns it off. + uint16_t clockPort = 0; }; struct RuntimeConfig diff --git a/examples/camera_streamer/argus_sender/src/SenderSession.cpp b/examples/camera_streamer/argus_sender/src/SenderSession.cpp index df459fe79d..2f21a7208e 100644 --- a/examples/camera_streamer/argus_sender/src/SenderSession.cpp +++ b/examples/camera_streamer/argus_sender/src/SenderSession.cpp @@ -7,16 +7,38 @@ #include "SenderSession.h" #include "CaptureCoordinator.h" +#include "ClockResponder.h" #include "Config.h" #include "Runtime.h" #include +#include +#include namespace argus_sender { bool executeConfiguredSender(const AppConfig& config) { + // Answers clock requests for as long as the process runs, whether or not a receiver is connected. + std::unique_ptr clockResponder; + if (config.network.clockPort != 0) + { + try + { + ClockResponderConfig clockConfig; + clockConfig.bindHost = config.network.bindHost; + clockConfig.port = config.network.clockPort; + clockResponder.reset(new ClockResponder(clockConfig)); + } + catch (const std::exception& e) + { + std::fprintf(stderr, "%s\n", e.what()); + return false; + } + std::printf("Clock responder on UDP %s:%u\n", config.network.bindHost.c_str(), clockResponder->port()); + } + uint64_t reconnectSession = 0; while (!stopRequested()) { diff --git a/examples/camera_streamer/argus_sender/tests/test_clock_exchange.cpp b/examples/camera_streamer/argus_sender/tests/test_clock_exchange.cpp new file mode 100644 index 0000000000..049bcdcc11 --- /dev/null +++ b/examples/camera_streamer/argus_sender/tests/test_clock_exchange.cpp @@ -0,0 +1,167 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "ClockExchange.h" +#include "TestHarness.h" + +#include +#include +#include + +namespace +{ + +using argus_sender::buildClockReply; +using argus_sender::kClockReplyBytes; +using argus_sender::kClockRequestBytes; +using argus_sender::parseClockRequest; + +void putLe(uint8_t* out, uint64_t value, int bytes) +{ + for (int i = 0; i < bytes; ++i) + out[i] = static_cast((value >> (8 * i)) & 0xFF); +} + +std::string makeRequest(uint32_t seq, int64_t t1) +{ + uint8_t bytes[kClockRequestBytes]; + bytes[0] = 'Q'; + putLe(bytes + 1, seq, 4); + putLe(bytes + 5, static_cast(t1), 8); + return std::string(reinterpret_cast(bytes), sizeof(bytes)); +} + +const uint8_t* bytesOf(const std::string& s) +{ + return reinterpret_cast(s.data()); +} + +bool parse(const std::string& request, uint32_t* seq, int64_t* t1) +{ + return parseClockRequest(bytesOf(request), request.size(), seq, t1); +} + +int64_t posixMonotonicNs() +{ + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return static_cast(ts.tv_sec) * 1000000000LL + ts.tv_nsec; +} + +} // namespace + +int main() +{ + TestContext test("clock_exchange"); + + TEST_CHECK_EQ(test, kClockRequestBytes, static_cast(13)); + TEST_CHECK_EQ(test, kClockReplyBytes, static_cast(29)); + + // Valid requests. + { + uint32_t seq = 0; + int64_t t1 = 0; + TEST_CHECK(test, parse(makeRequest(7, 123456789012345LL), &seq, &t1)); + TEST_CHECK_EQ(test, seq, 7U); + TEST_CHECK_EQ(test, t1, 123456789012345LL); + + TEST_CHECK(test, parse(makeRequest(0xFFFFFFFFU, -42), &seq, &t1)); + TEST_CHECK_EQ(test, seq, 0xFFFFFFFFU); + TEST_CHECK_EQ(test, t1, static_cast(-42)); + + TEST_CHECK(test, parse(makeRequest(0, INT64_MIN), &seq, &t1)); + TEST_CHECK_EQ(test, t1, INT64_MIN); + TEST_CHECK(test, parse(makeRequest(1, INT64_MAX), &seq, &t1)); + TEST_CHECK_EQ(test, t1, INT64_MAX); + } + + // Explicit byte layout of a request. + { + const uint8_t raw[13] = { 'Q', 0x04, 0x03, 0x02, 0x01, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; + uint32_t seq = 0; + int64_t t1 = 0; + TEST_CHECK(test, parseClockRequest(raw, sizeof(raw), &seq, &t1)); + TEST_CHECK_EQ(test, seq, 0x01020304U); + TEST_CHECK_EQ(test, t1, 0x0102030405060708LL); + } + + // Rejections. + { + const std::string good = makeRequest(5, 99); + uint32_t seq = 111; + int64_t t1 = 222; + TEST_CHECK(test, !parseClockRequest(bytesOf(good), 12, &seq, &t1)); + TEST_CHECK(test, !parseClockRequest(bytesOf(good), 0, &seq, &t1)); + const std::string longer = good + 'x'; + TEST_CHECK_EQ(test, longer.size(), static_cast(14)); + TEST_CHECK(test, !parseClockRequest(bytesOf(longer), longer.size(), &seq, &t1)); + std::string wrongTag = good; + wrongTag[0] = 'X'; + TEST_CHECK(test, !parse(wrongTag, &seq, &t1)); + wrongTag[0] = 'q'; + TEST_CHECK(test, !parse(wrongTag, &seq, &t1)); + wrongTag[0] = 'R'; + TEST_CHECK(test, !parse(wrongTag, &seq, &t1)); + TEST_CHECK(test, !parseClockRequest(nullptr, kClockRequestBytes, &seq, &t1)); + TEST_CHECK(test, !parseClockRequest(nullptr, 0, &seq, &t1)); + } + + // Reply layout. + { + const int64_t t1 = -5; + const int64_t t2 = 0x1122334455667788LL; + const int64_t t3 = (1LL << 32) + 0x0A0B0C0DLL; + const std::string reply = buildClockReply(0xA1B2C3D4U, t1, t2, t3); + TEST_CHECK_EQ(test, reply.size(), kClockReplyBytes); + const uint8_t* b = bytesOf(reply); + TEST_CHECK_EQ(test, static_cast(b[0]), static_cast('R')); + const uint8_t expected[28] = { + 0xD4, 0xC3, 0xB2, 0xA1, // seq + 0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // t1 = -5 + 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // t2 + 0x0D, 0x0C, 0x0B, 0x0A, 0x01, 0x00, 0x00, 0x00 // t3 = 2^32 + 0x0A0B0C0D (bytes 4..7 = 01 00 00 00) + }; + for (size_t i = 0; i < sizeof(expected); ++i) + TEST_CHECK_EQ(test, static_cast(b[1 + i]), static_cast(expected[i])); + } + + // Round trip. + { + const int64_t t1 = 1000000000000LL; + const std::string request = makeRequest(31337, t1); + uint32_t seq = 0; + int64_t parsedT1 = 0; + TEST_CHECK(test, parse(request, &seq, &parsedT1)); + const std::string reply = buildClockReply(seq, parsedT1, t1 + 100, t1 + 250); + TEST_CHECK_EQ(test, reply.size(), kClockReplyBytes); + // The echoed (seq, t1) in the reply equal the request's bytes 1..12. + TEST_CHECK(test, reply.compare(1, 12, request, 1, 12) == 0); + TEST_CHECK(test, reply[0] == 'R'); + } + + // Clock. + { + int64_t previous = argus_sender::monotonicClockNs(); + bool nonDecreasing = true; + for (int i = 0; i < 1000; ++i) + { + const int64_t now = argus_sender::monotonicClockNs(); + if (now < previous) + nonDecreasing = false; + previous = now; + } + TEST_CHECK(test, nonDecreasing); + + const int64_t before = posixMonotonicNs(); + const int64_t ours = argus_sender::monotonicClockNs(); + const int64_t after = posixMonotonicNs(); + TEST_CHECK(test, before <= ours); + TEST_CHECK(test, ours <= after); + TEST_CHECK(test, ours > 0); + } + + return test.finish(); +} diff --git a/examples/camera_streamer/argus_sender/tests/test_clock_responder.cpp b/examples/camera_streamer/argus_sender/tests/test_clock_responder.cpp new file mode 100644 index 0000000000..9bed065dfb --- /dev/null +++ b/examples/camera_streamer/argus_sender/tests/test_clock_responder.cpp @@ -0,0 +1,284 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#include "ClockExchange.h" +#include "ClockResponder.h" +#include "TestHarness.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +using argus_sender::kClockReplyBytes; +using argus_sender::kClockRequestBytes; + +class UdpClient +{ +public: + explicit UdpClient(uint16_t serverPort, int timeoutMs = 1000) + { + m_fd = ::socket(AF_INET, SOCK_DGRAM, 0); + timeval timeout; + timeout.tv_sec = timeoutMs / 1000; + timeout.tv_usec = (timeoutMs % 1000) * 1000; + ::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + std::memset(&m_server, 0, sizeof(m_server)); + m_server.sin_family = AF_INET; + m_server.sin_port = htons(serverPort); + ::inet_pton(AF_INET, "127.0.0.1", &m_server.sin_addr); + } + + ~UdpClient() + { + if (m_fd >= 0) + ::close(m_fd); + } + + UdpClient(const UdpClient&) = delete; + UdpClient& operator=(const UdpClient&) = delete; + + bool ok() const + { + return m_fd >= 0; + } + + bool send(const std::string& data) + { + return ::sendto(m_fd, data.data(), data.size(), 0, reinterpret_cast(&m_server), + sizeof(m_server)) == static_cast(data.size()); + } + + // Returns the datagram received, or false on timeout. + bool receive(std::string* data, int timeoutMs) + { + timeval timeout; + timeout.tv_sec = timeoutMs / 1000; + timeout.tv_usec = (timeoutMs % 1000) * 1000; + ::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + char buffer[256]; + const ssize_t n = ::recv(m_fd, buffer, sizeof(buffer), 0); + if (n < 0) + return false; + data->assign(buffer, static_cast(n)); + return true; + } + +private: + int m_fd = -1; + sockaddr_in m_server; +}; + +void putLe(std::string* out, uint64_t value, int bytes) +{ + for (int i = 0; i < bytes; ++i) + out->push_back(static_cast((value >> (8 * i)) & 0xFF)); +} + +uint64_t getLe(const std::string& data, size_t offset, int bytes) +{ + uint64_t value = 0; + for (int i = bytes - 1; i >= 0; --i) + value = (value << 8) | static_cast(data[offset + static_cast(i)]); + return value; +} + +std::string makeRequest(uint32_t seq, int64_t t1) +{ + std::string request(1, 'Q'); + putLe(&request, seq, 4); + putLe(&request, static_cast(t1), 8); + return request; +} + +struct Reply +{ + uint32_t seq; + int64_t t1; + int64_t t2; + int64_t t3; +}; + +bool decodeReply(const std::string& data, Reply* reply) +{ + if (data.size() != kClockReplyBytes || data[0] != 'R') + return false; + reply->seq = static_cast(getLe(data, 1, 4)); + reply->t1 = static_cast(getLe(data, 5, 8)); + reply->t2 = static_cast(getLe(data, 13, 8)); + reply->t3 = static_cast(getLe(data, 21, 8)); + return true; +} + +argus_sender::ClockResponderConfig loopbackConfig() +{ + argus_sender::ClockResponderConfig config; + config.bindHost = "127.0.0.1"; + config.port = 0; + return config; +} + +} // namespace + +int main() +{ + TestContext test("clock_responder"); + + // One request, timestamps bracketed by the test's own clock readings. + { + argus_sender::ClockResponder responder(loopbackConfig()); + TEST_CHECK(test, responder.port() != 0); + UdpClient client(responder.port()); + TEST_CHECK(test, client.ok()); + const int64_t before = argus_sender::monotonicClockNs(); + TEST_CHECK(test, client.send(makeRequest(0xFFFFFFF0U, -1234))); + std::string data; + TEST_CHECK(test, client.receive(&data, 1000)); + const int64_t after = argus_sender::monotonicClockNs(); + TEST_CHECK_EQ(test, data.size(), kClockReplyBytes); + Reply reply; + TEST_CHECK(test, decodeReply(data, &reply)); + TEST_CHECK_EQ(test, reply.seq, 0xFFFFFFF0U); + TEST_CHECK_EQ(test, reply.t1, static_cast(-1234)); + TEST_CHECK(test, reply.t2 <= reply.t3); + TEST_CHECK(test, before <= reply.t2); + TEST_CHECK(test, reply.t3 <= after); + } + + // Back-to-back requests, and the answered() count. + { + argus_sender::ClockResponder responder(loopbackConfig()); + UdpClient client(responder.port()); + int matched = 0; + for (uint32_t seq = 0; seq < 50; ++seq) + { + std::string data; + Reply reply; + if (client.send(makeRequest(seq, 1000 + seq)) && client.receive(&data, 1000) && decodeReply(data, &reply) && + reply.seq == seq && reply.t1 == static_cast(1000 + seq)) + { + ++matched; + } + } + TEST_CHECK_EQ(test, matched, 50); + TEST_CHECK_EQ(test, responder.answered(), static_cast(50)); + } + + // Anything that is not exactly one request gets no reply; a valid one afterwards still works. + { + argus_sender::ClockResponder responder(loopbackConfig()); + UdpClient client(responder.port()); + const std::string good = makeRequest(9, 77); + std::string wrongTag = good; + wrongTag[0] = 'X'; + const std::vector bad = { std::string(), good.substr(0, 12), good + 'x', wrongTag }; + for (const std::string& datagram : bad) + { + TEST_CHECK(test, client.send(datagram)); + std::string data; + TEST_CHECK(test, !client.receive(&data, 300)); + } + TEST_CHECK_EQ(test, responder.answered(), static_cast(0)); + + TEST_CHECK(test, client.send(good)); + std::string data; + Reply reply; + TEST_CHECK(test, client.receive(&data, 1000)); + TEST_CHECK(test, decodeReply(data, &reply)); + TEST_CHECK_EQ(test, reply.seq, 9U); + TEST_CHECK_EQ(test, reply.t1, static_cast(77)); + TEST_CHECK_EQ(test, responder.answered(), static_cast(1)); + } + + // Two clients each get their own reply. + { + argus_sender::ClockResponder responder(loopbackConfig()); + UdpClient a(responder.port()); + UdpClient b(responder.port()); + TEST_CHECK(test, a.send(makeRequest(1, 111))); + TEST_CHECK(test, b.send(makeRequest(2, 222))); + std::string dataA; + std::string dataB; + TEST_CHECK(test, a.receive(&dataA, 1000)); + TEST_CHECK(test, b.receive(&dataB, 1000)); + Reply replyA; + Reply replyB; + TEST_CHECK(test, decodeReply(dataA, &replyA)); + TEST_CHECK(test, decodeReply(dataB, &replyB)); + TEST_CHECK_EQ(test, replyA.seq, 1U); + TEST_CHECK_EQ(test, replyA.t1, static_cast(111)); + TEST_CHECK_EQ(test, replyB.seq, 2U); + TEST_CHECK_EQ(test, replyB.t1, static_cast(222)); + TEST_CHECK_EQ(test, responder.answered(), static_cast(2)); + } + + // Destruction is prompt. + { + std::unique_ptr responder(new argus_sender::ClockResponder(loopbackConfig())); + const auto start = std::chrono::steady_clock::now(); + responder.reset(); + const auto elapsed = std::chrono::steady_clock::now() - start; + TEST_CHECK(test, elapsed < std::chrono::seconds(1)); + } + + // The same address cannot be bound twice. + { + argus_sender::ClockResponder first(loopbackConfig()); + argus_sender::ClockResponderConfig config = loopbackConfig(); + config.port = first.port(); + bool threw = false; + std::string message; + try + { + argus_sender::ClockResponder second(config); + } + catch (const std::runtime_error& e) + { + threw = true; + message = e.what(); + } + TEST_CHECK(test, threw); + TEST_CHECK(test, !message.empty()); + } + + // A bind host that is not an IPv4 address. + { + for (const char* host : { "not-an-address", "::1", "" }) + { + argus_sender::ClockResponderConfig config = loopbackConfig(); + config.bindHost = host; + bool threw = false; + std::string message; + try + { + argus_sender::ClockResponder responder(config); + } + catch (const std::runtime_error& e) + { + threw = true; + message = e.what(); + } + TEST_CHECK(test, threw); + TEST_CHECK(test, !message.empty()); + } + } + + return test.finish(); +} diff --git a/examples/camera_streamer/argus_sender/tests/test_config.cpp b/examples/camera_streamer/argus_sender/tests/test_config.cpp index 97c3c47d9e..b8f9d728cc 100644 --- a/examples/camera_streamer/argus_sender/tests/test_config.cpp +++ b/examples/camera_streamer/argus_sender/tests/test_config.cpp @@ -75,6 +75,7 @@ int main() std::string error; TEST_CHECK(test, loadText(validConfig(), &config, &error)); TEST_CHECK_EQ(test, config.streams.size(), static_cast(2)); + TEST_CHECK_EQ(test, config.network.clockPort, static_cast(0)); TEST_CHECK_EQ(test, config.alignment.warmupFrames, 17U); TEST_CHECK_EQ(test, config.alignment.intervalFrames, 29U); TEST_CHECK_EQ(test, config.runtime.logIntervalSeconds, 5U); @@ -258,5 +259,15 @@ int main() " remap: {file: m.remap, capture_width: 2560, capture_height: 1984, supersample: 2}\n", &invalid, &error)); TEST_CHECK(test, error.find("unknown key") != std::string::npos); + { + const std::string streams = "streams:\n - {id: a, type: mono, cameras: [{device: 0}], output: {fps: 30}}\n"; + argus_sender::AppConfig clockConfig; + TEST_CHECK(test, loadText("version: 1\nnetwork: {clock_port: 5611}\n" + streams, &clockConfig, &error)); + TEST_CHECK_EQ(test, clockConfig.network.clockPort, static_cast(5611)); + TEST_CHECK(test, loadText("version: 1\nnetwork: {clock_port: 65535}\n" + streams, &clockConfig, &error)); + TEST_CHECK_EQ(test, clockConfig.network.clockPort, static_cast(65535)); + TEST_CHECK(test, !loadText("version: 1\nnetwork: {clock_port: 65536}\n" + streams, &clockConfig, &error)); + TEST_CHECK(test, error.find("clock_port") != std::string::npos); + } return test.finish(); } From 68e7ed264f16e1a596def20737e55f1db671f1bc Mon Sep 17 00:00:00 2001 From: Gareth Morgan Date: Tue, 10 Nov 2026 23:09:55 -0500 Subject: [PATCH 6/7] camera_streamer: document the clock exchange and network.clock_port Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Gareth Morgan --- examples/camera_streamer/argus_sender/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/examples/camera_streamer/argus_sender/README.md b/examples/camera_streamer/argus_sender/README.md index dea875962e..d374fada2f 100644 --- a/examples/camera_streamer/argus_sender/README.md +++ b/examples/camera_streamer/argus_sender/README.md @@ -97,6 +97,7 @@ network: port: 5000 reconnect_ms: 1000 dump_wire: "" # optional exact TCP-record dump + clock_port: 0 # UDP port that answers clock requests; 0 = off runtime: frames: 0 # 0 = run until stopped/disconnected @@ -290,6 +291,21 @@ raw streams immediately after its transform returns. It is not the later time at which the coordinator waits for that worker. These metrics are diagnostic metadata only and do not alter the AVS1 wire format. +## Clock exchange + +With `network.clock_port` set, the sender answers clock requests over UDP on that port for as long as the process runs, +whether or not a receiver is connected. A request is exactly 13 bytes (`Q`, `u32 seq`, `i64 t1`) and the reply 29 bytes +(`R`, `u32 seq`, `i64 t1`, `i64 t2`, `i64 t3`), all little-endian. `t2` is the sender's `CLOCK_MONOTONIC` read as soon as +the request arrives and `t3` the same clock read just before the reply is sent, in nanoseconds. A requester that records +its own send time `t1` and receive time `t4` gets the round-trip delay `(t4 - t1) - (t3 - t2)` and the offset between the +two clocks `((t2 - t1) + (t3 - t4)) / 2`, with an error up to half of any difference between the two network directions. +Any other datagram is ignored. + +The responder keeps no state, has no authentication and returns nothing but the sender's own clock readings, like the AVS1 +stream beside it, so keep the port on a trusted camera network. A requester should accept only replies that match a +request it still has outstanding, and should discard its outstanding requests and its estimate when the AVS1 `session_id` +changes or the connection is re-established; the responder cannot tell a late reply from a current one. + ## Wire dump Set `network.dump_wire` to save the exact length-prefixed AVS1 records that were @@ -307,6 +323,7 @@ See [WIRE_FORMAT.md](WIRE_FORMAT.md) for the byte-level format. ## Source layout +- `ClockExchange.*`, `ClockResponder.*`: the clock-exchange payloads and the UDP responder. - `Config.*`: YAML parsing and validation. - `CaptureManager.*` / `CaptureDomainPlan.*` / `PriorityFrameHandoff.h`: independent priority-eye acquisition and latest-frame slots, event-driven secondary Argus domains, configurable EGL mailbox/FIFO consumers, and timing. - `CapturePriorityPolicy.*`: priority-stream selection, startup synchronization tolerance, and secondary release timing. From ca8c6cdae3bfb1efd3c53dc846efe629c2fadc69 Mon Sep 17 00:00:00 2001 From: Gareth Morgan Date: Thu, 12 Nov 2026 18:28:33 -0500 Subject: [PATCH 7/7] camera_streamer: say what a clock-exchange offset does and does not establish A requester should check the reply's source, the estimate's age and the session id as well as the outstanding request, and an offset between two clocks is not a claim about when a sensor exposed a frame. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Gareth Morgan --- examples/camera_streamer/argus_sender/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/camera_streamer/argus_sender/README.md b/examples/camera_streamer/argus_sender/README.md index d374fada2f..30ab3e062a 100644 --- a/examples/camera_streamer/argus_sender/README.md +++ b/examples/camera_streamer/argus_sender/README.md @@ -306,6 +306,13 @@ stream beside it, so keep the port on a trusted camera network. A requester shou request it still has outstanding, and should discard its outstanding requests and its estimate when the AVS1 `session_id` changes or the connection is re-established; the responder cannot tell a late reply from a current one. +A requester that turns the exchange into an offset should also check that a reply comes from the sender's own address, +that its estimate is recent (discard it when no fresh reply has arrived for a while), and that the `session_id` has not +changed. The exchange measures the offset between two clocks and its uncertainty. It says nothing about when a sensor +exposed a frame: a capture time derived through it is the sensor's own timestamp, with that timestamp's caveats, +moved onto the requester's clock. Do not present the offset or its error bound as a claim of exact physical capture +time. + ## Wire dump Set `network.dump_wire` to save the exact length-prefixed AVS1 records that were