Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(960)

Unified Diff: media/capture/video/linux/v4l2_capture_delegate.cc

Issue 2479413002: Image Capture v4l2: reset all user controls to default values when closing device fd (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/capture/video/linux/v4l2_capture_delegate.cc
diff --git a/media/capture/video/linux/v4l2_capture_delegate.cc b/media/capture/video/linux/v4l2_capture_delegate.cc
index 8b48b8a1f087cf44a44001c78d1b8a4969a17e9c..8587b37479dc25cc6a0e3a565ca1b895e29a57ea 100644
--- a/media/capture/video/linux/v4l2_capture_delegate.cc
+++ b/media/capture/video/linux/v4l2_capture_delegate.cc
@@ -5,6 +5,7 @@
#include "media/capture/video/linux/v4l2_capture_delegate.h"
#include <linux/version.h>
+#include <linux/videodev2.h>
#include <poll.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
@@ -52,6 +53,8 @@ const int kMjpegHeight = 480;
// Typical framerate, in fps
const int kTypicalFramerate = 30;
+const int kMaxIOCtrlRetries = 5;
emircan 2016/12/07 21:45:31 Add a comment for this here as well.
mcasas 2016/12/08 23:46:52 Done.
+
// V4L2 color formats supported by V4L2CaptureDelegate derived classes.
// This list is ordered by precedence of use -- but see caveats for MJPEG.
static struct {
@@ -134,6 +137,115 @@ static mojom::RangePtr RetrieveUserControlRange(int device_fd, int control_id) {
return capability;
}
+// Determines if a |control_id| is special, i.e. controls another one's state.
+static bool IsSpecialControl(int control_id) {
+ if (control_id == V4L2_CID_AUTO_WHITE_BALANCE)
emircan 2016/12/07 21:45:31 Can you regroup this into a switch statement?
mcasas 2016/12/08 23:46:52 Done.
+ return true;
+ else if (control_id == V4L2_CID_EXPOSURE_AUTO)
+ return true;
+ else if (control_id == V4L2_CID_EXPOSURE_AUTO_PRIORITY)
+ return true;
+ else if (control_id == V4L2_CID_FOCUS_AUTO)
+ return true;
+ return false;
+}
+
+// Sets all user control to their default. Some controls are enabled by another
+// flag, usually having the word "auto" in the name, see ISpecialControl().
+// These flags are preset beforehand.
+static void ResetUserAndCameraControlsToDefault(int device_fd) {
+ // Set V4L2_CID_AUTO_WHITE_BALANCE to false first.
+ v4l2_control auto_white_balance = {};
+ auto_white_balance.id = V4L2_CID_AUTO_WHITE_BALANCE;
+ auto_white_balance.value = false;
+ int num_retries = 0;
+ // Setting up the first control right after stopping streaming seems
+ // not to the work the first time, so retry a few times.
+ for (; num_retries < kMaxIOCtrlRetries &&
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_S_CTRL, &auto_white_balance)) < 0;
emircan 2016/12/07 21:45:31 Would it help to log the return value here?
mcasas 2016/12/08 23:46:52 Not really, DPLOG() in l.167 will show what has ha
+ ++num_retries) {
+ DPLOG(WARNING) << "VIDIOC_S_CTRL";
+ }
+ if (num_retries == kMaxIOCtrlRetries) {
+ DLOG(ERROR) << "Cannot access device controls";
+ return;
+ }
+
+ std::vector<struct v4l2_ext_control> user_controls;
+ for (int control_id = V4L2_CID_USER_BASE; control_id < V4L2_CID_LASTP1;
+ ++control_id) {
+ if (IsSpecialControl(control_id))
emircan 2016/12/07 21:45:31 Can you refactor l.177-187 and l.226-236 into a st
mcasas 2016/12/08 23:46:52 I've rewritten the whole implementation so it's ho
+ continue;
+ v4l2_queryctrl range = {};
+ range.id = control_id;
+ if (HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range)) < 0)
+ continue;
+
+ struct v4l2_ext_control ext_control = {};
+ ext_control.id = control_id;
+ ext_control.value = range.default_value;
+ user_controls.push_back(ext_control);
+ }
+
+ if (!user_controls.empty()) {
+ struct v4l2_ext_controls ext_controls = {};
+ ext_controls.ctrl_class = V4L2_CTRL_CLASS_USER;
+ ext_controls.count = user_controls.size();
+ ext_controls.controls = user_controls.data();
+ if (HANDLE_EINTR(ioctl(device_fd, VIDIOC_S_EXT_CTRLS, &ext_controls)) < 0)
+ DPLOG(ERROR) << "VIDIOC_S_EXT_CTRLS";
+ }
+
+ std::vector<struct v4l2_ext_control> special_camera_controls;
+ // Set V4L2_CID_EXPOSURE_AUTO to V4L2_EXPOSURE_MANUAL.
+ v4l2_ext_control auto_exposure = {};
+ auto_exposure.id = V4L2_CID_EXPOSURE_AUTO;
+ auto_exposure.value = V4L2_EXPOSURE_MANUAL;
+ special_camera_controls.push_back(auto_exposure);
+ // Set V4L2_CID_EXPOSURE_AUTO_PRIORITY to false.
+ v4l2_ext_control priority_auto_exposure = {};
+ priority_auto_exposure.id = V4L2_CID_EXPOSURE_AUTO_PRIORITY;
+ priority_auto_exposure.value = false;
+ special_camera_controls.push_back(priority_auto_exposure);
+ // Set V4L2_CID_FOCUS_AUTO to false.
+ v4l2_ext_control auto_focus = {};
+ auto_focus.id = V4L2_CID_FOCUS_AUTO;
+ auto_focus.value = false;
+ special_camera_controls.push_back(auto_focus);
+
+ struct v4l2_ext_controls ext_controls = {};
+ ext_controls.ctrl_class = V4L2_CID_CAMERA_CLASS;
+ ext_controls.count = special_camera_controls.size();
+ ext_controls.controls = special_camera_controls.data();
+ if (HANDLE_EINTR(ioctl(device_fd, VIDIOC_S_EXT_CTRLS, &ext_controls)) < 0)
+ DPLOG(ERROR) << "VIDIOC_S_EXT_CTRLS";
+
+ std::vector<struct v4l2_ext_control> camera_controls;
+ for (int control_id = V4L2_CID_CAMERA_CLASS_BASE;
+ control_id < V4L2_CID_CAMERA_CLASS_BASE + 32; ++control_id) {
emircan 2016/12/07 21:45:31 Where is this 32 coming from? I was doing a quick
mcasas 2016/12/08 23:46:52 In the latest kernel yes, but in the 3.13 that we
+ if (IsSpecialControl(control_id))
+ continue;
+ v4l2_queryctrl range = {};
+ range.id = control_id;
+ if (HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range)) < 0)
+ continue;
+
+ struct v4l2_ext_control ext_control = {};
+ ext_control.id = control_id;
+ ext_control.value = range.default_value;
+ camera_controls.push_back(ext_control);
+ }
+
+ if (!camera_controls.empty()) {
+ struct v4l2_ext_controls ext_controls = {};
+ ext_controls.ctrl_class = V4L2_CID_CAMERA_CLASS;
+ ext_controls.count = camera_controls.size();
+ ext_controls.controls = camera_controls.data();
+ if (HANDLE_EINTR(ioctl(device_fd, VIDIOC_S_EXT_CTRLS, &ext_controls)) < 0)
+ DPLOG(ERROR) << "VIDIOC_S_EXT_CTRLS";
+ }
+}
+
// Class keeping track of a SPLANE V4L2 buffer, mmap()ed on construction and
// munmap()ed on destruction.
class V4L2CaptureDelegate::BufferTracker
@@ -355,6 +467,7 @@ void V4L2CaptureDelegate::StopAndDeAllocate() {
if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_REQBUFS, &r_buffer)) < 0)
SetErrorState(FROM_HERE, "Failed to VIDIOC_REQBUFS with count = 0");
+ ResetUserAndCameraControlsToDefault(device_fd_.get());
// At this point we can close the device.
// This is also needed for correctly changing settings later via VIDIOC_S_FMT.
device_fd_.reset();

Powered by Google App Engine
This is Rietveld 408576698