Replace V4L2 calls in V4L2Camera with wrapper.

Also updates naming of max streams to be in line with what they
technically are, not what they usually are (i.e. a non-stalling
processed stream is usually YUV, but not necessarily).

BUG: 30140438

Change-Id: Idb78c466a41c9c0e5957200fc7c8afc0cfdb88a6
diff --git a/modules/camera/3_4/V4L2Wrapper.cpp b/modules/camera/3_4/V4L2Wrapper.cpp
index 495467a..16061bf 100644
--- a/modules/camera/3_4/V4L2Wrapper.cpp
+++ b/modules/camera/3_4/V4L2Wrapper.cpp
@@ -46,7 +46,9 @@
 
 V4L2Wrapper::V4L2Wrapper(const std::string device_path,
                          std::unique_ptr<V4L2Gralloc> gralloc)
-    : device_path_(device_path), gralloc_(std::move(gralloc)), max_buffers_(0) {
+    : device_path_(std::move(device_path)),
+      gralloc_(std::move(gralloc)),
+      max_buffers_(0) {
   HAL_LOG_ENTER();
 }
 
@@ -130,6 +132,11 @@
 int V4L2Wrapper::StreamOff() {
   HAL_LOG_ENTER();
 
+  if (!format_) {
+    HAL_LOGE("Stream format must be set to turn off stream.");
+    return -ENODEV;
+  }
+
   int32_t type = format_->type();
   int res = IoctlLocked(VIDIOC_STREAMOFF, &type);
   // Calling STREAMOFF releases all queued buffers back to the user.
@@ -233,7 +240,8 @@
   return 0;
 }
 
-int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream) {
+int V4L2Wrapper::SetFormat(const default_camera_hal::Stream& stream,
+                           uint32_t* result_max_buffers) {
   HAL_LOG_ENTER();
 
   // Should be checked earlier; sanity check.
@@ -243,7 +251,7 @@
   }
 
   StreamFormat desired_format(stream);
-  if (desired_format == *format_) {
+  if (format_ && desired_format == *format_) {
     HAL_LOGV("Already in correct format, skipping format setting.");
     return 0;
   }
@@ -268,13 +276,23 @@
   format_.reset(new StreamFormat(new_format));
 
   // Format changed, setup new buffers.
-  SetupBuffers();
+  int res = SetupBuffers();
+  if (res) {
+    HAL_LOGE("Failed to set up buffers for new format.");
+    return res;
+  }
+  *result_max_buffers = max_buffers_;
   return 0;
 }
 
 int V4L2Wrapper::SetupBuffers() {
   HAL_LOG_ENTER();
 
+  if (!format_) {
+    HAL_LOGE("Stream format must be set before setting up buffers.");
+    return -ENODEV;
+  }
+
   // "Request" a buffer (since we're using a userspace buffer, this just
   // tells V4L2 to switch into userspace buffer mode).
   v4l2_requestbuffers req_buffers;
@@ -297,12 +315,22 @@
 
   // V4L2 will set req_buffers.count to a number of buffers it can handle.
   max_buffers_ = req_buffers.count;
+  // Sanity check.
+  if (max_buffers_ < 1) {
+    HAL_LOGE("REQBUFS claims it can't handle any buffers.");
+    return -ENODEV;
+  }
   return 0;
 }
 
 int V4L2Wrapper::EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
   HAL_LOG_ENTER();
 
+  if (!format_) {
+    HAL_LOGE("Stream format must be set before enqueuing buffers.");
+    return -ENODEV;
+  }
+
   // Set up a v4l2 buffer struct.
   v4l2_buffer device_buffer;
   memset(&device_buffer, 0, sizeof(device_buffer));
@@ -339,6 +367,11 @@
 int V4L2Wrapper::DequeueBuffer(v4l2_buffer* buffer) {
   HAL_LOG_ENTER();
 
+  if (!format_) {
+    HAL_LOGE("Stream format must be set before dequeueing buffers.");
+    return -ENODEV;
+  }
+
   memset(buffer, 0, sizeof(*buffer));
   buffer->type = format_->type();
   buffer->memory = V4L2_MEMORY_USERPTR;
@@ -350,6 +383,7 @@
   // Now that we're done painting the buffer, we can unlock it.
   int res = gralloc_->unlock(buffer);
   if (res) {
+    HAL_LOGE("Gralloc failed to unlock buffer after dequeueing.");
     return res;
   }