Abstract V4L2 controls into wrapper class.
This abstraction will both clean up the code in V4L2Camera.cpp, and make it
easier to implement tests in the future (by mocking over the class).
This CL only adds the class and implementation, it does not yet replace
the logic in V4L2Camera.cpp.
Also gates building the module, to prevent checkbuilds without the
proper v4l2 kernel headers from trying to build it.
BUG: 30140438
Change-Id: I4e5feee30fdf896b1f71f0df492ca313f5581e78
diff --git a/modules/camera/3_4/V4L2Wrapper.h b/modules/camera/3_4/V4L2Wrapper.h
new file mode 100644
index 0000000..4126e90
--- /dev/null
+++ b/modules/camera/3_4/V4L2Wrapper.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef V4L2_WRAPPER_H
+#define V4L2_WRAPPER_H
+
+#include <memory>
+#include <mutex>
+#include <string>
+
+#include <nativehelper/ScopedFd.h>
+
+#include "Common.h"
+#include "Stream.h"
+#include "StreamFormat.h"
+
+namespace v4l2_camera_hal {
+class V4L2Wrapper {
+ public:
+ V4L2Wrapper(const std::string device_path);
+ virtual ~V4L2Wrapper();
+
+ // Connect or disconnect to the device.
+ int Connect();
+ void Disconnect();
+ // Turn the stream on or off.
+ int StreamOn();
+ int StreamOff();
+ // Manage controls.
+ int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
+ int GetControl(uint32_t control_id, int32_t* value);
+ int SetControl(uint32_t control_id, int32_t desired, int32_t* result);
+ // Manage format.
+ int SetFormat(const default_camera_hal::Stream& stream);
+
+ inline bool connected() { return device_fd_.get() >= 0; }
+
+ private:
+ // Perform an ioctl call in a thread-safe fashion.
+ template <typename T>
+ int IoctlLocked(int request, T data);
+ // Adjust buffers any time a device is connected/reformatted.
+ int SetupBuffers();
+
+ // The camera device path. For example, /dev/video0.
+ const std::string device_path_;
+ // The opened device fd.
+ ScopedFd device_fd_;
+ // Whether or not the device supports the extended control query.
+ bool extended_query_supported_;
+ // The format this device is set up for.
+ std::unique_ptr<StreamFormat> format_;
+ // The maximum number of buffers this device can handle in its current format.
+ uint32_t max_buffers_;
+ // Lock protecting use of the device.
+ std::mutex device_lock_;
+
+ DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
+};
+
+} // namespace v4l2_camera_hal
+
+#endif // V4L2_WRAPPER_H