Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: media/capture/video/linux/camera_facing_chromeos.cc

Issue 2648743002: Rotate frames according to camera orientation (Closed)
Patch Set: removed {} for one-line blocks Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // The value for each field in the enum matches the value in
20 // /etc/camera/camera_characteristics.conf.
21 enum LensFacing { FRONT = 0, BACK = 1 };
22
23 bool GetCameraId(const base::StringPiece& sub_key, int* camera_id) {
24 const base::StringPiece camera_id_prefix = "camera";
25 if (!sub_key.starts_with(camera_id_prefix))
26 return false;
27 return base::StringToInt(sub_key.substr(camera_id_prefix.size()), camera_id);
28 }
29 }
30
31 // /etc/camera/camera_characteristics.conf contains camera information which
32 // driver cannot provide.
33 static const char kCameraCharacteristicsConfigFile[] =
34 "/etc/camera/camera_characteristics.conf";
35 static const char kLensFacing[] = "lens_facing";
36 static const char kUsbVidPid[] = "usb_vid_pid";
37 static const char kUsbPath[] = "usb_path";
38
39 CameraFacingChromeOS::CameraFacingChromeOS() {
40 InitializeDeviceInfo(std::string(kCameraCharacteristicsConfigFile));
41 }
42
43 CameraFacingChromeOS::CameraFacingChromeOS(
44 const std::string& config_file_path) {
45 InitializeDeviceInfo(config_file_path);
46 }
47
48 CameraFacingChromeOS::~CameraFacingChromeOS() {}
49
50 VideoFacingMode CameraFacingChromeOS::GetCameraFacing(
51 const std::string& device_id,
52 const std::string& model_id) const {
53 std::string usb_id = GetUsbId(device_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 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);
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 }
79
80 std::string CameraFacingChromeOS::GetUsbId(const std::string& device_id) const {
81 // |device_id| is of the form "/dev/video2". We want to retrieve "video2"
82 // into |file_name|.
83 const std::string device_dir = "/dev/";
84 if (!base::StartsWith(device_id, device_dir, base::CompareCase::SENSITIVE)) {
85 DLOG(ERROR) << "device_id is invalid: " << device_id;
86 return std::string();
87 }
88 const std::string file_name = device_id.substr(device_dir.length());
89
90 // Usb ID can be obtained by "readlink /sys/class/video4linux/video2/device".
91 const std::string symlink =
92 base::StringPrintf("/sys/class/video4linux/%s/device", file_name.c_str());
93 base::FilePath symlinkTarget;
94 if (!base::ReadSymbolicLink(base::FilePath(symlink), &symlinkTarget)) {
95 DPLOG(ERROR) << "Failed to readlink: " << symlink;
96 return std::string();
97 }
98
99 // |symlinkTarget| is of the format "../../../A-B:C.D". Remove the path
100 // prefix.
101 base::StringPiece usb_part = symlinkTarget.BaseName().value();
102
103 // |usb_part| is of the format "A-B:C.D" or "A-B.C:D". We want everything
104 // before ":".
105 std::vector<base::StringPiece> usb_id_pieces = base::SplitStringPiece(
106 usb_part, ":", base::WhitespaceHandling::TRIM_WHITESPACE,
107 base::SplitResult::SPLIT_WANT_ALL);
108
109 if (usb_id_pieces.empty()) {
110 DLOG(ERROR) << "Error after split: " << usb_part;
111 return std::string();
112 }
113 return usb_id_pieces[0].as_string();
114 }
115
116 void CameraFacingChromeOS::InitializeDeviceInfo(
117 const std::string& config_file_path) {
118 const base::FilePath path(config_file_path);
119 std::string content;
120 if (!base::ReadFileToString(path, &content)) {
121 DPLOG(ERROR) << "ReadFileToString fails";
122 return;
123 }
124 const std::vector<base::StringPiece> lines = base::SplitStringPiece(
125 content, "\n", base::WhitespaceHandling::TRIM_WHITESPACE,
126 base::SplitResult::SPLIT_WANT_NONEMPTY);
127
128 for (const base::StringPiece& line : lines) {
129 if (line.starts_with("#")) // Ignore the comments that starts with "#".
130 continue;
131 const std::vector<base::StringPiece> key_value = base::SplitStringPiece(
132 line, "=", base::WhitespaceHandling::TRIM_WHITESPACE,
133 base::SplitResult::SPLIT_WANT_ALL);
134 if (key_value.size() != 2) {
135 DLOG(ERROR) << "Invalid line in config file: " << line;
136 continue;
137 }
138 const auto& key = key_value[0];
139 const auto& value = key_value[1];
140 const std::vector<base::StringPiece> sub_keys = base::SplitStringPiece(
141 key, ".", base::WhitespaceHandling::TRIM_WHITESPACE,
142 base::SplitResult::SPLIT_WANT_ALL);
143
144 if (sub_keys.size() < 1) {
145 DLOG(ERROR) << "No valid sub key exists. Line format is invalid: "
146 << line;
147 continue;
148 }
149 int camera_id = 0;
150 if (!GetCameraId(sub_keys[0], &camera_id)) {
151 DLOG(ERROR) << "Invalid sub key for camera id: " << sub_keys[0];
152 continue;
153 }
154
155 if (sub_keys.size() == 2 && sub_keys[1] == kLensFacing) {
156 int lens_facing = -1;
157 if (!base::StringToInt(value, &lens_facing)) {
158 DLOG(ERROR) << "Invalid value for lens_facing: " << value;
159 continue;
160 }
161 switch (lens_facing) {
162 case LensFacing::FRONT:
163 camera_id_to_facing_[camera_id] =
164 VideoFacingMode::MEDIA_VIDEO_FACING_USER;
165 break;
166 case LensFacing::BACK:
167 camera_id_to_facing_[camera_id] =
168 VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT;
169 break;
170 default:
171 DLOG(ERROR) << "Invalid value for lens_facing: " << lens_facing;
172 continue;
173 }
174 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbVidPid) {
175 if (value.empty()) {
176 DLOG(ERROR) << "model_id is empty";
177 continue;
178 }
179 std::string model_id = value.as_string();
180 std::transform(model_id.begin(), model_id.end(), model_id.begin(),
181 ::tolower);
182 model_id_to_camera_id_[model_id] = camera_id;
183 } else if (sub_keys.size() == 3 && sub_keys[2] == kUsbPath) {
184 if (value.empty()) {
185 DLOG(ERROR) << "usb_path is empty";
186 continue;
187 }
188 usb_id_to_camera_id_[value.as_string()] = camera_id;
189 }
190 // Ignore unknown or unutilized attributes.
191 }
192 }
193
194 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/linux/camera_facing_chromeos.h ('k') | media/capture/video/linux/camera_facing_chromeos_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698