Index: media/video/capture/linux/video_capture_device_linux.cc |
diff --git a/media/video/capture/linux/video_capture_device_linux.cc b/media/video/capture/linux/video_capture_device_linux.cc |
index fdd52772cb1966d787c98d2fedfe696145c4fa37..9576273b938f2d15dfd7f2e324202ddc90d6ab86 100644 |
--- a/media/video/capture/linux/video_capture_device_linux.cc |
+++ b/media/video/capture/linux/video_capture_device_linux.cc |
@@ -101,7 +101,7 @@ static bool HasUsableFormats(int fd) { |
fmtdesc.pixelformat) != usable_fourccs.end()) |
return true; |
- fmtdesc.index++; |
+ ++fmtdesc.index; |
} |
return false; |
} |
@@ -272,6 +272,18 @@ const VideoCaptureDevice::Name& VideoCaptureDeviceLinux::device_name() { |
return device_name_; |
} |
+void VideoCaptureDeviceLinux::GetDeviceSupportedFormats( |
+ scoped_ptr<EventHandler> client) { |
+ if (v4l2_thread_.IsRunning()) { |
perkj_chrome
2013/09/25 07:58:41
|v4l2_thread| is started in Allocate - so this sti
mcasas
2013/10/02 08:13:23
If |v4l2_thread_| is not running, I'll switch it o
|
+ return; // Wrong state. |
+ } |
+ v4l2_thread_.message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&VideoCaptureDeviceLinux::OnGetDeviceSupportedFormats, |
+ base::Unretained(this), |
+ client.get())); |
+} |
+ |
void VideoCaptureDeviceLinux::OnAllocate(int width, |
int height, |
int frame_rate, |
@@ -312,7 +324,7 @@ void VideoCaptureDeviceLinux::OnAllocate(int width, |
std::list<int>::iterator best = v4l2_formats.end(); |
while (ioctl(device_fd_, VIDIOC_ENUM_FMT, &fmtdesc) == 0) { |
best = std::find(v4l2_formats.begin(), best, fmtdesc.pixelformat); |
- fmtdesc.index++; |
+ ++fmtdesc.index; |
} |
if (best == v4l2_formats.end()) { |
@@ -435,6 +447,92 @@ void VideoCaptureDeviceLinux::OnStop() { |
DeAllocateVideoBuffers(); |
} |
+void VideoCaptureDeviceLinux::OnGetDeviceSupportedFormats( |
+ EventHandler* client) { |
+ DCHECK_EQ(v4l2_thread_.message_loop(), base::MessageLoop::current()); |
+ if (device_name_.id().empty()) |
+ return; |
+ int fd; |
perkj_chrome
2013/09/25 07:58:41
please put the code for opening the driver in one
mcasas
2013/10/02 08:13:23
Done.
|
+ // If device_fd_ exists, means the current class has already opened it, |
+ // but otherwise we open it ourselves, and close it at leaving. |
+ if (device_fd_ < 0) { |
+ if ((fd = HANDLE_EINTR(open(device_name_.id().c_str(), O_RDONLY))) < 0) { |
+ DPLOG(ERROR) << "Couldn't open " << device_name_.id(); |
+ return; |
+ } |
+ } else |
+ fd = device_fd_; |
+ |
+ VideoCaptureCapability capture_format; |
+ VideoCaptureFormats capture_formats; |
+ |
+ v4l2_capability device; |
+ // Test if this is a V4L2 capture device. |
perkj_chrome
2013/09/25 07:58:41
No need to do this again.
mcasas
2013/10/02 08:13:23
If this method is called _before_ Allocate+Init, w
|
+ if ((ioctl(fd, VIDIOC_QUERYCAP, &device) != 0) || |
+ !(device.capabilities & V4L2_CAP_VIDEO_CAPTURE) || |
+ (device.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { |
+ // The selected device is not video capture as we like it. |
+ close(fd); |
+ capture_formats.clear(); |
+ return; |
+ } |
+ |
+ // Retrieve the caps one by one, first get colorspace, then sizes, then |
+ // framerates. 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) { |
+ capture_format.color = |
+ V4l2ColorToVideoCaptureColorFormat(pixel_format.pixelformat); |
+ |
+ v4l2_frmsizeenum frame_size = {}; |
+ frame_size.pixel_format = pixel_format.pixelformat; |
+ while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frame_size) == 0) { |
+ if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) { |
+ capture_format.width = frame_size.discrete.width; |
+ capture_format.height = frame_size.discrete.height; |
+ } else if (frame_size.type == V4L2_FRMSIZE_TYPE_STEPWISE) { |
+ // TODO(mcasas): see http://crbug.com/249953, support these devices. |
+ NOTIMPLEMENTED(); |
+ } else if (frame_size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) { |
+ // TODO(mcasas): see http://crbug.com/249953, support these devices. |
+ NOTIMPLEMENTED(); |
+ } |
+ v4l2_frmivalenum frame_interval = {}; |
+ 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) { |
+ if (frame_interval.type == V4L2_FRMIVAL_TYPE_DISCRETE) { |
+ if (frame_interval.discrete.numerator != 0) { |
+ capture_format.frame_rate = |
+ static_cast<float>(frame_interval.discrete.denominator) / |
+ static_cast<float>(frame_interval.discrete.numerator); |
+ } else |
+ capture_format.frame_rate = 0; |
+ |
+ } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) { |
+ // TODO(mcasas): see http://crbug.com/249953, support these devices. |
+ NOTIMPLEMENTED(); |
+ break; |
+ } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_STEPWISE) { |
+ // TODO(mcasas): see http://crbug.com/249953, support these devices. |
+ NOTIMPLEMENTED(); |
+ break; |
+ } |
+ capture_formats.push_back(capture_format); |
+ ++frame_interval.index; |
+ } |
+ ++frame_size.index; |
+ } |
+ ++pixel_format.index; |
+ } |
+ if (device_fd_ < 0) |
+ close(fd); |
perkj_chrome
2013/09/25 07:58:41
What if we are already capturing? Does it work to
mcasas
2013/10/02 08:13:23
Device file is only close if it was closed wheneve
|
+ // Report the resulting capabilities to the observer. |
+ client->OnDeviceSupportedFormatsEnumerated(capture_formats); |
+} |
+ |
void VideoCaptureDeviceLinux::OnCaptureTask() { |
DCHECK_EQ(v4l2_thread_.message_loop(), base::MessageLoop::current()); |
@@ -469,7 +567,7 @@ void VideoCaptureDeviceLinux::OnCaptureTask() { |
// Check if select timeout. |
if (result == 0) { |
- timeout_count_++; |
+ ++timeout_count_; |
if (timeout_count_ >= kContinuousTimeoutLimit) { |
SetErrorState(base::StringPrintf( |
"Continuous timeout %d times", timeout_count_)); |
@@ -530,7 +628,7 @@ bool VideoCaptureDeviceLinux::AllocateVideoBuffers() { |
// Map the buffers. |
buffer_pool_ = new Buffer[r_buffer.count]; |
- for (unsigned int i = 0; i < r_buffer.count; i++) { |
+ for (unsigned int i = 0; i < r_buffer.count; ++i) { |
v4l2_buffer buffer; |
memset(&buffer, 0, sizeof(buffer)); |
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
@@ -562,7 +660,7 @@ void VideoCaptureDeviceLinux::DeAllocateVideoBuffers() { |
return; |
// Unmaps buffers. |
- for (int i = 0; i < buffer_pool_size_; i++) { |
+ for (int i = 0; i < buffer_pool_size_; ++i) { |
munmap(buffer_pool_[i].start, buffer_pool_[i].length); |
} |
v4l2_requestbuffers r_buffer; |