-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context manager for AvailableData #122
Merged
aliddell
merged 16 commits into
acquire-project:main
from
nclack:68-availabledata-relies-on-deletion-for-cleanup-1
Dec 13, 2023
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2ac1e6a
(wip)
nclack 0bbd173
(wip) testing test_repeat_acq
nclack 3ce9e05
(wip)
nclack 74f6609
close but not quite right
nclack c055019
Merge remote-tracking branch 'upstream/main' into 68-availabledata-re…
aliddell 14bebdc
Update test_write_external_metadata_to_zarr to use new AvailableData …
aliddell 69f74d1
Updates for stubtest.
aliddell 2661fb1
Update minor release version.
aliddell d780af6
Remove explicit `del packet` statements as no longer necessary.
aliddell b422481
Merge branch 'main' into 68-availabledata-relies-on-deletion-for-clea…
aliddell e4a9617
Remove one more `packet = None` statement.
aliddell 15f088d
Merge branch '68-availabledata-relies-on-deletion-for-cleanup-1' of g…
aliddell 7cf06a9
Merge remote-tracking branch 'upstream/main' into 68-availabledata-re…
aliddell abb094d
Run formatters.
aliddell f4a573d
Update acquire-video-runtime.
aliddell ff23b80
make flake8 happy
aliddell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
[package] | ||
name = "acquire-imaging" | ||
authors = ["Nathan Clack <[email protected]>"] | ||
version = "0.2.1" | ||
version = "0.3.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "acquire" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
pyo3 = { version = "0.19", features = [ | ||
pyo3 = { version = "0.20", features = [ | ||
"extension-module", | ||
"anyhow", | ||
"abi3-py38", | ||
"serde", | ||
] } | ||
pyo3-log = "0.8" | ||
numpy = "0.19" | ||
pyo3-log = "0.9" | ||
numpy = "0.20" | ||
log = "0.4" | ||
anyhow = "1.0" | ||
parking_lot = "0.12" | ||
serde = { version = "1.0", features = ["derive"] } | ||
pythonize = "0.19" | ||
pythonize = "0.20.0" | ||
|
||
[build-dependencies] | ||
bindgen = "0.66" | ||
bindgen = "0.69.1" | ||
cmake = "0.1" | ||
http = "0.2" | ||
http = "1.0" | ||
json = "0.12" | ||
reqwest = { version = "0.11", features = ["blocking", "json"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,22 @@ impl RawRuntime { | |
unsafe { capi::acquire_abort(self.inner.as_ptr()) }.ok()?; | ||
Ok(()) | ||
} | ||
|
||
fn map_read(&self, stream_id: u32) -> Result<(*mut capi::VideoFrame, *mut capi::VideoFrame)> { | ||
let mut beg = null_mut(); | ||
let mut end = null_mut(); | ||
unsafe { | ||
capi::acquire_map_read(self.inner.as_ptr(), stream_id, &mut beg, &mut end).ok()?; | ||
} | ||
Ok((beg, end)) | ||
} | ||
|
||
fn unmap_read(&self, stream_id: u32, consumed_bytes: usize) -> Result<()> { | ||
unsafe { | ||
capi::acquire_unmap_read(self.inner.as_ptr(), stream_id, consumed_bytes).ok()?; | ||
} | ||
Ok(()) | ||
} | ||
Comment on lines
+82
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
} | ||
|
||
impl Drop for RawRuntime { | ||
|
@@ -175,34 +191,11 @@ impl Runtime { | |
Python::allow_threads(py, || Ok(self.inner.execute_trigger(stream_id)?)) | ||
} | ||
|
||
fn get_available_data(&self, stream_id: u32) -> PyResult<Option<AvailableData>> { | ||
let mut beg = null_mut(); | ||
let mut end = null_mut(); | ||
unsafe { | ||
capi::acquire_map_read(self.as_ref().as_ptr(), stream_id, &mut beg, &mut end).ok()?; | ||
} | ||
let nbytes = unsafe { byte_offset_from(beg, end) }; | ||
if nbytes > 0 { | ||
log::trace!( | ||
"[stream {}] ACQUIRED {:p}-{:p}:{} bytes", | ||
stream_id, | ||
beg, | ||
end, | ||
nbytes | ||
) | ||
}; | ||
Ok(if nbytes > 0 { | ||
Some(AvailableData { | ||
inner: Arc::new(RawAvailableData { | ||
runtime: self.inner.clone(), | ||
beg: NonNull::new(beg).ok_or(anyhow!("Expected non-null buffer"))?, | ||
end: NonNull::new(end).ok_or(anyhow!("Expected non-null buffer"))?, | ||
stream_id, | ||
consumed_bytes: None, | ||
}), | ||
}) | ||
} else { | ||
None | ||
fn get_available_data(&self, stream_id: u32) -> PyResult<AvailableDataContext> { | ||
Ok(AvailableDataContext { | ||
inner: self.inner.clone(), | ||
stream_id, | ||
available_data: None, | ||
}) | ||
} | ||
} | ||
|
@@ -271,62 +264,116 @@ impl Drop for RawAvailableData { | |
self.end.as_ptr(), | ||
consumed_bytes | ||
); | ||
unsafe { | ||
capi::acquire_unmap_read( | ||
self.runtime.inner.as_ptr(), | ||
self.stream_id, | ||
consumed_bytes as _, | ||
) | ||
.ok() | ||
.expect("Unexpected failure: Was the CoreRuntime NULL?"); | ||
} | ||
self.runtime | ||
.unmap_read(self.stream_id, consumed_bytes) | ||
.expect("Unexpected failure: Was the runtime NULL?"); | ||
} | ||
} | ||
|
||
#[pyclass] | ||
pub(crate) struct AvailableData { | ||
inner: Arc<RawAvailableData>, | ||
inner: Option<Arc<RawAvailableData>>, | ||
} | ||
|
||
#[pymethods] | ||
impl AvailableData { | ||
fn get_frame_count(&self) -> usize { | ||
self.inner.get_frame_count() | ||
if let Some(inner) = &self.inner { | ||
inner.get_frame_count() | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
fn frames(&self) -> VideoFrameIterator { | ||
VideoFrameIterator { | ||
store: self.inner.clone(), | ||
cur: Mutex::new(self.inner.beg), | ||
end: self.inner.end, | ||
inner: if let Some(frames) = &self.inner { | ||
Some(VideoFrameIteratorInner { | ||
store: frames.clone(), | ||
cur: Mutex::new(frames.beg), | ||
end: frames.end, | ||
}) | ||
} else { | ||
None | ||
}, | ||
} | ||
} | ||
|
||
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<VideoFrameIterator>> { | ||
Py::new(slf.py(), slf.frames()) | ||
} | ||
|
||
fn invalidate(&mut self) { | ||
// Will drop the RawAvailableData and cause Available data to act like | ||
// an empty iterator. | ||
self.inner = None; | ||
} | ||
} | ||
|
||
#[pyclass] | ||
struct VideoFrameIterator { | ||
store: Arc<RawAvailableData>, | ||
cur: Mutex<NonNull<capi::VideoFrame>>, | ||
end: NonNull<capi::VideoFrame>, | ||
pub(crate) struct AvailableDataContext { | ||
inner: Arc<RawRuntime>, | ||
stream_id: u32, | ||
available_data: Option<Py<AvailableData>>, | ||
} | ||
|
||
unsafe impl Send for VideoFrameIterator {} | ||
|
||
#[pymethods] | ||
impl VideoFrameIterator { | ||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
slf | ||
impl AvailableDataContext { | ||
fn __enter__(&mut self) -> PyResult<Option<Py<AvailableData>>> { | ||
let AvailableDataContext { | ||
inner, | ||
stream_id, | ||
available_data, | ||
} = self; | ||
let stream_id = *stream_id; | ||
let (beg, end) = inner.map_read(stream_id)?; | ||
let nbytes = unsafe { byte_offset_from(beg, end) }; | ||
if nbytes > 0 { | ||
log::trace!( | ||
"[stream {}] ACQUIRED {:p}-{:p}:{} bytes", | ||
stream_id, | ||
beg, | ||
end, | ||
nbytes | ||
) | ||
}; | ||
if nbytes > 0 { | ||
*available_data = Some(Python::with_gil(|py| { | ||
Py::new( | ||
py, | ||
AvailableData { | ||
inner: Some(Arc::new(RawAvailableData { | ||
runtime: self.inner.clone(), | ||
beg: NonNull::new(beg).ok_or(anyhow!("Expected non-null buffer"))?, | ||
end: NonNull::new(end).ok_or(anyhow!("Expected non-null buffer"))?, | ||
stream_id, | ||
consumed_bytes: None, | ||
})), | ||
}, | ||
) | ||
})?); | ||
} | ||
return Ok(self.available_data.clone()); | ||
} | ||
fn __next__(&mut self) -> Option<VideoFrame> { | ||
self.next() | ||
|
||
fn __exit__(&mut self, _exc_type: &PyAny, _exc_value: &PyAny, _traceback: &PyAny) { | ||
Python::with_gil(|py| { | ||
if let Some(a) = &self.available_data { | ||
a.as_ref(py).borrow_mut().invalidate() | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
impl Iterator for VideoFrameIterator { | ||
struct VideoFrameIteratorInner { | ||
store: Arc<RawAvailableData>, | ||
cur: Mutex<NonNull<capi::VideoFrame>>, | ||
end: NonNull<capi::VideoFrame>, | ||
} | ||
|
||
unsafe impl Send for VideoFrameIteratorInner {} | ||
|
||
impl Iterator for VideoFrameIteratorInner { | ||
type Item = VideoFrame; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
|
@@ -349,6 +396,29 @@ impl Iterator for VideoFrameIterator { | |
} | ||
} | ||
|
||
#[pyclass] | ||
struct VideoFrameIterator { | ||
inner: Option<VideoFrameIteratorInner>, | ||
} | ||
|
||
#[pymethods] | ||
impl VideoFrameIterator { | ||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
slf | ||
} | ||
fn __next__(&mut self) -> Option<VideoFrame> { | ||
self.next() | ||
} | ||
} | ||
|
||
impl Iterator for VideoFrameIterator { | ||
type Item = VideoFrame; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.inner.as_mut().and_then(|it| it.next()) | ||
} | ||
} | ||
|
||
#[pyclass] | ||
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)] | ||
pub(crate) struct VideoFrameTimestamps { | ||
|
@@ -489,6 +559,5 @@ impl VideoFrame { | |
} | ||
} | ||
|
||
// TODO: Can't ensure the output array doesn't outlive the available data | ||
// TODO: Is everything really Send | ||
// TODO: mark iterable and videoframe as things that can't be shared across threads |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What changed here? Something in bindgen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I beleive this means rerun_on_header_files is on by default