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

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..7277641aa7b4ad58e6b1768146496002ca21e360 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>
@@ -77,6 +78,16 @@ static struct {
{V4L2_PIX_FMT_JPEG, PIXEL_FORMAT_MJPEG, 1},
};
+// Maximum number of ioctl retries before giving up trying to reset controls.
+const int kMaxIOCtrlRetries = 5;
+
+// Base id and class identifier for Controls to be reset.
+static struct {
+ uint32_t control_base;
+ uint32_t class_id;
+} const kControls[] = {{V4L2_CID_USER_BASE, V4L2_CID_USER_CLASS},
+ {V4L2_CID_CAMERA_CLASS_BASE, V4L2_CID_CAMERA_CLASS}};
+
// Fill in |format| with the given parameters.
static void FillV4L2Format(v4l2_format* format,
uint32_t width,
@@ -134,6 +145,128 @@ static mojom::RangePtr RetrieveUserControlRange(int device_fd, int control_id) {
return capability;
}
+// Determines if |control_id| is special, i.e. controls another one's state.
+static bool IsSpecialControl(int control_id) {
+ switch (control_id) {
+ case V4L2_CID_AUTO_WHITE_BALANCE:
+ case V4L2_CID_EXPOSURE_AUTO:
+ case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
+ case 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 IsSpecialControl().
+// These flags are preset beforehand, then set to their defaults individually
+// afterwards.
+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;
+ ++num_retries) {
+ DPLOG(WARNING) << "VIDIOC_S_CTRL";
+ }
+ if (num_retries == kMaxIOCtrlRetries) {
+ DLOG(ERROR) << "Cannot access device controls";
+ return;
+ }
+
+ 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 (const auto& control : kControls) {
+ std::vector<struct v4l2_ext_control> camera_controls;
+
+ v4l2_queryctrl range = {};
+ range.id = control.control_base | V4L2_CTRL_FLAG_NEXT_CTRL;
+ while (0 == HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range))) {
+ if (V4L2_CTRL_ID2CLASS(range.id) != V4L2_CTRL_ID2CLASS(control.class_id))
+ break;
+ range.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
+
+ if (IsSpecialControl(range.id & ~V4L2_CTRL_FLAG_NEXT_CTRL))
+ continue;
+
+ struct v4l2_ext_control ext_control = {};
+ ext_control.id = range.id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
+ 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 = control.class_id;
+ 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";
+ }
+ }
+
+ // Now set the special flags to the default values
+ v4l2_queryctrl range = {};
+ range.id = V4L2_CID_AUTO_WHITE_BALANCE;
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range));
+ auto_white_balance.value = range.default_value;
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_S_CTRL, &auto_white_balance));
+
+ special_camera_controls.clear();
+ memset(&range, 0, sizeof(struct v4l2_queryctrl));
+ range.id = V4L2_CID_EXPOSURE_AUTO;
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range));
+ auto_exposure.value = range.default_value;
+ special_camera_controls.push_back(auto_exposure);
+
+ memset(&range, 0, sizeof(struct v4l2_queryctrl));
+ range.id = V4L2_CID_EXPOSURE_AUTO_PRIORITY;
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range));
+ priority_auto_exposure.value = range.default_value;
+ special_camera_controls.push_back(priority_auto_exposure);
+
+ memset(&range, 0, sizeof(struct v4l2_queryctrl));
+ range.id = V4L2_CID_FOCUS_AUTO;
+ HANDLE_EINTR(ioctl(device_fd, VIDIOC_QUERYCTRL, &range));
+ auto_focus.value = range.default_value;
+ special_camera_controls.push_back(auto_focus);
+
+ memset(&ext_controls, 0, sizeof(struct v4l2_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";
+}
+
// Class keeping track of a SPLANE V4L2 buffer, mmap()ed on construction and
// munmap()ed on destruction.
class V4L2CaptureDelegate::BufferTracker
@@ -355,6 +488,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();
« no previous file with comments | « media/capture/video/linux/v4l2_capture_delegate.h ('k') | media/capture/video/linux/v4l2_capture_delegate_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698