| 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/threading/thread_task_runner_handle.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/common/media/video_capture_messages.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 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 | 29 |
| 29 // A holder of a memory-backed buffer and accessors to it. | 30 // A holder of a memory-backed buffer and accessors to it. |
| 30 class VideoCaptureImpl::ClientBuffer | 31 class VideoCaptureImpl::ClientBuffer |
| 31 : public base::RefCountedThreadSafe<ClientBuffer> { | 32 : public base::RefCountedThreadSafe<ClientBuffer> { |
| 32 public: | 33 public: |
| 33 ClientBuffer(std::unique_ptr<base::SharedMemory> buffer, size_t buffer_size) | 34 ClientBuffer(std::unique_ptr<base::SharedMemory> buffer, size_t buffer_size) |
| 34 : buffer_(std::move(buffer)), buffer_size_(buffer_size) {} | 35 : buffer_(std::move(buffer)), buffer_size_(buffer_size) {} |
| 35 | 36 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 169 |
| 169 void VideoCaptureImpl::GetDeviceFormatsInUse( | 170 void VideoCaptureImpl::GetDeviceFormatsInUse( |
| 170 const VideoCaptureDeviceFormatsCB& callback) { | 171 const VideoCaptureDeviceFormatsCB& callback) { |
| 171 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 172 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 172 GetVideoCaptureHost()->GetDeviceFormatsInUse( | 173 GetVideoCaptureHost()->GetDeviceFormatsInUse( |
| 173 device_id_, session_id_, | 174 device_id_, session_id_, |
| 174 base::Bind(&VideoCaptureImpl::OnDeviceFormatsInUse, | 175 base::Bind(&VideoCaptureImpl::OnDeviceFormatsInUse, |
| 175 weak_factory_.GetWeakPtr(), callback)); | 176 weak_factory_.GetWeakPtr(), callback)); |
| 176 } | 177 } |
| 177 | 178 |
| 178 void VideoCaptureImpl::OnBufferCreated(base::SharedMemoryHandle handle, | |
| 179 int length, | |
| 180 int buffer_id) { | |
| 181 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 182 | |
| 183 // In case client calls StopCapture before the arrival of created buffer, | |
| 184 // just close this buffer and return. | |
| 185 if (state_ != VIDEO_CAPTURE_STATE_STARTED) { | |
| 186 base::SharedMemory::CloseHandle(handle); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 std::unique_ptr<base::SharedMemory> shm( | |
| 191 new base::SharedMemory(handle, false)); | |
| 192 if (!shm->Map(length)) { | |
| 193 DLOG(ERROR) << "OnBufferCreated: Map failed."; | |
| 194 return; | |
| 195 } | |
| 196 const bool inserted = | |
| 197 client_buffers_.insert(std::make_pair( | |
| 198 buffer_id, | |
| 199 new ClientBuffer(std::move(shm), length))) | |
| 200 .second; | |
| 201 DCHECK(inserted); | |
| 202 } | |
| 203 | |
| 204 void VideoCaptureImpl::OnDelegateAdded(int32_t device_id) { | 179 void VideoCaptureImpl::OnDelegateAdded(int32_t device_id) { |
| 205 DVLOG(1) << __func__ << " " << device_id; | 180 DVLOG(1) << __func__ << " " << device_id; |
| 206 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 181 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 207 | 182 |
| 208 device_id_ = device_id; | 183 device_id_ = device_id; |
| 209 ClientInfoMap::iterator it = clients_pending_on_filter_.begin(); | 184 ClientInfoMap::iterator it = clients_pending_on_filter_.begin(); |
| 210 while (it != clients_pending_on_filter_.end()) { | 185 while (it != clients_pending_on_filter_.end()) { |
| 211 const int client_id = it->first; | 186 const int client_id = it->first; |
| 212 const ClientInfo client_info = it->second; | 187 const ClientInfo client_info = it->second; |
| 213 clients_pending_on_filter_.erase(it++); | 188 clients_pending_on_filter_.erase(it++); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 case mojom::VideoCaptureState::ENDED: | 224 case mojom::VideoCaptureState::ENDED: |
| 250 // We'll only notify the client that the stream has stopped. | 225 // We'll only notify the client that the stream has stopped. |
| 251 for (const auto& client : clients_) | 226 for (const auto& client : clients_) |
| 252 client.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 227 client.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 253 clients_.clear(); | 228 clients_.clear(); |
| 254 state_ = VIDEO_CAPTURE_STATE_ENDED; | 229 state_ = VIDEO_CAPTURE_STATE_ENDED; |
| 255 break; | 230 break; |
| 256 } | 231 } |
| 257 } | 232 } |
| 258 | 233 |
| 234 void VideoCaptureImpl::OnBufferCreated(int32_t buffer_id, |
| 235 mojo::ScopedSharedBufferHandle handle) { |
| 236 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; |
| 237 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 238 DCHECK(handle.is_valid()); |
| 239 |
| 240 if (state_ != VIDEO_CAPTURE_STATE_STARTED) |
| 241 return; |
| 242 |
| 243 base::SharedMemoryHandle memory_handle; |
| 244 size_t memory_size = 0; |
| 245 bool read_only_flag = false; |
| 246 |
| 247 const MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 248 std::move(handle), &memory_handle, &memory_size, &read_only_flag); |
| 249 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 250 DCHECK_GT(memory_size, 0u); |
| 251 |
| 252 std::unique_ptr<base::SharedMemory> shm( |
| 253 new base::SharedMemory(memory_handle, true /* read_only */)); |
| 254 if (!shm->Map(memory_size)) { |
| 255 DLOG(ERROR) << "OnBufferCreated: Map failed."; |
| 256 return; |
| 257 } |
| 258 const bool inserted = |
| 259 client_buffers_ |
| 260 .insert(std::make_pair(buffer_id, |
| 261 new ClientBuffer(std::move(shm), memory_size))) |
| 262 .second; |
| 263 DCHECK(inserted); |
| 264 } |
| 265 |
| 259 void VideoCaptureImpl::OnBufferReady(int32_t buffer_id, | 266 void VideoCaptureImpl::OnBufferReady(int32_t buffer_id, |
| 260 mojom::VideoFrameInfoPtr info) { | 267 mojom::VideoFrameInfoPtr info) { |
| 261 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; | 268 DVLOG(1) << __func__ << " buffer_id: " << buffer_id; |
| 262 | |
| 263 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 269 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 264 DCHECK_EQ(media::PIXEL_FORMAT_I420, info->pixel_format); | 270 DCHECK_EQ(media::PIXEL_FORMAT_I420, info->pixel_format); |
| 265 DCHECK_EQ(media::PIXEL_STORAGE_CPU, info->storage_type); | 271 DCHECK_EQ(media::PIXEL_STORAGE_CPU, info->storage_type); |
| 266 | 272 |
| 267 if (state_ != VIDEO_CAPTURE_STATE_STARTED || | 273 if (state_ != VIDEO_CAPTURE_STATE_STARTED || |
| 268 info->pixel_format != media::PIXEL_FORMAT_I420 || | 274 info->pixel_format != media::PIXEL_FORMAT_I420 || |
| 269 info->storage_type != media::PIXEL_STORAGE_CPU) { | 275 info->storage_type != media::PIXEL_STORAGE_CPU) { |
| 270 GetVideoCaptureHost()->ReleaseBuffer(device_id_, buffer_id, | 276 GetVideoCaptureHost()->ReleaseBuffer(device_id_, buffer_id, |
| 271 gpu::SyncToken(), -1.0); | 277 gpu::SyncToken(), -1.0); |
| 272 return; | 278 return; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 callback.Run(supported_formats); | 401 callback.Run(supported_formats); |
| 396 } | 402 } |
| 397 | 403 |
| 398 void VideoCaptureImpl::OnDeviceFormatsInUse( | 404 void VideoCaptureImpl::OnDeviceFormatsInUse( |
| 399 const VideoCaptureDeviceFormatsCB& callback, | 405 const VideoCaptureDeviceFormatsCB& callback, |
| 400 const media::VideoCaptureFormats& formats_in_use) { | 406 const media::VideoCaptureFormats& formats_in_use) { |
| 401 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 407 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 402 callback.Run(formats_in_use); | 408 callback.Run(formats_in_use); |
| 403 } | 409 } |
| 404 | 410 |
| 405 void VideoCaptureImpl::Send(IPC::Message* message) { | |
| 406 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 407 message_filter_->Send(message); | |
| 408 } | |
| 409 | |
| 410 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { | 411 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { |
| 411 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 412 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 412 | 413 |
| 413 const ClientInfoMap::iterator it = clients->find(client_id); | 414 const ClientInfoMap::iterator it = clients->find(client_id); |
| 414 if (it == clients->end()) | 415 if (it == clients->end()) |
| 415 return false; | 416 return false; |
| 416 | 417 |
| 417 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 418 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 418 clients->erase(it); | 419 clients->erase(it); |
| 419 return true; | 420 return true; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 445 double consumer_resource_utilization = -1.0; | 446 double consumer_resource_utilization = -1.0; |
| 446 if (!metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, | 447 if (!metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, |
| 447 &consumer_resource_utilization)) { | 448 &consumer_resource_utilization)) { |
| 448 consumer_resource_utilization = -1.0; | 449 consumer_resource_utilization = -1.0; |
| 449 } | 450 } |
| 450 | 451 |
| 451 callback_to_io_thread.Run(*release_sync_token, consumer_resource_utilization); | 452 callback_to_io_thread.Run(*release_sync_token, consumer_resource_utilization); |
| 452 } | 453 } |
| 453 | 454 |
| 454 } // namespace content | 455 } // namespace content |
| OLD | NEW |