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

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

Issue 2508803002: Rotate frames correctly for back camera (Closed)
Patch Set: Use usb_path to get lens_facing 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/capture/video/linux/video_capture_device_linux.h" 5 #include "media/capture/video/linux/video_capture_device_linux.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <list> 9 #include <list>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "media/capture/video/linux/camera_characteristics.h"
14 #include "media/capture/video/linux/v4l2_capture_delegate.h" 15 #include "media/capture/video/linux/v4l2_capture_delegate.h"
15 16
16 #if defined(OS_OPENBSD) 17 #if defined(OS_OPENBSD)
17 #include <sys/videoio.h> 18 #include <sys/videoio.h>
18 #else 19 #else
19 #include <linux/videodev2.h> 20 #include <linux/videodev2.h>
20 #endif 21 #endif
21 22
22 namespace media { 23 namespace media {
23 24
24 // Translates Video4Linux pixel formats to Chromium pixel formats. 25 // Translates Video4Linux pixel formats to Chromium pixel formats.
25 // static 26 // static
26 VideoPixelFormat VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat( 27 VideoPixelFormat VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat(
27 uint32_t v4l2_fourcc) { 28 uint32_t v4l2_fourcc) {
28 return V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(v4l2_fourcc); 29 return V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(v4l2_fourcc);
29 } 30 }
30 31
31 // Gets a list of usable Four CC formats prioritized. 32 // Gets a list of usable Four CC formats prioritized.
32 // static 33 // static
33 std::list<uint32_t> VideoCaptureDeviceLinux::GetListOfUsableFourCCs( 34 std::list<uint32_t> VideoCaptureDeviceLinux::GetListOfUsableFourCCs(
34 bool favour_mjpeg) { 35 bool favour_mjpeg) {
35 return V4L2CaptureDelegate::GetListOfUsableFourCcs(favour_mjpeg); 36 return V4L2CaptureDelegate::GetListOfUsableFourCcs(favour_mjpeg);
36 } 37 }
37 38
38 VideoCaptureDeviceLinux::VideoCaptureDeviceLinux( 39 VideoCaptureDeviceLinux::VideoCaptureDeviceLinux(
39 const VideoCaptureDeviceDescriptor& device_descriptor) 40 const VideoCaptureDeviceDescriptor& device_descriptor)
40 : v4l2_thread_("V4L2CaptureThread"), 41 : v4l2_thread_("V4L2CaptureThread"),
41 device_descriptor_(device_descriptor) {} 42 device_descriptor_(device_descriptor),
43 camera_characteristics_(LAZY_INSTANCE_INITIALIZER) {
44 CameraCharacteristics characteristics;
45 is_back_camera_ =
kcwu 2016/11/24 10:16:08 Do you intent to have this in normal linux? If not
shenghao 2016/11/24 14:13:25 Done.
46 camera_characteristics_.Get().GetCameraFacing(
kcwu 2016/11/24 10:16:08 per coding style, don't do non-trivial thing in ct
shenghao 2016/11/24 14:13:25 Done.
kcwu 2016/11/25 09:09:03 I found the style guide changed the suggestion. It
47 device_descriptor.model_id, device_descriptor.device_id) ==
48 CameraDeviceInfo::LENS_FACING_BACK;
49 }
42 50
43 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() { 51 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() {
44 // Check if the thread is running. 52 // Check if the thread is running.
45 // This means that the device has not been StopAndDeAllocate()d properly. 53 // This means that the device has not been StopAndDeAllocate()d properly.
46 DCHECK(!v4l2_thread_.IsRunning()); 54 DCHECK(!v4l2_thread_.IsRunning());
47 v4l2_thread_.Stop(); 55 v4l2_thread_.Stop();
48 } 56 }
49 57
50 void VideoCaptureDeviceLinux::AllocateAndStart( 58 void VideoCaptureDeviceLinux::AllocateAndStart(
51 const VideoCaptureParams& params, 59 const VideoCaptureParams& params,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (!v4l2_thread_.IsRunning()) 114 if (!v4l2_thread_.IsRunning())
107 return; // Wrong state. 115 return; // Wrong state.
108 v4l2_thread_.task_runner()->PostTask( 116 v4l2_thread_.task_runner()->PostTask(
109 FROM_HERE, 117 FROM_HERE,
110 base::Bind(&V4L2CaptureDelegate::SetPhotoOptions, capture_impl_, 118 base::Bind(&V4L2CaptureDelegate::SetPhotoOptions, capture_impl_,
111 base::Passed(&settings), base::Passed(&callback))); 119 base::Passed(&settings), base::Passed(&callback)));
112 } 120 }
113 121
114 void VideoCaptureDeviceLinux::SetRotation(int rotation) { 122 void VideoCaptureDeviceLinux::SetRotation(int rotation) {
115 if (v4l2_thread_.IsRunning()) { 123 if (v4l2_thread_.IsRunning()) {
124 // We assume external camera is facing the users. If not, the users can
125 // rotate the camera manually by themselves.
126 if (is_back_camera_) {
127 // Original frame when screen_rotation = 0
128 // -----------------------
129 // | * |
130 // | * * |
131 // | * * |
132 // | ******* |
133 // | * * |
134 // | * * |
135 // -----------------------
136 //
137 // screen_rotation = 90, this is what back camera sees
138 // -----------------------
139 // | ******** |
140 // | * **** |
141 // | * *** |
142 // | * *** |
143 // | * **** |
144 // | ******** |
145 // -----------------------
146 //
147 // screen_rotation = 90, this is what front camera sees
148 // -----------------------
149 // | ******** |
150 // | **** * |
151 // | *** * |
152 // | *** * |
153 // | **** * |
154 // | ******** |
155 // -----------------------
156 //
157 // Therefore, for back camera, we need to rotate (360 - screen_rotation).
158 rotation = (360 - rotation) % 360;
159 }
116 v4l2_thread_.task_runner()->PostTask( 160 v4l2_thread_.task_runner()->PostTask(
117 FROM_HERE, 161 FROM_HERE,
118 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation)); 162 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation));
119 } 163 }
120 } 164 }
121 165
122 // static 166 // static
123 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2( 167 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2(
124 PowerLineFrequency frequency) { 168 PowerLineFrequency frequency) {
125 switch (frequency) { 169 switch (frequency) {
126 case media::PowerLineFrequency::FREQUENCY_50HZ: 170 case media::PowerLineFrequency::FREQUENCY_50HZ:
127 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; 171 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
128 case media::PowerLineFrequency::FREQUENCY_60HZ: 172 case media::PowerLineFrequency::FREQUENCY_60HZ:
129 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; 173 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
130 default: 174 default:
131 // If we have no idea of the frequency, at least try and set it to AUTO. 175 // If we have no idea of the frequency, at least try and set it to AUTO.
132 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; 176 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
133 } 177 }
134 } 178 }
135 179
136 } // namespace media 180 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698