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_facing_chromeos.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 namespace { | |
| 18 | |
| 19 bool GetCameraId(const base::StringPiece& sub_key, int* camera_id) { | |
| 20 const base::StringPiece camera_id_prefix = "camera"; | |
| 21 if (!sub_key.starts_with(camera_id_prefix)) | |
| 22 return false; | |
| 23 return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 // /etc/camera/camera_characteristics.conf contains camera information which | |
| 28 // driver cannot provide. | |
| 29 static const char kCameraCharacteristicsConfigFile[] = | |
| 30 "/etc/camera/camera_characteristics.conf"; | |
| 31 static const char kLensFacing[] = "lens_facing"; | |
| 32 static const char kUsbVidPid[] = "usb_vid_pid"; | |
| 33 static const char kUsbPath[] = "usb_path"; | |
| 34 | |
| 35 CameraFacingChromeOS::CameraFacingChromeOS() { | |
| 36 InitializeDeviceInfo(kCameraCharacteristicsConfigFile); | |
| 37 } | |
| 38 | |
| 39 CameraFacingChromeOS::CameraFacingChromeOS(const char* config_file_path) { | |
| 40 InitializeDeviceInfo(config_file_path); | |
| 41 } | |
| 42 | |
| 43 CameraFacingChromeOS::~CameraFacingChromeOS() {} | |
| 44 | |
| 45 CameraFacingChromeOS::LensFacing CameraFacingChromeOS::GetCameraFacing( | |
| 46 const std::string& device_id, | |
| 47 const std::string& model_id) const { | |
| 48 std::string usb_id = GetUsbId(device_id); | |
| 49 const auto& usb_id_to_camera_id_const = usb_id_to_camera_id_; | |
| 50 const auto& model_id_to_camera_id_const = model_id_to_camera_id_; | |
| 51 const auto& camera_id_to_facing_const = camera_id_to_facing_; | |
| 52 auto usb_id_iter = usb_id_to_camera_id_const.find(usb_id); | |
| 53 int camera_id; | |
| 54 if (usb_id_iter == usb_id_to_camera_id_const.end()) { | |
| 55 // Can't find Usb ID. Fall back to use model_id. | |
| 56 auto model_id_iter = model_id_to_camera_id_const.find(model_id); | |
| 57 if (model_id_iter == model_id_to_camera_id_const.end()) { | |
| 58 DLOG(ERROR) << "Can't find model ID in config file: " << model_id; | |
| 59 return kLensFacingDefault; | |
| 60 } | |
| 61 camera_id = model_id_iter->second; | |
| 62 } else { | |
| 63 camera_id = usb_id_iter->second; | |
| 64 } | |
| 65 | |
| 66 auto camera_id_iter = camera_id_to_facing_const.find(camera_id); | |
| 67 if (camera_id_iter == camera_id_to_facing_const.end()) { | |
| 68 DLOG(ERROR) << "Can't find lens_facing of camera ID " << camera_id | |
| 69 << " in config file"; | |
| 70 return kLensFacingDefault; | |
| 71 } | |
| 72 return camera_id_iter->second; | |
| 73 } | |
| 74 | |
| 75 std::string CameraFacingChromeOS::GetUsbId(const std::string& device_id) const { | |
| 76 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" | |
| 77 // into |file_name|. | |
| 78 const std::string device_dir = "/dev/"; | |
| 79 DCHECK(base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)); | |
| 80 const std::string file_name = device_id.substr(device_dir.length()); | |
| 81 | |
| 82 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". | |
| 83 const std::string symlink = | |
| 84 base::StringPrintf("/sys/class/video4linux/%s/device", file_name.c_str()); | |
| 85 base::FilePath symlinkTarget; | |
| 86 if (!base::ReadSymbolicLink(base::FilePath(symlink), &symlinkTarget)) { | |
| 87 DPLOG(ERROR) << "Failed to readlink: " << symlink; | |
| 88 return std::string(); | |
| 89 } | |
| 90 | |
| 91 // |symlinkTarget| is of the format "../../../A-B:C.D". Remove the path | |
| 92 // prefix. | |
| 93 base::StringPiece usb_part = symlinkTarget.BaseName().value(); | |
| 94 | |
| 95 // |usb_part| is of the format "A-B:C.D" or "A-B.C:D". We want everything | |
| 96 // before ":". | |
| 97 std::vector<base::StringPiece> usb_id_pieces = base::SplitStringPiece( | |
| 98 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 99 base::SplitResult::SPLIT_WANT_ALL); | |
| 100 | |
| 101 if (usb_id_pieces.empty()) { | |
| 102 DLOG(ERROR) << "Error after split: " << usb_part; | |
| 103 return std::string(); | |
| 104 } | |
| 105 return usb_id_pieces[0].as_string(); | |
| 106 } | |
| 107 | |
| 108 void CameraFacingChromeOS::InitializeDeviceInfo(const char* config_file_path) { | |
| 109 const base::FilePath path(config_file_path); | |
| 110 std::string content; | |
| 111 if (!base::ReadFileToString(path, &content)) { | |
| 112 DPLOG(ERROR) << "ReadFileToString fails"; | |
| 113 return; | |
| 114 } | |
| 115 std::vector<base::StringPiece> lines = base::SplitStringPiece( | |
|
mcasas
2016/12/03 03:37:11
nit: const
shenghao
2016/12/05 11:17:19
Done.
| |
| 116 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 117 base::SplitResult::SPLIT_WANT_NONEMPTY); | |
| 118 | |
| 119 for (base::StringPiece line : lines) { | |
|
mcasas
2016/12/03 03:37:11
const base::StringPiece& line
shenghao
2016/12/05 11:17:19
Done.
| |
| 120 if (line.starts_with("#")) { | |
| 121 // Ignore the comments that starts with "#". | |
| 122 continue; | |
| 123 } | |
|
mcasas
2016/12/03 03:37:11
micro nit:
if (line.starts_with("#")) // Ignore c
shenghao
2016/12/05 11:17:20
Done.
| |
| 124 std::vector<base::StringPiece> key_value = base::SplitStringPiece( | |
|
mcasas
2016/12/03 03:37:11
const ? Also in l.133, etc. Essentially, try to
shenghao
2016/12/05 11:17:19
Done.
| |
| 125 line, "=", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 126 base::SplitResult::SPLIT_WANT_ALL); | |
| 127 if (key_value.size() != 2) { | |
| 128 DLOG(ERROR) << "Invalid line in config file: " << line; | |
| 129 continue; | |
| 130 } | |
| 131 const auto& key = key_value[0]; | |
| 132 const auto& value = key_value[1]; | |
| 133 std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( | |
| 134 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, | |
| 135 base::SplitResult::SPLIT_WANT_ALL); | |
| 136 | |
| 137 if (sub_keys.size() < 1) { | |
| 138 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: " | |
| 139 << line; | |
| 140 continue; | |
| 141 } | |
| 142 int camera_id = 0; | |
| 143 if (!GetCameraId(sub_keys[0], &camera_id)) { | |
| 144 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | |
| 145 continue; | |
| 146 } | |
| 147 | |
| 148 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { | |
| 149 int lens_facing = -1; | |
| 150 if (!base::StringToInt(value, &lens_facing)) { | |
| 151 DLOG(ERROR) << "Invalid value for lens_facing: " << value; | |
| 152 continue; | |
| 153 } | |
| 154 switch (lens_facing) { | |
| 155 case LensFacing::FRONT: | |
| 156 camera_id_to_facing_[camera_id] = LensFacing::FRONT; | |
| 157 break; | |
| 158 case LensFacing::BACK: | |
| 159 camera_id_to_facing_[camera_id] = LensFacing::BACK; | |
| 160 break; | |
| 161 default: | |
| 162 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing; | |
| 163 continue; | |
| 164 } | |
| 165 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { | |
| 166 if (value.empty()) { | |
| 167 DLOG(ERROR) << "model_id is empty"; | |
| 168 continue; | |
| 169 } | |
| 170 model_id_to_camera_id_[value.as_string()] = camera_id; | |
| 171 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { | |
| 172 if (value.empty()) { | |
| 173 DLOG(ERROR) << "usb_path is empty"; | |
| 174 continue; | |
| 175 } | |
| 176 usb_id_to_camera_id_[value.as_string()] = camera_id; | |
| 177 } | |
| 178 // Ignore unknown or unutilized attributes. | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 } // namespace media | |
| OLD | NEW |