1 // Copyright 2014 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.
9 #include "base/rand_util.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "base/time/tick_clock.h"
12 #include "base/time/time.h"
13 #include "media/cast/logging/logging_defines.h"
14 #include "media/cast/logging/logging_impl.h"
15 #include "media/cast/logging/simple_event_subscriber.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 // Insert frame duration- one second.
22 const int64 kIntervalTime1S
= 1;
23 // Test frame rate goal - 30fps.
24 const int kFrameIntervalMs
= 33;
26 static const int64 kStartMillisecond
= INT64_C(12345678900000);
28 class LoggingImplTest
: public ::testing::Test
{
31 testing_clock_
.Advance(
32 base::TimeDelta::FromMilliseconds(kStartMillisecond
));
33 logging_
.AddRawEventSubscriber(&event_subscriber_
);
36 ~LoggingImplTest() override
{
37 logging_
.RemoveRawEventSubscriber(&event_subscriber_
);
41 base::SimpleTestTickClock testing_clock_
;
42 SimpleEventSubscriber event_subscriber_
;
44 DISALLOW_COPY_AND_ASSIGN(LoggingImplTest
);
47 TEST_F(LoggingImplTest
, BasicFrameLogging
) {
48 base::TimeTicks start_time
= testing_clock_
.NowTicks();
49 base::TimeDelta time_interval
= testing_clock_
.NowTicks() - start_time
;
50 uint32 rtp_timestamp
= 0;
54 now
= testing_clock_
.NowTicks();
55 logging_
.InsertFrameEvent(
56 now
, FRAME_CAPTURE_BEGIN
, VIDEO_EVENT
, rtp_timestamp
, frame_id
);
57 testing_clock_
.Advance(
58 base::TimeDelta::FromMilliseconds(kFrameIntervalMs
));
59 rtp_timestamp
+= kFrameIntervalMs
* 90;
61 time_interval
= now
- start_time
;
62 } while (time_interval
.InSeconds() < kIntervalTime1S
);
65 std::vector
<FrameEvent
> frame_events
;
66 event_subscriber_
.GetFrameEventsAndReset(&frame_events
);
67 // Size of vector should be equal to the number of events logged,
68 // which equals to number of frames in this case.
69 EXPECT_EQ(frame_id
, frame_events
.size());
72 TEST_F(LoggingImplTest
, FrameLoggingWithSize
) {
73 // Average packet size.
74 const int kBaseFrameSizeBytes
= 25000;
75 const int kRandomSizeInterval
= 100;
76 base::TimeTicks start_time
= testing_clock_
.NowTicks();
77 base::TimeDelta time_interval
= testing_clock_
.NowTicks() - start_time
;
78 uint32 rtp_timestamp
= 0;
81 int target_bitrate
= 1234;
83 int size
= kBaseFrameSizeBytes
+
84 base::RandInt(-kRandomSizeInterval
, kRandomSizeInterval
);
85 sum_size
+= static_cast<size_t>(size
);
86 logging_
.InsertEncodedFrameEvent(testing_clock_
.NowTicks(),
87 FRAME_ENCODED
, VIDEO_EVENT
, rtp_timestamp
,
88 frame_id
, size
, true, target_bitrate
,
90 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs
));
91 rtp_timestamp
+= kFrameIntervalMs
* 90;
93 time_interval
= testing_clock_
.NowTicks() - start_time
;
94 } while (time_interval
.InSeconds() < kIntervalTime1S
);
96 std::vector
<FrameEvent
> frame_events
;
97 event_subscriber_
.GetFrameEventsAndReset(&frame_events
);
98 // Size of vector should be equal to the number of events logged, which
99 // equals to number of frames in this case.
100 EXPECT_EQ(frame_id
, frame_events
.size());
103 TEST_F(LoggingImplTest
, FrameLoggingWithDelay
) {
104 // Average packet size.
105 const int kPlayoutDelayMs
= 50;
106 const int kRandomSizeInterval
= 20;
107 base::TimeTicks start_time
= testing_clock_
.NowTicks();
108 base::TimeDelta time_interval
= testing_clock_
.NowTicks() - start_time
;
109 uint32 rtp_timestamp
= 0;
112 int delay
= kPlayoutDelayMs
+
113 base::RandInt(-kRandomSizeInterval
, kRandomSizeInterval
);
114 logging_
.InsertFrameEventWithDelay(
115 testing_clock_
.NowTicks(),
120 base::TimeDelta::FromMilliseconds(delay
));
121 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs
));
122 rtp_timestamp
+= kFrameIntervalMs
* 90;
124 time_interval
= testing_clock_
.NowTicks() - start_time
;
125 } while (time_interval
.InSeconds() < kIntervalTime1S
);
127 std::vector
<FrameEvent
> frame_events
;
128 event_subscriber_
.GetFrameEventsAndReset(&frame_events
);
129 // Size of vector should be equal to the number of frames logged.
130 EXPECT_EQ(frame_id
, frame_events
.size());
133 TEST_F(LoggingImplTest
, MultipleEventFrameLogging
) {
134 base::TimeTicks start_time
= testing_clock_
.NowTicks();
135 base::TimeDelta time_interval
= testing_clock_
.NowTicks() - start_time
;
136 uint32 rtp_timestamp
= 0u;
137 uint32 frame_id
= 0u;
138 uint32 num_events
= 0u;
140 logging_
.InsertFrameEvent(testing_clock_
.NowTicks(),
147 logging_
.InsertEncodedFrameEvent(testing_clock_
.NowTicks(),
148 FRAME_ENCODED
, AUDIO_EVENT
,
150 frame_id
, 1500, true, 0, 4.5, 6.7);
151 } else if (frame_id
% 3) {
152 logging_
.InsertFrameEvent(testing_clock_
.NowTicks(), FRAME_DECODED
,
153 VIDEO_EVENT
, rtp_timestamp
, frame_id
);
155 logging_
.InsertFrameEventWithDelay(
156 testing_clock_
.NowTicks(), FRAME_PLAYOUT
, VIDEO_EVENT
,
157 rtp_timestamp
, frame_id
, base::TimeDelta::FromMilliseconds(20));
161 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs
));
162 rtp_timestamp
+= kFrameIntervalMs
* 90;
164 time_interval
= testing_clock_
.NowTicks() - start_time
;
165 } while (time_interval
.InSeconds() < kIntervalTime1S
);
167 std::vector
<FrameEvent
> frame_events
;
168 event_subscriber_
.GetFrameEventsAndReset(&frame_events
);
169 // Size of vector should be equal to the number of frames logged.
170 EXPECT_EQ(num_events
, frame_events
.size());
171 // Multiple events captured per frame.
174 TEST_F(LoggingImplTest
, PacketLogging
) {
175 const int kNumPacketsPerFrame
= 10;
176 const int kBaseSize
= 2500;
177 const int kSizeInterval
= 100;
178 base::TimeTicks start_time
= testing_clock_
.NowTicks();
179 base::TimeTicks latest_time
;
180 base::TimeDelta time_interval
= testing_clock_
.NowTicks() - start_time
;
181 RtpTimestamp rtp_timestamp
= 0;
186 for (int i
= 0; i
< kNumPacketsPerFrame
; ++i
) {
187 int size
= kBaseSize
+ base::RandInt(-kSizeInterval
, kSizeInterval
);
189 latest_time
= testing_clock_
.NowTicks();
191 logging_
.InsertPacketEvent(latest_time
,
200 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs
));
201 rtp_timestamp
+= kFrameIntervalMs
* 90;
203 time_interval
= testing_clock_
.NowTicks() - start_time
;
204 } while (time_interval
.InSeconds() < kIntervalTime1S
);
206 std::vector
<PacketEvent
> packet_events
;
207 event_subscriber_
.GetPacketEventsAndReset(&packet_events
);
208 // Size of vector should be equal to the number of packets logged.
209 EXPECT_EQ(num_packets
, static_cast<int>(packet_events
.size()));
212 TEST_F(LoggingImplTest
, MultipleRawEventSubscribers
) {
213 SimpleEventSubscriber event_subscriber_2
;
215 // Now logging_ has two subscribers.
216 logging_
.AddRawEventSubscriber(&event_subscriber_2
);
218 logging_
.InsertFrameEvent(testing_clock_
.NowTicks(),
221 /*rtp_timestamp*/ 0u,
224 std::vector
<FrameEvent
> frame_events
;
225 event_subscriber_
.GetFrameEventsAndReset(&frame_events
);
226 EXPECT_EQ(1u, frame_events
.size());
227 frame_events
.clear();
228 event_subscriber_2
.GetFrameEventsAndReset(&frame_events
);
229 EXPECT_EQ(1u, frame_events
.size());
231 logging_
.RemoveRawEventSubscriber(&event_subscriber_2
);