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

Unified Diff: components/video_capture/public/cpp/stream_client_impl.cc

Issue 1637793004: [WIP] Mojo Video Capture thingy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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: components/video_capture/public/cpp/stream_client_impl.cc
diff --git a/components/video_capture/public/cpp/stream_client_impl.cc b/components/video_capture/public/cpp/stream_client_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aa720cf323e49832925a78bb57c844cb18c1f385
--- /dev/null
+++ b/components/video_capture/public/cpp/stream_client_impl.cc
@@ -0,0 +1,113 @@
+// 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/public/cpp/stream_client_impl.h"
+
+#include <utility>
+
+#include "base/macros.h"
+#include "base/time/time.h"
+#include "media/base/bind_to_current_loop.h"
+#include "media/base/video_capture_types.h"
+#include "media/base/video_frame.h"
+#include "mojo/converters/geometry/geometry_type_converters.h"
+#include "mojo/public/cpp/bindings/interface_request.h"
+#include "mojo/public/cpp/system/buffer.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/size.h"
+
+namespace video_capture {
+
+namespace {
+
+void RunMojoClosure(const mojo::Closure& callback) {
+ callback.Run();
+}
+
+} // namespace
+
+class StreamClientImpl::FrameBuffer {
+ public:
+ FrameBuffer(mojo::ScopedSharedBufferHandle handle, size_t size)
+ : handle_(std::move(handle)), size_(size) {
+ DCHECK(handle_.is_valid());
+ MojoResult rv = mojo::MapBuffer(handle_.get(), 0, size_,
+ reinterpret_cast<void**>(&storage_), 0);
+ DCHECK_EQ(rv, MOJO_RESULT_OK);
+ DCHECK(storage_);
+ }
+
+ ~FrameBuffer() {
+ DCHECK(storage_);
+ mojo::UnmapBuffer(storage_);
+ }
+
+ uint8_t* storage() const { return storage_; }
+ size_t size() const { return size_; }
+
+ private:
+ mojo::ScopedSharedBufferHandle handle_;
+ size_t size_ = 0;
+ uint8_t* storage_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(FrameBuffer);
+};
+
+StreamClientImpl::StreamClientImpl(mojom::StreamPtr stream)
+ : binding_(this), stream_(std::move(stream)) {
+ mojom::StreamClientPtr client;
+ binding_.Bind(GetProxy(&client));
+ stream_->Start(std::move(client));
+}
+
+StreamClientImpl::~StreamClientImpl() {}
+
+void StreamClientImpl::AddFrameCallback(const FrameCallback& callback) {
+ frame_callbacks_.push_back(callback);
+}
+
+void StreamClientImpl::BufferCreated(mojom::FrameBufferInfoPtr info) {
+ DCHECK_GE(info->size, 0);
+ scoped_ptr<FrameBuffer> buffer(new FrameBuffer(
+ std::move(info->storage), static_cast<size_t>(info->size)));
+ auto result = buffer_map_.insert(std::make_pair(info->id, std::move(buffer)));
+ DCHECK(result.second) << "Unexpected duplicate buffer ID.";
+}
+
+void StreamClientImpl::BufferDestroyed(int32_t buffer_id) {
+ auto iter = buffer_map_.find(buffer_id);
+ DCHECK(iter != buffer_map_.end()) << "Received unknown buffer ID.";
+}
+
+void StreamClientImpl::FrameAvailable(mojom::FrameInfoPtr info,
+ const FrameAvailableCallback& callback) {
+ auto buffer_iter = buffer_map_.find(info->buffer_id);
+ DCHECK(buffer_iter != buffer_map_.end()) << "Received unknown buffer ID.";
+
+ base::TimeTicks timestamp =
+ base::TimeTicks::FromInternalValue(info->timestamp);
+ if (!first_frame_received_) {
+ first_frame_received_ = true;
+ first_frame_time_ = timestamp;
+ }
+
+ FrameBuffer* buffer = buffer_iter->second.get();
+ DCHECK(buffer);
+
+ gfx::Size frame_size = info->coded_size.To<gfx::Size>();
+ scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapExternalData(
+ static_cast<media::VideoPixelFormat>(info->pixel_format), frame_size,
+ info->visible_rect.To<gfx::Rect>(), frame_size, buffer->storage(),
+ buffer->size(), timestamp - first_frame_time_);
+
+ for (const auto& callback : frame_callbacks_)
+ callback.Run(frame);
+
+ // Ensure that we respond to the FrameAvailable message as soon as the
+ // VideoFrame is no longer in use.
+ frame->AddDestructionObserver(
+ media::BindToCurrentLoop(base::Bind(&RunMojoClosure, callback)));
+}
+
+} // namespace video_capture

Powered by Google App Engine
This is Rietveld 408576698