Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
|
not at google - send to devlin
2014/04/24 16:09:53
if this is effectively the same file then make sur
| |
| 4 | |
| 5 #include "chrome/browser/extensions/api/webcam_private/webcam_private_api.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <linux/videodev2.h> | |
| 9 #include <stdio.h> | |
| 10 #include <sys/ioctl.h> | |
| 11 #include <unistd.h> | |
| 12 | |
| 13 #include "base/files/scoped_file.h" | |
| 14 #include "base/posix/eintr_wrapper.h" | |
| 15 #include "chrome/common/extensions/api/webcam_private.h" | |
| 16 | |
| 17 namespace content { | |
| 18 class BrowserContext; | |
| 19 } // namespace content | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 base::ScopedFD OpenWebcam(const std::string& extension_id, | |
| 24 content::BrowserContext* browser_context, | |
| 25 const std::string& webcam_id) { | |
| 26 // TODO(zork): Get device_id from content::MediaStreamManager. | |
| 27 std::string device_id = "/dev/video0"; | |
| 28 | |
| 29 return base::ScopedFD(HANDLE_EINTR(open(device_id.c_str(), 0))); | |
| 30 } | |
| 31 | |
| 32 void SetWebcamParameter(int fd, int control_id, int value) { | |
| 33 struct v4l2_control v4l2_ctrl = {control_id, value}; | |
| 34 HANDLE_EINTR(ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl)); | |
| 35 } | |
| 36 | |
| 37 bool GetWebcamParameter(int fd, int control_id, int* value) { | |
| 38 struct v4l2_control v4l2_ctrl = {control_id}; | |
| 39 | |
| 40 if (HANDLE_EINTR(ioctl(fd, VIDIOC_G_CTRL, &v4l2_ctrl))) | |
| 41 return false; | |
| 42 | |
| 43 *value = v4l2_ctrl.value; | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 const char kUnknownWebcam[] = "Unknown webcam id"; | |
| 48 } // namespace | |
| 49 | |
| 50 namespace extensions { | |
| 51 | |
| 52 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { | |
| 53 } | |
| 54 | |
| 55 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { | |
| 56 } | |
| 57 | |
| 58 bool WebcamPrivateSetFunction::RunImpl() { | |
| 59 // Get parameters | |
| 60 scoped_ptr<api::webcam_private::Set::Params> params( | |
| 61 api::webcam_private::Set::Params::Create(*args_)); | |
| 62 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 63 | |
| 64 base::ScopedFD fd = | |
| 65 OpenWebcam(extension_id(), browser_context(), params->webcam_id); | |
| 66 if (!fd.is_valid()) { | |
| 67 SetError(kUnknownWebcam); | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 if (params->config.pan) { | |
| 72 SetWebcamParameter(fd.get(), V4L2_CID_PAN_ABSOLUTE, | |
| 73 *(params->config.pan)); | |
| 74 } | |
| 75 | |
| 76 if (params->config.tilt) { | |
| 77 SetWebcamParameter(fd.get(), V4L2_CID_TILT_ABSOLUTE, | |
| 78 *(params->config.tilt)); | |
| 79 } | |
| 80 | |
| 81 if (params->config.zoom) { | |
| 82 SetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE, | |
| 83 *(params->config.zoom)); | |
| 84 } | |
| 85 | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { | |
| 90 } | |
| 91 | |
| 92 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { | |
| 93 } | |
| 94 | |
| 95 bool WebcamPrivateGetFunction::RunImpl() { | |
| 96 // Get parameters | |
| 97 scoped_ptr<api::webcam_private::Get::Params> params( | |
| 98 api::webcam_private::Get::Params::Create(*args_)); | |
| 99 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 100 | |
| 101 base::ScopedFD fd = | |
| 102 OpenWebcam(extension_id(), browser_context(), params->webcam_id); | |
| 103 if (!fd.is_valid()) { | |
| 104 SetError(kUnknownWebcam); | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 api::webcam_private::WebcamConfiguration result; | |
| 109 | |
| 110 int pan; | |
| 111 if (GetWebcamParameter(fd.get(), V4L2_CID_PAN_ABSOLUTE, &pan)) | |
| 112 result.pan.reset(new double(pan)); | |
| 113 | |
| 114 int tilt; | |
| 115 if (GetWebcamParameter(fd.get(), V4L2_CID_TILT_ABSOLUTE, &tilt)) | |
| 116 result.tilt.reset(new double(tilt)); | |
| 117 | |
| 118 int zoom; | |
| 119 if (GetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE, &zoom)) | |
| 120 result.zoom.reset(new double(zoom)); | |
| 121 | |
| 122 SetResult(result.ToValue().release()); | |
| 123 | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { | |
| 128 } | |
| 129 | |
| 130 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { | |
| 131 } | |
| 132 | |
| 133 bool WebcamPrivateResetFunction::RunImpl() { | |
| 134 // Get parameters | |
| 135 scoped_ptr<api::webcam_private::Reset::Params> params( | |
| 136 api::webcam_private::Reset::Params::Create(*args_)); | |
| 137 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 138 | |
| 139 base::ScopedFD fd = | |
| 140 OpenWebcam(extension_id(), browser_context(), params->webcam_id); | |
| 141 if (!fd.is_valid()) { | |
| 142 SetError(kUnknownWebcam); | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 if (params->config.pan) { | |
| 147 struct v4l2_control v4l2_ctrl = {V4L2_CID_PAN_RESET}; | |
| 148 HANDLE_EINTR(ioctl(fd.get(), VIDIOC_S_CTRL, &v4l2_ctrl)); | |
| 149 } | |
| 150 | |
| 151 if (params->config.tilt) { | |
| 152 struct v4l2_control v4l2_ctrl = {V4L2_CID_TILT_RESET}; | |
| 153 HANDLE_EINTR(ioctl(fd.get(), VIDIOC_S_CTRL, &v4l2_ctrl)); | |
| 154 } | |
| 155 | |
| 156 if (params->config.zoom) { | |
| 157 const int kDefaultZoom = 100; | |
| 158 SetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE, kDefaultZoom); | |
| 159 } | |
| 160 | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 } // namespace extensions | |
| OLD | NEW |