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..f2550a80d1801188ddd9420483c4cc80885a87a3 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,104 @@ |
| #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 "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; |
| + |
| +int OpenWebcam(const std::string& extension_id, |
| + content::BrowserContext* browser_context, |
| + const std::string& webcam_id) { |
| + // TODO(zork): Get device_id from content::MediaStreamManager |
| + std::string device_id = "/dev/video0"; |
| + |
| + return open(device_id.c_str(), 0); |
| +} |
| + |
| +void SetWebcamParameter(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; |
| + } |
| + |
| + v4l2_ctrl.value = value; |
| + 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 (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 +116,22 @@ bool WebcamPrivateSetFunction::RunImpl() { |
| api::webcam_private::Set::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Send the value to the webcam. |
| + int fd = OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
| + if (fd < 0) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + if (params->config.pan) |
| + SetWebcamParameter(fd, WEBCAM_PAN, *(params->config.pan)); |
| + |
| + if (params->config.tilt) |
| + SetWebcamParameter(fd, WEBCAM_TILT, *(params->config.tilt)); |
| + |
| + if (params->config.zoom) |
| + SetWebcamParameter(fd, WEBCAM_ZOOM, *(params->config.zoom)); |
| + |
| + close(fd); |
| return true; |
| } |
| @@ -37,9 +148,31 @@ bool WebcamPrivateGetFunction::RunImpl() { |
| api::webcam_private::Get::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Get the value from the webcam. |
| + int fd = OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
| + if (fd < 0) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + api::webcam_private::WebcamConfiguration result; |
| + |
| + int pan; |
| + if (GetWebcamParameter(fd, WEBCAM_PAN, &pan)) |
| + result.pan.reset(new double(pan)); |
| + |
| + int tilt; |
| + if (GetWebcamParameter(fd, WEBCAM_TILT, &tilt)) |
| + result.tilt.reset(new double(tilt)); |
| - return false; |
| + int zoom; |
| + if (GetWebcamParameter(fd, WEBCAM_ZOOM, &zoom)) |
| + result.zoom.reset(new double(zoom)); |
| + |
| + close(fd); |
| + |
| + SetResult(result.ToValue().release()); |
| + |
| + return true; |
| } |
| WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
| @@ -54,7 +187,22 @@ bool WebcamPrivateResetFunction::RunImpl() { |
| api::webcam_private::Reset::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| - // TODO(zork): Reset the webcam state. |
| + int fd = OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
|
perkj_chrome
2014/04/17 09:35:48
There are some handy macros:
#include "base/files
Zachary Kuznia
2014/04/17 20:51:32
Done.
|
| + if (fd < 0) { |
| + SetError(kUnknownWebcam); |
| + return false; |
| + } |
| + |
| + if (params->config.pan) |
| + ResetWebcamParameter(fd, WEBCAM_PAN); |
| + |
| + if (params->config.tilt) |
| + ResetWebcamParameter(fd, WEBCAM_TILT); |
| + |
| + if (params->config.zoom) |
| + ResetWebcamParameter(fd, WEBCAM_ZOOM); |
| + |
| + close(fd); |
| return true; |
| } |