| Index: media/capture/service/video_capture_stream_impl.cc
|
| diff --git a/media/capture/service/video_capture_stream_impl.cc b/media/capture/service/video_capture_stream_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..87a07f0190038722d288105e0e22e6883edb7fd0
|
| --- /dev/null
|
| +++ b/media/capture/service/video_capture_stream_impl.cc
|
| @@ -0,0 +1,112 @@
|
| +// 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 "media/capture/service/video_capture_stream_impl.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| +#include "base/thread_task_runner_handle.h"
|
| +#include "base/time/time.h"
|
| +#include "media/base/bind_to_current_loop.h"
|
| +#include "media/capture/interfaces/video_capture.mojom.h"
|
| +#include "media/mojo/common/mojo_shared_buffer_video_frame.h"
|
| +#include "mojo/converters/geometry/geometry_type_converters.h"
|
| +
|
| +namespace media {
|
| +
|
| +VideoCaptureStreamImpl::VideoCaptureStreamImpl(
|
| + mojo::InterfaceRequest<mojom::VideoCaptureStream> request,
|
| + const base::Closure& stop_callback,
|
| + const base::Closure& error_callback)
|
| + : binding_(this, std::move(request)),
|
| + stop_callback_(stop_callback),
|
| + error_callback_(error_callback),
|
| + task_runner_(base::ThreadTaskRunnerHandle::Get()),
|
| + weak_factory_(this) {
|
| + binding_.set_connection_error_handler(error_callback_);
|
| +}
|
| +
|
| +VideoCaptureStreamImpl::~VideoCaptureStreamImpl() {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::Start(mojom::VideoCaptureStreamClientPtr client) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + DCHECK(!client_) << "Start() should not be called several times.";
|
| + client_ = std::move(client);
|
| + client_.set_connection_error_handler(error_callback_);
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::Stop() {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + DCHECK(client_);
|
| + stop_callback_.Run();
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::OnFrame(
|
| + const scoped_refptr<MojoSharedBufferVideoFrame>& frame,
|
| + const base::TimeTicks& timestamp) {
|
| + task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&VideoCaptureStreamImpl::NotifyOnFrameAvailable,
|
| + weak_factory_.GetWeakPtr(), frame, timestamp));
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::OnError(const std::string& reason) {
|
| + DLOG(ERROR) << __FUNCTION__ << " " << reason;
|
| + task_runner_->PostTask(FROM_HERE,
|
| + base::Bind(&VideoCaptureStreamImpl::NotifyError,
|
| + weak_factory_.GetWeakPtr(), reason));
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::NotifyOnFrameAvailable(
|
| + const scoped_refptr<MojoSharedBufferVideoFrame>& frame,
|
| + const base::TimeTicks& timestamp) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + DCHECK(client_);
|
| +
|
| + const int32_t buffer_id = running_buffer_id_++;
|
| +
|
| + mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New();
|
| +
|
| + // We duplicate the handle immediately to avoid passing around the primordial
|
| + // handle. While it's OK for someone to continue using the buffer after the
|
| + // pool drops ownership, it's not OK to dupe the primordial handle once it's
|
| + // been invalidated.
|
| + mojo::ScopedSharedBufferHandle handle;
|
| + MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle);
|
| + DLOG_IF(ERROR, rv != MOJO_RESULT_OK) << "Error duplicating handle";
|
| + if (rv != MOJO_RESULT_OK)
|
| + return;
|
| + frame_info->storage_handle = std::move(handle);
|
| + frame_info->storage_size = frame->mapped_size();
|
| +
|
| + frame_info->pixel_format =
|
| + static_cast<media::interfaces::VideoFormat>(frame->format());
|
| + frame_info->coded_size = mojo::Size::From(frame->coded_size());
|
| + frame_info->visible_rect = mojo::Rect::From(frame->visible_rect());
|
| + frame_info->timestamp = timestamp.ToInternalValue();
|
| +
|
| + in_use_video_frames_.insert(std::make_pair(buffer_id, frame));
|
| + client_->OnFrameAvailable(std::move(frame_info),
|
| + base::Bind(&VideoCaptureStreamImpl::OnFrameConsumed,
|
| + weak_factory_.GetWeakPtr(), buffer_id));
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::NotifyError(const mojo::String& error) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + // A device may notify of error(s) before Start() registers a |client_|.
|
| + if (client_)
|
| + client_->OnError(error);
|
| + error_callback_.Run();
|
| +}
|
| +
|
| +void VideoCaptureStreamImpl::OnFrameConsumed(int32_t buffer_id) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + const auto video_frame = in_use_video_frames_.find(buffer_id);
|
| + DCHECK(video_frame != in_use_video_frames_.end()) << "Buffer id " << buffer_id
|
| + << " not found!";
|
| + in_use_video_frames_.erase(video_frame);
|
| +}
|
| +
|
| +} // namespace media
|
|
|