| 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/macros.h" |
| 10 #include "base/time/time.h" |
| 11 #include "media/base/bind_to_current_loop.h" |
| 12 #include "media/base/video_capture_types.h" |
| 13 #include "media/base/video_frame.h" |
| 14 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 15 #include "mojo/public/cpp/bindings/interface_request.h" |
| 16 #include "mojo/public/cpp/system/buffer.h" |
| 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/gfx/geometry/size.h" |
| 19 |
| 20 namespace video_capture { |
| 21 |
| 22 namespace { |
| 23 |
| 24 void RunMojoClosure(const mojo::Closure& callback) { |
| 25 callback.Run(); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 class StreamClientImpl::FrameBuffer { |
| 31 public: |
| 32 FrameBuffer(mojo::ScopedSharedBufferHandle handle, size_t size) |
| 33 : handle_(std::move(handle)), size_(size) { |
| 34 DCHECK(handle_.is_valid()); |
| 35 MojoResult rv = mojo::MapBuffer(handle_.get(), 0, size_, |
| 36 reinterpret_cast<void**>(&storage_), 0); |
| 37 DCHECK_EQ(rv, MOJO_RESULT_OK); |
| 38 DCHECK(storage_); |
| 39 } |
| 40 |
| 41 ~FrameBuffer() { |
| 42 DCHECK(storage_); |
| 43 mojo::UnmapBuffer(storage_); |
| 44 } |
| 45 |
| 46 uint8_t* storage() const { return storage_; } |
| 47 size_t size() const { return size_; } |
| 48 |
| 49 private: |
| 50 const mojo::ScopedSharedBufferHandle handle_; |
| 51 const size_t size_ = 0; |
| 52 uint8_t* storage_ = nullptr; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(FrameBuffer); |
| 55 }; |
| 56 |
| 57 StreamClientImpl::StreamClientImpl(mojom::StreamPtr stream) |
| 58 : binding_(this), stream_(std::move(stream)) { |
| 59 mojom::StreamClientPtr client; |
| 60 binding_.Bind(GetProxy(&client)); |
| 61 stream_->Start(std::move(client)); |
| 62 } |
| 63 |
| 64 StreamClientImpl::~StreamClientImpl() { |
| 65 stream_->Stop(); |
| 66 } |
| 67 |
| 68 void StreamClientImpl::AddOnFrameReceivedCallback( |
| 69 const OnFrameReceivedCallback& callback) { |
| 70 on_frame_received_callbacks_.push_back(callback); |
| 71 } |
| 72 |
| 73 void StreamClientImpl::FrameAvailable(mojom::FrameInfoPtr info, |
| 74 const FrameAvailableCallback& callback) { |
| 75 |
| 76 scoped_ptr<FrameBuffer> buffer( |
| 77 new FrameBuffer(std::move(info->storage_handle), |
| 78 static_cast<size_t>(info->storage_size))); |
| 79 const base::TimeTicks timestamp = |
| 80 base::TimeTicks::FromInternalValue(info->timestamp); |
| 81 if (first_frame_time_.is_null()) |
| 82 first_frame_time_ = timestamp; |
| 83 |
| 84 const gfx::Size frame_size = info->coded_size.To<gfx::Size>(); |
| 85 |
| 86 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapExternalData( |
| 87 static_cast<media::VideoPixelFormat>(info->pixel_format), |
| 88 frame_size, |
| 89 info->visible_rect.To<gfx::Rect>(), |
| 90 frame_size, |
| 91 buffer->storage(), |
| 92 buffer->size(), |
| 93 timestamp - first_frame_time_); |
| 94 |
| 95 for (const auto& on_frame_received_callback : on_frame_received_callbacks_) |
| 96 on_frame_received_callback.Run(frame); |
| 97 |
| 98 // Ensure that we respond to the FrameAvailable message as soon as the |
| 99 // VideoFrame is no longer in use. |
| 100 frame->AddDestructionObserver( |
| 101 media::BindToCurrentLoop(base::Bind(&RunMojoClosure, callback))); |
| 102 } |
| 103 |
| 104 void StreamClientImpl::Error(const mojo::String& error) { |
| 105 DLOG(ERROR) << error; |
| 106 } |
| 107 |
| 108 } // namespace video_capture |
| OLD | NEW |