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