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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtcp_packet/nack_unittest.cc

Issue 1461623003: rtcp::Nack packet moved into own file and got Parse function (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years 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
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
12
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using ::testing::ElementsAre;
17 using ::testing::UnorderedElementsAreArray;
18
19 using webrtc::rtcp::Nack;
20 using webrtc::rtcp::RawPacket;
21 using webrtc::RTCPUtility::RtcpCommonHeader;
22 using webrtc::RTCPUtility::RtcpParseCommonHeader;
23
24 namespace webrtc {
25 namespace {
26
27 const uint32_t kSenderSsrc = 0x12345678;
28 const uint32_t kRemoteSsrc = 0x23456789;
29
30 const uint16_t kMinList[] = {0, 1, 3, 8, 16};
åsapersson 2015/12/21 11:17:35 maybe kList?
danilchap 2015/12/21 15:55:55 Done.
31 const size_t kMinListLength = sizeof(kMinList) / sizeof(kMinList[0]);
32 const uint8_t kMinPacket[] = {0x81, 205, 0x00, 0x03, 0x12, 0x34, 0x56, 0x78,
33 0x23, 0x45, 0x67, 0x89, 0x00, 0x00, 0x80, 0x85};
34 const size_t kMinPacketLength = sizeof(kMinPacket);
35
36 const uint16_t kWrapList[] = {0xffdc, 0xffec, 0xfffe, 0xffff, 0x0000,
37 0x0001, 0x0003, 0x0014, 0x0064};
38 const size_t kWrapListLength = sizeof(kWrapList) / sizeof(kWrapList[0]);
39 const uint8_t kWrapPacket[] = {0x81, 205, 0x00, 0x06, 0x12, 0x34, 0x56, 0x78,
40 0x23, 0x45, 0x67, 0x89, 0xff, 0xdc, 0x80, 0x00,
41 0xff, 0xfe, 0x00, 0x17, 0x00, 0x14, 0x00, 0x00,
42 0x00, 0x64, 0x00, 0x00};
43 const size_t kWrapPacketLength = sizeof(kWrapPacket);
44 const size_t kWrapPacketLength = sizeof(kWrapPacket);
åsapersson 2015/12/21 11:17:35 remove
danilchap 2015/12/21 15:55:55 oops
45
46 TEST(RtcpPacketNackTest, Create) {
47 Nack nack;
48 nack.From(kSenderSsrc);
49 nack.To(kRemoteSsrc);
50 nack.WithList(kMinList, kMinListLength);
51
52 rtc::scoped_ptr<RawPacket> packet = nack.Build();
53
54 EXPECT_EQ(kMinPacketLength, packet->Length());
55 EXPECT_EQ(0, memcmp(kMinPacket, packet->Buffer(), kMinPacketLength));
56 }
57
58 TEST(RtcpPacketNackTest, Parse) {
59 RtcpCommonHeader header;
60 EXPECT_TRUE(RtcpParseCommonHeader(kMinPacket, kMinPacketLength, &header));
61 EXPECT_EQ(kMinPacketLength, header.BlockSize());
62 Nack mparsed;
åsapersson 2015/12/21 11:17:35 maybe parsed?
danilchap 2015/12/21 15:55:55 Done.
63
64 EXPECT_TRUE(
65 mparsed.Parse(header, kMinPacket + RtcpCommonHeader::kHeaderSizeBytes));
66 const Nack& const_parsed = mparsed;
67
68 EXPECT_EQ(kSenderSsrc, const_parsed.sender_ssrc());
69 EXPECT_EQ(kRemoteSsrc, const_parsed.media_ssrc());
70 EXPECT_EQ(kMinListLength, const_parsed.packet_ids().size());
71 for (size_t i = 0; i < kMinListLength; ++i) {
72 EXPECT_EQ(kMinList[i], const_parsed.packet_ids()[i]);
73 }
74 }
75
76 TEST(RtcpPacketNackTest, CreateWrap) {
77 Nack nack;
78 nack.From(kSenderSsrc);
79 nack.To(kRemoteSsrc);
80 nack.WithList(kWrapList, kWrapListLength);
81
82 rtc::scoped_ptr<RawPacket> packet = nack.Build();
83
84 EXPECT_EQ(kWrapPacketLength, packet->Length());
85 EXPECT_EQ(0, memcmp(kWrapPacket, packet->Buffer(), kWrapPacketLength));
86 }
87
88 TEST(RtcpPacketNackTest, ParseWrap) {
89 RtcpCommonHeader header;
90 EXPECT_TRUE(RtcpParseCommonHeader(kWrapPacket, kWrapPacketLength, &header));
91 EXPECT_EQ(kWrapPacketLength, header.BlockSize());
92
93 Nack parsed;
94 EXPECT_TRUE(
95 parsed.Parse(header, kWrapPacket + RtcpCommonHeader::kHeaderSizeBytes));
96
97 EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
98 EXPECT_EQ(kRemoteSsrc, parsed.media_ssrc());
99 EXPECT_EQ(kWrapListLength, parsed.packet_ids().size());
100 for (size_t i = 0; i < kWrapListLength; ++i) {
101 EXPECT_EQ(kWrapList[i], parsed.packet_ids()[i]);
102 }
103 }
104
105 TEST(RtcpPacketNackTest, BadOrder) {
106 // Does not guarantee optimal packing, but should guarantee correctness.
107 const uint16_t kList[] = {1, 25, 13, 12, 9, 27, 29};
108 const size_t kListLength = sizeof(kList) / sizeof(kList[0]);
109 Nack nack;
110 nack.From(kSenderSsrc);
111 nack.To(kRemoteSsrc);
112 nack.WithList(kList, kListLength);
113
114 rtc::scoped_ptr<RawPacket> packet = nack.Build();
115
116 Nack parsed;
117 RtcpCommonHeader header;
118 EXPECT_TRUE(
119 RtcpParseCommonHeader(packet->Buffer(), packet->Length(), &header));
120 EXPECT_TRUE(parsed.Parse(
121 header, packet->Buffer() + RtcpCommonHeader::kHeaderSizeBytes));
122
123 EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
124 EXPECT_EQ(kRemoteSsrc, parsed.media_ssrc());
125 EXPECT_THAT(parsed.packet_ids(),
126 UnorderedElementsAreArray(kList, kListLength));
127 }
128
129 TEST(RtcpPacketNackTest, CreateFragmented) {
130 Nack nack;
131 const uint16_t kList[] = {1, 100, 200, 300, 400};
132 const uint16_t kListLength = sizeof(kList) / sizeof(kList[0]);
133 nack.From(kSenderSsrc);
134 nack.To(kRemoteSsrc);
135 nack.WithList(kList, kListLength);
136
137 class Verifier : public rtcp::RtcpPacket::PacketReadyCallback {
138 public:
139 void OnPacketReady(uint8_t* data, size_t length) override {
140 ++packets_created_;
141
142 RtcpCommonHeader header;
143 EXPECT_TRUE(RtcpParseCommonHeader(data, length, &header));
144 EXPECT_EQ(length, header.BlockSize());
145 Nack parsed;
146
147 EXPECT_TRUE(
148 parsed.Parse(header, data + RtcpCommonHeader::kHeaderSizeBytes));
149
150 EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
151 EXPECT_EQ(kRemoteSsrc, parsed.media_ssrc());
152 switch (packets_created_) {
153 case 1:
154 EXPECT_THAT(parsed.packet_ids(), ElementsAre(1, 100, 200));
155 break;
156 case 2:
157 EXPECT_THAT(parsed.packet_ids(), ElementsAre(300, 400));
158 break;
159 default:
160 ADD_FAILURE() << "Unexpected packet count: " << packets_created_;
161 }
162 }
163 int packets_created_ = 0;
164 } verifier;
165 const size_t kBufferSize = 12 + (3 * 4); // Fits common header + 3 nack items
166 uint8_t buffer[kBufferSize];
167 EXPECT_TRUE(nack.BuildExternalBuffer(buffer, kBufferSize, &verifier));
168 EXPECT_EQ(2, verifier.packets_created_);
169 }
170
171 TEST(RtcpPacketNackTest, CreateFailsWithTooSmallBuffer) {
172 const uint16_t kList[] = {1};
173 const size_t kMinNackBlockSize = 16;
174 Nack nack;
175 nack.From(kSenderSsrc);
176 nack.To(kRemoteSsrc);
177 nack.WithList(kList, 1);
178 class Verifier : public rtcp::RtcpPacket::PacketReadyCallback {
179 public:
180 void OnPacketReady(uint8_t* data, size_t length) override {
181 ADD_FAILURE() << "Buffer should be too small.";
182 }
183 } verifier;
184 uint8_t buffer[kMinNackBlockSize - 1];
185 EXPECT_FALSE(
186 nack.BuildExternalBuffer(buffer, kMinNackBlockSize - 1, &verifier));
187 }
188
189 TEST(RtcpPacketNackTest, ParseFailsWithTooSmallBuffer) {
190 RtcpCommonHeader header;
191 EXPECT_TRUE(RtcpParseCommonHeader(kMinPacket, kMinPacketLength, &header));
192 header.payload_size_bytes--; // Damage the packet
193 Nack parsed;
194 EXPECT_FALSE(
195 parsed.Parse(header, kMinPacket + RtcpCommonHeader::kHeaderSizeBytes));
196 }
197
198 } // namespace
199 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698