blob: e8dccf43afe512c43ba1b886480d796e13bedd8a [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;
zhihuang04262222017-04-11 11:28:10 -070037
38static uint32_t rtp_timestamp(int64_t time_ms) {
39 return static_cast<uint32_t>(time_ms * kTestRate / 1000);
40}
41
42} // namespace
hbos8d609f62017-04-10 07:39:05 -070043
44class RtpReceiverTest : public ::testing::Test {
45 protected:
46 RtpReceiverTest()
47 : fake_clock_(123456),
48 rtp_receiver_(
49 RtpReceiver::CreateAudioReceiver(&fake_clock_,
nisse7fcdb6d2017-06-01 00:30:55 -070050 &mock_rtp_data_,
hbos8d609f62017-04-10 07:39:05 -070051 &rtp_payload_registry_)) {
Karl Wibergc62f6c72017-10-04 12:38:53 +020052 rtp_receiver_->RegisterReceivePayload(kPcmuPayloadType,
53 SdpAudioFormat("PCMU", 8000, 1));
hbos8d609f62017-04-10 07:39:05 -070054 }
55 ~RtpReceiverTest() {}
56
57 bool FindSourceByIdAndType(const std::vector<RtpSource>& sources,
58 uint32_t source_id,
59 RtpSourceType type,
60 RtpSource* source) {
61 for (size_t i = 0; i < sources.size(); ++i) {
62 if (sources[i].source_id() == source_id &&
63 sources[i].source_type() == type) {
64 (*source) = sources[i];
65 return true;
66 }
67 }
68 return false;
69 }
70
71 SimulatedClock fake_clock_;
nisse7fcdb6d2017-06-01 00:30:55 -070072 NiceMock<MockRtpData> mock_rtp_data_;
hbos8d609f62017-04-10 07:39:05 -070073 RTPPayloadRegistry rtp_payload_registry_;
74 std::unique_ptr<RtpReceiver> rtp_receiver_;
75};
76
77TEST_F(RtpReceiverTest, GetSources) {
zhihuang04262222017-04-11 11:28:10 -070078 int64_t now_ms = fake_clock_.TimeInMilliseconds();
79
hbos8d609f62017-04-10 07:39:05 -070080 RTPHeader header;
81 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -070082 header.ssrc = kSsrc1;
83 header.timestamp = rtp_timestamp(now_ms);
hbos8d609f62017-04-10 07:39:05 -070084 header.numCSRCs = 2;
zhihuang04262222017-04-11 11:28:10 -070085 header.arrOfCSRCs[0] = kCsrc1;
86 header.arrOfCSRCs[1] = kCsrc2;
Karl Wibergc62f6c72017-10-04 12:38:53 +020087 const PayloadUnion payload_specific{
88 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -070089
zhihuang04262222017-04-11 11:28:10 -070090 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +020091 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -070092 auto sources = rtp_receiver_->GetSources();
93 // One SSRC source and two CSRC sources.
zhihuang04262222017-04-11 11:28:10 -070094 EXPECT_THAT(sources, UnorderedElementsAre(
95 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
96 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
97 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -070098
99 // Advance the fake clock and the method is expected to return the
100 // contributing source object with same source id and updated timestamp.
101 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700102 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200103 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700104 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700105 now_ms = fake_clock_.TimeInMilliseconds();
106 EXPECT_THAT(sources, UnorderedElementsAre(
107 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
108 RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
109 RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700110
111 // Test the edge case that the sources are still there just before the
112 // timeout.
zhihuang04262222017-04-11 11:28:10 -0700113 int64_t prev_time_ms = fake_clock_.TimeInMilliseconds();
hbos8d609f62017-04-10 07:39:05 -0700114 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
115 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700116 EXPECT_THAT(sources,
117 UnorderedElementsAre(
118 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
119 RtpSource(prev_time_ms, kCsrc1, RtpSourceType::CSRC),
120 RtpSource(prev_time_ms, kCsrc2, RtpSourceType::CSRC)));
hbos8d609f62017-04-10 07:39:05 -0700121
122 // Time out.
123 fake_clock_.AdvanceTimeMilliseconds(1);
124 sources = rtp_receiver_->GetSources();
125 // All the sources should be out of date.
126 ASSERT_EQ(0u, sources.size());
127}
128
129// Test the case that the SSRC is changed.
130TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
zhihuang04262222017-04-11 11:28:10 -0700131 int64_t prev_time_ms = -1;
132 int64_t now_ms = fake_clock_.TimeInMilliseconds();
133
hbos8d609f62017-04-10 07:39:05 -0700134 RTPHeader header;
135 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700136 header.ssrc = kSsrc1;
137 header.timestamp = rtp_timestamp(now_ms);
Karl Wibergc62f6c72017-10-04 12:38:53 +0200138 const PayloadUnion payload_specific{
139 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -0700140
zhihuang04262222017-04-11 11:28:10 -0700141 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200142 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700143 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700144 EXPECT_THAT(sources, UnorderedElementsAre(
145 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700146
147 // The SSRC is changed and the old SSRC is expected to be returned.
148 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700149 prev_time_ms = now_ms;
150 now_ms = fake_clock_.TimeInMilliseconds();
151 header.ssrc = kSsrc2;
152 header.timestamp = rtp_timestamp(now_ms);
153 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200154 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700155 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700156 EXPECT_THAT(sources, UnorderedElementsAre(
157 RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
158 RtpSource(now_ms, kSsrc2, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700159
160 // The SSRC is changed again and happen to be changed back to 1. No
161 // duplication is expected.
162 fake_clock_.AdvanceTimeMilliseconds(100);
zhihuang04262222017-04-11 11:28:10 -0700163 header.ssrc = kSsrc1;
164 header.timestamp = rtp_timestamp(now_ms);
165 prev_time_ms = now_ms;
166 now_ms = fake_clock_.TimeInMilliseconds();
167 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200168 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700169 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700170 EXPECT_THAT(sources, UnorderedElementsAre(
171 RtpSource(prev_time_ms, kSsrc2, RtpSourceType::SSRC),
172 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700173
174 // Old SSRC source timeout.
175 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700176 now_ms = fake_clock_.TimeInMilliseconds();
177 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200178 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700179 sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700180 EXPECT_THAT(sources, UnorderedElementsAre(
181 RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
hbos8d609f62017-04-10 07:39:05 -0700182}
183
184TEST_F(RtpReceiverTest, GetSourcesRemoveOutdatedSource) {
zhihuang04262222017-04-11 11:28:10 -0700185 int64_t now_ms = fake_clock_.TimeInMilliseconds();
186
hbos8d609f62017-04-10 07:39:05 -0700187 RTPHeader header;
188 header.payloadType = kPcmuPayloadType;
zhihuang04262222017-04-11 11:28:10 -0700189 header.timestamp = rtp_timestamp(now_ms);
Karl Wibergc62f6c72017-10-04 12:38:53 +0200190 const PayloadUnion payload_specific{
191 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
hbos8d609f62017-04-10 07:39:05 -0700192 header.numCSRCs = 1;
zhihuang04262222017-04-11 11:28:10 -0700193 size_t kSourceListSize = 20;
hbos8d609f62017-04-10 07:39:05 -0700194
zhihuang04262222017-04-11 11:28:10 -0700195 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700196 header.ssrc = i;
197 header.arrOfCSRCs[0] = (i + 1);
Niels Möller22ec9522017-10-05 08:39:15 +0200198 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
199 header, kTestPayload, sizeof(kTestPayload), payload_specific));
hbos8d609f62017-04-10 07:39:05 -0700200 }
201
zhihuang04262222017-04-11 11:28:10 -0700202 RtpSource source(0, 0, RtpSourceType::SSRC);
hbos8d609f62017-04-10 07:39:05 -0700203 auto sources = rtp_receiver_->GetSources();
zhihuang04262222017-04-11 11:28:10 -0700204 // Expect |kSourceListSize| SSRC sources and |kSourceListSize| CSRC sources.
205 ASSERT_EQ(2 * kSourceListSize, sources.size());
206 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700207 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
208 ASSERT_TRUE(
209 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700210 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700211
212 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
213 ASSERT_TRUE(
214 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700215 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700216 }
217
218 fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
zhihuang04262222017-04-11 11:28:10 -0700219 for (size_t i = 0; i < kSourceListSize; ++i) {
hbos8d609f62017-04-10 07:39:05 -0700220 // The SSRC source IDs are expected to be 19, 18, 17 ... 0
221 ASSERT_TRUE(
222 FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700223 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700224
225 // The CSRC source IDs are expected to be 20, 19, 18 ... 1
226 ASSERT_TRUE(
227 FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
zhihuang04262222017-04-11 11:28:10 -0700228 EXPECT_EQ(now_ms, source.timestamp_ms());
hbos8d609f62017-04-10 07:39:05 -0700229 }
230
231 // Timeout. All the existing objects are out of date and are expected to be
232 // removed.
233 fake_clock_.AdvanceTimeMilliseconds(1);
zhihuang04262222017-04-11 11:28:10 -0700234 header.ssrc = kSsrc1;
235 header.arrOfCSRCs[0] = kCsrc1;
236 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200237 header, kTestPayload, sizeof(kTestPayload), payload_specific));
Danil Chapovalovdd7e2842018-03-09 15:37:03 +0000238 auto* rtp_receiver_impl = static_cast<RtpReceiverImpl*>(rtp_receiver_.get());
hbos8d609f62017-04-10 07:39:05 -0700239 auto ssrc_sources = rtp_receiver_impl->ssrc_sources_for_testing();
240 ASSERT_EQ(1u, ssrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700241 EXPECT_EQ(kSsrc1, ssrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700242 EXPECT_EQ(RtpSourceType::SSRC, ssrc_sources.begin()->source_type());
243 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
244 ssrc_sources.begin()->timestamp_ms());
245
246 auto csrc_sources = rtp_receiver_impl->csrc_sources_for_testing();
247 ASSERT_EQ(1u, csrc_sources.size());
zhihuang04262222017-04-11 11:28:10 -0700248 EXPECT_EQ(kCsrc1, csrc_sources.begin()->source_id());
hbos8d609f62017-04-10 07:39:05 -0700249 EXPECT_EQ(RtpSourceType::CSRC, csrc_sources.begin()->source_type());
250 EXPECT_EQ(fake_clock_.TimeInMilliseconds(),
251 csrc_sources.begin()->timestamp_ms());
252}
253
zstein2b706342017-08-24 14:52:17 -0700254// The audio level from the RTPHeader extension should be stored in the
255// RtpSource with the matching SSRC.
256TEST_F(RtpReceiverTest, GetSourcesContainsAudioLevelExtension) {
257 RTPHeader header;
258 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
259 header.payloadType = kPcmuPayloadType;
260 header.ssrc = kSsrc1;
261 header.timestamp = rtp_timestamp(time1_ms);
262 header.extension.hasAudioLevel = true;
263 header.extension.audioLevel = 10;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200264 const PayloadUnion payload_specific{
265 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
zstein2b706342017-08-24 14:52:17 -0700266
267 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200268 header, kTestPayload, sizeof(kTestPayload), payload_specific));
zstein2b706342017-08-24 14:52:17 -0700269 auto sources = rtp_receiver_->GetSources();
270 EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
271 time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
272
273 // Receive a packet from a different SSRC with a different level and check
274 // that they are both remembered.
275 fake_clock_.AdvanceTimeMilliseconds(1);
276 int64_t time2_ms = fake_clock_.TimeInMilliseconds();
277 header.ssrc = kSsrc2;
278 header.timestamp = rtp_timestamp(time2_ms);
279 header.extension.hasAudioLevel = true;
280 header.extension.audioLevel = 20;
281
282 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200283 header, kTestPayload, sizeof(kTestPayload), payload_specific));
zstein2b706342017-08-24 14:52:17 -0700284 sources = rtp_receiver_->GetSources();
285 EXPECT_THAT(sources,
286 UnorderedElementsAre(
287 RtpSource(time1_ms, kSsrc1, RtpSourceType::SSRC, 10),
288 RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
289
290 // Receive a packet from the first SSRC again and check that the level is
291 // updated.
292 fake_clock_.AdvanceTimeMilliseconds(1);
293 int64_t time3_ms = fake_clock_.TimeInMilliseconds();
294 header.ssrc = kSsrc1;
295 header.timestamp = rtp_timestamp(time3_ms);
296 header.extension.hasAudioLevel = true;
297 header.extension.audioLevel = 30;
298
299 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200300 header, kTestPayload, sizeof(kTestPayload), payload_specific));
zstein2b706342017-08-24 14:52:17 -0700301 sources = rtp_receiver_->GetSources();
302 EXPECT_THAT(sources,
303 UnorderedElementsAre(
304 RtpSource(time3_ms, kSsrc1, RtpSourceType::SSRC, 30),
305 RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
306}
307
308TEST_F(RtpReceiverTest,
309 MissingAudioLevelHeaderExtensionClearsRtpSourceAudioLevel) {
310 RTPHeader header;
311 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
312 header.payloadType = kPcmuPayloadType;
313 header.ssrc = kSsrc1;
314 header.timestamp = rtp_timestamp(time1_ms);
315 header.extension.hasAudioLevel = true;
316 header.extension.audioLevel = 10;
Karl Wibergc62f6c72017-10-04 12:38:53 +0200317 const PayloadUnion payload_specific{
318 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
zstein2b706342017-08-24 14:52:17 -0700319
320 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200321 header, kTestPayload, sizeof(kTestPayload), payload_specific));
zstein2b706342017-08-24 14:52:17 -0700322 auto sources = rtp_receiver_->GetSources();
323 EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
324 time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
325
326 // Receive a second packet without the audio level header extension and check
327 // that the audio level is cleared.
328 fake_clock_.AdvanceTimeMilliseconds(1);
329 int64_t time2_ms = fake_clock_.TimeInMilliseconds();
330 header.timestamp = rtp_timestamp(time2_ms);
331 header.extension.hasAudioLevel = false;
332
333 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
Niels Möller22ec9522017-10-05 08:39:15 +0200334 header, kTestPayload, sizeof(kTestPayload), payload_specific));
zstein2b706342017-08-24 14:52:17 -0700335 sources = rtp_receiver_->GetSources();
336 EXPECT_THAT(sources, UnorderedElementsAre(
337 RtpSource(time2_ms, kSsrc1, RtpSourceType::SSRC)));
338}
339
Niels Möller22ec9522017-10-05 08:39:15 +0200340TEST_F(RtpReceiverTest, UpdatesTimestampsIfAndOnlyIfPacketArrivesInOrder) {
341 RTPHeader header;
342 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
343 header.payloadType = kPcmuPayloadType;
344 header.ssrc = kSsrc1;
345 header.timestamp = rtp_timestamp(time1_ms);
346 header.extension.hasAudioLevel = true;
347 header.extension.audioLevel = 10;
348 header.sequenceNumber = 0xfff0;
349
350 const PayloadUnion payload_specific{
351 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
352 uint32_t latest_timestamp;
353 int64_t latest_receive_time_ms;
354
355 // No packet received yet.
356 EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
357 &latest_receive_time_ms));
358 // Initial packet
359 const uint32_t timestamp_1 = header.timestamp;
360 const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds();
361 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
362 header, kTestPayload, sizeof(kTestPayload), payload_specific));
363 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
364 &latest_receive_time_ms));
365 EXPECT_EQ(latest_timestamp, timestamp_1);
366 EXPECT_EQ(latest_receive_time_ms, receive_time_1);
367
368 // Late packet, timestamp not recorded.
369 fake_clock_.AdvanceTimeMilliseconds(10);
370 header.timestamp -= 900;
371 header.sequenceNumber -= 2;
372
373 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
374 header, kTestPayload, sizeof(kTestPayload), payload_specific));
375 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
376 &latest_receive_time_ms));
377 EXPECT_EQ(latest_timestamp, timestamp_1);
378 EXPECT_EQ(latest_receive_time_ms, receive_time_1);
379
380 // New packet, still late, no wraparound.
381 fake_clock_.AdvanceTimeMilliseconds(10);
382 header.timestamp += 1800;
383 header.sequenceNumber += 1;
384
385 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
386 header, kTestPayload, sizeof(kTestPayload), payload_specific));
387 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
388 &latest_receive_time_ms));
389 EXPECT_EQ(latest_timestamp, timestamp_1);
390 EXPECT_EQ(latest_receive_time_ms, receive_time_1);
391
392 // New packet, new timestamp recorded
393 fake_clock_.AdvanceTimeMilliseconds(10);
394 header.timestamp += 900;
395 header.sequenceNumber += 2;
396 const uint32_t timestamp_2 = header.timestamp;
397 const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds();
398 const uint16_t seqno_2 = header.sequenceNumber;
399
400 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
401 header, kTestPayload, sizeof(kTestPayload), payload_specific));
402 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
403 &latest_receive_time_ms));
404 EXPECT_EQ(latest_timestamp, timestamp_2);
405 EXPECT_EQ(latest_receive_time_ms, receive_time_2);
406
407 // New packet, timestamp wraps around
408 fake_clock_.AdvanceTimeMilliseconds(10);
409 header.timestamp += 900;
410 header.sequenceNumber += 20;
411 const uint32_t timestamp_3 = header.timestamp;
412 const int64_t receive_time_3 = fake_clock_.TimeInMilliseconds();
413 EXPECT_LT(header.sequenceNumber, seqno_2); // Wrap-around
414
415 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
416 header, kTestPayload, sizeof(kTestPayload), payload_specific));
417 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
418 &latest_receive_time_ms));
419 EXPECT_EQ(latest_timestamp, timestamp_3);
420 EXPECT_EQ(latest_receive_time_ms, receive_time_3);
421}
422
423TEST_F(RtpReceiverTest, UpdatesTimestampsWhenStreamResets) {
424 RTPHeader header;
425 int64_t time1_ms = fake_clock_.TimeInMilliseconds();
426 header.payloadType = kPcmuPayloadType;
427 header.ssrc = kSsrc1;
428 header.timestamp = rtp_timestamp(time1_ms);
429 header.extension.hasAudioLevel = true;
430 header.extension.audioLevel = 10;
431 header.sequenceNumber = 0xfff0;
432
433 const PayloadUnion payload_specific{
434 AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
435 uint32_t latest_timestamp;
436 int64_t latest_receive_time_ms;
437
438 // No packet received yet.
439 EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
440 &latest_receive_time_ms));
441 // Initial packet
442 const uint32_t timestamp_1 = header.timestamp;
443 const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds();
444 const uint16_t seqno_1 = header.sequenceNumber;
445 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
446 header, kTestPayload, sizeof(kTestPayload), payload_specific));
447 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
448 &latest_receive_time_ms));
449 EXPECT_EQ(latest_timestamp, timestamp_1);
450 EXPECT_EQ(latest_receive_time_ms, receive_time_1);
451
452 // Packet with far in the past seqno, but unlikely to be a wrap-around.
453 // Treated as a seqno discontinuity, and timestamp is recorded.
454 fake_clock_.AdvanceTimeMilliseconds(10);
455 header.timestamp += 900;
456 header.sequenceNumber = 0x9000;
457
458 const uint32_t timestamp_2 = header.timestamp;
459 const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds();
460 const uint16_t seqno_2 = header.sequenceNumber;
461 EXPECT_LT(seqno_1 - seqno_2, 0x8000); // In the past.
462
463 EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
464 header, kTestPayload, sizeof(kTestPayload), payload_specific));
465 EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
466 &latest_receive_time_ms));
467 EXPECT_EQ(latest_timestamp, timestamp_2);
468 EXPECT_EQ(latest_receive_time_ms, receive_time_2);
469}
470
hbos8d609f62017-04-10 07:39:05 -0700471} // namespace webrtc