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