use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use crate::error::GenicamResult;
use crate::gige::stream::{Frame, FrameChannel, StreamChannel, StreamConfig, StreamStats};
use super::GenICamera;
#[derive(Debug)]
pub struct Acquisition<'a> {
cam: &'a mut GenICamera,
stream: StreamChannel,
frames: FrameChannel,
stopped: bool,
}
impl<'a> Acquisition<'a> {
pub(crate) fn start(cam: &'a mut GenICamera, mut cfg: StreamConfig) -> GenicamResult<Self> {
cam.fill_payload_size(&mut cfg)?;
let capacity = cfg.n_buffers;
let stream = cam.transport().open_stream(cfg)?;
let frames = stream.subscribe(capacity);
if let Err(e) = cam.set_integer("TLParamsLocked", 1) {
tracing::debug!("TLParamsLocked not set: {e}");
}
if let Err(e) = cam.execute("AcquisitionStart") {
if let Err(e) = cam.set_integer("TLParamsLocked", 0) {
tracing::debug!("TLParamsLocked not cleared: {e}");
}
return Err(e);
}
Ok(Self {
cam,
stream,
frames,
stopped: false,
})
}
pub fn wait_for(&self, timeout: Duration) -> Option<Arc<Frame>> {
self.frames.wait_for(timeout)
}
pub fn try_recv(&self) -> Option<Arc<Frame>> {
self.frames.try_recv()
}
pub fn recv_all(&self) -> Vec<Arc<Frame>> {
self.frames.recv_all()
}
pub fn clear(&self) {
self.frames.clear()
}
pub fn frames(&self) -> &FrameChannel {
&self.frames
}
pub fn subscribe(&self, capacity: usize) -> FrameChannel {
self.stream.subscribe(capacity)
}
pub fn stream(&self) -> &StreamChannel {
&self.stream
}
pub fn stats(&self) -> StreamStats {
self.stream.stats()
}
pub fn packet_size(&self) -> u16 {
self.stream.packet_size()
}
pub fn local_addr(&self) -> SocketAddr {
self.stream.local_addr()
}
pub fn features(&mut self) -> super::Features<'_> {
super::Features(self.cam)
}
pub fn stop(mut self) -> GenicamResult<FrameChannel> {
let frames = self.frames.clone();
self.stop_camera().map(|()| frames)
}
fn stop_camera(&mut self) -> GenicamResult<()> {
if self.stopped {
return Ok(());
}
self.stopped = true;
let result = self.cam.execute("AcquisitionStop");
if let Err(e) = self.cam.set_integer("TLParamsLocked", 0) {
tracing::debug!("TLParamsLocked not cleared: {e}");
}
result
}
}
impl Drop for Acquisition<'_> {
fn drop(&mut self) {
if let Err(e) = self.stop_camera() {
tracing::warn!("AcquisitionStop on drop failed: {e}");
}
}
}