Struct nokhwa::threaded::CallbackCamera
source · pub struct CallbackCamera { /* private fields */ }
output-threaded
only.Expand description
Creates a camera that runs in a different thread that you can use a callback to access the frames of.
It uses a Arc
and a Mutex
to ensure that this feels like a normal camera, but callback based.
See Camera
for more details on the camera itself.
Your function is called every time there is a new frame. In order to avoid frame loss, it should complete before a new frame is available. If you need to do heavy image processing, it may be beneficial to directly pipe the data to a new thread to process it there.
Note that this does not have WGPU
capabilities. This should be implemented in your callback.
SAFETY
The Mutex
guarantees exclusive access to the underlying camera struct. They should be safe to
impl Send
on.
Implementations§
source§impl CallbackCamera
impl CallbackCamera
sourcepub fn new(
index: CameraIndex,
format: RequestedFormat<'_>,
callback: impl FnMut(Buffer) + Send + 'static
) -> Result<Self, NokhwaError>
pub fn new( index: CameraIndex, format: RequestedFormat<'_>, callback: impl FnMut(Buffer) + Send + 'static ) -> Result<Self, NokhwaError>
Create a new ThreadedCamera
from a CameraIndex
and format
Errors
This will error if you either have a bad platform configuration (e.g. input-v4l
but not on linux) or the backend cannot create the camera (e.g. permission denied).
sourcepub fn with_custom(
camera: Camera,
callback: impl FnMut(Buffer) + Send + 'static
) -> Self
pub fn with_custom( camera: Camera, callback: impl FnMut(Buffer) + Send + 'static ) -> Self
Allows creation of a Camera
with a custom backend. This is useful if you are creating e.g. a custom module.
You must have set a format beforehand.
sourcepub fn index(&self) -> &CameraIndex
pub fn index(&self) -> &CameraIndex
Gets the current Camera’s index.
sourcepub fn set_index(&mut self, new_idx: &CameraIndex) -> Result<(), NokhwaError>
pub fn set_index(&mut self, new_idx: &CameraIndex) -> Result<(), NokhwaError>
Sets the current Camera’s index. Note that this re-initializes the camera.
Errors
The Backend may fail to initialize.
sourcepub fn backend(&self) -> Result<ApiBackend, NokhwaError>
pub fn backend(&self) -> Result<ApiBackend, NokhwaError>
Gets the current Camera’s backend
sourcepub fn set_backend(
&mut self,
new_backend: ApiBackend
) -> Result<(), NokhwaError>
pub fn set_backend( &mut self, new_backend: ApiBackend ) -> Result<(), NokhwaError>
Sets the current Camera’s backend. Note that this re-initializes the camera.
Errors
The new backend may not exist or may fail to initialize the new camera.
sourcepub fn info(&self) -> &CameraInfo
pub fn info(&self) -> &CameraInfo
Gets the camera information such as Name and Index as a CameraInfo
.
sourcepub fn camera_format(&self) -> Result<CameraFormat, NokhwaError>
pub fn camera_format(&self) -> Result<CameraFormat, NokhwaError>
Gets the current CameraFormat
.
sourcepub fn set_camera_format(
&mut self,
new_fmt: CameraFormat
) -> Result<(), NokhwaError>
👎Deprecated since 0.10.0: please use set_camera_requset
instead.
pub fn set_camera_format( &mut self, new_fmt: CameraFormat ) -> Result<(), NokhwaError>
set_camera_requset
instead.Will set the current CameraFormat
This will reset the current stream if used while stream is opened.
Errors
If you started the stream and the camera rejects the new camera format, this will return an error.
sourcepub fn set_camera_requset(
&mut self,
request: RequestedFormat<'_>
) -> Result<CameraFormat, NokhwaError>
pub fn set_camera_requset( &mut self, request: RequestedFormat<'_> ) -> Result<CameraFormat, NokhwaError>
Will set the current CameraFormat
, using a [RequestedFormat.
]
This will reset the current stream if used while stream is opened.
This will also update the cache.
This will return the new CameraFormat
Errors
If nothing fits the requested criteria, this will return an error.
sourcepub fn compatible_list_by_resolution(
&mut self,
fourcc: FrameFormat
) -> Result<HashMap<Resolution, Vec<u32>>, NokhwaError>
pub fn compatible_list_by_resolution( &mut self, fourcc: FrameFormat ) -> Result<HashMap<Resolution, Vec<u32>>, NokhwaError>
A hashmap of Resolution
s mapped to framerates
Errors
This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a UnsupportedOperationError
.
sourcepub fn compatible_fourcc(&mut self) -> Result<Vec<FrameFormat>, NokhwaError>
pub fn compatible_fourcc(&mut self) -> Result<Vec<FrameFormat>, NokhwaError>
A Vector of compatible FrameFormat
s.
Errors
This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a UnsupportedOperationError
.
sourcepub fn resolution(&self) -> Result<Resolution, NokhwaError>
pub fn resolution(&self) -> Result<Resolution, NokhwaError>
Gets the current camera resolution (See: Resolution
, CameraFormat
).
sourcepub fn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError>
pub fn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError>
Will set the current Resolution
This will reset the current stream if used while stream is opened.
Errors
If you started the stream and the camera rejects the new resolution, this will return an error.
sourcepub fn frame_rate(&self) -> Result<u32, NokhwaError>
pub fn frame_rate(&self) -> Result<u32, NokhwaError>
Gets the current camera framerate (See: CameraFormat
).
sourcepub fn set_frame_rate(&mut self, new_fps: u32) -> Result<(), NokhwaError>
pub fn set_frame_rate(&mut self, new_fps: u32) -> Result<(), NokhwaError>
Will set the current framerate This will reset the current stream if used while stream is opened.
Errors
If you started the stream and the camera rejects the new framerate, this will return an error.
sourcepub fn frame_format(&self) -> Result<FrameFormat, NokhwaError>
pub fn frame_format(&self) -> Result<FrameFormat, NokhwaError>
Gets the current camera’s frame format (See: FrameFormat
, CameraFormat
).
sourcepub fn set_frame_format(
&mut self,
fourcc: FrameFormat
) -> Result<(), NokhwaError>
pub fn set_frame_format( &mut self, fourcc: FrameFormat ) -> Result<(), NokhwaError>
Will set the current FrameFormat
This will reset the current stream if used while stream is opened.
Errors
If you started the stream and the camera rejects the new frame format, this will return an error.
sourcepub fn supported_camera_controls(
&self
) -> Result<Vec<KnownCameraControl>, NokhwaError>
pub fn supported_camera_controls( &self ) -> Result<Vec<KnownCameraControl>, NokhwaError>
Gets the current supported list of KnownCameraControl
Errors
If the list cannot be collected, this will error. This can be treated as a “nothing supported”.
sourcepub fn camera_controls(&self) -> Result<Vec<CameraControl>, NokhwaError>
pub fn camera_controls(&self) -> Result<Vec<CameraControl>, NokhwaError>
Gets the current supported list of CameraControl
s keyed by its name as a String
.
Errors
If the list cannot be collected, this will error. This can be treated as a “nothing supported”.
sourcepub fn camera_controls_string(
&self
) -> Result<HashMap<String, CameraControl>, NokhwaError>
pub fn camera_controls_string( &self ) -> Result<HashMap<String, CameraControl>, NokhwaError>
Gets the current supported list of CameraControl
s keyed by its name as a String
.
Errors
If the list cannot be collected, this will error. This can be treated as a “nothing supported”.
sourcepub fn camera_controls_known_camera_controls(
&self
) -> Result<HashMap<KnownCameraControl, CameraControl>, NokhwaError>
pub fn camera_controls_known_camera_controls( &self ) -> Result<HashMap<KnownCameraControl, CameraControl>, NokhwaError>
Gets the current supported list of CameraControl
s keyed by its name as a String
.
Errors
If the list cannot be collected, this will error. This can be treated as a “nothing supported”.
sourcepub fn camera_control(
&self,
control: KnownCameraControl
) -> Result<CameraControl, NokhwaError>
pub fn camera_control( &self, control: KnownCameraControl ) -> Result<CameraControl, NokhwaError>
Gets the value of KnownCameraControl
.
Errors
If the control
is not supported or there is an error while getting the camera control values (e.g. unexpected value, too high, etc)
this will error.
sourcepub fn set_camera_control(
&mut self,
id: KnownCameraControl,
control: ControlValueSetter
) -> Result<(), NokhwaError>
pub fn set_camera_control( &mut self, id: KnownCameraControl, control: ControlValueSetter ) -> Result<(), NokhwaError>
Sets the control to control
in the camera.
Usually, the pipeline is calling camera_control()
, getting a camera control that way
then calling value()
to get a ControlValueSetter
and setting the value that way.
Errors
If the control
is not supported, the value is invalid (less than min, greater than max, not in step), or there was an error setting the control,
this will error.
sourcepub fn open_stream(&mut self) -> Result<(), NokhwaError>
pub fn open_stream(&mut self) -> Result<(), NokhwaError>
Will open the camera stream with set parameters. This will be called internally if you try and call frame()
before you call open_stream()
.
The callback will be called every frame.
Errors
If the specific backend fails to open the camera (e.g. already taken, busy, doesn’t exist anymore) this will error.
sourcepub fn set_callback(
&mut self,
callback: impl FnMut(Buffer) + Send + 'static
) -> Result<(), NokhwaError>
pub fn set_callback( &mut self, callback: impl FnMut(Buffer) + Send + 'static ) -> Result<(), NokhwaError>
Sets the frame callback to the new specified function. This function will be called instead of the previous one(s).
sourcepub fn poll_frame(&mut self) -> Result<Buffer, NokhwaError>
pub fn poll_frame(&mut self) -> Result<Buffer, NokhwaError>
Polls the camera for a frame, analogous to Camera::frame
Errors
This will error if the camera fails to capture a frame.
sourcepub fn last_frame(&self) -> Result<Buffer, NokhwaError>
pub fn last_frame(&self) -> Result<Buffer, NokhwaError>
Gets the last frame captured by the camera.
sourcepub fn is_stream_open(&self) -> Result<bool, NokhwaError>
pub fn is_stream_open(&self) -> Result<bool, NokhwaError>
Checks if stream if open. If it is, it will return true.