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

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

Issue 12090109: Tab Capture: Backing store readbacks to YV12 VideoFrames. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix a verb tense parallelism problem in a comment. 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 #include "content/browser/renderer_host/media/web_contents_video_capture_device. h" 5 #include "content/browser/renderer_host/media/web_contents_video_capture_device. h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/synchronization/condition_variable.h" 8 #include "base/synchronization/condition_variable.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/render_widget_host_delegate.h" 12 #include "content/browser/renderer_host/render_widget_host_delegate.h"
13 #include "content/browser/renderer_host/render_widget_host_impl.h" 13 #include "content/browser/renderer_host/render_widget_host_impl.h"
14 #include "content/public/test/mock_render_process_host.h" 14 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_context.h" 15 #include "content/public/test/test_browser_context.h"
16 #include "media/base/video_util.h"
16 #include "media/video/capture/video_capture_types.h" 17 #include "media/video/capture/video_capture_types.h"
17 #include "skia/ext/platform_canvas.h" 18 #include "skia/ext/platform_canvas.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/skia/include/core/SkColor.h" 20 #include "third_party/skia/include/core/SkColor.h"
20 21
21 namespace content { 22 namespace content {
22 namespace { 23 namespace {
23 const int kTestWidth = 1280; 24 const int kTestWidth = 1280;
24 const int kTestHeight = 720; 25 const int kTestHeight = 720;
25 const int kBytesPerPixel = 4; 26 const int kBytesPerPixel = 4;
26 const int kTestFramesPerSecond = 8; 27 const int kTestFramesPerSecond = 8;
27 const base::TimeDelta kWaitTimeout = 28 const base::TimeDelta kWaitTimeout =
28 base::TimeDelta::FromMilliseconds(2000); 29 base::TimeDelta::FromMilliseconds(2000);
29 const SkColor kNothingYet = 0xdeadbeef; 30 const SkColor kNothingYet = 0xdeadbeef;
30 const SkColor kNotInterested = ~kNothingYet; 31 const SkColor kNotInterested = ~kNothingYet;
31 } 32 }
32 33
33 // A stub implementation which returns solid-color bitmaps in calls to 34 // A stub implementation which returns solid-color bitmaps in calls to
34 // CopyFromBackingStore(). The unit tests can change the color for successive 35 // CopyFromBackingStore(). The unit tests can change the color for successive
35 // captures. 36 // captures.
36 class StubRenderWidgetHost : public RenderWidgetHostImpl { 37 class StubRenderWidgetHost : public RenderWidgetHostImpl {
37 public: 38 public:
38 StubRenderWidgetHost(RenderProcessHost* process, int routing_id) 39 StubRenderWidgetHost(RenderProcessHost* process, int routing_id)
39 : RenderWidgetHostImpl(&delegate_, process, routing_id), 40 : RenderWidgetHostImpl(&delegate_, process, routing_id),
40 color_(kNothingYet), 41 color_(kNothingYet),
41 copy_result_size_(kTestWidth, kTestHeight), 42 copy_result_size_(kTestWidth, kTestHeight),
42 copy_event_(false, false) {} 43 copy_event_(false, false),
44 use_video_frames_(false) {}
43 45
44 void SetSolidColor(SkColor color) { 46 void SetSolidColor(SkColor color) {
45 base::AutoLock guard(lock_); 47 base::AutoLock guard(lock_);
46 color_ = color; 48 color_ = color;
47 } 49 }
48 50
49 void SetCopyResultSize(int width, int height) { 51 void SetCopyResultSize(int width, int height) {
50 base::AutoLock guard(lock_); 52 base::AutoLock guard(lock_);
51 copy_result_size_ = gfx::Size(width, height); 53 copy_result_size_ = gfx::Size(width, height);
52 } 54 }
53 55
56 void SetUseVideoFrames(bool use_them) {
57 use_video_frames_ = use_them;
58 }
59
54 bool WaitForNextBackingStoreCopy() { 60 bool WaitForNextBackingStoreCopy() {
55 if (!copy_event_.TimedWait(kWaitTimeout)) { 61 if (!copy_event_.TimedWait(kWaitTimeout)) {
56 ADD_FAILURE() << "WaitForNextBackingStoreCopy: wait deadline exceeded"; 62 ADD_FAILURE() << "WaitForNextBackingStoreCopy: wait deadline exceeded";
57 return false; 63 return false;
58 } 64 }
59 return true; 65 return true;
60 } 66 }
61 67
62 // RenderWidgetHostImpl overrides. 68 // RenderWidgetHostImpl overrides.
63 virtual void CopyFromBackingStore( 69 virtual void CopyFromBackingStore(
64 const gfx::Rect& src_rect, 70 const gfx::Rect& src_rect,
65 const gfx::Size& accelerated_dst_size, 71 const gfx::Size& accelerated_dst_size,
66 const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE { 72 const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE {
67 // Although it's not necessary, use a PlatformBitmap here (instead of a 73 // Although it's not necessary, use a PlatformBitmap here (instead of a
68 // regular SkBitmap) to exercise possible threading issues. 74 // regular SkBitmap) to exercise possible threading issues.
69 scoped_ptr<skia::PlatformBitmap> platform_bitmap(new skia::PlatformBitmap); 75 scoped_ptr<skia::PlatformBitmap> platform_bitmap(new skia::PlatformBitmap);
70 EXPECT_TRUE(platform_bitmap->Allocate( 76 EXPECT_TRUE(platform_bitmap->Allocate(
71 copy_result_size_.width(), copy_result_size_.height(), false)); 77 copy_result_size_.width(), copy_result_size_.height(), false));
72 { 78 {
73 SkAutoLockPixels locker(platform_bitmap->GetBitmap()); 79 SkAutoLockPixels locker(platform_bitmap->GetBitmap());
74 base::AutoLock guard(lock_); 80 base::AutoLock guard(lock_);
75 platform_bitmap->GetBitmap().eraseColor(color_); 81 platform_bitmap->GetBitmap().eraseColor(color_);
76 } 82 }
77 83
78 callback.Run(true, platform_bitmap->GetBitmap()); 84 callback.Run(true, platform_bitmap->GetBitmap());
79 copy_event_.Signal(); 85 copy_event_.Signal();
80 } 86 }
81 87
88 virtual bool CanCopyToVideoFrame() const OVERRIDE {
89 return use_video_frames_;
90 }
91
92 virtual void CopyFromBackingStoreToVideoFrame(
93 const gfx::Rect& src_rect,
94 const scoped_refptr<media::VideoFrame>& target,
95 const base::Callback<void(bool)>& callback) OVERRIDE {
96 media::FillYUV(target, SkColorGetR(color_), SkColorGetG(color_),
97 SkColorGetB(color_));
98 callback.Run(true);
99 copy_event_.Signal();
100 }
101
82 private: 102 private:
83 class StubRenderWidgetHostDelegate : public RenderWidgetHostDelegate { 103 class StubRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
84 public: 104 public:
85 StubRenderWidgetHostDelegate() {} 105 StubRenderWidgetHostDelegate() {}
86 virtual ~StubRenderWidgetHostDelegate() {} 106 virtual ~StubRenderWidgetHostDelegate() {}
87 107
88 private: 108 private:
89 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate); 109 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate);
90 }; 110 };
91 111
92 StubRenderWidgetHostDelegate delegate_; 112 StubRenderWidgetHostDelegate delegate_;
93 base::Lock lock_; // Guards changes to color_. 113 base::Lock lock_; // Guards changes to color_.
94 SkColor color_; 114 SkColor color_;
95 gfx::Size copy_result_size_; 115 gfx::Size copy_result_size_;
96 base::WaitableEvent copy_event_; 116 base::WaitableEvent copy_event_;
117 bool use_video_frames_;
97 118
98 DISALLOW_IMPLICIT_CONSTRUCTORS(StubRenderWidgetHost); 119 DISALLOW_IMPLICIT_CONSTRUCTORS(StubRenderWidgetHost);
99 }; 120 };
100 121
101 // A stub consumer of captured video frames, which checks the output of 122 // A stub consumer of captured video frames, which checks the output of
102 // WebContentsVideoCaptureDevice. 123 // WebContentsVideoCaptureDevice.
103 class StubConsumer : public media::VideoCaptureDevice::EventHandler { 124 class StubConsumer : public media::VideoCaptureDevice::EventHandler {
104 public: 125 public:
105 StubConsumer() : output_changed_(&lock_), 126 StubConsumer() : output_changed_(&lock_),
106 picture_color_(kNothingYet), 127 picture_color_(kNothingYet),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 166
146 { 167 {
147 base::AutoLock guard(lock_); 168 base::AutoLock guard(lock_);
148 if (color != picture_color_) { 169 if (color != picture_color_) {
149 picture_color_ = color; 170 picture_color_ = color;
150 output_changed_.Signal(); 171 output_changed_.Signal();
151 } 172 }
152 } 173 }
153 } 174 }
154 175
176 virtual void OnIncomingCapturedVideoFrame(media::VideoFrame* frame,
177 base::Time timestamp) OVERRIDE {
178 EXPECT_EQ(gfx::Size(kTestWidth, kTestHeight), frame->coded_size());
179 EXPECT_EQ(media::VideoFrame::YV12, frame->format());
180 bool all_pixels_are_the_same_color = true;
181 uint8 yuv[3] = {0};
182 for (int plane = 0; plane < 3; ++plane) {
183 yuv[plane] = frame->data(plane)[0];
184 for (int y = 0; y < frame->rows(plane); ++y) {
185 for (int x = 0; x < frame->row_bytes(plane); ++x) {
186 if (yuv[plane] != frame->data(plane)[x + y * frame->stride(plane)]) {
187 all_pixels_are_the_same_color = false;
188 break;
189 }
190 }
191 }
192 }
193 EXPECT_TRUE(all_pixels_are_the_same_color);
194 const SkColor color = SkColorSetRGB(yuv[0], yuv[1], yuv[2]);
195
196 {
197 base::AutoLock guard(lock_);
198 if (color != picture_color_) {
199 picture_color_ = color;
200 output_changed_.Signal();
201 }
202 }
203 }
204
155 virtual void OnError() OVERRIDE { 205 virtual void OnError() OVERRIDE {
156 base::AutoLock guard(lock_); 206 base::AutoLock guard(lock_);
157 error_encountered_ = true; 207 error_encountered_ = true;
158 output_changed_.Signal(); 208 output_changed_.Signal();
159 } 209 }
160 210
161 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE { 211 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE {
162 EXPECT_EQ(kTestWidth, info.width); 212 EXPECT_EQ(kTestWidth, info.width);
163 EXPECT_EQ(kTestHeight, info.height); 213 EXPECT_EQ(kTestHeight, info.height);
164 EXPECT_EQ(kTestFramesPerSecond, info.frame_rate); 214 EXPECT_EQ(kTestFramesPerSecond, info.frame_rate);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 scoped_ptr<StubRenderWidgetHost> source_; 279 scoped_ptr<StubRenderWidgetHost> source_;
230 scoped_ptr<base::WaitableEvent> destroyed_; 280 scoped_ptr<base::WaitableEvent> destroyed_;
231 scoped_ptr<media::VideoCaptureDevice> device_; 281 scoped_ptr<media::VideoCaptureDevice> device_;
232 scoped_ptr<StubConsumer> consumer_; 282 scoped_ptr<StubConsumer> consumer_;
233 283
234 DISALLOW_COPY_AND_ASSIGN(WebContentsVideoCaptureDeviceTest); 284 DISALLOW_COPY_AND_ASSIGN(WebContentsVideoCaptureDeviceTest);
235 }; 285 };
236 286
237 // The "happy case" test. No scaling is needed, so we should be able to change 287 // The "happy case" test. No scaling is needed, so we should be able to change
238 // the picture emitted from the source and expect to see each delivered to the 288 // the picture emitted from the source and expect to see each delivered to the
239 // consumer. 289 // consumer. The test will alternate between the SkBitmap and the VideoFrame
290 // paths, just as RenderWidgetHost might if the content falls in and out of
291 // accelerated compositing.
240 TEST_F(WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) { 292 TEST_F(WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) {
241 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, 293 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond,
242 consumer()); 294 consumer());
243 295
244 device()->Start(); 296 device()->Start();
245 source()->SetSolidColor(SK_ColorRED); 297
246 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED)); 298 bool use_video_frames = false;
247 source()->SetSolidColor(SK_ColorGREEN); 299 for (int i = 0; i < 4; i++, use_video_frames = !use_video_frames) {
248 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); 300 SCOPED_TRACE(
249 source()->SetSolidColor(SK_ColorBLUE); 301 testing::Message() << "Using "
scherkus (not reviewing) 2013/02/07 18:25:03 nit: you can also use base::StringPrintf() if you
250 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLUE)); 302 << (use_video_frames ? "VideoFrame" : "SkBitmap")
251 source()->SetSolidColor(SK_ColorBLACK); 303 << " path, iteration #" << i);
252 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK)); 304 source()->SetUseVideoFrames(use_video_frames);
305 source()->SetSolidColor(SK_ColorRED);
306 source()->WaitForNextBackingStoreCopy();
307 ASSERT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED));
308 source()->SetSolidColor(SK_ColorGREEN);
309 ASSERT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN));
310 source()->SetSolidColor(SK_ColorBLUE);
311 ASSERT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLUE));
312 source()->SetSolidColor(SK_ColorBLACK);
313 ASSERT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK));
314 }
253 315
254 device()->DeAllocate(); 316 device()->DeAllocate();
255 } 317 }
256 318
257 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) { 319 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) {
258 device()->Allocate(1280, 720, -2, consumer()); 320 device()->Allocate(1280, 720, -2, consumer());
259 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested)); 321 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested));
260 } 322 }
261 323
262 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) { 324 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) {
(...skipping 17 matching lines...) Expand all
280 // Now push some good frames through; they should be processed normally. 342 // Now push some good frames through; they should be processed normally.
281 source()->SetCopyResultSize(kTestWidth, kTestHeight); 343 source()->SetCopyResultSize(kTestWidth, kTestHeight);
282 source()->SetSolidColor(SK_ColorGREEN); 344 source()->SetSolidColor(SK_ColorGREEN);
283 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); 345 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN));
284 source()->SetSolidColor(SK_ColorRED); 346 source()->SetSolidColor(SK_ColorRED);
285 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED)); 347 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED));
286 device()->DeAllocate(); 348 device()->DeAllocate();
287 } 349 }
288 350
289 } // namespace content 351 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698