Chromium Code Reviews| 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..bcde7400c51cc84c56dbe383d92ba15b8a022281 |
| --- /dev/null |
| +++ b/components/video_capture/public/cpp/stream_client_impl.cc |
| @@ -0,0 +1,108 @@ |
| +// 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: |
| + const mojo::ScopedSharedBufferHandle handle_; |
| + const 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() { |
| + stream_->Stop(); |
| +} |
| + |
| +void StreamClientImpl::AddOnFrameReceivedCallback( |
| + const OnFrameReceivedCallback& callback) { |
| + on_frame_received_callbacks_.push_back(callback); |
| +} |
| + |
| +void StreamClientImpl::FrameAvailable(mojom::FrameInfoPtr info, |
| + const FrameAvailableCallback& callback) { |
|
Ken Rockot(use gerrit already)
2016/02/09 20:41:19
nit: formatting
mcasas
2016/02/11 23:51:32
Done.
|
| + |
| + scoped_ptr<FrameBuffer> buffer( |
|
Ken Rockot(use gerrit already)
2016/02/09 20:41:19
this is going to go out of scope at the end of the
mcasas
2016/02/11 23:51:32
I tried keeping it alive together with the (rename
|
| + new FrameBuffer(std::move(info->storage_handle), |
| + static_cast<size_t>(info->storage_size))); |
| + const base::TimeTicks timestamp = |
| + base::TimeTicks::FromInternalValue(info->timestamp); |
| + if (first_frame_time_.is_null()) |
| + first_frame_time_ = timestamp; |
| + |
| + const 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& on_frame_received_callback : on_frame_received_callbacks_) |
| + on_frame_received_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))); |
| +} |
| + |
| +void StreamClientImpl::Error(const mojo::String& error) { |
| + DLOG(ERROR) << error; |
| +} |
| + |
| +} // namespace video_capture |