Class to represent a single camera device. More...
#include <CamCap.hpp>
Public Types | |
enum | EV4L2_msg_handling { EV4L2_msg_handling::kStdErr, EV4L2_msg_handling::kDiscard, EV4L2_msg_handling::kKeep } |
Public Member Functions | |
Device (const std::string &fn="") | |
Constructor. More... | |
virtual | ~Device (void) |
Destructor. More... | |
virtual Device & | reset (void) |
Close the camera device. More... | |
std::string | describe (void) |
Creates a large block of text describing the device, what it supports, and the current state. More... | |
virtual Device & | set_v4l2_msg_handling (const EV4L2_msg_handling type=EV4L2_msg_handling::kDiscard) |
Set what CamCap should do with messages from libv4l2. More... | |
std::string | get_v4l2_messages (void) |
Obtain the most recent set of libv4l2 messages. More... | |
virtual Device & | initialize (const bool use_libv4l_interface=true) |
Initialize the camera device. More... | |
virtual Device & | initialize (const std::string &fn, const bool use_libv4l_interface=true) |
Initialize the given camera device. More... | |
virtual Device & | initialize (const char *const fn) |
Same as other calls to initialize() , but required to prevent string literals from being incorrectly interpreted as booleans. More... | |
virtual MInputs | get_inputs (void) const |
Get a map of all the inputs supported by this device. More... | |
virtual Device & | set_input_index (uint32_t index=0) |
Set the input device to the specified index. More... | |
virtual MFormats | get_formats (void) const |
Get a map of all the image formats supported by this device. More... | |
virtual v4l2_format | get_format (void) const |
Get a copy of the most recently set image format. More... | |
virtual v4l2_format | set_format (v4l2_format format) |
Set the prefered image format the camera should be using. More... | |
virtual v4l2_format | set_format (v4l2_fmtdesc format_description, const size_t width=0, const size_t height=0) |
Set the prefered image format using one of the enumerated formats returned by get_formats(). More... | |
virtual v4l2_format | set_format (const size_t width, const size_t height) |
Keep the format the same, but specify a specific width and height. More... | |
virtual v4l2_format | set_format (const int pixel_format, const size_t width, const size_t height) |
Set a specific pixel format and image dimension to use. More... | |
virtual Device & | request_memory_mapped_buffers (size_t number_of_buffers_to_use=1) |
Used to request memory blocks used to capture images. More... | |
virtual Device & | release_memory_mapped_buffers (void) |
Release all memory mapped buffers previously allocated through request_memory_mapped_buffers(). More... | |
virtual Device & | stream_start (void) |
Send the V4L call VIDIOC_STREAMON to begin capturing. More... | |
virtual Device & | stream_stop (void) |
Send the V4L call VIDIOC_STREAMOFF to stop capturing. More... | |
virtual Device & | xopen (const std::string &fn) |
Open the video device file. More... | |
virtual Device & | xclose (void) |
Close the video device file. More... | |
virtual void * | xmmap (size_t length, off_t offset) |
Mmap a block of memory. More... | |
virtual Device & | xmunmap (void *addr, size_t length) |
Unmmap a block of memory. More... | |
virtual int | xread (Bytes &bytes) |
Read from the device file descriptor. More... | |
virtual Bytes | capture (void) |
Capture an image from the camera device. More... | |
virtual v4l2_buffer | capture (Bytes &bytes) |
Capture an image from the camera device. More... | |
virtual v4l2_buffer | capture_to_jpeg (const std::string &output_filename) |
Calls capture() and then saves the image to a JPEG file. More... | |
virtual Device & | capture_many (const size_t count, Images &images) |
Capture a sequence of images. More... | |
virtual VStr | capture_many_to_jpeg (const size_t count, const std::string &directory=".") |
Capture a sequence of images as JPEG files. More... | |
virtual v4l2_buffer | capture_using_mmap_buffers (Bytes &bytes) |
Capture an image using memory mapped buffers. More... | |
virtual v4l2_buffer | capture_using_read (Bytes &bytes) |
Capture an image using read() or v4l2_read() . More... | |
virtual std::string | get_name (void) const |
Get the device name, such as /dev/video0 . This is not set until initialize() is called. More... | |
virtual v4l2_capability | get_capabilities (void) const |
The V4L2 device capabilities are used to identify kernel devices compatible with this specification and to obtain information about driver and hardware capabilities. More... | |
virtual MU32Str | get_capability_flags (void) const |
Returns a std::map that describes all the capabilities of this camera device. More... | |
virtual Device & | retry_incomplete_captures (const size_t retries=5) |
Determines how many times the CamCap library will retry a capture if an incomplete buffer is returned from V4L. More... | |
virtual bool | is_streaming_enabled (void) const |
Determine if streaming is enabled. More... | |
virtual Dimensions | get_dimensions (void) |
Query the camera to determine what image sizes are supported. More... | |
virtual Device & | set_libv4l_interface (const bool toggle=true) |
Decides if CamCap accesses the camera directly, or goes through the libv4l interface. More... | |
virtual Device & | set_transfer_method (const ETransferMethod method=ETransferMethod::kMmap) |
Toggle between memory mapped buffers and direct read when capturing images. More... | |
virtual VCtrls | get_all_controls (void) |
Get all of the controls supported by this camera. More... | |
virtual Device & | set_all_controls_to_defaults (void) |
Iterate through the available controls and set all to their default values. More... | |
virtual Device & | set_control (const uint32_t index, const int32_t value) |
Set a specific control to a certain value. More... | |
virtual int32_t | get_control (const uint32_t index) |
Get the current value for the specified control. More... | |
virtual uint32_t | control_name_to_index (std::string name) |
Map control names to control indexes. More... | |
virtual Device & | set_control (const std::string &name, const int32_t value) |
Similar to the other set_control(), but takes a name instead of the control index. More... | |
virtual int32_t | get_control (const std::string &name) |
Similar to the other get_control(), but takes a name instead of the control index. More... | |
virtual std::string | describe_controls (void) |
Return a text string showing the state of all camera device controls. More... | |
virtual bool | is_initialized (void) const |
Determine if the device has been initialized. Set by initialize(). More... | |
virtual bool | is_not_initialized (void) const |
Determine if the device has been initialized. Set by initialize(). More... | |
virtual Device & | xioctl (const int request, void *parm, const std::string &name) |
Perform the requested ioctl() call via v4l2_ioctl() from libv4l2. More... | |
virtual int | xioctl (const int request, void *parm) |
Protected Attributes | |
std::string | fname |
Name of the device that was opened. Set in the constructor or initialize(). Will normally be /dev/video0 . More... | |
int | fd |
File descriptor to the opened camera device. More... | |
bool | initialized |
This is set to true once the device has been properly initialized. More... | |
v4l2_capability | capabilities |
Device capabilities are obtained in initialize(). More... | |
MInputs | inputs |
Map of all the inputs supported by a camera. For most (all?) webcams, there is a single input at index #0. More... | |
MFormats | formats |
Map of all the formats supported by the camera. More... | |
v4l2_format | selected_format |
The most recently set image format. More... | |
MIdxBufferDetail | buffer_details |
Keep track of every memory mapped buffer. More... | |
size_t | retry_count |
Determine if partial captures are kept or rejected. More... | |
bool | streaming_enabled |
State of streaming. More... | |
bool | use_libv4l |
Should CamCap directly access the camera, or use the libv4l interface layer. More... | |
ETransferMethod | transfer_method |
Selected transfer method. More... | |
char * | v4l2_message_ptr |
Needed to track V4L2 messages. More... | |
size_t | v4l2_message_size |
Needed to track V4L2 messages. More... | |
Class to represent a single camera device.
Methods are provided to manipulate the camera and take pictures. These objects are small and can easily be created on the stack. Any buffers or memory needed internally is then allocated from the heap or using standard containers such as std::map and std::vector.
Example code:
|
strong |
Enumerator | |
---|---|
kStdErr | Tell libv4l2 to send all messages to |
kDiscard | Redirects all libv4l2 messages to |
kKeep | Internally store all libv4l2 messages.
|
CC::Device::Device | ( | const std::string & | fn = "" | ) |
Constructor.
[in] | fn | Specifies the device to open. If left blank, then an attempt will be made to auto-detect a device that matches /dev/video* . |
Example:
References CC::kMmap, reset(), and set_v4l2_msg_handling().
|
virtual |
Destructor.
References kStdErr, reset(), and set_v4l2_msg_handling().
|
virtual |
Close the camera device.
This is automatically called by the destructor and initialize() prior to opening a new device.
References capabilities, formats, initialized, inputs, is_initialized(), CC::kMmap, release_memory_mapped_buffers(), retry_count, selected_format, streaming_enabled, transfer_method, use_libv4l, xclose(), and ZERO.
Referenced by Device(), initialize(), and ~Device().
std::string CC::Device::describe | ( | void | ) |
Creates a large block of text describing the device, what it supports, and the current state.
Useful for debugging.
Example output:
References CC::BufferDetail::address, CC::BufferDetail::buffer, buffer_details, capabilities, describe_controls(), fname, CC::fourcc_to_string(), CC::gcd(), get_all_controls(), get_capability_flags(), get_dimensions(), get_formats(), get_inputs(), CC::get_version(), is_initialized(), is_streaming_enabled(), CC::kMmap, CC::kRead, retry_count, selected_format, transfer_method, use_libv4l, V4L2_COLORSPACE_DEFAULT, and V4L2_COLORSPACE_RAW.
Referenced by main().
|
virtual |
Set what CamCap should do with messages from libv4l2.
Default behaviour is to redirect them to /dev/null
.
CC::Device
objects then you'll need to leave it at kStdErr, or manage the v4l2_log_file
file pointer yourself. References kDiscard, kKeep, kStdErr, v4l2_message_ptr, and v4l2_message_size.
Referenced by Device(), and ~Device().
std::string CC::Device::get_v4l2_messages | ( | void | ) |
Obtain the most recent set of libv4l2 messages.
This only works when the v4l2 message handling has been set to EV4L2_msg_handling::kKeep, otherwise an empty string is returned.
References v4l2_message_ptr, and v4l2_message_size.
|
inlinevirtual |
Determine if the device has been initialized. Set by initialize().
Referenced by describe(), initialize(), and reset().
|
inlinevirtual |
Determine if the device has been initialized. Set by initialize().
Referenced by capture(), capture_to_jpeg(), get_all_controls(), get_capability_flags(), get_dimensions(), request_memory_mapped_buffers(), retry_incomplete_captures(), set_all_controls_to_defaults(), set_format(), set_transfer_method(), stream_start(), stream_stop(), and xioctl().
|
virtual |
Initialize the camera device.
Will attempt to scan for an auto-detect a device, and repeatedly call initialize() until a device is initialized, or all video devices have been tried.
[in] | use_libv4l_interface | determines if CamCap accesses the camera directly or through the libv4l interface. Default true is to use libv4l. Set to false to bypass libv4l. This can also be toggled through set_libv4l_interface(). |
std::runtime_error | if a camera device cannot be initialized |
References fname, is_initialized(), reset(), and use_libv4l.
Referenced by capture(), capture_to_jpeg(), get_all_controls(), get_dimensions(), main(), request_memory_mapped_buffers(), retry_incomplete_captures(), set_all_controls_to_defaults(), set_format(), set_transfer_method(), stream_start(), and stream_stop().
|
virtual |
Initialize the given camera device.
Does not make any attempt to auto-detect.
[in] | fn | Provide the name of the camera device to open, such as /dev/video0 . |
[in] | use_libv4l_interface | determines if CamCap accesses the camera directly or through the libv4l interface. Default true is to use libv4l. Set to false to bypass libv4l. This can also be toggled through set_libv4l_interface(). |
std::invalid_argument | if the camera device name is empty |
std::runtime_error | if enumerating the supported image formats fails to return anything |
References capabilities, fname, formats, initialized, inputs, reset(), set_format(), set_input_index(), use_libv4l, xioctl(), xopen(), and ZERO.
|
inlinevirtual |
Same as other calls to initialize()
, but required to prevent string literals from being incorrectly interpreted as booleans.
References initialize().
Referenced by initialize().
|
inlinevirtual |
Get a map of all the inputs supported by this device.
Normally a webcam will have a single input.
Referenced by describe().
|
inlinevirtual |
Set the input device to the specified index.
This is automatically called by initialize() to select the first input index returned by VIDIOC_ENUMINPUT
.
Referenced by initialize().
|
inlinevirtual |
Get a map of all the image formats supported by this device.
Referenced by describe(), and main().
|
inlinevirtual |
Get a copy of the most recently set image format.
Example:
Referenced by capture_to_jpeg(), and main().
|
virtual |
Set the prefered image format the camera should be using.
This includes the image dimensions and byte format.
References initialize(), is_not_initialized(), release_memory_mapped_buffers(), selected_format, and xioctl().
Referenced by capture_to_jpeg(), initialize(), main(), and set_format().
|
virtual |
Set the prefered image format using one of the enumerated formats returned by get_formats().
References initialize(), is_not_initialized(), selected_format, set_format(), V4L2_PIX_FMT_PRIV_MAGIC, and ZERO.
|
virtual |
Keep the format the same, but specify a specific width and height.
[in] | width | The requested image width. If zero, the camera will use the smallest value it supports. |
[in] | height | The requested image height. If zero, the camera will use the smallest value it supports. |
References initialize(), is_not_initialized(), selected_format, and set_format().
|
virtual |
Set a specific pixel format and image dimension to use.
[in] | pixel_format | One of the V4L2 defines, such as V4L2_PIX_FMT_YUYV or V4L2_PIX_FMT_RGB24 |
[in] | width | The requested image width. If zero, the camera will use the smallest value it supports. |
[in] | height | The requested image height. If zero, the camera will use the smallest value it supports. |
References initialize(), is_not_initialized(), selected_format, and set_format().
|
virtual |
Used to request memory blocks used to capture images.
This is called automatically by functions such as initialize() and set_format(). The only reason you'd need to manually this is if you want to manually set the number of buffers to use.
[in] | number_of_buffers_to_use | is usually a small number, such as 1 or 2.. |
References CC::BufferDetail::buffer, buffer_details, initialize(), is_not_initialized(), release_memory_mapped_buffers(), selected_format, xioctl(), xmmap(), and ZERO.
Referenced by capture_using_mmap_buffers(), set_transfer_method(), and stream_start().
|
virtual |
Release all memory mapped buffers previously allocated through request_memory_mapped_buffers().
This is automatically called when necessary, including in the destructor, so normally you shouldn't need to call this directly.
References CC::BufferDetail::address, CC::BufferDetail::buffer, buffer_details, stream_stop(), xioctl(), xmunmap(), and ZERO.
Referenced by capture_using_read(), request_memory_mapped_buffers(), reset(), set_format(), and set_transfer_method().
|
virtual |
Send the V4L call VIDIOC_STREAMON to begin capturing.
This is automatically called when necessary.
References buffer_details, initialize(), is_not_initialized(), CC::kMmap, request_memory_mapped_buffers(), streaming_enabled, transfer_method, and xioctl().
Referenced by capture_many(), capture_many_to_jpeg(), capture_using_mmap_buffers(), and main().
|
virtual |
Send the V4L call VIDIOC_STREAMOFF to stop capturing.
This is automatically called when necessary.
References initialize(), is_not_initialized(), streaming_enabled, and xioctl().
Referenced by release_memory_mapped_buffers().
|
virtual |
Perform the requested ioctl()
call via v4l2_ioctl()
from libv4l2.
[in] | request | V4L2 ioctl request, such as VIDIOC_QUERYCAP . |
[in] | parm | Pointer to V4L2 structure to pass as the 3rd parameter to ioctl() . |
[in] | name | Text name corresponding to the ioctl request. Will be used to generate an error message if the call to ioctl() fails. |
std::runtime_error | if the file descriptor is invalid (device not initialized?) |
std::runtime_error | if the camera device has not yet been fully initialized |
std::runtime_error | if the requested ioctl failed |
References fd, fname, and is_not_initialized().
Referenced by capture_using_mmap_buffers(), get_all_controls(), get_control(), get_dimensions(), initialize(), release_memory_mapped_buffers(), request_memory_mapped_buffers(), set_control(), set_format(), stream_start(), and stream_stop().
|
virtual |
xioctl()
does not throw. References fd, and use_libv4l.
|
virtual |
Open the video device file.
std::runtime_error | if the device named fails to open |
References fd, use_libv4l, and xclose().
Referenced by initialize().
|
virtual |
Close the video device file.
References fd, and use_libv4l.
Referenced by reset(), and xopen().
|
virtual |
Mmap a block of memory.
std::runtime_error | if mmap() fails to return an address to use for a buffer |
References fd, and use_libv4l.
Referenced by request_memory_mapped_buffers().
|
virtual |
Unmmap a block of memory.
References use_libv4l.
Referenced by release_memory_mapped_buffers().
|
virtual |
Read from the device file descriptor.
std::runtime_error | if the read request failed |
References fd, fname, and use_libv4l.
Referenced by capture_using_read().
|
virtual |
Capture an image from the camera device.
This automatically calls initialize() if it hasn't already been called. The format of the captured bytes depends on the currently selected image format.
Example:
Referenced by capture_many(), capture_to_jpeg(), and main().
|
virtual |
Capture an image from the camera device.
This automatically calls initialize() if it hasn't already been called. The format of the captured bytes depends on the currently selected image format.
Example:
References capture_using_mmap_buffers(), capture_using_read(), initialize(), is_not_initialized(), CC::kMmap, CC::kRead, and transfer_method.
|
virtual |
Calls capture() and then saves the image to a JPEG file.
Example:
std::invalid_argument | if the filename is empty |
References capture(), get_format(), initialize(), is_not_initialized(), and set_format().
Referenced by capture_many_to_jpeg(), and main().
|
virtual |
Capture a sequence of images.
[in] | count | Number of images to capture. |
[in,out] | images | Vector of captured images. |
Example:
References CC::CaptureAndBuffer::bytes, capture(), CC::CaptureAndBuffer::info, and stream_start().
|
virtual |
Capture a sequence of images as JPEG files.
[in] | count | Number of images to capture. |
[in] | directory | Directory into which the jpeg files are saved. |
Example:
References capture_to_jpeg(), fname, and stream_start().
Referenced by main().
|
virtual |
Capture an image using memory mapped buffers.
References CC::BufferDetail::address, CC::BufferDetail::buffer, buffer_details, request_memory_mapped_buffers(), retry_count, selected_format, stream_start(), xioctl(), and ZERO.
Referenced by capture().
|
virtual |
Capture an image using read()
or v4l2_read()
.
v4l2_buffer
returned is emulated, and most fields are set to zero. References release_memory_mapped_buffers(), retry_count, selected_format, xread(), and ZERO.
Referenced by capture().
|
inlinevirtual |
Get the device name, such as /dev/video0
. This is not set until initialize() is called.
Referenced by main().
|
inlinevirtual |
The V4L2 device capabilities are used to identify kernel devices compatible with this specification and to obtain information about driver and hardware capabilities.
This is automatically called during initialization, and normally does not need to be manually called.
|
virtual |
Returns a std::map that describes all the capabilities of this camera device.
Example:
std::runtime_error | if the device is unitialized. |
References capabilities, is_not_initialized(), V4L2_CAP_EXT_PIX_FORMAT, V4L2_CAP_SDR_CAPTURE, V4L2_CAP_SDR_OUTPUT, and V4L2_CAP_TOUCH.
Referenced by describe().
|
virtual |
Determines how many times the CamCap library will retry a capture if an incomplete buffer is returned from V4L.
Some cameras or drivers seem to return partial buffers. CamCap can try multiple times to transfer an image from the camera when this condition is detected.
[in] | retries | Determine the number of times a capture will be restarted. Normally is a relatively small number greater than zero. |
References initialize(), is_not_initialized(), and retry_count.
Referenced by main().
|
inlinevirtual |
Determine if streaming is enabled.
Referenced by describe().
|
virtual |
Query the camera to determine what image sizes are supported.
References initialize(), is_not_initialized(), selected_format, xioctl(), and ZERO.
Referenced by describe().
|
inlinevirtual |
Decides if CamCap accesses the camera directly, or goes through the libv4l interface.
This is also a parameter to initialize() since it can impact from very early on how the camera device is opened.
[in] | toggle | Set to true to use the compatibility layer offered by libv4l . |
[in] | toggle | Set to false if libv4l should be ignored. |
References CC::kMmap.
|
virtual |
Toggle between memory mapped buffers and direct read when capturing images.
References initialize(), is_not_initialized(), CC::kMmap, CC::kRead, release_memory_mapped_buffers(), request_memory_mapped_buffers(), and transfer_method.
Referenced by main().
|
virtual |
Get all of the controls supported by this camera.
This does not include
References initialize(), is_not_initialized(), xioctl(), and ZERO.
Referenced by control_name_to_index(), describe(), describe_controls(), main(), and set_all_controls_to_defaults().
|
virtual |
Iterate through the available controls and set all to their default values.
References get_all_controls(), initialize(), is_not_initialized(), and set_control().
Referenced by main().
|
virtual |
Set a specific control to a certain value.
Examples (not the full list!) of common V4L control indexes defined in videodev2.h which may be supported by cameras are:
V4L2_CID_BRIGHTNESS
V4L2_CID_CONTRAST
V4L2_CID_SATURATION
V4L2_CID_HUE
V4L2_CID_AUTO_WHITE_BALANCE
V4L2_CID_GAMMA
V4L2_CID_GAIN
V4L2_CID_POWER_LINE_FREQUENCY
V4L2_CID_WHITE_BALANCE_TEMPERATURE
V4L2_CID_SHARPNESS
V4L2_CID_BACKLIGHT_COMPENSATION
V4L2_CID_EXPOSURE_AUTO
V4L2_CID_EXPOSURE_ABSOLUTE
V4L2_CID_EXPOSURE_AUTO_PRIORITY
References xioctl(), and ZERO.
Referenced by main(), set_all_controls_to_defaults(), and set_control().
|
virtual |
Get the current value for the specified control.
References xioctl(), and ZERO.
Referenced by describe_controls(), get_control(), and main().
|
virtual |
Map control names to control indexes.
The match is partial-word and case-insensitive. The full list of control names is specific to each camera, though some common names found on most devices include:
brightness
contrast
sturation
hue
gamma
gain
sharpness
std::invalid_argument | if the control name is empty |
std::runtime_error | if a control with the given name cannot be found |
References get_all_controls().
Referenced by get_control(), main(), and set_control().
|
virtual |
Similar to the other set_control(), but takes a name instead of the control index.
The name comparison is case-insitive. For example, "gain" and "GaIn" both match "Gain".
References control_name_to_index(), and set_control().
|
virtual |
Similar to the other get_control(), but takes a name instead of the control index.
The name comparison is partial-word and case-insensitive. For example, "gain" and "GaIn" both match "Gain".
References control_name_to_index(), and get_control().
|
virtual |
Return a text string showing the state of all camera device controls.
For example:
References get_all_controls(), and get_control().
Referenced by describe(), and main().
|
protected |
Name of the device that was opened. Set in the constructor or initialize(). Will normally be /dev/video0
.
Referenced by capture_many_to_jpeg(), describe(), initialize(), xioctl(), and xread().
|
protected |
|
protected |
This is set to true
once the device has been properly initialized.
Referenced by initialize(), and reset().
|
protected |
Needed to track V4L2 messages.
Referenced by get_v4l2_messages(), and set_v4l2_msg_handling().
|
protected |
Needed to track V4L2 messages.
Referenced by get_v4l2_messages(), and set_v4l2_msg_handling().
|
protected |
Device capabilities are obtained in initialize().
Referenced by describe(), get_capability_flags(), initialize(), and reset().
|
protected |
Map of all the inputs supported by a camera. For most (all?) webcams, there is a single input at index #0.
Referenced by initialize(), and reset().
|
protected |
Map of all the formats supported by the camera.
Referenced by initialize(), and reset().
|
protected |
The most recently set image format.
Referenced by capture_using_mmap_buffers(), capture_using_read(), describe(), get_dimensions(), request_memory_mapped_buffers(), reset(), and set_format().
|
protected |
Keep track of every memory mapped buffer.
Referenced by capture_using_mmap_buffers(), describe(), release_memory_mapped_buffers(), request_memory_mapped_buffers(), and stream_start().
|
protected |
Determine if partial captures are kept or rejected.
Referenced by capture_using_mmap_buffers(), capture_using_read(), describe(), reset(), and retry_incomplete_captures().
|
protected |
State of streaming.
Referenced by reset(), stream_start(), and stream_stop().
|
protected |
Should CamCap directly access the camera, or use the libv4l interface layer.
Referenced by describe(), initialize(), reset(), xclose(), xioctl(), xmmap(), xmunmap(), xopen(), and xread().
|
protected |
Selected transfer method.
Referenced by capture(), describe(), reset(), set_transfer_method(), and stream_start().