Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "camera_facing_chromeos.h" | 5 #include "camera_config_chromeos.h" |
| 6 | 6 |
| 7 #include <base/files/file_util.h> | 7 #include <base/files/file_util.h> |
| 8 #include <base/logging.h> | 8 #include <base/logging.h> |
| 9 #include <base/strings/stringprintf.h> | 9 #include <base/strings/stringprintf.h> |
| 10 #include <base/strings/string_number_conversions.h> | 10 #include <base/strings/string_number_conversions.h> |
| 11 #include <base/strings/string_piece.h> | 11 #include <base/strings/string_piece.h> |
| 12 #include <base/strings/string_split.h> | 12 #include <base/strings/string_split.h> |
| 13 #include <base/strings/string_util.h> | 13 #include <base/strings/string_util.h> |
| 14 | 14 |
| 15 namespace media { | 15 namespace media { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The value for each field in the enum matches the value in | 19 // The value for each field in the enum matches the value in |
| 20 // /etc/camera/camera_characteristics.conf. | 20 // /etc/camera/camera_characteristics.conf. |
| 21 enum LensFacing { FRONT = 0, BACK = 1 }; | 21 enum LensFacing { FRONT = 0, BACK = 1 }; |
| 22 | 22 |
| 23 bool GetCameraId(const base::StringPiece& sub_key, int* camera_id) { | 23 bool ParseCameraId(const base::StringPiece& sub_key, int* camera_id) { |
| 24 const base::StringPiece camera_id_prefix = "camera"; | 24 const base::StringPiece camera_id_prefix = "camera"; |
| 25 if (!sub_key.starts_with(camera_id_prefix)) | 25 if (!sub_key.starts_with(camera_id_prefix)) |
| 26 return false; | 26 return false; |
| 27 return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id); | 27 return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id); |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 // /etc/camera/camera_characteristics.conf contains camera information which | 31 // /etc/camera/camera_characteristics.conf contains camera information which |
| 32 // driver cannot provide. | 32 // driver cannot provide. |
| 33 static const char kCameraCharacteristicsConfigFile[] = | 33 static const char kCameraCharacteristicsConfigFile[] = |
| 34 "/etc/camera/camera_characteristics.conf"; | 34 "/etc/camera/camera_characteristics.conf"; |
| 35 static const char kLensFacing[] = "lens_facing"; | 35 static const char kLensFacing[] = "lens_facing"; |
| 36 static const char kSensorOrientation[] = "sensor_orientation"; | |
| 36 static const char kUsbVidPid[] = "usb_vid_pid"; | 37 static const char kUsbVidPid[] = "usb_vid_pid"; |
| 37 static const char kUsbPath[] = "usb_path"; | 38 static const char kUsbPath[] = "usb_path"; |
| 39 static const int kOrientationDefault = 0; | |
| 40 static const int kCameraIdNotFound = -1; | |
| 38 | 41 |
| 39 CameraFacingChromeOS::CameraFacingChromeOS() { | 42 CameraConfigChromeOS::CameraConfigChromeOS() { |
| 40 InitializeDeviceInfo(std::string(kCameraCharacteristicsConfigFile)); | 43 InitializeDeviceInfo(std::string(kCameraCharacteristicsConfigFile)); |
| 41 } | 44 } |
| 42 | 45 |
| 43 CameraFacingChromeOS::CameraFacingChromeOS( | 46 CameraConfigChromeOS::CameraConfigChromeOS( |
| 44 const std::string& config_file_path) { | 47 const std::string& config_file_path) { |
| 45 InitializeDeviceInfo(config_file_path); | 48 InitializeDeviceInfo(config_file_path); |
| 46 } | 49 } |
| 47 | 50 |
| 48 CameraFacingChromeOS::~CameraFacingChromeOS() {} | 51 CameraConfigChromeOS::~CameraConfigChromeOS() {} |
| 49 | 52 |
| 50 VideoFacingMode CameraFacingChromeOS::GetCameraFacing( | 53 VideoFacingMode CameraConfigChromeOS::GetCameraFacing( |
| 51 const std::string& device_id, | 54 const std::string& device_id, |
| 52 const std::string& model_id) const { | 55 const std::string& model_id) const { |
| 53 std::string usb_id = GetUsbId(device_id); | 56 int camera_id = GetCameraId(device_id, model_id); |
| 54 const auto& usb_id_to_camera_id_const = usb_id_to_camera_id_; | |
| 55 const auto& model_id_to_camera_id_const = model_id_to_camera_id_; | |
| 56 const auto& camera_id_to_facing_const = camera_id_to_facing_; | 57 const auto& camera_id_to_facing_const = camera_id_to_facing_; |
| 57 auto usb_id_iter = usb_id_to_camera_id_const.find(usb_id); | |
| 58 int camera_id; | |
| 59 if (usb_id_iter == usb_id_to_camera_id_const.end()) { | |
| 60 // Can't find Usb ID. Fall back to use model_id. | |
| 61 auto model_id_iter = model_id_to_camera_id_const.find(model_id); | |
| 62 if (model_id_iter == model_id_to_camera_id_const.end()) { | |
| 63 DLOG(ERROR) << "Can't find model ID in config file: " << model_id; | |
| 64 return kLensFacingDefault; | |
| 65 } | |
| 66 camera_id = model_id_iter->second; | |
| 67 } else { | |
| 68 camera_id = usb_id_iter->second; | |
| 69 } | |
| 70 | |
| 71 auto camera_id_iter = camera_id_to_facing_const.find(camera_id); | 58 auto camera_id_iter = camera_id_to_facing_const.find(camera_id); |
| 72 if (camera_id_iter == camera_id_to_facing_const.end()) { | 59 if (camera_id_iter == camera_id_to_facing_const.end()) { |
| 73 DLOG(ERROR) << "Can't find lens_facing of camera ID " << camera_id | 60 DLOG(ERROR) << "Can't find lens_facing of camera ID " << camera_id |
| 74 << " in config file"; | 61 << " in config file"; |
| 75 return kLensFacingDefault; | 62 return kLensFacingDefault; |
| 76 } | 63 } |
| 77 return camera_id_iter->second; | 64 return camera_id_iter->second; |
| 78 } | 65 } |
| 79 | 66 |
| 80 std::string CameraFacingChromeOS::GetUsbId(const std::string& device_id) const { | 67 int CameraConfigChromeOS::GetOrientation(const std::string& device_id, |
| 68 const std::string& model_id) const { | |
| 69 int camera_id = GetCameraId(device_id, model_id); | |
| 70 const auto& camera_id_to_orientation_const = camera_id_to_orientation_; | |
| 71 auto camera_id_iter = camera_id_to_orientation_const.find(camera_id); | |
|
mcasas
2017/01/23 21:44:57
You shouldn't encode the type of the variable
or i
shenghao
2017/01/24 14:29:52
Done.
| |
| 72 if (camera_id_iter == camera_id_to_orientation_const.end()) { | |
| 73 DLOG(ERROR) << "Can't find sensor_orientation of camera ID " << camera_id | |
| 74 << " in config file"; | |
| 75 return kOrientationDefault; | |
| 76 } | |
| 77 return camera_id_iter->second; | |
| 78 } | |
| 79 | |
| 80 int CameraConfigChromeOS::GetCameraId(const std::string& device_id, | |
| 81 const std::string& model_id) const { | |
| 82 std::string usb_id = GetUsbId(device_id); | |
| 83 const auto& usb_id_to_camera_id_const = usb_id_to_camera_id_; | |
| 84 const auto& model_id_to_camera_id_const = model_id_to_camera_id_; | |
| 85 auto usb_id_iter = usb_id_to_camera_id_const.find(usb_id); | |
| 86 int camera_id = 0; | |
| 87 if (usb_id_iter == usb_id_to_camera_id_const.end()) { | |
| 88 // Can't find Usb ID. Fall back to use model_id. | |
|
mcasas
2017/01/23 21:44:57
s/model_id/|model_id|/
shenghao
2017/01/24 14:29:52
Done.
| |
| 89 auto model_id_iter = model_id_to_camera_id_const.find(model_id); | |
| 90 if (model_id_iter == model_id_to_camera_id_const.end()) { | |
| 91 DLOG(ERROR) << "Can't find model ID in config file: " << model_id; | |
| 92 return kCameraIdNotFound; | |
| 93 } | |
| 94 camera_id = model_id_iter->second; | |
| 95 } else { | |
| 96 camera_id = usb_id_iter->second; | |
| 97 } | |
| 98 return camera_id; | |
|
mcasas
2017/01/23 21:44:57
The code in this and GetOrientation() methods is h
shenghao
2017/01/24 14:29:52
Done, except that I have to keep the const tempora
| |
| 99 } | |
| 100 | |
| 101 std::string CameraConfigChromeOS::GetUsbId(const std::string& device_id) const { | |
| 81 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" | 102 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" |
| 82 // into |file_name|. | 103 // into |file_name|. |
| 83 const std::string device_dir = "/dev/"; | 104 const std::string device_dir = "/dev/"; |
| 84 if (!base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)) { | 105 if (!base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)) { |
| 85 DLOG(ERROR) << "device_id is invalid: " << device_id; | 106 DLOG(ERROR) << "device_id is invalid: " << device_id; |
| 86 return std::string(); | 107 return std::string(); |
| 87 } | 108 } |
| 88 const std::string file_name = device_id.substr(device_dir.length()); | 109 const std::string file_name = device_id.substr(device_dir.length()); |
| 89 | 110 |
| 90 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". | 111 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 106 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, | 127 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 107 base::SplitResult::SPLIT_WANT_ALL); | 128 base::SplitResult::SPLIT_WANT_ALL); |
| 108 | 129 |
| 109 if (usb_id_pieces.empty()) { | 130 if (usb_id_pieces.empty()) { |
| 110 DLOG(ERROR) << "Error after split: " << usb_part; | 131 DLOG(ERROR) << "Error after split: " << usb_part; |
| 111 return std::string(); | 132 return std::string(); |
| 112 } | 133 } |
| 113 return usb_id_pieces[0].as_string(); | 134 return usb_id_pieces[0].as_string(); |
| 114 } | 135 } |
| 115 | 136 |
| 116 void CameraFacingChromeOS::InitializeDeviceInfo( | 137 void CameraConfigChromeOS::InitializeDeviceInfo( |
| 117 const std::string& config_file_path) { | 138 const std::string& config_file_path) { |
| 118 const base::FilePath path(config_file_path); | 139 const base::FilePath path(config_file_path); |
| 119 std::string content; | 140 std::string content; |
| 120 if (!base::ReadFileToString(path, &content)) { | 141 if (!base::ReadFileToString(path, &content)) { |
| 121 DPLOG(ERROR) << "ReadFileToString fails"; | 142 DPLOG(ERROR) << "ReadFileToString fails"; |
| 122 return; | 143 return; |
| 123 } | 144 } |
| 124 const std::vector<base::StringPiece> lines = base::SplitStringPiece( | 145 const std::vector<base::StringPiece> lines = base::SplitStringPiece( |
| 125 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, | 146 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 126 base::SplitResult::SPLIT_WANT_NONEMPTY); | 147 base::SplitResult::SPLIT_WANT_NONEMPTY); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 140 const std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( | 161 const std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( |
| 141 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, | 162 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 142 base::SplitResult::SPLIT_WANT_ALL); | 163 base::SplitResult::SPLIT_WANT_ALL); |
| 143 | 164 |
| 144 if (sub_keys.size() < 1) { | 165 if (sub_keys.size() < 1) { |
| 145 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: " | 166 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: " |
| 146 << line; | 167 << line; |
| 147 continue; | 168 continue; |
| 148 } | 169 } |
| 149 int camera_id = 0; | 170 int camera_id = 0; |
| 150 if (!GetCameraId(sub_keys[0], &camera_id)) { | 171 if (!ParseCameraId(sub_keys[0], &camera_id)) { |
| 151 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | 172 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; |
| 152 continue; | 173 continue; |
| 153 } | 174 } |
| 154 | 175 |
| 155 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { | 176 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { |
| 156 int lens_facing = -1; | 177 int lens_facing = -1; |
| 157 if (!base::StringToInt(value, &lens_facing)) { | 178 if (!base::StringToInt(value, &lens_facing)) { |
| 158 DLOG(ERROR) << "Invalid value for lens_facing: " << value; | 179 DLOG(ERROR) << "Invalid value for lens_facing: " << value; |
| 159 continue; | 180 continue; |
| 160 } | 181 } |
| 161 switch (lens_facing) { | 182 switch (lens_facing) { |
| 162 case LensFacing::FRONT: | 183 case LensFacing::FRONT: |
| 163 camera_id_to_facing_[camera_id] = | 184 camera_id_to_facing_[camera_id] = |
| 164 VideoFacingMode::MEDIA_VIDEO_FACING_USER; | 185 VideoFacingMode::MEDIA_VIDEO_FACING_USER; |
| 165 break; | 186 break; |
| 166 case LensFacing::BACK: | 187 case LensFacing::BACK: |
| 167 camera_id_to_facing_[camera_id] = | 188 camera_id_to_facing_[camera_id] = |
| 168 VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT; | 189 VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT; |
| 169 break; | 190 break; |
| 170 default: | 191 default: |
| 171 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing; | 192 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing; |
| 172 continue; | 193 continue; |
| 173 } | 194 } |
| 195 } else if (sub_keys.size() == 2 && sub_keys[1] == kSensorOrientation) { | |
| 196 int orientation = 0; | |
| 197 if (!base::StringToInt(value, &orientation)) { | |
| 198 DLOG(ERROR) << "Invalid value for sensor_orientation: " << value; | |
| 199 continue; | |
| 200 } | |
| 201 camera_id_to_orientation_[camera_id] = orientation; | |
| 174 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { | 202 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { |
| 175 if (value.empty()) { | 203 if (value.empty()) { |
| 176 DLOG(ERROR) << "model_id is empty"; | 204 DLOG(ERROR) << "model_id is empty"; |
| 177 continue; | 205 continue; |
| 178 } | 206 } |
| 179 std::string model_id = value.as_string(); | 207 std::string model_id = value.as_string(); |
| 180 std::transform(model_id.begin(), model_id.end(), model_id.begin(), | 208 std::transform(model_id.begin(), model_id.end(), model_id.begin(), |
| 181 ::tolower); | 209 ::tolower); |
| 182 model_id_to_camera_id_[model_id] = camera_id; | 210 model_id_to_camera_id_[model_id] = camera_id; |
| 183 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { | 211 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { |
| 184 if (value.empty()) { | 212 if (value.empty()) { |
| 185 DLOG(ERROR) << "usb_path is empty"; | 213 DLOG(ERROR) << "usb_path is empty"; |
| 186 continue; | 214 continue; |
| 187 } | 215 } |
| 188 usb_id_to_camera_id_[value.as_string()] = camera_id; | 216 usb_id_to_camera_id_[value.as_string()] = camera_id; |
| 189 } | 217 } |
| 190 // Ignore unknown or unutilized attributes. | 218 // Ignore unknown or unutilized attributes. |
| 191 } | 219 } |
| 192 } | 220 } |
| 193 | 221 |
| 194 } // namespace media | 222 } // namespace media |
| OLD | NEW |