Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/browser/extensions/api/webcam_private/webcam_private_api.cc

Issue 234843006: Send commands from webcamPrivate API to the webcam via V4L2 ioctls (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comment Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
tommi (sloooow) - chröme 2014/04/23 14:40:00 nit: 2 spaces before // (here and throughout)
Zachary Kuznia 2014/04/24 00:22:13 Done.
20
21 namespace {
22
23 base::ScopedFD OpenWebcam(const std::string& extension_id,
tommi (sloooow) - chröme 2014/04/23 14:40:00 this seems like linux specific code. is it suppos
Zachary Kuznia 2014/04/24 00:22:13 Changed to webcam_private_api_chromeos.cc
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
tommi (sloooow) - chröme 2014/04/23 14:40:00 only one empty line
Zachary Kuznia 2014/04/24 00:22:13 Done.
47
48 const char kUnknownWebcam[] = "Unknown webcam id";
49 } // namespace
50
9 namespace extensions { 51 namespace extensions {
10 52
11 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { 53 WebcamPrivateSetFunction::WebcamPrivateSetFunction() {
12 } 54 }
13 55
14 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { 56 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() {
15 } 57 }
16 58
17 bool WebcamPrivateSetFunction::RunImpl() { 59 bool WebcamPrivateSetFunction::RunImpl() {
18 // Get parameters 60 // Get parameters
19 scoped_ptr<api::webcam_private::Set::Params> params( 61 scoped_ptr<api::webcam_private::Set::Params> params(
20 api::webcam_private::Set::Params::Create(*args_)); 62 api::webcam_private::Set::Params::Create(*args_));
21 EXTENSION_FUNCTION_VALIDATE(params.get()); 63 EXTENSION_FUNCTION_VALIDATE(params.get());
22 64
23 // TODO(zork): Send the value to the webcam. 65 base::ScopedFD fd =
66 OpenWebcam(extension_id(), browser_context(), params->webcam_id);
67 if (!fd.is_valid()) {
68 SetError(kUnknownWebcam);
69 return false;
70 }
71
72 if (params->config.pan) {
73 SetWebcamParameter(fd.get(), V4L2_CID_PAN_ABSOLUTE,
74 *(params->config.pan));
75 }
76
77 if (params->config.tilt) {
78 SetWebcamParameter(fd.get(), V4L2_CID_TILT_ABSOLUTE,
79 *(params->config.tilt));
80 }
81
82 if (params->config.zoom) {
83 SetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE,
84 *(params->config.zoom));
85 }
24 86
25 return true; 87 return true;
26 } 88 }
27 89
28 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { 90 WebcamPrivateGetFunction::WebcamPrivateGetFunction() {
29 } 91 }
30 92
31 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { 93 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() {
32 } 94 }
33 95
34 bool WebcamPrivateGetFunction::RunImpl() { 96 bool WebcamPrivateGetFunction::RunImpl() {
35 // Get parameters 97 // Get parameters
36 scoped_ptr<api::webcam_private::Get::Params> params( 98 scoped_ptr<api::webcam_private::Get::Params> params(
37 api::webcam_private::Get::Params::Create(*args_)); 99 api::webcam_private::Get::Params::Create(*args_));
38 EXTENSION_FUNCTION_VALIDATE(params.get()); 100 EXTENSION_FUNCTION_VALIDATE(params.get());
39 101
40 // TODO(zork): Get the value from the webcam. 102 base::ScopedFD fd =
103 OpenWebcam(extension_id(), browser_context(), params->webcam_id);
104 if (!fd.is_valid()) {
105 SetError(kUnknownWebcam);
106 return false;
107 }
41 108
42 return false; 109 api::webcam_private::WebcamConfiguration result;
110
111 int pan;
112 if (GetWebcamParameter(fd.get(), V4L2_CID_PAN_ABSOLUTE, &pan))
113 result.pan.reset(new double(pan));
114
115 int tilt;
116 if (GetWebcamParameter(fd.get(), V4L2_CID_TILT_ABSOLUTE, &tilt))
117 result.tilt.reset(new double(tilt));
118
119 int zoom;
120 if (GetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE, &zoom))
121 result.zoom.reset(new double(zoom));
122
123 SetResult(result.ToValue().release());
124
125 return true;
43 } 126 }
44 127
45 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { 128 WebcamPrivateResetFunction::WebcamPrivateResetFunction() {
46 } 129 }
47 130
48 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { 131 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() {
49 } 132 }
50 133
51 bool WebcamPrivateResetFunction::RunImpl() { 134 bool WebcamPrivateResetFunction::RunImpl() {
52 // Get parameters 135 // Get parameters
53 scoped_ptr<api::webcam_private::Reset::Params> params( 136 scoped_ptr<api::webcam_private::Reset::Params> params(
54 api::webcam_private::Reset::Params::Create(*args_)); 137 api::webcam_private::Reset::Params::Create(*args_));
55 EXTENSION_FUNCTION_VALIDATE(params.get()); 138 EXTENSION_FUNCTION_VALIDATE(params.get());
56 139
57 // TODO(zork): Reset the webcam state. 140 base::ScopedFD fd =
141 OpenWebcam(extension_id(), browser_context(), params->webcam_id);
142 if (!fd.is_valid()) {
143 SetError(kUnknownWebcam);
144 return false;
145 }
146
147 if (params->config.pan) {
148 struct v4l2_control v4l2_ctrl = {V4L2_CID_PAN_RESET};
149 HANDLE_EINTR(ioctl(fd.get(), VIDIOC_S_CTRL, &v4l2_ctrl));
150 }
151
152 if (params->config.tilt) {
153 struct v4l2_control v4l2_ctrl = {V4L2_CID_TILT_RESET};
154 HANDLE_EINTR(ioctl(fd.get(), VIDIOC_S_CTRL, &v4l2_ctrl));
155 }
156
157 if (params->config.zoom) {
158 const int kDefaultZoom = 100;
159 SetWebcamParameter(fd.get(), V4L2_CID_ZOOM_ABSOLUTE, kDefaultZoom);
160 }
58 161
59 return true; 162 return true;
60 } 163 }
61 164
62 } // namespace extensions 165 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698