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