| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/video/linux/video_capture_device_linux.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <list> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "media/capture/video/linux/v4l2_capture_delegate.h" | |
| 15 | |
| 16 #if defined(OS_OPENBSD) | |
| 17 #include <sys/videoio.h> | |
| 18 #else | |
| 19 #include <linux/videodev2.h> | |
| 20 #endif | |
| 21 | |
| 22 namespace media { | |
| 23 | |
| 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. | |
| 48 // static | |
| 49 VideoPixelFormat VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat( | |
| 50 uint32_t v4l2_fourcc) { | |
| 51 return V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(v4l2_fourcc); | |
| 52 } | |
| 53 | |
| 54 // Gets a list of usable Four CC formats prioritized. | |
| 55 // static | |
| 56 std::list<uint32_t> VideoCaptureDeviceLinux::GetListOfUsableFourCCs( | |
| 57 bool favour_mjpeg) { | |
| 58 return V4L2CaptureDelegate::GetListOfUsableFourCcs(favour_mjpeg); | |
| 59 } | |
| 60 | |
| 61 const std::string VideoCaptureDevice::Name::GetModel() const { | |
| 62 // |unique_id| is of the form "/dev/video2". |file_name| is "video2". | |
| 63 const std::string dev_dir = "/dev/"; | |
| 64 DCHECK_EQ(0, unique_id_.compare(0, dev_dir.length(), dev_dir)); | |
| 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 | |
| 86 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() { | |
| 87 // Check if the thread is running. | |
| 88 // This means that the device has not been StopAndDeAllocate()d properly. | |
| 89 DCHECK(!v4l2_thread_.IsRunning()); | |
| 90 v4l2_thread_.Stop(); | |
| 91 } | |
| 92 | |
| 93 void VideoCaptureDeviceLinux::AllocateAndStart( | |
| 94 const VideoCaptureParams& params, | |
| 95 std::unique_ptr<VideoCaptureDevice::Client> client) { | |
| 96 DCHECK(!capture_impl_); | |
| 97 if (v4l2_thread_.IsRunning()) | |
| 98 return; // Wrong state. | |
| 99 v4l2_thread_.Start(); | |
| 100 | |
| 101 const int line_frequency = | |
| 102 TranslatePowerLineFrequencyToV4L2(GetPowerLineFrequency(params)); | |
| 103 capture_impl_ = new V4L2CaptureDelegate( | |
| 104 device_name_, v4l2_thread_.task_runner(), line_frequency); | |
| 105 if (!capture_impl_) { | |
| 106 client->OnError(FROM_HERE, "Failed to create VideoCaptureDelegate"); | |
| 107 return; | |
| 108 } | |
| 109 v4l2_thread_.message_loop()->PostTask( | |
| 110 FROM_HERE, | |
| 111 base::Bind(&V4L2CaptureDelegate::AllocateAndStart, capture_impl_, | |
| 112 params.requested_format.frame_size.width(), | |
| 113 params.requested_format.frame_size.height(), | |
| 114 params.requested_format.frame_rate, base::Passed(&client))); | |
| 115 } | |
| 116 | |
| 117 void VideoCaptureDeviceLinux::StopAndDeAllocate() { | |
| 118 if (!v4l2_thread_.IsRunning()) | |
| 119 return; // Wrong state. | |
| 120 v4l2_thread_.message_loop()->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(&V4L2CaptureDelegate::StopAndDeAllocate, capture_impl_)); | |
| 123 v4l2_thread_.Stop(); | |
| 124 | |
| 125 capture_impl_ = NULL; | |
| 126 } | |
| 127 | |
| 128 void VideoCaptureDeviceLinux::SetRotation(int rotation) { | |
| 129 if (v4l2_thread_.IsRunning()) { | |
| 130 v4l2_thread_.message_loop()->PostTask( | |
| 131 FROM_HERE, | |
| 132 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation)); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2( | |
| 138 PowerLineFrequency frequency) { | |
| 139 switch (frequency) { | |
| 140 case media::PowerLineFrequency::FREQUENCY_50HZ: | |
| 141 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; | |
| 142 case media::PowerLineFrequency::FREQUENCY_60HZ: | |
| 143 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; | |
| 144 default: | |
| 145 // If we have no idea of the frequency, at least try and set it to AUTO. | |
| 146 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 } // namespace media | |
| OLD | NEW |