Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/video_capture/public/cpp/stream_client_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind_helpers.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "media/base/bind_to_current_loop.h" | |
| 13 #include "media/base/video_capture_types.h" | |
| 14 #include "media/base/video_frame.h" | |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 16 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 17 #include "mojo/public/cpp/system/buffer.h" | |
| 18 #include "ui/gfx/geometry/rect.h" | |
| 19 #include "ui/gfx/geometry/size.h" | |
| 20 | |
| 21 namespace video_capture { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Convenience class to wrap a SharedBufferHandle + size together and mapped. | |
| 26 class FrameBuffer { | |
| 27 public: | |
| 28 FrameBuffer(mojo::ScopedSharedBufferHandle handle, size_t size) | |
| 29 : handle_(std::move(handle)), size_(size) { | |
| 30 DCHECK(handle_.is_valid()); | |
| 31 const MojoResult rv = mojo::MapBuffer( | |
| 32 handle_.get(), 0, size_, reinterpret_cast<void**>(&storage_), 0); | |
|
emircan
2016/02/16 17:45:48
Check rv?
| |
| 33 DCHECK_EQ(rv, MOJO_RESULT_OK); | |
| 34 DCHECK(storage_); | |
| 35 } | |
| 36 | |
| 37 ~FrameBuffer() { | |
| 38 DCHECK(storage_); | |
| 39 mojo::UnmapBuffer(storage_); | |
| 40 } | |
| 41 | |
| 42 uint8_t* storage() const { return storage_; } | |
| 43 size_t size() const { return size_; } | |
| 44 | |
| 45 private: | |
| 46 const mojo::ScopedSharedBufferHandle handle_; | |
| 47 const size_t size_ = 0; | |
| 48 uint8_t* storage_ = nullptr; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(FrameBuffer); | |
| 51 }; | |
| 52 | |
| 53 // This function is also used to keep |buffer| alive. | |
| 54 void RunCallback(const mojo::Closure& callback, | |
| 55 scoped_ptr<FrameBuffer> /* buffer */) { | |
|
emircan
2016/02/16 17:45:48
Can you make a note that StreamImpl::OnFrameConsum
| |
| 56 callback.Run(); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 StreamClientImpl::StreamClientImpl(mojom::StreamPtr stream) | |
| 62 : binding_(this), stream_(std::move(stream)) { | |
| 63 mojom::StreamClientPtr client; | |
| 64 binding_.Bind(GetProxy(&client)); | |
| 65 stream_->Start(std::move(client)); | |
| 66 } | |
| 67 | |
| 68 StreamClientImpl::~StreamClientImpl() { | |
| 69 stream_->Stop(); | |
| 70 } | |
| 71 | |
| 72 void StreamClientImpl::AddOnFrameReceivedCallback( | |
| 73 const OnFrameReceivedCallback& callback) { | |
| 74 on_frame_received_callbacks_.push_back(callback); | |
| 75 } | |
| 76 | |
| 77 void StreamClientImpl::FrameAvailable(mojom::FrameInfoPtr info, | |
| 78 const FrameAvailableCallback& callback) { | |
| 79 scoped_ptr<FrameBuffer> buffer( | |
| 80 new FrameBuffer(std::move(info->storage_handle), | |
| 81 static_cast<size_t>(info->storage_size))); | |
| 82 const base::TimeTicks timestamp = | |
| 83 base::TimeTicks::FromInternalValue(info->timestamp); | |
| 84 if (first_frame_time_.is_null()) | |
| 85 first_frame_time_ = timestamp; | |
| 86 | |
| 87 const gfx::Size frame_size = info->coded_size.To<gfx::Size>(); | |
| 88 | |
| 89 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapExternalData( | |
| 90 static_cast<media::VideoPixelFormat>(info->pixel_format), | |
| 91 frame_size, | |
| 92 info->visible_rect.To<gfx::Rect>(), | |
| 93 frame_size, | |
| 94 buffer->storage(), | |
| 95 buffer->size(), | |
| 96 timestamp - first_frame_time_); | |
| 97 | |
| 98 for (const auto& on_frame_received_callback : on_frame_received_callbacks_) | |
| 99 on_frame_received_callback.Run(frame); | |
| 100 | |
| 101 // Tie FrameAvailable message and the VideoFrame lifetime. |buffer| has the | |
| 102 // same lifetime so its kept in the same Callback. | |
| 103 frame->AddDestructionObserver(media::BindToCurrentLoop( | |
| 104 base::Bind(&RunCallback, callback, base::Passed(&buffer)))); | |
| 105 } | |
| 106 | |
| 107 void StreamClientImpl::Error(const mojo::String& error) { | |
| 108 DLOG(ERROR) << error; | |
| 109 } | |
| 110 | |
| 111 } // namespace video_capture | |
| OLD | NEW |