Classes | Public Member Functions | Private Member Functions | Private Attributes
vidl_v4l2_device Class Reference

A class for handle a video device. More...

#include <vidl_v4l2_device.h>

List of all members.

Classes

struct  buffer

Public Member Functions

 vidl_v4l2_device (const char *file)
 Constructor.
 ~vidl_v4l2_device ()
vcl_string device_file () const
 Name of the associated file device (same as constructor).
vcl_string card_name () const
 Friendly name of the device.
unsigned int n_inputs () const
 Number of inputs in device.
unsigned int current_input () const
 Inputs been used (0 to ninputs-1).
const vidl_v4l2_inputinput (int i) const
 Return input number i (0 ..\ ninputs-1).
bool set_input (unsigned int i)
 Select input i.
bool set_v4l2_format (unsigned int fourcode, int width, int height, double fps=0.0)
 Select a new format.
bool try_formats (int width=640, int height=480)
 Try several formats implemented in vidl.
bool format_is_set () const
 Return if the format is set.
unsigned int get_v4l2_format () const
 Get pixel format of type of compression.
int get_width () const
 Return Image width in pixels.
int get_height () const
 Return Image height in pixels.
double get_frame_rate () const
 Return Current frame rate.
void reset_controls ()
 Reset controls to default.
int n_controls () const
 Get number of controls.
vidl_v4l2_controlget_control (int i) const
 Get control.
vidl_v4l2_controlget_control_id (int id) const
 Get control from driver id.
vidl_v4l2_control_integerget_control_integer_id (int id) const
 Get control from driver id.
vidl_v4l2_control_menuget_control_menu_id (int id) const
 Get control from driver id.
vidl_v4l2_control_booleanget_control_boolean_id (int id) const
 Get control from driver id.
vidl_v4l2_control_buttonget_control_button_id (int id) const
 Get control from driver id.
bool start_capturing ()
 Start capturing.
bool is_capturing () const
 Return if device is capturing.
bool stop_capturing ()
 Stop capturing.
bool read_frame ()
 Read next frame.
bool set_number_of_buffers (unsigned int nb)
 Set numbers of buffers.
unsigned int get_number_of_buffers () const
 Get numbers of buffers.
void * current_buffer () const
 Return the last used buffer.
unsigned int current_buffer_length () const
 Return length of the last used buffer.
void * ibuffer (unsigned int i) const
 Return buffer number i.
unsigned int sequence () const
bool time_available () const
 Return if driver has time associated to the captured frame.
v4l2_timecode time () const
 Time from last frame.
 operator safe_bool () const
 Cast to bool is true if video device is working ok.
bool operator! () const
 Return false if video device is not working ok.
vcl_string get_error () const
 Return last error if device is in a bad state. Empty if ok.
void ref ()
 Increment reference count.
void unref ()
 Decrement reference count.
int ref_count () const
 Number of objects referring to this data.

Private Member Functions

void update_controls ()
void reset ()
bool open ()
bool initialize_device ()
bool init_mmap (int reqbuf)
bool uninit_mmap ()
bool close ()
bool is_open () const
bool is_prepared_for_capturing () const
bool good () const
 vidl_v4l2_device (const vidl_v4l2_device &)
vidl_v4l2_deviceoperator= (const vidl_v4l2_device &)

Private Attributes

 VCL_SAFE_BOOL_DEFINE
int fd
unsigned int pre_nbuffers
struct bufferbuffers
unsigned int n_buffers
int last_buffer
struct v4l2_format fmt
double frame_rate
vcl_string dev_name_
vcl_string card_name_
vcl_string last_error
bool capturing
vcl_vector< vidl_v4l2_inputinputs_
vcl_vector< vidl_v4l2_control * > controls_
int ref_count_

Detailed Description

A class for handle a video device.

This class is designed to be vidl independent, although it is very easy to create a vidl istream from it. It has not been fully tested, although it has been proven to capture images from multiple webcams with a single buffer (default) and has worked properly. Generally, the steps to be made for a capture are as follows:

  1. Select input
  2. Select format
  3. Select number of buffers.
  4. Start capturing
  5. Stop capturing When a device is opened, an input and format are selected by default (see vidl_v4l2_device::try_formats function). Then if you start capturing, you will probably get 640x480 frames, from input 0, using just one buffer with an unknown format (the first one valid for the driver).
    See also:
    vidl_v4l2_devices

Definition at line 57 of file vidl_v4l2_device.h.


Constructor & Destructor Documentation

vidl_v4l2_device::vidl_v4l2_device ( const vidl_v4l2_device ) [private]
vidl_v4l2_device::vidl_v4l2_device ( const char *  file)

Constructor.

After construction, device is closed (device is not busy). You can use device as a boolean expression to test if device is ok.

Parameters:
filedevice name (for example, "/dev/video")
filedevice name (for example, "/dev/video")

Definition at line 103 of file vidl_v4l2_device.cxx.

vidl_v4l2_device::~vidl_v4l2_device ( )

Definition at line 145 of file vidl_v4l2_device.cxx.


Member Function Documentation

vcl_string vidl_v4l2_device::card_name ( ) const [inline]

Friendly name of the device.

Definition at line 114 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::close ( void  ) [private]

Definition at line 579 of file vidl_v4l2_device.cxx.

void* vidl_v4l2_device::current_buffer ( ) const [inline]

Return the last used buffer.

Precondition:
device is capturing

Definition at line 257 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::current_buffer_length ( ) const [inline]

Return length of the last used buffer.

Precondition:
device is capturing

Definition at line 263 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::current_input ( ) const

Inputs been used (0 to ninputs-1).

if equal to ninputs, indicates unknown

See also:
ninputs

Definition at line 599 of file vidl_v4l2_device.cxx.

vcl_string vidl_v4l2_device::device_file ( ) const [inline]

Name of the associated file device (same as constructor).

Definition at line 112 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::format_is_set ( ) const [inline]

Return if the format is set.

Normally, a format is automatically selected or user call set_v4l2_format. User can use this function to know if a format is selected before calling start_capturing.

See also:
set_v4l2_format

Definition at line 149 of file vidl_v4l2_device.h.

vidl_v4l2_control* vidl_v4l2_device::get_control ( int  i) const [inline]

Get control.

The user must downcast the pointer -depending on type- to use all functionality.

Parameters:
iindicates the control to be extracted, from 0 to n_controls-1
Returns:
pointer to control
See also:
n_controls

Definition at line 185 of file vidl_v4l2_device.h.

vidl_v4l2_control_boolean* vidl_v4l2_device::get_control_boolean_id ( int  id) const [inline]

Get control from driver id.

Parameters:
idis control ID from v4l2 specification. For example, V4L2_CID_BRIGHTNESS
Returns:
pointer to control. Null if doesn't exist or is not of type V4L2_CTRL_TYPE_BOOLEAN

Definition at line 216 of file vidl_v4l2_device.h.

vidl_v4l2_control_button* vidl_v4l2_device::get_control_button_id ( int  id) const [inline]

Get control from driver id.

Parameters:
idis control ID from v4l2 specification. For example, V4L2_CID_BRIGHTNESS
Returns:
pointer to control. Null if doesn't exist or is not of type V4L2_CTRL_TYPE_BUTTON

Definition at line 226 of file vidl_v4l2_device.h.

vidl_v4l2_control* vidl_v4l2_device::get_control_id ( int  id) const [inline]

Get control from driver id.

The user must downcast the pointer -depending on type- to use all functionality.

Parameters:
idis control ID from v4l2 specification. For example, V4L2_CID_BRIGHTNESS
Returns:
pointer to control or 0 if does not exist

Definition at line 190 of file vidl_v4l2_device.h.

vidl_v4l2_control_integer* vidl_v4l2_device::get_control_integer_id ( int  id) const [inline]

Get control from driver id.

Parameters:
idis control ID from v4l2 specification. For example, V4L2_CID_BRIGHTNESS
Returns:
pointer to control. Null if doesn't exist or is not of type V4L2_CTRL_TYPE_INTEGER

Definition at line 196 of file vidl_v4l2_device.h.

vidl_v4l2_control_menu* vidl_v4l2_device::get_control_menu_id ( int  id) const [inline]

Get control from driver id.

Parameters:
idis control ID from v4l2 specification. For example, V4L2_CID_BRIGHTNESS
Returns:
pointer to control. Null if doesn't exist or is not of type V4L2_CTRL_TYPE_MENU

Definition at line 206 of file vidl_v4l2_device.h.

vcl_string vidl_v4l2_device::get_error ( ) const [inline]

Return last error if device is in a bad state. Empty if ok.

Definition at line 305 of file vidl_v4l2_device.h.

double vidl_v4l2_device::get_frame_rate ( ) const [inline]

Return Current frame rate.

See also:
set_v4l2_format

Definition at line 170 of file vidl_v4l2_device.h.

int vidl_v4l2_device::get_height ( ) const [inline]

Return Image height in pixels.

Note:
You can use this function to know the height selected by driver after calling set_v4l2_format
See also:
set_v4l2_format

Definition at line 166 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::get_number_of_buffers ( ) const [inline]

Get numbers of buffers.

Definition at line 253 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::get_v4l2_format ( ) const [inline]

Get pixel format of type of compression.

Returns:
the four character code which is being used by driver(see v4l2 specification and vidl_pixel_format.h) or 0 if not set
See also:
set_v4l2_format

Definition at line 154 of file vidl_v4l2_device.h.

int vidl_v4l2_device::get_width ( ) const [inline]

Return Image width in pixels.

Note:
You can use this function to know the width selected by driver after calling set_v4l2_format
See also:
set_v4l2_format

Definition at line 162 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::good ( ) const [inline, private]

Definition at line 95 of file vidl_v4l2_device.h.

void* vidl_v4l2_device::ibuffer ( unsigned int  i) const [inline]

Return buffer number i.

Precondition:
device is capturing
See also:
get_number_of_buffers

Definition at line 270 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::init_mmap ( int  reqbuf) [private]

Definition at line 365 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::initialize_device ( ) [private]

Definition at line 226 of file vidl_v4l2_device.cxx.

const vidl_v4l2_input& vidl_v4l2_device::input ( int  i) const [inline]

Return input number i (0 ..\ ninputs-1).

Definition at line 122 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::is_capturing ( ) const [inline]

Return if device is capturing.

Definition at line 239 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::is_open ( ) const [inline, private]

Definition at line 93 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::is_prepared_for_capturing ( ) const [inline, private]

Definition at line 94 of file vidl_v4l2_device.h.

int vidl_v4l2_device::n_controls ( ) const [inline]

Get number of controls.

Returns:
the number of detected controls (control not disabled and not inactive).

Definition at line 179 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::n_inputs ( ) const [inline]

Number of inputs in device.

Definition at line 116 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::open ( ) [private]

Definition at line 194 of file vidl_v4l2_device.cxx.

vidl_v4l2_device::operator safe_bool ( ) const [inline]

Cast to bool is true if video device is working ok.

Definition at line 297 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::operator! ( ) const [inline]

Return false if video device is not working ok.

Definition at line 301 of file vidl_v4l2_device.h.

vidl_v4l2_device& vidl_v4l2_device::operator= ( const vidl_v4l2_device ) [private]
bool vidl_v4l2_device::read_frame ( void  )

Read next frame.

Returns:
if successful

Definition at line 490 of file vidl_v4l2_device.cxx.

void vidl_v4l2_device::ref ( ) [inline]

Increment reference count.

Definition at line 312 of file vidl_v4l2_device.h.

int vidl_v4l2_device::ref_count ( ) const [inline]

Number of objects referring to this data.

Definition at line 325 of file vidl_v4l2_device.h.

void vidl_v4l2_device::reset ( ) [private]

Definition at line 160 of file vidl_v4l2_device.cxx.

void vidl_v4l2_device::reset_controls ( )

Reset controls to default.

Definition at line 93 of file vidl_v4l2_device.cxx.

unsigned int vidl_v4l2_device::sequence ( ) const [inline]

Definition at line 275 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::set_input ( unsigned int  i)

Select input i.

Returns:
if successful

Definition at line 614 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::set_number_of_buffers ( unsigned int  nb)

Set numbers of buffers.

Returns:
if successful

Definition at line 442 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::set_v4l2_format ( unsigned int  fourcode,
int  width,
int  height,
double  fps = 0.0 
)

Select a new format.

Device try automatically different formats (

See also:
try_formats()). Then this function could be ignored if you don't mind the format. Usually, user wants a concrete pixel format or, simply, change width and height.
Parameters:
fourcodeA four character code defined in v4l2 (see v4l2 specification and vidl_pixel_format.h) indicating pixel encoding
widthcan be changed by drivers to the closest possible value
heightcan be changed by drivers to the closest possible value
fpsframe rate to try (not garanteed).
Returns:
if successful
See also:
format_is_set

Definition at line 294 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::start_capturing ( )

Start capturing.

Returns:
if successful

Definition at line 455 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::stop_capturing ( )

Stop capturing.

Returns:
if successful

Definition at line 548 of file vidl_v4l2_device.cxx.

v4l2_timecode vidl_v4l2_device::time ( ) const [inline]

Time from last frame.

See also:
time_available

Definition at line 290 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::time_available ( ) const [inline]

Return if driver has time associated to the captured frame.

Precondition:
device is capturing

Definition at line 284 of file vidl_v4l2_device.h.

bool vidl_v4l2_device::try_formats ( int  width = 640,
int  height = 480 
)

Try several formats implemented in vidl.

Parameters:
widthcan be changed by drivers to the closest possible value
heightcan be changed by drivers to the closest possible value
fpsframe rate to try (not garanteed).
Returns:
if successful

Definition at line 263 of file vidl_v4l2_device.cxx.

bool vidl_v4l2_device::uninit_mmap ( ) [private]

Definition at line 566 of file vidl_v4l2_device.cxx.

void vidl_v4l2_device::unref ( ) [inline]

Decrement reference count.

Definition at line 315 of file vidl_v4l2_device.h.

void vidl_v4l2_device::update_controls ( ) [private]

Definition at line 56 of file vidl_v4l2_device.cxx.


Member Data Documentation

struct buffer* vidl_v4l2_device::buffers [private]

Definition at line 68 of file vidl_v4l2_device.h.

Definition at line 77 of file vidl_v4l2_device.h.

vcl_string vidl_v4l2_device::card_name_ [private]

Definition at line 75 of file vidl_v4l2_device.h.

Definition at line 79 of file vidl_v4l2_device.h.

vcl_string vidl_v4l2_device::dev_name_ [private]

Definition at line 74 of file vidl_v4l2_device.h.

int vidl_v4l2_device::fd [private]

Definition at line 60 of file vidl_v4l2_device.h.

struct v4l2_format vidl_v4l2_device::fmt [private]

Definition at line 71 of file vidl_v4l2_device.h.

double vidl_v4l2_device::frame_rate [private]

Definition at line 72 of file vidl_v4l2_device.h.

Definition at line 78 of file vidl_v4l2_device.h.

Definition at line 70 of file vidl_v4l2_device.h.

vcl_string vidl_v4l2_device::last_error [mutable, private]

Definition at line 76 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::n_buffers [private]

Definition at line 69 of file vidl_v4l2_device.h.

unsigned int vidl_v4l2_device::pre_nbuffers [private]

Definition at line 67 of file vidl_v4l2_device.h.

Definition at line 328 of file vidl_v4l2_device.h.

Definition at line 59 of file vidl_v4l2_device.h.


The documentation for this class was generated from the following files: