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/receiver_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;
21 const int64 kDelayMs
= 20L;
25 class ReceiverRtcpEventSubscriberTest
: public ::testing::Test
{
27 ReceiverRtcpEventSubscriberTest()
28 : testing_clock_(new base::SimpleTestTickClock()),
29 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_
)),
30 cast_environment_(new CastEnvironment(
31 scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
36 virtual ~ReceiverRtcpEventSubscriberTest() {}
38 virtual void TearDown() OVERRIDE
{
39 if (event_subscriber_
) {
40 cast_environment_
->Logging()->RemoveRawEventSubscriber(
41 event_subscriber_
.get());
45 void Init(EventMediaType type
) {
46 event_subscriber_
.reset(
47 new ReceiverRtcpEventSubscriber(kMaxEventEntries
, type
));
48 cast_environment_
->Logging()->AddRawEventSubscriber(
49 event_subscriber_
.get());
54 cast_environment_
->Logging()->InsertFrameEventWithDelay(
55 testing_clock_
->NowTicks(), FRAME_PLAYOUT
, VIDEO_EVENT
,
56 /*rtp_timestamp*/ 100u, /*frame_id*/ 2u,
57 base::TimeDelta::FromMilliseconds(kDelayMs
));
58 cast_environment_
->Logging()->InsertFrameEvent(
59 testing_clock_
->NowTicks(), FRAME_DECODED
, VIDEO_EVENT
,
60 /*rtp_timestamp*/ 200u, /*frame_id*/ 1u);
61 cast_environment_
->Logging()->InsertPacketEvent(
62 testing_clock_
->NowTicks(), PACKET_RECEIVED
, VIDEO_EVENT
,
63 /*rtp_timestamp */ 200u, /*frame_id*/ 2u, /*packet_id*/ 1u,
64 /*max_packet_id*/ 10u, /*size*/ 1024u);
67 cast_environment_
->Logging()->InsertFrameEventWithDelay(
68 testing_clock_
->NowTicks(), FRAME_PLAYOUT
, AUDIO_EVENT
,
69 /*rtp_timestamp*/ 300u, /*frame_id*/ 4u,
70 base::TimeDelta::FromMilliseconds(kDelayMs
));
71 cast_environment_
->Logging()->InsertFrameEvent(
72 testing_clock_
->NowTicks(), FRAME_DECODED
, AUDIO_EVENT
,
73 /*rtp_timestamp*/ 400u, /*frame_id*/ 3u);
74 cast_environment_
->Logging()->InsertPacketEvent(
75 testing_clock_
->NowTicks(), PACKET_RECEIVED
, AUDIO_EVENT
,
76 /*rtp_timestamp */ 400u, /*frame_id*/ 5u, /*packet_id*/ 1u,
77 /*max_packet_id*/ 10u, /*size*/ 128u);
80 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
83 /*rtp_timestamp*/ 100u,
85 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
88 /*rtp_timestamp*/ 100u,
92 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
93 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
94 scoped_refptr
<CastEnvironment
> cast_environment_
;
95 scoped_ptr
<ReceiverRtcpEventSubscriber
> event_subscriber_
;
98 TEST_F(ReceiverRtcpEventSubscriberTest
, LogVideoEvents
) {
102 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
103 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
104 EXPECT_EQ(3u, rtcp_events
.size());
107 TEST_F(ReceiverRtcpEventSubscriberTest
, LogAudioEvents
) {
111 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
112 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
113 EXPECT_EQ(3u, rtcp_events
.size());
116 TEST_F(ReceiverRtcpEventSubscriberTest
, DropEventsWhenSizeExceeded
) {
119 for (uint32 i
= 1u; i
<= 10u; ++i
) {
120 cast_environment_
->Logging()->InsertFrameEvent(
121 testing_clock_
->NowTicks(), FRAME_DECODED
, VIDEO_EVENT
,
122 /*rtp_timestamp*/ i
* 10, /*frame_id*/ i
);
125 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
126 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
127 EXPECT_EQ(10u, rtcp_events
.size());