Factoring out camera_params.proto.

Change-Id: Idf886f9cb33764d505d5cff11ca766cea96d2024
diff --git a/aos/vision/debug/camera-source.cc b/aos/vision/debug/camera-source.cc
index ef48a11..77bd6ed 100644
--- a/aos/vision/debug/camera-source.cc
+++ b/aos/vision/debug/camera-source.cc
@@ -4,6 +4,7 @@
 #include <fstream>
 #include <string>
 
+#include "aos/vision/image/camera_params.pb.h"
 #include "aos/vision/image/image_stream.h"
 
 namespace aos {
@@ -15,26 +16,20 @@
             DebugFrameworkInterface *interface) override {
     // TODO: Get these params from a config file passed in through the
     // constructor.
-    camera::CameraParams params = {.width = 640 * 2,
-                                   .height = 480 * 2,
-                                   .exposure = 10,
-                                   .brightness = 128,
-                                   .gain = 0,
-                                   .fps = 30};
-    image_stream_.reset(new ImageStream(jpeg_list_filename, params, interface));
+    image_stream_.reset(new ImageStream(
+        jpeg_list_filename, aos::vision::CameraParams(), interface));
   }
 
   const char *GetHelpMessage() override {
     return &R"(
     format_spec is filename of the camera device.
     example: camera:/dev/video0
-    This viewer source will stream video from a usb camera of your choice.
-)"[1];
+    This viewer source will stream video from a usb camera of your choice.)"[1];
   }
 
   class ImageStream : public ImageStreamEvent {
    public:
-    ImageStream(const std::string &fname, camera::CameraParams params,
+    ImageStream(const std::string &fname, aos::vision::CameraParams params,
                 DebugFrameworkInterface *interface)
         : ImageStreamEvent(fname, params), interface_(interface) {
       interface_->Loop()->Add(this);
diff --git a/aos/vision/image/BUILD b/aos/vision/image/BUILD
index 662addc..27646a5 100644
--- a/aos/vision/image/BUILD
+++ b/aos/vision/image/BUILD
@@ -1,4 +1,5 @@
 package(default_visibility = ['//visibility:public'])
+load('/tools/build_rules/protobuf', 'proto_cc_library')
 
 cc_library(
   name = 'image_types',
@@ -8,6 +9,11 @@
   ],
 )
 
+proto_cc_library(
+  name = 'camera_params',
+  src = 'camera_params.proto',
+)
+
 cc_library(
   name = 'reader',
   srcs = ['reader.cc'],
@@ -16,6 +22,7 @@
     '//aos/common:time',
     '//aos/common/logging:logging',
     ':image_types',
+    ':camera_params',
   ],
 )
 
diff --git a/aos/vision/image/camera_params.proto b/aos/vision/image/camera_params.proto
new file mode 100644
index 0000000..c109154
--- /dev/null
+++ b/aos/vision/image/camera_params.proto
@@ -0,0 +1,23 @@
+syntax = "proto2";
+
+package aos.vision;
+
+message CameraParams {
+  // Width of the image.
+  optional int32 width = 1 [default = 1280];
+
+  // Height of the image.
+  optional int32 height = 2 [default = 960];
+
+  // Exposure setting.
+  optional int32 exposure = 3 [default = 10];
+
+  // Brightness setting.
+  optional int32 brightness = 4 [default = 128];
+
+  // Hardware gain multiplier on pixel values.
+  optional int32 gain = 5 [default = 0];
+
+  // Frames per second to run camera.
+  optional int32 fps = 6 [default = 30];
+}
diff --git a/aos/vision/image/image_stream.h b/aos/vision/image/image_stream.h
index 5a1ad21..308d3ec 100644
--- a/aos/vision/image/image_stream.h
+++ b/aos/vision/image/image_stream.h
@@ -2,6 +2,7 @@
 #define _AOS_VISION_IMAGE_IMAGE_STREAM_H_
 
 #include "aos/vision/events/epoll_events.h"
+#include "aos/vision/image/camera_params.pb.h"
 #include "aos/vision/image/reader.h"
 
 #include <memory>
@@ -15,7 +16,7 @@
  public:
   static std::unique_ptr<::camera::Reader> GetCamera(
       const std::string &fname, ImageStreamEvent *obj,
-      camera::CameraParams params) {
+      aos::vision::CameraParams params) {
     using namespace std::placeholders;
     std::unique_ptr<::camera::Reader> camread(new ::camera::Reader(
         fname, std::bind(&ImageStreamEvent::ProcessHelper, obj, _1, _2),
@@ -28,7 +29,7 @@
       : ::aos::events::EpollEvent(reader->fd()), reader_(std::move(reader)) {}
 
   explicit ImageStreamEvent(const std::string &fname,
-                            camera::CameraParams params)
+                            aos::vision::CameraParams params)
       : ImageStreamEvent(GetCamera(fname, this, params)) {}
 
   void ProcessHelper(DataRef data, aos::monotonic_clock::time_point timestamp) {
diff --git a/aos/vision/image/reader.cc b/aos/vision/image/reader.cc
index 03e2fc8..57766e2 100644
--- a/aos/vision/image/reader.cc
+++ b/aos/vision/image/reader.cc
@@ -1,7 +1,19 @@
-#include "aos/common/time.h"
+#include "aos/vision/image/reader.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "aos/common/logging/logging.h"
-#include "aos/vision/image/reader.h"
+#include "aos/common/time.h"
 
 #define CLEAR(x) memset(&(x), 0, sizeof(x))
 
@@ -12,8 +24,21 @@
   size_t length;  // for munmap
 };
 
+aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height,
+                                           int32_t exposure, int32_t brightness,
+                                           int32_t gain, int32_t fps) {
+  aos::vision::CameraParams cam;
+  cam.set_width(width);
+  cam.set_height(height);
+  cam.set_exposure(exposure);
+  cam.set_brightness(brightness);
+  cam.set_gain(gain);
+  cam.set_fps(fps);
+  return cam;
+}
+
 Reader::Reader(const std::string &dev_name, ProcessCb process,
-               CameraParams params)
+               aos::vision::CameraParams params)
     : dev_name_(dev_name), process_(std::move(process)), params_(params) {
   struct stat st;
   if (stat(dev_name.c_str(), &st) == -1) {
@@ -67,7 +92,7 @@
     }
 
     if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
-                          "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure)) {
+                          "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) {
       LOG(FATAL, "Failed to set exposure\n");
     }
   }
@@ -200,8 +225,8 @@
   v4l2_format fmt;
   CLEAR(fmt);
   fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-  fmt.fmt.pix.width = params_.width;
-  fmt.fmt.pix.height = params_.height;
+  fmt.fmt.pix.width = params_.width();
+  fmt.fmt.pix.height = params_.height();
   fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
   fmt.fmt.pix.field = V4L2_FIELD_ANY;
   // fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
@@ -224,16 +249,16 @@
   }
 
   if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
-                        "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure)) {
+                        "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure())) {
     LOG(FATAL, "Failed to set exposure\n");
   }
 
   if (!SetCameraControl(V4L2_CID_BRIGHTNESS, "V4L2_CID_BRIGHTNESS",
-                        params_.brightness)) {
+                        params_.brightness())) {
     LOG(FATAL, "Failed to set up camera\n");
   }
 
-  if (!SetCameraControl(V4L2_CID_GAIN, "V4L2_CID_GAIN", params_.gain)) {
+  if (!SetCameraControl(V4L2_CID_GAIN, "V4L2_CID_GAIN", params_.gain())) {
     LOG(FATAL, "Failed to set up camera\n");
   }
 
@@ -243,7 +268,7 @@
   memset(setfps, 0, sizeof(struct v4l2_streamparm));
   setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
   setfps->parm.capture.timeperframe.numerator = 1;
-  setfps->parm.capture.timeperframe.denominator = params_.fps;
+  setfps->parm.capture.timeperframe.denominator = params_.fps();
   if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) {
     PLOG(FATAL, "ioctl VIDIOC_S_PARM(%d, %p)\n", fd_, setfps);
   }
diff --git a/aos/vision/image/reader.h b/aos/vision/image/reader.h
index 28735fb..e913ad1 100644
--- a/aos/vision/image/reader.h
+++ b/aos/vision/image/reader.h
@@ -1,16 +1,5 @@
 #ifndef AOS_VISION_IMAGE_READER_H_
 #define AOS_VISION_IMAGE_READER_H_
-#include <errno.h>
-#include <fcntl.h>
-#include <malloc.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <unistd.h>
 
 #include <inttypes.h>
 #include <functional>
@@ -18,24 +7,21 @@
 
 #include "aos/common/time.h"
 #include "aos/vision/image/V4L2.h"
+#include "aos/vision/image/camera_params.pb.h"
 #include "aos/vision/image/image_types.h"
 
 namespace camera {
 
-struct CameraParams {
-  int32_t width;
-  int32_t height;
-  int32_t exposure;
-  int32_t brightness;
-  int32_t gain;
-  int32_t fps;
-};
+aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height,
+                                           int32_t exposure, int32_t brightness,
+                                           int32_t gain, int32_t fps);
 
 class Reader {
  public:
   using ProcessCb = std::function<void(
       aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
-  Reader(const std::string &dev_name, ProcessCb process, CameraParams params);
+  Reader(const std::string &dev_name, ProcessCb process,
+         aos::vision::CameraParams params);
 
   aos::vision::ImageFormat get_format();
 
@@ -74,7 +60,7 @@
   static const unsigned int kNumBuffers = 5;
 
   // set only at initialize
-  CameraParams params_;
+  aos::vision::CameraParams params_;
 };
 
 }  // namespace camera
diff --git a/aos/vision/tools/camera_primer.cc b/aos/vision/tools/camera_primer.cc
index a431ac8..3ba24bc 100644
--- a/aos/vision/tools/camera_primer.cc
+++ b/aos/vision/tools/camera_primer.cc
@@ -5,7 +5,7 @@
 
 class ImageStream : public aos::vision::ImageStreamEvent {
  public:
-  ImageStream(const std::string &fname, camera::CameraParams params)
+  ImageStream(const std::string &fname, aos::vision::CameraParams params)
       : ImageStreamEvent(fname, params) {}
   void ProcessImage(aos::vision::DataRef /*data*/,
                     aos::monotonic_clock::time_point) override {
@@ -30,12 +30,7 @@
   ::aos::logging::AddImplementation(
       new ::aos::logging::StreamLogImplementation(stdout));
 
-  camera::CameraParams params = {.width = 640 * 2,
-                                 .height = 480 * 2,
-                                 .exposure = 10,
-                                 .brightness = 128,
-                                 .gain = 0,
-                                 .fps = 30};
+  aos::vision::CameraParams params;
 
   if (argc != 2) {
     fprintf(stderr, "usage: %s path_to_camera\n", argv[0]);
diff --git a/aos/vision/tools/jpeg_vision_test.cc b/aos/vision/tools/jpeg_vision_test.cc
index 52dd22f..059166e 100644
--- a/aos/vision/tools/jpeg_vision_test.cc
+++ b/aos/vision/tools/jpeg_vision_test.cc
@@ -42,7 +42,7 @@
 class ChannelImageStream : public ImageStreamEvent {
  public:
   ChannelImageStream(const std::string &fname,
-                     const camera::CameraParams &params)
+                     const aos::vision::CameraParams &params)
       : ImageStreamEvent(fname, params), view_(false) {
     // Lambda to record image data to a file on key press.
     view_.view()->key_press_event = [this](uint32_t keyval) {
@@ -117,12 +117,7 @@
   aos::events::EpollLoop loop;
   gtk_init(&argc, &argv);
 
-  camera::CameraParams params = {.width = 640 * 2,
-                                 .height = 480 * 2,
-                                 .exposure = 10,
-                                 .brightness = 128,
-                                 .gain = 0,
-                                 .fps = 25};
+  aos::vision::CameraParams params;
 
   aos::vision::ChannelImageStream strm1("/dev/video1", params);