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 "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/cast_environment.h"
10 #include "media/cast/logging/logging_defines.h"
11 #include "media/cast/rtcp/sender_rtcp_event_subscriber.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
20 const size_t kMaxEventEntries
= 10u;
24 class SenderRtcpEventSubscriberTest
: public ::testing::Test
{
26 SenderRtcpEventSubscriberTest()
27 : testing_clock_(new base::SimpleTestTickClock()),
28 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_
)),
29 cast_environment_(new CastEnvironment(
30 scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(), task_runner_
,
31 task_runner_
, task_runner_
, task_runner_
, task_runner_
,
32 task_runner_
, GetLoggingConfigWithRawEventsAndStatsEnabled())),
33 event_subscriber_(kMaxEventEntries
) {
34 cast_environment_
->Logging()->AddRawEventSubscriber(&event_subscriber_
);
37 virtual ~SenderRtcpEventSubscriberTest() {
38 cast_environment_
->Logging()->RemoveRawEventSubscriber(&event_subscriber_
);
41 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
42 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
43 scoped_refptr
<CastEnvironment
> cast_environment_
;
44 SenderRtcpEventSubscriber event_subscriber_
;
47 TEST_F(SenderRtcpEventSubscriberTest
, InsertEntry
) {
48 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
49 kVideoFrameCaptured
, 100u, 1u);
50 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
51 kVideoFrameCaptured
, 200u, 2u);
52 cast_environment_
->Logging()->InsertFrameEvent(
53 testing_clock_
->NowTicks(), kVideoFrameSentToEncoder
, 100u, 1u);
54 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
55 kVideoFrameEncoded
, 100u, 1u);
56 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
57 kVideoFrameEncoded
, 300u, 3u);
58 cast_environment_
->Logging()->InsertFrameEvent(
59 testing_clock_
->NowTicks(), kVideoFrameSentToEncoder
, 300u, 3u);
62 event_subscriber_
.GetRtcpEventsAndReset(&events
);
64 ASSERT_EQ(3u, events
.size());
66 RtcpEventMap::iterator it
= events
.begin();
67 EXPECT_EQ(100u, it
->first
);
68 EXPECT_EQ(kVideoFrameEncoded
, it
->second
.type
);
71 EXPECT_EQ(200u, it
->first
);
72 EXPECT_EQ(kVideoFrameCaptured
, it
->second
.type
);
75 EXPECT_EQ(300u, it
->first
);
76 EXPECT_EQ(kVideoFrameEncoded
, it
->second
.type
);
79 TEST_F(SenderRtcpEventSubscriberTest
, MapReset
) {
80 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
81 kVideoFrameCaptured
, 100u, 1u);
84 event_subscriber_
.GetRtcpEventsAndReset(&events
);
85 EXPECT_EQ(1u, events
.size());
87 // Call again without any logging in between, should return empty map.
88 event_subscriber_
.GetRtcpEventsAndReset(&events
);
89 EXPECT_TRUE(events
.empty());
92 TEST_F(SenderRtcpEventSubscriberTest
, DropEventsWhenSizeExceeded
) {
93 for (uint32 i
= 1u; i
<= 10u; ++i
) {
94 cast_environment_
->Logging()->InsertFrameEvent(
95 testing_clock_
->NowTicks(), kVideoFrameCaptured
, i
* 10, i
);
99 event_subscriber_
.GetRtcpEventsAndReset(&events
);
101 ASSERT_EQ(10u, events
.size());
102 EXPECT_EQ(10u, events
.begin()->first
);
103 EXPECT_EQ(100u, events
.rbegin()->first
);
105 for (uint32 i
= 1u; i
<= 11u; ++i
) {
106 cast_environment_
->Logging()->InsertFrameEvent(
107 testing_clock_
->NowTicks(), kVideoFrameCaptured
, i
* 10, i
);
110 event_subscriber_
.GetRtcpEventsAndReset(&events
);
112 // Event with RTP timestamp 10 should have been dropped when 110 is inserted.
113 ASSERT_EQ(10u, events
.size());
114 EXPECT_EQ(20u, events
.begin()->first
);
115 EXPECT_EQ(110u, events
.rbegin()->first
);