Chromium Code Reviews| Index: chrome/browser/extensions/api/webcam_private/webcam_private_api.cc |
| diff --git a/chrome/browser/extensions/api/webcam_private/webcam_private_api.cc b/chrome/browser/extensions/api/webcam_private/webcam_private_api.cc |
| index f956ba4a11d4f056906d3fec1168d3f802cca9c6..a0259d2f84bdade987d3ce588030a24883fb5cb2 100644 |
| --- a/chrome/browser/extensions/api/webcam_private/webcam_private_api.cc |
| +++ b/chrome/browser/extensions/api/webcam_private/webcam_private_api.cc |
| @@ -4,8 +4,106 @@ |
| #include "chrome/browser/extensions/api/webcam_private/webcam_private_api.h" |
| +#include <fcntl.h> |
| +#include <linux/videodev2.h> |
| +#include <stdio.h> |
| +#include <sys/ioctl.h> |
| +#include <unistd.h> |
| + |
| +#include "base/files/scoped_file.h" |
| +#include "base/posix/eintr_wrapper.h" |
| #include "chrome/common/extensions/api/webcam_private.h" |
| +namespace content { |
| +class BrowserContext; |
| +} // namespace content |
| + |
| +namespace { |
| + |
| +enum WebcamParameter { |
| + WEBCAM_PAN, |
| + WEBCAM_TILT, |
| + WEBCAM_ZOOM, |
| +}; |
| + |
| +const int kDefaultZoom = 100; |
|
vrk (LEFT CHROMIUM)
2014/04/17 23:16:56
You should make this a static local constant in th
Zachary Kuznia
2014/04/18 22:44:15
Done.
|
| + |
| +base::ScopedFD OpenWebcam(const std::string& extension_id, |
| + content::BrowserContext* browser_context, |
| + const std::string& webcam_id) { |
| + // TODO(zork): Get device_id from content::MediaStreamManager |
|
vrk (LEFT CHROMIUM)
2014/04/17 23:16:56
nit: period at end of sentence
Zachary Kuznia
2014/04/18 22:44:15
Done.
|
| + std::string device_id = "/dev/video0"; |
|
vrk (LEFT CHROMIUM)
2014/04/17 23:16:56
Hmmm... this feels wrong to me. I know you're plan
Zachary Kuznia
2014/04/18 22:44:15
I'll be sending out a follow-up CL for review as s
|
| + |
| + return base::ScopedFD(HANDLE_EINTR(open(device_id.c_str(), 0))); |
| +} |
| + |
| +void SetWebcamParameter(int fd, WebcamParameter param, int value) { |
|
vrk (LEFT CHROMIUM)
2014/04/17 23:16:56
The WebcamParameter enum seems superfluous. I'd sp
Zachary Kuznia
2014/04/18 22:44:15
Done.
|
| + struct v4l2_control v4l2_ctrl; |
| + switch (param) { |
| + case WEBCAM_PAN: |
| + v4l2_ctrl.id = V4L2_CID_PAN_ABSOLUTE; |
| + break; |
| + |
| + case WEBCAM_TILT: |
| + v4l2_ctrl.id = V4L2_CID_TILT_ABSOLUTE; |
| + break; |
| + |
| + case WEBCAM_ZOOM: |
| + v4l2_ctrl.id = V4L2_CID_ZOOM_ABSOLUTE; |
| + break; |
| + } |
| + |
| + v4l2_ctrl.value = value; |
| + HANDLE_EINTR(ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl)); |
| +} |
| + |
| +bool GetWebcamParameter(int fd, WebcamParameter param, int* value) { |
| + struct v4l2_control v4l2_ctrl; |
| + switch (param) { |
| + case WEBCAM_PAN: |
| + v4l2_ctrl.id = V4L2_CID_PAN_ABSOLUTE; |
| + break; |
| + case WEBCAM_TILT: |
| + v4l2_ctrl.id = V4L2_CID_TILT_ABSOLUTE; |
| + break; |
| + case WEBCAM_ZOOM: |
| + v4l2_ctrl.id = V4L2_CID_ZOOM_ABSOLUTE; |
| + break; |
| + } |
| + |
| + if (HANDLE_EINTR(ioctl(fd, VIDIOC_G_CTRL, &v4l2_ctrl))) |
| + return false; |
| + |
| + *value = v4l2_ctrl.value; |
| + return true; |
| +} |
| + |
| +void ResetWebcamParameter(int fd, WebcamParameter param) { |
| + switch (param) { |
| + case WEBCAM_PAN: { |
| + struct v4l2_control v4l2_ctrl; |
| + v4l2_ctrl.id = V4L2_CID_PAN_RESET; |
| + ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl); |
| + } |
| + break; |
| + |
| + case WEBCAM_TILT: { |
| + struct v4l2_control v4l2_ctrl; |
| + v4l2_ctrl.id = V4L2_CID_TILT_RESET; |
| + ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl); |
| + } |
| + break; |
| + |
| + case WEBCAM_ZOOM: |
| + SetWebcamParameter(fd, WEBCAM_ZOOM, kDefaultZoom); |
| + break; |
| + } |
| + |
| +} |
| + |
| +const char kUnknownWebcam[] = "Unknown webcam id"; |
| +} // namespace |
| + |
| namespace extensions { |
| WebcamPrivateSetFunction::WebcamPrivateSetFunction() { |
| @@ -20,7 +118,21 @@ bool WebcamPrivateSetFunction::RunImpl() { |
| api::webcam_private::Set::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Send the value to the webcam. |
| + base::ScopedFD fd = |
| + OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
| + if (!fd.is_valid()) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + if (params->config.pan) |
| + SetWebcamParameter(fd.get(), WEBCAM_PAN, *(params->config.pan)); |
| + |
| + if (params->config.tilt) |
| + SetWebcamParameter(fd.get(), WEBCAM_TILT, *(params->config.tilt)); |
| + |
| + if (params->config.zoom) |
| + SetWebcamParameter(fd.get(), WEBCAM_ZOOM, *(params->config.zoom)); |
| return true; |
| } |
| @@ -37,9 +149,30 @@ bool WebcamPrivateGetFunction::RunImpl() { |
| api::webcam_private::Get::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Get the value from the webcam. |
| + base::ScopedFD fd = |
| + OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
| + if (!fd.is_valid()) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + api::webcam_private::WebcamConfiguration result; |
| + |
| + int pan; |
| + if (GetWebcamParameter(fd.get(), WEBCAM_PAN, &pan)) |
| + result.pan.reset(new double(pan)); |
| - return false; |
| + int tilt; |
| + if (GetWebcamParameter(fd.get(), WEBCAM_TILT, &tilt)) |
| + result.tilt.reset(new double(tilt)); |
| + |
| + int zoom; |
| + if (GetWebcamParameter(fd.get(), WEBCAM_ZOOM, &zoom)) |
| + result.zoom.reset(new double(zoom)); |
| + |
| + SetResult(result.ToValue().release()); |
| + |
| + return true; |
| } |
| WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
| @@ -54,7 +187,21 @@ bool WebcamPrivateResetFunction::RunImpl() { |
| api::webcam_private::Reset::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Reset the webcam state. |
| + base::ScopedFD fd = |
| + OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
| + if (!fd.is_valid()) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + if (params->config.pan) |
| + ResetWebcamParameter(fd.get(), WEBCAM_PAN); |
| + |
| + if (params->config.tilt) |
| + ResetWebcamParameter(fd.get(), WEBCAM_TILT); |
| + |
| + if (params->config.zoom) |
| + ResetWebcamParameter(fd.get(), WEBCAM_ZOOM); |
| return true; |
| } |