| OLD | NEW |
| 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> |
| 11 #else | 11 #else |
| 12 #include <linux/videodev2.h> | 12 #include <linux/videodev2.h> |
| 13 #endif | 13 #endif |
| 14 #include <sys/ioctl.h> | 14 #include <sys/ioctl.h> |
| 15 #include <sys/mman.h> | 15 #include <sys/mman.h> |
| 16 | 16 |
| 17 #include <list> | 17 #include <list> |
| 18 #include <string> | 18 #include <string> |
| 19 | 19 |
| 20 #include "base/bind.h" | 20 #include "base/bind.h" |
| 21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
| 22 #include "base/files/file_enumerator.h" |
| 22 #include "base/stringprintf.h" | 23 #include "base/stringprintf.h" |
| 23 | 24 |
| 24 namespace media { | 25 namespace media { |
| 25 | 26 |
| 26 // Max number of video buffers VideoCaptureDeviceLinux can allocate. | 27 // Max number of video buffers VideoCaptureDeviceLinux can allocate. |
| 27 enum { kMaxVideoBuffers = 2 }; | 28 enum { kMaxVideoBuffers = 2 }; |
| 28 // Timeout in microseconds v4l2_thread_ blocks waiting for a frame from the hw. | 29 // Timeout in microseconds v4l2_thread_ blocks waiting for a frame from the hw. |
| 29 enum { kCaptureTimeoutUs = 200000 }; | 30 enum { kCaptureTimeoutUs = 200000 }; |
| 30 // The number of continuous timeouts tolerated before treated as error. | 31 // The number of continuous timeouts tolerated before treated as error. |
| 31 enum { kContinuousTimeoutLimit = 10 }; | 32 enum { kContinuousTimeoutLimit = 10 }; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 return false; | 94 return false; |
| 94 } | 95 } |
| 95 | 96 |
| 96 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 97 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
| 97 int fd = -1; | 98 int fd = -1; |
| 98 | 99 |
| 99 // Empty the name list. | 100 // Empty the name list. |
| 100 device_names->clear(); | 101 device_names->clear(); |
| 101 | 102 |
| 102 base::FilePath path("/dev/"); | 103 base::FilePath path("/dev/"); |
| 103 file_util::FileEnumerator enumerator( | 104 base::FileEnumerator enumerator( |
| 104 path, false, file_util::FileEnumerator::FILES, "video*"); | 105 path, false, base::FileEnumerator::FILES, "video*"); |
| 105 | 106 |
| 106 while (!enumerator.Next().empty()) { | 107 while (!enumerator.Next().empty()) { |
| 107 file_util::FileEnumerator::FindInfo info; | 108 base::FileEnumerator::FileInfo info = enumerator.GetInfo(); |
| 108 enumerator.GetFindInfo(&info); | |
| 109 | 109 |
| 110 Name name; | 110 Name name; |
| 111 name.unique_id = path.value() + info.filename; | 111 name.unique_id = path.value() + info.GetName().value(); |
| 112 if ((fd = open(name.unique_id.c_str() , O_RDONLY)) < 0) { | 112 if ((fd = open(name.unique_id.c_str() , O_RDONLY)) < 0) { |
| 113 // Failed to open this device. | 113 // Failed to open this device. |
| 114 continue; | 114 continue; |
| 115 } | 115 } |
| 116 // Test if this is a V4L2 capture device. | 116 // Test if this is a V4L2 capture device. |
| 117 v4l2_capability cap; | 117 v4l2_capability cap; |
| 118 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) && | 118 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) && |
| 119 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && | 119 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && |
| 120 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { | 120 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { |
| 121 // This is a V4L2 video capture device | 121 // This is a V4L2 video capture device |
| 122 if (HasUsableFormats(fd)) { | 122 if (HasUsableFormats(fd)) { |
| 123 name.device_name = base::StringPrintf("%s", cap.card); | 123 name.device_name = base::StringPrintf("%s", cap.card); |
| 124 device_names->push_back(name); | 124 device_names->push_back(name); |
| 125 } else { | 125 } else { |
| 126 DVLOG(1) << "No usable formats reported by " << info.filename; | 126 DVLOG(1) << "No usable formats reported by " << info.GetName().value(); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 close(fd); | 129 close(fd); |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | 133 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { |
| 134 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); | 134 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); |
| 135 if (!self) | 135 if (!self) |
| 136 return NULL; | 136 return NULL; |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 buffer_pool_size_ = 0; | 507 buffer_pool_size_ = 0; |
| 508 } | 508 } |
| 509 | 509 |
| 510 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) { | 510 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) { |
| 511 DVLOG(1) << reason; | 511 DVLOG(1) << reason; |
| 512 state_ = kError; | 512 state_ = kError; |
| 513 observer_->OnError(); | 513 observer_->OnError(); |
| 514 } | 514 } |
| 515 | 515 |
| 516 } // namespace media | 516 } // namespace media |
| OLD | NEW |