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

Unified Diff: media/capture/service/stream_impl.cc

Issue 1699553002: Mojo Video Capture service in media/capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: emircan@ comments 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 side-by-side diff with in-line comments
Download patch
Index: media/capture/service/stream_impl.cc
diff --git a/media/capture/service/stream_impl.cc b/media/capture/service/stream_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f346f00a67975ca3602a1fb5b0e80bc70d6a9877
--- /dev/null
+++ b/media/capture/service/stream_impl.cc
@@ -0,0 +1,116 @@
+// 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/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/capture/service/mojo_video_frame.h"
+#include "mojo/converters/geometry/geometry_type_converters.h"
+
+namespace media {
+
+StreamImpl::StreamImpl(mojo::InterfaceRequest<mojom::Stream> request,
+ const base::Closure& start_callback,
+ const base::Closure& close_callback,
+ const base::Closure& error_callback)
+ : binding_(this, std::move(request)),
+ start_callback_(start_callback),
+ close_callback_(close_callback),
+ error_callback_(error_callback),
+ task_runner_(base::ThreadTaskRunnerHandle::Get()),
+ weak_factory_(this) {
+ binding_.set_connection_error_handler(error_callback_);
+}
+
+StreamImpl::~StreamImpl() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+}
+
+void StreamImpl::Start(mojom::StreamClientPtr 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_);
+ start_callback_.Run();
+}
+
+void StreamImpl::Stop() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ DCHECK(client_);
+ close_callback_.Run();
+}
+
+void StreamImpl::OnFrame(const scoped_refptr<MojoVideoFrame>& frame,
perkj_chrome 2016/03/03 13:12:41 This is an implementation of VideoCaptureDeviceCli
mcasas 2016/03/03 18:41:15 The sequence replicates the current ownership grap
+ const base::TimeTicks& timestamp) {
+ task_runner_->PostTask(
+ FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable,
+ weak_factory_.GetWeakPtr(), frame, timestamp));
perkj_chrome 2016/03/03 13:12:41 nit: you can cash a weak ptr in the ctor if you wa
mcasas 2016/03/03 18:41:15 The created WeakPtr is passed to the bound Callba
+}
+
+void StreamImpl::OnError(const std::string& reason) {
+ DLOG(ERROR) << __FUNCTION__ << " " << reason;
+ task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason));
+}
+
+void StreamImpl::NotifyFrameAvailable(
+ const scoped_refptr<MojoVideoFrame>& 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_->FrameAvailable(std::move(frame_info),
+ base::Bind(&StreamImpl::OnFrameConsumed,
+ weak_factory_.GetWeakPtr(), buffer_id));
perkj_chrome 2016/03/03 13:12:41 dito- you can use a cached weakptr here if you wan
mcasas 2016/03/03 18:41:15 Acknowledged.
+}
+
+void StreamImpl::NotifyError(const mojo::String& error) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ // A device may notify of error(s) before Start() registers a |client_|.
+ if (client_)
+ client_->Error(error);
+ error_callback_.Run();
+}
+
+void StreamImpl::OnFrameConsumed(int32_t buffer_id) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ const auto video_frame = in_use_video_frames_.find(buffer_id);
+ if (video_frame == in_use_video_frames_.end()) {
perkj_chrome 2016/03/03 13:12:41 DCHECK this?
mcasas 2016/03/03 18:41:15 Done.
+ DLOG(ERROR) << "Buffer id " << buffer_id << " not found!";
+ return;
+ }
+ in_use_video_frames_.erase(video_frame);
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698