Chromium Code Reviews| 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 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 34 } | |
| 35 | |
| 36 void StreamImpl::Start(mojom::StreamClientPtr client) { | |
| 37 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 38 DCHECK(!client_) << "Start() should not be called several times."; | |
| 39 DCHECK(!paused_); | |
| 40 client_ = std::move(client); | |
| 41 client_.set_connection_error_handler(error_callback_); | |
| 42 start_callback_.Run(); | |
| 43 } | |
| 44 | |
| 45 void StreamImpl::Pause() { | |
| 46 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 47 DCHECK(client_); | |
| 48 paused_ = true; | |
| 49 } | |
| 50 | |
| 51 void StreamImpl::Resume() { | |
| 52 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 53 DCHECK(client_); | |
| 54 paused_ = false; | |
| 55 } | |
| 56 | |
| 57 void StreamImpl::Stop() { | |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 59 DCHECK(client_); | |
| 60 close_callback_.Run(); | |
| 61 } | |
| 62 | |
| 63 void StreamImpl::OnFrame(const scoped_refptr<MojoVideoFrame>& frame, | |
| 64 const base::TimeTicks& timestamp) { | |
| 65 task_runner_->PostTask( | |
| 66 FROM_HERE, base::Bind(&StreamImpl::NotifyFrameAvailable, | |
| 67 weak_factory_.GetWeakPtr(), frame, timestamp)); | |
| 68 } | |
| 69 | |
| 70 void StreamImpl::OnError(const std::string& reason) { | |
| 71 DLOG(ERROR) << __FUNCTION__ << " " << reason; | |
| 72 task_runner_->PostTask( | |
| 73 FROM_HERE, | |
| 74 base::Bind(&StreamImpl::NotifyError, weak_factory_.GetWeakPtr(), reason)); | |
| 75 } | |
| 76 | |
| 77 void StreamImpl::NotifyFrameAvailable( | |
| 78 const scoped_refptr<MojoVideoFrame>& frame, | |
| 79 const base::TimeTicks& timestamp) { | |
| 80 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 81 DCHECK(client_); | |
| 82 | |
| 83 if (paused_) // Drop the frame immediately if we aren't playing. | |
| 84 return; | |
|
emircan
2016/02/16 17:45:48
Should we stop the capture here, via a notificatio
| |
| 85 | |
| 86 const int32_t buffer_id = running_buffer_id_++; | |
| 87 | |
| 88 mojom::FrameInfoPtr frame_info = mojom::FrameInfo::New(); | |
| 89 frame_info->buffer_id = buffer_id; | |
| 90 | |
| 91 // We duplicate the handle immediately to avoid passing around the primordial | |
| 92 // handle. While it's OK for someone to continue using the buffer after the | |
| 93 // pool drops ownership, it's not OK to dupe the primordial handle once it's | |
| 94 // been invalidated. | |
| 95 mojo::ScopedSharedBufferHandle handle; | |
| 96 MojoResult rv = mojo::DuplicateBuffer(frame->handle(), nullptr, &handle); | |
|
emircan
2016/02/16 17:45:48
Duplicate works for
https://code.google.com/p/ch
| |
| 97 DCHECK_EQ(rv, MOJO_RESULT_OK); | |
| 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 if(client_) // A devices can notify of errors before there is a |client_|. | |
| 117 client_->Error(error); | |
| 118 error_callback_.Run(); | |
| 119 } | |
| 120 | |
| 121 void StreamImpl::OnFrameConsumed(int32_t buffer_id) { | |
| 122 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 123 if (!ContainsKey(in_use_video_frames_, buffer_id)) { | |
| 124 DLOG(ERROR) << "Buffer id " << buffer_id << " not found!"; | |
| 125 return; | |
| 126 } | |
| 127 in_use_video_frames_.erase(in_use_video_frames_.find(buffer_id)); | |
| 128 } | |
| 129 | |
| 130 } // namespace video_capture | |
| OLD | NEW |