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

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

Issue 2508803002: Rotate frames correctly for back camera (Closed)
Patch Set: Add comment 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"), device_descriptor_(device_descriptor) {
41 device_descriptor_(device_descriptor) {} 42 CameraCharacteristics characteristics;
wuchengli 2016/11/22 07:59:34 base::LazyInstance<CameraCharacteristics> g_camera
shenghao 2016/11/24 07:13:04 Done.
43 is_back_camera_ =
44 characteristics.GetCameraFacing(device_descriptor.model_id) ==
45 DeviceInfo::LENS_FACING_BACK;
46 }
42 47
43 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() { 48 VideoCaptureDeviceLinux::~VideoCaptureDeviceLinux() {
44 // Check if the thread is running. 49 // Check if the thread is running.
45 // This means that the device has not been StopAndDeAllocate()d properly. 50 // This means that the device has not been StopAndDeAllocate()d properly.
46 DCHECK(!v4l2_thread_.IsRunning()); 51 DCHECK(!v4l2_thread_.IsRunning());
47 v4l2_thread_.Stop(); 52 v4l2_thread_.Stop();
48 } 53 }
49 54
50 void VideoCaptureDeviceLinux::AllocateAndStart( 55 void VideoCaptureDeviceLinux::AllocateAndStart(
51 const VideoCaptureParams& params, 56 const VideoCaptureParams& params,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (!v4l2_thread_.IsRunning()) 111 if (!v4l2_thread_.IsRunning())
107 return; // Wrong state. 112 return; // Wrong state.
108 v4l2_thread_.task_runner()->PostTask( 113 v4l2_thread_.task_runner()->PostTask(
109 FROM_HERE, 114 FROM_HERE,
110 base::Bind(&V4L2CaptureDelegate::SetPhotoOptions, capture_impl_, 115 base::Bind(&V4L2CaptureDelegate::SetPhotoOptions, capture_impl_,
111 base::Passed(&settings), base::Passed(&callback))); 116 base::Passed(&settings), base::Passed(&callback)));
112 } 117 }
113 118
114 void VideoCaptureDeviceLinux::SetRotation(int rotation) { 119 void VideoCaptureDeviceLinux::SetRotation(int rotation) {
115 if (v4l2_thread_.IsRunning()) { 120 if (v4l2_thread_.IsRunning()) {
121 // We assume external camera is facing the users. If not, the users can
122 // rotate the camera manually by theirselves.
wuchengli 2016/11/22 03:47:06 s/theirselves/themselves/
shenghao 2016/11/24 07:13:04 Done.
123 if (is_back_camera_) {
124 // Original frame when screen_rotation = 0
125 // -----------------------
126 // | |
127 // | * |
128 // | * * |
129 // | ***** |
wuchengli 2016/11/22 03:47:06 Great diagram! Can you make the A of the first one
shenghao 2016/11/24 07:13:04 Done.
130 // | * * |
131 // | |
132 // -----------------------
133 //
134 // screen_rotation = 90, this is what back camera sees
135 // -----------------------
136 // | ******** |
137 // | * **** |
138 // | * *** |
139 // | * *** |
140 // | * **** |
141 // | ******** |
142 // -----------------------
143 //
144 // screen_rotation = 90, this is what front camera sees
145 // -----------------------
146 // | ******** |
147 // | **** * |
148 // | *** * |
149 // | *** * |
150 // | **** * |
151 // | ******** |
152 // -----------------------
153 //
154 // Therefore, for back camera, we need to rotate (360 - screen_rotation).
155 rotation = (360 - rotation) % 360;
156 }
116 v4l2_thread_.task_runner()->PostTask( 157 v4l2_thread_.task_runner()->PostTask(
117 FROM_HERE, 158 FROM_HERE,
118 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation)); 159 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation));
119 } 160 }
120 } 161 }
121 162
122 // static 163 // static
123 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2( 164 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2(
124 PowerLineFrequency frequency) { 165 PowerLineFrequency frequency) {
125 switch (frequency) { 166 switch (frequency) {
126 case media::PowerLineFrequency::FREQUENCY_50HZ: 167 case media::PowerLineFrequency::FREQUENCY_50HZ:
127 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; 168 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
128 case media::PowerLineFrequency::FREQUENCY_60HZ: 169 case media::PowerLineFrequency::FREQUENCY_60HZ:
129 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; 170 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
130 default: 171 default:
131 // If we have no idea of the frequency, at least try and set it to AUTO. 172 // If we have no idea of the frequency, at least try and set it to AUTO.
132 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; 173 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
133 } 174 }
134 } 175 }
135 176
136 } // namespace media 177 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698