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

Side by Side Diff: media/video/capture/linux/video_capture_device_linux.cc

Issue 17846002: Refactor the VideoCaptureDevice::Name struct. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 5 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/video/capture/linux/video_capture_device_linux.h" 5 #include "media/video/capture/linux/video_capture_device_linux.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #if defined(OS_OPENBSD) 9 #if defined(OS_OPENBSD)
10 #include <sys/videoio.h> 10 #include <sys/videoio.h>
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 // Empty the name list. 102 // Empty the name list.
103 device_names->clear(); 103 device_names->clear();
104 104
105 base::FilePath path("/dev/"); 105 base::FilePath path("/dev/");
106 base::FileEnumerator enumerator( 106 base::FileEnumerator enumerator(
107 path, false, base::FileEnumerator::FILES, "video*"); 107 path, false, base::FileEnumerator::FILES, "video*");
108 108
109 while (!enumerator.Next().empty()) { 109 while (!enumerator.Next().empty()) {
110 base::FileEnumerator::FileInfo info = enumerator.GetInfo(); 110 base::FileEnumerator::FileInfo info = enumerator.GetInfo();
111 111
112 Name name; 112 std::string unique_id = path.value() + info.GetName().value();
113 name.unique_id = path.value() + info.GetName().value(); 113 if ((fd = open(unique_id.c_str() , O_RDONLY)) < 0) {
114 if ((fd = open(name.unique_id.c_str() , O_RDONLY)) < 0) {
115 // Failed to open this device. 114 // Failed to open this device.
116 continue; 115 continue;
117 } 116 }
118 // Test if this is a V4L2 capture device. 117 // Test if this is a V4L2 capture device.
119 v4l2_capability cap; 118 v4l2_capability cap;
120 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) && 119 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) &&
121 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && 120 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
122 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { 121 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
123 // This is a V4L2 video capture device 122 // This is a V4L2 video capture device
124 if (HasUsableFormats(fd)) { 123 if (HasUsableFormats(fd)) {
125 name.device_name = base::StringPrintf("%s", cap.card); 124 Name device_name(base::StringPrintf("%s", cap.card), unique_id);
126 device_names->push_back(name); 125 device_names->push_back(device_name);
127 } else { 126 } else {
128 DVLOG(1) << "No usable formats reported by " << info.GetName().value(); 127 DVLOG(1) << "No usable formats reported by " << info.GetName().value();
129 } 128 }
130 } 129 }
131 close(fd); 130 close(fd);
132 } 131 }
133 } 132 }
134 133
135 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { 134 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) {
136 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); 135 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name);
137 if (!self) 136 if (!self)
138 return NULL; 137 return NULL;
139 // Test opening the device driver. This is to make sure it is available. 138 // Test opening the device driver. This is to make sure it is available.
140 // We will reopen it again in our worker thread when someone 139 // We will reopen it again in our worker thread when someone
141 // allocates the camera. 140 // allocates the camera.
142 int fd = open(device_name.unique_id.c_str(), O_RDONLY); 141 int fd = open(device_name.id().c_str(), O_RDONLY);
143 if (fd < 0) { 142 if (fd < 0) {
144 DVLOG(1) << "Cannot open device"; 143 DVLOG(1) << "Cannot open device";
145 delete self; 144 delete self;
146 return NULL; 145 return NULL;
147 } 146 }
148 close(fd); 147 close(fd);
149 148
150 return self; 149 return self;
151 } 150 }
152 151
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 226
228 void VideoCaptureDeviceLinux::OnAllocate(int width, 227 void VideoCaptureDeviceLinux::OnAllocate(int width,
229 int height, 228 int height,
230 int frame_rate, 229 int frame_rate,
231 EventHandler* observer) { 230 EventHandler* observer) {
232 DCHECK_EQ(v4l2_thread_.message_loop(), base::MessageLoop::current()); 231 DCHECK_EQ(v4l2_thread_.message_loop(), base::MessageLoop::current());
233 232
234 observer_ = observer; 233 observer_ = observer;
235 234
236 // Need to open camera with O_RDWR after Linux kernel 3.3. 235 // Need to open camera with O_RDWR after Linux kernel 3.3.
237 if ((device_fd_ = open(device_name_.unique_id.c_str(), O_RDWR)) < 0) { 236 if ((device_fd_ = open(device_name_.id().c_str(), O_RDWR)) < 0) {
238 SetErrorState("Failed to open V4L2 device driver."); 237 SetErrorState("Failed to open V4L2 device driver.");
239 return; 238 return;
240 } 239 }
241 240
242 // Test if this is a V4L2 capture device. 241 // Test if this is a V4L2 capture device.
243 v4l2_capability cap; 242 v4l2_capability cap;
244 if (!((ioctl(device_fd_, VIDIOC_QUERYCAP, &cap) == 0) && 243 if (!((ioctl(device_fd_, VIDIOC_QUERYCAP, &cap) == 0) &&
245 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && 244 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
246 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT))) { 245 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT))) {
247 // This is not a V4L2 video capture device. 246 // This is not a V4L2 video capture device.
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 buffer_pool_size_ = 0; 532 buffer_pool_size_ = 0;
534 } 533 }
535 534
536 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) { 535 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) {
537 DVLOG(1) << reason; 536 DVLOG(1) << reason;
538 state_ = kError; 537 state_ = kError;
539 observer_->OnError(); 538 observer_->OnError();
540 } 539 }
541 540
542 } // namespace media 541 } // namespace media
OLDNEW
« no previous file with comments | « media/video/capture/fake_video_capture_device.cc ('k') | media/video/capture/mac/video_capture_device_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698