1 /*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <string>
29
30 #include "talk/media/base/rtpdump.h"
31 #include "talk/media/base/rtputils.h"
32 #include "talk/media/base/testutils.h"
33 #include "webrtc/base/bytebuffer.h"
34 #include "webrtc/base/gunit.h"
35 #include "webrtc/base/thread.h"
36
37 namespace cricket {
38
39 static const uint32_t kTestSsrc = 1;
40
41 // Test that we read the correct header fields from the RTP/RTCP packet.
TEST(RtpDumpTest,ReadRtpDumpPacket)42 TEST(RtpDumpTest, ReadRtpDumpPacket) {
43 rtc::ByteBuffer rtp_buf;
44 RtpTestUtility::kTestRawRtpPackets[0].WriteToByteBuffer(kTestSsrc, &rtp_buf);
45 RtpDumpPacket rtp_packet(rtp_buf.Data(), rtp_buf.Length(), 0, false);
46
47 int payload_type;
48 int seq_num;
49 uint32_t ts;
50 uint32_t ssrc;
51 int rtcp_type;
52 EXPECT_FALSE(rtp_packet.is_rtcp());
53 EXPECT_TRUE(rtp_packet.IsValidRtpPacket());
54 EXPECT_FALSE(rtp_packet.IsValidRtcpPacket());
55 EXPECT_TRUE(rtp_packet.GetRtpPayloadType(&payload_type));
56 EXPECT_EQ(0, payload_type);
57 EXPECT_TRUE(rtp_packet.GetRtpSeqNum(&seq_num));
58 EXPECT_EQ(0, seq_num);
59 EXPECT_TRUE(rtp_packet.GetRtpTimestamp(&ts));
60 EXPECT_EQ(0U, ts);
61 EXPECT_TRUE(rtp_packet.GetRtpSsrc(&ssrc));
62 EXPECT_EQ(kTestSsrc, ssrc);
63 EXPECT_FALSE(rtp_packet.GetRtcpType(&rtcp_type));
64
65 rtc::ByteBuffer rtcp_buf;
66 RtpTestUtility::kTestRawRtcpPackets[0].WriteToByteBuffer(&rtcp_buf);
67 RtpDumpPacket rtcp_packet(rtcp_buf.Data(), rtcp_buf.Length(), 0, true);
68
69 EXPECT_TRUE(rtcp_packet.is_rtcp());
70 EXPECT_FALSE(rtcp_packet.IsValidRtpPacket());
71 EXPECT_TRUE(rtcp_packet.IsValidRtcpPacket());
72 EXPECT_TRUE(rtcp_packet.GetRtcpType(&rtcp_type));
73 EXPECT_EQ(0, rtcp_type);
74 }
75
76 // Test that we read only the RTP dump file.
TEST(RtpDumpTest,ReadRtpDumpFile)77 TEST(RtpDumpTest, ReadRtpDumpFile) {
78 RtpDumpPacket packet;
79 rtc::MemoryStream stream;
80 RtpDumpWriter writer(&stream);
81 rtc::scoped_ptr<RtpDumpReader> reader;
82
83 // Write a RTP packet to the stream, which is a valid RTP dump. Next, we will
84 // change the first line to make the RTP dump valid or invalid.
85 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, false, kTestSsrc, &writer));
86 stream.Rewind();
87 reader.reset(new RtpDumpReader(&stream));
88 EXPECT_EQ(rtc::SR_SUCCESS, reader->ReadPacket(&packet));
89
90 // The first line is correct.
91 stream.Rewind();
92 const char new_line[] = "#!rtpplay1.0 1.1.1.1/1\n";
93 EXPECT_EQ(rtc::SR_SUCCESS,
94 stream.WriteAll(new_line, strlen(new_line), NULL, NULL));
95 stream.Rewind();
96 reader.reset(new RtpDumpReader(&stream));
97 EXPECT_EQ(rtc::SR_SUCCESS, reader->ReadPacket(&packet));
98
99 // The first line is not correct: not started with #!rtpplay1.0.
100 stream.Rewind();
101 const char new_line2[] = "#!rtpplaz1.0 0.0.0.0/0\n";
102 EXPECT_EQ(rtc::SR_SUCCESS,
103 stream.WriteAll(new_line2, strlen(new_line2), NULL, NULL));
104 stream.Rewind();
105 reader.reset(new RtpDumpReader(&stream));
106 EXPECT_EQ(rtc::SR_ERROR, reader->ReadPacket(&packet));
107
108 // The first line is not correct: no port.
109 stream.Rewind();
110 const char new_line3[] = "#!rtpplay1.0 0.0.0.0//\n";
111 EXPECT_EQ(rtc::SR_SUCCESS,
112 stream.WriteAll(new_line3, strlen(new_line3), NULL, NULL));
113 stream.Rewind();
114 reader.reset(new RtpDumpReader(&stream));
115 EXPECT_EQ(rtc::SR_ERROR, reader->ReadPacket(&packet));
116 }
117
118 // Test that we read the same RTP packets that rtp dump writes.
TEST(RtpDumpTest,WriteReadSameRtp)119 TEST(RtpDumpTest, WriteReadSameRtp) {
120 rtc::MemoryStream stream;
121 RtpDumpWriter writer(&stream);
122 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
123 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer));
124 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
125 RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc));
126
127 // Check stream has only RtpTestUtility::GetTestPacketCount() packets.
128 RtpDumpPacket packet;
129 RtpDumpReader reader(&stream);
130 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) {
131 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
132 uint32_t ssrc;
133 EXPECT_TRUE(GetRtpSsrc(&packet.data[0], packet.data.size(), &ssrc));
134 EXPECT_EQ(kTestSsrc, ssrc);
135 }
136 // No more packets to read.
137 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
138
139 // Rewind the stream and read again with a specified ssrc.
140 stream.Rewind();
141 RtpDumpReader reader_w_ssrc(&stream);
142 const uint32_t send_ssrc = kTestSsrc + 1;
143 reader_w_ssrc.SetSsrc(send_ssrc);
144 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) {
145 EXPECT_EQ(rtc::SR_SUCCESS, reader_w_ssrc.ReadPacket(&packet));
146 EXPECT_FALSE(packet.is_rtcp());
147 EXPECT_EQ(packet.original_data_len, packet.data.size());
148 uint32_t ssrc;
149 EXPECT_TRUE(GetRtpSsrc(&packet.data[0], packet.data.size(), &ssrc));
150 EXPECT_EQ(send_ssrc, ssrc);
151 }
152 // No more packets to read.
153 EXPECT_EQ(rtc::SR_EOS, reader_w_ssrc.ReadPacket(&packet));
154 }
155
156 // Test that we read the same RTCP packets that rtp dump writes.
TEST(RtpDumpTest,WriteReadSameRtcp)157 TEST(RtpDumpTest, WriteReadSameRtcp) {
158 rtc::MemoryStream stream;
159 RtpDumpWriter writer(&stream);
160 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
161 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer));
162 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
163 RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc));
164
165 // Check stream has only RtpTestUtility::GetTestPacketCount() packets.
166 RtpDumpPacket packet;
167 RtpDumpReader reader(&stream);
168 reader.SetSsrc(kTestSsrc + 1); // Does not affect RTCP packet.
169 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) {
170 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
171 EXPECT_TRUE(packet.is_rtcp());
172 EXPECT_EQ(0U, packet.original_data_len);
173 }
174 // No more packets to read.
175 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
176 }
177
178 // Test dumping only RTP packet headers.
TEST(RtpDumpTest,WriteReadRtpHeadersOnly)179 TEST(RtpDumpTest, WriteReadRtpHeadersOnly) {
180 rtc::MemoryStream stream;
181 RtpDumpWriter writer(&stream);
182 writer.set_packet_filter(PF_RTPHEADER);
183
184 // Write some RTP and RTCP packets. RTP packets should only have headers;
185 // RTCP packets should be eaten.
186 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
187 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer));
188 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
189 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer));
190 stream.Rewind();
191
192 // Check that only RTP packet headers are present.
193 RtpDumpPacket packet;
194 RtpDumpReader reader(&stream);
195 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) {
196 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
197 EXPECT_FALSE(packet.is_rtcp());
198 size_t len = 0;
199 packet.GetRtpHeaderLen(&len);
200 EXPECT_EQ(len, packet.data.size());
201 EXPECT_GT(packet.original_data_len, packet.data.size());
202 }
203 // No more packets to read.
204 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
205 }
206
207 // Test dumping only RTCP packets.
TEST(RtpDumpTest,WriteReadRtcpOnly)208 TEST(RtpDumpTest, WriteReadRtcpOnly) {
209 rtc::MemoryStream stream;
210 RtpDumpWriter writer(&stream);
211 writer.set_packet_filter(PF_RTCPPACKET);
212
213 // Write some RTP and RTCP packets. RTP packets should be eaten.
214 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
215 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer));
216 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
217 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer));
218 stream.Rewind();
219
220 // Check that only RTCP packets are present.
221 RtpDumpPacket packet;
222 RtpDumpReader reader(&stream);
223 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) {
224 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
225 EXPECT_TRUE(packet.is_rtcp());
226 EXPECT_EQ(0U, packet.original_data_len);
227 }
228 // No more packets to read.
229 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
230 }
231
232 // Test that RtpDumpLoopReader reads RTP packets continously and the elapsed
233 // time, the sequence number, and timestamp are maintained properly.
TEST(RtpDumpTest,LoopReadRtp)234 TEST(RtpDumpTest, LoopReadRtp) {
235 rtc::MemoryStream stream;
236 RtpDumpWriter writer(&stream);
237 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
238 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer));
239 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
240 3 * RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc));
241 }
242
243 // Test that RtpDumpLoopReader reads RTCP packets continously and the elapsed
244 // time is maintained properly.
TEST(RtpDumpTest,LoopReadRtcp)245 TEST(RtpDumpTest, LoopReadRtcp) {
246 rtc::MemoryStream stream;
247 RtpDumpWriter writer(&stream);
248 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(
249 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer));
250 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
251 3 * RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc));
252 }
253
254 // Test that RtpDumpLoopReader reads continously from stream with a single RTP
255 // packets.
TEST(RtpDumpTest,LoopReadSingleRtp)256 TEST(RtpDumpTest, LoopReadSingleRtp) {
257 rtc::MemoryStream stream;
258 RtpDumpWriter writer(&stream);
259 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, false, kTestSsrc, &writer));
260
261 // The regular reader can read only one packet.
262 RtpDumpPacket packet;
263 stream.Rewind();
264 RtpDumpReader reader(&stream);
265 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
266 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
267
268 // The loop reader reads three packets from the input stream.
269 stream.Rewind();
270 RtpDumpLoopReader loop_reader(&stream);
271 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
272 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
273 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
274 }
275
276 // Test that RtpDumpLoopReader reads continously from stream with a single RTCP
277 // packets.
TEST(RtpDumpTest,LoopReadSingleRtcp)278 TEST(RtpDumpTest, LoopReadSingleRtcp) {
279 rtc::MemoryStream stream;
280 RtpDumpWriter writer(&stream);
281 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, true, kTestSsrc, &writer));
282
283 // The regular reader can read only one packet.
284 RtpDumpPacket packet;
285 stream.Rewind();
286 RtpDumpReader reader(&stream);
287 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet));
288 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet));
289
290 // The loop reader reads three packets from the input stream.
291 stream.Rewind();
292 RtpDumpLoopReader loop_reader(&stream);
293 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
294 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
295 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet));
296 }
297
298 } // namespace cricket
299