| Index: components/video_capture/device_client_impl.cc
|
| diff --git a/components/video_capture/device_client_impl.cc b/components/video_capture/device_client_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..234ef349c49225f7657f48f2dc2f16e8fa88d896
|
| --- /dev/null
|
| +++ b/components/video_capture/device_client_impl.cc
|
| @@ -0,0 +1,383 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/video_capture/device_client_impl.h"
|
| +
|
| +#include <algorithm>
|
| +#include <utility>
|
| +
|
| +#include "base/macros.h"
|
| +#include "base/time/time.h"
|
| +#include "media/base/bind_to_current_loop.h"
|
| +#include "media/base/video_frame.h"
|
| +#include "third_party/libyuv/include/libyuv.h"
|
| +#include "ui/gfx/geometry/rect.h"
|
| +#include "ui/gfx/geometry/size.h"
|
| +
|
| +namespace video_capture {
|
| +
|
| +namespace {
|
| +
|
| +// This implements a VideoCaptureDevice::Client::Buffer as a lightweight wrapper
|
| +// around a VideoFrame.
|
| +class BufferImpl : public media::VideoCaptureDevice::Client::Buffer {
|
| + public:
|
| + explicit BufferImpl(scoped_refptr<media::VideoFrame> video_frame)
|
| + : video_frame_(video_frame) {}
|
| +
|
| + ~BufferImpl() override {}
|
| +
|
| + // media::VideoCaptureDevice::Client::Buffer:
|
| + int id() const override {
|
| + // TODO(mcasas): probably not needed, DCI's Delegate should not know this
|
| + // internal number, and use the handle?
|
| + return 0;
|
| + }
|
| + gfx::Size dimensions() const override { return video_frame_->coded_size(); }
|
| + size_t mapped_size() const override {
|
| + return video_frame_->shared_memory().mapped_size();
|
| + }
|
| + void* data(int plane) override { return video_frame_->data(plane); }
|
| + ClientBuffer AsClientBuffer(int plane) override {
|
| + NOTREACHED();
|
| + return nullptr;
|
| + }
|
| +#if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
|
| + base::FileDescriptor AsPlatformFile() override {
|
| + NOTREACHED();
|
| + return base::FileDescriptor();
|
| + }
|
| +#endif
|
| +
|
| + const scoped_refptr<media::VideoFrame> video_frame() const {
|
| + return video_frame_;
|
| + }
|
| +
|
| + private:
|
| + const scoped_refptr<media::VideoFrame> video_frame_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BufferImpl);
|
| +};
|
| +
|
| +void ReleaseBuffer(
|
| + scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer) {
|
| + // This lets us bind the lifetime of a buffer to that of a closure.
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class DeviceClientImpl::VideoFramePool
|
| + : public base::RefCountedThreadSafe<DeviceClientImpl::VideoFramePool> {
|
| + public:
|
| + VideoFramePool();
|
| +
|
| + // Returns a frame from the pool that matches the specified parameters or
|
| + // creates a new frame if no suitable frame exists in the pool.
|
| + scoped_refptr<media::VideoFrame> CreateFrame(
|
| + media::VideoPixelFormat format,
|
| + const gfx::Size& coded_size,
|
| + const gfx::Rect& visible_rect,
|
| + const gfx::Size& natural_size,
|
| + base::TimeDelta timestamp);
|
| +
|
| + private:
|
| + // Can't use a WeakPtr since this class is used by multiple threads.
|
| + friend class base::RefCountedThreadSafe<DeviceClientImpl::VideoFramePool>;
|
| + ~VideoFramePool();
|
| +
|
| + // Called when the frame wrapper gets destroyed. |frame| is the actual frame
|
| + // that was wrapped and is re inserted in |frames_| by this function.
|
| + void FrameReleased(const scoped_refptr<media::VideoFrame>& frame);
|
| +
|
| + base::Lock lock_;
|
| + std::list<scoped_refptr<media::VideoFrame>> frames_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(VideoFramePool);
|
| +};
|
| +
|
| +
|
| +DeviceClientImpl::DeviceClientImpl(Delegate* delegate)
|
| + : delegate_(delegate),
|
| + video_frame_pool_(new VideoFramePool()),
|
| + weak_factory_(this) {
|
| + // |this| is used exclusively by internal video capture device code which may
|
| + // operate on a different thread from the constructor.
|
| + thread_checker_.DetachFromThread();
|
| +}
|
| +
|
| +DeviceClientImpl::~DeviceClientImpl() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| +}
|
| +
|
| +DeviceClientImpl::VideoFramePool::VideoFramePool() {}
|
| +
|
| +scoped_refptr<media::VideoFrame> DeviceClientImpl::VideoFramePool::CreateFrame(
|
| + media::VideoPixelFormat format,
|
| + const gfx::Size& coded_size,
|
| + const gfx::Rect& visible_rect,
|
| + const gfx::Size& natural_size,
|
| + base::TimeDelta timestamp) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + base::AutoLock auto_lock(lock_);
|
| +
|
| + scoped_refptr<media::VideoFrame> frame;
|
| + while (!frame.get() && !frames_.empty()) {
|
| + scoped_refptr<media::VideoFrame> pool_frame = frames_.front();
|
| + frames_.pop_front();
|
| +
|
| + if (pool_frame->format() == format &&
|
| + pool_frame->coded_size() == coded_size &&
|
| + pool_frame->visible_rect() == visible_rect &&
|
| + pool_frame->natural_size() == natural_size) {
|
| + frame = pool_frame;
|
| + frame->set_timestamp(timestamp);
|
| + frame->metadata()->Clear();
|
| + break;
|
| + }
|
| + }
|
| +
|
| + // Found no suitable frame available: Create a new one.
|
| + if (!frame.get()) {
|
| + scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
|
| + const bool result = shared_memory->CreateAndMapAnonymous(
|
| + media::VideoFrame::AllocationSize(format, coded_size));
|
| + DCHECK(result);
|
| +
|
| + frame = media::VideoFrame::WrapExternalSharedMemory(
|
| + format,
|
| + coded_size,
|
| + visible_rect,
|
| + natural_size,
|
| + static_cast<uint8_t*>(shared_memory->memory()),
|
| + shared_memory->mapped_size(),
|
| + shared_memory->handle(),
|
| + 0 /* shared_memory_offset */,
|
| + timestamp);
|
| + LOG_IF(ERROR, !frame.get()) << "Failed to create a video frame";
|
| + if (frame)
|
| + frame->AddSharedMemory(std::move(shared_memory));
|
| + }
|
| +
|
| + scoped_refptr<media::VideoFrame> wrapped_frame =
|
| + media::VideoFrame::WrapVideoFrame(frame, frame->visible_rect(),
|
| + frame->natural_size());
|
| + wrapped_frame->AddDestructionObserver(
|
| + base::Bind(&VideoFramePool::FrameReleased, this, frame));
|
| + return wrapped_frame;
|
| +}
|
| +
|
| +DeviceClientImpl::VideoFramePool::~VideoFramePool() {
|
| +}
|
| +
|
| +void DeviceClientImpl::VideoFramePool::FrameReleased(
|
| + const scoped_refptr<media::VideoFrame>& frame) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + frames_.push_back(frame);
|
| +}
|
| +
|
| +void DeviceClientImpl::OnIncomingCapturedData(
|
| + const uint8_t* data,
|
| + int length,
|
| + const media::VideoCaptureFormat& frame_format,
|
| + int clockwise_rotation,
|
| + const base::TimeTicks& timestamp) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + // N.B.: All of the logic throughout this method is lifted directly from
|
| + // content::VideoCaptureDeviceClient and should generally continue to mirror
|
| + // that implementation until this one becomes the source of truth.
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage);
|
| +
|
| + if (!frame_format.IsValid())
|
| + return;
|
| +
|
| + const int width_parity = frame_format.frame_size.width() & 1;
|
| + const int height_parity = frame_format.frame_size.height() & 1;
|
| + const int even_width = frame_format.frame_size.width() & ~1;
|
| + const int even_height = frame_format.frame_size.height() & ~1;
|
| +
|
| + int destination_width = even_width;
|
| + int destination_height = even_height;
|
| +
|
| + if (clockwise_rotation == 90 || clockwise_rotation == 270)
|
| + std::swap(destination_width, destination_height);
|
| +
|
| + DCHECK_EQ(clockwise_rotation % 90, 0)
|
| + << "Rotation must be a multiple of 90, now: " << clockwise_rotation;
|
| + libyuv::RotationMode rotation_mode = libyuv::kRotate0;
|
| + if (clockwise_rotation == 90)
|
| + rotation_mode = libyuv::kRotate90;
|
| + else if (clockwise_rotation == 180)
|
| + rotation_mode = libyuv::kRotate180;
|
| + else if (clockwise_rotation == 270)
|
| + rotation_mode = libyuv::kRotate270;
|
| +
|
| + libyuv::FourCC colorspace = libyuv::FOURCC_ANY;
|
| + bool flip = false;
|
| + switch (frame_format.pixel_format) {
|
| + case media::PIXEL_FORMAT_UNKNOWN:
|
| + break;
|
| + case media::PIXEL_FORMAT_I420:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_I420;
|
| + break;
|
| + case media::PIXEL_FORMAT_YV12:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_YV12;
|
| + break;
|
| + case media::PIXEL_FORMAT_NV12:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_NV12;
|
| + break;
|
| + case media::PIXEL_FORMAT_NV21:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_NV21;
|
| + break;
|
| + case media::PIXEL_FORMAT_YUY2:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_YUY2;
|
| + break;
|
| + case media::PIXEL_FORMAT_UYVY:
|
| + DCHECK(!width_parity && !height_parity);
|
| + colorspace = libyuv::FOURCC_UYVY;
|
| + break;
|
| + case media::PIXEL_FORMAT_RGB24:
|
| +// Linux RGB24 defines red at lowest byte address,
|
| +// see http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html.
|
| +// Windows RGB24 defines blue at lowest byte,
|
| +// see https://msdn.microsoft.com/en-us/library/windows/desktop/dd407253
|
| +#if defined(OS_LINUX)
|
| + colorspace = libyuv::FOURCC_RAW;
|
| +#elif defined(OS_WIN)
|
| + colorspace = libyuv::FOURCC_24BG;
|
| +#else
|
| + NOTREACHED() << "RGB24 is only available in Linux and Windows platforms";
|
| +#endif
|
| +#if defined(OS_WIN)
|
| + // TODO(wjia): Currently, for RGB24 on WIN, capture device always
|
| + // passes in positive src_width and src_height. Remove this hardcoded
|
| + // value when nagative src_height is supported. The negative src_height
|
| + // indicates that vertical flipping is needed.
|
| + flip = true;
|
| +#endif
|
| + break;
|
| + case media::PIXEL_FORMAT_RGB32:
|
| + // Fallback to PIXEL_FORMAT_ARGB setting |flip| in Windows
|
| + // platforms.
|
| +#if defined(OS_WIN)
|
| + flip = true;
|
| +#endif
|
| + case media::PIXEL_FORMAT_ARGB:
|
| + colorspace = libyuv::FOURCC_ARGB;
|
| + break;
|
| + case media::PIXEL_FORMAT_MJPEG:
|
| + colorspace = libyuv::FOURCC_MJPG;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| +
|
| + const gfx::Size dimensions(destination_width, destination_height);
|
| + scoped_ptr<Buffer> buffer = ReserveOutputBuffer(
|
| + dimensions, media::PIXEL_FORMAT_I420, media::PIXEL_STORAGE_CPU);
|
| + if (!buffer)
|
| + return;
|
| +
|
| + int y_stride = dimensions.width();
|
| + int uv_stride = y_stride / 2;
|
| + if (libyuv::ConvertToI420(
|
| + data, length,
|
| + static_cast<uint8_t*>(buffer->data(media::VideoFrame::kYPlane)),
|
| + y_stride,
|
| + static_cast<uint8_t*>(buffer->data(media::VideoFrame::kUPlane)),
|
| + uv_stride,
|
| + static_cast<uint8_t*>(buffer->data(media::VideoFrame::kVPlane)),
|
| + uv_stride, 0 /* crop_x */, 0 /* crop_y */,
|
| + frame_format.frame_size.width(),
|
| + (flip ? -1 : 1) * frame_format.frame_size.height(), even_width,
|
| + even_height, rotation_mode, colorspace)) {
|
| + DLOG(ERROR) << "Failed to convert frame pixels to I420";
|
| + return;
|
| + }
|
| +
|
| + OnIncomingCapturedBuffer(std::move(buffer),
|
| + media::VideoCaptureFormat(frame_format.frame_size,
|
| + 0.0f,
|
| + media::PIXEL_FORMAT_I420),
|
| + timestamp);
|
| +}
|
| +
|
| +void DeviceClientImpl::OnIncomingCapturedYuvData(
|
| + const uint8_t* y_data,
|
| + const uint8_t* u_data,
|
| + const uint8_t* v_data,
|
| + size_t y_stride,
|
| + size_t u_stride,
|
| + size_t v_stride,
|
| + const media::VideoCaptureFormat& frame_format,
|
| + int clockwise_rotation,
|
| + const base::TimeTicks& timestamp) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +scoped_ptr<media::VideoCaptureDevice::Client::Buffer>
|
| +DeviceClientImpl::ReserveOutputBuffer(
|
| + const gfx::Size& dimensions,
|
| + media::VideoPixelFormat pixel_format,
|
| + media::VideoPixelStorage pixel_storage) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_EQ(media::PIXEL_FORMAT_I420, pixel_format);
|
| + DCHECK_EQ(media::PIXEL_STORAGE_CPU, pixel_storage);
|
| + DCHECK_GT(dimensions.width(), 0);
|
| + DCHECK_GT(dimensions.height(), 0);
|
| +
|
| + return make_scoped_ptr<Buffer>(new BufferImpl(video_frame_pool_->CreateFrame(
|
| + pixel_format, dimensions, gfx::Rect(dimensions), dimensions,
|
| + base::TimeTicks::Now() - base::TimeTicks())));
|
| +}
|
| +
|
| +void DeviceClientImpl::OnIncomingCapturedBuffer(
|
| + scoped_ptr<Buffer> buffer,
|
| + const media::VideoCaptureFormat& frame_format,
|
| + const base::TimeTicks& timestamp) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(buffer);
|
| + DCHECK_EQ(frame_format.pixel_format, media::PIXEL_FORMAT_I420);
|
| + DCHECK_EQ(frame_format.pixel_storage, media::PIXEL_STORAGE_CPU);
|
| +
|
| + scoped_refptr<media::VideoFrame> frame =
|
| + static_cast<BufferImpl*>(buffer.get())->video_frame();
|
| +
|
| + OnIncomingCapturedVideoFrame(std::move(buffer), frame, timestamp);
|
| +}
|
| +
|
| +void DeviceClientImpl::OnIncomingCapturedVideoFrame(
|
| + scoped_ptr<Buffer> buffer,
|
| + const scoped_refptr<media::VideoFrame>& frame,
|
| + const base::TimeTicks& timestamp) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(buffer);
|
| + delegate_->OnFrame(frame, timestamp,
|
| + media::BindToCurrentLoop(
|
| + base::Bind(&ReleaseBuffer, base::Passed(&buffer))));
|
| +}
|
| +
|
| +void DeviceClientImpl::OnError(
|
| + const tracked_objects::Location& from_here,
|
| + const std::string& reason) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + delegate_->OnError(reason);
|
| +}
|
| +
|
| +double DeviceClientImpl::GetBufferPoolUtilization() const {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + NOTIMPLEMENTED();
|
| + // TODO(mcasas): In content/, the frame pool did have a maximum. Consider
|
| + // doing the same in this VideoFramePool, and return here the amount in use
|
| + // there.
|
| + return 0.0;
|
| +}
|
| +
|
| +} // namespace video_capture
|
|
|