1 // Copyright 2013 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 <gtest/gtest.h>
7 #include "base/memory/scoped_ptr.h"
8 #include "media/cast/rtp_common/rtp_defines.h"
9 #include "media/cast/rtp_receiver/rtp_parser/rtp_parser.h"
10 #include "media/cast/rtp_receiver/rtp_parser/test/rtp_packet_builder.h"
11 #include "media/cast/rtp_receiver/rtp_receiver.h"
16 static const size_t kPacketLength
= 1500;
17 static const int kTestPayloadType
= 127;
18 static const uint32 kTestSsrc
= 1234;
19 static const uint32 kTestTimestamp
= 111111;
20 static const uint16 kTestSeqNum
= 4321;
21 static const uint8 kRefFrameId
= 17;
23 class RtpDataTest
: public RtpData
{
26 expected_header_
.reset(new RtpCastHeader());
29 virtual ~RtpDataTest() {}
31 void SetExpectedHeader(const RtpCastHeader
& cast_header
) {
32 memcpy(expected_header_
.get(), &cast_header
, sizeof(RtpCastHeader
));
35 virtual void OnReceivedPayloadData(const uint8
* payloadData
,
37 const RtpCastHeader
* rtpHeader
) OVERRIDE
{
38 VerifyCommonHeader(*rtpHeader
);
39 VerifyCastHeader(*rtpHeader
);
42 void VerifyCommonHeader(const RtpCastHeader
& parsed_header
) {
43 EXPECT_EQ(expected_header_
->packet_id
== expected_header_
->max_packet_id
,
44 parsed_header
.webrtc
.header
.markerBit
);
45 EXPECT_EQ(kTestPayloadType
, parsed_header
.webrtc
.header
.payloadType
);
46 EXPECT_EQ(kTestSsrc
, parsed_header
.webrtc
.header
.ssrc
);
47 EXPECT_EQ(0, parsed_header
.webrtc
.header
.numCSRCs
);
50 void VerifyCastHeader(const RtpCastHeader
& parsed_header
) {
51 EXPECT_EQ(expected_header_
->is_key_frame
, parsed_header
.is_key_frame
);
52 EXPECT_EQ(expected_header_
->frame_id
, parsed_header
.frame_id
);
53 EXPECT_EQ(expected_header_
->packet_id
, parsed_header
.packet_id
);
54 EXPECT_EQ(expected_header_
->max_packet_id
, parsed_header
.max_packet_id
);
55 EXPECT_EQ(expected_header_
->is_reference
, parsed_header
.is_reference
);
59 scoped_ptr
<RtpCastHeader
> expected_header_
;
62 class RtpParserTest
: public ::testing::Test
{
66 rtp_data_
.reset(new RtpDataTest());
67 rtp_parser_
.reset(new RtpParser(rtp_data_
.get(), config_
));
70 virtual ~RtpParserTest() {}
72 virtual void SetUp() {
73 cast_header_
.is_reference
= true;
74 cast_header_
.reference_frame_id
= kRefFrameId
;
75 packet_builder_
.SetSsrc(kTestSsrc
);
76 packet_builder_
.SetReferenceFrameId(kRefFrameId
, true);
77 packet_builder_
.SetSequenceNumber(kTestSeqNum
);
78 packet_builder_
.SetTimestamp(kTestTimestamp
);
79 packet_builder_
.SetPayloadType(kTestPayloadType
);
80 packet_builder_
.SetMarkerBit(true); // Only one packet.
83 void PopulateConfig() {
84 config_
.payload_type
= kTestPayloadType
;
85 config_
.ssrc
= kTestSsrc
;
88 scoped_ptr
<RtpDataTest
> rtp_data_
;
89 RtpPacketBuilder packet_builder_
;
90 scoped_ptr
<RtpParser
> rtp_parser_
;
91 RtpParserConfig config_
;
92 RtpCastHeader cast_header_
;
95 TEST_F(RtpParserTest
, ParseDefaultCastPacket
) {
96 // Build generic data packet.
97 uint8 packet
[kPacketLength
];
98 packet_builder_
.BuildHeader(packet
, kPacketLength
);
99 // Parse packet as is.
100 RtpCastHeader rtp_header
;
101 rtp_data_
->SetExpectedHeader(cast_header_
);
102 EXPECT_TRUE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
105 TEST_F(RtpParserTest
, ParseNonDefaultCastPacket
) {
106 // Build generic data packet.
107 uint8 packet
[kPacketLength
];
108 packet_builder_
.SetKeyFrame(true);
109 packet_builder_
.SetFrameId(10);
110 packet_builder_
.SetPacketId(5);
111 packet_builder_
.SetMaxPacketId(15);
112 packet_builder_
.SetMarkerBit(false);
113 packet_builder_
.BuildHeader(packet
, kPacketLength
);
114 cast_header_
.is_key_frame
= true;
115 cast_header_
.frame_id
= 10;
116 cast_header_
.packet_id
= 5;
117 cast_header_
.max_packet_id
= 15;
118 rtp_data_
->SetExpectedHeader(cast_header_
);
119 // Parse packet as is.
120 RtpCastHeader rtp_header
;
121 EXPECT_TRUE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
124 TEST_F(RtpParserTest
, TooBigPacketId
) {
125 // Build generic data packet.
126 uint8 packet
[kPacketLength
];
127 packet_builder_
.SetKeyFrame(true);
128 packet_builder_
.SetFrameId(10);
129 packet_builder_
.SetPacketId(15);
130 packet_builder_
.SetMaxPacketId(5);
131 packet_builder_
.BuildHeader(packet
, kPacketLength
);
132 // Parse packet as is.
133 RtpCastHeader rtp_header
;
134 EXPECT_FALSE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
137 TEST_F(RtpParserTest
, MaxPacketId
) {
138 // Build generic data packet.
139 uint8 packet
[kPacketLength
];
140 packet_builder_
.SetKeyFrame(true);
141 packet_builder_
.SetFrameId(10);
142 packet_builder_
.SetPacketId(65535);
143 packet_builder_
.SetMaxPacketId(65535);
144 packet_builder_
.BuildHeader(packet
, kPacketLength
);
145 cast_header_
.is_key_frame
= true;
146 cast_header_
.frame_id
= 10;
147 cast_header_
.packet_id
= 65535;
148 cast_header_
.max_packet_id
= 65535;
149 rtp_data_
->SetExpectedHeader(cast_header_
);
150 // Parse packet as is.
151 RtpCastHeader rtp_header
;
152 EXPECT_TRUE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
155 TEST_F(RtpParserTest
, InvalidPayloadType
) {
156 // Build generic data packet.
157 uint8 packet
[kPacketLength
];
158 packet_builder_
.SetKeyFrame(true);
159 packet_builder_
.SetFrameId(10);
160 packet_builder_
.SetPacketId(65535);
161 packet_builder_
.SetMaxPacketId(65535);
162 packet_builder_
.SetPayloadType(kTestPayloadType
- 1);
163 packet_builder_
.BuildHeader(packet
, kPacketLength
);
164 // Parse packet as is.
165 RtpCastHeader rtp_header
;
166 EXPECT_FALSE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
169 TEST_F(RtpParserTest
, InvalidSsrc
) {
170 // Build generic data packet.
171 uint8 packet
[kPacketLength
];
172 packet_builder_
.SetKeyFrame(true);
173 packet_builder_
.SetFrameId(10);
174 packet_builder_
.SetPacketId(65535);
175 packet_builder_
.SetMaxPacketId(65535);
176 packet_builder_
.SetSsrc(kTestSsrc
- 1);
177 packet_builder_
.BuildHeader(packet
, kPacketLength
);
178 // Parse packet as is.
179 RtpCastHeader rtp_header
;
180 EXPECT_FALSE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));
183 TEST_F(RtpParserTest
, ParseCastPacketWithoutReference
) {
184 cast_header_
.is_reference
= false;
185 cast_header_
.reference_frame_id
= 0;
186 packet_builder_
.SetReferenceFrameId(kRefFrameId
, false);
188 // Build generic data packet.
189 uint8 packet
[kPacketLength
];
190 packet_builder_
.BuildHeader(packet
, kPacketLength
);
191 // Parse packet as is.
192 RtpCastHeader rtp_header
;
193 rtp_data_
->SetExpectedHeader(cast_header_
);
194 EXPECT_TRUE(rtp_parser_
->ParsePacket(packet
, kPacketLength
, &rtp_header
));