| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return false; | 96 return false; |
| 96 } | 97 } |
| 97 | 98 |
| 98 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 99 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
| 99 int fd = -1; | 100 int fd = -1; |
| 100 | 101 |
| 101 // Empty the name list. | 102 // Empty the name list. |
| 102 device_names->clear(); | 103 device_names->clear(); |
| 103 | 104 |
| 104 base::FilePath path("/dev/"); | 105 base::FilePath path("/dev/"); |
| 105 file_util::FileEnumerator enumerator( | 106 base::FileEnumerator enumerator( |
| 106 path, false, file_util::FileEnumerator::FILES, "video*"); | 107 path, false, base::FileEnumerator::FILES, "video*"); |
| 107 | 108 |
| 108 while (!enumerator.Next().empty()) { | 109 while (!enumerator.Next().empty()) { |
| 109 file_util::FileEnumerator::FindInfo info; | 110 base::FileEnumerator::FileInfo info = enumerator.GetInfo(); |
| 110 enumerator.GetFindInfo(&info); | |
| 111 | 111 |
| 112 Name name; | 112 Name name; |
| 113 name.unique_id = path.value() + info.filename; | 113 name.unique_id = path.value() + info.GetName().value(); |
| 114 if ((fd = open(name.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. | 115 // Failed to open this device. |
| 116 continue; | 116 continue; |
| 117 } | 117 } |
| 118 // Test if this is a V4L2 capture device. | 118 // Test if this is a V4L2 capture device. |
| 119 v4l2_capability cap; | 119 v4l2_capability cap; |
| 120 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) && | 120 if ((ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) && |
| 121 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && | 121 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && |
| 122 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { | 122 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) { |
| 123 // This is a V4L2 video capture device | 123 // This is a V4L2 video capture device |
| 124 if (HasUsableFormats(fd)) { | 124 if (HasUsableFormats(fd)) { |
| 125 name.device_name = base::StringPrintf("%s", cap.card); | 125 name.device_name = base::StringPrintf("%s", cap.card); |
| 126 device_names->push_back(name); | 126 device_names->push_back(name); |
| 127 } else { | 127 } else { |
| 128 DVLOG(1) << "No usable formats reported by " << info.filename; | 128 DVLOG(1) << "No usable formats reported by " << info.GetName().value(); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 close(fd); | 131 close(fd); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | 135 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { |
| 136 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); | 136 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); |
| 137 if (!self) | 137 if (!self) |
| 138 return NULL; | 138 return NULL; |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 buffer_pool_size_ = 0; | 533 buffer_pool_size_ = 0; |
| 534 } | 534 } |
| 535 | 535 |
| 536 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) { | 536 void VideoCaptureDeviceLinux::SetErrorState(const std::string& reason) { |
| 537 DVLOG(1) << reason; | 537 DVLOG(1) << reason; |
| 538 state_ = kError; | 538 state_ = kError; |
| 539 observer_->OnError(); | 539 observer_->OnError(); |
| 540 } | 540 } |
| 541 | 541 |
| 542 } // namespace media | 542 } // namespace media |
| OLD | NEW |