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