OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/webcam_private/webcam_private_api.h" | 5 #include "chrome/browser/extensions/api/webcam_private/webcam_private_api.h" |
6 | 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" |
7 #include "chrome/common/extensions/api/webcam_private.h" | 15 #include "chrome/common/extensions/api/webcam_private.h" |
8 | 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 |
9 namespace extensions { | 50 namespace extensions { |
10 | 51 |
11 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { | 52 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { |
12 } | 53 } |
13 | 54 |
14 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { | 55 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { |
15 } | 56 } |
16 | 57 |
17 bool WebcamPrivateSetFunction::RunImpl() { | 58 bool WebcamPrivateSetFunction::RunImpl() { |
18 // Get parameters | 59 // Get parameters |
19 scoped_ptr<api::webcam_private::Set::Params> params( | 60 scoped_ptr<api::webcam_private::Set::Params> params( |
20 api::webcam_private::Set::Params::Create(*args_)); | 61 api::webcam_private::Set::Params::Create(*args_)); |
21 EXTENSION_FUNCTION_VALIDATE(params.get()); | 62 EXTENSION_FUNCTION_VALIDATE(params.get()); |
22 | 63 |
23 // TODO(zork): Send the value to the webcam. | 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 } |
24 | 85 |
25 return true; | 86 return true; |
26 } | 87 } |
27 | 88 |
28 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { | 89 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { |
29 } | 90 } |
30 | 91 |
31 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { | 92 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { |
32 } | 93 } |
33 | 94 |
34 bool WebcamPrivateGetFunction::RunImpl() { | 95 bool WebcamPrivateGetFunction::RunImpl() { |
35 // Get parameters | 96 // Get parameters |
36 scoped_ptr<api::webcam_private::Get::Params> params( | 97 scoped_ptr<api::webcam_private::Get::Params> params( |
37 api::webcam_private::Get::Params::Create(*args_)); | 98 api::webcam_private::Get::Params::Create(*args_)); |
38 EXTENSION_FUNCTION_VALIDATE(params.get()); | 99 EXTENSION_FUNCTION_VALIDATE(params.get()); |
39 | 100 |
40 // TODO(zork): Get the value from the webcam. | 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 } |
41 | 107 |
42 return false; | 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; |
43 } | 125 } |
44 | 126 |
45 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { | 127 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
46 } | 128 } |
47 | 129 |
48 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { | 130 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { |
49 } | 131 } |
50 | 132 |
51 bool WebcamPrivateResetFunction::RunImpl() { | 133 bool WebcamPrivateResetFunction::RunImpl() { |
52 // Get parameters | 134 // Get parameters |
53 scoped_ptr<api::webcam_private::Reset::Params> params( | 135 scoped_ptr<api::webcam_private::Reset::Params> params( |
54 api::webcam_private::Reset::Params::Create(*args_)); | 136 api::webcam_private::Reset::Params::Create(*args_)); |
55 EXTENSION_FUNCTION_VALIDATE(params.get()); | 137 EXTENSION_FUNCTION_VALIDATE(params.get()); |
56 | 138 |
57 // TODO(zork): Reset the webcam state. | 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 } |
58 | 160 |
59 return true; | 161 return true; |
60 } | 162 } |
61 | 163 |
62 } // namespace extensions | 164 } // namespace extensions |
OLD | NEW |