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