Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / logging / logging_impl_unittest.cc
blobfb2779c049e6598ace14000a05ac66a4ab93acd7
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.
5 #include <stdint.h>
7 #include <vector>
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"
18 namespace media {
19 namespace cast {
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 {
29 protected:
30 LoggingImplTest() {
31 testing_clock_.Advance(
32 base::TimeDelta::FromMilliseconds(kStartMillisecond));
33 logging_.AddRawEventSubscriber(&event_subscriber_);
36 ~LoggingImplTest() override {
37 logging_.RemoveRawEventSubscriber(&event_subscriber_);
40 LoggingImpl logging_;
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;
51 uint32 frame_id = 0;
52 base::TimeTicks now;
53 do {
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;
60 ++frame_id;
61 time_interval = now - start_time;
62 } while (time_interval.InSeconds() < kIntervalTime1S);
64 // Get logging data.
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;
79 uint32 frame_id = 0;
80 size_t sum_size = 0;
81 int target_bitrate = 1234;
82 do {
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,
89 0.1, 2.3);
90 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs));
91 rtp_timestamp += kFrameIntervalMs * 90;
92 ++frame_id;
93 time_interval = testing_clock_.NowTicks() - start_time;
94 } while (time_interval.InSeconds() < kIntervalTime1S);
95 // Get logging data.
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;
110 uint32 frame_id = 0;
111 do {
112 int delay = kPlayoutDelayMs +
113 base::RandInt(-kRandomSizeInterval, kRandomSizeInterval);
114 logging_.InsertFrameEventWithDelay(
115 testing_clock_.NowTicks(),
116 FRAME_CAPTURE_BEGIN,
117 VIDEO_EVENT,
118 rtp_timestamp,
119 frame_id,
120 base::TimeDelta::FromMilliseconds(delay));
121 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs));
122 rtp_timestamp += kFrameIntervalMs * 90;
123 ++frame_id;
124 time_interval = testing_clock_.NowTicks() - start_time;
125 } while (time_interval.InSeconds() < kIntervalTime1S);
126 // Get logging data.
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;
139 do {
140 logging_.InsertFrameEvent(testing_clock_.NowTicks(),
141 FRAME_CAPTURE_END,
142 VIDEO_EVENT,
143 rtp_timestamp,
144 frame_id);
145 ++num_events;
146 if (frame_id % 2) {
147 logging_.InsertEncodedFrameEvent(testing_clock_.NowTicks(),
148 FRAME_ENCODED, AUDIO_EVENT,
149 rtp_timestamp,
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);
154 } else {
155 logging_.InsertFrameEventWithDelay(
156 testing_clock_.NowTicks(), FRAME_PLAYOUT, VIDEO_EVENT,
157 rtp_timestamp, frame_id, base::TimeDelta::FromMilliseconds(20));
159 ++num_events;
161 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs));
162 rtp_timestamp += kFrameIntervalMs * 90;
163 ++frame_id;
164 time_interval = testing_clock_.NowTicks() - start_time;
165 } while (time_interval.InSeconds() < kIntervalTime1S);
166 // Get logging data.
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;
182 int frame_id = 0;
183 int num_packets = 0;
184 int sum_size = 0u;
185 do {
186 for (int i = 0; i < kNumPacketsPerFrame; ++i) {
187 int size = kBaseSize + base::RandInt(-kSizeInterval, kSizeInterval);
188 sum_size += size;
189 latest_time = testing_clock_.NowTicks();
190 ++num_packets;
191 logging_.InsertPacketEvent(latest_time,
192 PACKET_RECEIVED,
193 VIDEO_EVENT,
194 rtp_timestamp,
195 frame_id,
197 kNumPacketsPerFrame,
198 size);
200 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(kFrameIntervalMs));
201 rtp_timestamp += kFrameIntervalMs * 90;
202 ++frame_id;
203 time_interval = testing_clock_.NowTicks() - start_time;
204 } while (time_interval.InSeconds() < kIntervalTime1S);
205 // Get logging data.
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(),
219 FRAME_CAPTURE_BEGIN,
220 VIDEO_EVENT,
221 /*rtp_timestamp*/ 0u,
222 /*frame_id*/ 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);
234 } // namespace cast
235 } // namespace media