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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/rtp_rtcp/source/rtp_receiver_unittest.cc
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_receiver_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtp_receiver_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..00ddee2b4406ac038761c8cb8320bad0d845afb7
--- /dev/null
+++ b/webrtc/modules/rtp_rtcp/source/rtp_receiver_unittest.cc
@@ -0,0 +1,221 @@
+/*
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <memory>
+
+#include "webrtc/common_types.h"
+#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
+#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
+#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
+#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
+#include "webrtc/test/gtest.h"
+
+namespace webrtc {
+
+const uint32_t kTestRate = 64000u;
+const uint8_t kTestPayload[] = {'t', 'e', 's', 't'};
+const uint8_t kPcmuPayloadType = 96;
+const int64_t kGetSourcesTimeoutMs = 10000;
+const int kMaxSourceListsSize = 100;
+
+class RtpReceiverTest : public ::testing::Test {
+ protected:
+ RtpReceiverTest() : fake_clock(123456) {}
+ ~RtpReceiverTest() {}
+
+ 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.
+ rtp_payload_registry_.reset(new RTPPayloadRegistry());
+
+ rtp_receiver_.reset(RtpReceiver::CreateAudioReceiver(
+ &fake_clock, nullptr, nullptr, rtp_payload_registry_.get()));
+
+ CodecInst voice_codec = {};
+ voice_codec.pltype = kPcmuPayloadType;
+ voice_codec.plfreq = 8000;
+ voice_codec.rate = kTestRate;
+ memcpy(voice_codec.plname, "PCMU", 5);
+ rtp_receiver_->RegisterReceivePayload(voice_codec);
+ }
+
+ 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.
+ std::unique_ptr<RtpReceiver> rtp_receiver_;
+ 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.
+};
+
+TEST_F(RtpReceiverTest, GetSources) {
+ int64_t timestamp = fake_clock.TimeInMilliseconds();
+ RTPHeader header;
+ header.payloadType = kPcmuPayloadType;
+ header.ssrc = 1;
+ header.timestamp = timestamp;
+ header.numCSRCs = 2;
+ header.arrOfCSRCs[0] = 111;
+ header.arrOfCSRCs[1] = 222;
+ PayloadUnion payload_specific = {AudioPayload()};
+ bool in_order = false;
+
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ auto sources = rtp_receiver_->GetSources();
+ // One SSRC source and two CSRC sources.
+ ASSERT_EQ(3u, sources.size());
+ 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
+ EXPECT_EQ(timestamp, sources[0].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+ EXPECT_EQ(222u, sources[1].source_id());
+ EXPECT_EQ(timestamp, sources[1].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
+ EXPECT_EQ(111u, sources[2].source_id());
+ EXPECT_EQ(timestamp, sources[2].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[2].source_type());
+
+ // Advance the fake clock and the method is expected to return the
+ // contributing source object with same |source| and updated |timestamp()|.
+ fake_clock.AdvanceTimeMilliseconds(1);
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(3u, sources.size());
+ EXPECT_EQ(1u, sources[0].source_id());
+ EXPECT_EQ(timestamp + 1, sources[0].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+ EXPECT_EQ(222u, sources[1].source_id());
+ EXPECT_EQ(timestamp + 1, sources[1].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
+ EXPECT_EQ(111u, sources[2].source_id());
+ EXPECT_EQ(timestamp + 1, sources[2].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[2].source_type());
+
+ // Simulate the time out.
+ 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.
+ sources = rtp_receiver_->GetSources();
+ // All the sources should be out of date.
+ ASSERT_EQ(0u, sources.size());
+}
+
+// Test the case that the SSRC is changed.
+TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
+ int64_t prev_time = -1;
+ int64_t cur_time = fake_clock.TimeInMilliseconds();
+ RTPHeader header;
+ header.payloadType = kPcmuPayloadType;
+ header.ssrc = 1;
+ header.timestamp = cur_time;
+ PayloadUnion payload_specific = {AudioPayload()};
+ bool in_order = false;
+
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ auto sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(1u, sources.size());
+ EXPECT_EQ(1u, sources[0].source_id());
+ EXPECT_EQ(cur_time, sources[0].timestamp());
+
+ // The SSRC is changed and the old SSRC is expected to be returned.
+ fake_clock.AdvanceTimeMilliseconds(100);
+ prev_time = cur_time;
+ cur_time = fake_clock.TimeInMilliseconds();
+ header.ssrc = 2;
+ header.timestamp = cur_time;
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(2u, sources.size());
+ EXPECT_EQ(2u, sources[0].source_id());
+ EXPECT_EQ(cur_time, sources[0].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+ EXPECT_EQ(1u, sources[1].source_id());
+ EXPECT_EQ(prev_time, sources[1].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[1].source_type());
+
+ // The SSRC is changed again and happen to be changed back to 1. No
+ // duplication is expected.
+ fake_clock.AdvanceTimeMilliseconds(100);
+ header.ssrc = 1;
+ header.timestamp = cur_time;
+ prev_time = cur_time;
+ cur_time = fake_clock.TimeInMilliseconds();
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(2u, sources.size());
+ EXPECT_EQ(1u, sources[0].source_id());
+ EXPECT_EQ(cur_time, sources[0].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+ EXPECT_EQ(2u, sources[1].source_id());
+ EXPECT_EQ(prev_time, sources[1].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[1].source_type());
+
+ // Old SSRC source timeout.
+ fake_clock.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
+ cur_time = fake_clock.TimeInMilliseconds();
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(1u, sources.size());
+ EXPECT_EQ(1u, sources[0].source_id());
+ EXPECT_EQ(cur_time, sources[0].timestamp());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+}
+
+// Test that the out of date objects will be removed when the source lists are
+// too large.
+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
+ int64_t timestamp = fake_clock.TimeInMilliseconds();
+ bool in_order = false;
+ RTPHeader header;
+ header.payloadType = kPcmuPayloadType;
+ header.timestamp = timestamp;
+ PayloadUnion payload_specific = {AudioPayload()};
+ header.numCSRCs = 1;
+
+ for (size_t i = 0; i < kMaxSourceListsSize; ++i) {
+ header.ssrc = i;
+ header.arrOfCSRCs[0] = (i + 1);
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ }
+ auto sources = rtp_receiver_->GetSources();
+ // Expect |kMaxSourceListsSize| SSRC sources and |kMaxSourceListsSize| CSRC
+ // sources.
+ ASSERT_TRUE(sources.size() == 2 * kMaxSourceListsSize);
+ for (size_t i = 0; i < kMaxSourceListsSize; ++i) {
+ // The SSRC source IDs are expected to be 99, 98, 97 ... 0
+ EXPECT_EQ(kMaxSourceListsSize - i - 1, sources[i].source_id());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[i].source_type());
+ EXPECT_EQ(timestamp, sources[i].timestamp());
+
+ // The CSRC source IDs are expected to be 100, 99, 98 ... 1
+ EXPECT_EQ(kMaxSourceListsSize - i,
+ sources[i + kMaxSourceListsSize].source_id());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE,
+ sources[i + kMaxSourceListsSize].source_type());
+ EXPECT_EQ(timestamp, sources[i + kMaxSourceListsSize].timestamp());
+ }
+
+ // Timeout. All the existing objects are out of date and are expected to be
+ // removed.
+ fake_clock.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs + 1);
+ header.ssrc = 111;
+ header.arrOfCSRCs[0] = 222;
+ EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(header, kTestPayload, 4,
+ payload_specific, in_order));
+ sources = rtp_receiver_->GetSources();
+ ASSERT_EQ(2u, sources.size());
+ EXPECT_EQ(111u, sources[0].source_id());
+ EXPECT_EQ(RtpSourceType::RTP_SSRC_SOURCE, sources[0].source_type());
+ EXPECT_EQ(fake_clock.TimeInMilliseconds(), sources[0].timestamp());
+
+ EXPECT_EQ(222u, sources[1].source_id());
+ EXPECT_EQ(RtpSourceType::RTP_CSRC_SOURCE, sources[1].source_type());
+ EXPECT_EQ(fake_clock.TimeInMilliseconds(), sources[1].timestamp());
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698