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..c56fd2249edda6691be902f8b130a8b016b8858d |
| --- /dev/null |
| +++ b/media/capture/video/linux/camera_characteristics.cc |
| @@ -0,0 +1,162 @@ |
| +// 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> |
| +#include <base/strings/string_util.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"; |
| + |
| +CameraCharacteristics::CameraCharacteristics() {} |
| + |
| +CameraCharacteristics::~CameraCharacteristics() {} |
| + |
| +int CameraCharacteristics::GetCameraFacing(const std::string& device_id, |
| + const std::string& model_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; |
|
kcwu
2016/11/24 15:48:52
"Can't find facing data in config file for camera:
shenghao
2016/11/28 08:52:04
Done.
|
| + 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(base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)); |
| + const std::string file_name = device_id.substr(device_dir.length()); |
| + |
| + // 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()); |
| + base::FilePath symlinkTarget; |
| + if (!base::ReadSymbolicLink(base::FilePath(symlink), &symlinkTarget)) { |
| + LOG(ERROR) << "Failed to readlink: " << symlink; |
| + return std::string(); |
| + } |
| + |
| + // |symlinkTarget| is of the format "../../../A-B:C.D". Remove the path |
| + // prefix. |
| + base::StringPiece usb_part = symlinkTarget.BaseName().value(); |
| + |
| + // |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); |
| + |
| + if (usb_id_pieces.empty()) { |
| + LOG(ERROR) << "Error after split: " << usb_part; |
| + return std::string(); |
| + } |
| + return usb_id_pieces[0].as_string(); |
| +} |
| + |
| +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() { |
| + const base::FilePath path(kCameraCharacteristicsConfigFile); |
| + std::string content; |
| + if (!base::ReadFileToString(path, &content)) { |
| + DVLOG(1) << "ReadFileToString fails"; |
| + 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("#")) { |
| + // 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)) { |
|
kcwu
2016/11/24 15:48:52
Please refactor to avoid duplicated code.
shenghao
2016/11/28 08:52:04
Done.
|
| + 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; |
| + 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; |
| + 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.as_string()] = camera_id; |
| + } |
| + } |
| +} |
| + |
| +} // namespace media |