summaryrefslogtreecommitdiffstats
path: root/media
diff options
context:
space:
mode:
authormcasas@chromium.org <mcasas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-17 14:41:35 +0000
committermcasas@chromium.org <mcasas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-17 14:41:35 +0000
commit79414e1e180c4a68d653b468d804dbf260a3fd8c (patch)
tree61b06376d5dd8545cd3853648ee0c31daaa01936 /media
parentbfd8987a8d3a0f10b4371182415564f5bd9623f4 (diff)
downloadchromium_src-79414e1e180c4a68d653b468d804dbf260a3fd8c.zip
chromium_src-79414e1e180c4a68d653b468d804dbf260a3fd8c.tar.gz
chromium_src-79414e1e180c4a68d653b468d804dbf260a3fd8c.tar.bz2
Linux video capture: use HANDLE_EINTR in all direct use cases of
int's as file descriptors, and use file_utils::ScopedFD. FD_SET, FD_ISSET and mmap() are not wrapped. Also using file_util::ScopedFD to close files manipulated as int. In particular, the idiom auto-close-my-file is used [1] as opposed to the create-a-scopedFD-and-use-it-instead-of-the-int-file-descriptor as in [2]. (Not applicable to this CL since close() calls are gone but JIC IGNORE_EINTR would have not been used since return values were ignored.) [1] https://code.google.com/p/chromium/codesearch#chromium/src/base/debug/proc_maps_linux.cc&sq=package:chromium&rcl=1386553303&l=53&type=cs [2] https://code.google.com/p/chromium/codesearch#chromium/src/base/memory/shared_memory_posix.cc&sq=package:chromium&rcl=1386553303&l=136&type=cs BUG=324428 Review URL: https://codereview.chromium.org/109263005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241281 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media')
-rw-r--r--media/video/capture/linux/video_capture_device_linux.cc80
-rw-r--r--media/video/capture/linux/video_capture_device_linux.h2
2 files changed, 41 insertions, 41 deletions
diff --git a/media/video/capture/linux/video_capture_device_linux.cc b/media/video/capture/linux/video_capture_device_linux.cc
index b2717b8..9e01ff0 100644
--- a/media/video/capture/linux/video_capture_device_linux.cc
+++ b/media/video/capture/linux/video_capture_device_linux.cc
@@ -18,8 +18,8 @@
#include <string>
#include "base/bind.h"
-#include "base/file_util.h"
#include "base/files/file_enumerator.h"
+#include "base/posix/eintr_wrapper.h"
#include "base/strings/stringprintf.h"
namespace media {
@@ -99,7 +99,7 @@ static bool HasUsableFormats(int fd) {
memset(&fmtdesc, 0, sizeof(v4l2_fmtdesc));
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
+ while (HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) == 0) {
if (std::find(usable_fourccs.begin(), usable_fourccs.end(),
fmtdesc.pixelformat) != usable_fourccs.end())
return true;
@@ -110,11 +110,7 @@ static bool HasUsableFormats(int fd) {
}
void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
- int fd = -1;
-
- // Empty the name list.
- device_names->clear();
-
+ DCHECK(device_names->empty());
base::FilePath path("/dev/");
base::FileEnumerator enumerator(
path, false, base::FileEnumerator::FILES, "video*");
@@ -123,13 +119,15 @@ void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
base::FileEnumerator::FileInfo info = enumerator.GetInfo();
std::string unique_id = path.value() + info.GetName().value();
- if ((fd = open(unique_id.c_str() , O_RDONLY)) < 0) {
+ int fd;
+ if ((fd = HANDLE_EINTR(open(unique_id.c_str() , O_RDONLY))) < 0) {
// Failed to open this device.
continue;
}
+ file_util::ScopedFD fd_closer(&fd);
// Test if this is a V4L2 capture device.
v4l2_capability cap;
- if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) &&
+ if ((HANDLE_EINTR(ioctl(fd, VIDIOC_QUERYCAP, &cap)) == 0) &&
(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
// This is a V4L2 video capture device
@@ -140,7 +138,6 @@ void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
DVLOG(1) << "No usable formats reported by " << info.GetName().value();
}
}
- close(fd);
}
}
@@ -150,15 +147,18 @@ void VideoCaptureDevice::GetDeviceSupportedFormats(
if (device.id().empty())
return;
int fd;
- if ((fd = open(device.id().c_str(), O_RDONLY)) < 0)
+ if ((fd = HANDLE_EINTR(open(device.id().c_str(), O_RDONLY))) < 0) {
+ // Failed to open this device.
return;
-
+ }
+ file_util::ScopedFD fd_closer(&fd);
supported_formats->clear();
+
// Retrieve the caps one by one, first get pixel format, then sizes, then
// frame rates. See http://linuxtv.org/downloads/v4l-dvb-apis for reference.
v4l2_fmtdesc pixel_format = {};
pixel_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- while (ioctl(fd, VIDIOC_ENUM_FMT, &pixel_format) == 0) {
+ while (HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &pixel_format)) == 0) {
VideoCaptureFormat supported_format;
supported_format.pixel_format =
V4l2ColorToVideoCaptureColorFormat((int32)pixel_format.pixelformat);
@@ -168,7 +168,7 @@ void VideoCaptureDevice::GetDeviceSupportedFormats(
v4l2_frmsizeenum frame_size = {};
frame_size.pixel_format = pixel_format.pixelformat;
- while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frame_size) == 0) {
+ while (HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frame_size)) == 0) {
if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
supported_format.frame_size.SetSize(
frame_size.discrete.width, frame_size.discrete.height);
@@ -183,7 +183,8 @@ void VideoCaptureDevice::GetDeviceSupportedFormats(
frame_interval.pixel_format = pixel_format.pixelformat;
frame_interval.width = frame_size.discrete.width;
frame_interval.height = frame_size.discrete.height;
- while (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frame_interval) == 0) {
+ while (HANDLE_EINTR(
+ ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frame_interval)) == 0) {
if (frame_interval.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
if (frame_interval.discrete.numerator != 0) {
supported_format.frame_rate =
@@ -208,8 +209,6 @@ void VideoCaptureDevice::GetDeviceSupportedFormats(
}
++pixel_format.index;
}
-
- close(fd);
return;
}
@@ -255,7 +254,7 @@ VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) {
// Test opening the device driver. This is to make sure it is available.
// We will reopen it again in our worker thread when someone
// allocates the camera.
- int fd = open(device_name.id().c_str(), O_RDONLY);
+ int fd = HANDLE_EINTR(open(device_name.id().c_str(), O_RDONLY));
if (fd < 0) {
DVLOG(1) << "Cannot open device";
delete self;
@@ -280,11 +279,7 @@ VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() {
// Check if the thread is running.
// This means that the device have not been DeAllocated properly.
DCHECK(!v4l2_thread_.IsRunning());
-
v4l2_thread_.Stop();
- if (device_fd_ >= 0) {
- close(device_fd_);
- }
}
void VideoCaptureDeviceLinux::AllocateAndStart(
@@ -328,18 +323,20 @@ void VideoCaptureDeviceLinux::OnAllocateAndStart(int width,
client_ = client.Pass();
// Need to open camera with O_RDWR after Linux kernel 3.3.
- if ((device_fd_ = open(device_name_.id().c_str(), O_RDWR)) < 0) {
+ device_fd_ = HANDLE_EINTR(open(device_name_.id().c_str(), O_RDWR));
+ if (device_fd_ < 0) {
SetErrorState("Failed to open V4L2 device driver.");
return;
}
+ device_fd_closer_.reset(&device_fd_);
// Test if this is a V4L2 capture device.
v4l2_capability cap;
- if (!((ioctl(device_fd_, VIDIOC_QUERYCAP, &cap) == 0) &&
- (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
- !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT))) {
+ if (!((HANDLE_EINTR(ioctl(device_fd_, VIDIOC_QUERYCAP, &cap)) == 0) &&
+ (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
+ !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT))) {
// This is not a V4L2 video capture device.
- close(device_fd_);
+ device_fd_closer_.reset();
device_fd_ = -1;
SetErrorState("This is not a V4L2 video capture device");
return;
@@ -357,7 +354,7 @@ void VideoCaptureDeviceLinux::OnAllocateAndStart(int width,
// Enumerate image formats.
std::list<int>::iterator best = v4l2_formats.end();
- while (ioctl(device_fd_, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
+ while (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_ENUM_FMT, &fmtdesc)) == 0) {
best = std::find(v4l2_formats.begin(), best, fmtdesc.pixelformat);
fmtdesc.index++;
}
@@ -376,7 +373,7 @@ void VideoCaptureDeviceLinux::OnAllocateAndStart(int width,
video_fmt.fmt.pix.height = height;
video_fmt.fmt.pix.pixelformat = *best;
- if (ioctl(device_fd_, VIDIOC_S_FMT, &video_fmt) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_S_FMT, &video_fmt)) < 0) {
SetErrorState("Failed to set camera format");
return;
}
@@ -386,14 +383,14 @@ void VideoCaptureDeviceLinux::OnAllocateAndStart(int width,
memset(&streamparm, 0, sizeof(v4l2_streamparm));
streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
// The following line checks that the driver knows about framerate get/set.
- if (ioctl(device_fd_, VIDIOC_G_PARM, &streamparm) >= 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_G_PARM, &streamparm)) >= 0) {
// Now check if the device is able to accept a capture framerate set.
if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
streamparm.parm.capture.timeperframe.numerator = 1;
streamparm.parm.capture.timeperframe.denominator =
(frame_rate) ? frame_rate : kTypicalFramerate;
- if (ioctl(device_fd_, VIDIOC_S_PARM, &streamparm) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_S_PARM, &streamparm)) < 0) {
SetErrorState("Failed to set camera framerate");
return;
}
@@ -421,7 +418,7 @@ void VideoCaptureDeviceLinux::OnAllocateAndStart(int width,
// Start UVC camera.
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (ioctl(device_fd_, VIDIOC_STREAMON, &type) == -1) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_STREAMON, &type)) == -1) {
SetErrorState("VIDIOC_STREAMON failed");
return;
}
@@ -438,7 +435,7 @@ void VideoCaptureDeviceLinux::OnStopAndDeAllocate() {
DCHECK_EQ(v4l2_thread_.message_loop(), base::MessageLoop::current());
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (ioctl(device_fd_, VIDIOC_STREAMOFF, &type) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_STREAMOFF, &type)) < 0) {
SetErrorState("VIDIOC_STREAMOFF failed");
return;
}
@@ -449,7 +446,7 @@ void VideoCaptureDeviceLinux::OnStopAndDeAllocate() {
// We need to close and open the device if we want to change the settings
// Otherwise VIDIOC_S_FMT will return error
// Sad but true.
- close(device_fd_);
+ device_fd_closer_.reset();
device_fd_ = -1;
state_ = kIdle;
client_.reset();
@@ -472,7 +469,8 @@ void VideoCaptureDeviceLinux::OnCaptureTask() {
// First argument to select is the highest numbered file descriptor +1.
// Refer to http://linux.die.net/man/2/select for more information.
- int result = select(device_fd_ + 1, &r_set, NULL, NULL, &timeout);
+ int result =
+ HANDLE_EINTR(select(device_fd_ + 1, &r_set, NULL, NULL, &timeout));
// Check if select have failed.
if (result < 0) {
// EINTR is a signal. This is not really an error.
@@ -507,7 +505,7 @@ void VideoCaptureDeviceLinux::OnCaptureTask() {
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
// Dequeue a buffer.
- if (ioctl(device_fd_, VIDIOC_DQBUF, &buffer) == 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_DQBUF, &buffer)) == 0) {
client_->OnIncomingCapturedFrame(
static_cast<uint8*>(buffer_pool_[buffer.index].start),
buffer.bytesused,
@@ -516,7 +514,7 @@ void VideoCaptureDeviceLinux::OnCaptureTask() {
capture_format_);
// Enqueue the buffer again.
- if (ioctl(device_fd_, VIDIOC_QBUF, &buffer) == -1) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_QBUF, &buffer)) == -1) {
SetErrorState(base::StringPrintf(
"Failed to enqueue capture buffer errno %d", errno));
}
@@ -541,7 +539,7 @@ bool VideoCaptureDeviceLinux::AllocateVideoBuffers() {
r_buffer.memory = V4L2_MEMORY_MMAP;
r_buffer.count = kMaxVideoBuffers;
- if (ioctl(device_fd_, VIDIOC_REQBUFS, &r_buffer) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_REQBUFS, &r_buffer)) < 0) {
return false;
}
@@ -560,7 +558,7 @@ bool VideoCaptureDeviceLinux::AllocateVideoBuffers() {
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = i;
- if (ioctl(device_fd_, VIDIOC_QUERYBUF, &buffer) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_QUERYBUF, &buffer)) < 0) {
return false;
}
@@ -573,7 +571,7 @@ bool VideoCaptureDeviceLinux::AllocateVideoBuffers() {
}
buffer_pool_[i].length = buffer.length;
// Enqueue the buffer in the drivers incoming queue.
- if (ioctl(device_fd_, VIDIOC_QBUF, &buffer) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_QBUF, &buffer)) < 0) {
return false;
}
}
@@ -594,7 +592,7 @@ void VideoCaptureDeviceLinux::DeAllocateVideoBuffers() {
r_buffer.memory = V4L2_MEMORY_MMAP;
r_buffer.count = 0;
- if (ioctl(device_fd_, VIDIOC_REQBUFS, &r_buffer) < 0) {
+ if (HANDLE_EINTR(ioctl(device_fd_, VIDIOC_REQBUFS, &r_buffer)) < 0) {
SetErrorState("Failed to reset buf.");
}
diff --git a/media/video/capture/linux/video_capture_device_linux.h b/media/video/capture/linux/video_capture_device_linux.h
index a5917b7..4cc1597 100644
--- a/media/video/capture/linux/video_capture_device_linux.h
+++ b/media/video/capture/linux/video_capture_device_linux.h
@@ -12,6 +12,7 @@
#include <string>
+#include "base/file_util.h"
#include "base/threading/thread.h"
#include "media/video/capture/video_capture_device.h"
#include "media/video/capture/video_capture_types.h"
@@ -60,6 +61,7 @@ class VideoCaptureDeviceLinux : public VideoCaptureDevice {
scoped_ptr<VideoCaptureDevice::Client> client_;
Name device_name_;
int device_fd_; // File descriptor for the opened camera device.
+ file_util::ScopedFD device_fd_closer_;
base::Thread v4l2_thread_; // Thread used for reading data from the device.
Buffer* buffer_pool_;
int buffer_pool_size_; // Number of allocated buffers.