| Index: components/video_capture/stream_impl.cc
|
| diff --git a/components/video_capture/stream_impl.cc b/components/video_capture/stream_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..03ce2126ce23c3da2c1eb44a1ae517a556a26171
|
| --- /dev/null
|
| +++ b/components/video_capture/stream_impl.cc
|
| @@ -0,0 +1,134 @@
|
| +// 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/stream_impl.h"
|
| +
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| +#include "base/stl_util.h"
|
| +#include "base/time/time.h"
|
| +#include "components/video_capture/public/interfaces/video_capture.mojom.h"
|
| +#include "media/base/video_frame.h"
|
| +#include "mojo/converters/geometry/geometry_type_converters.h"
|
| +
|
| +namespace video_capture {
|
| +
|
| +StreamImpl::StreamImpl(
|
| + mojo::InterfaceRequest<mojom::Stream> request,
|
| + const base::Closure& start_callback,
|
| + const base::Closure& close_callback)
|
| + : binding_(this, std::move(request)),
|
| + start_callback_(start_callback),
|
| + close_callback_(close_callback),
|
| + weak_factory_(this) {
|
| + binding_.set_connection_error_handler(close_callback_);
|
| +}
|
| +
|
| +StreamImpl::~StreamImpl() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + // Ensure we release any frames about which we've been notified.
|
| + std::map<int32_t, base::Closure> release_callbacks;
|
| + release_callbacks.swap(pending_release_callbacks_);
|
| + for (const auto& callback : release_callbacks)
|
| + callback.second.Run();
|
| +}
|
| +
|
| +void StreamImpl::OnFrame(const scoped_refptr<media::VideoFrame>& frame,
|
| + const base::TimeTicks& timestamp,
|
| + const base::Closure& release_callback) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + int32_t buffer_id = 0;
|
| + NotifyFrameAvailable(buffer_id, frame, timestamp, release_callback);
|
| +}
|
| +
|
| +void StreamImpl::OnError(const std::string& reason) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + // TODO(mcasas): Notify our owner so they can stop capture.
|
| + // TODO(mcasas): Notify somehow the client?
|
| +}
|
| +
|
| +void StreamImpl::Start(mojom::StreamClientPtr client) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(!client_) << "Start() should not be called several times.";
|
| + DCHECK(!paused_);
|
| + client_ = std::move(client);
|
| + client_.set_connection_error_handler(close_callback_);
|
| + start_callback_.Run();
|
| +}
|
| +
|
| +void StreamImpl::Pause() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(client_);
|
| + paused_ = true;
|
| +}
|
| +
|
| +void StreamImpl::Resume() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(client_);
|
| + paused_ = false;
|
| +}
|
| +
|
| +void StreamImpl::NotifyBufferCreated(int32_t id,
|
| + mojo::ScopedSharedBufferHandle handle,
|
| + size_t size) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(client_);
|
| + mojom::FrameBufferInfoPtr info = mojom::FrameBufferInfo::New();
|
| + info->id = id;
|
| + info->storage = std::move(handle);
|
| + info->size = static_cast<int32_t>(size);
|
| + client_->BufferCreated(std::move(info));
|
| +}
|
| +
|
| +void StreamImpl::NotifyBufferDestroyed(int32_t id) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(client_);
|
| + client_->BufferDestroyed(id);
|
| +}
|
| +
|
| +void StreamImpl::NotifyFrameAvailable(
|
| + int32_t buffer_id,
|
| + const scoped_refptr<media::VideoFrame>& frame,
|
| + const base::TimeTicks& timestamp,
|
| + const base::Closure& release_callback) {
|
| + DVLOG(1) << __FUNCTION__;
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(client_);
|
| +
|
| + if (paused_) {
|
| + // Drop the frame immediately if we aren't playing.
|
| + release_callback.Run();
|
| + return;
|
| + }
|
| + DVLOG(1) << "miguelao 1";
|
| + mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New();
|
| + frame_info->buffer_id = buffer_id;
|
| + 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();
|
| +
|
| + pending_release_callbacks_.insert(
|
| + std::make_pair(buffer_id, release_callback));
|
| +
|
| + DVLOG(1) << "miguelao 2";
|
| + client_->FrameAvailable(std::move(frame_info),
|
| + base::Bind(&StreamImpl::OnFrameConsumed,
|
| + weak_factory_.GetWeakPtr(), buffer_id));
|
| +}
|
| +
|
| +void StreamImpl::OnFrameConsumed(int32_t buffer_id) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(ContainsKey(pending_release_callbacks_, buffer_id));
|
| +
|
| + auto iter = pending_release_callbacks_.find(buffer_id);
|
| + base::Closure release_callback = iter->second;
|
| + pending_release_callbacks_.erase(iter);
|
| + release_callback.Run();
|
| +}
|
| +
|
| +} // namespace video_capture
|
|
|