| 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 "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/stl_util.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" |
| 12 #include "components/video_capture/mojo_video_frame.h" |
| 13 #include "components/video_capture/public/interfaces/video_capture.mojom.h" |
| 14 #include "media/base/bind_to_current_loop.h" |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 16 |
| 17 namespace video_capture { |
| 18 |
| 19 StreamImpl::StreamImpl(mojo::InterfaceRequest<mojom::Stream> request, |
| 20 const base::Closure& start_callback, |
| 21 const base::Closure& close_callback, |
| 22 const base::Closure& error_callback) |
| 23 : binding_(this, std::move(request)), |
| 24 start_callback_(start_callback), |
| 25 close_callback_(close_callback), |
| 26 error_callback_(error_callback), |
| 27 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 28 weak_factory_(this) { |
| 29 binding_.set_connection_error_handler(error_callback_); |
| 30 } |
| 31 |
| 32 StreamImpl::~StreamImpl() { |
| 33 DVLOG(1) << __FUNCTION__; |
| 34 DCHECK(false); |
| 35 DCHECK(client_); |
| 36 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 37 } |
| 38 |
| 39 void StreamImpl::Start(mojom::StreamClientPtr client) { |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 41 DCHECK(!client_) << "Start() should not be called several times."; |
| 42 DCHECK(!paused_); |
| 43 client_ = std::move(client); |
| 44 client_.set_connection_error_handler(error_callback_); |
| 45 start_callback_.Run(); |
| 46 } |
| 47 |
| 48 void StreamImpl::Pause() { |
| 49 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 50 DCHECK(client_); |
| 51 paused_ = true; |
| 52 } |
| 53 |
| 54 void StreamImpl::Resume() { |
| 55 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 56 DCHECK(client_); |
| 57 paused_ = false; |
| 58 } |
| 59 |
| 60 void StreamImpl::Stop() { |
| 61 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 62 DCHECK(client_); |
| 63 close_callback_.Run(); |
| 64 } |
| 65 |
| 66 void StreamImpl::OnFrame(const scoped_refptr<MojoVideoFrame>& frame, |
| 67 const base::TimeTicks& timestamp) { |
| 68 DVLOG(1) << __FUNCTION__; |
| 69 task_runner_->PostTask( |
| 70 FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable, |
| 71 weak_factory_.GetWeakPtr(), frame, timestamp)); |
| 72 } |
| 73 |
| 74 void StreamImpl::OnError(const std::string& reason) { |
| 75 DLOG(ERROR) << __FUNCTION__ << " " << reason; |
| 76 task_runner_->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason)); |
| 79 } |
| 80 |
| 81 void StreamImpl::NotifyFrameAvailable( |
| 82 const scoped_refptr<MojoVideoFrame>& frame, |
| 83 const base::TimeTicks& timestamp) { |
| 84 DVLOG(1) << __FUNCTION__; |
| 85 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 86 DCHECK(client_); |
| 87 |
| 88 if (paused_) // Drop the frame immediately if we aren't playing. |
| 89 return; |
| 90 |
| 91 const int32_t buffer_id = running_buffer_id_++; |
| 92 |
| 93 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New(); |
| 94 frame_info->buffer_id = buffer_id; |
| 95 |
| 96 // We duplicate the handle immediately to avoid passing around the primordial |
| 97 // handle. While it's OK for someone to continue using the buffer after the |
| 98 // pool drops ownership, it's not OK to dupe the primordial handle once it's |
| 99 // been invalidated. |
| 100 mojo::ScopedSharedBufferHandle handle; |
| 101 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle); |
| 102 DCHECK_EQ(rv, MOJO_RESULT_OK); |
| 103 frame_info->storage_handle = std::move(handle); |
| 104 frame_info->storage_size = frame->mapped_size(); |
| 105 |
| 106 frame_info->pixel_format = |
| 107 static_cast<media::interfaces::VideoFormat>(frame->format()); |
| 108 frame_info->coded_size = mojo::Size::From(frame->coded_size()); |
| 109 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect()); |
| 110 frame_info->timestamp = timestamp.ToInternalValue(); |
| 111 |
| 112 in_use_video_frames_.insert(std::make_pair(buffer_id, frame)); |
| 113 |
| 114 client_->FrameAvailable(std::move(frame_info), |
| 115 base::Bind(&StreamImpl::OnFrameConsumed, |
| 116 weak_factory_.GetWeakPtr(), buffer_id)); |
| 117 } |
| 118 |
| 119 void StreamImpl::NotifyError(const mojo::String& error) { |
| 120 DVLOG(1) << __FUNCTION__; |
| 121 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 122 DCHECK(client_); |
| 123 client_->Error(error); |
| 124 error_callback_.Run(); |
| 125 } |
| 126 |
| 127 void StreamImpl::OnFrameConsumed(int32_t buffer_id) { |
| 128 DVLOG(1) << __FUNCTION__; |
| 129 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 130 if (!ContainsKey(in_use_video_frames_, buffer_id)) { |
| 131 DLOG(ERROR) << "Buffer id " << buffer_id << " not found!"; |
| 132 return; |
| 133 } |
| 134 in_use_video_frames_.erase(in_use_video_frames_.find(buffer_id)); |
| 135 } |
| 136 |
| 137 } // namespace video_capture |
| OLD | NEW |