1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/media/capture/video_capture_oracle.h"
7 #include "base/strings/stringprintf.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 base::TimeTicks
InitialTestTimeTicks() {
15 return base::TimeTicks() + base::TimeDelta::FromSeconds(1);
20 // Tests that VideoCaptureOracle filters out events whose timestamps are
22 TEST(VideoCaptureOracleTest
, EnforcesEventTimeMonotonicity
) {
23 const base::TimeDelta min_capture_period
=
24 base::TimeDelta::FromSeconds(1) / 30;
25 const gfx::Rect
damage_rect(0, 0, 1280, 720);
26 const base::TimeDelta event_increment
= min_capture_period
* 2;
28 VideoCaptureOracle
oracle(min_capture_period
);
30 base::TimeTicks t
= InitialTestTimeTicks();
31 for (int i
= 0; i
< 10; ++i
) {
33 ASSERT_TRUE(oracle
.ObserveEventAndDecideCapture(
34 VideoCaptureOracle::kCompositorUpdate
,
38 base::TimeTicks furthest_event_time
= t
;
39 for (int i
= 0; i
< 10; ++i
) {
41 ASSERT_FALSE(oracle
.ObserveEventAndDecideCapture(
42 VideoCaptureOracle::kCompositorUpdate
,
46 t
= furthest_event_time
;
47 for (int i
= 0; i
< 10; ++i
) {
49 ASSERT_TRUE(oracle
.ObserveEventAndDecideCapture(
50 VideoCaptureOracle::kCompositorUpdate
,
55 // Tests that VideoCaptureOracle is enforcing the requirement that captured
56 // frames are delivered in order. Otherwise, downstream consumers could be
57 // tripped-up by out-of-order frames or frame timestamps.
58 TEST(VideoCaptureOracleTest
, EnforcesFramesDeliveredInOrder
) {
59 const base::TimeDelta min_capture_period
=
60 base::TimeDelta::FromSeconds(1) / 30;
61 const gfx::Rect
damage_rect(0, 0, 1280, 720);
62 const base::TimeDelta event_increment
= min_capture_period
* 2;
64 VideoCaptureOracle
oracle(min_capture_period
);
66 // Most basic scenario: Frames delivered one at a time, with no additional
67 // captures in-between deliveries.
68 base::TimeTicks t
= InitialTestTimeTicks();
69 int last_frame_number
;
70 base::TimeTicks ignored
;
71 for (int i
= 0; i
< 10; ++i
) {
73 ASSERT_TRUE(oracle
.ObserveEventAndDecideCapture(
74 VideoCaptureOracle::kCompositorUpdate
,
76 last_frame_number
= oracle
.RecordCapture();
77 ASSERT_TRUE(oracle
.CompleteCapture(last_frame_number
, &ignored
));
80 // Basic pipelined scenario: More than one frame in-flight at delivery points.
81 for (int i
= 0; i
< 50; ++i
) {
82 const int num_in_flight
= 1 + i
% 3;
83 for (int j
= 0; j
< num_in_flight
; ++j
) {
85 ASSERT_TRUE(oracle
.ObserveEventAndDecideCapture(
86 VideoCaptureOracle::kCompositorUpdate
,
88 last_frame_number
= oracle
.RecordCapture();
90 for (int j
= num_in_flight
- 1; j
>= 0; --j
) {
91 ASSERT_TRUE(oracle
.CompleteCapture(last_frame_number
- j
, &ignored
));
95 // Pipelined scenario with out-of-order delivery attempts rejected.
96 for (int i
= 0; i
< 50; ++i
) {
97 const int num_in_flight
= 1 + i
% 3;
98 for (int j
= 0; j
< num_in_flight
; ++j
) {
100 ASSERT_TRUE(oracle
.ObserveEventAndDecideCapture(
101 VideoCaptureOracle::kCompositorUpdate
,
103 last_frame_number
= oracle
.RecordCapture();
105 ASSERT_TRUE(oracle
.CompleteCapture(last_frame_number
, &ignored
));
106 for (int j
= 1; j
< num_in_flight
; ++j
) {
107 ASSERT_FALSE(oracle
.CompleteCapture(last_frame_number
- j
, &ignored
));
112 // Tests that VideoCaptureOracle transitions between using its two samplers in a
113 // way that does not introduce severe jank, pauses, etc.
114 TEST(VideoCaptureOracleTest
, TransitionsSmoothlyBetweenSamplers
) {
115 const base::TimeDelta min_capture_period
=
116 base::TimeDelta::FromSeconds(1) / 30;
117 const gfx::Rect
animation_damage_rect(0, 0, 1280, 720);
118 const base::TimeDelta event_increment
= min_capture_period
* 2;
120 VideoCaptureOracle
oracle(min_capture_period
);
122 // Run sequences of animation events and non-animation events through the
123 // oracle. As the oracle transitions between each sampler, make sure the
124 // frame timestamps won't trip-up downstream consumers.
125 base::TimeTicks t
= InitialTestTimeTicks();
126 base::TimeTicks last_frame_timestamp
;
127 for (int i
= 0; i
< 1000; ++i
) {
128 t
+= event_increment
;
130 // For every 100 events, provide 50 that will cause the
131 // AnimatedContentSampler to lock-in, followed by 50 that will cause it to
132 // lock-out (i.e., the oracle will use the SmoothEventSampler instead).
133 const bool provide_animated_content_event
=
134 (i
% 100) >= 25 && (i
% 100) < 75;
136 // Only the few events that trigger the lock-out transition should be
137 // dropped, because the AnimatedContentSampler doesn't yet realize the
138 // animation ended. Otherwise, the oracle should always decide to sample
139 // because one of its samplers says to.
140 const bool require_oracle_says_sample
= (i
% 100) < 75 || (i
% 100) >= 78;
141 const bool oracle_says_sample
= oracle
.ObserveEventAndDecideCapture(
142 VideoCaptureOracle::kCompositorUpdate
,
143 provide_animated_content_event
? animation_damage_rect
: gfx::Rect(),
145 if (require_oracle_says_sample
)
146 ASSERT_TRUE(oracle_says_sample
);
147 if (!oracle_says_sample
)
150 const int frame_number
= oracle
.RecordCapture();
152 base::TimeTicks frame_timestamp
;
153 ASSERT_TRUE(oracle
.CompleteCapture(frame_number
, &frame_timestamp
));
154 ASSERT_FALSE(frame_timestamp
.is_null());
155 if (!last_frame_timestamp
.is_null()) {
156 const base::TimeDelta delta
= frame_timestamp
- last_frame_timestamp
;
157 EXPECT_LE(event_increment
.InMicroseconds(), delta
.InMicroseconds());
158 // Right after the AnimatedContentSampler lock-out transition, there were
159 // a few frames dropped, so allow a gap in the timestamps. Otherwise, the
160 // delta between frame timestamps should never be more than 2X the
161 // |event_increment|.
162 const base::TimeDelta max_acceptable_delta
= (i
% 100) == 78 ?
163 event_increment
* 5 : event_increment
* 2;
164 EXPECT_GE(max_acceptable_delta
.InMicroseconds(), delta
.InMicroseconds());
166 last_frame_timestamp
= frame_timestamp
;
170 } // namespace content