blob: e6597bdcaed15e65bb0ba82880cd77b01ed4bccd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Sergey Ulanovdc305db2016-01-14 17:14:54 -080011#include "webrtc/base/asyncpacketsocket.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000012#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080013#include "webrtc/media/base/fakertp.h"
14#include "webrtc/media/base/rtputils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
16namespace cricket {
17
Sergey Ulanovdc305db2016-01-14 17:14:54 -080018static const uint8_t kRtpPacketWithMarker[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
20};
21// 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD)
22// Extension (0xBEDE, 0x1122334455667788)
Sergey Ulanovdc305db2016-01-14 17:14:54 -080023static const uint8_t kRtpPacketWithMarkerAndCsrcAndExtension[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
25 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
26 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
27};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080028static const uint8_t kInvalidPacket[] = { 0x80, 0x00 };
29static const uint8_t kInvalidPacketWithCsrc[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
31 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC
32};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080033static const uint8_t kInvalidPacketWithCsrcAndExtension1[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
35 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
36 0xBE, 0xDE, 0x00
37};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080038static const uint8_t kInvalidPacketWithCsrcAndExtension2[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
40 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
41 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
42};
43
44// PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111
45// No FCI information is needed for PLI.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080046static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11
48};
49
50// Packet has only mandatory fixed RTCP header
51// PT = 204, SSRC = 0x1111
Sergey Ulanovdc305db2016-01-14 17:14:54 -080052static const uint8_t kNonCompoundRtcpAppPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 0x81, 0xCC, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11
54};
55
56// PT = 202, Source count = 0
Sergey Ulanovdc305db2016-01-14 17:14:54 -080057static const uint8_t kNonCompoundRtcpSDESPacket[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 0x80, 0xCA, 0x00, 0x00
59};
60
Sergey Ulanovdc305db2016-01-14 17:14:54 -080061static uint8_t kFakeTag[4] = { 0xba, 0xdd, 0xba, 0xdd };
62static uint8_t kTestKey[] = "12345678901234567890";
63static uint8_t kTestAstValue[3] = { 0xaa, 0xbb, 0xcc };
64
65// Valid rtp Message with 2 byte header extension.
66static uint8_t kRtpMsgWith2ByteExtnHeader[] = {
67 0x90, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00,
69 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
70 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension
71 0x01, 0x00, 0x00, 0x00
72};
73
74// RTP packet with single byte extension header of length 4 bytes.
75// Extension id = 3 and length = 3
76static uint8_t kRtpMsgWithAbsSendTimeExtension[] = {
77 0x90, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00,
80 0xBE, 0xDE, 0x00, 0x02,
81 0x22, 0x00, 0x02, 0x1c,
82 0x32, 0xaa, 0xbb, 0xcc,
83};
84
85// Index of AbsSendTimeExtn data in message |kRtpMsgWithAbsSendTimeExtension|.
86static const int kAstIndexInRtpMsg = 21;
87
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088TEST(RtpUtilsTest, GetRtp) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +000089 EXPECT_TRUE(IsRtpPacket(kPcmuFrame, sizeof(kPcmuFrame)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91 int pt;
92 EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt));
93 EXPECT_EQ(0, pt);
94 EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker,
95 sizeof(kRtpPacketWithMarker), &pt));
96 EXPECT_EQ(0, pt);
97
98 int seq_num;
99 EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num));
100 EXPECT_EQ(1, seq_num);
101
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 uint32_t ts;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts));
104 EXPECT_EQ(0u, ts);
105
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc));
108 EXPECT_EQ(1u, ssrc);
109
110 RtpHeader header;
111 EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header));
112 EXPECT_EQ(0, header.payload_type);
113 EXPECT_EQ(1, header.seq_num);
114 EXPECT_EQ(0u, header.timestamp);
115 EXPECT_EQ(1u, header.ssrc);
116
117 EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
118 EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num));
119 EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts));
120 EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc));
121}
122
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000123TEST(RtpUtilsTest, SetRtpHeader) {
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800124 uint8_t packet[] = {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
126 };
127
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000128 RtpHeader header = { 9, 1111, 2222u, 3333u };
129 EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 // Bits: 10 0 0 0000
132 EXPECT_EQ(128u, packet[0]);
133 size_t len;
134 EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len));
135 EXPECT_EQ(12U, len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header));
137 EXPECT_EQ(9, header.payload_type);
138 EXPECT_EQ(1111, header.seq_num);
139 EXPECT_EQ(2222u, header.timestamp);
140 EXPECT_EQ(3333u, header.ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141}
142
143TEST(RtpUtilsTest, GetRtpHeaderLen) {
144 size_t len;
145 EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len));
146 EXPECT_EQ(12U, len);
147
148 EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension,
149 sizeof(kRtpPacketWithMarkerAndCsrcAndExtension),
150 &len));
151 EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len);
152
153 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len));
154 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc,
155 sizeof(kInvalidPacketWithCsrc), &len));
156 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1,
157 sizeof(kInvalidPacketWithCsrcAndExtension1),
158 &len));
159 EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2,
160 sizeof(kInvalidPacketWithCsrcAndExtension2),
161 &len));
162}
163
164TEST(RtpUtilsTest, GetRtcp) {
165 int pt;
166 EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
167 EXPECT_EQ(0xc9, pt);
168
169 EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
170
Peter Boström0c4e06b2015-10-07 12:23:21 +0200171 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
173 sizeof(kNonCompoundRtcpPliFeedbackPacket),
174 &ssrc));
175 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
176 sizeof(kNonCompoundRtcpAppPacket),
177 &ssrc));
178 EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
179 sizeof(kNonCompoundRtcpSDESPacket),
180 &ssrc));
181}
182
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800183// Invalid RTP packets.
184TEST(RtpUtilsTest, InvalidRtpHeader) {
185 // Rtp message with invalid length.
186 const uint8_t kRtpMsgWithInvalidLength[] = {
187 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
189 0xDD, 0xCC, 0xBB, 0xAA, // Only 1 CSRC, but CC count is 4.
190 };
191 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength,
192 sizeof(kRtpMsgWithInvalidLength), nullptr));
193
194 // Rtp message with single byte header extension, invalid extension length.
195 const uint8_t kRtpMsgWithInvalidExtnLength[] = {
196 0x90, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00,
199 0xBE, 0xDE, 0x0A, 0x00, // Extn length - 0x0A00
200 };
201 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength,
202 sizeof(kRtpMsgWithInvalidExtnLength),
203 nullptr));
204}
205
206// Valid RTP packet with a 2byte header extension.
207TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) {
208 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader,
209 sizeof(kRtpMsgWith2ByteExtnHeader), nullptr));
210}
211
212// Valid RTP packet which has 1 byte header AbsSendTime extension in it.
213TEST(RtpUtilsTest, ValidRtpPacketWithAbsSendTimeExtension) {
214 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithAbsSendTimeExtension,
215 sizeof(kRtpMsgWithAbsSendTimeExtension),
216 nullptr));
217}
218
219// Verify handling of a 2 byte extension header RTP messsage. Currently these
220// messages are not supported.
221TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionIn2ByteHeaderExtn) {
222 std::vector<uint8_t> data(
223 kRtpMsgWith2ByteExtnHeader,
224 kRtpMsgWith2ByteExtnHeader + sizeof(kRtpMsgWith2ByteExtnHeader));
225 EXPECT_FALSE(UpdateRtpAbsSendTimeExtension(&data[0], data.size(), 3, 0));
226}
227
228// Verify finding an extension ID in the TURN send indication message.
229TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) {
230 // A valid STUN indication message with a valid RTP header in data attribute
231 // payload field and no extension bit set.
232 uint8_t message_without_extension[] = {
233 0x00, 0x16, 0x00, 0x18, // length of
234 0x21, 0x12, 0xA4, 0x42, // magic cookie
235 '0', '1', '2', '3', // transaction id
236 '4', '5', '6', '7',
237 '8', '9', 'a', 'b',
238 0x00, 0x20, 0x00, 0x04, // Mapped address.
239 0x00, 0x00, 0x00, 0x00,
240 0x00, 0x13, 0x00, 0x0C, // Data attribute.
241 0x80, 0x00, 0x00, 0x00, // RTP packet.
242 0x00, 0x00, 0x00, 0x00,
243 0x00, 0x00, 0x00, 0x00,
244 };
245 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(
246 message_without_extension, sizeof(message_without_extension), 3, 0));
247
248 // A valid STUN indication message with a valid RTP header and a extension
249 // header.
250 uint8_t message[] = {
251 0x00, 0x16, 0x00, 0x24, // length of
252 0x21, 0x12, 0xA4, 0x42, // magic cookie
253 '0', '1', '2', '3', // transaction id
254 '4', '5', '6', '7',
255 '8', '9', 'a', 'b',
256 0x00, 0x20, 0x00, 0x04, // Mapped address.
257 0x00, 0x00, 0x00, 0x00,
258 0x00, 0x13, 0x00, 0x18, // Data attribute.
259 0x90, 0x00, 0x00, 0x00, // RTP packet.
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE,
261 0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc,
262 };
263 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, sizeof(message), 3, 0));
264}
265
266// Test without any packet options variables set. This method should return
267// without HMAC value in the packet.
268TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) {
269 rtc::PacketTimeUpdateParams packet_time_params;
270 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
271 kRtpMsgWithAbsSendTimeExtension +
272 sizeof(kRtpMsgWithAbsSendTimeExtension));
273 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
274 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
275 packet_time_params, 0));
276
277 // Making sure HMAC wasn't updated..
278 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
279 kFakeTag, 4));
280
281 // Verify AbsouluteSendTime extension field wasn't modified.
282 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
283 sizeof(kTestAstValue)));
284}
285
286// Veirfy HMAC is updated when packet option parameters are set.
287TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) {
288 rtc::PacketTimeUpdateParams packet_time_params;
289 packet_time_params.srtp_auth_key.assign(kTestKey,
290 kTestKey + sizeof(kTestKey));
291 packet_time_params.srtp_auth_tag_len = 4;
292
293 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
294 kRtpMsgWithAbsSendTimeExtension +
295 sizeof(kRtpMsgWithAbsSendTimeExtension));
296 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
297 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
298 packet_time_params, 0));
299
300 uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0};
301 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
302 kExpectedTag, sizeof(kExpectedTag)));
303
304 // Verify AbsouluteSendTime extension field is not modified.
305 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
306 sizeof(kTestAstValue)));
307}
308
309// Verify finding an extension ID in a raw rtp message.
310TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInRtpPacket) {
311 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
312 kRtpMsgWithAbsSendTimeExtension +
313 sizeof(kRtpMsgWithAbsSendTimeExtension));
314
315 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(),
316 3, 51183266));
317
318 // Verify that the timestamp was updated.
319 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
320 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
321 sizeof(kExpectedTimestamp)));
322}
323
324// Verify we update both AbsSendTime extension header and HMAC.
325TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) {
326 rtc::PacketTimeUpdateParams packet_time_params;
327 packet_time_params.srtp_auth_key.assign(kTestKey,
328 kTestKey + sizeof(kTestKey));
329 packet_time_params.srtp_auth_tag_len = 4;
330 packet_time_params.rtp_sendtime_extension_id = 3;
331 // 3 is also present in the test message.
332
333 std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
334 kRtpMsgWithAbsSendTimeExtension +
335 sizeof(kRtpMsgWithAbsSendTimeExtension));
336 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
337 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
338 packet_time_params, 51183266));
339
340 const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e};
341 EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
342 kExpectedTag, sizeof(kExpectedTag)));
343
344 // Verify that the timestamp was updated.
345 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
346 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
347 sizeof(kExpectedTimestamp)));
348}
349
350
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351} // namespace cricket