| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Notes about usage of this object by VideoCaptureImplManager. | 5 // Notes about usage of this object by VideoCaptureImplManager. |
| 6 // | 6 // |
| 7 // VideoCaptureImplManager access this object by using a Unretained() | 7 // VideoCaptureImplManager access this object by using a Unretained() |
| 8 // binding and tasks on the IO thread. It is then important that | 8 // binding and tasks on the IO thread. It is then important that |
| 9 // VideoCaptureImpl never post task to itself. All operations must be | 9 // VideoCaptureImpl never post task to itself. All operations must be |
| 10 // synchronous. | 10 // synchronous. |
| 11 | 11 |
| 12 #include "content/renderer/media/video_capture_impl.h" | 12 #include "content/renderer/media/video_capture_impl.h" |
| 13 | 13 |
| 14 #include <stddef.h> | 14 #include <stddef.h> |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/trace_event/trace_event.h" |
| 21 #include "content/child/child_process.h" | 21 #include "content/child/child_process.h" |
| 22 #include "content/common/media/video_capture_messages.h" | 22 #include "content/public/child/child_thread.h" |
| 23 #include "media/base/bind_to_current_loop.h" | 23 #include "media/base/bind_to_current_loop.h" |
| 24 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
| 25 #include "media/base/video_frame.h" | 25 #include "media/base/video_frame.h" |
| 26 #include "mojo/public/cpp/system/platform_handle.h" | 26 #include "mojo/public/cpp/system/platform_handle.h" |
| 27 #include "services/service_manager/public/cpp/interface_provider.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 // A holder of a memory-backed buffer and accessors to it. | 31 // A holder of a memory-backed buffer and accessors to it. |
| 31 class VideoCaptureImpl::ClientBuffer | 32 class VideoCaptureImpl::ClientBuffer |
| 32 : public base::RefCountedThreadSafe<ClientBuffer> { | 33 : public base::RefCountedThreadSafe<ClientBuffer> { |
| 33 public: | 34 public: |
| 34 ClientBuffer(std::unique_ptr<base::SharedMemory> buffer, size_t buffer_size) | 35 ClientBuffer(std::unique_ptr<base::SharedMemory> buffer, size_t buffer_size) |
| 35 : buffer_(std::move(buffer)), buffer_size_(buffer_size) {} | 36 : buffer_(std::move(buffer)), buffer_size_(buffer_size) {} |
| 36 | 37 |
| 37 base::SharedMemory* buffer() const { return buffer_.get(); } | 38 base::SharedMemory* buffer() const { return buffer_.get(); } |
| 38 size_t buffer_size() const { return buffer_size_; } | 39 size_t buffer_size() const { return buffer_size_; } |
| 39 | 40 |
| 40 private: | 41 private: |
| 41 friend class base::RefCountedThreadSafe<ClientBuffer>; | 42 friend class base::RefCountedThreadSafe<ClientBuffer>; |
| 42 | 43 |
| 43 virtual ~ClientBuffer() {} | 44 virtual ~ClientBuffer() {} |
| 44 | 45 |
| 45 const std::unique_ptr<base::SharedMemory> buffer_; | 46 const std::unique_ptr<base::SharedMemory> buffer_; |
| 46 const size_t buffer_size_; | 47 const size_t buffer_size_; |
| 47 | 48 |
| 48 DISALLOW_COPY_AND_ASSIGN(ClientBuffer); | 49 DISALLOW_COPY_AND_ASSIGN(ClientBuffer); |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 VideoCaptureImpl::ClientInfo::ClientInfo() {} | 52 // Information about a video capture client of ours. |
| 52 VideoCaptureImpl::ClientInfo::ClientInfo(const ClientInfo& other) = default; | 53 struct VideoCaptureImpl::ClientInfo { |
| 53 VideoCaptureImpl::ClientInfo::~ClientInfo() {} | 54 ClientInfo() = default; |
| 54 | 55 |
| 55 VideoCaptureImpl::VideoCaptureImpl( | 56 ClientInfo(const ClientInfo& other) = default; |
| 56 media::VideoCaptureSessionId session_id, | 57 |
| 57 VideoCaptureMessageFilter* filter, | 58 ~ClientInfo() = default; |
| 58 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 59 |
| 59 : message_filter_(filter), | 60 media::VideoCaptureParams params; |
| 60 device_id_(0), | 61 |
| 62 VideoCaptureStateUpdateCB state_update_cb; |
| 63 |
| 64 VideoCaptureDeliverFrameCB deliver_frame_cb; |
| 65 }; |
| 66 |
| 67 VideoCaptureImpl::VideoCaptureImpl(media::VideoCaptureSessionId session_id) |
| 68 : device_id_(session_id), |
| 61 session_id_(session_id), | 69 session_id_(session_id), |
| 62 video_capture_host_for_testing_(nullptr), | 70 video_capture_host_for_testing_(nullptr), |
| 63 observer_binding_(this), | 71 observer_binding_(this), |
| 64 state_(VIDEO_CAPTURE_STATE_STOPPED), | 72 state_(VIDEO_CAPTURE_STATE_STOPPED), |
| 65 io_task_runner_(std::move(io_task_runner)), | |
| 66 weak_factory_(this) { | 73 weak_factory_(this) { |
| 67 DCHECK(filter); | 74 io_thread_checker_.DetachFromThread(); |
| 68 io_task_runner_->PostTask(FROM_HERE, | 75 |
| 69 base::Bind(&VideoCaptureMessageFilter::AddDelegate, | 76 if (ChildThread::Get()) { // This will be null in unit tests. |
| 70 message_filter_, this)); | 77 mojom::VideoCaptureHostPtr temp_video_capture_host; |
| 78 ChildThread::Get()->GetRemoteInterfaces()->GetInterface( |
| 79 mojo::GetProxy(&temp_video_capture_host)); |
| 80 video_capture_host_info_ = temp_video_capture_host.PassInterface(); |
| 81 } |
| 71 } | 82 } |
| 72 | 83 |
| 73 VideoCaptureImpl::~VideoCaptureImpl() { | 84 VideoCaptureImpl::~VideoCaptureImpl() { |
| 74 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 85 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 75 if (state_ == VIDEO_CAPTURE_STATE_STARTED && GetVideoCaptureHost()) | 86 if (state_ == VIDEO_CAPTURE_STATE_STARTED && GetVideoCaptureHost()) |
| 76 GetVideoCaptureHost()->Stop(device_id_); | 87 GetVideoCaptureHost()->Stop(device_id_); |
| 77 message_filter_->RemoveDelegate(this); | |
| 78 } | 88 } |
| 79 | 89 |
| 80 void VideoCaptureImpl::SuspendCapture(bool suspend) { | 90 void VideoCaptureImpl::SuspendCapture(bool suspend) { |
| 81 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 91 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 82 if (suspend) | 92 if (suspend) |
| 83 GetVideoCaptureHost()->Pause(device_id_); | 93 GetVideoCaptureHost()->Pause(device_id_); |
| 84 else | 94 else |
| 85 GetVideoCaptureHost()->Resume(device_id_, session_id_, params_); | 95 GetVideoCaptureHost()->Resume(device_id_, session_id_, params_); |
| 86 } | 96 } |
| 87 | 97 |
| 88 void VideoCaptureImpl::StartCapture( | 98 void VideoCaptureImpl::StartCapture( |
| 89 int client_id, | 99 int client_id, |
| 90 const media::VideoCaptureParams& params, | 100 const media::VideoCaptureParams& params, |
| 91 const VideoCaptureStateUpdateCB& state_update_cb, | 101 const VideoCaptureStateUpdateCB& state_update_cb, |
| 92 const VideoCaptureDeliverFrameCB& deliver_frame_cb) { | 102 const VideoCaptureDeliverFrameCB& deliver_frame_cb) { |
| 93 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 103 DVLOG(1) << __func__ << " |device_id_| = " << device_id_; |
| 104 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 94 ClientInfo client_info; | 105 ClientInfo client_info; |
| 95 client_info.params = params; | 106 client_info.params = params; |
| 96 client_info.state_update_cb = state_update_cb; | 107 client_info.state_update_cb = state_update_cb; |
| 97 client_info.deliver_frame_cb = deliver_frame_cb; | 108 client_info.deliver_frame_cb = deliver_frame_cb; |
| 98 | 109 |
| 99 if (state_ == VIDEO_CAPTURE_STATE_ERROR) { | 110 if (state_ == VIDEO_CAPTURE_STATE_ERROR) { |
| 100 state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); | 111 state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); |
| 101 } else if (clients_pending_on_filter_.count(client_id) || | 112 } else if (clients_pending_on_restart_.count(client_id) || |
| 102 clients_pending_on_restart_.count(client_id) || | |
| 103 clients_.count(client_id)) { | 113 clients_.count(client_id)) { |
| 104 DLOG(FATAL) << __func__ << " This client has already started."; | 114 DLOG(FATAL) << __func__ << " This client has already started."; |
| 105 } else if (!device_id_) { | |
| 106 clients_pending_on_filter_[client_id] = client_info; | |
| 107 } else { | 115 } else { |
| 108 // Note: |state_| might not be started at this point. But we tell | 116 // Note: |state_| might not be started at this point. But we tell |
| 109 // client that we have started. | 117 // client that we have started. |
| 110 state_update_cb.Run(VIDEO_CAPTURE_STATE_STARTED); | 118 state_update_cb.Run(VIDEO_CAPTURE_STATE_STARTED); |
| 111 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 119 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 112 clients_[client_id] = client_info; | 120 clients_[client_id] = client_info; |
| 113 // TODO(sheu): Allowing resolution change will require that all | 121 // TODO(sheu): Allowing resolution change will require that all |
| 114 // outstanding clients of a capture session support resolution change. | 122 // outstanding clients of a capture session support resolution change. |
| 115 DCHECK_EQ(params_.resolution_change_policy, | 123 DCHECK_EQ(params_.resolution_change_policy, |
| 116 params.resolution_change_policy); | 124 params.resolution_change_policy); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 128 static_cast<float>(media::limits::kMaxFramesPerSecond)); | 136 static_cast<float>(media::limits::kMaxFramesPerSecond)); |
| 129 | 137 |
| 130 DVLOG(1) << "StartCapture: starting with first resolution " | 138 DVLOG(1) << "StartCapture: starting with first resolution " |
| 131 << params_.requested_format.frame_size.ToString(); | 139 << params_.requested_format.frame_size.ToString(); |
| 132 StartCaptureInternal(); | 140 StartCaptureInternal(); |
| 133 } | 141 } |
| 134 } | 142 } |
| 135 } | 143 } |
| 136 | 144 |
| 137 void VideoCaptureImpl::StopCapture(int client_id) { | 145 void VideoCaptureImpl::StopCapture(int client_id) { |
| 138 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 146 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 139 // A client ID can be in only one client list. | 147 // A client ID can be in only one client list. |
| 140 // If this ID is in any client list, we can just remove it from | 148 // If this ID is in any client list, we can just remove it from |
| 141 // that client list and don't have to run the other following RemoveClient(). | 149 // that client list and don't have to run the other following RemoveClient(). |
| 142 if (!RemoveClient(client_id, &clients_pending_on_filter_)) { | 150 if (!RemoveClient(client_id, &clients_pending_on_restart_)) { |
| 143 if (!RemoveClient(client_id, &clients_pending_on_restart_)) { | 151 RemoveClient(client_id, &clients_); |
| 144 RemoveClient(client_id, &clients_); | |
| 145 } | |
| 146 } | 152 } |
| 147 | 153 |
| 148 if (!clients_.empty()) | 154 if (!clients_.empty()) |
| 149 return; | 155 return; |
| 150 DVLOG(1) << "StopCapture: No more client, stopping ..."; | 156 DVLOG(1) << "StopCapture: No more client, stopping ..."; |
| 151 StopDevice(); | 157 StopDevice(); |
| 152 client_buffers_.clear(); | 158 client_buffers_.clear(); |
| 153 weak_factory_.InvalidateWeakPtrs(); | 159 weak_factory_.InvalidateWeakPtrs(); |
| 154 } | 160 } |
| 155 | 161 |
| 156 void VideoCaptureImpl::RequestRefreshFrame() { | 162 void VideoCaptureImpl::RequestRefreshFrame() { |
| 157 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 163 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 158 GetVideoCaptureHost()->RequestRefreshFrame(device_id_); | 164 GetVideoCaptureHost()->RequestRefreshFrame(device_id_); |
| 159 } | 165 } |
| 160 | 166 |
| 161 void VideoCaptureImpl::GetDeviceSupportedFormats( | 167 void VideoCaptureImpl::GetDeviceSupportedFormats( |
| 162 const VideoCaptureDeviceFormatsCB& callback) { | 168 const VideoCaptureDeviceFormatsCB& callback) { |
| 163 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 169 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 164 GetVideoCaptureHost()->GetDeviceSupportedFormats( | 170 GetVideoCaptureHost()->GetDeviceSupportedFormats( |
| 165 device_id_, session_id_, | 171 device_id_, session_id_, |
| 166 base::Bind(&VideoCaptureImpl::OnDeviceSupportedFormats, | 172 base::Bind(&VideoCaptureImpl::OnDeviceSupportedFormats, |
| 167 weak_factory_.GetWeakPtr(), callback)); | 173 weak_factory_.GetWeakPtr(), callback)); |
| 168 } | 174 } |
| 169 | 175 |
| 170 void VideoCaptureImpl::GetDeviceFormatsInUse( | 176 void VideoCaptureImpl::GetDeviceFormatsInUse( |
| 171 const VideoCaptureDeviceFormatsCB& callback) { | 177 const VideoCaptureDeviceFormatsCB& callback) { |
| 172 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 178 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 173 GetVideoCaptureHost()->GetDeviceFormatsInUse( | 179 GetVideoCaptureHost()->GetDeviceFormatsInUse( |
| 174 device_id_, session_id_, | 180 device_id_, session_id_, |
| 175 base::Bind(&VideoCaptureImpl::OnDeviceFormatsInUse, | 181 base::Bind(&VideoCaptureImpl::OnDeviceFormatsInUse, |
| 176 weak_factory_.GetWeakPtr(), callback)); | 182 weak_factory_.GetWeakPtr(), callback)); |
| 177 } | 183 } |
| 178 | 184 |
| 179 void VideoCaptureImpl::OnDelegateAdded(int32_t device_id) { | |
| 180 DVLOG(1) << __func__ << " " << device_id; | |
| 181 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 182 | |
| 183 device_id_ = device_id; | |
| 184 ClientInfoMap::iterator it = clients_pending_on_filter_.begin(); | |
| 185 while (it != clients_pending_on_filter_.end()) { | |
| 186 const int client_id = it->first; | |
| 187 const ClientInfo client_info = it->second; | |
| 188 clients_pending_on_filter_.erase(it++); | |
| 189 StartCapture(client_id, client_info.params, client_info.state_update_cb, | |
| 190 client_info.deliver_frame_cb); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 void VideoCaptureImpl::OnStateChanged(mojom::VideoCaptureState state) { | 185 void VideoCaptureImpl::OnStateChanged(mojom::VideoCaptureState state) { |
| 195 DVLOG(1) << __func__ << " state: " << state; | 186 DVLOG(1) << __func__ << " state: " << state; |
| 196 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 187 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 197 | 188 |
| 198 switch (state) { | 189 switch (state) { |
| 199 case mojom::VideoCaptureState::STARTED: | 190 case mojom::VideoCaptureState::STARTED: |
| 200 // Capture has started in the browser process. Since we have already | 191 // Capture has started in the browser process. Since we have already |
| 201 // told all clients that we have started there's nothing to do. | 192 // told all clients that we have started there's nothing to do. |
| 202 break; | 193 break; |
| 203 case mojom::VideoCaptureState::STOPPED: | 194 case mojom::VideoCaptureState::STOPPED: |
| 204 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 195 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
| 205 client_buffers_.clear(); | 196 client_buffers_.clear(); |
| 206 weak_factory_.InvalidateWeakPtrs(); | 197 weak_factory_.InvalidateWeakPtrs(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 227 client.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 218 client.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 228 clients_.clear(); | 219 clients_.clear(); |
| 229 state_ = VIDEO_CAPTURE_STATE_ENDED; | 220 state_ = VIDEO_CAPTURE_STATE_ENDED; |
| 230 break; | 221 break; |
| 231 } | 222 } |
| 232 } | 223 } |
| 233 | 224 |
| 234 void VideoCaptureImpl::OnBufferCreated(int32_t buffer_id, | 225 void VideoCaptureImpl::OnBufferCreated(int32_t buffer_id, |
| 235 mojo::ScopedSharedBufferHandle handle) { | 226 mojo::ScopedSharedBufferHandle handle) { |
| 236 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; | 227 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; |
| 237 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 228 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 238 DCHECK(handle.is_valid()); | 229 DCHECK(handle.is_valid()); |
| 239 | 230 |
| 240 if (state_ != VIDEO_CAPTURE_STATE_STARTED) | 231 if (state_ != VIDEO_CAPTURE_STATE_STARTED) |
| 241 return; | 232 return; |
| 242 | 233 |
| 243 base::SharedMemoryHandle memory_handle; | 234 base::SharedMemoryHandle memory_handle; |
| 244 size_t memory_size = 0; | 235 size_t memory_size = 0; |
| 245 bool read_only_flag = false; | 236 bool read_only_flag = false; |
| 246 | 237 |
| 247 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | 238 const MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 259 client_buffers_ | 250 client_buffers_ |
| 260 .insert(std::make_pair(buffer_id, | 251 .insert(std::make_pair(buffer_id, |
| 261 new ClientBuffer(std::move(shm), memory_size))) | 252 new ClientBuffer(std::move(shm), memory_size))) |
| 262 .second; | 253 .second; |
| 263 DCHECK(inserted); | 254 DCHECK(inserted); |
| 264 } | 255 } |
| 265 | 256 |
| 266 void VideoCaptureImpl::OnBufferReady(int32_t buffer_id, | 257 void VideoCaptureImpl::OnBufferReady(int32_t buffer_id, |
| 267 mojom::VideoFrameInfoPtr info) { | 258 mojom::VideoFrameInfoPtr info) { |
| 268 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; | 259 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; |
| 269 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 260 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 270 DCHECK_EQ(media::PIXEL_FORMAT_I420, info->pixel_format); | 261 DCHECK_EQ(media::PIXEL_FORMAT_I420, info->pixel_format); |
| 271 DCHECK_EQ(media::PIXEL_STORAGE_CPU, info->storage_type); | 262 DCHECK_EQ(media::PIXEL_STORAGE_CPU, info->storage_type); |
| 272 | 263 |
| 273 if (state_ != VIDEO_CAPTURE_STATE_STARTED || | 264 if (state_ != VIDEO_CAPTURE_STATE_STARTED || |
| 274 info->pixel_format != media::PIXEL_FORMAT_I420 || | 265 info->pixel_format != media::PIXEL_FORMAT_I420 || |
| 275 info->storage_type != media::PIXEL_STORAGE_CPU) { | 266 info->storage_type != media::PIXEL_STORAGE_CPU) { |
| 276 GetVideoCaptureHost()->ReleaseBuffer(device_id_, buffer_id, | 267 GetVideoCaptureHost()->ReleaseBuffer(device_id_, buffer_id, |
| 277 gpu::SyncToken(), -1.0); | 268 gpu::SyncToken(), -1.0); |
| 278 return; | 269 return; |
| 279 } | 270 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 320 |
| 330 frame->metadata()->MergeInternalValuesFrom(info->metadata); | 321 frame->metadata()->MergeInternalValuesFrom(info->metadata); |
| 331 | 322 |
| 332 // TODO(qiangchen): Dive into the full code path to let frame metadata hold | 323 // TODO(qiangchen): Dive into the full code path to let frame metadata hold |
| 333 // reference time rather than using an extra parameter. | 324 // reference time rather than using an extra parameter. |
| 334 for (const auto& client : clients_) | 325 for (const auto& client : clients_) |
| 335 client.second.deliver_frame_cb.Run(frame, reference_time); | 326 client.second.deliver_frame_cb.Run(frame, reference_time); |
| 336 } | 327 } |
| 337 | 328 |
| 338 void VideoCaptureImpl::OnBufferDestroyed(int32_t buffer_id) { | 329 void VideoCaptureImpl::OnBufferDestroyed(int32_t buffer_id) { |
| 339 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 330 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 340 | 331 |
| 341 const auto& cb_iter = client_buffers_.find(buffer_id); | 332 const auto& cb_iter = client_buffers_.find(buffer_id); |
| 342 if (cb_iter != client_buffers_.end()) { | 333 if (cb_iter != client_buffers_.end()) { |
| 343 DCHECK(!cb_iter->second.get() || cb_iter->second->HasOneRef()) | 334 DCHECK(!cb_iter->second.get() || cb_iter->second->HasOneRef()) |
| 344 << "Instructed to delete buffer we are still using."; | 335 << "Instructed to delete buffer we are still using."; |
| 345 client_buffers_.erase(cb_iter); | 336 client_buffers_.erase(cb_iter); |
| 346 } | 337 } |
| 347 } | 338 } |
| 348 | 339 |
| 349 void VideoCaptureImpl::OnClientBufferFinished( | 340 void VideoCaptureImpl::OnClientBufferFinished( |
| 350 int buffer_id, | 341 int buffer_id, |
| 351 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, | 342 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, |
| 352 const gpu::SyncToken& release_sync_token, | 343 const gpu::SyncToken& release_sync_token, |
| 353 double consumer_resource_utilization) { | 344 double consumer_resource_utilization) { |
| 354 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 345 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 355 GetVideoCaptureHost()->ReleaseBuffer( | 346 GetVideoCaptureHost()->ReleaseBuffer( |
| 356 device_id_, buffer_id, release_sync_token, consumer_resource_utilization); | 347 device_id_, buffer_id, release_sync_token, consumer_resource_utilization); |
| 357 } | 348 } |
| 358 | 349 |
| 359 void VideoCaptureImpl::StopDevice() { | 350 void VideoCaptureImpl::StopDevice() { |
| 360 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 351 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 361 if (state_ != VIDEO_CAPTURE_STATE_STARTED) | 352 if (state_ != VIDEO_CAPTURE_STATE_STARTED) |
| 362 return; | 353 return; |
| 363 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 354 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
| 364 GetVideoCaptureHost()->Stop(device_id_); | 355 GetVideoCaptureHost()->Stop(device_id_); |
| 365 params_.requested_format.frame_size.SetSize(0, 0); | 356 params_.requested_format.frame_size.SetSize(0, 0); |
| 366 } | 357 } |
| 367 | 358 |
| 368 void VideoCaptureImpl::RestartCapture() { | 359 void VideoCaptureImpl::RestartCapture() { |
| 369 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 360 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 370 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); | 361 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); |
| 371 | 362 |
| 372 int width = 0; | 363 int width = 0; |
| 373 int height = 0; | 364 int height = 0; |
| 374 clients_.insert(clients_pending_on_restart_.begin(), | 365 clients_.insert(clients_pending_on_restart_.begin(), |
| 375 clients_pending_on_restart_.end()); | 366 clients_pending_on_restart_.end()); |
| 376 clients_pending_on_restart_.clear(); | 367 clients_pending_on_restart_.clear(); |
| 377 for (const auto& client : clients_) { | 368 for (const auto& client : clients_) { |
| 378 width = std::max(width, | 369 width = std::max(width, |
| 379 client.second.params.requested_format.frame_size.width()); | 370 client.second.params.requested_format.frame_size.width()); |
| 380 height = std::max( | 371 height = std::max( |
| 381 height, client.second.params.requested_format.frame_size.height()); | 372 height, client.second.params.requested_format.frame_size.height()); |
| 382 } | 373 } |
| 383 params_.requested_format.frame_size.SetSize(width, height); | 374 params_.requested_format.frame_size.SetSize(width, height); |
| 384 DVLOG(1) << __func__ << " " << params_.requested_format.frame_size.ToString(); | 375 DVLOG(1) << __func__ << " " << params_.requested_format.frame_size.ToString(); |
| 385 StartCaptureInternal(); | 376 StartCaptureInternal(); |
| 386 } | 377 } |
| 387 | 378 |
| 388 void VideoCaptureImpl::StartCaptureInternal() { | 379 void VideoCaptureImpl::StartCaptureInternal() { |
| 389 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 380 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 390 DCHECK(device_id_); | |
| 391 | 381 |
| 392 GetVideoCaptureHost()->Start(device_id_, session_id_, params_, | 382 GetVideoCaptureHost()->Start(device_id_, session_id_, params_, |
| 393 observer_binding_.CreateInterfacePtrAndBind()); | 383 observer_binding_.CreateInterfacePtrAndBind()); |
| 394 state_ = VIDEO_CAPTURE_STATE_STARTED; | 384 state_ = VIDEO_CAPTURE_STATE_STARTED; |
| 395 } | 385 } |
| 396 | 386 |
| 397 void VideoCaptureImpl::OnDeviceSupportedFormats( | 387 void VideoCaptureImpl::OnDeviceSupportedFormats( |
| 398 const VideoCaptureDeviceFormatsCB& callback, | 388 const VideoCaptureDeviceFormatsCB& callback, |
| 399 const media::VideoCaptureFormats& supported_formats) { | 389 const media::VideoCaptureFormats& supported_formats) { |
| 400 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 390 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 401 callback.Run(supported_formats); | 391 callback.Run(supported_formats); |
| 402 } | 392 } |
| 403 | 393 |
| 404 void VideoCaptureImpl::OnDeviceFormatsInUse( | 394 void VideoCaptureImpl::OnDeviceFormatsInUse( |
| 405 const VideoCaptureDeviceFormatsCB& callback, | 395 const VideoCaptureDeviceFormatsCB& callback, |
| 406 const media::VideoCaptureFormats& formats_in_use) { | 396 const media::VideoCaptureFormats& formats_in_use) { |
| 407 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 397 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 408 callback.Run(formats_in_use); | 398 callback.Run(formats_in_use); |
| 409 } | 399 } |
| 410 | 400 |
| 411 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { | 401 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { |
| 412 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 402 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 413 | 403 |
| 414 const ClientInfoMap::iterator it = clients->find(client_id); | 404 const ClientInfoMap::iterator it = clients->find(client_id); |
| 415 if (it == clients->end()) | 405 if (it == clients->end()) |
| 416 return false; | 406 return false; |
| 417 | 407 |
| 418 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 408 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 419 clients->erase(it); | 409 clients->erase(it); |
| 420 return true; | 410 return true; |
| 421 } | 411 } |
| 422 | 412 |
| 423 mojom::VideoCaptureHost* VideoCaptureImpl::GetVideoCaptureHost() { | 413 mojom::VideoCaptureHost* VideoCaptureImpl::GetVideoCaptureHost() { |
| 424 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 414 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 425 if (video_capture_host_for_testing_) | 415 if (video_capture_host_for_testing_) |
| 426 return video_capture_host_for_testing_; | 416 return video_capture_host_for_testing_; |
| 427 | 417 |
| 428 if (!video_capture_host_.get()) { | 418 if (!video_capture_host_.get()) |
| 429 DCHECK(message_filter_->channel()); | 419 video_capture_host_.Bind(std::move(video_capture_host_info_)); |
| 430 auto interface_support = | |
| 431 message_filter_->channel()->GetAssociatedInterfaceSupport(); | |
| 432 if (!interface_support) | |
| 433 return nullptr; | |
| 434 interface_support->GetRemoteAssociatedInterface(&video_capture_host_); | |
| 435 } | |
| 436 return video_capture_host_.get(); | 420 return video_capture_host_.get(); |
| 437 }; | 421 }; |
| 438 | 422 |
| 439 // static | 423 // static |
| 440 void VideoCaptureImpl::DidFinishConsumingFrame( | 424 void VideoCaptureImpl::DidFinishConsumingFrame( |
| 441 const media::VideoFrameMetadata* metadata, | 425 const media::VideoFrameMetadata* metadata, |
| 442 std::unique_ptr<gpu::SyncToken> release_sync_token, | 426 std::unique_ptr<gpu::SyncToken> release_sync_token, |
| 443 const BufferFinishedCallback& callback_to_io_thread) { | 427 const BufferFinishedCallback& callback_to_io_thread) { |
| 444 // Note: This function may be called on any thread by the VideoFrame | 428 // Note: This function may be called on any thread by the VideoFrame |
| 445 // destructor. |metadata| is still valid for read-access at this point. | 429 // destructor. |metadata| is still valid for read-access at this point. |
| 446 double consumer_resource_utilization = -1.0; | 430 double consumer_resource_utilization = -1.0; |
| 447 if (!metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, | 431 if (!metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
| 448 &consumer_resource_utilization)) { | 432 &consumer_resource_utilization)) { |
| 449 consumer_resource_utilization = -1.0; | 433 consumer_resource_utilization = -1.0; |
| 450 } | 434 } |
| 451 | 435 |
| 452 callback_to_io_thread.Run(*release_sync_token, consumer_resource_utilization); | 436 callback_to_io_thread.Run(*release_sync_token, consumer_resource_utilization); |
| 453 } | 437 } |
| 454 | 438 |
| 455 } // namespace content | 439 } // namespace content |
| OLD | NEW |