| 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 #include "content/renderer/media/video_capture_impl.h" | 5 #include "content/renderer/media/video_capture_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 9 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 11 #include "content/common/media/encoded_video_capture_messages.h" |
| 10 #include "content/common/media/video_capture_messages.h" | 12 #include "content/common/media/video_capture_messages.h" |
| 11 #include "media/base/limits.h" | 13 #include "media/base/limits.h" |
| 12 | 14 |
| 13 namespace content { | 15 namespace content { |
| 14 | 16 |
| 15 struct VideoCaptureImpl::DIBBuffer { | 17 struct VideoCaptureImpl::DIBBuffer { |
| 16 public: | 18 public: |
| 17 DIBBuffer( | 19 DIBBuffer( |
| 18 base::SharedMemory* d, | 20 base::SharedMemory* d, |
| 19 media::VideoCapture::VideoFrameBuffer* ptr) | 21 media::VideoCapture::VideoFrameBuffer* ptr) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 base::MessageLoopProxy* capture_message_loop_proxy, | 53 base::MessageLoopProxy* capture_message_loop_proxy, |
| 52 VideoCaptureMessageFilter* filter) | 54 VideoCaptureMessageFilter* filter) |
| 53 : VideoCapture(), | 55 : VideoCapture(), |
| 54 message_filter_(filter), | 56 message_filter_(filter), |
| 55 capture_message_loop_proxy_(capture_message_loop_proxy), | 57 capture_message_loop_proxy_(capture_message_loop_proxy), |
| 56 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), | 58 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), |
| 57 device_id_(0), | 59 device_id_(0), |
| 58 video_type_(media::VideoCaptureCapability::kI420), | 60 video_type_(media::VideoCaptureCapability::kI420), |
| 59 device_info_available_(false), | 61 device_info_available_(false), |
| 60 suspended_(false), | 62 suspended_(false), |
| 61 state_(VIDEO_CAPTURE_STATE_STOPPED) { | 63 state_(VIDEO_CAPTURE_STATE_STOPPED), |
| 64 encoded_video_source_client_(NULL), |
| 65 bitstream_open_(false) { |
| 62 DCHECK(filter); | 66 DCHECK(filter); |
| 63 memset(¤t_params_, 0, sizeof(current_params_)); | 67 memset(¤t_params_, 0, sizeof(current_params_)); |
| 64 memset(&device_info_, 0, sizeof(device_info_)); | 68 memset(&device_info_, 0, sizeof(device_info_)); |
| 65 current_params_.session_id = id; | 69 current_params_.session_id = id; |
| 66 } | 70 } |
| 67 | 71 |
| 68 VideoCaptureImpl::~VideoCaptureImpl() { | 72 VideoCaptureImpl::~VideoCaptureImpl() { |
| 69 STLDeleteValues(&cached_dibs_); | 73 STLDeleteValues(&cached_dibs_); |
| 70 } | 74 } |
| 71 | 75 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 94 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, | 98 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, |
| 95 base::Unretained(this), handler, capability)); | 99 base::Unretained(this), handler, capability)); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { | 102 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { |
| 99 capture_message_loop_proxy_->PostTask(FROM_HERE, | 103 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 100 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, | 104 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, |
| 101 base::Unretained(this), handler)); | 105 base::Unretained(this), handler)); |
| 102 } | 106 } |
| 103 | 107 |
| 108 void VideoCaptureImpl::RequestCapabilities( |
| 109 const media::EncodedVideoSource::RequestCapabilitiesCallback& callback) { |
| 110 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 111 base::Bind(&VideoCaptureImpl::DoRequestCapabilitiesOnCaptureThread, |
| 112 base::Unretained(this), callback)); |
| 113 } |
| 114 |
| 115 void VideoCaptureImpl::StartFetchCapabilities() { |
| 116 Send(new EncodedVideoCaptureHostMsg_GetCapabilities( |
| 117 device_id_, current_params_.session_id)); |
| 118 } |
| 119 |
| 120 void VideoCaptureImpl::OpenBitstream( |
| 121 media::EncodedVideoSource::Client* client, |
| 122 const media::VideoEncodingParameters& params) { |
| 123 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 124 base::Bind(&VideoCaptureImpl::DoOpenBitstreamOnCaptureThread, |
| 125 base::Unretained(this), client, params)); |
| 126 } |
| 127 |
| 128 void VideoCaptureImpl::CloseBitstream() { |
| 129 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 130 base::Bind(&VideoCaptureImpl::DoCloseBitstreamOnCaptureThread, |
| 131 base::Unretained(this))); |
| 132 } |
| 133 |
| 134 void VideoCaptureImpl::ReturnBitstreamBuffer( |
| 135 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { |
| 136 Send(new EncodedVideoCaptureHostMsg_BitstreamBufferConsumed( |
| 137 device_id_, buffer->buffer_id())); |
| 138 } |
| 139 |
| 140 void VideoCaptureImpl::TrySetBitstreamConfig( |
| 141 const media::RuntimeVideoEncodingParameters& params) { |
| 142 Send(new EncodedVideoCaptureHostMsg_TryConfigureBitstream( |
| 143 device_id_, params)); |
| 144 } |
| 145 |
| 146 void VideoCaptureImpl::OnEncodingCapabilitiesAvailable( |
| 147 const media::VideoEncodingCapabilities& capabilities) { |
| 148 capture_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 149 &VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread, |
| 150 base::Unretained(this), capabilities)); |
| 151 } |
| 152 |
| 153 void VideoCaptureImpl::OnEncodedBitstreamOpened( |
| 154 const media::VideoEncodingParameters& params, |
| 155 const std::vector<base::SharedMemoryHandle>& buffers, |
| 156 uint32 buffer_size) { |
| 157 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 158 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread, |
| 159 base::Unretained(this), params, buffers, buffer_size)); |
| 160 } |
| 161 |
| 162 void VideoCaptureImpl::OnEncodedBitstreamClosed() { |
| 163 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 164 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread, |
| 165 base::Unretained(this))); |
| 166 } |
| 167 |
| 168 void VideoCaptureImpl::OnEncodingConfigChanged( |
| 169 const media::RuntimeVideoEncodingParameters& params) { |
| 170 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 171 base::Bind( |
| 172 &VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread, |
| 173 base::Unretained(this), params)); |
| 174 } |
| 175 |
| 176 void VideoCaptureImpl::OnEncodedBufferReady( |
| 177 int buffer_id, |
| 178 uint32 size, |
| 179 const media::BufferEncodingMetadata& metadata) { |
| 180 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 181 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread, |
| 182 base::Unretained(this), buffer_id, size, metadata)); |
| 183 } |
| 184 |
| 104 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 185 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
| 105 capture_message_loop_proxy_->PostTask(FROM_HERE, | 186 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 106 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, | 187 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, |
| 107 base::Unretained(this), buffer)); | 188 base::Unretained(this), buffer)); |
| 108 } | 189 } |
| 109 | 190 |
| 110 void VideoCaptureImpl::OnBufferCreated( | 191 void VideoCaptureImpl::OnBufferCreated( |
| 111 base::SharedMemoryHandle handle, | 192 base::SharedMemoryHandle handle, |
| 112 int length, int buffer_id) { | 193 int length, int buffer_id) { |
| 113 capture_message_loop_proxy_->PostTask(FROM_HERE, | 194 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 it->first->OnBufferReady(this, buffer); | 380 it->first->OnBufferReady(this, buffer); |
| 300 } | 381 } |
| 301 cached_dibs_[buffer_id]->references = clients_.size(); | 382 cached_dibs_[buffer_id]->references = clients_.size(); |
| 302 } | 383 } |
| 303 | 384 |
| 304 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { | 385 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { |
| 305 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 386 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 306 | 387 |
| 307 switch (state) { | 388 switch (state) { |
| 308 case VIDEO_CAPTURE_STATE_STARTED: | 389 case VIDEO_CAPTURE_STATE_STARTED: |
| 390 if (!encoding_caps_callback_.is_null()) { |
| 391 StartFetchCapabilities(); |
| 392 } |
| 309 break; | 393 break; |
| 310 case VIDEO_CAPTURE_STATE_STOPPED: | 394 case VIDEO_CAPTURE_STATE_STOPPED: |
| 311 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 395 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
| 312 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 396 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
| 313 STLDeleteValues(&cached_dibs_); | 397 STLDeleteValues(&cached_dibs_); |
| 314 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 398 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
| 315 RestartCapture(); | 399 RestartCapture(); |
| 316 break; | 400 break; |
| 317 case VIDEO_CAPTURE_STATE_PAUSED: | 401 case VIDEO_CAPTURE_STATE_PAUSED: |
| 318 for (ClientInfo::iterator it = clients_.begin(); | 402 for (ClientInfo::iterator it = clients_.begin(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 } | 457 } |
| 374 } | 458 } |
| 375 | 459 |
| 376 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { | 460 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { |
| 377 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); | 461 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); |
| 378 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 462 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 379 | 463 |
| 380 suspended_ = suspend; | 464 suspended_ = suspend; |
| 381 } | 465 } |
| 382 | 466 |
| 467 void VideoCaptureImpl::DoRequestCapabilitiesOnCaptureThread( |
| 468 const RequestCapabilitiesCallback& callback) { |
| 469 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 470 DCHECK(encoding_caps_callback_.is_null()); |
| 471 encoding_caps_callback_ = callback; |
| 472 |
| 473 // Invoke callback immediately if capabilities are already available. |
| 474 if (!encoding_caps_.empty()) |
| 475 base::ResetAndReturn(&encoding_caps_callback_).Run(encoding_caps_); |
| 476 } |
| 477 |
| 478 void VideoCaptureImpl::DoOpenBitstreamOnCaptureThread( |
| 479 media::EncodedVideoSource::Client* client, |
| 480 const media::VideoEncodingParameters& params) { |
| 481 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 482 DCHECK(!encoded_video_source_client_); |
| 483 encoded_video_source_client_ = client; |
| 484 Send(new EncodedVideoCaptureHostMsg_OpenBitstream( |
| 485 device_id_, current_params_.session_id, params)); |
| 486 } |
| 487 |
| 488 void VideoCaptureImpl::DoCloseBitstreamOnCaptureThread() { |
| 489 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 490 DCHECK(bitstream_open_); |
| 491 |
| 492 // Immediately clear EVS client pointer and release bitstream buffers if the |
| 493 // client requests to close bitstream. Any further encoded capture messages |
| 494 // from the browser process will be ignored. |
| 495 bitstream_open_ = false; |
| 496 for (size_t i = 0; i < bitstream_buffers_.size(); ++i) { |
| 497 bitstream_buffers_[i]->Close(); |
| 498 delete bitstream_buffers_[i]; |
| 499 } |
| 500 bitstream_buffers_.clear(); |
| 501 encoded_video_source_client_ = NULL; |
| 502 |
| 503 Send(new EncodedVideoCaptureHostMsg_CloseBitstream(device_id_)); |
| 504 } |
| 505 |
| 506 void VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread( |
| 507 const media::VideoEncodingParameters& params, |
| 508 const std::vector<base::SharedMemoryHandle>& buffers, |
| 509 uint32 buffer_size) { |
| 510 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 511 DCHECK(!bitstream_open_ && bitstream_buffers_.empty()); |
| 512 if (!encoded_video_source_client_) |
| 513 return; |
| 514 bitstream_open_ = true; |
| 515 for (size_t i = 0; i < buffers.size(); ++i) { |
| 516 base::SharedMemory* shm = new base::SharedMemory(buffers[i], true); |
| 517 CHECK(shm->Map(buffer_size)); |
| 518 bitstream_buffers_.push_back(shm); |
| 519 } |
| 520 encoded_video_source_client_->OnOpened(params); |
| 521 } |
| 522 |
| 523 void VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread() { |
| 524 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 525 |
| 526 // Ignore the BitstreamClosed message if bitstream has already been closed |
| 527 // by the EVS client. |
| 528 if (!bitstream_open_) |
| 529 return; |
| 530 |
| 531 // The bitstream may still be open when we receive BitstreamClosed message |
| 532 // if the request to close bitstream comes from the browser process. |
| 533 bitstream_open_ = false; |
| 534 for (size_t i = 0; i < bitstream_buffers_.size(); ++i) { |
| 535 bitstream_buffers_[i]->Close(); |
| 536 delete bitstream_buffers_[i]; |
| 537 } |
| 538 bitstream_buffers_.clear(); |
| 539 if (encoded_video_source_client_) { |
| 540 encoded_video_source_client_->OnClosed(); |
| 541 encoded_video_source_client_ = NULL; |
| 542 } |
| 543 } |
| 544 |
| 545 void VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread( |
| 546 const media::RuntimeVideoEncodingParameters& params) { |
| 547 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 548 if (!encoded_video_source_client_) |
| 549 return; |
| 550 encoded_video_source_client_->OnConfigChanged(params); |
| 551 } |
| 552 |
| 553 void VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread( |
| 554 int buffer_id, |
| 555 uint32 size, |
| 556 const media::BufferEncodingMetadata& metadata) { |
| 557 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 558 if (!encoded_video_source_client_) |
| 559 return; |
| 560 if (buffer_id >= 0 && |
| 561 static_cast<size_t>(buffer_id) < bitstream_buffers_.size()) { |
| 562 base::SharedMemory* shm = bitstream_buffers_.at(buffer_id); |
| 563 scoped_refptr<media::EncodedBitstreamBuffer> buffer = |
| 564 new media::EncodedBitstreamBuffer( |
| 565 buffer_id, (uint8*)shm->memory(), size, shm->handle(), |
| 566 metadata, base::Bind(&base::DoNothing)); |
| 567 encoded_video_source_client_->OnBufferReady(buffer); |
| 568 } |
| 569 } |
| 570 |
| 571 void VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread( |
| 572 const media::VideoEncodingCapabilities& capabilities) { |
| 573 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 574 encoding_caps_ = capabilities; |
| 575 if (!encoding_caps_callback_.is_null()) |
| 576 base::ResetAndReturn(&encoding_caps_callback_).Run(encoding_caps_); |
| 577 } |
| 578 |
| 383 void VideoCaptureImpl::StopDevice() { | 579 void VideoCaptureImpl::StopDevice() { |
| 384 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 580 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 385 | 581 |
| 386 device_info_available_ = false; | 582 device_info_available_ = false; |
| 387 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 583 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 388 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 584 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
| 389 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 585 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 390 current_params_.width = current_params_.height = 0; | 586 current_params_.width = current_params_.height = 0; |
| 391 } | 587 } |
| 392 } | 588 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 if (it != clients->end()) { | 657 if (it != clients->end()) { |
| 462 handler->OnStopped(this); | 658 handler->OnStopped(this); |
| 463 handler->OnRemoved(this); | 659 handler->OnRemoved(this); |
| 464 clients->erase(it); | 660 clients->erase(it); |
| 465 found = true; | 661 found = true; |
| 466 } | 662 } |
| 467 return found; | 663 return found; |
| 468 } | 664 } |
| 469 | 665 |
| 470 } // namespace content | 666 } // namespace content |
| OLD | NEW |