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

Side by Side Diff: components/video_capture/video_capture_adapter.cc

Issue 1637793004: [WIP] Mojo Video Capture thingy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: heave!! Created 4 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 "components/video_capture/video_capture_adapter.h"
6
7 #include "base/time/time.h"
8 #include "components/video_capture/mojo_video_frame.h"
9 #include "media/base/bind_to_current_loop.h"
10 #include "third_party/libyuv/include/libyuv.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/size.h"
13
14 namespace video_capture {
15
16 namespace {
17
18 // This implements a VideoCaptureDevice::Client::Buffer as a lightweight wrapper
19 // around a MojoVideoFrame. This Buffer is not meant to be used beyond the
20 // VideoCaptureDevice and its Client implementation.
21 class BufferImpl : public media::VideoCaptureDevice::Client::Buffer {
22 public:
23 BufferImpl(scoped_refptr<MojoVideoFrame> video_frame)
24 : video_frame_(video_frame) {}
25
26 ~BufferImpl() override {}
27
28 // media::VideoCaptureDevice::Client::Buffer:
29 int id() const override { // Not needed.
30 NOTREACHED();
31 return 0;
32 }
33 gfx::Size dimensions() const override { return video_frame_->coded_size(); }
34 size_t mapped_size() const override { return video_frame_->mapped_size(); }
35 void* data(int plane) override { return video_frame_->data(plane); }
36 ClientBuffer AsClientBuffer(int plane) override {
37 NOTREACHED();
38 return nullptr;
39 }
40 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
41 base::FileDescriptor AsPlatformFile() override {
42 NOTREACHED();
43 return base::FileDescriptor();
44 }
45 #endif
46
47 const scoped_refptr<MojoVideoFrame> video_frame() { return video_frame_; }
48
49 private:
50 const scoped_refptr<MojoVideoFrame> video_frame_;
51
52 DISALLOW_COPY_AND_ASSIGN(BufferImpl);
53 };
54
55 } // namespace
56
57 // A pool of -possibly heterogenous- MojoVideoFrames, provides MojoVideoFrames
58 // if available and of the appropriate format, otherwise allocates it. The MVF
59 // are returned via registration of DestructionObserver. This class is used from
60 // the mojo component main thread and from a number of device threads.
61 class VideoCaptureAdapter::VideoFramePool
62 : public base::RefCountedThreadSafe<VideoCaptureAdapter::VideoFramePool> {
63 public:
64 VideoFramePool();
65
66 // Produces a frame matching the specified |dimensions|, either by grabbing it
67 // from the pool or creating a new frame if no suitable one is found.
68 scoped_refptr<MojoVideoFrame> CreateFrame(const gfx::Size& dimensions,
69 base::TimeDelta timestamp);
70
71 // Once this is called frames will no longer be inserted into |frames_|.
72 void Shutdown();
73
74 private:
75 // Can't use a WeakPtr since this class is used by multiple threads.
76 friend class base::RefCountedThreadSafe<VideoCaptureAdapter::VideoFramePool>;
77 ~VideoFramePool();
78
79 // Called when the frame wrapper gets destroyed. |frame| is the actual frame
80 // that was wrapped and is re inserted in |frames_| by this function.
81 void FrameReleased(const scoped_refptr<MojoVideoFrame>& frame);
82
83 // For mutual exclusion when accessing from multiple threads.
84 base::Lock lock_;
85 bool is_shutdown_;
86
87 std::list<scoped_refptr<MojoVideoFrame>> frames_;
88
89 DISALLOW_COPY_AND_ASSIGN(VideoFramePool);
90 };
91
92
93 VideoCaptureAdapter::VideoCaptureAdapter(Delegate* delegate)
94 : delegate_(delegate),
95 video_frame_pool_(new VideoFramePool()),
96 weak_factory_(this) {
97 DCHECK(delegate_);
98 // |this| is used exclusively by internal video capture device code which may,
99 // and ususally will, operate on a different thread from the constructor.
100 thread_checker_.DetachFromThread();
101 }
102
103 VideoCaptureAdapter::~VideoCaptureAdapter() {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 video_frame_pool_->Shutdown();
106 }
107
108 void VideoCaptureAdapter::OnIncomingCapturedData(
109 const uint8_t* data,
110 int length,
111 const media::VideoCaptureFormat& frame_format,
112 int clockwise_rotation,
113 const base::TimeTicks& timestamp) {
114 DVLOG(1) << __FUNCTION__;
115 // N.B.: All of the logic throughout this method is lifted directly from
116 // content::VideoCaptureDeviceClient and should generally continue to mirror
117 // that implementation until this one becomes the source of truth.
118 DCHECK(thread_checker_.CalledOnValidThread());
119 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage);
120
121 if (!frame_format.IsValid())
122 return;
123
124 const int width_parity = frame_format.frame_size.width() & 1;
125 const int height_parity = frame_format.frame_size.height() & 1;
126 const int even_width = frame_format.frame_size.width() & ~1;
127 const int even_height = frame_format.frame_size.height() & ~1;
128
129 int destination_width = even_width;
130 int destination_height = even_height;
131
132 if (clockwise_rotation == 90 || clockwise_rotation == 270)
133 std::swap(destination_width, destination_height);
134
135 DCHECK_EQ(clockwise_rotation % 90, 0)
136 << "Rotation must be a multiple of 90, now: " << clockwise_rotation;
137 libyuv::RotationMode rotation_mode = libyuv::kRotate0;
138 if (clockwise_rotation == 90)
139 rotation_mode = libyuv::kRotate90;
140 else if (clockwise_rotation == 180)
141 rotation_mode = libyuv::kRotate180;
142 else if (clockwise_rotation == 270)
143 rotation_mode = libyuv::kRotate270;
144
145 libyuv::FourCC colorspace = libyuv::FOURCC_ANY;
146 bool flip = false;
147 switch (frame_format.pixel_format) {
148 case media::PIXEL_FORMAT_UNKNOWN:
149 break;
150 case media::PIXEL_FORMAT_I420:
151 DCHECK(!width_parity && !height_parity);
152 colorspace = libyuv::FOURCC_I420;
153 break;
154 case media::PIXEL_FORMAT_YV12:
155 DCHECK(!width_parity && !height_parity);
156 colorspace = libyuv::FOURCC_YV12;
157 break;
158 case media::PIXEL_FORMAT_NV12:
159 DCHECK(!width_parity && !height_parity);
160 colorspace = libyuv::FOURCC_NV12;
161 break;
162 case media::PIXEL_FORMAT_NV21:
163 DCHECK(!width_parity && !height_parity);
164 colorspace = libyuv::FOURCC_NV21;
165 break;
166 case media::PIXEL_FORMAT_YUY2:
167 DCHECK(!width_parity && !height_parity);
168 colorspace = libyuv::FOURCC_YUY2;
169 break;
170 case media::PIXEL_FORMAT_UYVY:
171 DCHECK(!width_parity && !height_parity);
172 colorspace = libyuv::FOURCC_UYVY;
173 break;
174 case media::PIXEL_FORMAT_RGB24:
175 // Linux RGB24 defines red at lowest byte address,
176 // see http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html.
177 // Windows RGB24 defines blue at lowest byte,
178 // see https://msdn.microsoft.com/en-us/library/windows/desktop/dd407253
179 #if defined(OS_LINUX)
180 colorspace = libyuv::FOURCC_RAW;
181 #elif defined(OS_WIN)
182 colorspace = libyuv::FOURCC_24BG;
183 #else
184 NOTREACHED() << "RGB24 is only available in Linux and Windows platforms";
185 #endif
186 #if defined(OS_WIN)
187 // TODO(wjia): Currently, for RGB24 on WIN, capture device always
188 // passes in positive src_width and src_height. Remove this hardcoded
189 // value when nagative src_height is supported. The negative src_height
190 // indicates that vertical flipping is needed.
191 flip = true;
192 #endif
193 break;
194 case media::PIXEL_FORMAT_RGB32:
195 // Fallback to PIXEL_FORMAT_ARGB setting |flip| in Windows
196 // platforms.
197 #if defined(OS_WIN)
198 flip = true;
199 #endif
200 case media::PIXEL_FORMAT_ARGB:
201 colorspace = libyuv::FOURCC_ARGB;
202 break;
203 case media::PIXEL_FORMAT_MJPEG:
204 colorspace = libyuv::FOURCC_MJPG;
205 break;
206 default:
207 NOTREACHED();
208 }
209
210 const gfx::Size dimensions(destination_width, destination_height);
211 scoped_ptr<Buffer> buffer = ReserveOutputBuffer(
212 dimensions, media::PIXEL_FORMAT_I420, media::PIXEL_STORAGE_CPU);
213 if (!buffer)
214 return;
215
216 int y_stride = dimensions.width();
217 int uv_stride = y_stride / 2;
218 if (libyuv::ConvertToI420(
219 data, length,
220 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kYPlane)),
221 y_stride,
222 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kUPlane)),
223 uv_stride,
224 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kVPlane)),
225 uv_stride, 0 /* crop_x */, 0 /* crop_y */,
226 frame_format.frame_size.width(),
227 (flip ? -1 : 1) * frame_format.frame_size.height(), even_width,
228 even_height, rotation_mode, colorspace)) {
229 DLOG(ERROR) << "Failed to convert frame pixels to I420";
230 return;
231 }
232
233 OnIncomingCapturedBuffer(std::move(buffer),
234 media::VideoCaptureFormat(frame_format.frame_size,
235 0.0f,
236 media::PIXEL_FORMAT_I420),
237 timestamp);
238 }
239
240 void VideoCaptureAdapter::OnIncomingCapturedYuvData(
241 const uint8_t* y_data,
242 const uint8_t* u_data,
243 const uint8_t* v_data,
244 size_t y_stride,
245 size_t u_stride,
246 size_t v_stride,
247 const media::VideoCaptureFormat& frame_format,
248 int clockwise_rotation,
249 const base::TimeTicks& timestamp) {
250 NOTIMPLEMENTED();
251 }
252
253 scoped_ptr<media::VideoCaptureDevice::Client::Buffer>
254 VideoCaptureAdapter::ReserveOutputBuffer(
255 const gfx::Size& dimensions,
256 media::VideoPixelFormat pixel_format,
257 media::VideoPixelStorage /* pixel_storage */) {
258 DVLOG(1) << __FUNCTION__;
259 DCHECK(thread_checker_.CalledOnValidThread());
260 DCHECK_EQ(media::PIXEL_FORMAT_I420, pixel_format);
261 DCHECK(!dimensions.IsEmpty());
262
263 return make_scoped_ptr<Buffer>(new BufferImpl(video_frame_pool_->CreateFrame(
264 dimensions, base::TimeTicks::Now() - base::TimeTicks())));
265 }
266
267 void VideoCaptureAdapter::OnIncomingCapturedBuffer(
268 scoped_ptr<Buffer> buffer,
269 const media::VideoCaptureFormat& frame_format,
270 const base::TimeTicks& timestamp) {
271 DVLOG(1) << __FUNCTION__;
272 DCHECK(thread_checker_.CalledOnValidThread());
273 DCHECK(buffer);
274 DCHECK_EQ(frame_format.pixel_format, media::PIXEL_FORMAT_I420);
275 DCHECK_EQ(frame_format.pixel_storage, media::PIXEL_STORAGE_CPU);
276
277 // |buffer| can get out of scope since we have the important frame.
278 delegate_->OnFrame(static_cast<BufferImpl*>(buffer.get())->video_frame(),
279 timestamp);
280 }
281
282 void VideoCaptureAdapter::OnIncomingCapturedVideoFrame(
283 scoped_ptr<Buffer> buffer,
284 const scoped_refptr<media::VideoFrame>& frame,
285 const base::TimeTicks& timestamp) {
286 DCHECK(thread_checker_.CalledOnValidThread());
287 DCHECK(buffer);
288
289 // TODO(mcasas): Contents capture calls here. Wire this path by wrapping
290 // |frame| in a MojoVideoFrame.
291 NOTIMPLEMENTED();
292 }
293
294 void VideoCaptureAdapter::OnError(const tracked_objects::Location& from_here,
295 const std::string& reason) {
296 DVLOG(1) << __FUNCTION__ << " " << reason;
297 DCHECK(thread_checker_.CalledOnValidThread());
298 delegate_->OnError(reason);
299 }
300
301 double VideoCaptureAdapter::GetBufferPoolUtilization() const {
302 DCHECK(thread_checker_.CalledOnValidThread());
303 NOTIMPLEMENTED();
304 // TODO(mcasas): In content/, the frame pool did have a maximum. Consider
305 // doing the same in this VideoFramePool, and return here the amount in use
306 // there.
307 return 0.0;
308 }
309
310 VideoCaptureAdapter::VideoFramePool::VideoFramePool() : is_shutdown_(false) {}
311
312 scoped_refptr<MojoVideoFrame> VideoCaptureAdapter::VideoFramePool::CreateFrame(
313 const gfx::Size& dimensions,
314 base::TimeDelta timestamp) {
315 DVLOG(1) << __FUNCTION__ << " " << dimensions.ToString();
316 base::AutoLock auto_lock(lock_);
317 DCHECK(!is_shutdown_);
318
319 scoped_refptr<MojoVideoFrame> frame;
320 for(const auto& pool_frame: frames_) {
321 if (pool_frame->coded_size() == dimensions) {
322 DCHECK(pool_frame->handle().is_valid());
323 frame = pool_frame;
324 frame->set_timestamp(timestamp);
325 frame->metadata()->Clear();
326 break;
327 }
328 }
329
330 // Found no suitable frame available: Create a new MojoVideoFrame.
331 if (!frame.get()) {
332 frame = MojoVideoFrame::CreateMojoVideoFrame(dimensions, timestamp);
333 LOG_IF(ERROR, !frame.get()) << "Failed to create a video frame";
334
335 frames_.push_back(frame);
336 }
337
338 // Make a copy of |frame| to register a Destruction Observer.
339 scoped_refptr<MojoVideoFrame> wrapped_frame = frame;
340 wrapped_frame->AddDestructionObserver(
341 base::Bind(&VideoFramePool::FrameReleased, this, frame));
342 return wrapped_frame;
343 }
344
345 void VideoCaptureAdapter::VideoFramePool::Shutdown() {
346 base::AutoLock auto_lock(lock_);
347 is_shutdown_ = true;
348 frames_.clear();
349 }
350
351 VideoCaptureAdapter::VideoFramePool::~VideoFramePool() {
352 DCHECK(is_shutdown_);
353 }
354
355 void VideoCaptureAdapter::VideoFramePool::FrameReleased(
356 const scoped_refptr<MojoVideoFrame>& frame) {
357 DVLOG(1) << __FUNCTION__;
358 if (is_shutdown_)
359 return;
360 frames_.push_back(frame);
361 }
362
363 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698