Replaced exit statements for exceptions

Former-commit-id: 1eb180620fb59613eaa02da84026ffdcb28803a4
This commit is contained in:
johan 2014-01-26 12:35:31 +01:00
parent 4f7997bbfe
commit df9b33f560
2 changed files with 351 additions and 330 deletions

View File

@ -1,4 +1,6 @@
#include <iostream> #include <iostream>
#include <sstream>
#include <stdexcept>
#include <cstdio> #include <cstdio>
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
@ -93,13 +95,12 @@ void V4L2Grabber::capture(int frameCount)
{ {
if (EINTR == errno) if (EINTR == errno)
continue; continue;
errno_exit("select"); throw_errno_exception("select");
} }
if (0 == r) if (0 == r)
{ {
fprintf(stderr, "select timeout\n"); throw_exception("select timeout");
exit(EXIT_FAILURE);
} }
if (read_frame()) if (read_frame())
@ -123,29 +124,32 @@ void V4L2Grabber::open_device()
if (-1 == stat(_deviceName.c_str(), &st)) if (-1 == stat(_deviceName.c_str(), &st))
{ {
fprintf(stderr, "Cannot identify '%s': %d, %s\n", _deviceName.c_str(), errno, strerror(errno)); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "Cannot identify '" << _deviceName << "'";
throw_errno_exception(oss.str());
} }
if (!S_ISCHR(st.st_mode)) if (!S_ISCHR(st.st_mode))
{ {
fprintf(stderr, "%s is no device\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' is no device";
throw_exception(oss.str());
} }
_fileDescriptor = open(_deviceName.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0); _fileDescriptor = open(_deviceName.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0);
if (-1 == _fileDescriptor) if (-1 == _fileDescriptor)
{ {
fprintf(stderr, "Cannot open '%s': %d, %s\n", _deviceName.c_str(), errno, strerror(errno)); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "Cannot open '" << _deviceName << "'";
throw_errno_exception(oss.str());
} }
} }
void V4L2Grabber::close_device() void V4L2Grabber::close_device()
{ {
if (-1 == close(_fileDescriptor)) if (-1 == close(_fileDescriptor))
errno_exit("close"); throw_errno_exception("close");
_fileDescriptor = -1; _fileDescriptor = -1;
} }
@ -158,8 +162,7 @@ void V4L2Grabber::init_read(unsigned int buffer_size)
_buffers[0].start = malloc(buffer_size); _buffers[0].start = malloc(buffer_size);
if (!_buffers[0].start) { if (!_buffers[0].start) {
fprintf(stderr, "Out of memory\n"); throw_exception("Out of memory");
exit(EXIT_FAILURE);
} }
} }
@ -175,16 +178,18 @@ void V4L2Grabber::init_mmap()
if (-1 == xioctl(VIDIOC_REQBUFS, &req)) { if (-1 == xioctl(VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) { if (EINVAL == errno) {
fprintf(stderr, "%s does not support memory mapping\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' does not support memory mapping";
throw_exception(oss.str());
} else { } else {
errno_exit("VIDIOC_REQBUFS"); throw_errno_exception("VIDIOC_REQBUFS");
} }
} }
if (req.count < 2) { if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "Insufficient buffer memory on " << _deviceName;
throw_exception(oss.str());
} }
_buffers.resize(req.count); _buffers.resize(req.count);
@ -199,7 +204,7 @@ void V4L2Grabber::init_mmap()
buf.index = n_buffers; buf.index = n_buffers;
if (-1 == xioctl(VIDIOC_QUERYBUF, &buf)) if (-1 == xioctl(VIDIOC_QUERYBUF, &buf))
errno_exit("VIDIOC_QUERYBUF"); throw_errno_exception("VIDIOC_QUERYBUF");
_buffers[n_buffers].length = buf.length; _buffers[n_buffers].length = buf.length;
_buffers[n_buffers].start = _buffers[n_buffers].start =
@ -210,7 +215,7 @@ void V4L2Grabber::init_mmap()
_fileDescriptor, buf.m.offset); _fileDescriptor, buf.m.offset);
if (MAP_FAILED == _buffers[n_buffers].start) if (MAP_FAILED == _buffers[n_buffers].start)
errno_exit("mmap"); throw_errno_exception("mmap");
} }
} }
@ -227,10 +232,11 @@ void V4L2Grabber::init_userp(unsigned int buffer_size)
if (-1 == xioctl(VIDIOC_REQBUFS, &req)) { if (-1 == xioctl(VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) if (EINVAL == errno)
{ {
fprintf(stderr, "%s does not support user pointer i/o\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' does not support user pointer";
throw_exception(oss.str());
} else { } else {
errno_exit("VIDIOC_REQBUFS"); throw_errno_exception("VIDIOC_REQBUFS");
} }
} }
@ -241,8 +247,7 @@ void V4L2Grabber::init_userp(unsigned int buffer_size)
_buffers[n_buffers].start = malloc(buffer_size); _buffers[n_buffers].start = malloc(buffer_size);
if (!_buffers[n_buffers].start) { if (!_buffers[n_buffers].start) {
fprintf(stderr, "Out of memory\n"); throw_exception("Out of memory");
exit(EXIT_FAILURE);
} }
} }
} }
@ -253,25 +258,28 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
if (-1 == xioctl(VIDIOC_QUERYCAP, &cap)) if (-1 == xioctl(VIDIOC_QUERYCAP, &cap))
{ {
if (EINVAL == errno) { if (EINVAL == errno) {
fprintf(stderr, "%s is no V4L2 device\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' is no V4L2 device";
throw_exception(oss.str());
} else { } else {
errno_exit("VIDIOC_QUERYCAP"); throw_errno_exception("VIDIOC_QUERYCAP");
} }
} }
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
{ {
fprintf(stderr, "%s is no video capture device\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' is no video capture device";
throw_exception(oss.str());
} }
switch (_ioMethod) { switch (_ioMethod) {
case IO_METHOD_READ: case IO_METHOD_READ:
if (!(cap.capabilities & V4L2_CAP_READWRITE)) if (!(cap.capabilities & V4L2_CAP_READWRITE))
{ {
fprintf(stderr, "%s does not support read i/o\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' does not support read i/o";
throw_exception(oss.str());
} }
break; break;
@ -279,8 +287,9 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
case IO_METHOD_USERPTR: case IO_METHOD_USERPTR:
if (!(cap.capabilities & V4L2_CAP_STREAMING)) if (!(cap.capabilities & V4L2_CAP_STREAMING))
{ {
fprintf(stderr, "%s does not support streaming i/o\n", _deviceName.c_str()); std::ostringstream oss;
exit(EXIT_FAILURE); oss << "'" << _deviceName << "' does not support streaming i/o";
throw_exception(oss.str());
} }
break; break;
} }
@ -317,7 +326,7 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
{ {
if (-1 == xioctl(VIDIOC_S_INPUT, &input)) if (-1 == xioctl(VIDIOC_S_INPUT, &input))
{ {
errno_exit("VIDIOC_S_INPUT"); throw_errno_exception("VIDIOC_S_INPUT");
} }
} }
@ -329,7 +338,7 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
v4l2_std_id std_id = V4L2_STD_PAL; v4l2_std_id std_id = V4L2_STD_PAL;
if (-1 == xioctl(VIDIOC_S_STD, &std_id)) if (-1 == xioctl(VIDIOC_S_STD, &std_id))
{ {
errno_exit("VIDIOC_S_STD"); throw_errno_exception("VIDIOC_S_STD");
} }
} }
break; break;
@ -338,7 +347,7 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
v4l2_std_id std_id = V4L2_STD_NTSC; v4l2_std_id std_id = V4L2_STD_NTSC;
if (-1 == xioctl(VIDIOC_S_STD, &std_id)) if (-1 == xioctl(VIDIOC_S_STD, &std_id))
{ {
errno_exit("VIDIOC_S_STD"); throw_errno_exception("VIDIOC_S_STD");
} }
} }
break; break;
@ -355,13 +364,13 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(VIDIOC_G_FMT, &fmt)) if (-1 == xioctl(VIDIOC_G_FMT, &fmt))
{ {
errno_exit("VIDIOC_G_FMT"); throw_errno_exception("VIDIOC_G_FMT");
} }
// check pixel format // check pixel format
if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY) if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
{ {
exit(EXIT_FAILURE); throw_exception("Only pixel format UYVY is supported");
} }
if (_width > 0 || _height > 0) if (_width > 0 || _height > 0)
@ -379,20 +388,22 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
// set the settings // set the settings
if (-1 == xioctl(VIDIOC_S_FMT, &fmt)) if (-1 == xioctl(VIDIOC_S_FMT, &fmt))
{ {
errno_exit("VIDIOC_S_FMT"); throw_errno_exception("VIDIOC_S_FMT");
} }
// get the format settings again // get the format settings again
// (the size may not have been accepted without an error) // (the size may not have been accepted without an error)
if (-1 == xioctl(VIDIOC_G_FMT, &fmt)) if (-1 == xioctl(VIDIOC_G_FMT, &fmt))
{ {
errno_exit("VIDIOC_G_FMT"); throw_errno_exception("VIDIOC_G_FMT");
} }
} }
// store width & height // store width & height
_width = fmt.fmt.pix.width; _width = fmt.fmt.pix.width;
_height = fmt.fmt.pix.height; _height = fmt.fmt.pix.height;
// print the eventually used width and height
std::cout << "V4L2 width=" << _width << " height=" << _height << std::endl; std::cout << "V4L2 width=" << _width << " height=" << _height << std::endl;
switch (_ioMethod) { switch (_ioMethod) {
@ -420,7 +431,7 @@ void V4L2Grabber::uninit_device()
case IO_METHOD_MMAP: case IO_METHOD_MMAP:
for (size_t i = 0; i < _buffers.size(); ++i) for (size_t i = 0; i < _buffers.size(); ++i)
if (-1 == munmap(_buffers[i].start, _buffers[i].length)) if (-1 == munmap(_buffers[i].start, _buffers[i].length))
errno_exit("munmap"); throw_errno_exception("munmap");
break; break;
case IO_METHOD_USERPTR: case IO_METHOD_USERPTR:
@ -450,11 +461,11 @@ void V4L2Grabber::start_capturing()
buf.index = i; buf.index = i;
if (-1 == xioctl(VIDIOC_QBUF, &buf)) if (-1 == xioctl(VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF"); throw_errno_exception("VIDIOC_QBUF");
} }
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(VIDIOC_STREAMON, &type)) if (-1 == xioctl(VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON"); throw_errno_exception("VIDIOC_STREAMON");
break; break;
} }
case IO_METHOD_USERPTR: case IO_METHOD_USERPTR:
@ -470,11 +481,11 @@ void V4L2Grabber::start_capturing()
buf.length = _buffers[i].length; buf.length = _buffers[i].length;
if (-1 == xioctl(VIDIOC_QBUF, &buf)) if (-1 == xioctl(VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF"); throw_errno_exception("VIDIOC_QBUF");
} }
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(VIDIOC_STREAMON, &type)) if (-1 == xioctl(VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON"); throw_errno_exception("VIDIOC_STREAMON");
break; break;
} }
} }
@ -493,7 +504,7 @@ void V4L2Grabber::stop_capturing()
case IO_METHOD_USERPTR: case IO_METHOD_USERPTR:
type = V4L2_BUF_TYPE_VIDEO_CAPTURE; type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(VIDIOC_STREAMOFF, &type)) if (-1 == xioctl(VIDIOC_STREAMOFF, &type))
errno_exit("VIDIOC_STREAMOFF"); throw_errno_exception("VIDIOC_STREAMOFF");
break; break;
} }
} }
@ -520,7 +531,7 @@ int V4L2Grabber::read_frame()
/* fall through */ /* fall through */
default: default:
errno_exit("read"); throw_errno_exception("read");
} }
} }
@ -546,7 +557,7 @@ int V4L2Grabber::read_frame()
/* fall through */ /* fall through */
default: default:
errno_exit("VIDIOC_DQBUF"); throw_errno_exception("VIDIOC_DQBUF");
} }
} }
@ -556,7 +567,7 @@ int V4L2Grabber::read_frame()
if (-1 == xioctl(VIDIOC_QBUF, &buf)) if (-1 == xioctl(VIDIOC_QBUF, &buf))
{ {
errno_exit("VIDIOC_QBUF"); throw_errno_exception("VIDIOC_QBUF");
} }
break; break;
@ -580,7 +591,7 @@ int V4L2Grabber::read_frame()
/* fall through */ /* fall through */
default: default:
errno_exit("VIDIOC_DQBUF"); throw_errno_exception("VIDIOC_DQBUF");
} }
} }
@ -596,7 +607,7 @@ int V4L2Grabber::read_frame()
if (-1 == xioctl(VIDIOC_QBUF, &buf)) if (-1 == xioctl(VIDIOC_QBUF, &buf))
{ {
errno_exit("VIDIOC_QBUF"); throw_errno_exception("VIDIOC_QBUF");
} }
break; break;
} }
@ -665,8 +676,16 @@ int V4L2Grabber::xioctl(int request, void *arg)
return r; return r;
} }
void V4L2Grabber::errno_exit(const char *s) void V4L2Grabber::throw_exception(const std::string & error)
{ {
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno)); std::ostringstream oss;
exit(EXIT_FAILURE); oss << error << " error";
throw std::runtime_error(oss.str());
}
void V4L2Grabber::throw_errno_exception(const std::string & error)
{
std::ostringstream oss;
oss << error << " error " << errno << ", " << strerror(errno);
throw std::runtime_error(oss.str());
} }

View File

@ -59,7 +59,9 @@ private:
int xioctl(int request, void *arg); int xioctl(int request, void *arg);
void errno_exit(const char *s); void throw_exception(const std::string &error);
void throw_errno_exception(const std::string &error);
private: private:
enum io_method { enum io_method {