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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/rand_util.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/tick_clock.h"
#include "media/cast/cast_environment.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/logging/stats_event_subscriber.h"
#include "media/cast/test/fake_receiver_time_offset_estimator.h"
#include "media/cast/test/fake_single_thread_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const int kReceiverOffsetSecs = 100;
}
namespace media {
namespace cast {
class StatsEventSubscriberTest : public ::testing::Test {
protected:
StatsEventSubscriberTest()
: sender_clock_(new base::SimpleTestTickClock()),
task_runner_(new test::FakeSingleThreadTaskRunner(sender_clock_)),
cast_environment_(new CastEnvironment(
scoped_ptr<base::TickClock>(sender_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_)),
fake_offset_estimator_(
base::TimeDelta::FromSeconds(kReceiverOffsetSecs)) {
receiver_clock_.Advance(base::TimeDelta::FromSeconds(kReceiverOffsetSecs));
cast_environment_->Logging()->AddRawEventSubscriber(
&fake_offset_estimator_);
}
virtual ~StatsEventSubscriberTest() {
if (subscriber_.get())
cast_environment_->Logging()->RemoveRawEventSubscriber(subscriber_.get());
cast_environment_->Logging()->RemoveRawEventSubscriber(
&fake_offset_estimator_);
}
void AdvanceClocks(base::TimeDelta delta) {
sender_clock_->Advance(delta);
receiver_clock_.Advance(delta);
}
void Init(EventMediaType event_media_type) {
DCHECK(!subscriber_.get());
subscriber_.reset(new StatsEventSubscriber(
event_media_type, cast_environment_->Clock(), &fake_offset_estimator_));
cast_environment_->Logging()->AddRawEventSubscriber(subscriber_.get());
}
base::SimpleTestTickClock* sender_clock_; // Owned by CastEnvironment.
base::SimpleTestTickClock receiver_clock_;
scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
scoped_refptr<CastEnvironment> cast_environment_;
test::FakeReceiverTimeOffsetEstimator fake_offset_estimator_;
scoped_ptr<StatsEventSubscriber> subscriber_;
};
TEST_F(StatsEventSubscriberTest, Capture) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
uint32 frame_id = 0;
int num_frames = 10;
base::TimeTicks start_time = sender_clock_->NowTicks();
for (int i = 0; i < num_frames; i++) {
cast_environment_->Logging()->InsertFrameEvent(sender_clock_->NowTicks(),
FRAME_CAPTURE_BEGIN,
VIDEO_EVENT,
rtp_timestamp,
frame_id);
AdvanceClocks(base::TimeDelta::FromMicroseconds(34567));
rtp_timestamp += 90;
frame_id++;
}
base::TimeTicks end_time = sender_clock_->NowTicks();
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::CAPTURE_FPS);
ASSERT_NE(it, stats_map.end());
base::TimeDelta duration = end_time - start_time;
EXPECT_DOUBLE_EQ(
it->second,
static_cast<double>(num_frames) / duration.InMillisecondsF() * 1000);
}
TEST_F(StatsEventSubscriberTest, Encode) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
uint32 frame_id = 0;
int num_frames = 10;
base::TimeTicks start_time = sender_clock_->NowTicks();
int total_size = 0;
for (int i = 0; i < num_frames; i++) {
int size = 1000 + base::RandInt(-100, 100);
total_size += size;
cast_environment_->Logging()->InsertEncodedFrameEvent(
sender_clock_->NowTicks(),
FRAME_ENCODED, VIDEO_EVENT,
rtp_timestamp,
frame_id,
size,
true,
5678);
AdvanceClocks(base::TimeDelta::FromMicroseconds(35678));
rtp_timestamp += 90;
frame_id++;
}
base::TimeTicks end_time = sender_clock_->NowTicks();
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::ENCODE_FPS);
ASSERT_NE(it, stats_map.end());
base::TimeDelta duration = end_time - start_time;
EXPECT_DOUBLE_EQ(
it->second,
static_cast<double>(num_frames) / duration.InMillisecondsF() * 1000);
it = stats_map.find(StatsEventSubscriber::ENCODE_KBPS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(it->second,
static_cast<double>(total_size) / duration.InMillisecondsF() * 8);
}
TEST_F(StatsEventSubscriberTest, Decode) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
uint32 frame_id = 0;
int num_frames = 10;
base::TimeTicks start_time = sender_clock_->NowTicks();
for (int i = 0; i < num_frames; i++) {
cast_environment_->Logging()->InsertFrameEvent(receiver_clock_.NowTicks(),
FRAME_DECODED, VIDEO_EVENT,
rtp_timestamp,
frame_id);
AdvanceClocks(base::TimeDelta::FromMicroseconds(36789));
rtp_timestamp += 90;
frame_id++;
}
base::TimeTicks end_time = sender_clock_->NowTicks();
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::DECODE_FPS);
ASSERT_NE(it, stats_map.end());
base::TimeDelta duration = end_time - start_time;
EXPECT_DOUBLE_EQ(
it->second,
static_cast<double>(num_frames) / duration.InMillisecondsF() * 1000);
}
TEST_F(StatsEventSubscriberTest, PlayoutDelay) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
uint32 frame_id = 0;
int num_frames = 10;
int total_delay_ms = 0;
for (int i = 0; i < num_frames; i++) {
int delay_ms = base::RandInt(-50, 50);
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(delay_ms);
total_delay_ms += delay_ms;
cast_environment_->Logging()->InsertFrameEventWithDelay(
receiver_clock_.NowTicks(),
FRAME_PLAYOUT,
VIDEO_EVENT,
rtp_timestamp,
frame_id,
delay);
AdvanceClocks(base::TimeDelta::FromMicroseconds(37890));
rtp_timestamp += 90;
frame_id++;
}
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::AVG_PLAYOUT_DELAY_MS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(
it->second, static_cast<double>(total_delay_ms) / num_frames);
}
TEST_F(StatsEventSubscriberTest, E2ELatency) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
uint32 frame_id = 0;
int num_frames = 10;
base::TimeDelta total_latency;
for (int i = 0; i < num_frames; i++) {
cast_environment_->Logging()->InsertFrameEvent(sender_clock_->NowTicks(),
FRAME_CAPTURE_BEGIN,
VIDEO_EVENT,
rtp_timestamp,
frame_id);
int latency_micros = 100000 + base::RandInt(-5000, 50000);
base::TimeDelta latency = base::TimeDelta::FromMicroseconds(latency_micros);
AdvanceClocks(latency);
int delay_micros = base::RandInt(-50000, 50000);
base::TimeDelta delay = base::TimeDelta::FromMilliseconds(delay_micros);
total_latency += latency + delay;
cast_environment_->Logging()->InsertFrameEventWithDelay(
receiver_clock_.NowTicks(),
FRAME_PLAYOUT,
VIDEO_EVENT,
rtp_timestamp,
frame_id,
delay);
rtp_timestamp += 90;
frame_id++;
}
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::AVG_E2E_LATENCY_MS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(
it->second, total_latency.InMillisecondsF() / num_frames);
}
TEST_F(StatsEventSubscriberTest, Packets) {
Init(VIDEO_EVENT);
uint32 rtp_timestamp = 0;
int num_packets = 10;
int num_latency_recorded_packets = 0;
base::TimeTicks start_time = sender_clock_->NowTicks();
int total_size = 0;
int retransmit_total_size = 0;
base::TimeDelta total_latency;
int num_packets_sent = 0;
int num_packets_retransmitted = 0;
// Every 2nd packet will be retransmitted once.
// Every 4th packet will be retransmitted twice.
// Every 8th packet will be retransmitted 3 times.
for (int i = 0; i < num_packets; i++) {
int size = 1000 + base::RandInt(-100, 100);
total_size += size;
cast_environment_->Logging()->InsertPacketEvent(sender_clock_->NowTicks(),
PACKET_SENT_TO_NETWORK,
VIDEO_EVENT,
rtp_timestamp,
0,
i,
num_packets - 1,
size);
num_packets_sent++;
int latency_micros = 20000 + base::RandInt(-10000, 10000);
base::TimeDelta latency = base::TimeDelta::FromMicroseconds(latency_micros);
// Latency is only recorded for packets that aren't retransmitted.
if (i % 2 != 0) {
total_latency += latency;
num_latency_recorded_packets++;
}
AdvanceClocks(latency);
base::TimeTicks received_time = receiver_clock_.NowTicks();
// Retransmission 1.
AdvanceClocks(base::TimeDelta::FromMicroseconds(12345));
if (i % 2 == 0) {
cast_environment_->Logging()->InsertPacketEvent(
receiver_clock_.NowTicks(),
PACKET_RETRANSMITTED,
VIDEO_EVENT,
rtp_timestamp,
0,
i,
num_packets - 1,
size);
retransmit_total_size += size;
num_packets_sent++;
num_packets_retransmitted++;
}
// Retransmission 2.
AdvanceClocks(base::TimeDelta::FromMicroseconds(13456));
if (i % 4 == 0) {
cast_environment_->Logging()->InsertPacketEvent(
receiver_clock_.NowTicks(),
PACKET_RETRANSMITTED,
VIDEO_EVENT,
rtp_timestamp,
0,
i,
num_packets - 1,
size);
retransmit_total_size += size;
num_packets_sent++;
num_packets_retransmitted++;
}
// Retransmission 3.
AdvanceClocks(base::TimeDelta::FromMicroseconds(14567));
if (i % 8 == 0) {
cast_environment_->Logging()->InsertPacketEvent(
receiver_clock_.NowTicks(),
PACKET_RETRANSMITTED,
VIDEO_EVENT,
rtp_timestamp,
0,
i,
num_packets - 1,
size);
retransmit_total_size += size;
num_packets_sent++;
num_packets_retransmitted++;
}
cast_environment_->Logging()->InsertPacketEvent(received_time,
PACKET_RECEIVED,
VIDEO_EVENT,
rtp_timestamp,
0,
i,
num_packets - 1,
size);
}
base::TimeTicks end_time = sender_clock_->NowTicks();
base::TimeDelta duration = end_time - start_time;
StatsEventSubscriber::StatsMap stats_map;
subscriber_->GetStatsInternal(&stats_map);
// Measure AVG_NETWORK_LATENCY_MS, TRANSMISSION_KBPS, RETRANSMISSION_KBPS,
// and PACKET_LOSS_FRACTION.
StatsEventSubscriber::StatsMap::iterator it =
stats_map.find(StatsEventSubscriber::AVG_NETWORK_LATENCY_MS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(
it->second,
total_latency.InMillisecondsF() / num_latency_recorded_packets);
it = stats_map.find(StatsEventSubscriber::TRANSMISSION_KBPS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(it->second,
static_cast<double>(total_size) / duration.InMillisecondsF() * 8);
it = stats_map.find(StatsEventSubscriber::RETRANSMISSION_KBPS);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(it->second,
static_cast<double>(retransmit_total_size) /
duration.InMillisecondsF() * 8);
it = stats_map.find(StatsEventSubscriber::PACKET_LOSS_FRACTION);
ASSERT_NE(it, stats_map.end());
EXPECT_DOUBLE_EQ(
it->second,
static_cast<double>(num_packets_retransmitted) / num_packets_sent);
}
} // namespace cast
} // namespace media
|