Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: talk/media/base/rtputils_unittest.cc

Issue 1587193006: Move talk/media to webrtc/media (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased to b647aca12a884a13c1728118586245399b55fa3d (#11493) Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « talk/media/base/rtputils.cc ('k') | talk/media/base/screencastid.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/media/base/fakertp.h"
29 #include "talk/media/base/rtputils.h"
30 #include "webrtc/base/asyncpacketsocket.h"
31 #include "webrtc/base/gunit.h"
32
33 namespace cricket {
34
35 static const uint8_t kRtpPacketWithMarker[] = {
36 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
37 };
38 // 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD)
39 // Extension (0xBEDE, 0x1122334455667788)
40 static const uint8_t kRtpPacketWithMarkerAndCsrcAndExtension[] = {
41 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 };
45 static const uint8_t kInvalidPacket[] = { 0x80, 0x00 };
46 static const uint8_t kInvalidPacketWithCsrc[] = {
47 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 };
50 static const uint8_t kInvalidPacketWithCsrcAndExtension1[] = {
51 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 };
55 static const uint8_t kInvalidPacketWithCsrcAndExtension2[] = {
56 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.
63 static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = {
64 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
69 static const uint8_t kNonCompoundRtcpAppPacket[] = {
70 0x81, 0xCC, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11
71 };
72
73 // PT = 202, Source count = 0
74 static const uint8_t kNonCompoundRtcpSDESPacket[] = {
75 0x80, 0xCA, 0x00, 0x00
76 };
77
78 static uint8_t kFakeTag[4] = { 0xba, 0xdd, 0xba, 0xdd };
79 static uint8_t kTestKey[] = "12345678901234567890";
80 static uint8_t kTestAstValue[3] = { 0xaa, 0xbb, 0xcc };
81
82 // Valid rtp Message with 2 byte header extension.
83 static 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
93 static 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|.
103 static const int kAstIndexInRtpMsg = 21;
104
105 TEST(RtpUtilsTest, GetRtp) {
106 EXPECT_TRUE(IsRtpPacket(kPcmuFrame, sizeof(kPcmuFrame)));
107
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
119 uint32_t ts;
120 EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts));
121 EXPECT_EQ(0u, ts);
122
123 uint32_t ssrc;
124 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
140 TEST(RtpUtilsTest, SetRtpHeader) {
141 uint8_t packet[] = {
142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
143 };
144
145 RtpHeader header = { 9, 1111, 2222u, 3333u };
146 EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
147
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);
153 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);
158 }
159
160 TEST(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
181 TEST(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
188 uint32_t ssrc;
189 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
200 // Invalid RTP packets.
201 TEST(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.
224 TEST(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.
230 TEST(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.
238 TEST(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.
246 TEST(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.
285 TEST(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.
304 TEST(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.
327 TEST(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.
342 TEST(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
368 } // namespace cricket
OLDNEW
« no previous file with comments | « talk/media/base/rtputils.cc ('k') | talk/media/base/screencastid.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698