Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: content/browser/renderer_host/media/web_contents_video_capture_device.cc

Issue 12090109: Tab Capture: Backing store readbacks to YV12 VideoFrames. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch for review. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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. Therefore, the process of capturing has been split up into a 7 // performance. Therefore, the process of capturing has been split up into a
8 // pipeline of three stages. Each stage executes on its own thread: 8 // pipeline of three stages. Each stage executes on its own thread:
9 // 9 //
10 // 1. Capture: A bitmap is snapshotted/copied from the RenderView's backing 10 // 1. Capture: A bitmap is snapshotted/copied from the RenderView's backing
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 #include "base/time.h" 57 #include "base/time.h"
58 #include "content/browser/renderer_host/media/web_contents_capture_util.h" 58 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
59 #include "content/browser/web_contents/web_contents_impl.h" 59 #include "content/browser/web_contents/web_contents_impl.h"
60 #include "content/public/browser/browser_thread.h" 60 #include "content/public/browser/browser_thread.h"
61 #include "content/public/browser/render_process_host.h" 61 #include "content/public/browser/render_process_host.h"
62 #include "content/public/browser/render_view_host.h" 62 #include "content/public/browser/render_view_host.h"
63 #include "content/public/browser/render_widget_host_view.h" 63 #include "content/public/browser/render_widget_host_view.h"
64 #include "content/public/browser/web_contents.h" 64 #include "content/public/browser/web_contents.h"
65 #include "content/public/browser/web_contents_observer.h" 65 #include "content/public/browser/web_contents_observer.h"
66 #include "media/base/bind_to_loop.h" 66 #include "media/base/bind_to_loop.h"
67 #include "media/base/video_frame.h"
67 #include "media/video/capture/video_capture_types.h" 68 #include "media/video/capture/video_capture_types.h"
68 #include "skia/ext/image_operations.h" 69 #include "skia/ext/image_operations.h"
69 #include "third_party/skia/include/core/SkBitmap.h" 70 #include "third_party/skia/include/core/SkBitmap.h"
70 #include "third_party/skia/include/core/SkColor.h" 71 #include "third_party/skia/include/core/SkColor.h"
71 #include "ui/gfx/rect.h" 72 #include "ui/gfx/rect.h"
73 #include "ui/gfx/skia_util.h"
72 74
73 // Used to self-trampoline invocation of methods to the approprate thread. This 75 // Used to self-trampoline invocation of methods to the approprate thread. This
74 // should be used sparingly, only when it's not clear which thread is invoking a 76 // should be used sparingly, only when it's not clear which thread is invoking a
75 // method. 77 // method.
76 #define ENSURE_INVOKED_ON_THREAD(thread, ...) { \ 78 #define ENSURE_INVOKED_ON_THREAD(thread, ...) { \
77 DCHECK(thread.IsRunning()); \ 79 DCHECK(thread.IsRunning()); \
78 if (MessageLoop::current() != thread.message_loop()) { \ 80 if (MessageLoop::current() != thread.message_loop()) { \
79 thread.message_loop()->PostTask(FROM_HERE, base::Bind(__VA_ARGS__)); \ 81 thread.message_loop()->PostTask(FROM_HERE, base::Bind(__VA_ARGS__)); \
80 return; \ 82 return; \
81 } \ 83 } \
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 fitted_width = std::max(kMinFrameWidth, MakeEven(fitted_width)); 134 fitted_width = std::max(kMinFrameWidth, MakeEven(fitted_width));
133 fitted_height = std::max(kMinFrameHeight, MakeEven(fitted_height)); 135 fitted_height = std::max(kMinFrameHeight, MakeEven(fitted_height));
134 136
135 *fitted_size = gfx::Size(fitted_width, fitted_height); 137 *fitted_size = gfx::Size(fitted_width, fitted_height);
136 } 138 }
137 139
138 // Keeps track of the RenderView to be sourced, and executes copying of the 140 // Keeps track of the RenderView to be sourced, and executes copying of the
139 // backing store on the UI BrowserThread. 141 // backing store on the UI BrowserThread.
140 class BackingStoreCopier : public WebContentsObserver { 142 class BackingStoreCopier : public WebContentsObserver {
141 public: 143 public:
142 // Result status and done callback used with StartCopy().
143 enum Result {
144 OK,
145 TRANSIENT_ERROR,
146 NO_SOURCE,
147 };
148 typedef base::Callback<void(Result result,
149 const SkBitmap& capture,
150 const base::Time& capture_time)> DoneCB;
151
152 BackingStoreCopier(int render_process_id, int render_view_id); 144 BackingStoreCopier(int render_process_id, int render_view_id);
153 145
154 // If non-NULL, use the given |override| to access the backing store. 146 // If non-NULL, use the given |override| to access the backing store.
155 // This is used for unit testing. 147 // This is used for unit testing.
156 void SetRenderWidgetHostForTesting(RenderWidgetHost* override); 148 void SetRenderWidgetHostForTesting(RenderWidgetHost* override);
157 149
158 // Starts the copy from the backing store. Must be run on the UI 150 // Starts the copy from the backing store. Must be run on the UI
159 // BrowserThread. |done_cb| is invoked with result status. When successful 151 // BrowserThread. Resulting frame is conveyed back to |consumer|.
160 // (OK), the bitmap of the capture is transferred to the callback along with 152 void StartCopy(CaptureMachine* consumer,
161 // the timestamp at which the capture was completed. 153 int frame_number,
162 void StartCopy(int frame_number, int desired_width, int desired_height, 154 int desired_width,
163 const DoneCB& done_cb); 155 int desired_height);
164 156
157 // content::WebContentsObserver implementation.
165 virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE { 158 virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
167 fullscreen_widget_id_ = routing_id; 160 fullscreen_widget_id_ = routing_id;
168 } 161 }
169 162
170 virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE { 163 virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE {
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
172 DCHECK_EQ(fullscreen_widget_id_, routing_id); 165 DCHECK_EQ(fullscreen_widget_id_, routing_id);
173 fullscreen_widget_id_ = MSG_ROUTING_NONE; 166 fullscreen_widget_id_ = MSG_ROUTING_NONE;
174 } 167 }
175 168
176 private: 169 private:
177 void LookUpAndObserveWebContents(); 170 void LookUpAndObserveWebContents();
178 171
179 void CopyFromBackingStoreComplete(int frame_number, 172 // Response callback for RenderWidgetHost::CopyFromBackingStore.
180 const DoneCB& done_cb, 173 void DidCopyFromBackingStore(CaptureMachine* consumer,
181 bool success, 174 int frame_number,
182 const SkBitmap& result); 175 const base::Time& start_time,
176 bool success,
177 const SkBitmap& frame);
178
179 // Response callback for RenderWidgetHost::CopyFromBackingStoreToVideoFrame.
180 void DidCopyFromBackingStoreToVideoFrame(
181 CaptureMachine* consumer,
182 int frame_number,
183 const base::Time& start_time,
184 media::VideoFrame* frame);
183 185
184 // The "starting point" to find the capture source. 186 // The "starting point" to find the capture source.
185 const int render_process_id_; 187 const int render_process_id_;
186 const int render_view_id_; 188 const int render_view_id_;
187 189
188 // Routing ID of any active fullscreen render widget or MSG_ROUTING_NONE 190 // Routing ID of any active fullscreen render widget or MSG_ROUTING_NONE
189 // otherwise. 191 // otherwise.
190 int fullscreen_widget_id_; 192 int fullscreen_widget_id_;
191 193
192 // Last known RenderView size. 194 // Last known RenderView size.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 class SynchronizedConsumer { 244 class SynchronizedConsumer {
243 public: 245 public:
244 SynchronizedConsumer(); 246 SynchronizedConsumer();
245 247
246 void SetConsumer(media::VideoCaptureDevice::EventHandler* consumer); 248 void SetConsumer(media::VideoCaptureDevice::EventHandler* consumer);
247 249
248 void OnFrameInfo(const media::VideoCaptureCapability& info); 250 void OnFrameInfo(const media::VideoCaptureCapability& info);
249 void OnError(); 251 void OnError();
250 void OnIncomingCapturedFrame(const uint8* pixels, int size, 252 void OnIncomingCapturedFrame(const uint8* pixels, int size,
251 const base::Time& timestamp); 253 const base::Time& timestamp);
254 void OnIncomingCapturedVideoFrame(media::VideoFrame* video_frame,
255 const base::Time& timestamp);
252 256
253 private: 257 private:
254 base::Lock consumer_lock_; 258 base::Lock consumer_lock_;
255 media::VideoCaptureDevice::EventHandler* wrapped_consumer_; 259 media::VideoCaptureDevice::EventHandler* wrapped_consumer_;
256 260
257 DISALLOW_COPY_AND_ASSIGN(SynchronizedConsumer); 261 DISALLOW_COPY_AND_ASSIGN(SynchronizedConsumer);
258 }; 262 };
259 263
260 // Delivers rendered video frames to a consumer on a separate thread. Also 264 // Delivers rendered video frames to a consumer on a separate thread. Also
261 // responsible for logging the effective frame rate. 265 // responsible for logging the effective frame rate.
262 class VideoFrameDeliverer { 266 class VideoFrameDeliverer {
263 public: 267 public:
264 explicit VideoFrameDeliverer(SynchronizedConsumer* consumer); 268 explicit VideoFrameDeliverer(SynchronizedConsumer* consumer);
265 269
270 // Deliver a fully rendered ARGB frame, using SkBitmap as a container.
271 // |done_cb| will be invoked after delivery is complete.
266 void Deliver(int frame_number, 272 void Deliver(int frame_number,
267 const SkBitmap& frame_buffer, const base::Time& frame_timestamp, 273 const SkBitmap& frame_buffer,
274 const base::Time& frame_timestamp,
268 const base::Closure& done_cb); 275 const base::Closure& done_cb);
276 // Deliver a fully rendered frame YV12 frame, using VideoFrame as a container.
277 // A refcount is taken on |frame| until delivery is complete.
278 void Deliver(int frame_number,
279 media::VideoFrame* frame,
280 const base::Time& frame_timestamp);
269 281
270 private: 282 private:
271 void DeliverOnDeliverThread(int frame_number, 283 void DeliverOnDeliverThread(int frame_number,
272 const SkBitmap& frame_buffer, 284 const SkBitmap& frame,
273 const base::Time& frame_timestamp, 285 const base::Time& frame_timestamp,
274 const base::Closure& done_cb); 286 const base::Closure& done_cb);
287 void DeliverVideoFrameOnDeliverThread(int frame_number,
288 media::VideoFrame* frame,
289 const base::Time& frame_timestamp);
290
291 // Treat |frame_number| as having been delivered, and update the
292 // frame rate statistics accordingly.
293 void ChronicleFrameDelivery(int frame_number);
275 294
276 base::Thread deliver_thread_; 295 base::Thread deliver_thread_;
277 SynchronizedConsumer* const consumer_; 296 SynchronizedConsumer* const consumer_;
278 297
279 // The following keep track of and log the effective frame rate (from the 298 // The following keep track of and log the effective frame rate (from the
280 // deliver stage) whenever verbose logging is turned on. 299 // deliver stage) whenever verbose logging is turned on.
281 base::Time last_frame_rate_log_time_; 300 base::Time last_frame_rate_log_time_;
282 int count_frames_rendered_; 301 int count_frames_rendered_;
283 int last_frame_number_; 302 int last_frame_number_;
284 303
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 fullscreen_widget_id_ = static_cast<WebContentsImpl*>(web_contents())-> 335 fullscreen_widget_id_ = static_cast<WebContentsImpl*>(web_contents())->
317 GetFullscreenWidgetRoutingID(); 336 GetFullscreenWidgetRoutingID();
318 } 337 }
319 } 338 }
320 339
321 void BackingStoreCopier::SetRenderWidgetHostForTesting( 340 void BackingStoreCopier::SetRenderWidgetHostForTesting(
322 RenderWidgetHost* override) { 341 RenderWidgetHost* override) {
323 rwh_for_testing_ = override; 342 rwh_for_testing_ = override;
324 } 343 }
325 344
326 void BackingStoreCopier::StartCopy(int frame_number,
327 int desired_width, int desired_height,
328 const DoneCB& done_cb) {
329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
330
331 TRACE_EVENT_ASYNC_BEGIN1("mirroring", "Capture", this,
332 "frame_number", frame_number);
333
334 RenderWidgetHost* rwh;
335 if (rwh_for_testing_) {
336 rwh = rwh_for_testing_;
337 } else {
338 if (!web_contents()) { // No source yet.
339 LookUpAndObserveWebContents();
340 if (!web_contents()) { // No source ever.
341 done_cb.Run(NO_SOURCE, SkBitmap(), base::Time());
342 return;
343 }
344 }
345
346 if (fullscreen_widget_id_ != MSG_ROUTING_NONE) {
347 RenderProcessHost* process = web_contents()->GetRenderProcessHost();
348 rwh = process ? process->GetRenderWidgetHostByID(fullscreen_widget_id_)
349 : NULL;
350 } else {
351 rwh = web_contents()->GetRenderViewHost();
352 }
353
354 if (!rwh) {
355 // Transient failure state (e.g., a RenderView is being replaced).
356 done_cb.Run(TRANSIENT_ERROR, SkBitmap(), base::Time());
357 return;
358 }
359 }
360
361 gfx::Size fitted_size;
362 if (RenderWidgetHostView* const view = rwh->GetView()) {
363 const gfx::Size& view_size = view->GetViewBounds().size();
364 if (!view_size.IsEmpty()) {
365 CalculateFittedSize(view_size.width(), view_size.height(),
366 desired_width, desired_height,
367 &fitted_size);
368 }
369 if (view_size != last_view_size_) {
370 last_view_size_ = view_size;
371
372 // Measure the number of kilopixels.
373 UMA_HISTOGRAM_COUNTS_10000(
374 "TabCapture.ViewChangeKiloPixels",
375 view_size.width() * view_size.height() / 1024);
376 }
377 }
378
379 rwh->CopyFromBackingStore(
380 gfx::Rect(),
381 fitted_size,
382 base::Bind(&BackingStoreCopier::CopyFromBackingStoreComplete,
383 base::Unretained(this),
384 frame_number, done_cb));
385
386 // TODO(miu): When a tab is not visible to the user, rendering stops. For
387 // mirroring, however, it's important that rendering continues to happen.
388 }
389
390 void BackingStoreCopier::CopyFromBackingStoreComplete(
391 int frame_number,
392 const DoneCB& done_cb,
393 bool success,
394 const SkBitmap& frame) {
395 // Note: No restriction on which thread invokes this method but, currently,
396 // it's always the UI BrowserThread.
397 TRACE_EVENT_ASYNC_END1("mirroring", "Capture", this,
398 "frame_number", frame_number);
399 if (success) {
400 done_cb.Run(OK, frame, base::Time::Now());
401 } else {
402 // Capture can fail due to transient issues, so just skip this frame.
403 DVLOG(1) << "CopyFromBackingStore was not successful; skipping frame.";
404 done_cb.Run(TRANSIENT_ERROR, SkBitmap(), base::Time());
405 }
406 }
407
408 VideoFrameRenderer::VideoFrameRenderer() 345 VideoFrameRenderer::VideoFrameRenderer()
409 : render_thread_("WebContentsVideo_RenderThread") { 346 : render_thread_("WebContentsVideo_RenderThread") {
410 output_[0].in_use = false; 347 output_[0].in_use = false;
411 output_[1].in_use = false; 348 output_[1].in_use = false;
412 render_thread_.Start(); 349 render_thread_.Start();
413 } 350 }
414 351
415 void VideoFrameRenderer::Render(int frame_number, 352 void VideoFrameRenderer::Render(int frame_number,
416 const SkBitmap& capture, 353 const SkBitmap& capture,
417 int frame_width, int frame_height, 354 int frame_width, int frame_height,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 } 504 }
568 505
569 void SynchronizedConsumer::OnIncomingCapturedFrame( 506 void SynchronizedConsumer::OnIncomingCapturedFrame(
570 const uint8* pixels, int size, const base::Time& timestamp) { 507 const uint8* pixels, int size, const base::Time& timestamp) {
571 base::AutoLock guard(consumer_lock_); 508 base::AutoLock guard(consumer_lock_);
572 if (wrapped_consumer_) { 509 if (wrapped_consumer_) {
573 wrapped_consumer_->OnIncomingCapturedFrame(pixels, size, timestamp); 510 wrapped_consumer_->OnIncomingCapturedFrame(pixels, size, timestamp);
574 } 511 }
575 } 512 }
576 513
514 void SynchronizedConsumer::OnIncomingCapturedVideoFrame(
515 media::VideoFrame* video_frame,
516 const base::Time& timestamp) {
517 base::AutoLock guard(consumer_lock_);
518 if (wrapped_consumer_) {
519 wrapped_consumer_->OnIncomingCapturedVideoFrame(video_frame, timestamp);
520 }
521 }
522
577 VideoFrameDeliverer::VideoFrameDeliverer(SynchronizedConsumer* consumer) 523 VideoFrameDeliverer::VideoFrameDeliverer(SynchronizedConsumer* consumer)
578 : deliver_thread_("WebContentsVideo_DeliverThread"), 524 : deliver_thread_("WebContentsVideo_DeliverThread"),
579 consumer_(consumer), 525 consumer_(consumer),
580 last_frame_number_(0) { 526 last_frame_number_(0) {
581 DCHECK(consumer_); 527 DCHECK(consumer_);
582 deliver_thread_.Start(); 528 deliver_thread_.Start();
583 } 529 }
584 530
585 void VideoFrameDeliverer::Deliver( 531 void VideoFrameDeliverer::Deliver(
586 int frame_number, 532 int frame_number,
587 const SkBitmap& frame_buffer, const base::Time& frame_timestamp, 533 const SkBitmap& frame_buffer, const base::Time& frame_timestamp,
588 const base::Closure& done_cb) { 534 const base::Closure& done_cb) {
589 deliver_thread_.message_loop()->PostTask( 535 deliver_thread_.message_loop()->PostTask(
590 FROM_HERE, 536 FROM_HERE,
591 base::Bind(&VideoFrameDeliverer::DeliverOnDeliverThread, 537 base::Bind(&VideoFrameDeliverer::DeliverOnDeliverThread,
592 base::Unretained(this), 538 base::Unretained(this),
593 frame_number, base::ConstRef(frame_buffer), frame_timestamp, 539 frame_number, base::ConstRef(frame_buffer), frame_timestamp,
594 done_cb)); 540 done_cb));
595 } 541 }
596 542
543 void VideoFrameDeliverer::Deliver(
544 int frame_number,
545 media::VideoFrame* frame, const base::Time& frame_timestamp) {
546 deliver_thread_.message_loop()->PostTask(
547 FROM_HERE,
548 base::Bind(&VideoFrameDeliverer::DeliverVideoFrameOnDeliverThread,
549 base::Unretained(this),
550 frame_number, make_scoped_refptr(frame), frame_timestamp));
551 }
552
597 void VideoFrameDeliverer::DeliverOnDeliverThread( 553 void VideoFrameDeliverer::DeliverOnDeliverThread(
598 int frame_number, 554 int frame_number,
599 const SkBitmap& frame_buffer, const base::Time& frame_timestamp, 555 const SkBitmap& frame_buffer, const base::Time& frame_timestamp,
600 const base::Closure& done_cb) { 556 const base::Closure& done_cb) {
601 DCHECK_EQ(deliver_thread_.message_loop(), MessageLoop::current()); 557 DCHECK_EQ(deliver_thread_.message_loop(), MessageLoop::current());
602 558
603 TRACE_EVENT1("mirroring", "DeliverFrame", "frame_number", frame_number); 559 TRACE_EVENT1("mirroring", "DeliverFrame", "frame_number", frame_number);
604 560
605 // Send the frame to the consumer. 561 // Send the frame to the consumer.
606 // Note: The consumer will do an ARGB-->YUV conversion in this callback, 562 // Note: The consumer will do an ARGB-->YUV conversion in this callback,
607 // blocking the current thread for a bit. 563 // blocking the current thread for a bit.
608 SkAutoLockPixels frame_buffer_locker(frame_buffer); 564 SkAutoLockPixels frame_buffer_locker(frame_buffer);
609 consumer_->OnIncomingCapturedFrame( 565 consumer_->OnIncomingCapturedFrame(
610 static_cast<const uint8*>(frame_buffer.getPixels()), 566 static_cast<const uint8*>(frame_buffer.getPixels()),
611 frame_buffer.getSize(), 567 frame_buffer.getSize(),
612 frame_timestamp); 568 frame_timestamp);
613 569
570 ChronicleFrameDelivery(frame_number);
571
572 // All done.
573 done_cb.Run();
574 }
575
576 void VideoFrameDeliverer::DeliverVideoFrameOnDeliverThread(
577 int frame_number,
578 media::VideoFrame* frame, const base::Time& frame_timestamp) {
579 DCHECK_EQ(deliver_thread_.message_loop(), MessageLoop::current());
580
581 TRACE_EVENT1("mirroring", "DeliverFrame", "frame_number", frame_number);
582
583 // Send the frame to the consumer.
584 consumer_->OnIncomingCapturedVideoFrame(frame, frame_timestamp);
585
586 ChronicleFrameDelivery(frame_number);
587 }
588
589 void VideoFrameDeliverer::ChronicleFrameDelivery(int frame_number) {
614 // Log frame rate, if verbose logging is turned on. 590 // Log frame rate, if verbose logging is turned on.
615 static const base::TimeDelta kFrameRateLogInterval = 591 static const base::TimeDelta kFrameRateLogInterval =
616 base::TimeDelta::FromSeconds(10); 592 base::TimeDelta::FromSeconds(10);
617 const base::Time& now = base::Time::Now(); 593 const base::Time& now = base::Time::Now();
618 if (last_frame_rate_log_time_.is_null()) { 594 if (last_frame_rate_log_time_.is_null()) {
619 last_frame_rate_log_time_ = now; 595 last_frame_rate_log_time_ = now;
620 count_frames_rendered_ = 0; 596 count_frames_rendered_ = 0;
621 last_frame_number_ = frame_number; 597 last_frame_number_ = frame_number;
622 } else { 598 } else {
623 ++count_frames_rendered_; 599 ++count_frames_rendered_;
(...skipping 10 matching lines...) Expand all
634 UMA_HISTOGRAM_COUNTS( 610 UMA_HISTOGRAM_COUNTS(
635 "TabCapture.FrameRate", 611 "TabCapture.FrameRate",
636 static_cast<int>(measured_fps)); 612 static_cast<int>(measured_fps));
637 VLOG(1) << "Current measured frame rate for CaptureMachine@" << this 613 VLOG(1) << "Current measured frame rate for CaptureMachine@" << this
638 << " is " << measured_fps << " FPS."; 614 << " is " << measured_fps << " FPS.";
639 last_frame_rate_log_time_ = now; 615 last_frame_rate_log_time_ = now;
640 count_frames_rendered_ = 0; 616 count_frames_rendered_ = 0;
641 last_frame_number_ = frame_number; 617 last_frame_number_ = frame_number;
642 } 618 }
643 } 619 }
644
645 // All done.
646 done_cb.Run();
647 } 620 }
648 621
649 } // namespace 622 } // namespace
650 623
651 // The "meat" of the video capture implementation, which is a ref-counted class. 624 // The "meat" of the video capture implementation, which is a ref-counted class.
652 // Separating this from the "shell class" WebContentsVideoCaptureDevice allows 625 // Separating this from the "shell class" WebContentsVideoCaptureDevice allows
653 // safe destruction without needing to block any threads (e.g., the IO 626 // safe destruction without needing to block any threads (e.g., the IO
654 // BrowserThread). 627 // BrowserThread).
655 // 628 //
656 // CaptureMachine manages a simple state machine and the pipeline (see notes at 629 // CaptureMachine manages a simple state machine and the pipeline (see notes at
657 // top of this file). It times the start of successive captures and 630 // top of this file). It times the start of successive captures and
658 // facilitates the processing of each through the stages of the pipeline. 631 // facilitates the processing of each through the stages of the pipeline.
659 class CaptureMachine 632 class CaptureMachine
660 : public base::RefCountedThreadSafe<CaptureMachine, CaptureMachine> { 633 : public base::RefCountedThreadSafe<CaptureMachine, CaptureMachine> {
661 public: 634 public:
635 enum SnapshotError {
636 NO_SOURCE,
637 TRANSIENT_ERROR
638 };
639
662 CaptureMachine(int render_process_id, int render_view_id); 640 CaptureMachine(int render_process_id, int render_view_id);
663 641
664 // Sets the capture source to the given |override| for unit testing. 642 // Sets the capture source to the given |override| for unit testing.
665 // Also, |destroy_cb| will be invoked after CaptureMachine is fully destroyed 643 // Also, |destroy_cb| will be invoked after CaptureMachine is fully destroyed
666 // (to synchronize tear-down). 644 // (to synchronize tear-down).
667 void InitializeForTesting(RenderWidgetHost* override, 645 void InitializeForTesting(RenderWidgetHost* override,
668 const base::Closure& destroy_cb); 646 const base::Closure& destroy_cb);
669 647
670 // Synchronously sets/unsets the consumer. Pass |consumer| as NULL to remove 648 // Synchronously sets/unsets the consumer. Pass |consumer| as NULL to remove
671 // the reference to the consumer; then, once this method returns, 649 // the reference to the consumer; then, once this method returns,
672 // CaptureMachine will no longer invoke callbacks on the old consumer from any 650 // CaptureMachine will no longer invoke callbacks on the old consumer from any
673 // thread. 651 // thread.
674 void SetConsumer(media::VideoCaptureDevice::EventHandler* consumer); 652 void SetConsumer(media::VideoCaptureDevice::EventHandler* consumer);
675 653
676 // Asynchronous requests to change CaptureMachine state. 654 // Asynchronous requests to change CaptureMachine state.
677 void Allocate(int width, int height, int frame_rate); 655 void Allocate(int width, int height, int frame_rate);
678 void Start(); 656 void Start();
679 void Stop(); 657 void Stop();
680 void DeAllocate(); 658 void DeAllocate();
681 659
660 // Snapshot result events.
661 void OnSnapshotComplete(int frame_number,
662 const base::Time& start_time,
663 const base::TimeDelta& duration,
664 const SkBitmap& frame);
665 void OnSnapshotComplete(int frame_number,
666 const base::Time& start_time,
667 const base::TimeDelta& duration,
668 media::VideoFrame* frame);
669 void OnSnapshotFailed(SnapshotError error,
670 int frame_number);
671
682 private: 672 private:
683 friend class base::RefCountedThreadSafe<CaptureMachine, CaptureMachine>; 673 friend class base::RefCountedThreadSafe<CaptureMachine, CaptureMachine>;
684 674
685 // Flag indicating current state. 675 // Flag indicating current state.
686 enum State { 676 enum State {
687 kIdle, 677 kIdle,
688 kAllocated, 678 kAllocated,
689 kCapturing, 679 kCapturing,
690 kError, 680 kError,
691 kDestroyed 681 kDestroyed
692 }; 682 };
693 683
694 virtual ~CaptureMachine(); 684 virtual ~CaptureMachine();
695 685
696 void TransitionStateTo(State next_state); 686 void TransitionStateTo(State next_state);
697 687
698 // Stops capturing and notifies consumer_ of an error state. 688 // Stops capturing and notifies consumer_ of an error state.
699 void Error(); 689 void Error();
700 690
701 // Schedules the next frame capture off of the system clock, skipping frames 691 // Schedules the next frame capture off of the system clock, skipping frames
702 // to catch-up if necessary. 692 // to catch-up if necessary.
703 void ScheduleNextFrameCapture(); 693 void ScheduleNextFrameCapture();
704 694
705 // The glue between the pipeline stages. 695 // The glue between the pipeline stages.
706 void StartSnapshot(); 696 void StartSnapshot();
707 void SnapshotComplete(int frame_number, 697 bool FinishSnapshot();
708 const base::Time& start_time, 698 void SnapshotCompleteBitmap(int frame_number,
709 BackingStoreCopier::Result result, 699 const base::Time& start_time,
710 const SkBitmap& capture, 700 const base::TimeDelta& duration,
711 const base::Time& capture_time); 701 const SkBitmap& frame);
702 void SnapshotCompleteVideoFrame(int frame_number,
703 const base::Time& start_time,
704 const base::TimeDelta& duration,
705 media::VideoFrame* frame);
706 void SnapshotFailed(SnapshotError error, int frame_number);
707
712 void RenderComplete(int frame_number, 708 void RenderComplete(int frame_number,
713 const base::Time& capture_time, 709 const base::Time& capture_time,
714 const SkBitmap* frame_buffer); 710 const SkBitmap* frame_buffer);
715 void DeliverComplete(const SkBitmap* frame_buffer); 711 void DeliverComplete(const SkBitmap* frame_buffer);
716 712
717 // Specialized RefCounted traits for CaptureMachine, so that operator delete 713 // Specialized RefCounted traits for CaptureMachine, so that operator delete
718 // is called from an "outside" thread. See comments for "traits" in 714 // is called from an "outside" thread. See comments for "traits" in
719 // base/memory/ref_counted.h. 715 // base/memory/ref_counted.h.
720 static void Destruct(const CaptureMachine* x); 716 static void Destruct(const CaptureMachine* x);
721 static void DeleteFromOutsideThread(const CaptureMachine* x); 717 static void DeleteFromOutsideThread(const CaptureMachine* x);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 if (width < kMinFrameWidth || height < kMinFrameHeight) { 785 if (width < kMinFrameWidth || height < kMinFrameHeight) {
790 DVLOG(1) << "invalid width (" << width << ") and/or height (" 786 DVLOG(1) << "invalid width (" << width << ") and/or height ("
791 << height << ")"; 787 << height << ")";
792 Error(); 788 Error();
793 return; 789 return;
794 } 790 }
795 791
796 settings_.width = width; 792 settings_.width = width;
797 settings_.height = height; 793 settings_.height = height;
798 settings_.frame_rate = frame_rate; 794 settings_.frame_rate = frame_rate;
795 // Sets the color format used by OnIncomingCapturedFrame.
796 // Does not apply to OnIncomingCapturedVideoFrame.
799 settings_.color = media::VideoCaptureCapability::kARGB; 797 settings_.color = media::VideoCaptureCapability::kARGB;
800 settings_.expected_capture_delay = 0; 798 settings_.expected_capture_delay = 0;
801 settings_.interlaced = false; 799 settings_.interlaced = false;
802 800
803 capture_period_ = base::TimeDelta::FromMicroseconds( 801 capture_period_ = base::TimeDelta::FromMicroseconds(
804 1000000.0 / settings_.frame_rate + 0.5); 802 1000000.0 / settings_.frame_rate + 0.5);
805 803
806 consumer_.OnFrameInfo(settings_); 804 consumer_.OnFrameInfo(settings_);
807 805
808 TransitionStateTo(kAllocated); 806 TransitionStateTo(kAllocated);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 void CaptureMachine::StartSnapshot() { 931 void CaptureMachine::StartSnapshot() {
934 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current()); 932 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current());
935 933
936 if (state_ != kCapturing) { 934 if (state_ != kCapturing) {
937 return; 935 return;
938 } 936 }
939 937
940 if (!is_snapshotting_) { 938 if (!is_snapshotting_) {
941 is_snapshotting_ = true; 939 is_snapshotting_ = true;
942 940
943 const BackingStoreCopier::DoneCB& done_cb = 941 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
944 media::BindToLoop(manager_thread_.message_loop_proxy(),
945 base::Bind(&CaptureMachine::SnapshotComplete, this,
946 frame_number_, base::Time::Now()));
947 const base::Closure& start_cb =
948 base::Bind(&BackingStoreCopier::StartCopy, 942 base::Bind(&BackingStoreCopier::StartCopy,
949 base::Unretained(&copier_), 943 base::Unretained(&copier_), make_scoped_refptr(this),
950 frame_number_, settings_.width, settings_.height, done_cb); 944 frame_number_, settings_.width, settings_.height)
951 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, start_cb); 945 );
952 } 946 }
953 947
954 ScheduleNextFrameCapture(); 948 ScheduleNextFrameCapture();
955 } 949 }
956 950
957 void CaptureMachine::SnapshotComplete(int frame_number, 951 bool CaptureMachine::FinishSnapshot() {
958 const base::Time& start_time, 952 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current());
959 BackingStoreCopier::Result result, 953
960 const SkBitmap& capture, 954 DCHECK(is_snapshotting_);
961 const base::Time& capture_time) { 955 is_snapshotting_ = false;
956
957 return state_ == kCapturing;
958 }
959
960 void CaptureMachine::OnSnapshotComplete(int frame_number,
961 const base::Time& start_time,
962 const base::TimeDelta& duration,
963 const SkBitmap& frame) {
964 manager_thread_.message_loop()->PostTask(FROM_HERE,
965 base::Bind(&CaptureMachine::SnapshotCompleteBitmap, this,
966 frame_number, start_time, duration, frame));
967 }
968
969 void CaptureMachine::SnapshotCompleteBitmap(int frame_number,
970 const base::Time& start_time,
971 const base::TimeDelta& duration,
972 const SkBitmap& capture) {
973 if (!FinishSnapshot())
974 return;
975
976 UMA_HISTOGRAM_TIMES("TabCapture.SnapshotTime", duration);
977 if (num_renders_pending_ <= 1) {
978 ++num_renders_pending_;
979 renderer_.Render(
980 frame_number,
981 capture,
982 settings_.width, settings_.height,
983 media::BindToLoop(manager_thread_.message_loop_proxy(),
984 base::Bind(&CaptureMachine::RenderComplete, this,
985 frame_number, start_time + duration)));
986 }
987 }
988
989 void CaptureMachine::OnSnapshotComplete(int frame_number,
990 const base::Time& start_time,
991 const base::TimeDelta& duration,
992 media::VideoFrame* frame) {
993 manager_thread_.message_loop()->PostTask(FROM_HERE,
994 base::Bind(&CaptureMachine::SnapshotCompleteVideoFrame, this,
995 frame_number, start_time, duration,
996 make_scoped_refptr(frame)));
997 }
998
999 void CaptureMachine::SnapshotCompleteVideoFrame(
1000 int frame_number,
1001 const base::Time& start_time,
1002 const base::TimeDelta& duration,
1003 media::VideoFrame* frame) {
1004 if (!FinishSnapshot())
1005 return;
1006
1007 UMA_HISTOGRAM_TIMES("TabCapture.SnapshotTime", duration);
1008
1009 deliverer_.Deliver(frame_number, frame, start_time + duration);
1010 }
1011
1012 void CaptureMachine::OnSnapshotFailed(CaptureMachine::SnapshotError error,
1013 int frame_number) {
1014 manager_thread_.message_loop()->PostTask(FROM_HERE,
1015 base::Bind(&CaptureMachine::SnapshotFailed, this, error, frame_number));
1016 }
1017
1018 void CaptureMachine::SnapshotFailed(CaptureMachine::SnapshotError error,
1019 int frame_number) {
962 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current()); 1020 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current());
963 1021
964 DCHECK(is_snapshotting_); 1022 DCHECK(is_snapshotting_);
965 is_snapshotting_ = false; 1023 is_snapshotting_ = false;
966 1024
967 if (state_ != kCapturing) { 1025 if (state_ != kCapturing) {
968 return; 1026 return;
969 } 1027 }
970 1028
971 switch (result) { 1029 if (error == NO_SOURCE)
972 case BackingStoreCopier::OK: 1030 Error();
973 UMA_HISTOGRAM_TIMES("TabCapture.SnapshotTime",
974 base::Time::Now() - start_time);
975 if (num_renders_pending_ <= 1) {
976 ++num_renders_pending_;
977 DCHECK(!capture_time.is_null());
978 renderer_.Render(
979 frame_number,
980 capture,
981 settings_.width, settings_.height,
982 media::BindToLoop(manager_thread_.message_loop_proxy(),
983 base::Bind(&CaptureMachine::RenderComplete, this,
984 frame_number, capture_time)));
985 }
986 break;
987
988 case BackingStoreCopier::TRANSIENT_ERROR:
989 // Skip this frame.
990 break;
991
992 case BackingStoreCopier::NO_SOURCE:
993 DVLOG(1) << "no capture source";
994 Error();
995 break;
996 }
997 } 1031 }
998 1032
999 void CaptureMachine::RenderComplete(int frame_number, 1033 void CaptureMachine::RenderComplete(int frame_number,
1000 const base::Time& capture_time, 1034 const base::Time& capture_time,
1001 const SkBitmap* frame_buffer) { 1035 const SkBitmap* frame_buffer) {
1002 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current()); 1036 DCHECK_EQ(manager_thread_.message_loop(), MessageLoop::current());
1003 1037
1004 --num_renders_pending_; 1038 --num_renders_pending_;
1005 DCHECK_LE(0, num_renders_pending_); 1039 DCHECK_LE(0, num_renders_pending_);
1006 1040
1007 if (state_ != kCapturing || !frame_buffer) { 1041 if (state_ != kCapturing || !frame_buffer) {
1008 return; 1042 return;
1009 } 1043 }
1010 1044
1011 DCHECK(!capture_time.is_null()); 1045 DCHECK(!capture_time.is_null());
1012 DCHECK(frame_buffer); 1046 DCHECK(frame_buffer);
1013 deliverer_.Deliver( 1047 deliverer_.Deliver(
1014 frame_number, *frame_buffer, capture_time, 1048 frame_number, *frame_buffer, capture_time,
1015 base::Bind(&CaptureMachine::DeliverComplete, this, frame_buffer)); 1049 base::Bind(&CaptureMachine::DeliverComplete, this, frame_buffer));
1016 } 1050 }
1017 1051
1018 void CaptureMachine::DeliverComplete(const SkBitmap* frame_buffer) { 1052 void CaptureMachine::DeliverComplete(const SkBitmap* frame_buffer) {
1019 renderer_.Release(frame_buffer); 1053 renderer_.Release(frame_buffer);
1020 } 1054 }
1021 1055
1056 void BackingStoreCopier::StartCopy(CaptureMachine* consumer,
1057 int frame_number,
1058 int desired_width,
1059 int desired_height) {
1060 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1061
1062 RenderWidgetHost* rwh;
1063 if (rwh_for_testing_) {
1064 rwh = rwh_for_testing_;
1065 } else {
1066 if (!web_contents()) { // No source yet.
1067 LookUpAndObserveWebContents();
1068 if (!web_contents()) { // No source ever.
1069 consumer->OnSnapshotFailed(CaptureMachine::NO_SOURCE, frame_number);
1070 return;
1071 }
1072 }
1073
1074 if (fullscreen_widget_id_ != MSG_ROUTING_NONE) {
1075 RenderProcessHost* process = web_contents()->GetRenderProcessHost();
1076 rwh = process ? process->GetRenderWidgetHostByID(fullscreen_widget_id_)
1077 : NULL;
1078 } else {
1079 rwh = web_contents()->GetRenderViewHost();
1080 }
1081
1082 if (!rwh) {
1083 // Transient failure state (e.g., a RenderView is being replaced).
1084 consumer->OnSnapshotFailed(CaptureMachine::TRANSIENT_ERROR, frame_number);
1085 return;
1086 }
1087 }
1088
1089 gfx::Size fitted_size;
1090 if (RenderWidgetHostView* const view = rwh->GetView()) {
1091 const gfx::Size& view_size = view->GetViewBounds().size();
1092 if (!view_size.IsEmpty()) {
1093 CalculateFittedSize(view_size.width(), view_size.height(),
1094 desired_width, desired_height,
1095 &fitted_size);
1096 }
1097 if (view_size != last_view_size_) {
1098 last_view_size_ = view_size;
1099
1100 // Measure the number of kilopixels.
1101 UMA_HISTOGRAM_COUNTS_10000(
1102 "TabCapture.ViewChangeKiloPixels",
1103 view_size.width() * view_size.height() / 1024);
1104 }
1105 }
1106
1107 TRACE_EVENT_ASYNC_BEGIN1("mirroring", "Capture", this,
1108 "frame_number", frame_number);
1109 if (rwh->CanCopyToVideoFrame()) {
1110 rwh->CopyFromBackingStoreToVideoFrame(
1111 gfx::Rect(),
1112 gfx::Size(desired_width, desired_height),
1113 base::Bind(&BackingStoreCopier::DidCopyFromBackingStoreToVideoFrame,
1114 base::Unretained(this), make_scoped_refptr(consumer),
1115 frame_number, base::Time::Now()));
1116 } else {
1117 rwh->CopyFromBackingStore(
1118 gfx::Rect(),
1119 fitted_size,
1120 base::Bind(&BackingStoreCopier::DidCopyFromBackingStore,
1121 base::Unretained(this), make_scoped_refptr(consumer),
1122 frame_number, base::Time::Now()));
1123 }
1124 // TODO(miu): When a tab is not visible to the user, rendering stops. For
1125 // mirroring, however, it's important that rendering continues to happen.
1126 }
1127
1128 void BackingStoreCopier::DidCopyFromBackingStore(
1129 CaptureMachine* consumer,
1130 int frame_number,
1131 const base::Time& start_time,
1132 bool success,
1133 const SkBitmap& frame) {
1134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1135 // Note: No restriction on which thread invokes this method but, currently,
1136 // it's always the UI BrowserThread.
1137 TRACE_EVENT_ASYNC_END1("mirroring", "Capture", this,
1138 "frame_number", frame_number);
1139
1140 if (success) {
1141 consumer->OnSnapshotComplete(
1142 frame_number, start_time, base::Time::Now() - start_time, frame);
1143 } else {
1144 // Capture can fail due to transient issues, so just skip this frame.
1145 DVLOG(1) << "CopyFromBackingStore was not successful; skipping frame.";
1146 consumer->OnSnapshotFailed(CaptureMachine::TRANSIENT_ERROR, frame_number);
1147 }
1148 }
1149
1150 void BackingStoreCopier::DidCopyFromBackingStoreToVideoFrame(
1151 CaptureMachine* consumer,
1152 int frame_number,
1153 const base::Time& start_time,
1154 media::VideoFrame* frame) {
1155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1156 // Note: No restriction on which thread invokes this method but, currently,
1157 // it's always the UI BrowserThread.
1158 TRACE_EVENT_ASYNC_END1("mirroring", "Capture", this,
1159 "frame_number", frame_number);
1160
1161 if (frame && frame->format() != media::VideoFrame::INVALID &&
1162 frame->format() != media::VideoFrame::EMPTY) {
1163 consumer->OnSnapshotComplete(
1164 frame_number, start_time, base::Time::Now() - start_time, frame);
1165 } else {
1166 // Capture can fail due to transient issues, so just skip this frame.
1167 DVLOG(1) << "CopyFromBackingStoreToVideoFrame failure; skipping frame.";
1168 consumer->OnSnapshotFailed(
1169 CaptureMachine::TRANSIENT_ERROR, frame_number);
1170 }
1171 }
1172
1022 WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice( 1173 WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice(
1023 const media::VideoCaptureDevice::Name& name, 1174 const media::VideoCaptureDevice::Name& name,
1024 int render_process_id, int render_view_id) 1175 int render_process_id, int render_view_id)
1025 : device_name_(name), 1176 : device_name_(name),
1026 capturer_(new CaptureMachine(render_process_id, render_view_id)) {} 1177 capturer_(new CaptureMachine(render_process_id, render_view_id)) {}
1027 1178
1028 WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice( 1179 WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice(
1029 RenderWidgetHost* test_source, const base::Closure& destroy_cb) 1180 RenderWidgetHost* test_source, const base::Closure& destroy_cb)
1030 : capturer_(new CaptureMachine(-1, -1)) { 1181 : capturer_(new CaptureMachine(-1, -1)) {
1031 device_name_.device_name = "WebContentsForTesting"; 1182 device_name_.device_name = "WebContentsForTesting";
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1087 capturer_->SetConsumer(NULL); 1238 capturer_->SetConsumer(NULL);
1088 capturer_->DeAllocate(); 1239 capturer_->DeAllocate();
1089 } 1240 }
1090 1241
1091 const media::VideoCaptureDevice::Name& 1242 const media::VideoCaptureDevice::Name&
1092 WebContentsVideoCaptureDevice::device_name() { 1243 WebContentsVideoCaptureDevice::device_name() {
1093 return device_name_; 1244 return device_name_;
1094 } 1245 }
1095 1246
1096 } // namespace content 1247 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698