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