blob: c8c936232d4e63b602c87ee0b3ccd0a0635c680d [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_)) {
Karl Wibergc62f6c72017-10-04 12:38:53 +020054 rtp_receiver_->RegisterReceivePayload(kPcmuPayloadType,
55 SdpAudioFormat("PCMU", 8000, 1));
hbos8d609f62017-04-10 07:39:05 -070056 }
57 ~RtpReceiverTest() {}
58
59 bool FindSourceByIdAndType(const std::vector<RtpSource>& sources,
60 uint32_t source_id,
61 RtpSourceType type,
62 RtpSource* source) {
63 for (size_t i = 0; i < sources.size(); ++i) {
64 if (sources[i].source_id() == source_id &&
65 sources[i].source_type() == type) {
66 (*source) = sources[i];
67 return true;
68 }
69 }
70 return false;
71 }
72
73 SimulatedClock fake_clock_;
nisse7fcdb6d2017-06-01 00:30:55 -070074 NiceMock<MockRtpData> mock_rtp_data_;
hbos8d609f62017-04-10 07:39:05 -070075 RTPPayloadRegistry rtp_payload_registry_;
76 std::unique_ptr<RtpReceiver> rtp_receiver_;
77};
78
79TEST_F(RtpReceiverTest, GetSources) {
zhihuang04262222017-04-11 11:28:10 -070080 int64_t now_ms = fake_clock_.TimeInMilliseconds();
81
hbos8d609f62017-04-10 07:39:05 -070082 RTPHeader header;
83 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -070084 header.ssrc = kSsrc1;
85 header.timestamp = rtp_timestamp(now_ms);
hbos8d609f62017-04-10 07:39:05 -070086 header.numCSRCs = 2;
zhihuang04262222017-04-11 11:28:10 -070087 header.arrOfCSRCs[0] = kCsrc1;
88 header.arrOfCSRCs[1] = kCsrc2;
Karl Wibergc62f6c72017-10-04 12:38:53 +020089 const PayloadUnion payload_specific{
90 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -070091
zhihuang04262222017-04-11 11:28:10 -070092 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
93 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -070094 auto sources = rtp_receiver_->GetSources();
95 // One SSRC source and two CSRC sources.
zhihuang04262222017-04-11 11:28:10 -070096 EXPECT_THAT(sources, UnorderedElementsAre(
97 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
98 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
99 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700100
101 // Advance the fake clock and the method is expected to return the
102 // contributing source object with same source id and updated timestamp.
103 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700104 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
105 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700106 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700107 now_ms = fake_clock_.TimeInMilliseconds();
108 EXPECT_THAT(sources, UnorderedElementsAre(
109 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
110 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
111 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700112
113 // Test the edge case that the sources are still there just before the
114 // timeout.
zhihuang04262222017-04-11 11:28:10 -0700115 int64_t prev_time_ms = fake_clock_.TimeInMilliseconds();
hbos8d609f62017-04-10 07:39:05 -0700116 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
117 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700118 EXPECT_THAT(sources,
119 UnorderedElementsAre(
120 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
121 RtpSource(prev_time_ms, kCsrc1, RtpSourceType::CSRC),
122 RtpSource(prev_time_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700123
124 // Time out.
125 fake_clock_.AdvanceTimeMilliseconds(1);
126 sources = rtp_receiver_->GetSources();
127 // All the sources should be out of date.
128 ASSERT_EQ(0u, sources.size());
129}
130
131// Test the case that the SSRC is changed.
132TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
zhihuang04262222017-04-11 11:28:10 -0700133 int64_t prev_time_ms = -1;
134 int64_t now_ms = fake_clock_.TimeInMilliseconds();
135
hbos8d609f62017-04-10 07:39:05 -0700136 RTPHeader header;
137 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700138 header.ssrc = kSsrc1;
139 header.timestamp = rtp_timestamp(now_ms);
Karl Wibergc62f6c72017-10-04 12:38:53 +0200140 const PayloadUnion payload_specific{
141 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -0700142
zhihuang04262222017-04-11 11:28:10 -0700143 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
144 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700145 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700146 EXPECT_THAT(sources, UnorderedElementsAre(
147 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700148
149 // The SSRC is changed and the old SSRC is expected to be returned.
150 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700151 prev_time_ms = now_ms;
152 now_ms = fake_clock_.TimeInMilliseconds();
153 header.ssrc = kSsrc2;
154 header.timestamp = rtp_timestamp(now_ms);
155 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
156 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700157 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700158 EXPECT_THAT(sources, UnorderedElementsAre(
159 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
160 RtpSource(now_ms, kSsrc2, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700161
162 // The SSRC is changed again and happen to be changed back to 1. No
163 // duplication is expected.
164 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700165 header.ssrc = kSsrc1;
166 header.timestamp = rtp_timestamp(now_ms);
167 prev_time_ms = now_ms;
168 now_ms = fake_clock_.TimeInMilliseconds();
169 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
170 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700171 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700172 EXPECT_THAT(sources, UnorderedElementsAre(
173 RtpSource(prev_time_ms, kSsrc2, RtpSourceType::SSRC),
174 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700175
176 // Old SSRC source timeout.
177 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700178 now_ms = fake_clock_.TimeInMilliseconds();
179 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
180 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700181 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700182 EXPECT_THAT(sources, UnorderedElementsAre(
183 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700184}
185
186TEST_F(RtpReceiverTest, GetSourcesRemoveOutdatedSource) {
zhihuang04262222017-04-11 11:28:10 -0700187 int64_t now_ms = fake_clock_.TimeInMilliseconds();
188
hbos8d609f62017-04-10 07:39:05 -0700189 RTPHeader header;
190 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700191 header.timestamp = rtp_timestamp(now_ms);
Karl Wibergc62f6c72017-10-04 12:38:53 +0200192 const PayloadUnion payload_specific{
193 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -0700194 header.numCSRCs = 1;
zhihuang04262222017-04-11 11:28:10 -0700195 size_t kSourceListSize = 20;
hbos8d609f62017-04-10 07:39:05 -0700196
zhihuang04262222017-04-11 11:28:10 -0700197 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700198 header.ssrc = i;
199 header.arrOfCSRCs[0] = (i + 1);
zhihuang04262222017-04-11 11:28:10 -0700200 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload,
201 sizeof(kTestPayload),
202 payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700203 }
204
zhihuang04262222017-04-11 11:28:10 -0700205 RtpSource source(0, 0, RtpSourceType::SSRC);
hbos8d609f62017-04-10 07:39:05 -0700206 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700207 // Expect |kSourceListSize| SSRC sources and |kSourceListSize| CSRC sources.
208 ASSERT_EQ(2 * kSourceListSize, sources.size());
209 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700210 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
211 ASSERT_TRUE(
212 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700213 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700214
215 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
216 ASSERT_TRUE(
217 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700218 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700219 }
220
221 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700222 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700223 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
224 ASSERT_TRUE(
225 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700226 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700227
228 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
229 ASSERT_TRUE(
230 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700231 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700232 }
233
234 // Timeout. All the existing objects are out of date and are expected to be
235 // removed.
236 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700237 header.ssrc = kSsrc1;
238 header.arrOfCSRCs[0] = kCsrc1;
239 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
240 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
hbos8d609f62017-04-10 07:39:05 -0700241 auto rtp_receiver_impl = static_cast<RtpReceiverImpl*>(rtp_receiver_.get());
242 auto ssrc_sources = rtp_receiver_impl->ssrc_sources_for_testing();
243 ASSERT_EQ(1u, ssrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700244 EXPECT_EQ(kSsrc1, ssrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700245 EXPECT_EQ(RtpSourceType::SSRC, ssrc_sources.begin()->source_type());
246 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
247 ssrc_sources.begin()->timestamp_ms());
248
249 auto csrc_sources = rtp_receiver_impl->csrc_sources_for_testing();
250 ASSERT_EQ(1u, csrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700251 EXPECT_EQ(kCsrc1, csrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700252 EXPECT_EQ(RtpSourceType::CSRC, csrc_sources.begin()->source_type());
253 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
254 csrc_sources.begin()->timestamp_ms());
255}
256
zstein2b706342017-08-24 14:52:17 -0700257// The audio level from the RTPHeader extension should be stored in the
258// RtpSource with the matching SSRC.
259TEST_F(RtpReceiverTest, GetSourcesContainsAudioLevelExtension) {
260 RTPHeader header;
261 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
262 header.payloadType = kPcmuPayloadType;
263 header.ssrc = kSsrc1;
264 header.timestamp = rtp_timestamp(time1_ms);
265 header.extension.hasAudioLevel = true;
266 header.extension.audioLevel = 10;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200267 const PayloadUnion payload_specific{
268 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
zstein2b706342017-08-24 14:52:17 -0700269
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;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200320 const PayloadUnion payload_specific{
321 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
zstein2b706342017-08-24 14:52:17 -0700322
323 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
324 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
325 auto sources = rtp_receiver_->GetSources();
326 EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
327 time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
328
329 // Receive a second packet without the audio level header extension and check
330 // that the audio level is cleared.
331 fake_clock_.AdvanceTimeMilliseconds(1);
332 int64_t time2_ms = fake_clock_.TimeInMilliseconds();
333 header.timestamp = rtp_timestamp(time2_ms);
334 header.extension.hasAudioLevel = false;
335
336 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
337 header, kTestPayload, sizeof(kTestPayload), payload_specific, !kInOrder));
338 sources = rtp_receiver_->GetSources();
339 EXPECT_THAT(sources, UnorderedElementsAre(
340 RtpSource(time2_ms, kSsrc1, RtpSourceType::SSRC)));
341}
342
hbos8d609f62017-04-10 07:39:05 -0700343} // namespace webrtc