Skip to main content

matrice_streaming — Streaming Gateway KT Docs

This folder is the knowledge-transfer reference for matrice_streaming, the Streaming Gateway (SG). It is written to be walked through in a live KT session: a presenter takes each chapter top to bottom with a new engineer. Every chapter is self-contained. Chapters 01–06 follow the same shape — an "In one minute" summary, a code-free high-level walkthrough, a "Going deeper" section with file/line anchors into the source, and a configuration table; 07 is a flat reference and 08 an appendix.

What the Streaming Gateway is

SG is the frame producer of the Matrice video pipeline. Given a set of cameras, it opens each stream (RTSP fronted by MediaMTX, or file/HTTP sources), decodes frames at a target FPS, and publishes every decoded frame into a per-camera shared-memory ring buffer that downstream processes — primarily the inference engine — attach to and read zero-copy. It runs hundreds of cameras per host and manages the camera set fully dynamically: cameras are added, updated, and removed at runtime without restarting the process, driven by Kafka events plus a periodic backend poll.

There are two decode backends, chosen at construction:

  • NVDEC (GPU) — the production path. Demuxed compressed packets are decoded by NVIDIA's hardware decoder; frames stay on the GPU as NV12 surfaces and are handed to the inference engine through CUDA IPC, never touching host RAM. This path gets the depth in these docs (chapter 03).
  • OpenCV (CPU) — a cv2.VideoCapture-based fallback for hosts without a usable GPU decode stack. It is covered briefly in chapter 05.

SG does not run inference and does not force preprocessing: frames are published at native resolution and the inference engine owns resize/letterbox/color conversion.

Where SG sits

 Cameras (RTSP via MediaMTX / file / HTTP)
              │
              ▼
 ┌─────────────────────────────────────────────────┐
 │  Streaming Gateway (this package)               │
 │  demux → decode (NVDEC on GPU in prod) →        │
 │  publish into per-camera ring buffers           │
 └─────────────────────────────────────────────────┘
              │  /dev/shm DataBus ring buffers
              │  (NVDEC: CUDA IPC handles to GPU-resident NV12 frames)
              ▼
 Inference Engine ──► results ──► analytics / results-agg
              ▲
              │  control plane
 ┌─────────────────────────────────────────────────┐
 │  Matrice backend: REST API (camera list, Kafka  │
 │  connection info, status) + Kafka (camera       │
 │  add/remove/update events, stop commands,       │
 │  heartbeats, metrics)                           │
 └─────────────────────────────────────────────────┘

Three external dependencies, and only three: the backend REST API (via the matrice_common SDK's Session.rpc), Kafka, and shared memory via matrice_common.stream (DataBus / CUDA IPC ring buffers). The matrice_common SDK has its own KT doc set in the py_common repo; chapter 04 here explains the publish boundary from SG's side so you do not need that set to follow these docs.

Session reading order

Read the chapters in order; each builds on the picture established by the previous one, but none requires flipping back.

# Chapter One line
01 01-architecture-overview.md The big map: StreamingAction → StreamingGateway → one decode backend, the process/thread model, lifecycle states, and where every output goes.
02 02-camera-management.md How the camera set stays in sync at runtime: InstanceEventListener (Kafka + periodic poll), the dynamic camera manager, config diffing, and phantom-camera self-healing.
03 03-nvdec-pipeline.md The production decode path in depth: demuxers, codec handling, one worker process per GPU, the decoder pool, GPU placement, and the watchdog.
04 04-publishing-and-matrice-common.md The publish boundary: DataBus addresses, ring-buffer semantics, CUDA IPC handle sharing, frame counters, and what SG cleans up (or deliberately leaves) on stop.
05 05-opencv-path.md The CPU fallback, briefly: an async worker-process pool sharding cameras, JPEG/BGR publishing through the same DataBus addresses.
06 06-metrics-and-heartbeat.md Health reporting: per-camera metrics collection/aggregation, heartbeat payloads, and the Kafka topics the backend watches.
07 07-configuration-and-ops.md Environment variables and constructor parameters in one place, plus production symptoms → causes → knobs.
08 08-appendix-other.md Everything that is real but not on the main path: LocalDecoder, the frame-optimizer seam, camera tampering (Case 1), and other supporting utilities.
— 11-motion-optimizer-test-results.md Motion frame optimizer validation (unit, E2E, threshold tuning).
— 14-camera-tampering-test-results.md Blank-screen tampering detector validation on labelled clips + NVDEC E2E.

Suggested emphasis for a session: 01 and 03 carry most of the weight; 02 and 04 are where the subtle bugs have historically lived; 05 is a skim; 06–08 are reference.

Public API in one glance

Everything external lives under matrice_streaming.streaming_gateway: StreamingAction (the production entry point — turns an action_id into a running, monitored, auto-restarting gateway), StreamingGateway (the orchestrator underneath, used directly in tests or custom control loops), InstanceStreamingGatewayUtil / StreamingGatewayUtil (backend API clients), InputStream (the per-camera config dataclass), and InstanceEventListener (Kafka-driven camera reconciliation). Chapter 01 walks the relationships between them.

Glossary

  • Stream key — the gateway's internal handle for one camera stream; maps 1:1 to a camera_id in the gateway's camera↔stream-key map.
  • Camera config / InputStream — the per-camera configuration dataclass (source URL, target FPS, codec, camera ids/keys, location, topic). width=0/height=0 means native resolution — SG does not resize by default.
  • Demuxer — the component that splits a container or transport (RTSP, MP4) into raw compressed video packets (H.264/H.265 NAL units) ready for the decoder.
  • NVDEC — NVIDIA's fixed-function hardware video decoder; the production decode backend. Decoding costs essentially no CPU and no CUDA compute.
  • NV12 — a YUV 4:2:0 pixel format (full-resolution Y plane plus interleaved UV at half resolution); the native output of NVDEC. As a buffer it is (H*1.5, W) uint8.
  • DataBus address — the name a producer publishes under and a consumer attaches to, {camera_id}__sg__frames, backed by a /dev/shm/databus__… file. Both decode backends publish frames under the same address scheme.
  • CUDA IPC — the CUDA mechanism for sharing a GPU memory buffer between processes without copying it to host RAM. On the NVDEC path the shared-memory ring buffer carries CUDA IPC handles; the actual NV12 pixels never leave the GPU.
  • Phantom camera — a camera the gateway believes it is streaming but which has no live frame ring buffer in /dev/shm. The event listener detects phantoms and self-heals by re-adding the camera (chapter 02).

Release files for matrice-streaming 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matrice-streaming 0.7.0
File Size Uploaded
matrice_streaming-0.7.0.tar.gz 362.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for matrice-streaming 0.7.0
File Interpreter ABI Platform
matrice_streaming-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 805.3 kB

Release files / matrice_streaming-0.7.0.tar.gz

Download URL matrice_streaming-0.7.0.tar.gz
Size 362.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bedd8757c38b4fc112991327a3d1599e23e9a19f5abfb1636eacca54ae8454e6
BLAKE2b-256 checksum
How to use checksums
bda47265c97ffa2dd25beb3c87690c7de5b666a02394a2e3cbaace1d823905c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / matrice_streaming-0.7.0-py3-none-any.whl

Download URL matrice_streaming-0.7.0-py3-none-any.whl
Size 442.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69e75871e2b96c0139e5b46818c579c46246c60644c77acb3b449984c0e1df7a
BLAKE2b-256 checksum
How to use checksums
04da581aca9062fc5cfe47cc798127f00ab59e0d701683b713c069ba469ea58f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.16.0

2 release files

0.15.2

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.132

1 release file

0.1.131

1 release file

0.1.130

1 release file

0.1.129

1 release file

0.1.128

1 release file

0.1.127

1 release file

0.1.126

1 release file

0.1.125

1 release file

0.1.101

1 release file

0.1.100

1 release file

0.1.99

1 release file

0.1.98

1 release file

0.1.97

1 release file

0.1.96

1 release file

0.1.95

1 release file

0.1.94

1 release file

0.1.93

1 release file

0.1.92

1 release file

0.1.88

2 release files

0.1.87

2 release files

0.1.86

2 release files

0.1.85

2 release files

0.1.84

2 release files

0.1.83

2 release files

0.1.82

2 release files

0.1.81

2 release files

0.1.80

2 release files

0.1.79

2 release files

0.1.78

2 release files

0.1.77

2 release files

0.1.75

1 release file

0.1.74

2 release files

0.1.73

2 release files

0.1.72

2 release files

0.1.71

2 release files

0.1.70

2 release files

0.1.60

2 release files

0.1.59

2 release files

0.1.58

2 release files

0.1.57

2 release files

0.1.56

2 release files

0.1.55

2 release files

0.1.54

2 release files

0.1.53

2 release files

0.1.52

2 release files

0.1.51

2 release files

0.1.50

2 release files

0.1.49

2 release files

0.1.48

2 release files

0.1.47

2 release files

0.1.46

2 release files

0.1.45

2 release files

0.1.44

2 release files

0.1.43

2 release files

0.1.42

2 release files

0.1.41

2 release files

0.1.40

2 release files

0.1.30

2 release files

0.1.29

2 release files

0.1.28

2 release files

0.1.27

2 release files

0.1.26

2 release files

0.1.25

2 release files

0.1.24

2 release files

0.1.23

2 release files

0.1.22

2 release files

0.1.21

2 release files

0.1.20

2 release files

0.1.19

2 release files

0.1.18

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.15

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page