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/capture/video/linux/video_capture_device_linux.h" | 5 #include "media/capture/video/linux/video_capture_device_linux.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/strings/stringprintf.h" | |
13 #include "build/build_config.h" | 12 #include "build/build_config.h" |
14 #include "media/capture/video/linux/v4l2_capture_delegate.h" | 13 #include "media/capture/video/linux/v4l2_capture_delegate.h" |
15 | 14 |
16 #if defined(OS_OPENBSD) | 15 #if defined(OS_OPENBSD) |
17 #include <sys/videoio.h> | 16 #include <sys/videoio.h> |
18 #else | 17 #else |
19 #include <linux/videodev2.h> | 18 #include <linux/videodev2.h> |
20 #endif | 19 #endif |
21 | 20 |
22 namespace media { | 21 namespace media { |
23 | 22 |
24 // USB VID and PID are both 4 bytes long. | |
25 static const size_t kVidPidSize = 4; | |
26 | |
27 // /sys/class/video4linux/video{N}/device is a symlink to the corresponding | |
28 // USB device info directory. | |
29 static const char kVidPathTemplate[] = | |
30 "/sys/class/video4linux/%s/device/../idVendor"; | |
31 static const char kPidPathTemplate[] = | |
32 "/sys/class/video4linux/%s/device/../idProduct"; | |
33 | |
34 static bool ReadIdFile(const std::string& path, std::string* id) { | |
35 char id_buf[kVidPidSize]; | |
36 FILE* file = fopen(path.c_str(), "rb"); | |
37 if (!file) | |
38 return false; | |
39 const bool success = fread(id_buf, kVidPidSize, 1, file) == 1; | |
40 fclose(file); | |
41 if (!success) | |
42 return false; | |
43 id->append(id_buf, kVidPidSize); | |
44 return true; | |
45 } | |
46 | |
47 // Translates Video4Linux pixel formats to Chromium pixel formats. | 23 // Translates Video4Linux pixel formats to Chromium pixel formats. |
48 // static | 24 // static |
49 VideoPixelFormat VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat( | 25 VideoPixelFormat VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat( |
50 uint32_t v4l2_fourcc) { | 26 uint32_t v4l2_fourcc) { |
51 return V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(v4l2_fourcc); | 27 return V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(v4l2_fourcc); |
52 } | 28 } |
53 | 29 |
54 // Gets a list of usable Four CC formats prioritized. | 30 // Gets a list of usable Four CC formats prioritized. |
55 // static | 31 // static |
56 std::list<uint32_t> VideoCaptureDeviceLinux::GetListOfUsableFourCCs( | 32 std::list<uint32_t> VideoCaptureDeviceLinux::GetListOfUsableFourCCs( |
57 bool favour_mjpeg) { | 33 bool favour_mjpeg) { |
58 return V4L2CaptureDelegate::GetListOfUsableFourCcs(favour_mjpeg); | 34 return V4L2CaptureDelegate::GetListOfUsableFourCcs(favour_mjpeg); |
59 } | 35 } |
60 | 36 |
61 const std::string VideoCaptureDevice::Name::GetModel() const { | 37 VideoCaptureDeviceLinux::VideoCaptureDeviceLinux( |
62 // |unique_id| is of the form "/dev/video2". |file_name| is "video2". | 38 const VideoCaptureDeviceDescriptor& device_descriptor) |
63 const std::string dev_dir = "/dev/"; | 39 : v4l2_thread_("V4L2CaptureThread"), |
64 DCHECK_EQ(0, unique_id_.compare(0, dev_dir.length(), dev_dir)); | 40 device_descriptor_(device_descriptor) {} |
65 const std::string file_name = | |
66 unique_id_.substr(dev_dir.length(), unique_id_.length()); | |
67 | |
68 const std::string vidPath = | |
69 base::StringPrintf(kVidPathTemplate, file_name.c_str()); | |
70 const std::string pidPath = | |
71 base::StringPrintf(kPidPathTemplate, file_name.c_str()); | |
72 | |
73 std::string usb_id; | |
74 if (!ReadIdFile(vidPath, &usb_id)) | |
75 return ""; | |
76 usb_id.append(":"); | |
77 if (!ReadIdFile(pidPath, &usb_id)) | |
78 return ""; | |
79 | |
80 return usb_id; | |
81 } | |
82 | |
83 VideoCaptureDeviceLinux::VideoCaptureDeviceLinux(const Name& device_name) | |
84 : v4l2_thread_("V4L2CaptureThread"), device_name_(device_name) {} | |
85 | 41 |
86 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() { | 42 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() { |
87 // Check if the thread is running. | 43 // Check if the thread is running. |
88 // This means that the device has not been StopAndDeAllocate()d properly. | 44 // This means that the device has not been StopAndDeAllocate()d properly. |
89 DCHECK(!v4l2_thread_.IsRunning()); | 45 DCHECK(!v4l2_thread_.IsRunning()); |
90 v4l2_thread_.Stop(); | 46 v4l2_thread_.Stop(); |
91 } | 47 } |
92 | 48 |
93 void VideoCaptureDeviceLinux::AllocateAndStart( | 49 void VideoCaptureDeviceLinux::AllocateAndStart( |
94 const VideoCaptureParams& params, | 50 const VideoCaptureParams& params, |
95 std::unique_ptr<VideoCaptureDevice::Client> client) { | 51 std::unique_ptr<VideoCaptureDevice::Client> client) { |
96 DCHECK(!capture_impl_); | 52 DCHECK(!capture_impl_); |
97 if (v4l2_thread_.IsRunning()) | 53 if (v4l2_thread_.IsRunning()) |
98 return; // Wrong state. | 54 return; // Wrong state. |
99 v4l2_thread_.Start(); | 55 v4l2_thread_.Start(); |
100 | 56 |
101 const int line_frequency = | 57 const int line_frequency = |
102 TranslatePowerLineFrequencyToV4L2(GetPowerLineFrequency(params)); | 58 TranslatePowerLineFrequencyToV4L2(GetPowerLineFrequency(params)); |
103 capture_impl_ = new V4L2CaptureDelegate( | 59 capture_impl_ = new V4L2CaptureDelegate( |
104 device_name_, v4l2_thread_.task_runner(), line_frequency); | 60 device_descriptor_, v4l2_thread_.task_runner(), line_frequency); |
105 if (!capture_impl_) { | 61 if (!capture_impl_) { |
106 client->OnError(FROM_HERE, "Failed to create VideoCaptureDelegate"); | 62 client->OnError(FROM_HERE, "Failed to create VideoCaptureDelegate"); |
107 return; | 63 return; |
108 } | 64 } |
109 v4l2_thread_.message_loop()->PostTask( | 65 v4l2_thread_.message_loop()->PostTask( |
110 FROM_HERE, | 66 FROM_HERE, |
111 base::Bind(&V4L2CaptureDelegate::AllocateAndStart, capture_impl_, | 67 base::Bind(&V4L2CaptureDelegate::AllocateAndStart, capture_impl_, |
112 params.requested_format.frame_size.width(), | 68 params.requested_format.frame_size.width(), |
113 params.requested_format.frame_size.height(), | 69 params.requested_format.frame_size.height(), |
114 params.requested_format.frame_rate, base::Passed(&client))); | 70 params.requested_format.frame_rate, base::Passed(&client))); |
(...skipping 26 matching lines...) Expand all Loading... |
141 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; | 97 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; |
142 case media::PowerLineFrequency::FREQUENCY_60HZ: | 98 case media::PowerLineFrequency::FREQUENCY_60HZ: |
143 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; | 99 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; |
144 default: | 100 default: |
145 // If we have no idea of the frequency, at least try and set it to AUTO. | 101 // If we have no idea of the frequency, at least try and set it to AUTO. |
146 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; | 102 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; |
147 } | 103 } |
148 } | 104 } |
149 | 105 |
150 } // namespace media | 106 } // namespace media |
OLD | NEW |