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 "media/cast/net/rtp/rtp_packetizer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "media/cast/logging/logging_impl.h"
12 #include "media/cast/logging/simple_event_subscriber.h"
13 #include "media/cast/net/pacing/paced_sender.h"
14 #include "media/cast/net/rtp/packet_storage.h"
15 #include "media/cast/net/rtp/rtp_header_parser.h"
16 #include "media/cast/test/fake_single_thread_task_runner.h"
17 #include "testing/gmock/include/gmock/gmock.h"
23 static const int kPayload
= 127;
24 static const uint32 kTimestampMs
= 10;
25 static const uint16 kSeqNum
= 33;
26 static const int kMaxPacketLength
= 1500;
27 static const int kSsrc
= 0x12345;
28 static const unsigned int kFrameSize
= 5000;
31 class TestRtpPacketTransport
: public PacketSender
{
33 explicit TestRtpPacketTransport(RtpPacketizerConfig config
)
35 sequence_number_(kSeqNum
),
37 expected_number_of_packets_(0),
38 expected_packet_id_(0),
39 expected_frame_id_(0),
40 expected_rtp_timestamp_(0) {}
42 void VerifyRtpHeader(const RtpCastTestHeader
& rtp_header
) {
43 VerifyCommonRtpHeader(rtp_header
);
44 VerifyCastRtpHeader(rtp_header
);
47 void VerifyCommonRtpHeader(const RtpCastTestHeader
& rtp_header
) {
48 EXPECT_EQ(kPayload
, rtp_header
.payload_type
);
49 EXPECT_EQ(sequence_number_
, rtp_header
.sequence_number
);
50 EXPECT_EQ(expected_rtp_timestamp_
, rtp_header
.rtp_timestamp
);
51 EXPECT_EQ(config_
.ssrc
, rtp_header
.ssrc
);
52 EXPECT_EQ(0, rtp_header
.num_csrcs
);
55 void VerifyCastRtpHeader(const RtpCastTestHeader
& rtp_header
) {
56 EXPECT_FALSE(rtp_header
.is_key_frame
);
57 EXPECT_EQ(expected_frame_id_
, rtp_header
.frame_id
);
58 EXPECT_EQ(expected_packet_id_
, rtp_header
.packet_id
);
59 EXPECT_EQ(expected_number_of_packets_
- 1, rtp_header
.max_packet_id
);
60 EXPECT_TRUE(rtp_header
.is_reference
);
61 EXPECT_EQ(expected_frame_id_
- 1u, rtp_header
.reference_frame_id
);
64 bool SendPacket(PacketRef packet
, const base::Closure
& cb
) final
{
66 RtpHeaderParser
parser(&packet
->data
[0], packet
->data
.size());
67 RtpCastTestHeader rtp_header
;
68 parser
.Parse(&rtp_header
);
69 VerifyRtpHeader(rtp_header
);
71 ++expected_packet_id_
;
75 int64
GetBytesSent() final
{ return 0; }
77 size_t number_of_packets_received() const { return packets_sent_
; }
79 void set_expected_number_of_packets(size_t expected_number_of_packets
) {
80 expected_number_of_packets_
= expected_number_of_packets
;
83 void set_rtp_timestamp(uint32 rtp_timestamp
) {
84 expected_rtp_timestamp_
= rtp_timestamp
;
87 RtpPacketizerConfig config_
;
88 uint32 sequence_number_
;
90 size_t number_of_packets_
;
91 size_t expected_number_of_packets_
;
92 // Assuming packets arrive in sequence.
93 int expected_packet_id_
;
94 uint32 expected_frame_id_
;
95 uint32 expected_rtp_timestamp_
;
97 DISALLOW_COPY_AND_ASSIGN(TestRtpPacketTransport
);
100 class RtpPacketizerTest
: public ::testing::Test
{
103 : task_runner_(new test::FakeSingleThreadTaskRunner(&testing_clock_
)) {
104 config_
.sequence_number
= kSeqNum
;
105 config_
.ssrc
= kSsrc
;
106 config_
.payload_type
= kPayload
;
107 config_
.max_payload_length
= kMaxPacketLength
;
108 transport_
.reset(new TestRtpPacketTransport(config_
));
109 pacer_
.reset(new PacedSender(kTargetBurstSize
,
115 pacer_
->RegisterVideoSsrc(config_
.ssrc
);
116 rtp_packetizer_
.reset(new RtpPacketizer(
117 pacer_
.get(), &packet_storage_
, config_
));
118 video_frame_
.dependency
= EncodedFrame::DEPENDENT
;
119 video_frame_
.frame_id
= 0;
120 video_frame_
.referenced_frame_id
= kStartFrameId
;
121 video_frame_
.data
.assign(kFrameSize
, 123);
122 video_frame_
.rtp_timestamp
= 0x0055aa11;
125 void RunTasks(int during_ms
) {
126 for (int i
= 0; i
< during_ms
; ++i
) {
127 // Call process the timers every 1 ms.
128 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(1));
129 task_runner_
->RunTasks();
133 base::SimpleTestTickClock testing_clock_
;
134 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
135 EncodedFrame video_frame_
;
136 PacketStorage packet_storage_
;
137 RtpPacketizerConfig config_
;
138 scoped_ptr
<TestRtpPacketTransport
> transport_
;
139 LoggingImpl logging_
;
140 scoped_ptr
<PacedSender
> pacer_
;
141 scoped_ptr
<RtpPacketizer
> rtp_packetizer_
;
143 DISALLOW_COPY_AND_ASSIGN(RtpPacketizerTest
);
146 TEST_F(RtpPacketizerTest
, SendStandardPackets
) {
147 size_t expected_num_of_packets
= kFrameSize
/ kMaxPacketLength
+ 1;
148 transport_
->set_expected_number_of_packets(expected_num_of_packets
);
149 transport_
->set_rtp_timestamp(video_frame_
.rtp_timestamp
);
151 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kTimestampMs
));
152 video_frame_
.reference_time
= testing_clock_
.NowTicks();
153 rtp_packetizer_
->SendFrameAsPackets(video_frame_
);
155 EXPECT_EQ(expected_num_of_packets
, transport_
->number_of_packets_received());
158 TEST_F(RtpPacketizerTest
, Stats
) {
159 EXPECT_FALSE(rtp_packetizer_
->send_packet_count());
160 EXPECT_FALSE(rtp_packetizer_
->send_octet_count());
161 // Insert packets at varying lengths.
162 size_t expected_num_of_packets
= kFrameSize
/ kMaxPacketLength
+ 1;
163 transport_
->set_expected_number_of_packets(expected_num_of_packets
);
164 transport_
->set_rtp_timestamp(video_frame_
.rtp_timestamp
);
166 testing_clock_
.Advance(base::TimeDelta::FromMilliseconds(kTimestampMs
));
167 video_frame_
.reference_time
= testing_clock_
.NowTicks();
168 rtp_packetizer_
->SendFrameAsPackets(video_frame_
);
170 EXPECT_EQ(expected_num_of_packets
, rtp_packetizer_
->send_packet_count());
171 EXPECT_EQ(kFrameSize
, rtp_packetizer_
->send_octet_count());
172 EXPECT_EQ(expected_num_of_packets
, transport_
->number_of_packets_received());