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 "talk/base/gunit.h" 29 #include "talk/media/base/fakertp.h" 30 #include "talk/media/base/rtputils.h" 31 32 namespace cricket { 33 34 static const unsigned char kRtpPacketWithMarker[] = { 35 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 36 }; 37 // 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD) 38 // Extension (0xBEDE, 0x1122334455667788) 39 static const unsigned char kRtpPacketWithMarkerAndCsrcAndExtension[] = { 40 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 41 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, 42 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 43 }; 44 static const unsigned char kInvalidPacket[] = { 0x80, 0x00 }; 45 static const unsigned char kInvalidPacketWithCsrc[] = { 46 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 47 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC 48 }; 49 static const unsigned char kInvalidPacketWithCsrcAndExtension1[] = { 50 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 51 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, 52 0xBE, 0xDE, 0x00 53 }; 54 static const unsigned char kInvalidPacketWithCsrcAndExtension2[] = { 55 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 56 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, 57 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 58 }; 59 60 // PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111 61 // No FCI information is needed for PLI. 62 static const unsigned char kNonCompoundRtcpPliFeedbackPacket[] = { 63 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11 64 }; 65 66 // Packet has only mandatory fixed RTCP header 67 // PT = 204, SSRC = 0x1111 68 static const unsigned char kNonCompoundRtcpAppPacket[] = { 69 0x81, 0xCC, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11 70 }; 71 72 // PT = 202, Source count = 0 73 static const unsigned char kNonCompoundRtcpSDESPacket[] = { 74 0x80, 0xCA, 0x00, 0x00 75 }; 76 77 TEST(RtpUtilsTest, GetRtp) { 78 int ver; 79 EXPECT_TRUE(GetRtpVersion(kPcmuFrame, sizeof(kPcmuFrame), &ver)); 80 EXPECT_EQ(2, ver); 81 82 int pt; 83 EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt)); 84 EXPECT_EQ(0, pt); 85 EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker, 86 sizeof(kRtpPacketWithMarker), &pt)); 87 EXPECT_EQ(0, pt); 88 89 int seq_num; 90 EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num)); 91 EXPECT_EQ(1, seq_num); 92 93 uint32 ts; 94 EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts)); 95 EXPECT_EQ(0u, ts); 96 97 uint32 ssrc; 98 EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc)); 99 EXPECT_EQ(1u, ssrc); 100 101 RtpHeader header; 102 EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header)); 103 EXPECT_EQ(0, header.payload_type); 104 EXPECT_EQ(1, header.seq_num); 105 EXPECT_EQ(0u, header.timestamp); 106 EXPECT_EQ(1u, header.ssrc); 107 108 EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt)); 109 EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num)); 110 EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts)); 111 EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc)); 112 } 113 114 TEST(RtpUtilsTest, SetRtp) { 115 unsigned char packet[] = { 116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 117 }; 118 119 EXPECT_TRUE(SetRtpHeaderFlags(packet, sizeof(packet), false, false, 0)); 120 EXPECT_TRUE(SetRtpPayloadType(packet, sizeof(packet), 9u)); 121 EXPECT_TRUE(SetRtpSeqNum(packet, sizeof(packet), 1111u)); 122 EXPECT_TRUE(SetRtpTimestamp(packet, sizeof(packet), 2222u)); 123 EXPECT_TRUE(SetRtpSsrc(packet, sizeof(packet), 3333u)); 124 125 // Bits: 10 0 0 0000 126 EXPECT_EQ(128u, packet[0]); 127 size_t len; 128 EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len)); 129 EXPECT_EQ(12U, len); 130 RtpHeader header; 131 EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header)); 132 EXPECT_EQ(9, header.payload_type); 133 EXPECT_EQ(1111, header.seq_num); 134 EXPECT_EQ(2222u, header.timestamp); 135 EXPECT_EQ(3333u, header.ssrc); 136 137 unsigned char packet2[] = { 138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 139 }; 140 141 EXPECT_TRUE(SetRtpHeader(packet2, sizeof(packet2), header)); 142 143 // Bits: 10 0 0 0000 144 EXPECT_EQ(128u, packet2[0]); 145 EXPECT_TRUE(GetRtpHeaderLen(packet2, sizeof(packet2), &len)); 146 EXPECT_EQ(12U, len); 147 EXPECT_TRUE(GetRtpHeader(packet2, sizeof(packet2), &header)); 148 EXPECT_EQ(9, header.payload_type); 149 EXPECT_EQ(1111, header.seq_num); 150 EXPECT_EQ(2222u, header.timestamp); 151 EXPECT_EQ(3333u, header.ssrc); 152 } 153 154 TEST(RtpUtilsTest, GetRtpHeaderLen) { 155 size_t len; 156 EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len)); 157 EXPECT_EQ(12U, len); 158 159 EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension, 160 sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), 161 &len)); 162 EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len); 163 164 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len)); 165 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc, 166 sizeof(kInvalidPacketWithCsrc), &len)); 167 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1, 168 sizeof(kInvalidPacketWithCsrcAndExtension1), 169 &len)); 170 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2, 171 sizeof(kInvalidPacketWithCsrcAndExtension2), 172 &len)); 173 } 174 175 TEST(RtpUtilsTest, GetRtcp) { 176 int pt; 177 EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt)); 178 EXPECT_EQ(0xc9, pt); 179 180 EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt)); 181 182 uint32 ssrc; 183 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket, 184 sizeof(kNonCompoundRtcpPliFeedbackPacket), 185 &ssrc)); 186 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket, 187 sizeof(kNonCompoundRtcpAppPacket), 188 &ssrc)); 189 EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket, 190 sizeof(kNonCompoundRtcpSDESPacket), 191 &ssrc)); 192 } 193 194 } // namespace cricket 195