blob: 4deda7d7fbfe00af97d4ea0901db92d5871d2cd3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028#include "talk/media/base/fakertp.h"
29#include "talk/media/base/rtputils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080030#include "webrtc/base/asyncpacketsocket.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000031#include "webrtc/base/gunit.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
33namespace cricket {
34
Sergey Ulanovdc305db2016-01-14 17:14:54 -080035static const uint8_t kRtpPacketWithMarker[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
37};
38// 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD)
39// Extension (0xBEDE, 0x1122334455667788)
Sergey Ulanovdc305db2016-01-14 17:14:54 -080040static const uint8_t kRtpPacketWithMarkerAndCsrcAndExtension[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
42 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
43 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
44};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080045static const uint8_t kInvalidPacket[] = { 0x80, 0x00 };
46static const uint8_t kInvalidPacketWithCsrc[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
48 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC
49};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080050static const uint8_t kInvalidPacketWithCsrcAndExtension1[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
52 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
53 0xBE, 0xDE, 0x00
54};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080055static const uint8_t kInvalidPacketWithCsrcAndExtension2[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
57 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
58 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
59};
60
61// PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111
62// No FCI information is needed for PLI.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080063static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11
65};
66
67// Packet has only mandatory fixed RTCP header
68// PT = 204, SSRC = 0x1111
Sergey Ulanovdc305db2016-01-14 17:14:54 -080069static const uint8_t kNonCompoundRtcpAppPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 0x81, 0xCC, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11
71};
72
73// PT = 202, Source count = 0
Sergey Ulanovdc305db2016-01-14 17:14:54 -080074static const uint8_t kNonCompoundRtcpSDESPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 0x80, 0xCA, 0x00, 0x00
76};
77
Sergey Ulanovdc305db2016-01-14 17:14:54 -080078static uint8_t kFakeTag[4] = { 0xba, 0xdd, 0xba, 0xdd };
79static uint8_t kTestKey[] = "12345678901234567890";
80static uint8_t kTestAstValue[3] = { 0xaa, 0xbb, 0xcc };
81
82// Valid rtp Message with 2 byte header extension.
83static uint8_t kRtpMsgWith2ByteExtnHeader[] = {
84 0x90, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00,
86 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
87 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension
88 0x01, 0x00, 0x00, 0x00
89};
90
91// RTP packet with single byte extension header of length 4 bytes.
92// Extension id = 3 and length = 3
93static uint8_t kRtpMsgWithAbsSendTimeExtension[] = {
94 0x90, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00,
97 0xBE, 0xDE, 0x00, 0x02,
98 0x22, 0x00, 0x02, 0x1c,
99 0x32, 0xaa, 0xbb, 0xcc,
100};
101
102// Index of AbsSendTimeExtn data in message |kRtpMsgWithAbsSendTimeExtension|.
103static const int kAstIndexInRtpMsg = 21;
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105TEST(RtpUtilsTest, GetRtp) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000106 EXPECT_TRUE(IsRtpPacket(kPcmuFrame, sizeof(kPcmuFrame)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 int pt;
109 EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt));
110 EXPECT_EQ(0, pt);
111 EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker,
112 sizeof(kRtpPacketWithMarker), &pt));
113 EXPECT_EQ(0, pt);
114
115 int seq_num;
116 EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num));
117 EXPECT_EQ(1, seq_num);
118
Peter Boström0c4e06b2015-10-07 12:23:21 +0200119 uint32_t ts;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts));
121 EXPECT_EQ(0u, ts);
122
Peter Boström0c4e06b2015-10-07 12:23:21 +0200123 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc));
125 EXPECT_EQ(1u, ssrc);
126
127 RtpHeader header;
128 EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header));
129 EXPECT_EQ(0, header.payload_type);
130 EXPECT_EQ(1, header.seq_num);
131 EXPECT_EQ(0u, header.timestamp);
132 EXPECT_EQ(1u, header.ssrc);
133
134 EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
135 EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num));
136 EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts));
137 EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc));
138}
139
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000140TEST(RtpUtilsTest, SetRtpHeader) {
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800141 uint8_t packet[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
143 };
144
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000145 RtpHeader header = { 9, 1111, 2222u, 3333u };
146 EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
148 // Bits: 10 0 0 0000
149 EXPECT_EQ(128u, packet[0]);
150 size_t len;
151 EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len));
152 EXPECT_EQ(12U, len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header));
154 EXPECT_EQ(9, header.payload_type);
155 EXPECT_EQ(1111, header.seq_num);
156 EXPECT_EQ(2222u, header.timestamp);
157 EXPECT_EQ(3333u, header.ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158}
159
160TEST(RtpUtilsTest, GetRtpHeaderLen) {
161 size_t len;
162 EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len));
163 EXPECT_EQ(12U, len);
164
165 EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension,
166 sizeof(kRtpPacketWithMarkerAndCsrcAndExtension),
167 &len));
168 EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len);
169
170 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len));
171 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc,
172 sizeof(kInvalidPacketWithCsrc), &len));
173 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1,
174 sizeof(kInvalidPacketWithCsrcAndExtension1),
175 &len));
176 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2,
177 sizeof(kInvalidPacketWithCsrcAndExtension2),
178 &len));
179}
180
181TEST(RtpUtilsTest, GetRtcp) {
182 int pt;
183 EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
184 EXPECT_EQ(0xc9, pt);
185
186 EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
187
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
190 sizeof(kNonCompoundRtcpPliFeedbackPacket),
191 &ssrc));
192 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
193 sizeof(kNonCompoundRtcpAppPacket),
194 &ssrc));
195 EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
196 sizeof(kNonCompoundRtcpSDESPacket),
197 &ssrc));
198}
199
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800200// Invalid RTP packets.
201TEST(RtpUtilsTest, InvalidRtpHeader) {
202 // Rtp message with invalid length.
203 const uint8_t kRtpMsgWithInvalidLength[] = {
204 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
206 0xDD, 0xCC, 0xBB, 0xAA, // Only 1 CSRC, but CC count is 4.
207 };
208 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength,
209 sizeof(kRtpMsgWithInvalidLength), nullptr));
210
211 // Rtp message with single byte header extension, invalid extension length.
212 const uint8_t kRtpMsgWithInvalidExtnLength[] = {
213 0x90, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00,
215 0x00, 0x00, 0x00, 0x00,
216 0xBE, 0xDE, 0x0A, 0x00, // Extn length - 0x0A00
217 };
218 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength,
219 sizeof(kRtpMsgWithInvalidExtnLength),
220 nullptr));
221}
222
223// Valid RTP packet with a 2byte header extension.
224TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) {
225 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader,
226 sizeof(kRtpMsgWith2ByteExtnHeader), nullptr));
227}
228
229// Valid RTP packet which has 1 byte header AbsSendTime extension in it.
230TEST(RtpUtilsTest, ValidRtpPacketWithAbsSendTimeExtension) {
231 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithAbsSendTimeExtension,
232 sizeof(kRtpMsgWithAbsSendTimeExtension),
233 nullptr));
234}
235
236// Verify handling of a 2 byte extension header RTP messsage. Currently these
237// messages are not supported.
238TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionIn2ByteHeaderExtn) {
239 std::vector<uint8_t> data(
240 kRtpMsgWith2ByteExtnHeader,
241 kRtpMsgWith2ByteExtnHeader + sizeof(kRtpMsgWith2ByteExtnHeader));
242 EXPECT_FALSE(UpdateRtpAbsSendTimeExtension(&data[0], data.size(), 3, 0));
243}
244
245// Verify finding an extension ID in the TURN send indication message.
246TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) {
247 // A valid STUN indication message with a valid RTP header in data attribute
248 // payload field and no extension bit set.
249 uint8_t message_without_extension[] = {
250 0x00, 0x16, 0x00, 0x18, // length of
251 0x21, 0x12, 0xA4, 0x42, // magic cookie
252 '0', '1', '2', '3', // transaction id
253 '4', '5', '6', '7',
254 '8', '9', 'a', 'b',
255 0x00, 0x20, 0x00, 0x04, // Mapped address.
256 0x00, 0x00, 0x00, 0x00,
257 0x00, 0x13, 0x00, 0x0C, // Data attribute.
258 0x80, 0x00, 0x00, 0x00, // RTP packet.
259 0x00, 0x00, 0x00, 0x00,
260 0x00, 0x00, 0x00, 0x00,
261 };
262 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(
263 message_without_extension, sizeof(message_without_extension), 3, 0));
264
265 // A valid STUN indication message with a valid RTP header and a extension
266 // header.
267 uint8_t message[] = {
268 0x00, 0x16, 0x00, 0x24, // length of
269 0x21, 0x12, 0xA4, 0x42, // magic cookie
270 '0', '1', '2', '3', // transaction id
271 '4', '5', '6', '7',
272 '8', '9', 'a', 'b',
273 0x00, 0x20, 0x00, 0x04, // Mapped address.
274 0x00, 0x00, 0x00, 0x00,
275 0x00, 0x13, 0x00, 0x18, // Data attribute.
276 0x90, 0x00, 0x00, 0x00, // RTP packet.
277 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE,
278 0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc,
279 };
280 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, sizeof(message), 3, 0));
281}
282
283// Test without any packet options variables set. This method should return
284// without HMAC value in the packet.
285TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) {
286 rtc::PacketTimeUpdateParams packet_time_params;
287 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
288 kRtpMsgWithAbsSendTimeExtension +
289 sizeof(kRtpMsgWithAbsSendTimeExtension));
290 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
291 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
292 packet_time_params, 0));
293
294 // Making sure HMAC wasn't updated..
295 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
296 kFakeTag, 4));
297
298 // Verify AbsouluteSendTime extension field wasn't modified.
299 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
300 sizeof(kTestAstValue)));
301}
302
303// Veirfy HMAC is updated when packet option parameters are set.
304TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) {
305 rtc::PacketTimeUpdateParams packet_time_params;
306 packet_time_params.srtp_auth_key.assign(kTestKey,
307 kTestKey + sizeof(kTestKey));
308 packet_time_params.srtp_auth_tag_len = 4;
309
310 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
311 kRtpMsgWithAbsSendTimeExtension +
312 sizeof(kRtpMsgWithAbsSendTimeExtension));
313 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
314 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
315 packet_time_params, 0));
316
317 uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0};
318 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
319 kExpectedTag, sizeof(kExpectedTag)));
320
321 // Verify AbsouluteSendTime extension field is not modified.
322 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
323 sizeof(kTestAstValue)));
324}
325
326// Verify finding an extension ID in a raw rtp message.
327TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInRtpPacket) {
328 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
329 kRtpMsgWithAbsSendTimeExtension +
330 sizeof(kRtpMsgWithAbsSendTimeExtension));
331
332 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(),
333 3, 51183266));
334
335 // Verify that the timestamp was updated.
336 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
337 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
338 sizeof(kExpectedTimestamp)));
339}
340
341// Verify we update both AbsSendTime extension header and HMAC.
342TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) {
343 rtc::PacketTimeUpdateParams packet_time_params;
344 packet_time_params.srtp_auth_key.assign(kTestKey,
345 kTestKey + sizeof(kTestKey));
346 packet_time_params.srtp_auth_tag_len = 4;
347 packet_time_params.rtp_sendtime_extension_id = 3;
348 // 3 is also present in the test message.
349
350 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
351 kRtpMsgWithAbsSendTimeExtension +
352 sizeof(kRtpMsgWithAbsSendTimeExtension));
353 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
354 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
355 packet_time_params, 51183266));
356
357 const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e};
358 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
359 kExpectedTag, sizeof(kExpectedTag)));
360
361 // Verify that the timestamp was updated.
362 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
363 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
364 sizeof(kExpectedTimestamp)));
365}
366
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368} // namespace cricket