Repository navigation
feat(pycvc): zero-copy stream producer/consumer bindings (Phase 2 PR4) - #519
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New
bindings/pycvc/pycvc_stream.iexposescvc::streamto Python with a numpy surface, %included afterpycvc_image.iso it reuses theArrayViewout-typemap +pycvc_ownercapsule. 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.Frame.numpy()→ read-only zero-copy view whose numpy base pins theframe_ptr(and so its slab / producer ndarray) alive.publish()rejects (never coerces) a non-uint8 / non-C-contiguous / wrong-size array,Py_INCREFs it, and aliases its buffer viapublish_external(PR3) with a GIL-safe DECREF keepalive (the lastframe_ptrmay drop off-thread).pop()releases the GIL while blocking. The PythonStreamkeeps the app alive (_pycvc_app) so~streamnever touches a GC'd app (boost::lock_error).Folds in the PR3-review follow-ups now that
publish_externalis wired into a producer: F1 —stream::update_stats()useschannel_->total_published()(includes external publishes); F2/F3 — debug asserts inpublish_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).