usb_cam.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, Robert Bosch LLC.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Robert Bosch nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *********************************************************************/
36 #ifndef USB_CAM_USB_CAM_H
37 #define USB_CAM_USB_CAM_H
38 
39 #include <asm/types.h> /* for videodev2.h */
40 
41 extern "C"
42 {
43 #include <linux/videodev2.h>
44 #include <libavcodec/avcodec.h>
45 #include <libswscale/swscale.h>
46 #include <libavutil/mem.h>
47 }
48 
49 // legacy reasons
50 #include <libavcodec/version.h>
51 #if LIBAVCODEC_VERSION_MAJOR < 55
52 #define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
53 #endif
54 
55 #include <string>
56 #include <sstream>
57 
58 #include <sensor_msgs/Image.h>
59 
60 namespace usb_cam {
61 
62 class UsbCam {
63  public:
64  typedef enum
65  {
67  } io_method;
68 
69  typedef enum
70  {
72  } pixel_format;
73 
74  UsbCam();
75  ~UsbCam();
76 
77  // start camera
78  void start(const std::string& dev, io_method io, pixel_format pf,
79  int image_width, int image_height, int framerate);
80  // shutdown camera
81  void shutdown(void);
82 
83  // grabs a new image from the camera
84  void grab_image(sensor_msgs::Image* image);
85 
86  // enables/disable auto focus
87  void set_auto_focus(int value);
88 
89  // Set video device parameters
90  void set_v4l_parameter(const std::string& param, int value);
91  void set_v4l_parameter(const std::string& param, const std::string& value);
92 
93  static io_method io_method_from_string(const std::string& str);
94  static pixel_format pixel_format_from_string(const std::string& str);
95 
96  void stop_capturing(void);
97  void start_capturing(void);
98  bool is_capturing();
99 
100  private:
101  typedef struct
102  {
103  int width;
104  int height;
107  char *image;
108  int is_new;
109  } camera_image_t;
110 
111  struct buffer
112  {
113  void * start;
114  size_t length;
115  };
116 
117 
118  int init_mjpeg_decoder(int image_width, int image_height);
119  void mjpeg2rgb(char *MJPEG, int len, char *RGB, int NumPixels);
120  void process_image(const void * src, int len, camera_image_t *dest);
121  int read_frame();
122  void uninit_device(void);
123  void init_read(unsigned int buffer_size);
124  void init_mmap(void);
125  void init_userp(unsigned int buffer_size);
126  void init_device(int image_width, int image_height, int framerate);
127  void close_device(void);
128  void open_device(void);
129  void grab_image();
131 
132 
133  std::string camera_dev_;
134  unsigned int pixelformat_;
136  io_method io_;
137  int fd_;
139  unsigned int n_buffers_;
140  AVFrame *avframe_camera_;
141  AVFrame *avframe_rgb_;
142  AVCodec *avcodec_;
143  AVDictionary *avoptions_;
144  AVCodecContext *avcodec_context_;
147  struct SwsContext *video_sws_;
149 
150 };
151 
152 }
153 
154 #endif
155 
int init_mjpeg_decoder(int image_width, int image_height)
Definition: usb_cam.cpp:366
bool is_capturing_
Definition: usb_cam.h:130
void start_capturing(void)
Definition: usb_cam.cpp:616
unsigned int n_buffers_
Definition: usb_cam.h:139
AVFrame * avframe_camera_
Definition: usb_cam.h:140
void set_auto_focus(int value)
Definition: usb_cam.cpp:1135
void open_device(void)
Definition: usb_cam.cpp:980
void uninit_device(void)
Definition: usb_cam.cpp:679
void shutdown(void)
Definition: usb_cam.cpp:1059
struct SwsContext * video_sws_
Definition: usb_cam.h:147
void stop_capturing(void)
Definition: usb_cam.cpp:592
void process_image(const void *src, int len, camera_image_t *dest)
Definition: usb_cam.cpp:462
AVCodec * avcodec_
Definition: usb_cam.h:142
static pixel_format pixel_format_from_string(const std::string &str)
Definition: usb_cam.cpp:1229
void close_device(void)
Definition: usb_cam.cpp:972
io_method io_
Definition: usb_cam.h:136
bool is_capturing()
Definition: usb_cam.cpp:588
void mjpeg2rgb(char *MJPEG, int len, char *RGB, int NumPixels)
Definition: usb_cam.cpp:409
int avframe_camera_size_
Definition: usb_cam.h:145
unsigned int pixelformat_
Definition: usb_cam.h:134
std::string camera_dev_
Definition: usb_cam.h:133
bool monochrome_
Definition: usb_cam.h:135
void set_v4l_parameter(const std::string &param, int value)
Definition: usb_cam.cpp:1181
static io_method io_method_from_string(const std::string &str)
Definition: usb_cam.cpp:1217
void grab_image()
Definition: usb_cam.cpp:1101
void init_userp(unsigned int buffer_size)
Definition: usb_cam.cpp:784
AVDictionary * avoptions_
Definition: usb_cam.h:143
void init_device(int image_width, int image_height, int framerate)
Definition: usb_cam.cpp:833
buffer * buffers_
Definition: usb_cam.h:138
void init_mmap(void)
Definition: usb_cam.cpp:724
camera_image_t * image_
Definition: usb_cam.h:148
AVCodecContext * avcodec_context_
Definition: usb_cam.h:144
int avframe_rgb_size_
Definition: usb_cam.h:146
void init_read(unsigned int buffer_size)
Definition: usb_cam.cpp:704
AVFrame * avframe_rgb_
Definition: usb_cam.h:141
int read_frame()
Definition: usb_cam.cpp:485
void start(const std::string &dev, io_method io, pixel_format pf, int image_width, int image_height, int framerate)
Definition: usb_cam.cpp:1005


usb_cam
Author(s): Benjamin Pitzer
autogenerated on Fri Jun 7 2019 21:46:25