| 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/stream_impl.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/time/time.h" |
| 13 #include "components/video_capture/public/interfaces/video_capture.mojom.h" |
| 14 #include "media/base/video_frame.h" |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 16 |
| 17 namespace video_capture { |
| 18 |
| 19 StreamImpl::StreamImpl( |
| 20 mojo::InterfaceRequest<mojom::Stream> request, |
| 21 const base::Closure& start_callback, |
| 22 const base::Closure& close_callback) |
| 23 : binding_(this, std::move(request)), |
| 24 start_callback_(start_callback), |
| 25 close_callback_(close_callback), |
| 26 weak_factory_(this) { |
| 27 binding_.set_connection_error_handler(close_callback_); |
| 28 } |
| 29 |
| 30 StreamImpl::~StreamImpl() { |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); |
| 32 // Ensure we release any frames about which we've been notified. |
| 33 std::map<int32_t, base::Closure> release_callbacks; |
| 34 release_callbacks.swap(pending_release_callbacks_); |
| 35 for (const auto& callback : release_callbacks) |
| 36 callback.second.Run(); |
| 37 } |
| 38 |
| 39 void StreamImpl::OnFrame(const scoped_refptr<media::VideoFrame>& frame, |
| 40 const base::TimeTicks& timestamp, |
| 41 const base::Closure& release_callback) { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 int32_t buffer_id = 0; |
| 44 NotifyFrameAvailable(buffer_id, frame, timestamp, release_callback); |
| 45 } |
| 46 |
| 47 void StreamImpl::OnError(const std::string& reason) { |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 // TODO(mcasas): Notify our owner so they can stop capture. |
| 50 // TODO(mcasas): Notify somehow the client? |
| 51 } |
| 52 |
| 53 void StreamImpl::Start(mojom::StreamClientPtr client) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 DCHECK(!client_) << "Start() should not be called several times."; |
| 56 DCHECK(!paused_); |
| 57 client_ = std::move(client); |
| 58 client_.set_connection_error_handler(close_callback_); |
| 59 start_callback_.Run(); |
| 60 } |
| 61 |
| 62 void StreamImpl::Pause() { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 DCHECK(client_); |
| 65 paused_ = true; |
| 66 } |
| 67 |
| 68 void StreamImpl::Resume() { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 DCHECK(client_); |
| 71 paused_ = false; |
| 72 } |
| 73 |
| 74 void StreamImpl::NotifyBufferCreated(int32_t id, |
| 75 mojo::ScopedSharedBufferHandle handle, |
| 76 size_t size) { |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 78 DCHECK(client_); |
| 79 mojom::FrameBufferInfoPtr info = mojom::FrameBufferInfo::New(); |
| 80 info->id = id; |
| 81 info->storage = std::move(handle); |
| 82 info->size = static_cast<int32_t>(size); |
| 83 client_->BufferCreated(std::move(info)); |
| 84 } |
| 85 |
| 86 void StreamImpl::NotifyBufferDestroyed(int32_t id) { |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 DCHECK(client_); |
| 89 client_->BufferDestroyed(id); |
| 90 } |
| 91 |
| 92 void StreamImpl::NotifyFrameAvailable( |
| 93 int32_t buffer_id, |
| 94 const scoped_refptr<media::VideoFrame>& frame, |
| 95 const base::TimeTicks& timestamp, |
| 96 const base::Closure& release_callback) { |
| 97 DVLOG(1) << __FUNCTION__; |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 DCHECK(client_); |
| 100 |
| 101 if (paused_) { |
| 102 // Drop the frame immediately if we aren't playing. |
| 103 release_callback.Run(); |
| 104 return; |
| 105 } |
| 106 DVLOG(1) << "miguelao 1"; |
| 107 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New(); |
| 108 frame_info->buffer_id = buffer_id; |
| 109 frame_info->pixel_format = |
| 110 static_cast<media::interfaces::VideoFormat>(frame->format()); |
| 111 frame_info->coded_size = mojo::Size::From(frame->coded_size()); |
| 112 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect()); |
| 113 frame_info->timestamp = timestamp.ToInternalValue(); |
| 114 |
| 115 pending_release_callbacks_.insert( |
| 116 std::make_pair(buffer_id, release_callback)); |
| 117 |
| 118 DVLOG(1) << "miguelao 2"; |
| 119 client_->FrameAvailable(std::move(frame_info), |
| 120 base::Bind(&StreamImpl::OnFrameConsumed, |
| 121 weak_factory_.GetWeakPtr(), buffer_id)); |
| 122 } |
| 123 |
| 124 void StreamImpl::OnFrameConsumed(int32_t buffer_id) { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 DCHECK(ContainsKey(pending_release_callbacks_, buffer_id)); |
| 127 |
| 128 auto iter = pending_release_callbacks_.find(buffer_id); |
| 129 base::Closure release_callback = iter->second; |
| 130 pending_release_callbacks_.erase(iter); |
| 131 release_callback.Run(); |
| 132 } |
| 133 |
| 134 } // namespace video_capture |
| OLD | NEW |