blob: c1cde0f59900870f03b0fdb282c7807ad9be8a65 [file] [log] [blame]
hbos8d609f62017-04-10 07:39:05 -07001/*
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
Mirko Bonadei71207422017-09-15 13:58:09 +020013#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/rtp_rtcp/include/rtp_header_parser.h"
15#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
16#include "modules/rtp_rtcp/include/rtp_receiver.h"
17#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
18#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
19#include "modules/rtp_rtcp/source/rtp_receiver_impl.h"
20#include "test/gmock.h"
21#include "test/gtest.h"
hbos8d609f62017-04-10 07:39:05 -070022
23namespace webrtc {
zhihuang04262222017-04-11 11:28:10 -070024namespace {
25
nisse7fcdb6d2017-06-01 00:30:55 -070026using ::testing::NiceMock;
zhihuang04262222017-04-11 11:28:10 -070027using ::testing::UnorderedElementsAre;
hbos8d609f62017-04-10 07:39:05 -070028
29const uint32_t kTestRate = 64000u;
30const uint8_t kTestPayload[] = {'t', 'e', 's', 't'};
31const uint8_t kPcmuPayloadType = 96;
32const int64_t kGetSourcesTimeoutMs = 10000;
zhihuang04262222017-04-11 11:28:10 -070033const uint32_t kSsrc1 = 123;
34const uint32_t kSsrc2 = 124;
35const uint32_t kCsrc1 = 111;
36const uint32_t kCsrc2 = 222;
37const bool kInOrder = true;
38
39static uint32_t rtp_timestamp(int64_t time_ms) {
40 return static_cast<uint32_t>(time_ms * kTestRate / 1000);
41}
42
43} // namespace
hbos8d609f62017-04-10 07:39:05 -070044
45class RtpReceiverTest : public ::testing::Test {
46 protected:
47 RtpReceiverTest()
48 : fake_clock_(123456),
49 rtp_receiver_(
50 RtpReceiver::CreateAudioReceiver(&fake_clock_,
nisse7fcdb6d2017-06-01 00:30:55 -070051 &mock_rtp_data_,
hbos8d609f62017-04-10 07:39:05 -070052 nullptr,
53 &rtp_payload_registry_)) {
54 CodecInst voice_codec = {};
55 voice_codec.pltype = kPcmuPayloadType;
56 voice_codec.plfreq = 8000;
57 voice_codec.rate = kTestRate;
58 memcpy(voice_codec.plname, "PCMU", 5);
59 rtp_receiver_->RegisterReceivePayload(voice_codec);
60 }
61 ~RtpReceiverTest() {}
62
63 bool FindSourceByIdAndType(const std::vector<RtpSource>& sources,
64 uint32_t source_id,
65 RtpSourceType type,
66 RtpSource* source) {
67 for (size_t i = 0; i < sources.size(); ++i) {
68 if (sources[i].source_id() == source_id &&
69 sources[i].source_type() == type) {
70 (*source) = sources[i];
71 return true;
72 }
73 }
74 return false;
75 }
76
77 SimulatedClock fake_clock_;
nisse7fcdb6d2017-06-01 00:30:55 -070078 NiceMock<MockRtpData> mock_rtp_data_;
hbos8d609f62017-04-10 07:39:05 -070079 RTPPayloadRegistry rtp_payload_registry_;
80 std::unique_ptr<RtpReceiver> rtp_receiver_;
81};
82
83TEST_F(RtpReceiverTest, GetSources) {
zhihuang04262222017-04-11 11:28:10 -070084 int64_t now_ms = fake_clock_.TimeInMilliseconds();
85
hbos8d609f62017-04-10 07:39:05 -070086 RTPHeader header;
87 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -070088 header.ssrc = kSsrc1;
89 header.timestamp = rtp_timestamp(now_ms);
hbos8d609f62017-04-10 07:39:05 -070090 header.numCSRCs = 2;
zhihuang04262222017-04-11 11:28:10 -070091 header.arrOfCSRCs[0] = kCsrc1;
92 header.arrOfCSRCs[1] = kCsrc2;
hbos8d609f62017-04-10 07:39:05 -070093 PayloadUnion payload_specific = {AudioPayload()};
hbos8d609f62017-04-10 07:39:05 -070094
zhihuang04262222017-04-11 11:28:10 -070095 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
96 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -070097 auto sources = rtp_receiver_->GetSources();
98 // One SSRC source and two CSRC sources.
zhihuang04262222017-04-11 11:28:10 -070099 EXPECT_THAT(sources, UnorderedElementsAre(
100 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
101 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
102 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700103
104 // Advance the fake clock and the method is expected to return the
105 // contributing source object with same source id and updated timestamp.
106 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700107 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
108 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700109 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700110 now_ms = fake_clock_.TimeInMilliseconds();
111 EXPECT_THAT(sources, UnorderedElementsAre(
112 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
113 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
114 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700115
116 // Test the edge case that the sources are still there just before the
117 // timeout.
zhihuang04262222017-04-11 11:28:10 -0700118 int64_t prev_time_ms = fake_clock_.TimeInMilliseconds();
hbos8d609f62017-04-10 07:39:05 -0700119 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
120 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700121 EXPECT_THAT(sources,
122 UnorderedElementsAre(
123 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
124 RtpSource(prev_time_ms, kCsrc1, RtpSourceType::CSRC),
125 RtpSource(prev_time_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700126
127 // Time out.
128 fake_clock_.AdvanceTimeMilliseconds(1);
129 sources = rtp_receiver_->GetSources();
130 // All the sources should be out of date.
131 ASSERT_EQ(0u, sources.size());
132}
133
134// Test the case that the SSRC is changed.
135TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
zhihuang04262222017-04-11 11:28:10 -0700136 int64_t prev_time_ms = -1;
137 int64_t now_ms = fake_clock_.TimeInMilliseconds();
138
hbos8d609f62017-04-10 07:39:05 -0700139 RTPHeader header;
140 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700141 header.ssrc = kSsrc1;
142 header.timestamp = rtp_timestamp(now_ms);
hbos8d609f62017-04-10 07:39:05 -0700143 PayloadUnion payload_specific = {AudioPayload()};
hbos8d609f62017-04-10 07:39:05 -0700144
zhihuang04262222017-04-11 11:28:10 -0700145 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
146 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700147 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700148 EXPECT_THAT(sources, UnorderedElementsAre(
149 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700150
151 // The SSRC is changed and the old SSRC is expected to be returned.
152 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700153 prev_time_ms = now_ms;
154 now_ms = fake_clock_.TimeInMilliseconds();
155 header.ssrc = kSsrc2;
156 header.timestamp = rtp_timestamp(now_ms);
157 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
158 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700159 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700160 EXPECT_THAT(sources, UnorderedElementsAre(
161 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
162 RtpSource(now_ms, kSsrc2, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700163
164 // The SSRC is changed again and happen to be changed back to 1. No
165 // duplication is expected.
166 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700167 header.ssrc = kSsrc1;
168 header.timestamp = rtp_timestamp(now_ms);
169 prev_time_ms = now_ms;
170 now_ms = fake_clock_.TimeInMilliseconds();
171 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
172 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700173 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700174 EXPECT_THAT(sources, UnorderedElementsAre(
175 RtpSource(prev_time_ms, kSsrc2, RtpSourceType::SSRC),
176 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700177
178 // Old SSRC source timeout.
179 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700180 now_ms = fake_clock_.TimeInMilliseconds();
181 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
182 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700183 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700184 EXPECT_THAT(sources, UnorderedElementsAre(
185 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700186}
187
188TEST_F(RtpReceiverTest, GetSourcesRemoveOutdatedSource) {
zhihuang04262222017-04-11 11:28:10 -0700189 int64_t now_ms = fake_clock_.TimeInMilliseconds();
190
hbos8d609f62017-04-10 07:39:05 -0700191 RTPHeader header;
192 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700193 header.timestamp = rtp_timestamp(now_ms);
hbos8d609f62017-04-10 07:39:05 -0700194 PayloadUnion payload_specific = {AudioPayload()};
195 header.numCSRCs = 1;
zhihuang04262222017-04-11 11:28:10 -0700196 size_t kSourceListSize = 20;
hbos8d609f62017-04-10 07:39:05 -0700197
zhihuang04262222017-04-11 11:28:10 -0700198 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700199 header.ssrc = i;
200 header.arrOfCSRCs[0] = (i + 1);
zhihuang04262222017-04-11 11:28:10 -0700201 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload,
202 sizeof(kTestPayload),
203 payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700204 }
205
zhihuang04262222017-04-11 11:28:10 -0700206 RtpSource source(0, 0, RtpSourceType::SSRC);
hbos8d609f62017-04-10 07:39:05 -0700207 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700208 // Expect |kSourceListSize| SSRC sources and |kSourceListSize| CSRC sources.
209 ASSERT_EQ(2 * kSourceListSize, sources.size());
210 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700211 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
212 ASSERT_TRUE(
213 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700214 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700215
216 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
217 ASSERT_TRUE(
218 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700219 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700220 }
221
222 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700223 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700224 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
225 ASSERT_TRUE(
226 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700227 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700228
229 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
230 ASSERT_TRUE(
231 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700232 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700233 }
234
235 // Timeout. All the existing objects are out of date and are expected to be
236 // removed.
237 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700238 header.ssrc = kSsrc1;
239 header.arrOfCSRCs[0] = kCsrc1;
240 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
241 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700242 auto rtp_receiver_impl = static_cast<RtpReceiverImpl*>(rtp_receiver_.get());
243 auto ssrc_sources = rtp_receiver_impl->ssrc_sources_for_testing();
244 ASSERT_EQ(1u, ssrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700245 EXPECT_EQ(kSsrc1, ssrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700246 EXPECT_EQ(RtpSourceType::SSRC, ssrc_sources.begin()->source_type());
247 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
248 ssrc_sources.begin()->timestamp_ms());
249
250 auto csrc_sources = rtp_receiver_impl->csrc_sources_for_testing();
251 ASSERT_EQ(1u, csrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700252 EXPECT_EQ(kCsrc1, csrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700253 EXPECT_EQ(RtpSourceType::CSRC, csrc_sources.begin()->source_type());
254 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
255 csrc_sources.begin()->timestamp_ms());
256}
257
zstein2b706342017-08-24 14:52:17 -0700258// The audio level from the RTPHeader extension should be stored in the
259// RtpSource with the matching SSRC.
260TEST_F(RtpReceiverTest, GetSourcesContainsAudioLevelExtension) {
261 RTPHeader header;
262 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
263 header.payloadType = kPcmuPayloadType;
264 header.ssrc = kSsrc1;
265 header.timestamp = rtp_timestamp(time1_ms);
266 header.extension.hasAudioLevel = true;
267 header.extension.audioLevel = 10;
268 PayloadUnion payload_specific = {AudioPayload()};
269
270 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
271 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
272 auto sources = rtp_receiver_->GetSources();
273 EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
274 time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
275
276 // Receive a packet from a different SSRC with a different level and check
277 // that they are both remembered.
278 fake_clock_.AdvanceTimeMilliseconds(1);
279 int64_t time2_ms = fake_clock_.TimeInMilliseconds();
280 header.ssrc = kSsrc2;
281 header.timestamp = rtp_timestamp(time2_ms);
282 header.extension.hasAudioLevel = true;
283 header.extension.audioLevel = 20;
284
285 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
286 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
287 sources = rtp_receiver_->GetSources();
288 EXPECT_THAT(sources,
289 UnorderedElementsAre(
290 RtpSource(time1_ms, kSsrc1, RtpSourceType::SSRC, 10),
291 RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
292
293 // Receive a packet from the first SSRC again and check that the level is
294 // updated.
295 fake_clock_.AdvanceTimeMilliseconds(1);
296 int64_t time3_ms = fake_clock_.TimeInMilliseconds();
297 header.ssrc = kSsrc1;
298 header.timestamp = rtp_timestamp(time3_ms);
299 header.extension.hasAudioLevel = true;
300 header.extension.audioLevel = 30;
301
302 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
303 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
304 sources = rtp_receiver_->GetSources();
305 EXPECT_THAT(sources,
306 UnorderedElementsAre(
307 RtpSource(time3_ms, kSsrc1, RtpSourceType::SSRC, 30),
308 RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
309}
310
311TEST_F(RtpReceiverTest,
312 MissingAudioLevelHeaderExtensionClearsRtpSourceAudioLevel) {
313 RTPHeader header;
314 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
315 header.payloadType = kPcmuPayloadType;
316 header.ssrc = kSsrc1;
317 header.timestamp = rtp_timestamp(time1_ms);
318 header.extension.hasAudioLevel = true;
319 header.extension.audioLevel = 10;
320 PayloadUnion payload_specific = {AudioPayload()};
321
322 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
323 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
324 auto sources = rtp_receiver_->GetSources();
325 EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
326 time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
327
328 // Receive a second packet without the audio level header extension and check
329 // that the audio level is cleared.
330 fake_clock_.AdvanceTimeMilliseconds(1);
331 int64_t time2_ms = fake_clock_.TimeInMilliseconds();
332 header.timestamp = rtp_timestamp(time2_ms);
333 header.extension.hasAudioLevel = false;
334
335 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
336 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
337 sources = rtp_receiver_->GetSources();
338 EXPECT_THAT(sources, UnorderedElementsAre(
339 RtpSource(time2_ms, kSsrc1, RtpSourceType::SSRC)));
340}
341
hbos8d609f62017-04-10 07:39:05 -0700342} // namespace webrtc