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 | |
7 #include "chrome/common/extensions/api/webcam_private.h" | 13 #include "chrome/common/extensions/api/webcam_private.h" |
8 | 14 |
15 namespace content { | |
16 class BrowserContext; | |
17 } // namespace content | |
18 | |
19 namespace { | |
20 | |
21 enum WebcamParameter { | |
22 WEBCAM_PAN, | |
23 WEBCAM_TILT, | |
24 WEBCAM_ZOOM, | |
25 }; | |
26 | |
27 const int kDefaultZoom = 100; | |
28 | |
29 int OpenWebcam(const std::string& extension_id, | |
30 content::BrowserContext* browser_context, | |
31 const std::string& webcam_id) { | |
32 // TODO(zork): Get device_id from content::MediaStreamManager | |
33 std::string device_id = "/dev/video0"; | |
34 | |
35 return open(device_id.c_str(), 0); | |
36 } | |
37 | |
38 void SetWebcamParameter(int fd, WebcamParameter param, int value) { | |
39 struct v4l2_control v4l2_ctrl; | |
40 switch (param) { | |
41 case WEBCAM_PAN: | |
42 v4l2_ctrl.id = V4L2_CID_PAN_ABSOLUTE; | |
43 break; | |
44 | |
45 case WEBCAM_TILT: | |
46 v4l2_ctrl.id = V4L2_CID_TILT_ABSOLUTE; | |
47 break; | |
48 | |
49 case WEBCAM_ZOOM: | |
50 v4l2_ctrl.id = V4L2_CID_ZOOM_ABSOLUTE; | |
51 break; | |
52 } | |
53 | |
54 v4l2_ctrl.value = value; | |
55 ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl); | |
56 } | |
57 | |
58 bool GetWebcamParameter(int fd, WebcamParameter param, int* value) { | |
59 struct v4l2_control v4l2_ctrl; | |
60 switch (param) { | |
61 case WEBCAM_PAN: | |
62 v4l2_ctrl.id = V4L2_CID_PAN_ABSOLUTE; | |
63 break; | |
64 case WEBCAM_TILT: | |
65 v4l2_ctrl.id = V4L2_CID_TILT_ABSOLUTE; | |
66 break; | |
67 case WEBCAM_ZOOM: | |
68 v4l2_ctrl.id = V4L2_CID_ZOOM_ABSOLUTE; | |
69 break; | |
70 } | |
71 | |
72 if (ioctl(fd, VIDIOC_G_CTRL, &v4l2_ctrl)) | |
73 return false; | |
74 | |
75 *value = v4l2_ctrl.value; | |
76 return true; | |
77 } | |
78 | |
79 void ResetWebcamParameter(int fd, WebcamParameter param) { | |
80 switch (param) { | |
81 case WEBCAM_PAN: { | |
82 struct v4l2_control v4l2_ctrl; | |
83 v4l2_ctrl.id = V4L2_CID_PAN_RESET; | |
84 ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl); | |
85 } | |
86 break; | |
87 | |
88 case WEBCAM_TILT: { | |
89 struct v4l2_control v4l2_ctrl; | |
90 v4l2_ctrl.id = V4L2_CID_TILT_RESET; | |
91 ioctl(fd, VIDIOC_S_CTRL, &v4l2_ctrl); | |
92 } | |
93 break; | |
94 | |
95 case WEBCAM_ZOOM: | |
96 SetWebcamParameter(fd, WEBCAM_ZOOM, kDefaultZoom); | |
97 break; | |
98 } | |
99 | |
100 } | |
101 | |
102 const char kUnknownWebcam[] = "Unknown webcam id"; | |
103 } // namespace | |
104 | |
9 namespace extensions { | 105 namespace extensions { |
10 | 106 |
11 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { | 107 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { |
12 } | 108 } |
13 | 109 |
14 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { | 110 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { |
15 } | 111 } |
16 | 112 |
17 bool WebcamPrivateSetFunction::RunImpl() { | 113 bool WebcamPrivateSetFunction::RunImpl() { |
18 // Get parameters | 114 // Get parameters |
19 scoped_ptr<api::webcam_private::Set::Params> params( | 115 scoped_ptr<api::webcam_private::Set::Params> params( |
20 api::webcam_private::Set::Params::Create(*args_)); | 116 api::webcam_private::Set::Params::Create(*args_)); |
21 EXTENSION_FUNCTION_VALIDATE(params.get()); | 117 EXTENSION_FUNCTION_VALIDATE(params.get()); |
22 | 118 |
23 // TODO(zork): Send the value to the webcam. | 119 int fd = OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
120 if (fd < 0) { | |
121 SetError(kUnknownWebcam); | |
122 return false; | |
123 } | |
124 | |
125 if (params->config.pan) | |
126 SetWebcamParameter(fd, WEBCAM_PAN, *(params->config.pan)); | |
127 | |
128 if (params->config.tilt) | |
129 SetWebcamParameter(fd, WEBCAM_TILT, *(params->config.tilt)); | |
130 | |
131 if (params->config.zoom) | |
132 SetWebcamParameter(fd, WEBCAM_ZOOM, *(params->config.zoom)); | |
133 | |
134 close(fd); | |
24 | 135 |
25 return true; | 136 return true; |
26 } | 137 } |
27 | 138 |
28 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { | 139 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { |
29 } | 140 } |
30 | 141 |
31 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { | 142 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { |
32 } | 143 } |
33 | 144 |
34 bool WebcamPrivateGetFunction::RunImpl() { | 145 bool WebcamPrivateGetFunction::RunImpl() { |
35 // Get parameters | 146 // Get parameters |
36 scoped_ptr<api::webcam_private::Get::Params> params( | 147 scoped_ptr<api::webcam_private::Get::Params> params( |
37 api::webcam_private::Get::Params::Create(*args_)); | 148 api::webcam_private::Get::Params::Create(*args_)); |
38 EXTENSION_FUNCTION_VALIDATE(params.get()); | 149 EXTENSION_FUNCTION_VALIDATE(params.get()); |
39 | 150 |
40 // TODO(zork): Get the value from the webcam. | 151 int fd = OpenWebcam(extension_id(), browser_context(), params->webcam_id); |
152 if (fd < 0) { | |
153 SetError(kUnknownWebcam); | |
154 return false; | |
155 } | |
41 | 156 |
42 return false; | 157 api::webcam_private::WebcamConfiguration result; |
158 | |
159 int pan; | |
160 if (GetWebcamParameter(fd, WEBCAM_PAN, &pan)) | |
161 result.pan.reset(new double(pan)); | |
162 | |
163 int tilt; | |
164 if (GetWebcamParameter(fd, WEBCAM_TILT, &tilt)) | |
165 result.tilt.reset(new double(tilt)); | |
166 | |
167 int zoom; | |
168 if (GetWebcamParameter(fd, WEBCAM_ZOOM, &zoom)) | |
169 result.zoom.reset(new double(zoom)); | |
170 | |
171 close(fd); | |
172 | |
173 SetResult(result.ToValue().release()); | |
174 | |
175 return true; | |
43 } | 176 } |
44 | 177 |
45 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { | 178 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
46 } | 179 } |
47 | 180 |
48 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { | 181 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { |
49 } | 182 } |
50 | 183 |
51 bool WebcamPrivateResetFunction::RunImpl() { | 184 bool WebcamPrivateResetFunction::RunImpl() { |
52 // Get parameters | 185 // Get parameters |
53 scoped_ptr<api::webcam_private::Reset::Params> params( | 186 scoped_ptr<api::webcam_private::Reset::Params> params( |
54 api::webcam_private::Reset::Params::Create(*args_)); | 187 api::webcam_private::Reset::Params::Create(*args_)); |
55 EXTENSION_FUNCTION_VALIDATE(params.get()); | 188 EXTENSION_FUNCTION_VALIDATE(params.get()); |
56 | 189 |
57 // TODO(zork): Reset the webcam state. | 190 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.
| |
191 if (fd < 0) { | |
192 SetError(kUnknownWebcam); | |
193 return false; | |
194 } | |
195 | |
196 if (params->config.pan) | |
197 ResetWebcamParameter(fd, WEBCAM_PAN); | |
198 | |
199 if (params->config.tilt) | |
200 ResetWebcamParameter(fd, WEBCAM_TILT); | |
201 | |
202 if (params->config.zoom) | |
203 ResetWebcamParameter(fd, WEBCAM_ZOOM); | |
204 | |
205 close(fd); | |
58 | 206 |
59 return true; | 207 return true; |
60 } | 208 } |
61 | 209 |
62 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |