Skip to content

feat(pycvc): zero-copy stream producer/consumer bindings (Phase 2 PR4) - #519

Merged
transfix merged 2 commits into
masterfrom
feat/pycvc-stream
Oct 1, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/pycvc-stream

Conversation

@transfix

@transfix transfix commented Oct 1, 2026

Copy link
Copy Markdown
Owner

New bindings/pycvc/pycvc_stream.i exposes cvc::stream to Python with a numpy surface, %included after pycvc_image.i so it reuses the ArrayView out-typemap + pycvc_owner capsule. The raw classes (unique_ptr<stream>, shared_ptr<subscription>, shared_ptr<const frame>) are wrapped in thin value-holder shims (Stream/StreamSub/StreamFrame) carrying the shared_ptrs internally — SWIG copies the shim, never the C++ object.

s   = cvc.stream_open(app, "cam0", "rgba8", 1920, 1080)
sub = s.subscribe("latest", 3)      # or "ring"
f   = sub.latest()                  # f.valid()/seq()/pts()/width()/height()
arr = f.numpy()                     # zero-copy (H,W,C) uint8 view, read-only
seq = s.publish(ndarray_uint8, pts) # zero-copy producer: aliases the ndarray
  • Consumer: Frame.numpy() → read-only zero-copy view whose numpy base pins the frame_ptr (and so its slab / producer ndarray) alive.
  • Producer: publish() rejects (never coerces) a non-uint8 / non-C-contiguous / wrong-size array, Py_INCREFs it, and aliases its buffer via publish_external (PR3) with a GIL-safe DECREF keepalive (the last frame_ptr may drop off-thread). pop() releases the GIL while blocking. The Python Stream keeps the app alive (_pycvc_app) so ~stream never touches a GC'd app (boost::lock_error).

Folds in the PR3-review follow-ups now that publish_external is wired into a producer: F1 — stream::update_stats() uses channel_->total_published() (includes external publishes); F2/F3 — debug asserts in publish_external (non-null data + keepalive).

Test test_pycvc_stream.py: producer→consumer zero-copy roundtrip (np.shares_memory + mutate-through), malformed-array rejection, ring drop-oldest order, close/reopen. 4/4 pass; pycvc exec suite still 8/8; stream_test 20/20. (Built locally with the system swig in build-ari; CI builds the full cpXX matrix with the cvcpkg swig.) Depends on PR3 (merged).

New bindings/pycvc/pycvc_stream.i exposes cvc::stream to Python with a numpy
surface, %included after pycvc_image.i so it reuses the ArrayView out-typemap +
pycvc_owner capsule. The raw classes (unique_ptr<stream>, shared_ptr<subscription>,
shared_ptr<const frame>) are wrapped in thin VALUE-HOLDER shims (Stream/StreamSub/
StreamFrame) that carry the shared_ptrs internally — SWIG copies the shim, never
the C++ object.

  s   = cvc.stream_open(app, "cam0", "rgba8", 1920, 1080)
  sub = s.subscribe("latest", 3)      # or "ring"
  f   = sub.latest()                  # StreamFrame; f.valid()/seq()/pts()/width()/height()
  arr = f.numpy()                     # zero-copy (H,W,C) uint8 view, read-only
  seq = s.publish(ndarray_uint8, pts) # zero-copy producer: aliases the ndarray

- CONSUMER: Frame.numpy() returns a read-only zero-copy view whose numpy base pins
  the frame_ptr (so its slab / producer ndarray) alive.
- PRODUCER: publish() REJECTS (never coerces) a non-uint8 / non-C-contiguous /
  wrong-size array, Py_INCREF's it, and aliases its buffer via publish_external
  (PR3) with a GIL-safe DECREF keepalive (the last frame_ptr may drop off-thread).
- pop() releases the GIL while blocking. The Python Stream keeps the app alive
  (_pycvc_app keepalive) so ~stream never touches a GC'd app (boost::lock_error).

Folds in the PR3 review follow-ups now that publish_external is wired into a
producer: F1 — stream::update_stats() uses channel_->total_published() (includes
external publishes; pool_->total_published() undercounted them); F2/F3 — debug
asserts in publish_external that data + keepalive are non-null.

Test test_pycvc_stream.py (pycvc build + pytest, no GL): producer->consumer
zero-copy roundtrip (np.shares_memory + mutate-through), malformed-array
rejection, ring drop-oldest order, close/reopen. 4/4 pass; pycvc exec suite
still 8/8; stream_test 20/20.
…GIL/int64 fixes

Adversarial review of PR4 (verdict SHIP-AFTER-FIXES; GIL/refcount + sub/frame-
outlives-app confirmed correct). Fold in the one real defect + hardening:

- M1 (real): publish() used PyArray_ISCARRAY, which also requires WRITEABLE, so it
  rejected valid READ-ONLY C-contiguous uint8 arrays — e.g. np.frombuffer(decoder
  bytes) / mmap, the most common zero-copy producer sources. Use PyArray_ISCARRAY_RO
  (C-contiguous + aligned, no writable requirement); the buffer is aliased read-only
  so source writability is irrelevant. New test publishes a read-only frombuffer array.
- M2: StreamFrame.numpy() now rejects a padded-stride (stride != w*ch) or short
  (size < h*w*ch) frame instead of building a misaligned/OOB dense view. Safe today
  (stream_open is dense + publish validates size) but a latent trap for a host-C++
  or future-codec frame.
- M3: ~PyArrayKeepalive guards PyGILState_Ensure with Py_IsInitialized() — if a
  frame_ptr outlives Py_Finalize (a host C++ thread), leak the ref rather than crash.
- L1: seq()/publish() return long long (int64), not long, so seq doesn't truncate on
  Windows LLP64 past 2^31.

5/5 stream tests pass.
@transfix
transfix merged commit 811a3a6 into master Oct 1, 2026
13 checks passed
@transfix
transfix deleted the feat/pycvc-stream branch October 29, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant