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

Side by Side Diff: media/capture/video/video_capture_device_client.cc

Issue 2428263004: 16 bpp video stream capture, render and createImageBitmap(video) using (CPU) shared memory buffers (Closed)
Patch Set: Split webrtc_depth_capture_browsertest. Thanks phoglund@, Created 4 years, 1 month 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/video_capture_device_client.h" 5 #include "media/capture/video/video_capture_device_client.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/trace_event/trace_event.h" 15 #include "base/trace_event/trace_event.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "media/base/bind_to_current_loop.h" 17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/video_frame.h" 18 #include "media/base/video_frame.h"
19 #include "media/capture/video/video_capture_buffer_handle.h" 19 #include "media/capture/video/video_capture_buffer_handle.h"
20 #include "media/capture/video/video_capture_buffer_pool.h" 20 #include "media/capture/video/video_capture_buffer_pool.h"
21 #include "media/capture/video/video_capture_jpeg_decoder.h" 21 #include "media/capture/video/video_capture_jpeg_decoder.h"
22 #include "media/capture/video/video_frame_receiver.h" 22 #include "media/capture/video/video_frame_receiver.h"
23 #include "media/capture/video_capture_types.h" 23 #include "media/capture/video_capture_types.h"
24 #include "third_party/libyuv/include/libyuv.h" 24 #include "third_party/libyuv/include/libyuv.h"
25 25
26 using media::VideoCaptureFormat; 26 using media::VideoCaptureFormat;
27 using media::VideoFrame; 27 using media::VideoFrame;
28 using media::VideoFrameMetadata; 28 using media::VideoFrameMetadata;
29 29
30 namespace {
31
32 bool IsFormatSupported(media::VideoPixelFormat pixel_format) {
33 return (pixel_format == media::PIXEL_FORMAT_I420 ||
34 pixel_format == media::PIXEL_FORMAT_Y16);
35 }
36 }
37
30 namespace media { 38 namespace media {
31 39
32 // Class combining a Client::Buffer interface implementation and a pool buffer 40 // Class combining a Client::Buffer interface implementation and a pool buffer
33 // implementation to guarantee proper cleanup on destruction on our side. 41 // implementation to guarantee proper cleanup on destruction on our side.
34 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer { 42 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer {
35 public: 43 public:
36 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool, 44 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool,
37 int buffer_id) 45 int buffer_id)
38 : id_(buffer_id), 46 : id_(buffer_id),
39 pool_(pool), 47 pool_(pool),
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 !external_jpeg_decoder_initialized_) { 107 !external_jpeg_decoder_initialized_) {
100 external_jpeg_decoder_initialized_ = true; 108 external_jpeg_decoder_initialized_ = true;
101 external_jpeg_decoder_ = jpeg_decoder_factory_callback_.Run(); 109 external_jpeg_decoder_ = jpeg_decoder_factory_callback_.Run();
102 external_jpeg_decoder_->Initialize(); 110 external_jpeg_decoder_->Initialize();
103 } 111 }
104 } 112 }
105 113
106 if (!frame_format.IsValid()) 114 if (!frame_format.IsValid())
107 return; 115 return;
108 116
117 if (frame_format.pixel_format == media::PIXEL_FORMAT_Y16) {
118 return OnIncomingCapturedY16Data(data, length, frame_format, reference_time,
119 timestamp);
120 }
121
109 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest 122 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest
110 // bit decomposition of {width, height}, grabbing the odd and even parts. 123 // bit decomposition of {width, height}, grabbing the odd and even parts.
111 const int chopped_width = frame_format.frame_size.width() & 1; 124 const int chopped_width = frame_format.frame_size.width() & 1;
112 const int chopped_height = frame_format.frame_size.height() & 1; 125 const int chopped_height = frame_format.frame_size.height() & 1;
113 const int new_unrotated_width = frame_format.frame_size.width() & ~1; 126 const int new_unrotated_width = frame_format.frame_size.width() & ~1;
114 const int new_unrotated_height = frame_format.frame_size.height() & ~1; 127 const int new_unrotated_height = frame_format.frame_size.height() & ~1;
115 128
116 int destination_width = new_unrotated_width; 129 int destination_width = new_unrotated_width;
117 int destination_height = new_unrotated_height; 130 int destination_height = new_unrotated_height;
118 if (rotation == 90 || rotation == 270) 131 if (rotation == 90 || rotation == 270)
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 timestamp); 263 timestamp);
251 } 264 }
252 265
253 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> 266 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer>
254 VideoCaptureDeviceClient::ReserveOutputBuffer( 267 VideoCaptureDeviceClient::ReserveOutputBuffer(
255 const gfx::Size& frame_size, 268 const gfx::Size& frame_size,
256 media::VideoPixelFormat pixel_format, 269 media::VideoPixelFormat pixel_format,
257 media::VideoPixelStorage pixel_storage) { 270 media::VideoPixelStorage pixel_storage) {
258 DCHECK_GT(frame_size.width(), 0); 271 DCHECK_GT(frame_size.width(), 0);
259 DCHECK_GT(frame_size.height(), 0); 272 DCHECK_GT(frame_size.height(), 0);
260 // Currently, only I420 pixel format is supported. 273 DCHECK(IsFormatSupported(pixel_format));
261 DCHECK_EQ(media::PIXEL_FORMAT_I420, pixel_format);
262 274
263 // TODO(mcasas): For PIXEL_STORAGE_GPUMEMORYBUFFER, find a way to indicate if 275 // TODO(mcasas): For PIXEL_STORAGE_GPUMEMORYBUFFER, find a way to indicate if
264 // it's a ShMem GMB or a DmaBuf GMB. 276 // it's a ShMem GMB or a DmaBuf GMB.
265 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; 277 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId;
266 const int buffer_id = buffer_pool_->ReserveForProducer( 278 const int buffer_id = buffer_pool_->ReserveForProducer(
267 frame_size, pixel_format, pixel_storage, &buffer_id_to_drop); 279 frame_size, pixel_format, pixel_storage, &buffer_id_to_drop);
268 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) 280 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId)
269 receiver_->OnBufferDestroyed(buffer_id_to_drop); 281 receiver_->OnBufferDestroyed(buffer_id_to_drop);
270 if (buffer_id == VideoCaptureBufferPool::kInvalidId) 282 if (buffer_id == VideoCaptureBufferPool::kInvalidId)
271 return nullptr; 283 return nullptr;
272 return base::WrapUnique<Buffer>( 284 return base::WrapUnique<Buffer>(
273 new AutoReleaseBuffer(buffer_pool_, buffer_id)); 285 new AutoReleaseBuffer(buffer_pool_, buffer_id));
274 } 286 }
275 287
276 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( 288 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer(
277 std::unique_ptr<Buffer> buffer, 289 std::unique_ptr<Buffer> buffer,
278 const VideoCaptureFormat& frame_format, 290 const VideoCaptureFormat& frame_format,
279 base::TimeTicks reference_time, 291 base::TimeTicks reference_time,
280 base::TimeDelta timestamp) { 292 base::TimeDelta timestamp) {
281 // Currently, only I420 pixel format is supported. 293 DCHECK(IsFormatSupported(frame_format.pixel_format));
282 DCHECK_EQ(media::PIXEL_FORMAT_I420, frame_format.pixel_format);
283 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage); 294 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage);
284 295
285 scoped_refptr<VideoFrame> frame; 296 scoped_refptr<VideoFrame> frame;
286 if (buffer->IsBackedByVideoFrame()) { 297 if (buffer->IsBackedByVideoFrame()) {
287 frame = buffer->GetVideoFrame(); 298 frame = buffer->GetVideoFrame();
288 frame->set_timestamp(timestamp); 299 frame->set_timestamp(timestamp);
289 } else { 300 } else {
290 frame = VideoFrame::WrapExternalSharedMemory( 301 frame = VideoFrame::WrapExternalSharedMemory(
291 media::PIXEL_FORMAT_I420, frame_format.frame_size, 302 frame_format.pixel_format, frame_format.frame_size,
292 gfx::Rect(frame_format.frame_size), frame_format.frame_size, 303 gfx::Rect(frame_format.frame_size), frame_format.frame_size,
293 reinterpret_cast<uint8_t*>(buffer->data()), 304 reinterpret_cast<uint8_t*>(buffer->data()),
294 VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, 305 VideoFrame::AllocationSize(frame_format.pixel_format,
295 frame_format.frame_size), 306 frame_format.frame_size),
296 base::SharedMemory::NULLHandle(), 0u, timestamp); 307 base::SharedMemory::NULLHandle(), 0u, timestamp);
297 } 308 }
298 if (!frame) 309 if (!frame)
299 return; 310 return;
300 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, 311 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
301 frame_format.frame_rate); 312 frame_format.frame_rate);
302 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, 313 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME,
303 reference_time); 314 reference_time);
304 OnIncomingCapturedVideoFrame(std::move(buffer), frame); 315 OnIncomingCapturedVideoFrame(std::move(buffer), frame);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 *y_plane_data = reinterpret_cast<uint8_t*>(buffer->data()); 376 *y_plane_data = reinterpret_cast<uint8_t*>(buffer->data());
366 *u_plane_data = 377 *u_plane_data =
367 *y_plane_data + 378 *y_plane_data +
368 VideoFrame::PlaneSize(format, VideoFrame::kYPlane, dimensions).GetArea(); 379 VideoFrame::PlaneSize(format, VideoFrame::kYPlane, dimensions).GetArea();
369 *v_plane_data = 380 *v_plane_data =
370 *u_plane_data + 381 *u_plane_data +
371 VideoFrame::PlaneSize(format, VideoFrame::kUPlane, dimensions).GetArea(); 382 VideoFrame::PlaneSize(format, VideoFrame::kUPlane, dimensions).GetArea();
372 return buffer; 383 return buffer;
373 } 384 }
374 385
386 void VideoCaptureDeviceClient::OnIncomingCapturedY16Data(
387 const uint8_t* data,
388 int length,
389 const VideoCaptureFormat& frame_format,
390 base::TimeTicks reference_time,
391 base::TimeDelta timestamp) {
392 std::unique_ptr<Buffer> buffer(ReserveOutputBuffer(frame_format.frame_size,
393 media::PIXEL_FORMAT_Y16,
394 media::PIXEL_STORAGE_CPU));
395 // The input |length| can be greater than the required buffer size because of
396 // paddings and/or alignments, but it cannot be smaller.
397 DCHECK_GE(static_cast<size_t>(length), frame_format.ImageAllocationSize());
398 #if DCHECK_IS_ON()
399 dropped_frame_counter_ = buffer.get() ? 0 : dropped_frame_counter_ + 1;
400 if (dropped_frame_counter_ >= kMaxDroppedFrames)
401 OnError(FROM_HERE, "Too many frames dropped");
402 #endif
403 // Failed to reserve output buffer, so drop the frame.
404 if (!buffer.get())
405 return;
406 memcpy(buffer->data(), data, length);
407 const VideoCaptureFormat output_format =
408 VideoCaptureFormat(frame_format.frame_size, frame_format.frame_rate,
409 media::PIXEL_FORMAT_Y16, media::PIXEL_STORAGE_CPU);
410 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time,
411 timestamp);
412 }
413
375 } // namespace media 414 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698