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/video_capture_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/mojo/common/mojo_shared_buffer_video_frame.h" |
| 14 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 VideoCaptureStreamImpl::VideoCaptureStreamImpl( |
| 19 mojo::InterfaceRequest<mojom::VideoCaptureStream> request, |
| 20 const base::Closure& stop_callback, |
| 21 const base::Closure& error_callback) |
| 22 : binding_(this, std::move(request)), |
| 23 stop_callback_(stop_callback), |
| 24 error_callback_(error_callback), |
| 25 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 26 weak_factory_(this) { |
| 27 binding_.set_connection_error_handler(error_callback_); |
| 28 } |
| 29 |
| 30 VideoCaptureStreamImpl::~VideoCaptureStreamImpl() { |
| 31 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 32 } |
| 33 |
| 34 void VideoCaptureStreamImpl::Start(mojom::VideoCaptureStreamClientPtr client) { |
| 35 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 36 DCHECK(!client_) << "Start() should not be called several times."; |
| 37 client_ = std::move(client); |
| 38 client_.set_connection_error_handler(error_callback_); |
| 39 } |
| 40 |
| 41 void VideoCaptureStreamImpl::Stop() { |
| 42 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 43 DCHECK(client_); |
| 44 stop_callback_.Run(); |
| 45 } |
| 46 |
| 47 void VideoCaptureStreamImpl::OnFrame( |
| 48 const scoped_refptr<MojoSharedBufferVideoFrame>& frame, |
| 49 const base::TimeTicks& timestamp) { |
| 50 task_runner_->PostTask( |
| 51 FROM_HERE, base::Bind(&VideoCaptureStreamImpl::NotifyOnFrameAvailable, |
| 52 weak_factory_.GetWeakPtr(), frame, timestamp)); |
| 53 } |
| 54 |
| 55 void VideoCaptureStreamImpl::OnError(const std::string& reason) { |
| 56 DLOG(ERROR) << __FUNCTION__ << " " << reason; |
| 57 task_runner_->PostTask(FROM_HERE, |
| 58 base::Bind(&VideoCaptureStreamImpl::NotifyError, |
| 59 weak_factory_.GetWeakPtr(), reason)); |
| 60 } |
| 61 |
| 62 void VideoCaptureStreamImpl::NotifyOnFrameAvailable( |
| 63 const scoped_refptr<MojoSharedBufferVideoFrame>& frame, |
| 64 const base::TimeTicks& timestamp) { |
| 65 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 66 DCHECK(client_); |
| 67 |
| 68 const int32_t buffer_id = running_buffer_id_++; |
| 69 |
| 70 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New(); |
| 71 |
| 72 // We duplicate the handle immediately to avoid passing around the primordial |
| 73 // handle. While it's OK for someone to continue using the buffer after the |
| 74 // pool drops ownership, it's not OK to dupe the primordial handle once it's |
| 75 // been invalidated. |
| 76 mojo::ScopedSharedBufferHandle handle; |
| 77 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle); |
| 78 DLOG_IF(ERROR, rv != MOJO_RESULT_OK) << "Error duplicating handle"; |
| 79 if (rv != MOJO_RESULT_OK) |
| 80 return; |
| 81 frame_info->storage_handle = std::move(handle); |
| 82 frame_info->storage_size = frame->mapped_size(); |
| 83 |
| 84 frame_info->pixel_format = |
| 85 static_cast<media::interfaces::VideoFormat>(frame->format()); |
| 86 frame_info->coded_size = mojo::Size::From(frame->coded_size()); |
| 87 frame_info->visible_rect = mojo::Rect::From(frame->visible_rect()); |
| 88 frame_info->timestamp = timestamp.ToInternalValue(); |
| 89 |
| 90 in_use_video_frames_.insert(std::make_pair(buffer_id, frame)); |
| 91 client_->OnFrameAvailable(std::move(frame_info), |
| 92 base::Bind(&VideoCaptureStreamImpl::OnFrameConsumed, |
| 93 weak_factory_.GetWeakPtr(), buffer_id)); |
| 94 } |
| 95 |
| 96 void VideoCaptureStreamImpl::NotifyError(const mojo::String& error) { |
| 97 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 98 // A device may notify of error(s) before Start() registers a |client_|. |
| 99 if (client_) |
| 100 client_->OnError(error); |
| 101 error_callback_.Run(); |
| 102 } |
| 103 |
| 104 void VideoCaptureStreamImpl::OnFrameConsumed(int32_t buffer_id) { |
| 105 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 106 const auto video_frame = in_use_video_frames_.find(buffer_id); |
| 107 DCHECK(video_frame != in_use_video_frames_.end()) << "Buffer id " << buffer_id |
| 108 << " not found!"; |
| 109 in_use_video_frames_.erase(video_frame); |
| 110 } |
| 111 |
| 112 } // namespace media |
OLD | NEW |