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/gunit.h"
31
32namespace cricket {
33
34static 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)
39static 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};
44static const unsigned char kInvalidPacket[] = { 0x80, 0x00 };
45static 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};
49static 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};
54static 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.
62static 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
68static const unsigned char kNonCompoundRtcpAppPacket[] = {
69    0x81, 0xCC, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11
70};
71
72// PT = 202, Source count = 0
73static const unsigned char kNonCompoundRtcpSDESPacket[] = {
74    0x80, 0xCA, 0x00, 0x00
75};
76
77TEST(RtpUtilsTest, GetRtp) {
78  EXPECT_TRUE(IsRtpPacket(kPcmuFrame, sizeof(kPcmuFrame)));
79
80  int pt;
81  EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt));
82  EXPECT_EQ(0, pt);
83  EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker,
84                                sizeof(kRtpPacketWithMarker), &pt));
85  EXPECT_EQ(0, pt);
86
87  int seq_num;
88  EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num));
89  EXPECT_EQ(1, seq_num);
90
91  uint32_t ts;
92  EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts));
93  EXPECT_EQ(0u, ts);
94
95  uint32_t ssrc;
96  EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc));
97  EXPECT_EQ(1u, ssrc);
98
99  RtpHeader header;
100  EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header));
101  EXPECT_EQ(0, header.payload_type);
102  EXPECT_EQ(1, header.seq_num);
103  EXPECT_EQ(0u, header.timestamp);
104  EXPECT_EQ(1u, header.ssrc);
105
106  EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
107  EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num));
108  EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts));
109  EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc));
110}
111
112TEST(RtpUtilsTest, SetRtpHeader) {
113  unsigned char packet[] = {
114    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
115  };
116
117  RtpHeader header = { 9, 1111, 2222u, 3333u };
118  EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
119
120  // Bits: 10 0 0 0000
121  EXPECT_EQ(128u, packet[0]);
122  size_t len;
123  EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len));
124  EXPECT_EQ(12U, len);
125  EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header));
126  EXPECT_EQ(9, header.payload_type);
127  EXPECT_EQ(1111, header.seq_num);
128  EXPECT_EQ(2222u, header.timestamp);
129  EXPECT_EQ(3333u, header.ssrc);
130}
131
132TEST(RtpUtilsTest, GetRtpHeaderLen) {
133  size_t len;
134  EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len));
135  EXPECT_EQ(12U, len);
136
137  EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension,
138                              sizeof(kRtpPacketWithMarkerAndCsrcAndExtension),
139                              &len));
140  EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len);
141
142  EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len));
143  EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc,
144                               sizeof(kInvalidPacketWithCsrc), &len));
145  EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1,
146                               sizeof(kInvalidPacketWithCsrcAndExtension1),
147                               &len));
148  EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2,
149                               sizeof(kInvalidPacketWithCsrcAndExtension2),
150                               &len));
151}
152
153TEST(RtpUtilsTest, GetRtcp) {
154  int pt;
155  EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
156  EXPECT_EQ(0xc9, pt);
157
158  EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
159
160  uint32_t ssrc;
161  EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
162                          sizeof(kNonCompoundRtcpPliFeedbackPacket),
163                          &ssrc));
164  EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
165                          sizeof(kNonCompoundRtcpAppPacket),
166                          &ssrc));
167  EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
168                           sizeof(kNonCompoundRtcpSDESPacket),
169                           &ssrc));
170}
171
172}  // namespace cricket
173