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(ReceiverRtcpEventSubscriber::Type 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(), kVideoRenderDelay
, /*rtp_timestamp*/ 100u,
56 /*frame_id*/ 2u, base::TimeDelta::FromMilliseconds(kDelayMs
));
57 cast_environment_
->Logging()->InsertFrameEvent(
58 testing_clock_
->NowTicks(), kVideoFrameDecoded
, /*rtp_timestamp*/ 200u,
60 cast_environment_
->Logging()->InsertPacketEvent(
61 testing_clock_
->NowTicks(), kVideoPacketReceived
,
62 /*rtp_timestamp */ 200u, /*frame_id*/ 2u, /*packet_id*/ 1u,
63 /*max_packet_id*/ 10u, /*size*/ 1024u);
66 cast_environment_
->Logging()->InsertFrameEventWithDelay(
67 testing_clock_
->NowTicks(), kAudioPlayoutDelay
, /*rtp_timestamp*/ 300u,
68 /*frame_id*/ 4u, base::TimeDelta::FromMilliseconds(kDelayMs
));
69 cast_environment_
->Logging()->InsertFrameEvent(
70 testing_clock_
->NowTicks(), kAudioFrameDecoded
, /*rtp_timestamp*/ 400u,
72 cast_environment_
->Logging()->InsertPacketEvent(
73 testing_clock_
->NowTicks(), kAudioPacketReceived
,
74 /*rtp_timestamp */ 400u, /*frame_id*/ 5u, /*packet_id*/ 1u,
75 /*max_packet_id*/ 10u, /*size*/ 128u);
78 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
79 kVideoFrameCaptureEnd
,
80 /*rtp_timestamp*/ 100u,
82 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
83 kAudioFrameCaptureEnd
,
84 /*rtp_timestamp*/ 100u,
88 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
89 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
90 scoped_refptr
<CastEnvironment
> cast_environment_
;
91 scoped_ptr
<ReceiverRtcpEventSubscriber
> event_subscriber_
;
94 TEST_F(ReceiverRtcpEventSubscriberTest
, LogVideoEvents
) {
95 Init(ReceiverRtcpEventSubscriber::kVideoEventSubscriber
);
98 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
99 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
100 EXPECT_EQ(3u, rtcp_events
.size());
103 TEST_F(ReceiverRtcpEventSubscriberTest
, LogAudioEvents
) {
104 Init(ReceiverRtcpEventSubscriber::kAudioEventSubscriber
);
107 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
108 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
109 EXPECT_EQ(3u, rtcp_events
.size());
112 TEST_F(ReceiverRtcpEventSubscriberTest
, DropEventsWhenSizeExceeded
) {
113 Init(ReceiverRtcpEventSubscriber::kVideoEventSubscriber
);
115 for (uint32 i
= 1u; i
<= 10u; ++i
) {
116 cast_environment_
->Logging()->InsertFrameEvent(
117 testing_clock_
->NowTicks(), kVideoFrameDecoded
,
118 /*rtp_timestamp*/ i
* 10, /*frame_id*/ i
);
121 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events
;
122 event_subscriber_
->GetRtcpEventsAndReset(&rtcp_events
);
123 EXPECT_EQ(10u, rtcp_events
.size());