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

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_receiver_unittest.cc

Issue 2770233003: Implemented the GetSources() in native code. (Closed)
Patch Set: Renaming. Add a unit test. Resolve the comments. Created 3 years, 8 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
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 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 <memory>
12
13 #include "webrtc/common_types.h"
14 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
15 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
16 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
18 #include "webrtc/test/gtest.h"
19
20 namespace webrtc {
21
22 const uint32_t kTestRate = 64000u;
23 const uint8_t kTestPayload[] = {'t', 'e', 's', 't'};
24 const uint8_t kPcmuPayloadType = 96;
25 const int64_t kGetSourcesTimeoutMs = 10000;
26 const int kMaxSourceListsSize = 100;
27
28 class RtpReceiverTest : public ::testing::Test {
29 protected:
30 RtpReceiverTest() : fake_clock(123456) {}
31 ~RtpReceiverTest() {}
32
33 void SetUp() override {
Taylor Brandstetter 2017/04/05 04:27:55 nit: This code can just go in the constructor.
Zhi Huang 2017/04/06 03:09:50 Done.
34 rtp_payload_registry_.reset(new RTPPayloadRegistry());
35
36 rtp_receiver_.reset(RtpReceiver::CreateAudioReceiver(
37 &fake_clock, nullptr, nullptr, rtp_payload_registry_.get()));
38
39 CodecInst voice_codec = {};
40 voice_codec.pltype = kPcmuPayloadType;
41 voice_codec.plfreq = 8000;
42 voice_codec.rate = kTestRate;
43 memcpy(voice_codec.plname, "PCMU", 5);
44 rtp_receiver_->RegisterReceivePayload(voice_codec);
45 }
46
47 std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
danilchap 2017/04/05 16:24:21 nit: doesn't need to wrap it in unique_ptr
Zhi Huang 2017/04/06 03:09:50 Done.
48 std::unique_ptr<RtpReceiver> rtp_receiver_;
49 SimulatedClock fake_clock;
danilchap 2017/04/05 16:24:22 put fake_clock before rtp_receiver_ to be sure it
Zhi Huang 2017/04/06 03:09:50 Done.
50 };
51
52 TEST_F(RtpReceiverTest, GetSources) {
53 int64_t timestamp = fake_clock.TimeInMilliseconds();
54 RTPHeader header;
55 header.payloadType = kPcmuPayloadType;
56 header.ssrc = 1;
57 header.timestamp = timestamp;
58 header.numCSRCs = 2;
59 header.arrOfCSRCs[0] = 111;
60 header.arrOfCSRCs[1] = 222;
61 PayloadUnion payload_specific = {AudioPayload()};
62 bool in_order = false;
63
64 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
65 payload_specific, in_order));
66 auto sources = rtp_receiver_->GetSources();
67 // One SSRC source and two CSRC sources.
68 ASSERT_EQ(3u, sources.size());
69 EXPECT_EQ(1u, sources[0].source_id());
Taylor Brandstetter 2017/04/05 04:27:55 nit: We aren't guaranteeing anything about the ord
Zhi Huang 2017/04/06 03:09:50 Maybe should use FindSourceByIdAndType since two s
70 EXPECT_EQ(timestamp, sources[0].timestamp());
71 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
72 EXPECT_EQ(222u, sources[1].source_id());
73 EXPECT_EQ(timestamp, sources[1].timestamp());
74 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
75 EXPECT_EQ(111u, sources[2].source_id());
76 EXPECT_EQ(timestamp, sources[2].timestamp());
77 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[2].source_type());
78
79 // Advance the fake clock and the method is expected to return the
80 // contributing source object with same |source| and updated |timestamp()|.
81 fake_clock.AdvanceTimeMilliseconds(1);
82 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
83 payload_specific, in_order));
84 sources = rtp_receiver_->GetSources();
85 ASSERT_EQ(3u, sources.size());
86 EXPECT_EQ(1u, sources[0].source_id());
87 EXPECT_EQ(timestamp + 1, sources[0].timestamp());
88 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
89 EXPECT_EQ(222u, sources[1].source_id());
90 EXPECT_EQ(timestamp + 1, sources[1].timestamp());
91 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
92 EXPECT_EQ(111u, sources[2].source_id());
93 EXPECT_EQ(timestamp + 1, sources[2].timestamp());
94 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[2].source_type());
95
96 // Simulate the time out.
97 fake_clock.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs + 1);
Taylor Brandstetter 2017/04/05 04:27:55 To make the test even more thorough, could advance
Zhi Huang 2017/04/06 03:09:50 Done.
98 sources = rtp_receiver_->GetSources();
99 // All the sources should be out of date.
100 ASSERT_EQ(0u, sources.size());
101 }
102
103 // Test the case that the SSRC is changed.
104 TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
105 int64_t prev_time = -1;
106 int64_t cur_time = fake_clock.TimeInMilliseconds();
107 RTPHeader header;
108 header.payloadType = kPcmuPayloadType;
109 header.ssrc = 1;
110 header.timestamp = cur_time;
111 PayloadUnion payload_specific = {AudioPayload()};
112 bool in_order = false;
113
114 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
115 payload_specific, in_order));
116 auto sources = rtp_receiver_->GetSources();
117 ASSERT_EQ(1u, sources.size());
118 EXPECT_EQ(1u, sources[0].source_id());
119 EXPECT_EQ(cur_time, sources[0].timestamp());
120
121 // The SSRC is changed and the old SSRC is expected to be returned.
122 fake_clock.AdvanceTimeMilliseconds(100);
123 prev_time = cur_time;
124 cur_time = fake_clock.TimeInMilliseconds();
125 header.ssrc = 2;
126 header.timestamp = cur_time;
127 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
128 payload_specific, in_order));
129 sources = rtp_receiver_->GetSources();
130 ASSERT_EQ(2u, sources.size());
131 EXPECT_EQ(2u, sources[0].source_id());
132 EXPECT_EQ(cur_time, sources[0].timestamp());
133 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
134 EXPECT_EQ(1u, sources[1].source_id());
135 EXPECT_EQ(prev_time, sources[1].timestamp());
136 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[1].source_type());
137
138 // The SSRC is changed again and happen to be changed back to 1. No
139 // duplication is expected.
140 fake_clock.AdvanceTimeMilliseconds(100);
141 header.ssrc = 1;
142 header.timestamp = cur_time;
143 prev_time = cur_time;
144 cur_time = fake_clock.TimeInMilliseconds();
145 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
146 payload_specific, in_order));
147 sources = rtp_receiver_->GetSources();
148 ASSERT_EQ(2u, sources.size());
149 EXPECT_EQ(1u, sources[0].source_id());
150 EXPECT_EQ(cur_time, sources[0].timestamp());
151 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
152 EXPECT_EQ(2u, sources[1].source_id());
153 EXPECT_EQ(prev_time, sources[1].timestamp());
154 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[1].source_type());
155
156 // Old SSRC source timeout.
157 fake_clock.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
158 cur_time = fake_clock.TimeInMilliseconds();
159 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
160 payload_specific, in_order));
161 sources = rtp_receiver_->GetSources();
162 ASSERT_EQ(1u, sources.size());
163 EXPECT_EQ(1u, sources[0].source_id());
164 EXPECT_EQ(cur_time, sources[0].timestamp());
165 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
166 }
167
168 // Test that the out of date objects will be removed when the source lists are
169 // too large.
170 TEST_F(RtpReceiverTest, GetSourcesMaxListSize) {
Taylor Brandstetter 2017/04/05 04:27:55 This test would succeed even if the out-of-date ob
Zhi Huang 2017/04/06 03:09:50 Agreed. Thanks for catching this. I plan to expose
171 int64_t timestamp = fake_clock.TimeInMilliseconds();
172 bool in_order = false;
173 RTPHeader header;
174 header.payloadType = kPcmuPayloadType;
175 header.timestamp = timestamp;
176 PayloadUnion payload_specific = {AudioPayload()};
177 header.numCSRCs = 1;
178
179 for (size_t i = 0; i < kMaxSourceListsSize; ++i) {
180 header.ssrc = i;
181 header.arrOfCSRCs[0] = (i + 1);
182 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
183 payload_specific, in_order));
184 }
185 auto sources = rtp_receiver_->GetSources();
186 // Expect |kMaxSourceListsSize| SSRC sources and |kMaxSourceListsSize| CSRC
187 // sources.
188 ASSERT_TRUE(sources.size() == 2 * kMaxSourceListsSize);
189 for (size_t i = 0; i < kMaxSourceListsSize; ++i) {
190 // The SSRC source IDs are expected to be 99, 98, 97 ... 0
191 EXPECT_EQ(kMaxSourceListsSize - i - 1, sources[i].source_id());
192 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[i].source_type());
193 EXPECT_EQ(timestamp, sources[i].timestamp());
194
195 // The CSRC source IDs are expected to be 100, 99, 98 ... 1
196 EXPECT_EQ(kMaxSourceListsSize - i,
197 sources[i + kMaxSourceListsSize].source_id());
198 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE,
199 sources[i + kMaxSourceListsSize].source_type());
200 EXPECT_EQ(timestamp, sources[i + kMaxSourceListsSize].timestamp());
201 }
202
203 // Timeout. All the existing objects are out of date and are expected to be
204 // removed.
205 fake_clock.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs + 1);
206 header.ssrc = 111;
207 header.arrOfCSRCs[0] = 222;
208 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
209 payload_specific, in_order));
210 sources = rtp_receiver_->GetSources();
211 ASSERT_EQ(2u, sources.size());
212 EXPECT_EQ(111u, sources[0].source_id());
213 EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
214 EXPECT_EQ(fake_clock.TimeInMilliseconds(), sources[0].timestamp());
215
216 EXPECT_EQ(222u, sources[1].source_id());
217 EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
218 EXPECT_EQ(fake_clock.TimeInMilliseconds(), sources[1].timestamp());
219 }
220
221 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698