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