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 kTestSsrc = 1; 40 41 // Test that we read the correct header fields from the RTP/RTCP packet. 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 type; 48 int seq_num; 49 uint32 ts; 50 uint32 ssrc; 51 EXPECT_FALSE(rtp_packet.is_rtcp()); 52 EXPECT_TRUE(rtp_packet.IsValidRtpPacket()); 53 EXPECT_FALSE(rtp_packet.IsValidRtcpPacket()); 54 EXPECT_TRUE(rtp_packet.GetRtpPayloadType(&type)); 55 EXPECT_EQ(0, type); 56 EXPECT_TRUE(rtp_packet.GetRtpSeqNum(&seq_num)); 57 EXPECT_EQ(0, seq_num); 58 EXPECT_TRUE(rtp_packet.GetRtpTimestamp(&ts)); 59 EXPECT_EQ(0U, ts); 60 EXPECT_TRUE(rtp_packet.GetRtpSsrc(&ssrc)); 61 EXPECT_EQ(kTestSsrc, ssrc); 62 EXPECT_FALSE(rtp_packet.GetRtcpType(&type)); 63 64 rtc::ByteBuffer rtcp_buf; 65 RtpTestUtility::kTestRawRtcpPackets[0].WriteToByteBuffer(&rtcp_buf); 66 RtpDumpPacket rtcp_packet(rtcp_buf.Data(), rtcp_buf.Length(), 0, true); 67 68 EXPECT_TRUE(rtcp_packet.is_rtcp()); 69 EXPECT_FALSE(rtcp_packet.IsValidRtpPacket()); 70 EXPECT_TRUE(rtcp_packet.IsValidRtcpPacket()); 71 EXPECT_TRUE(rtcp_packet.GetRtcpType(&type)); 72 EXPECT_EQ(0, type); 73 } 74 75 // Test that we read only the RTP dump file. 76 TEST(RtpDumpTest, ReadRtpDumpFile) { 77 RtpDumpPacket packet; 78 rtc::MemoryStream stream; 79 RtpDumpWriter writer(&stream); 80 rtc::scoped_ptr<RtpDumpReader> reader; 81 82 // Write a RTP packet to the stream, which is a valid RTP dump. Next, we will 83 // change the first line to make the RTP dump valid or invalid. 84 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, false, kTestSsrc, &writer)); 85 stream.Rewind(); 86 reader.reset(new RtpDumpReader(&stream)); 87 EXPECT_EQ(rtc::SR_SUCCESS, reader->ReadPacket(&packet)); 88 89 // The first line is correct. 90 stream.Rewind(); 91 const char new_line[] = "#!rtpplay1.0 1.1.1.1/1\n"; 92 EXPECT_EQ(rtc::SR_SUCCESS, 93 stream.WriteAll(new_line, strlen(new_line), NULL, NULL)); 94 stream.Rewind(); 95 reader.reset(new RtpDumpReader(&stream)); 96 EXPECT_EQ(rtc::SR_SUCCESS, reader->ReadPacket(&packet)); 97 98 // The first line is not correct: not started with #!rtpplay1.0. 99 stream.Rewind(); 100 const char new_line2[] = "#!rtpplaz1.0 0.0.0.0/0\n"; 101 EXPECT_EQ(rtc::SR_SUCCESS, 102 stream.WriteAll(new_line2, strlen(new_line2), NULL, NULL)); 103 stream.Rewind(); 104 reader.reset(new RtpDumpReader(&stream)); 105 EXPECT_EQ(rtc::SR_ERROR, reader->ReadPacket(&packet)); 106 107 // The first line is not correct: no port. 108 stream.Rewind(); 109 const char new_line3[] = "#!rtpplay1.0 0.0.0.0//\n"; 110 EXPECT_EQ(rtc::SR_SUCCESS, 111 stream.WriteAll(new_line3, strlen(new_line3), NULL, NULL)); 112 stream.Rewind(); 113 reader.reset(new RtpDumpReader(&stream)); 114 EXPECT_EQ(rtc::SR_ERROR, reader->ReadPacket(&packet)); 115 } 116 117 // Test that we read the same RTP packets that rtp dump writes. 118 TEST(RtpDumpTest, WriteReadSameRtp) { 119 rtc::MemoryStream stream; 120 RtpDumpWriter writer(&stream); 121 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 122 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer)); 123 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream( 124 RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc)); 125 126 // Check stream has only RtpTestUtility::GetTestPacketCount() packets. 127 RtpDumpPacket packet; 128 RtpDumpReader reader(&stream); 129 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) { 130 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 131 uint32 ssrc; 132 EXPECT_TRUE(GetRtpSsrc(&packet.data[0], packet.data.size(), &ssrc)); 133 EXPECT_EQ(kTestSsrc, ssrc); 134 } 135 // No more packets to read. 136 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 137 138 // Rewind the stream and read again with a specified ssrc. 139 stream.Rewind(); 140 RtpDumpReader reader_w_ssrc(&stream); 141 const uint32 send_ssrc = kTestSsrc + 1; 142 reader_w_ssrc.SetSsrc(send_ssrc); 143 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) { 144 EXPECT_EQ(rtc::SR_SUCCESS, reader_w_ssrc.ReadPacket(&packet)); 145 EXPECT_FALSE(packet.is_rtcp()); 146 EXPECT_EQ(packet.original_data_len, packet.data.size()); 147 uint32 ssrc; 148 EXPECT_TRUE(GetRtpSsrc(&packet.data[0], packet.data.size(), &ssrc)); 149 EXPECT_EQ(send_ssrc, ssrc); 150 } 151 // No more packets to read. 152 EXPECT_EQ(rtc::SR_EOS, reader_w_ssrc.ReadPacket(&packet)); 153 } 154 155 // Test that we read the same RTCP packets that rtp dump writes. 156 TEST(RtpDumpTest, WriteReadSameRtcp) { 157 rtc::MemoryStream stream; 158 RtpDumpWriter writer(&stream); 159 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 160 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer)); 161 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream( 162 RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc)); 163 164 // Check stream has only RtpTestUtility::GetTestPacketCount() packets. 165 RtpDumpPacket packet; 166 RtpDumpReader reader(&stream); 167 reader.SetSsrc(kTestSsrc + 1); // Does not affect RTCP packet. 168 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) { 169 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 170 EXPECT_TRUE(packet.is_rtcp()); 171 EXPECT_EQ(0U, packet.original_data_len); 172 } 173 // No more packets to read. 174 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 175 } 176 177 // Test dumping only RTP packet headers. 178 TEST(RtpDumpTest, WriteReadRtpHeadersOnly) { 179 rtc::MemoryStream stream; 180 RtpDumpWriter writer(&stream); 181 writer.set_packet_filter(PF_RTPHEADER); 182 183 // Write some RTP and RTCP packets. RTP packets should only have headers; 184 // RTCP packets should be eaten. 185 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 186 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer)); 187 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 188 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer)); 189 stream.Rewind(); 190 191 // Check that only RTP packet headers are present. 192 RtpDumpPacket packet; 193 RtpDumpReader reader(&stream); 194 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) { 195 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 196 EXPECT_FALSE(packet.is_rtcp()); 197 size_t len = 0; 198 packet.GetRtpHeaderLen(&len); 199 EXPECT_EQ(len, packet.data.size()); 200 EXPECT_GT(packet.original_data_len, packet.data.size()); 201 } 202 // No more packets to read. 203 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 204 } 205 206 // Test dumping only RTCP packets. 207 TEST(RtpDumpTest, WriteReadRtcpOnly) { 208 rtc::MemoryStream stream; 209 RtpDumpWriter writer(&stream); 210 writer.set_packet_filter(PF_RTCPPACKET); 211 212 // Write some RTP and RTCP packets. RTP packets should be eaten. 213 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 214 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer)); 215 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 216 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer)); 217 stream.Rewind(); 218 219 // Check that only RTCP packets are present. 220 RtpDumpPacket packet; 221 RtpDumpReader reader(&stream); 222 for (size_t i = 0; i < RtpTestUtility::GetTestPacketCount(); ++i) { 223 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 224 EXPECT_TRUE(packet.is_rtcp()); 225 EXPECT_EQ(0U, packet.original_data_len); 226 } 227 // No more packets to read. 228 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 229 } 230 231 // Test that RtpDumpLoopReader reads RTP packets continously and the elapsed 232 // time, the sequence number, and timestamp are maintained properly. 233 TEST(RtpDumpTest, LoopReadRtp) { 234 rtc::MemoryStream stream; 235 RtpDumpWriter writer(&stream); 236 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 237 RtpTestUtility::GetTestPacketCount(), false, kTestSsrc, &writer)); 238 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream( 239 3 * RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc)); 240 } 241 242 // Test that RtpDumpLoopReader reads RTCP packets continously and the elapsed 243 // time is maintained properly. 244 TEST(RtpDumpTest, LoopReadRtcp) { 245 rtc::MemoryStream stream; 246 RtpDumpWriter writer(&stream); 247 ASSERT_TRUE(RtpTestUtility::WriteTestPackets( 248 RtpTestUtility::GetTestPacketCount(), true, kTestSsrc, &writer)); 249 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream( 250 3 * RtpTestUtility::GetTestPacketCount(), &stream, kTestSsrc)); 251 } 252 253 // Test that RtpDumpLoopReader reads continously from stream with a single RTP 254 // packets. 255 TEST(RtpDumpTest, LoopReadSingleRtp) { 256 rtc::MemoryStream stream; 257 RtpDumpWriter writer(&stream); 258 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, false, kTestSsrc, &writer)); 259 260 // The regular reader can read only one packet. 261 RtpDumpPacket packet; 262 stream.Rewind(); 263 RtpDumpReader reader(&stream); 264 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 265 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 266 267 // The loop reader reads three packets from the input stream. 268 stream.Rewind(); 269 RtpDumpLoopReader loop_reader(&stream); 270 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 271 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 272 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 273 } 274 275 // Test that RtpDumpLoopReader reads continously from stream with a single RTCP 276 // packets. 277 TEST(RtpDumpTest, LoopReadSingleRtcp) { 278 rtc::MemoryStream stream; 279 RtpDumpWriter writer(&stream); 280 ASSERT_TRUE(RtpTestUtility::WriteTestPackets(1, true, kTestSsrc, &writer)); 281 282 // The regular reader can read only one packet. 283 RtpDumpPacket packet; 284 stream.Rewind(); 285 RtpDumpReader reader(&stream); 286 EXPECT_EQ(rtc::SR_SUCCESS, reader.ReadPacket(&packet)); 287 EXPECT_EQ(rtc::SR_EOS, reader.ReadPacket(&packet)); 288 289 // The loop reader reads three packets from the input stream. 290 stream.Rewind(); 291 RtpDumpLoopReader loop_reader(&stream); 292 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 293 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 294 EXPECT_EQ(rtc::SR_SUCCESS, loop_reader.ReadPacket(&packet)); 295 } 296 297 } // namespace cricket 298