| 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 // Implementation notes: This needs to work on a variety of hardware | 5 // Implementation notes: This needs to work on a variety of hardware |
| 6 // configurations where the speed of the CPU and GPU greatly affect overall | 6 // configurations where the speed of the CPU and GPU greatly affect overall |
| 7 // performance. Spanning several threads, the process of capturing has been | 7 // performance. Spanning several threads, the process of capturing has been |
| 8 // split up into four conceptual stages: | 8 // split up into four conceptual stages: |
| 9 // | 9 // |
| 10 // 1. Reserve Buffer: Before a frame can be captured, a slot in the client's | 10 // 1. Reserve Buffer: Before a frame can be captured, a slot in the client's |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 void Stop(); | 157 void Stop(); |
| 158 | 158 |
| 159 // Signal an error to the client. | 159 // Signal an error to the client. |
| 160 void ReportError(); | 160 void ReportError(); |
| 161 | 161 |
| 162 private: | 162 private: |
| 163 friend class base::RefCountedThreadSafe<ThreadSafeCaptureOracle>; | 163 friend class base::RefCountedThreadSafe<ThreadSafeCaptureOracle>; |
| 164 virtual ~ThreadSafeCaptureOracle() {} | 164 virtual ~ThreadSafeCaptureOracle() {} |
| 165 | 165 |
| 166 // Callback invoked on completion of all captures. | 166 // Callback invoked on completion of all captures. |
| 167 void DidCaptureFrame(const scoped_refptr<media::VideoFrame>& frame, | 167 void DidCaptureFrame( |
| 168 int frame_number, | 168 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 169 base::Time timestamp, | 169 int frame_number, |
| 170 bool success); | 170 base::Time timestamp, |
| 171 bool success); |
| 171 // Protects everything below it. | 172 // Protects everything below it. |
| 172 base::Lock lock_; | 173 base::Lock lock_; |
| 173 | 174 |
| 174 // Recipient of our capture activity. | 175 // Recipient of our capture activity. |
| 175 scoped_ptr<media::VideoCaptureDevice::Client> client_; | 176 scoped_ptr<media::VideoCaptureDevice::Client> client_; |
| 176 | 177 |
| 177 // Makes the decision to capture a frame. | 178 // Makes the decision to capture a frame. |
| 178 const scoped_ptr<VideoCaptureOracle> oracle_; | 179 const scoped_ptr<VideoCaptureOracle> oracle_; |
| 179 | 180 |
| 180 // The current capturing resolution and frame rate. | 181 // The current capturing resolution and frame rate. |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( | 415 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( |
| 415 VideoCaptureOracle::Event event, | 416 VideoCaptureOracle::Event event, |
| 416 base::Time event_time, | 417 base::Time event_time, |
| 417 scoped_refptr<media::VideoFrame>* storage, | 418 scoped_refptr<media::VideoFrame>* storage, |
| 418 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback* callback) { | 419 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback* callback) { |
| 419 base::AutoLock guard(lock_); | 420 base::AutoLock guard(lock_); |
| 420 | 421 |
| 421 if (!client_) | 422 if (!client_) |
| 422 return false; // Capture is stopped. | 423 return false; // Capture is stopped. |
| 423 | 424 |
| 424 scoped_refptr<media::VideoFrame> output_buffer = | 425 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> output_buffer = |
| 425 client_->ReserveOutputBuffer(capture_size_); | 426 client_->ReserveOutputBuffer(media::VideoFrame::I420, capture_size_); |
| 426 const bool should_capture = | 427 const bool should_capture = |
| 427 oracle_->ObserveEventAndDecideCapture(event, event_time); | 428 oracle_->ObserveEventAndDecideCapture(event, event_time); |
| 428 const bool content_is_dirty = | 429 const bool content_is_dirty = |
| 429 (event == VideoCaptureOracle::kCompositorUpdate || | 430 (event == VideoCaptureOracle::kCompositorUpdate || |
| 430 event == VideoCaptureOracle::kSoftwarePaint); | 431 event == VideoCaptureOracle::kSoftwarePaint); |
| 431 const char* event_name = | 432 const char* event_name = |
| 432 (event == VideoCaptureOracle::kTimerPoll ? "poll" : | 433 (event == VideoCaptureOracle::kTimerPoll ? "poll" : |
| 433 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : | 434 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : |
| 434 "paint")); | 435 "paint")); |
| 435 | 436 |
| 436 // Consider the various reasons not to initiate a capture. | 437 // Consider the various reasons not to initiate a capture. |
| 437 if (should_capture && !output_buffer.get()) { | 438 if (should_capture && !output_buffer) { |
| 438 TRACE_EVENT_INSTANT1("mirroring", | 439 TRACE_EVENT_INSTANT1("mirroring", |
| 439 "EncodeLimited", | 440 "EncodeLimited", |
| 440 TRACE_EVENT_SCOPE_THREAD, | 441 TRACE_EVENT_SCOPE_THREAD, |
| 441 "trigger", | 442 "trigger", |
| 442 event_name); | 443 event_name); |
| 443 return false; | 444 return false; |
| 444 } else if (!should_capture && output_buffer.get()) { | 445 } else if (!should_capture && output_buffer) { |
| 445 if (content_is_dirty) { | 446 if (content_is_dirty) { |
| 446 // This is a normal and acceptable way to drop a frame. We've hit our | 447 // This is a normal and acceptable way to drop a frame. We've hit our |
| 447 // capture rate limit: for example, the content is animating at 60fps but | 448 // capture rate limit: for example, the content is animating at 60fps but |
| 448 // we're capturing at 30fps. | 449 // we're capturing at 30fps. |
| 449 TRACE_EVENT_INSTANT1("mirroring", "FpsRateLimited", | 450 TRACE_EVENT_INSTANT1("mirroring", "FpsRateLimited", |
| 450 TRACE_EVENT_SCOPE_THREAD, | 451 TRACE_EVENT_SCOPE_THREAD, |
| 451 "trigger", event_name); | 452 "trigger", event_name); |
| 452 } | 453 } |
| 453 return false; | 454 return false; |
| 454 } else if (!should_capture && !output_buffer.get()) { | 455 } else if (!should_capture && !output_buffer) { |
| 455 // We decided not to capture, but we wouldn't have been able to if we wanted | 456 // We decided not to capture, but we wouldn't have been able to if we wanted |
| 456 // to because no output buffer was available. | 457 // to because no output buffer was available. |
| 457 TRACE_EVENT_INSTANT1("mirroring", "NearlyEncodeLimited", | 458 TRACE_EVENT_INSTANT1("mirroring", "NearlyEncodeLimited", |
| 458 TRACE_EVENT_SCOPE_THREAD, | 459 TRACE_EVENT_SCOPE_THREAD, |
| 459 "trigger", event_name); | 460 "trigger", event_name); |
| 460 return false; | 461 return false; |
| 461 } | 462 } |
| 462 int frame_number = oracle_->RecordCapture(); | 463 int frame_number = oracle_->RecordCapture(); |
| 463 TRACE_EVENT_ASYNC_BEGIN2("mirroring", "Capture", output_buffer.get(), | 464 TRACE_EVENT_ASYNC_BEGIN2("mirroring", "Capture", output_buffer.get(), |
| 464 "frame_number", frame_number, | 465 "frame_number", frame_number, |
| 465 "trigger", event_name); | 466 "trigger", event_name); |
| 466 *storage = output_buffer; | 467 |
| 467 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, | 468 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, |
| 468 this, output_buffer, frame_number); | 469 this, |
| 470 output_buffer, |
| 471 frame_number); |
| 472 *storage = media::VideoFrame::WrapExternalPackedMemory( |
| 473 media::VideoFrame::I420, |
| 474 capture_size_, |
| 475 gfx::Rect(capture_size_), |
| 476 capture_size_, |
| 477 static_cast<uint8*>(output_buffer->data()), |
| 478 output_buffer->size(), |
| 479 base::SharedMemory::NULLHandle(), |
| 480 base::TimeDelta(), |
| 481 base::Closure()); |
| 469 return true; | 482 return true; |
| 470 } | 483 } |
| 471 | 484 |
| 472 void ThreadSafeCaptureOracle::Stop() { | 485 void ThreadSafeCaptureOracle::Stop() { |
| 473 base::AutoLock guard(lock_); | 486 base::AutoLock guard(lock_); |
| 474 client_.reset(); | 487 client_.reset(); |
| 475 } | 488 } |
| 476 | 489 |
| 477 void ThreadSafeCaptureOracle::ReportError() { | 490 void ThreadSafeCaptureOracle::ReportError() { |
| 478 base::AutoLock guard(lock_); | 491 base::AutoLock guard(lock_); |
| 479 if (client_) | 492 if (client_) |
| 480 client_->OnError(); | 493 client_->OnError(); |
| 481 } | 494 } |
| 482 | 495 |
| 483 void ThreadSafeCaptureOracle::DidCaptureFrame( | 496 void ThreadSafeCaptureOracle::DidCaptureFrame( |
| 484 const scoped_refptr<media::VideoFrame>& frame, | 497 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer, |
| 485 int frame_number, | 498 int frame_number, |
| 486 base::Time timestamp, | 499 base::Time timestamp, |
| 487 bool success) { | 500 bool success) { |
| 488 base::AutoLock guard(lock_); | 501 base::AutoLock guard(lock_); |
| 489 TRACE_EVENT_ASYNC_END2("mirroring", "Capture", frame.get(), | 502 TRACE_EVENT_ASYNC_END2("mirroring", "Capture", buffer.get(), |
| 490 "success", success, | 503 "success", success, |
| 491 "timestamp", timestamp.ToInternalValue()); | 504 "timestamp", timestamp.ToInternalValue()); |
| 492 | 505 |
| 493 if (!client_) | 506 if (!client_) |
| 494 return; // Capture is stopped. | 507 return; // Capture is stopped. |
| 495 | 508 |
| 496 if (success) { | 509 if (success) { |
| 497 if (oracle_->CompleteCapture(frame_number, timestamp)) | 510 if (oracle_->CompleteCapture(frame_number, timestamp)) { |
| 498 client_->OnIncomingCapturedVideoFrame( | 511 client_->OnIncomingCapturedBuffer(buffer, |
| 499 frame, timestamp, frame_rate_); | 512 media::VideoFrame::I420, |
| 513 capture_size_, |
| 514 timestamp, |
| 515 frame_rate_); |
| 516 } |
| 500 } | 517 } |
| 501 } | 518 } |
| 502 | 519 |
| 503 bool FrameSubscriber::ShouldCaptureFrame( | 520 bool FrameSubscriber::ShouldCaptureFrame( |
| 504 base::Time present_time, | 521 base::Time present_time, |
| 505 scoped_refptr<media::VideoFrame>* storage, | 522 scoped_refptr<media::VideoFrame>* storage, |
| 506 DeliverFrameCallback* deliver_frame_cb) { | 523 DeliverFrameCallback* deliver_frame_cb) { |
| 507 TRACE_EVENT1("mirroring", "FrameSubscriber::ShouldCaptureFrame", | 524 TRACE_EVENT1("mirroring", "FrameSubscriber::ShouldCaptureFrame", |
| 508 "instance", this); | 525 "instance", this); |
| 509 | 526 |
| (...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1180 capture_format.height, | 1197 capture_format.height, |
| 1181 capture_format.frame_rate, | 1198 capture_format.frame_rate, |
| 1182 client.Pass()); | 1199 client.Pass()); |
| 1183 } | 1200 } |
| 1184 | 1201 |
| 1185 void WebContentsVideoCaptureDevice::StopAndDeAllocate() { | 1202 void WebContentsVideoCaptureDevice::StopAndDeAllocate() { |
| 1186 impl_->StopAndDeAllocate(); | 1203 impl_->StopAndDeAllocate(); |
| 1187 } | 1204 } |
| 1188 | 1205 |
| 1189 } // namespace content | 1206 } // namespace content |
| OLD | NEW |