| 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/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 gfx::Size& dst_size, |
| 95 const base::Callback<void(media::VideoFrame*)>& callback) OVERRIDE { |
| 96 callback.Run( |
| 97 media::VideoFrame::CreateColorFrame( |
| 98 dst_size, |
| 99 SkColorGetR(color_), SkColorGetG(color_), SkColorGetB(color_), |
| 100 base::TimeDelta())); |
| 101 copy_event_.Signal(); |
| 102 } |
| 103 |
| 82 private: | 104 private: |
| 83 class StubRenderWidgetHostDelegate : public RenderWidgetHostDelegate { | 105 class StubRenderWidgetHostDelegate : public RenderWidgetHostDelegate { |
| 84 public: | 106 public: |
| 85 StubRenderWidgetHostDelegate() {} | 107 StubRenderWidgetHostDelegate() {} |
| 86 virtual ~StubRenderWidgetHostDelegate() {} | 108 virtual ~StubRenderWidgetHostDelegate() {} |
| 87 | 109 |
| 88 private: | 110 private: |
| 89 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate); | 111 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate); |
| 90 }; | 112 }; |
| 91 | 113 |
| 92 StubRenderWidgetHostDelegate delegate_; | 114 StubRenderWidgetHostDelegate delegate_; |
| 93 base::Lock lock_; // Guards changes to color_. | 115 base::Lock lock_; // Guards changes to color_. |
| 94 SkColor color_; | 116 SkColor color_; |
| 95 gfx::Size copy_result_size_; | 117 gfx::Size copy_result_size_; |
| 96 base::WaitableEvent copy_event_; | 118 base::WaitableEvent copy_event_; |
| 119 bool use_video_frames_; |
| 97 | 120 |
| 98 DISALLOW_IMPLICIT_CONSTRUCTORS(StubRenderWidgetHost); | 121 DISALLOW_IMPLICIT_CONSTRUCTORS(StubRenderWidgetHost); |
| 99 }; | 122 }; |
| 100 | 123 |
| 101 // A stub consumer of captured video frames, which checks the output of | 124 // A stub consumer of captured video frames, which checks the output of |
| 102 // WebContentsVideoCaptureDevice. | 125 // WebContentsVideoCaptureDevice. |
| 103 class StubConsumer : public media::VideoCaptureDevice::EventHandler { | 126 class StubConsumer : public media::VideoCaptureDevice::EventHandler { |
| 104 public: | 127 public: |
| 105 StubConsumer() : output_changed_(&lock_), | 128 StubConsumer() : output_changed_(&lock_), |
| 106 picture_color_(kNothingYet), | 129 picture_color_(kNothingYet), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 168 |
| 146 { | 169 { |
| 147 base::AutoLock guard(lock_); | 170 base::AutoLock guard(lock_); |
| 148 if (color != picture_color_) { | 171 if (color != picture_color_) { |
| 149 picture_color_ = color; | 172 picture_color_ = color; |
| 150 output_changed_.Signal(); | 173 output_changed_.Signal(); |
| 151 } | 174 } |
| 152 } | 175 } |
| 153 } | 176 } |
| 154 | 177 |
| 178 virtual void OnIncomingCapturedVideoFrame(media::VideoFrame* frame, |
| 179 base::Time timestamp) OVERRIDE { |
| 180 EXPECT_EQ(gfx::Size(kTestWidth, kTestHeight), frame->coded_size()); |
| 181 EXPECT_EQ(media::VideoFrame::YV12, frame->format()); |
| 182 bool all_pixels_are_the_same_color = true; |
| 183 uint8 yuv[3] = {0}; |
| 184 for (int plane = 0; plane < 3; ++plane) { |
| 185 yuv[plane] = frame->data(plane)[0]; |
| 186 for (int y = 0; y < frame->rows(plane); ++y) { |
| 187 for (int x = 0; x < frame->row_bytes(plane); ++x) { |
| 188 if (yuv[plane] != frame->data(plane)[x + y * frame->stride(plane)]) { |
| 189 all_pixels_are_the_same_color = false; |
| 190 break; |
| 191 } |
| 192 } |
| 193 } |
| 194 } |
| 195 EXPECT_TRUE(all_pixels_are_the_same_color); |
| 196 const SkColor color = SkColorSetRGB(yuv[0], yuv[1], yuv[2]); |
| 197 |
| 198 { |
| 199 base::AutoLock guard(lock_); |
| 200 if (color != picture_color_) { |
| 201 picture_color_ = color; |
| 202 output_changed_.Signal(); |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 155 virtual void OnError() OVERRIDE { | 207 virtual void OnError() OVERRIDE { |
| 156 base::AutoLock guard(lock_); | 208 base::AutoLock guard(lock_); |
| 157 error_encountered_ = true; | 209 error_encountered_ = true; |
| 158 output_changed_.Signal(); | 210 output_changed_.Signal(); |
| 159 } | 211 } |
| 160 | 212 |
| 161 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE { | 213 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE { |
| 162 EXPECT_EQ(kTestWidth, info.width); | 214 EXPECT_EQ(kTestWidth, info.width); |
| 163 EXPECT_EQ(kTestHeight, info.height); | 215 EXPECT_EQ(kTestHeight, info.height); |
| 164 EXPECT_EQ(kTestFramesPerSecond, info.frame_rate); | 216 EXPECT_EQ(kTestFramesPerSecond, info.frame_rate); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 }; | 287 }; |
| 236 | 288 |
| 237 // The "happy case" test. No scaling is needed, so we should be able to change | 289 // 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 | 290 // the picture emitted from the source and expect to see each delivered to the |
| 239 // consumer. | 291 // consumer. |
| 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 for (int i = 0; i < 4; i++) { |
| 247 source()->SetSolidColor(SK_ColorGREEN); | 299 source()->SetUseVideoFrames(i % 2 == 1); |
| 248 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); | 300 source()->SetSolidColor(SK_ColorRED); |
| 249 source()->SetSolidColor(SK_ColorBLUE); | 301 source()->WaitForNextBackingStoreCopy(); |
| 250 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLUE)); | 302 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED)); |
| 251 source()->SetSolidColor(SK_ColorBLACK); | 303 source()->SetSolidColor(SK_ColorGREEN); |
| 252 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK)); | 304 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); |
| 305 source()->SetSolidColor(SK_ColorBLUE); |
| 306 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLUE)); |
| 307 source()->SetSolidColor(SK_ColorBLACK); |
| 308 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK)); |
| 309 } |
| 253 | 310 |
| 254 device()->DeAllocate(); | 311 device()->DeAllocate(); |
| 255 } | 312 } |
| 256 | 313 |
| 257 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) { | 314 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) { |
| 258 device()->Allocate(1280, 720, -2, consumer()); | 315 device()->Allocate(1280, 720, -2, consumer()); |
| 259 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested)); | 316 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested)); |
| 260 } | 317 } |
| 261 | 318 |
| 262 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) { | 319 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 280 // Now push some good frames through; they should be processed normally. | 337 // Now push some good frames through; they should be processed normally. |
| 281 source()->SetCopyResultSize(kTestWidth, kTestHeight); | 338 source()->SetCopyResultSize(kTestWidth, kTestHeight); |
| 282 source()->SetSolidColor(SK_ColorGREEN); | 339 source()->SetSolidColor(SK_ColorGREEN); |
| 283 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); | 340 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorGREEN)); |
| 284 source()->SetSolidColor(SK_ColorRED); | 341 source()->SetSolidColor(SK_ColorRED); |
| 285 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED)); | 342 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorRED)); |
| 286 device()->DeAllocate(); | 343 device()->DeAllocate(); |
| 287 } | 344 } |
| 288 | 345 |
| 289 } // namespace content | 346 } // namespace content |
| OLD | NEW |