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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, | 96 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, |
95 base::Unretained(this), handler, capability)); | 97 base::Unretained(this), handler, capability)); |
96 } | 98 } |
97 | 99 |
98 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { | 100 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { |
99 capture_message_loop_proxy_->PostTask(FROM_HERE, | 101 capture_message_loop_proxy_->PostTask(FROM_HERE, |
100 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, | 102 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, |
101 base::Unretained(this), handler)); | 103 base::Unretained(this), handler)); |
102 } | 104 } |
103 | 105 |
| 106 void VideoCaptureImpl::RequestCapabilities( |
| 107 const media::EncodedVideoSource::RequestCapabilitiesCallback& callback) { |
| 108 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 109 base::Bind(&VideoCaptureImpl::DoRequestCapabilitiesOnCaptureThread, |
| 110 base::Unretained(this), callback)); |
| 111 } |
| 112 |
| 113 void VideoCaptureImpl::StartFetchCapabilities() { |
| 114 Send(new EncodedVideoCaptureHostMsg_GetCapabilities( |
| 115 device_id_, current_params_.session_id)); |
| 116 } |
| 117 |
| 118 void VideoCaptureImpl::OpenBitstream( |
| 119 media::EncodedVideoSource::Client* client, |
| 120 const media::VideoEncodingParameters& params) { |
| 121 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 122 base::Bind(&VideoCaptureImpl::DoOpenBitstreamOnCaptureThread, |
| 123 base::Unretained(this), client, params)); |
| 124 } |
| 125 |
| 126 void VideoCaptureImpl::CloseBitstream() { |
| 127 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 128 base::Bind(&VideoCaptureImpl::DoCloseBitstreamOnCaptureThread, |
| 129 base::Unretained(this))); |
| 130 } |
| 131 |
| 132 void VideoCaptureImpl::ReturnBitstreamBuffer( |
| 133 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { |
| 134 Send(new EncodedVideoCaptureHostMsg_BitstreamBufferConsumed( |
| 135 device_id_, buffer->buffer_id())); |
| 136 } |
| 137 |
| 138 void VideoCaptureImpl::TrySetBitstreamConfig( |
| 139 const media::RuntimeVideoEncodingParameters& params) { |
| 140 Send(new EncodedVideoCaptureHostMsg_TryConfigureBitstream( |
| 141 device_id_, params)); |
| 142 } |
| 143 |
| 144 void VideoCaptureImpl::OnEncodingCapabilitiesAvailable( |
| 145 const media::VideoEncodingCapabilities& capabilities) { |
| 146 capture_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 147 &VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread, |
| 148 base::Unretained(this), capabilities)); |
| 149 } |
| 150 |
| 151 void VideoCaptureImpl::OnEncodedBitstreamOpened( |
| 152 const media::VideoEncodingParameters& params, |
| 153 const std::vector<base::SharedMemoryHandle>& buffers, |
| 154 uint32 buffer_size) { |
| 155 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 156 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread, |
| 157 base::Unretained(this), params, buffers, buffer_size)); |
| 158 } |
| 159 |
| 160 void VideoCaptureImpl::OnEncodedBitstreamClosed() { |
| 161 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 162 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread, |
| 163 base::Unretained(this))); |
| 164 } |
| 165 |
| 166 void VideoCaptureImpl::OnEncodingConfigChanged( |
| 167 const media::RuntimeVideoEncodingParameters& params) { |
| 168 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 169 base::Bind( |
| 170 &VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread, |
| 171 base::Unretained(this), params)); |
| 172 } |
| 173 |
| 174 void VideoCaptureImpl::OnEncodedBufferReady( |
| 175 int buffer_id, |
| 176 uint32 size, |
| 177 const media::BufferEncodingMetadata& metadata) { |
| 178 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 179 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread, |
| 180 base::Unretained(this), buffer_id, size, metadata)); |
| 181 } |
| 182 |
104 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 183 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
105 capture_message_loop_proxy_->PostTask(FROM_HERE, | 184 capture_message_loop_proxy_->PostTask(FROM_HERE, |
106 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, | 185 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, |
107 base::Unretained(this), buffer)); | 186 base::Unretained(this), buffer)); |
108 } | 187 } |
109 | 188 |
110 void VideoCaptureImpl::OnBufferCreated( | 189 void VideoCaptureImpl::OnBufferCreated( |
111 base::SharedMemoryHandle handle, | 190 base::SharedMemoryHandle handle, |
112 int length, int buffer_id) { | 191 int length, int buffer_id) { |
113 capture_message_loop_proxy_->PostTask(FROM_HERE, | 192 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); | 378 it->first->OnBufferReady(this, buffer); |
300 } | 379 } |
301 cached_dibs_[buffer_id]->references = clients_.size(); | 380 cached_dibs_[buffer_id]->references = clients_.size(); |
302 } | 381 } |
303 | 382 |
304 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { | 383 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { |
305 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 384 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
306 | 385 |
307 switch (state) { | 386 switch (state) { |
308 case VIDEO_CAPTURE_STATE_STARTED: | 387 case VIDEO_CAPTURE_STATE_STARTED: |
| 388 StartFetchCapabilities(); |
309 break; | 389 break; |
310 case VIDEO_CAPTURE_STATE_STOPPED: | 390 case VIDEO_CAPTURE_STATE_STOPPED: |
311 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 391 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
312 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 392 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
313 STLDeleteValues(&cached_dibs_); | 393 STLDeleteValues(&cached_dibs_); |
314 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 394 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
315 RestartCapture(); | 395 RestartCapture(); |
316 break; | 396 break; |
317 case VIDEO_CAPTURE_STATE_PAUSED: | 397 case VIDEO_CAPTURE_STATE_PAUSED: |
318 for (ClientInfo::iterator it = clients_.begin(); | 398 for (ClientInfo::iterator it = clients_.begin(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 } | 453 } |
374 } | 454 } |
375 | 455 |
376 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { | 456 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { |
377 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); | 457 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); |
378 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 458 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
379 | 459 |
380 suspended_ = suspend; | 460 suspended_ = suspend; |
381 } | 461 } |
382 | 462 |
| 463 void VideoCaptureImpl::DoRequestCapabilitiesOnCaptureThread( |
| 464 const RequestCapabilitiesCallback& callback) { |
| 465 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 466 DCHECK(callback_.is_null()); |
| 467 callback_ = callback; |
| 468 |
| 469 // Invoke callback immediately if capabilities are already available. |
| 470 if (!capabilities_.empty()) |
| 471 base::ResetAndReturn(&callback_).Run(capabilities_); |
| 472 } |
| 473 |
| 474 void VideoCaptureImpl::DoOpenBitstreamOnCaptureThread( |
| 475 media::EncodedVideoSource::Client* client, |
| 476 const media::VideoEncodingParameters& params) { |
| 477 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 478 DCHECK(!client_); |
| 479 client_ = client; |
| 480 Send(new EncodedVideoCaptureHostMsg_OpenBitstream( |
| 481 device_id_, current_params_.session_id, params)); |
| 482 } |
| 483 |
| 484 void VideoCaptureImpl::DoCloseBitstreamOnCaptureThread() { |
| 485 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 486 Send(new EncodedVideoCaptureHostMsg_CloseBitstream(device_id_)); |
| 487 client_ = NULL; |
| 488 } |
| 489 |
| 490 void VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread( |
| 491 const media::VideoEncodingParameters& params, |
| 492 const std::vector<base::SharedMemoryHandle>& buffers, |
| 493 uint32 buffer_size) { |
| 494 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 495 DCHECK(bitstream_buffers_.empty()); |
| 496 for (size_t i = 0; i < buffers.size(); ++i) { |
| 497 base::SharedMemory* shm = new base::SharedMemory(buffers[i], true); |
| 498 bitstream_buffers_.push_back(shm); |
| 499 } |
| 500 client_->OnOpened(params); |
| 501 } |
| 502 |
| 503 void VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread() { |
| 504 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 505 for (size_t i = 0; i < bitstream_buffers_.size(); ++i) { |
| 506 bitstream_buffers_[i]->Close(); |
| 507 delete bitstream_buffers_[i]; |
| 508 } |
| 509 bitstream_buffers_.clear(); |
| 510 if (client_) { |
| 511 client_->OnClosed(); |
| 512 client_ = NULL; |
| 513 } |
| 514 } |
| 515 |
| 516 void VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread( |
| 517 const media::RuntimeVideoEncodingParameters& params) { |
| 518 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 519 client_->OnConfigChanged(params); |
| 520 } |
| 521 |
| 522 void VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread( |
| 523 int buffer_id, |
| 524 uint32 size, |
| 525 const media::BufferEncodingMetadata& metadata) { |
| 526 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 527 if (buffer_id >= 0 && buffer_id < (int)bitstream_buffers_.size()) { |
| 528 base::SharedMemory* shm = bitstream_buffers_[buffer_id]; |
| 529 if (shm && shm->Map(size)) { |
| 530 scoped_refptr<media::EncodedBitstreamBuffer> buffer = |
| 531 new media::EncodedBitstreamBuffer( |
| 532 buffer_id, (uint8*)shm->memory(), size, metadata); |
| 533 client_->OnBufferReady(buffer); |
| 534 shm->Unmap(); |
| 535 } |
| 536 } |
| 537 } |
| 538 |
| 539 void VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread( |
| 540 const media::VideoEncodingCapabilities& capabilities) { |
| 541 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 542 capabilities_ = capabilities; |
| 543 if (!callback_.is_null()) |
| 544 base::ResetAndReturn(&callback_).Run(capabilities_); |
| 545 } |
| 546 |
383 void VideoCaptureImpl::StopDevice() { | 547 void VideoCaptureImpl::StopDevice() { |
384 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 548 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
385 | 549 |
386 device_info_available_ = false; | 550 device_info_available_ = false; |
387 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 551 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
388 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 552 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
389 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 553 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
390 current_params_.width = current_params_.height = 0; | 554 current_params_.width = current_params_.height = 0; |
391 } | 555 } |
392 } | 556 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 if (it != clients->end()) { | 625 if (it != clients->end()) { |
462 handler->OnStopped(this); | 626 handler->OnStopped(this); |
463 handler->OnRemoved(this); | 627 handler->OnRemoved(this); |
464 clients->erase(it); | 628 clients->erase(it); |
465 found = true; | 629 found = true; |
466 } | 630 } |
467 return found; | 631 return found; |
468 } | 632 } |
469 | 633 |
470 } // namespace content | 634 } // namespace content |
OLD | NEW |