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..abdfd3da6b22f529556fbe1f3d810eac6d0df218 |
| --- /dev/null |
| +++ b/components/video_capture/public/cpp/stream_client_impl.cc |
| @@ -0,0 +1,111 @@ |
| +// 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/bind_helpers.h" |
| +#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 { |
| + |
| +// Convenience class to wrap a SharedBufferHandle + size together and mapped. |
| +class FrameBuffer { |
| + public: |
| + FrameBuffer(mojo::ScopedSharedBufferHandle handle, size_t size) |
| + : handle_(std::move(handle)), size_(size) { |
| + DCHECK(handle_.is_valid()); |
| + const MojoResult rv = mojo::MapBuffer( |
| + handle_.get(), 0, size_, reinterpret_cast<void**>(&storage_), 0); |
|
emircan
2016/02/16 17:45:48
Check rv?
|
| + 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); |
| +}; |
| + |
| +// This function is also used to keep |buffer| alive. |
| +void RunCallback(const mojo::Closure& callback, |
| + scoped_ptr<FrameBuffer> /* buffer */) { |
|
emircan
2016/02/16 17:45:48
Can you make a note that StreamImpl::OnFrameConsum
|
| + callback.Run(); |
| +} |
| + |
| +} // namespace |
| + |
| +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) { |
| + scoped_ptr<FrameBuffer> buffer( |
| + 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); |
| + |
| + // Tie FrameAvailable message and the VideoFrame lifetime. |buffer| has the |
| + // same lifetime so its kept in the same Callback. |
| + frame->AddDestructionObserver(media::BindToCurrentLoop( |
| + base::Bind(&RunCallback, callback, base::Passed(&buffer)))); |
| +} |
| + |
| +void StreamClientImpl::Error(const mojo::String& error) { |
| + DLOG(ERROR) << error; |
| +} |
| + |
| +} // namespace video_capture |