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

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

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

Powered by Google App Engine
This is Rietveld 408576698