Chromium Code Reviews| Index: media/capture/video/linux/camera_characteristics.cc |
| diff --git a/media/capture/video/linux/camera_characteristics.cc b/media/capture/video/linux/camera_characteristics.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cecd3a34eb17c41598f3520f67f1a92609413f96 |
| --- /dev/null |
| +++ b/media/capture/video/linux/camera_characteristics.cc |
| @@ -0,0 +1,164 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "camera_characteristics.h" |
| + |
| +#include <base/files/file_util.h> |
| +#include <base/logging.h> |
| +#include <base/strings/stringprintf.h> |
| +#include <base/strings/string_number_conversions.h> |
| +#include <base/strings/string_piece.h> |
| +#include <base/strings/string_split.h> |
| + |
| +namespace media { |
| + |
| +// /etc/camera/camera_characteristics.conf contains camera information which |
| +// driver cannot provide. |
| +static const char kCameraCharacteristicsConfigFile[] = |
| + "/etc/camera/camera_characteristics.conf"; |
| +static const char kLensFacing[] = "lens_facing"; |
| +static const char kUsbVidPid[] = "usb_vid_pid"; |
| +static const char kUsbPath[] = "usb_path"; |
| +static const int kUsbIdFullSize = 128; |
| + |
| +CameraCharacteristics::CameraCharacteristics() { |
| + const base::FilePath path(kCameraCharacteristicsConfigFile); |
| + if (!base::ReadFileToString(path, &content_)) { |
|
kcwu
2016/11/24 10:16:08
Don't have non-trivial code in ctor.
shenghao
2016/11/24 14:13:24
Done.
|
| + DVLOG(1) << "ReadFileToString fails"; |
| + content_.erase(); |
| + } else { |
| + InitializeDeviceInfo(); |
| + } |
| +} |
| + |
| +CameraCharacteristics::~CameraCharacteristics() {} |
| + |
| +int CameraCharacteristics::GetCameraFacing(const std::string& model_id, |
| + const std::string& device_id) { |
| + std::string usb_id = GetUsbId(device_id); |
| + auto usb_id_iter = usb_id_to_camera_id_.find(usb_id); |
| + int camera_id; |
| + if (usb_id_iter == usb_id_to_camera_id_.end()) { |
| + // Can't find Usb ID. Fall back to use model_id. |
| + auto model_id_iter = model_id_to_camera_id_.find(model_id); |
| + if (model_id_iter == model_id_to_camera_id_.end()) { |
| + LOG(ERROR) << "Can't find model ID in config file: " << model_id; |
| + return CameraDeviceInfo::LENS_FACING_FRONT; |
| + } |
| + camera_id = model_id_iter->second; |
| + } else { |
| + camera_id = usb_id_iter->second; |
| + } |
| + |
| + auto camera_id_iter = camera_id_to_facing_.find(camera_id); |
| + if (camera_id_iter == camera_id_to_facing_.end()) { |
| + LOG(ERROR) << "Can't find camera ID in config file: " << camera_id; |
| + return CameraDeviceInfo::LENS_FACING_FRONT; |
| + } |
| + return camera_id_iter->second; |
| +} |
| + |
| +std::string CameraCharacteristics::GetUsbId(const std::string& device_id) { |
| + // |device_id| is of the form "/dev/video2". We want to retrieve "video2" |
| + // into |file_name|. |
| + const std::string device_dir = "/dev/"; |
| + DCHECK_EQ(0, device_id.compare(0, device_dir.length(), device_dir)); |
|
kcwu
2016/11/24 10:16:08
DCHECK(base::StartsWith(device_id, device_dir, bas
shenghao
2016/11/24 14:13:24
Done.
|
| + const std::string file_name = |
| + device_id.substr(device_dir.length(), device_id.length()); |
|
kcwu
2016/11/24 10:16:08
device_id.substr(device_dir.length());
the second
shenghao
2016/11/24 14:13:24
Done.
|
| + |
| + // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". |
| + const std::string symlink = |
| + base::StringPrintf("/sys/class/video4linux/%s/device", file_name.c_str()); |
| + char usb_full[kUsbIdFullSize] = {0}; |
| + if (readlink(symlink.c_str(), usb_full, kUsbIdFullSize) == -1) { |
|
kcwu
2016/11/24 10:16:08
base::ReadSymbolicLink() then we can get rid of kU
shenghao
2016/11/24 14:13:24
Done.
|
| + LOG(ERROR) << "Failed to readlink: " << symlink; |
| + return std::string(); |
| + } |
| + |
| + // |usb_full| is of the format "../../../A-B:C.D". Remove the path prefix. |
| + base::StringPiece usb_part = usb_full; |
|
kcwu
2016/11/24 10:16:08
If usb_full is base::FilePath, it has BaseName()
shenghao
2016/11/24 14:13:24
Done.
|
| + usb_part.remove_prefix(usb_part.find_last_of("/") + 1); |
| + |
| + // |usb_part| is of the format "A-B:C.D" or "A-B.C:D". We want everything |
| + // before ":". |
| + std::vector<base::StringPiece> usb_id_pieces = base::SplitStringPiece( |
| + usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, |
| + base::SplitResult::SPLIT_WANT_ALL); |
| + return usb_id_pieces[0].as_string(); |
|
kcwu
2016/11/24 10:16:08
Check usb_id_pieces.size(), otherwise access usb_i
shenghao
2016/11/24 14:13:24
Done.
|
| +} |
| + |
| +bool CameraCharacteristics::GetCameraId(const base::StringPiece& sub_key, |
| + int* camera_id) { |
| + const base::StringPiece camera_id_prefix = "camera"; |
| + if (!sub_key.starts_with(camera_id_prefix)) { |
| + return false; |
| + } |
| + return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id); |
| +} |
| + |
| +void CameraCharacteristics::InitializeDeviceInfo() { |
| + if (content_.empty()) { |
|
kcwu
2016/11/24 10:16:08
No need to keep |content_| in memory after parse.
shenghao
2016/11/24 14:13:24
Done.
|
| + // The config file was not read successfully. Return early. |
| + return; |
| + } |
| + std::vector<base::StringPiece> lines = base::SplitStringPiece( |
| + content_, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, |
| + base::SplitResult::SPLIT_WANT_NONEMPTY); |
| + |
| + for (base::StringPiece line : lines) { |
| + if (line.starts_with(base::StringPiece("#"))) { |
|
kcwu
2016/11/24 10:16:08
line.starts_with("#")
IIUC, the good of StringPiec
shenghao
2016/11/24 14:13:25
Done.
|
| + // Ignore the comments that starts with "#". |
| + continue; |
| + } |
| + std::vector<base::StringPiece> key_value = base::SplitStringPiece( |
| + line, "=", base::WhitespaceHandling::TRIM_WHITESPACE, |
| + base::SplitResult::SPLIT_WANT_ALL); |
| + if (key_value.size() != 2) { |
| + LOG(ERROR) << "Invalid line in config file: " << line; |
| + continue; |
| + } |
| + const auto& key = key_value[0]; |
| + const auto& value = key_value[1]; |
| + std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( |
| + key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, |
| + base::SplitResult::SPLIT_WANT_ALL); |
| + if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { |
| + int lens_facing = CameraDeviceInfo::LENS_FACING_FRONT; |
| + if (!base::StringToInt(value, &lens_facing)) { |
| + LOG(ERROR) << "Invalid value for lens_facing: " << value; |
| + continue; |
| + } |
| + int camera_id = 0; |
| + if (!GetCameraId(sub_keys[0], &camera_id)) { |
| + LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; |
| + continue; |
| + } |
| + camera_id_to_facing_[camera_id] = lens_facing; |
| + } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { |
| + int camera_id = 0; |
|
kcwu
2016/11/24 10:16:08
sub_keys[1] is not used?
shenghao
2016/11/24 14:13:24
Yeah, the module number is not needed for our case
kcwu
2016/11/24 15:48:52
What if the line is
camera0.helloworld.usb_vid_pid
|
| + if (!GetCameraId(sub_keys[0], &camera_id)) { |
| + LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; |
| + continue; |
| + } |
| + if (value.empty()) { |
| + LOG(ERROR) << "model_id is empty"; |
| + continue; |
| + } |
| + model_id_to_camera_id_[value.as_string()] = camera_id; |
| + } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { |
| + int camera_id = 0; |
|
kcwu
2016/11/24 10:16:08
sub_keys[1] is not used?
shenghao
2016/11/24 14:13:24
Yeah, the module number is not needed for our case
|
| + if (!GetCameraId(sub_keys[0], &camera_id)) { |
| + LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; |
| + continue; |
| + } |
| + if (value.empty()) { |
| + LOG(ERROR) << "usb_path is empty"; |
| + continue; |
| + } |
| + usb_id_to_camera_id_[value] = camera_id; |
| + } |
| + } |
| +} |
| + |
| +} // namespace media |