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/scoped_ptr.h"
6 #include "base/rand_util.h"
7 #include "media/cast/net/rtp/rtp_packet_builder.h"
8 #include "media/cast/net/rtp/rtp_parser.h"
9 #include "media/cast/net/rtp/rtp_receiver_defines.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 static const size_t kPacketLength
= 1500;
16 static const int kTestPayloadType
= 127;
17 static const uint32 kTestSsrc
= 1234;
18 static const uint32 kTestTimestamp
= 111111;
19 static const uint16 kTestSeqNum
= 4321;
20 static const uint8 kRefFrameId
= 17;
22 class RtpParserTest
: public ::testing::Test
{
24 RtpParserTest() : rtp_parser_(kTestSsrc
, kTestPayloadType
) {
25 packet_builder_
.SetSsrc(kTestSsrc
);
26 packet_builder_
.SetSequenceNumber(kTestSeqNum
);
27 packet_builder_
.SetTimestamp(kTestTimestamp
);
28 packet_builder_
.SetPayloadType(kTestPayloadType
);
29 packet_builder_
.SetMarkerBit(true); // Only one packet.
30 cast_header_
.sender_ssrc
= kTestSsrc
;
31 cast_header_
.sequence_number
= kTestSeqNum
;
32 cast_header_
.rtp_timestamp
= kTestTimestamp
;
33 cast_header_
.payload_type
= kTestPayloadType
;
34 cast_header_
.marker
= true;
37 ~RtpParserTest() override
{}
39 void ExpectParsesPacket() {
40 RtpCastHeader parsed_header
;
41 const uint8
* payload
= NULL
;
42 size_t payload_size
= static_cast<size_t>(-1);
43 EXPECT_TRUE(rtp_parser_
.ParsePacket(
44 packet_
, kPacketLength
, &parsed_header
, &payload
, &payload_size
));
46 EXPECT_EQ(cast_header_
.marker
, parsed_header
.marker
);
47 EXPECT_EQ(cast_header_
.payload_type
, parsed_header
.payload_type
);
48 EXPECT_EQ(cast_header_
.sequence_number
, parsed_header
.sequence_number
);
49 EXPECT_EQ(cast_header_
.rtp_timestamp
, parsed_header
.rtp_timestamp
);
50 EXPECT_EQ(cast_header_
.sender_ssrc
, parsed_header
.sender_ssrc
);
52 EXPECT_EQ(cast_header_
.is_key_frame
, parsed_header
.is_key_frame
);
53 EXPECT_EQ(cast_header_
.frame_id
, parsed_header
.frame_id
);
54 EXPECT_EQ(cast_header_
.packet_id
, parsed_header
.packet_id
);
55 EXPECT_EQ(cast_header_
.max_packet_id
, parsed_header
.max_packet_id
);
56 EXPECT_EQ(cast_header_
.reference_frame_id
,
57 parsed_header
.reference_frame_id
);
60 EXPECT_NE(static_cast<size_t>(-1), payload_size
);
63 void ExpectDoesNotParsePacket() {
64 RtpCastHeader parsed_header
;
65 const uint8
* payload
= NULL
;
66 size_t payload_size
= static_cast<size_t>(-1);
67 EXPECT_FALSE(rtp_parser_
.ParsePacket(
68 packet_
, kPacketLength
, &parsed_header
, &payload
, &payload_size
));
71 RtpPacketBuilder packet_builder_
;
72 uint8 packet_
[kPacketLength
];
73 RtpParser rtp_parser_
;
74 RtpCastHeader cast_header_
;
77 TEST_F(RtpParserTest
, ParseDefaultCastPacket
) {
78 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
82 TEST_F(RtpParserTest
, ParseNonDefaultCastPacket
) {
83 packet_builder_
.SetKeyFrame(true);
84 packet_builder_
.SetFrameIds(10, 10);
85 packet_builder_
.SetPacketId(5);
86 packet_builder_
.SetMaxPacketId(15);
87 packet_builder_
.SetMarkerBit(false);
88 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
89 cast_header_
.is_key_frame
= true;
90 cast_header_
.frame_id
= 10;
91 cast_header_
.reference_frame_id
= 10;
92 cast_header_
.packet_id
= 5;
93 cast_header_
.max_packet_id
= 15;
94 cast_header_
.marker
= false;
98 TEST_F(RtpParserTest
, TooBigPacketId
) {
99 packet_builder_
.SetKeyFrame(true);
100 packet_builder_
.SetFrameIds(10, 10);
101 packet_builder_
.SetPacketId(15);
102 packet_builder_
.SetMaxPacketId(5);
103 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
104 cast_header_
.is_key_frame
= true;
105 cast_header_
.frame_id
= 10;
106 cast_header_
.reference_frame_id
= 10;
107 cast_header_
.packet_id
= 15;
108 cast_header_
.max_packet_id
= 5;
109 ExpectDoesNotParsePacket();
112 TEST_F(RtpParserTest
, MaxPacketId
) {
113 packet_builder_
.SetKeyFrame(true);
114 packet_builder_
.SetFrameIds(10, 10);
115 packet_builder_
.SetPacketId(65535);
116 packet_builder_
.SetMaxPacketId(65535);
117 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
118 cast_header_
.is_key_frame
= true;
119 cast_header_
.frame_id
= 10;
120 cast_header_
.reference_frame_id
= 10;
121 cast_header_
.packet_id
= 65535;
122 cast_header_
.max_packet_id
= 65535;
123 ExpectParsesPacket();
126 TEST_F(RtpParserTest
, InvalidPayloadType
) {
127 packet_builder_
.SetKeyFrame(true);
128 packet_builder_
.SetFrameIds(10, 10);
129 packet_builder_
.SetPacketId(65535);
130 packet_builder_
.SetMaxPacketId(65535);
131 packet_builder_
.SetPayloadType(kTestPayloadType
- 1);
132 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
133 cast_header_
.is_key_frame
= true;
134 cast_header_
.frame_id
= 10;
135 cast_header_
.reference_frame_id
= 10;
136 cast_header_
.packet_id
= 65535;
137 cast_header_
.max_packet_id
= 65535;
138 cast_header_
.payload_type
= kTestPayloadType
- 1;
139 ExpectDoesNotParsePacket();
142 TEST_F(RtpParserTest
, InvalidSsrc
) {
143 packet_builder_
.SetKeyFrame(true);
144 packet_builder_
.SetFrameIds(10, 10);
145 packet_builder_
.SetPacketId(65535);
146 packet_builder_
.SetMaxPacketId(65535);
147 packet_builder_
.SetSsrc(kTestSsrc
- 1);
148 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
149 cast_header_
.is_key_frame
= true;
150 cast_header_
.frame_id
= 10;
151 cast_header_
.reference_frame_id
= 10;
152 cast_header_
.packet_id
= 65535;
153 cast_header_
.max_packet_id
= 65535;
154 cast_header_
.sender_ssrc
= kTestSsrc
- 1;
155 ExpectDoesNotParsePacket();
158 TEST_F(RtpParserTest
, ParseCastPacketWithSpecificFrameReference
) {
159 packet_builder_
.SetFrameIds(kRefFrameId
+ 3, kRefFrameId
);
160 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
161 cast_header_
.frame_id
= kRefFrameId
+ 3;
162 cast_header_
.reference_frame_id
= kRefFrameId
;
163 ExpectParsesPacket();
166 TEST_F(RtpParserTest
, ParseExpandingFrameIdTo32Bits
) {
167 const uint32 kMaxFrameId
= 1000;
168 packet_builder_
.SetKeyFrame(true);
169 cast_header_
.is_key_frame
= true;
170 for (uint32 frame_id
= 0; frame_id
<= kMaxFrameId
; ++frame_id
) {
171 packet_builder_
.SetFrameIds(frame_id
, frame_id
);
172 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
173 cast_header_
.frame_id
= frame_id
;
174 cast_header_
.reference_frame_id
= frame_id
;
175 ExpectParsesPacket();
179 TEST_F(RtpParserTest
, ParseExpandingReferenceFrameIdTo32Bits
) {
180 const uint32 kMaxFrameId
= 1000;
181 const uint32 kMaxBackReferenceOffset
= 10;
182 packet_builder_
.SetKeyFrame(false);
183 cast_header_
.is_key_frame
= false;
184 for (uint32 frame_id
= kMaxBackReferenceOffset
;
185 frame_id
<= kMaxFrameId
; ++frame_id
) {
186 const uint32 reference_frame_id
=
187 frame_id
- base::RandInt(1, kMaxBackReferenceOffset
);
188 packet_builder_
.SetFrameIds(frame_id
, reference_frame_id
);
189 packet_builder_
.BuildHeader(packet_
, kPacketLength
);
190 cast_header_
.frame_id
= frame_id
;
191 cast_header_
.reference_frame_id
= reference_frame_id
;
192 ExpectParsesPacket();