1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/media/capture/aura_window_video_capture_device.h"
#include <tuple>
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "cc/test/pixel_test_utils.h"
#include "components/viz/common/features.h"
#include "content/browser/media/capture/content_capture_device_browsertest_base.h"
#include "content/browser/media/capture/fake_video_capture_stack.h"
#include "content/browser/media/capture/frame_test_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/shell/browser/shell.h"
#include "media/base/video_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
namespace content {
namespace {
class AuraWindowVideoCaptureDeviceBrowserTest
: public ContentCaptureDeviceBrowserTestBase,
public FrameTestUtil {
public:
AuraWindowVideoCaptureDeviceBrowserTest() = default;
AuraWindowVideoCaptureDeviceBrowserTest(
const AuraWindowVideoCaptureDeviceBrowserTest&) = delete;
AuraWindowVideoCaptureDeviceBrowserTest& operator=(
const AuraWindowVideoCaptureDeviceBrowserTest&) = delete;
~AuraWindowVideoCaptureDeviceBrowserTest() override = default;
aura::Window* GetCapturedWindow() const {
// Note: The Window with an associated compositor frame sink (required for
// capture) is the root window, which is an immediate ancestor of the
// aura::Window provided by shell()->window().
return shell()->window()->GetRootWindow();
}
// Returns the location of the content within the window.
gfx::Rect GetWebContentsRect() const {
aura::Window* const contents_window =
shell()->web_contents()->GetNativeView();
gfx::Rect rect = gfx::Rect(contents_window->bounds().size());
aura::Window::ConvertRectToTarget(contents_window, GetCapturedWindow(),
&rect);
return rect;
}
// Runs the browser until a frame whose content matches the given |color| is
// found in the captured frames queue, or until a testing failure has
// occurred.
void WaitForFrameWithColor(SkColor color) {
VLOG(1) << "Waiting for frame content area filled with color: red="
<< SkColorGetR(color) << ", green=" << SkColorGetG(color)
<< ", blue=" << SkColorGetB(color);
while (!testing::Test::HasFailure()) {
EXPECT_TRUE(capture_stack()->Started());
EXPECT_FALSE(capture_stack()->ErrorOccurred());
capture_stack()->ExpectNoLogMessages();
while (capture_stack()->HasCapturedFrames() &&
!testing::Test::HasFailure()) {
// Pop the next frame from the front of the queue and convert to a RGB
// bitmap for analysis.
const SkBitmap rgb_frame = capture_stack()->NextCapturedFrame();
EXPECT_FALSE(rgb_frame.empty());
// Three regions of the frame will be analyzed: 1) the WebContents
// region containing a solid color, 2) the remaining part of the
// captured window containing the content shell UI, and 3) the
// solid-black letterboxed region surrounding them.
const gfx::Size frame_size(rgb_frame.width(), rgb_frame.height());
const gfx::Size window_size = GetExpectedSourceSize();
const gfx::Rect webcontents_rect = GetWebContentsRect();
// Compute the Rects representing where the three regions would be in
// the frame.
const gfx::RectF window_in_frame_rect_f(
media::ComputeLetterboxRegion(gfx::Rect(frame_size), window_size));
const gfx::RectF webcontents_in_frame_rect_f = TransformSimilarly(
gfx::Rect(window_size), window_in_frame_rect_f, webcontents_rect);
// viz::SoftwareRenderer does not do color space management. Otherwise
// (normal case), be strict about color differences.
const int max_color_diff = IsSoftwareCompositingTest()
? kVeryLooseMaxColorDifference
: kMaxColorDifference;
// Determine the average RGB color in the three regions of the frame.
const auto average_webcontents_rgb = ComputeAverageColor(
rgb_frame, ToSafeIncludeRect(webcontents_in_frame_rect_f),
gfx::Rect());
const auto average_window_rgb = ComputeAverageColor(
rgb_frame, ToSafeIncludeRect(window_in_frame_rect_f),
ToSafeExcludeRect(webcontents_in_frame_rect_f));
const auto average_letterbox_rgb =
ComputeAverageColor(rgb_frame, gfx::Rect(frame_size),
ToSafeExcludeRect(window_in_frame_rect_f));
VLOG(1) << "Video frame analysis: size=" << frame_size.ToString()
<< ", captured webcontents should be bound by approx. "
<< ToSafeIncludeRect(webcontents_in_frame_rect_f).ToString()
<< " and has average color " << average_webcontents_rgb
<< ", captured window should be bound by approx. "
<< ToSafeIncludeRect(window_in_frame_rect_f).ToString()
<< " and has average color " << average_window_rgb
<< ", letterbox region has average color "
<< average_letterbox_rgb;
// The letterboxed region should always be black.
if (IsFixedAspectRatioTest()) {
EXPECT_TRUE(IsApproximatelySameColor(
SK_ColorBLACK, average_letterbox_rgb, max_color_diff));
}
if (testing::Test::HasFailure()) {
ADD_FAILURE() << "Test failure occurred at this frame; PNG dump: "
<< cc::GetPNGDataUrl(rgb_frame);
return;
}
// Return if the WebContents region now has the new |color|.
if (IsApproximatelySameColor(color, average_webcontents_rgb,
max_color_diff)) {
VLOG(1) << "Observed desired frame.";
return;
} else {
VLOG(3) << "PNG dump of undesired frame: "
<< cc::GetPNGDataUrl(rgb_frame);
}
}
// Wait for at least the minimum capture period before checking for more
// captured frames.
base::RunLoop run_loop;
GetUIThreadTaskRunner({})->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), GetMinCapturePeriod());
run_loop.Run();
}
}
protected:
// Note: Test code should call <BaseClass>::GetExpectedSourceSize() instead of
// this method since it has extra code to sanity-check that the source size is
// not changing during the test.
gfx::Size GetCapturedSourceSize() const final {
return GetCapturedWindow()->bounds().size();
}
std::unique_ptr<FrameSinkVideoCaptureDevice> CreateDevice() final {
const DesktopMediaID source_id = DesktopMediaID::RegisterNativeWindow(
DesktopMediaID::TYPE_WINDOW, GetCapturedWindow());
EXPECT_TRUE(DesktopMediaID::GetNativeWindowById(source_id));
return std::make_unique<AuraWindowVideoCaptureDevice>(source_id);
}
void WaitForFirstFrame() final { WaitForFrameWithColor(SK_ColorBLACK); }
};
// Tests that the device refuses to start if the target window was destroyed
// before the device could start.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTest,
ErrorsOutIfWindowHasGoneBeforeDeviceStart) {
NavigateToInitialDocument();
const DesktopMediaID source_id = DesktopMediaID::RegisterNativeWindow(
DesktopMediaID::TYPE_WINDOW, GetCapturedWindow());
EXPECT_TRUE(DesktopMediaID::GetNativeWindowById(source_id));
const auto capture_params = SnapshotCaptureParams();
// Close the Shell. This should close the window it owned, making the capture
// target invalid.
shell()->Close();
// Create the device.
auto device = std::make_unique<AuraWindowVideoCaptureDevice>(source_id);
// Running the pending UI tasks should cause the device to realize the window
// is gone.
RunUntilIdle();
// Attempt to start the device, and expect the video capture stack to have
// been notified of the error.
device->AllocateAndStartWithReceiver(capture_params,
capture_stack()->CreateFrameReceiver());
RunUntilIdle();
EXPECT_FALSE(capture_stack()->Started());
EXPECT_TRUE(capture_stack()->ErrorOccurred());
capture_stack()->ExpectHasLogMessages();
device->StopAndDeAllocate();
RunUntilIdle();
}
// Disabled (https://crbug.com/1096946)
// Tests that the device starts, captures a frame, and then gracefully
// errors-out because the target window is destroyed before the device is
// stopped.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTest,
DISABLED_ErrorsOutWhenWindowIsDestroyed) {
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
// Initially, the device captures any content changes normally.
ChangePageContentColor(SK_ColorRED);
WaitForFrameWithColor(SK_ColorRED);
// Close the Shell. This should close the window it owned, causing a "target
// permanently lost" error to propagate to the video capture stack.
shell()->Close();
RunUntilIdle();
EXPECT_TRUE(capture_stack()->ErrorOccurred());
capture_stack()->ExpectHasLogMessages();
StopAndDeAllocate();
}
// Disabled (https://crbug.com/1096946)
// Tests that the device stops delivering frames while suspended. When resumed,
// any content changes that occurred during the suspend should cause a new frame
// to be delivered, to ensure the client is up-to-date.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTest,
DISABLED_SuspendsAndResumes) {
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
// Initially, the device captures any content changes normally.
ChangePageContentColor(SK_ColorRED);
WaitForFrameWithColor(SK_ColorRED);
// Suspend the device.
device()->MaybeSuspend();
RunUntilIdle();
ClearCapturedFramesQueue();
// Change the page content and run the browser for five seconds. Expect no
// frames were queued because the device should be suspended.
ChangePageContentColor(SK_ColorGREEN);
base::RunLoop run_loop;
GetUIThreadTaskRunner({})->PostDelayedTask(FROM_HERE, run_loop.QuitClosure(),
base::Seconds(5));
run_loop.Run();
EXPECT_FALSE(HasCapturedFramesInQueue());
// Resume the device and wait for an automatic refresh frame containing the
// content that was updated while the device was suspended.
device()->Resume();
WaitForFrameWithColor(SK_ColorGREEN);
StopAndDeAllocate();
}
// Disabled (https://crbug.com/1096946)
// Tests that the device delivers refresh frames when asked, while the source
// content is not changing.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTest,
DISABLED_DeliversRefreshFramesUponRequest) {
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
// Set the page content to a known color.
ChangePageContentColor(SK_ColorRED);
WaitForFrameWithColor(SK_ColorRED);
// Without making any further changes to the source (which would trigger
// frames to be captured), request and wait for ten refresh frames.
for (int i = 0; i < 10; ++i) {
ClearCapturedFramesQueue();
device()->RequestRefreshFrame();
WaitForFrameWithColor(SK_ColorRED);
}
StopAndDeAllocate();
}
#if BUILDFLAG(IS_WIN)
class AuraWindowVideoCaptureDeviceBrowserTestWin
: public AuraWindowVideoCaptureDeviceBrowserTest {
public:
// AuraWindowVideoCaptureDeviceBrowserTest:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kApplyNativeOcclusionToCompositor,
{{features::kApplyNativeOcclusionToCompositorType.name,
features::kApplyNativeOcclusionToCompositorTypeRelease}});
AuraWindowVideoCaptureDeviceBrowserTest::SetUp();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(https://crbug.com/1096946): enable.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTestWin,
DISABLED_CapturesOccludedWindow) {
aura::WindowTreeHost* window_tree_host = shell()->window()->GetHost();
aura::test::DisableNativeWindowOcclusionTracking(window_tree_host);
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
// Simulate the WindowTreeHost being occluded.
window_tree_host->SetNativeWindowOcclusionState(
aura::Window::OcclusionState::OCCLUDED, {});
// Even though the WindowTreeHost is occluded, the compositor should still be
// visible and content captured.
static constexpr SkColor kColorsToCycleThrough[] = {
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorYELLOW,
SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorWHITE,
};
for (SkColor color : kColorsToCycleThrough) {
ChangePageContentColor(color);
WaitForFrameWithColor(color);
}
StopAndDeAllocate();
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
// Disabled (https://crbug.com/1096946)
// On ChromeOS, another window may occlude a window that is being captured.
// Make sure the visibility is set to visible during capture if it's occluded.
IN_PROC_BROWSER_TEST_F(AuraWindowVideoCaptureDeviceBrowserTest,
DISABLED_CapturesOccludedWindows) {
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
ASSERT_EQ(aura::Window::OcclusionState::VISIBLE,
shell()->web_contents()->GetNativeView()->GetOcclusionState());
// Create a window on top of the window being captured with same size so that
// it is occluded.
auto window = std::make_unique<aura::Window>(nullptr);
window->Init(ui::LAYER_TEXTURED);
shell()->window()->GetRootWindow()->AddChild(window.get());
window->SetBounds(shell()->window()->bounds());
window->Show();
EXPECT_EQ(aura::Window::OcclusionState::VISIBLE,
shell()->web_contents()->GetNativeView()->GetOcclusionState());
window.reset();
StopAndDeAllocate();
}
#endif // BUILDFLAG(IS_CHROMEOS)
class AuraWindowVideoCaptureDeviceBrowserTestP
: public AuraWindowVideoCaptureDeviceBrowserTest,
public testing::WithParamInterface<std::tuple<bool, bool>> {
public:
bool IsSoftwareCompositingTest() const override {
return std::get<0>(GetParam());
}
bool IsFixedAspectRatioTest() const override {
return std::get<1>(GetParam());
}
};
#if BUILDFLAG(IS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P(
All,
AuraWindowVideoCaptureDeviceBrowserTestP,
testing::Combine(
// Note: On ChromeOS, software compositing is not an option.
testing::Values(false /* GPU-accelerated compositing */),
testing::Values(false /* variable aspect ratio */,
true /* fixed aspect ratio */)));
#else
INSTANTIATE_TEST_SUITE_P(
All,
AuraWindowVideoCaptureDeviceBrowserTestP,
testing::Combine(testing::Values(false /* GPU-accelerated compositing */,
true /* software compositing */),
testing::Values(false /* variable aspect ratio */,
true /* fixed aspect ratio */)));
#endif // BUILDFLAG(IS_CHROMEOS)
// Disabled (https://crbug.com/1096946)
// Tests that the device successfully captures a series of content changes,
// whether the browser is running with software compositing or GPU-accelerated
// compositing.
IN_PROC_BROWSER_TEST_P(AuraWindowVideoCaptureDeviceBrowserTestP,
DISABLED_CapturesContentChanges) {
SCOPED_TRACE(testing::Message()
<< "Test parameters: "
<< (IsSoftwareCompositingTest() ? "Software Compositing"
: "GPU Compositing")
<< " with "
<< (IsFixedAspectRatioTest() ? "Fixed Video Aspect Ratio"
: "Variable Video Aspect Ratio"));
NavigateToInitialDocument();
AllocateAndStartAndWaitForFirstFrame();
ASSERT_EQ(shell()->web_contents()->GetVisibility(),
content::Visibility::VISIBLE);
static constexpr SkColor kColorsToCycleThrough[] = {
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorYELLOW,
SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorWHITE,
};
for (SkColor color : kColorsToCycleThrough) {
ChangePageContentColor(color);
WaitForFrameWithColor(color);
}
StopAndDeAllocate();
}
} // namespace
} // namespace content
|