Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "camera_characteristics.h" | |
| 6 | |
| 7 #include <base/files/file_util.h> | |
| 8 #include <base/logging.h> | |
| 9 #include <base/strings/stringprintf.h> | |
| 10 #include <base/strings/string_number_conversions.h> | |
| 11 #include <base/strings/string_piece.h> | |
| 12 #include <base/strings/string_split.h> | |
| 13 #include <base/strings/string_util.h> | |
| 14 | |
| 15 namespace media { | |
| 16 | |
| 17 // /etc/camera/camera_characteristics.conf contains camera information which | |
| 18 // driver cannot provide. | |
| 19 static const char kCameraCharacteristicsConfigFile[] = | |
| 20 "/etc/camera/camera_characteristics.conf"; | |
| 21 static const char kLensFacing[] = "lens_facing"; | |
| 22 static const char kUsbVidPid[] = "usb_vid_pid"; | |
| 23 static const char kUsbPath[] = "usb_path"; | |
| 24 | |
| 25 CameraCharacteristics::CameraCharacteristics() {} | |
| 26 | |
| 27 CameraCharacteristics::~CameraCharacteristics() {} | |
| 28 | |
| 29 int CameraCharacteristics::GetCameraFacing(const std::string& device_id, | |
| 30 const std::string& model_id) { | |
| 31 std::string usb_id = GetUsbId(device_id); | |
| 32 auto usb_id_iter = usb_id_to_camera_id_.find(usb_id); | |
| 33 int camera_id; | |
| 34 if (usb_id_iter == usb_id_to_camera_id_.end()) { | |
| 35 // Can't find Usb ID. Fall back to use model_id. | |
| 36 auto model_id_iter = model_id_to_camera_id_.find(model_id); | |
| 37 if (model_id_iter == model_id_to_camera_id_.end()) { | |
| 38 LOG(ERROR) << "Can't find model ID in config file: " << model_id; | |
| 39 return CameraDeviceInfo::LENS_FACING_FRONT; | |
| 40 } | |
| 41 camera_id = model_id_iter->second; | |
| 42 } else { | |
| 43 camera_id = usb_id_iter->second; | |
| 44 } | |
| 45 | |
| 46 auto camera_id_iter = camera_id_to_facing_.find(camera_id); | |
| 47 if (camera_id_iter == camera_id_to_facing_.end()) { | |
| 48 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.
| |
| 49 return CameraDeviceInfo::LENS_FACING_FRONT; | |
| 50 } | |
| 51 return camera_id_iter->second; | |
| 52 } | |
| 53 | |
| 54 std::string CameraCharacteristics::GetUsbId(const std::string& device_id) { | |
| 55 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" | |
| 56 // into |file_name|. | |
| 57 const std::string device_dir = "/dev/"; | |
| 58 DCHECK(base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)); | |
| 59 const std::string file_name = device_id.substr(device_dir.length()); | |
| 60 | |
| 61 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". | |
| 62 const std::string symlink = | |
| 63 base::StringPrintf("/sys/class/video4linux/%s/device", file_name.c_str()); | |
| 64 base::FilePath symlinkTarget; | |
| 65 if (!base::ReadSymbolicLink(base::FilePath(symlink), &symlinkTarget)) { | |
| 66 LOG(ERROR) << "Failed to readlink: " << symlink; | |
| 67 return std::string(); | |
| 68 } | |
| 69 | |
| 70 // |symlinkTarget| is of the format "../../../A-B:C.D". Remove the path | |
| 71 // prefix. | |
| 72 base::StringPiece usb_part = symlinkTarget.BaseName().value(); | |
| 73 | |
| 74 // |usb_part| is of the format "A-B:C.D" or "A-B.C:D". We want everything | |
| 75 // before ":". | |
| 76 std::vector<base::StringPiece> usb_id_pieces = base::SplitStringPiece( | |
| 77 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 78 base::SplitResult::SPLIT_WANT_ALL); | |
| 79 | |
| 80 if (usb_id_pieces.empty()) { | |
| 81 LOG(ERROR) << "Error after split: " << usb_part; | |
| 82 return std::string(); | |
| 83 } | |
| 84 return usb_id_pieces[0].as_string(); | |
| 85 } | |
| 86 | |
| 87 bool CameraCharacteristics::GetCameraId(const base::StringPiece& sub_key, | |
| 88 int* camera_id) { | |
| 89 const base::StringPiece camera_id_prefix = "camera"; | |
| 90 if (!sub_key.starts_with(camera_id_prefix)) { | |
| 91 return false; | |
| 92 } | |
| 93 return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id); | |
| 94 } | |
| 95 | |
| 96 void CameraCharacteristics::InitializeDeviceInfo() { | |
| 97 const base::FilePath path(kCameraCharacteristicsConfigFile); | |
| 98 std::string content; | |
| 99 if (!base::ReadFileToString(path, &content)) { | |
| 100 DVLOG(1) << "ReadFileToString fails"; | |
| 101 return; | |
| 102 } | |
| 103 std::vector<base::StringPiece> lines = base::SplitStringPiece( | |
| 104 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 105 base::SplitResult::SPLIT_WANT_NONEMPTY); | |
| 106 | |
| 107 for (base::StringPiece line : lines) { | |
| 108 if (line.starts_with("#")) { | |
| 109 // Ignore the comments that starts with "#". | |
| 110 continue; | |
| 111 } | |
| 112 std::vector<base::StringPiece> key_value = base::SplitStringPiece( | |
| 113 line, "=", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 114 base::SplitResult::SPLIT_WANT_ALL); | |
| 115 if (key_value.size() != 2) { | |
| 116 LOG(ERROR) << "Invalid line in config file: " << line; | |
| 117 continue; | |
| 118 } | |
| 119 const auto& key = key_value[0]; | |
| 120 const auto& value = key_value[1]; | |
| 121 std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( | |
| 122 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 123 base::SplitResult::SPLIT_WANT_ALL); | |
| 124 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { | |
| 125 int lens_facing = CameraDeviceInfo::LENS_FACING_FRONT; | |
| 126 if (!base::StringToInt(value, &lens_facing)) { | |
| 127 LOG(ERROR) << "Invalid value for lens_facing: " << value; | |
| 128 continue; | |
| 129 } | |
| 130 int camera_id = 0; | |
| 131 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.
| |
| 132 LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | |
| 133 continue; | |
| 134 } | |
| 135 camera_id_to_facing_[camera_id] = lens_facing; | |
| 136 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { | |
| 137 int camera_id = 0; | |
| 138 if (!GetCameraId(sub_keys[0], &camera_id)) { | |
| 139 LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | |
| 140 continue; | |
| 141 } | |
| 142 if (value.empty()) { | |
| 143 LOG(ERROR) << "model_id is empty"; | |
| 144 continue; | |
| 145 } | |
| 146 model_id_to_camera_id_[value.as_string()] = camera_id; | |
| 147 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { | |
| 148 int camera_id = 0; | |
| 149 if (!GetCameraId(sub_keys[0], &camera_id)) { | |
| 150 LOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | |
| 151 continue; | |
| 152 } | |
| 153 if (value.empty()) { | |
| 154 LOG(ERROR) << "usb_path is empty"; | |
| 155 continue; | |
| 156 } | |
| 157 usb_id_to_camera_id_[value.as_string()] = camera_id; | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 } // namespace media | |
| OLD | NEW |