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; | |
| 38 | 40 |
| 39 CameraFacingChromeOS::CameraFacingChromeOS() { | 41 CameraConfigChromeOS::CameraConfigChromeOS() { |
| 40 InitializeDeviceInfo(std::string(kCameraCharacteristicsConfigFile)); | 42 InitializeDeviceInfo(std::string(kCameraCharacteristicsConfigFile)); |
| 41 } | 43 } |
| 42 | 44 |
| 43 CameraFacingChromeOS::CameraFacingChromeOS( | 45 CameraConfigChromeOS::CameraConfigChromeOS( |
| 44 const std::string& config_file_path) { | 46 const std::string& config_file_path) { |
| 45 InitializeDeviceInfo(config_file_path); | 47 InitializeDeviceInfo(config_file_path); |
| 46 } | 48 } |
| 47 | 49 |
| 48 CameraFacingChromeOS::~CameraFacingChromeOS() {} | 50 CameraConfigChromeOS::~CameraConfigChromeOS() {} |
| 49 | 51 |
| 50 VideoFacingMode CameraFacingChromeOS::GetCameraFacing( | 52 VideoFacingMode CameraConfigChromeOS::GetCameraFacing( |
| 51 const std::string& device_id, | 53 const std::string& device_id, |
| 52 const std::string& model_id) const { | 54 const std::string& model_id) const { |
| 55 int camera_id = GetCameraId(device_id, model_id); | |
| 56 const auto& camera_id_to_facing_const = camera_id_to_facing_; | |
| 57 auto camera_id_iter = camera_id_to_facing_const.find(camera_id); | |
| 58 if (camera_id_iter == camera_id_to_facing_const.end()) { | |
| 59 DLOG(ERROR) << "Can't find lens_facing of camera ID " << camera_id | |
| 60 << " in config file"; | |
| 61 return kLensFacingDefault; | |
| 62 } | |
| 63 return camera_id_iter->second; | |
| 64 } | |
| 65 | |
| 66 int CameraConfigChromeOS::GetOrientation(const std::string& device_id, | |
| 67 const std::string& model_id) const { | |
| 68 int camera_id = GetCameraId(device_id, model_id); | |
| 69 const auto& camera_id_to_orientation_const = camera_id_to_orientation_; | |
| 70 auto camera_id_iter = camera_id_to_orientation_const.find(camera_id); | |
| 71 if (camera_id_iter == camera_id_to_orientation_const.end()) { | |
| 72 DLOG(ERROR) << "Can't find sensor_orientation of camera ID " << camera_id | |
| 73 << " in config file"; | |
| 74 return kOrientationDefault; | |
| 75 } | |
| 76 return camera_id_iter->second; | |
| 77 } | |
| 78 | |
| 79 int CameraConfigChromeOS::GetCameraId(const std::string& device_id, | |
| 80 const std::string& model_id) const { | |
| 53 std::string usb_id = GetUsbId(device_id); | 81 std::string usb_id = GetUsbId(device_id); |
| 54 const auto& usb_id_to_camera_id_const = usb_id_to_camera_id_; | 82 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_; | 83 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 auto usb_id_iter = usb_id_to_camera_id_const.find(usb_id); | 84 auto usb_id_iter = usb_id_to_camera_id_const.find(usb_id); |
| 58 int camera_id; | 85 int camera_id = 0; |
| 59 if (usb_id_iter == usb_id_to_camera_id_const.end()) { | 86 if (usb_id_iter == usb_id_to_camera_id_const.end()) { |
| 60 // Can't find Usb ID. Fall back to use model_id. | 87 // 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); | 88 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()) { | 89 if (model_id_iter == model_id_to_camera_id_const.end()) { |
| 63 DLOG(ERROR) << "Can't find model ID in config file: " << model_id; | 90 DLOG(ERROR) << "Can't find model ID in config file: " << model_id; |
| 64 return kLensFacingDefault; | 91 return kLensFacingDefault; |
|
wuchengli
2017/01/23 09:43:27
Use kCameraIdNotFound = -1
shenghao
2017/01/23 12:06:58
Done.
| |
| 65 } | 92 } |
| 66 camera_id = model_id_iter->second; | 93 camera_id = model_id_iter->second; |
| 67 } else { | 94 } else { |
| 68 camera_id = usb_id_iter->second; | 95 camera_id = usb_id_iter->second; |
| 69 } | 96 } |
| 70 | 97 return camera_id; |
| 71 auto camera_id_iter = camera_id_to_facing_const.find(camera_id); | |
| 72 if (camera_id_iter == camera_id_to_facing_const.end()) { | |
| 73 DLOG(ERROR) << "Can't find lens_facing of camera ID " << camera_id | |
| 74 << " in config file"; | |
| 75 return kLensFacingDefault; | |
| 76 } | |
| 77 return camera_id_iter->second; | |
| 78 } | 98 } |
| 79 | 99 |
| 80 std::string CameraFacingChromeOS::GetUsbId(const std::string& device_id) const { | 100 std::string CameraConfigChromeOS::GetUsbId(const std::string& device_id) const { |
| 81 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" | 101 // |device_id| is of the form "/dev/video2". We want to retrieve "video2" |
| 82 // into |file_name|. | 102 // into |file_name|. |
| 83 const std::string device_dir = "/dev/"; | 103 const std::string device_dir = "/dev/"; |
| 84 if (!base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)) { | 104 if (!base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)) { |
| 85 DLOG(ERROR) << "device_id is invalid: " << device_id; | 105 DLOG(ERROR) << "device_id is invalid: " << device_id; |
| 86 return std::string(); | 106 return std::string(); |
| 87 } | 107 } |
| 88 const std::string file_name = device_id.substr(device_dir.length()); | 108 const std::string file_name = device_id.substr(device_dir.length()); |
| 89 | 109 |
| 90 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device". | 110 // 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, | 126 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 107 base::SplitResult::SPLIT_WANT_ALL); | 127 base::SplitResult::SPLIT_WANT_ALL); |
| 108 | 128 |
| 109 if (usb_id_pieces.empty()) { | 129 if (usb_id_pieces.empty()) { |
| 110 DLOG(ERROR) << "Error after split: " << usb_part; | 130 DLOG(ERROR) << "Error after split: " << usb_part; |
| 111 return std::string(); | 131 return std::string(); |
| 112 } | 132 } |
| 113 return usb_id_pieces[0].as_string(); | 133 return usb_id_pieces[0].as_string(); |
| 114 } | 134 } |
| 115 | 135 |
| 116 void CameraFacingChromeOS::InitializeDeviceInfo( | 136 void CameraConfigChromeOS::InitializeDeviceInfo( |
| 117 const std::string& config_file_path) { | 137 const std::string& config_file_path) { |
| 118 const base::FilePath path(config_file_path); | 138 const base::FilePath path(config_file_path); |
| 119 std::string content; | 139 std::string content; |
| 120 if (!base::ReadFileToString(path, &content)) { | 140 if (!base::ReadFileToString(path, &content)) { |
| 121 DPLOG(ERROR) << "ReadFileToString fails"; | 141 DPLOG(ERROR) << "ReadFileToString fails"; |
| 122 return; | 142 return; |
| 123 } | 143 } |
| 124 const std::vector<base::StringPiece> lines = base::SplitStringPiece( | 144 const std::vector<base::StringPiece> lines = base::SplitStringPiece( |
| 125 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, | 145 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 126 base::SplitResult::SPLIT_WANT_NONEMPTY); | 146 base::SplitResult::SPLIT_WANT_NONEMPTY); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 140 const std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( | 160 const std::vector<base::StringPiece> sub_keys = base::SplitStringPiece( |
| 141 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, | 161 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 142 base::SplitResult::SPLIT_WANT_ALL); | 162 base::SplitResult::SPLIT_WANT_ALL); |
| 143 | 163 |
| 144 if (sub_keys.size() < 1) { | 164 if (sub_keys.size() < 1) { |
| 145 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: " | 165 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: " |
| 146 << line; | 166 << line; |
| 147 continue; | 167 continue; |
| 148 } | 168 } |
| 149 int camera_id = 0; | 169 int camera_id = 0; |
| 150 if (!GetCameraId(sub_keys[0], &camera_id)) { | 170 if (!ParseCameraId(sub_keys[0], &camera_id)) { |
| 151 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; | 171 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0]; |
| 152 continue; | 172 continue; |
| 153 } | 173 } |
| 154 | 174 |
| 155 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { | 175 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) { |
| 156 int lens_facing = -1; | 176 int lens_facing = -1; |
| 157 if (!base::StringToInt(value, &lens_facing)) { | 177 if (!base::StringToInt(value, &lens_facing)) { |
| 158 DLOG(ERROR) << "Invalid value for lens_facing: " << value; | 178 DLOG(ERROR) << "Invalid value for lens_facing: " << value; |
| 159 continue; | 179 continue; |
| 160 } | 180 } |
| 161 switch (lens_facing) { | 181 switch (lens_facing) { |
| 162 case LensFacing::FRONT: | 182 case LensFacing::FRONT: |
| 163 camera_id_to_facing_[camera_id] = | 183 camera_id_to_facing_[camera_id] = |
| 164 VideoFacingMode::MEDIA_VIDEO_FACING_USER; | 184 VideoFacingMode::MEDIA_VIDEO_FACING_USER; |
| 165 break; | 185 break; |
| 166 case LensFacing::BACK: | 186 case LensFacing::BACK: |
| 167 camera_id_to_facing_[camera_id] = | 187 camera_id_to_facing_[camera_id] = |
| 168 VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT; | 188 VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT; |
| 169 break; | 189 break; |
| 170 default: | 190 default: |
| 171 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing; | 191 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing; |
| 172 continue; | 192 continue; |
| 173 } | 193 } |
| 194 } else if (sub_keys.size() == 2 && sub_keys[1] == kSensorOrientation) { | |
| 195 int orientation = 0; | |
| 196 if (!base::StringToInt(value, &orientation)) { | |
| 197 DLOG(ERROR) << "Invalid value for sensor_orientation: " << value; | |
| 198 continue; | |
| 199 } | |
| 200 camera_id_to_orientation_[camera_id] = orientation; | |
| 174 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { | 201 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) { |
| 175 if (value.empty()) { | 202 if (value.empty()) { |
| 176 DLOG(ERROR) << "model_id is empty"; | 203 DLOG(ERROR) << "model_id is empty"; |
| 177 continue; | 204 continue; |
| 178 } | 205 } |
| 179 std::string model_id = value.as_string(); | 206 std::string model_id = value.as_string(); |
| 180 std::transform(model_id.begin(), model_id.end(), model_id.begin(), | 207 std::transform(model_id.begin(), model_id.end(), model_id.begin(), |
| 181 ::tolower); | 208 ::tolower); |
| 182 model_id_to_camera_id_[model_id] = camera_id; | 209 model_id_to_camera_id_[model_id] = camera_id; |
| 183 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { | 210 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) { |
| 184 if (value.empty()) { | 211 if (value.empty()) { |
| 185 DLOG(ERROR) << "usb_path is empty"; | 212 DLOG(ERROR) << "usb_path is empty"; |
| 186 continue; | 213 continue; |
| 187 } | 214 } |
| 188 usb_id_to_camera_id_[value.as_string()] = camera_id; | 215 usb_id_to_camera_id_[value.as_string()] = camera_id; |
| 189 } | 216 } |
| 190 // Ignore unknown or unutilized attributes. | 217 // Ignore unknown or unutilized attributes. |
| 191 } | 218 } |
| 192 } | 219 } |
| 193 | 220 |
| 194 } // namespace media | 221 } // namespace media |
| OLD | NEW |