Add 1 sec timer to ModuleRtpRtcpImpl2 instead of frequent polling.

This reduces the number of times we grab a few locks down from
somewhere upwards of around a thousand time a second to a few times.

* Update the RTT value on the worker thread and fire callbacks.
* Trigger NotifyTmmbrUpdated() calls from the worker.
* Update the tests to use a GlobalSimulatedTimeController.

Change-Id: Ib81582494066b9460ae0aa84271f32311f30fbce
Bug: webrtc:11581
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/177664
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31602}
This commit is contained in:
Tomas Gunnarsson
2020-07-01 08:53:21 +02:00
committed by Commit Bot
parent 20f45823e3
commit ba0ba71e93
8 changed files with 254 additions and 197 deletions

View File

@ -27,6 +27,7 @@
#include "test/rtcp_packet_parser.h"
#include "test/rtp_header_parser.h"
#include "test/run_loop.h"
#include "test/time_controller/simulated_time_controller.h"
using ::testing::ElementsAre;
@ -53,14 +54,14 @@ class SendTransport : public Transport {
public:
SendTransport()
: receiver_(nullptr),
clock_(nullptr),
time_controller_(nullptr),
delay_ms_(0),
rtp_packets_sent_(0),
rtcp_packets_sent_(0) {}
void SetRtpRtcpModule(ModuleRtpRtcpImpl2* receiver) { receiver_ = receiver; }
void SimulateNetworkDelay(int64_t delay_ms, SimulatedClock* clock) {
clock_ = clock;
void SimulateNetworkDelay(int64_t delay_ms, TimeController* time_controller) {
time_controller_ = time_controller;
delay_ms_ = delay_ms;
}
bool SendRtp(const uint8_t* data,
@ -78,17 +79,19 @@ class SendTransport : public Transport {
parser.Parse(data, len);
last_nack_list_ = parser.nack()->packet_ids();
if (clock_) {
clock_->AdvanceTimeMilliseconds(delay_ms_);
if (time_controller_) {
time_controller_->AdvanceTime(TimeDelta::Millis(delay_ms_));
}
EXPECT_TRUE(receiver_);
receiver_->IncomingRtcpPacket(data, len);
++rtcp_packets_sent_;
return true;
}
size_t NumRtcpSent() { return rtcp_packets_sent_; }
ModuleRtpRtcpImpl2* receiver_;
SimulatedClock* clock_;
TimeController* time_controller_;
int64_t delay_ms_;
int rtp_packets_sent_;
size_t rtcp_packets_sent_;
@ -98,12 +101,13 @@ class SendTransport : public Transport {
class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
public:
RtpRtcpModule(SimulatedClock* clock, bool is_sender)
RtpRtcpModule(TimeController* time_controller, bool is_sender)
: is_sender_(is_sender),
receive_statistics_(ReceiveStatistics::Create(clock)),
clock_(clock) {
receive_statistics_(
ReceiveStatistics::Create(time_controller->GetClock())),
time_controller_(time_controller) {
CreateModuleImpl();
transport_.SimulateNetworkDelay(kOneWayNetworkDelayMs, clock);
transport_.SimulateNetworkDelay(kOneWayNetworkDelayMs, time_controller);
}
const bool is_sender_;
@ -146,7 +150,7 @@ class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
void CreateModuleImpl() {
RtpRtcpInterface::Configuration config;
config.audio = false;
config.clock = clock_;
config.clock = time_controller_->GetClock();
config.outgoing_transport = &transport_;
config.receive_statistics = receive_statistics_.get();
config.rtcp_packet_type_counter_observer = this;
@ -160,7 +164,7 @@ class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
impl_->SetRTCPStatus(RtcpMode::kCompound);
}
SimulatedClock* const clock_;
TimeController* const time_controller_;
std::map<uint32_t, RtcpPacketTypeCounter> counter_map_;
};
} // namespace
@ -168,9 +172,9 @@ class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
class RtpRtcpImpl2Test : public ::testing::Test {
protected:
RtpRtcpImpl2Test()
: clock_(133590000000000),
sender_(&clock_, /*is_sender=*/true),
receiver_(&clock_, /*is_sender=*/false) {}
: time_controller_(Timestamp::Micros(133590000000000)),
sender_(&time_controller_, /*is_sender=*/true),
receiver_(&time_controller_, /*is_sender=*/false) {}
void SetUp() override {
// Send module.
@ -181,7 +185,7 @@ class RtpRtcpImpl2Test : public ::testing::Test {
FieldTrialBasedConfig field_trials;
RTPSenderVideo::Config video_config;
video_config.clock = &clock_;
video_config.clock = time_controller_.GetClock();
video_config.rtp_sender = sender_.impl_->RtpSender();
video_config.field_trials = &field_trials;
sender_video_ = std::make_unique<RTPSenderVideo>(video_config);
@ -199,8 +203,13 @@ class RtpRtcpImpl2Test : public ::testing::Test {
receiver_.transport_.SetRtpRtcpModule(sender_.impl_.get());
}
test::RunLoop loop_;
SimulatedClock clock_;
void AdvanceTimeMs(int64_t milliseconds) {
time_controller_.AdvanceTime(TimeDelta::Millis(milliseconds));
}
GlobalSimulatedTimeController time_controller_;
// test::RunLoop loop_;
// SimulatedClock clock_;
RtpRtcpModule sender_;
std::unique_ptr<RTPSenderVideo> sender_video_;
RtpRtcpModule receiver_;
@ -256,7 +265,7 @@ TEST_F(RtpRtcpImpl2Test, RetransmitsAllLayers) {
EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
// Min required delay until retransmit = 5 + RTT ms (RTT = 0).
clock_.AdvanceTimeMilliseconds(5);
AdvanceTimeMs(5);
// Frame with kBaseLayerTid re-sent.
IncomingRtcpNack(&sender_, kSequenceNumber);
@ -286,7 +295,7 @@ TEST_F(RtpRtcpImpl2Test, Rtt) {
EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
// Receiver module should send a RR with a response to the last received SR.
clock_.AdvanceTimeMilliseconds(1000);
AdvanceTimeMs(1000);
EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
// Verify RTT.
@ -308,7 +317,8 @@ TEST_F(RtpRtcpImpl2Test, Rtt) {
// Verify RTT from rtt_stats config.
EXPECT_EQ(0, sender_.rtt_stats_.LastProcessedRtt());
EXPECT_EQ(0, sender_.impl_->rtt_ms());
sender_.impl_->Process();
AdvanceTimeMs(1000);
EXPECT_NEAR(2 * kOneWayNetworkDelayMs, sender_.rtt_stats_.LastProcessedRtt(),
1);
EXPECT_NEAR(2 * kOneWayNetworkDelayMs, sender_.impl_->rtt_ms(), 1);
@ -327,7 +337,7 @@ TEST_F(RtpRtcpImpl2Test, RttForReceiverOnly) {
EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
// Sender module should send a response to the last received RTRR (DLRR).
clock_.AdvanceTimeMilliseconds(1000);
AdvanceTimeMs(1000);
// Send Frame before sending a SR.
SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
@ -335,7 +345,7 @@ TEST_F(RtpRtcpImpl2Test, RttForReceiverOnly) {
// Verify RTT.
EXPECT_EQ(0, receiver_.rtt_stats_.LastProcessedRtt());
EXPECT_EQ(0, receiver_.impl_->rtt_ms());
receiver_.impl_->Process();
AdvanceTimeMs(1000);
EXPECT_NEAR(2 * kOneWayNetworkDelayMs,
receiver_.rtt_stats_.LastProcessedRtt(), 1);
EXPECT_NEAR(2 * kOneWayNetworkDelayMs, receiver_.impl_->rtt_ms(), 1);
@ -343,16 +353,16 @@ TEST_F(RtpRtcpImpl2Test, RttForReceiverOnly) {
TEST_F(RtpRtcpImpl2Test, NoSrBeforeMedia) {
// Ignore fake transport delays in this test.
sender_.transport_.SimulateNetworkDelay(0, &clock_);
receiver_.transport_.SimulateNetworkDelay(0, &clock_);
sender_.transport_.SimulateNetworkDelay(0, &time_controller_);
receiver_.transport_.SimulateNetworkDelay(0, &time_controller_);
sender_.impl_->Process();
EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
// Verify no SR is sent before media has been sent, RR should still be sent
// from the receiving module though.
clock_.AdvanceTimeMilliseconds(2000);
int64_t current_time = clock_.TimeInMilliseconds();
AdvanceTimeMs(2000);
int64_t current_time = time_controller_.GetClock()->TimeInMilliseconds();
sender_.impl_->Process();
receiver_.impl_->Process();
EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
@ -460,7 +470,7 @@ TEST_F(RtpRtcpImpl2Test, SendsExtendedNackList) {
}
TEST_F(RtpRtcpImpl2Test, ReSendsNackListAfterRttMs) {
sender_.transport_.SimulateNetworkDelay(0, &clock_);
sender_.transport_.SimulateNetworkDelay(0, &time_controller_);
// Send module sends a NACK.
const uint16_t kNackLength = 2;
uint16_t nack_list[kNackLength] = {123, 125};
@ -473,19 +483,19 @@ TEST_F(RtpRtcpImpl2Test, ReSendsNackListAfterRttMs) {
// Same list not re-send, rtt interval has not passed.
const int kStartupRttMs = 100;
clock_.AdvanceTimeMilliseconds(kStartupRttMs);
AdvanceTimeMs(kStartupRttMs);
EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
// Rtt interval passed, full list sent.
clock_.AdvanceTimeMilliseconds(1);
AdvanceTimeMs(1);
EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
EXPECT_EQ(2U, sender_.RtcpSent().nack_packets);
EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123, 125));
}
TEST_F(RtpRtcpImpl2Test, UniqueNackRequests) {
receiver_.transport_.SimulateNetworkDelay(0, &clock_);
receiver_.transport_.SimulateNetworkDelay(0, &time_controller_);
EXPECT_EQ(0U, receiver_.RtcpSent().nack_packets);
EXPECT_EQ(0U, receiver_.RtcpSent().nack_requests);
EXPECT_EQ(0U, receiver_.RtcpSent().unique_nack_requests);
@ -508,7 +518,7 @@ TEST_F(RtpRtcpImpl2Test, UniqueNackRequests) {
// Receive module sends new request with duplicated packets.
const int kStartupRttMs = 100;
clock_.AdvanceTimeMilliseconds(kStartupRttMs + 1);
AdvanceTimeMs(kStartupRttMs + 1);
const uint16_t kNackLength2 = 4;
uint16_t nack_list2[kNackLength2] = {11, 18, 20, 21};
EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list2, kNackLength2));
@ -539,13 +549,13 @@ TEST_F(RtpRtcpImpl2Test, ConfigurableRtcpReportInterval) {
EXPECT_EQ(0u, sender_.transport_.NumRtcpSent());
// Move ahead to the last ms before a rtcp is expected, no action.
clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2 - 1);
AdvanceTimeMs(kVideoReportInterval / 2 - 1);
sender_.impl_->Process();
EXPECT_EQ(sender_.RtcpSent().first_packet_time_ms, -1);
EXPECT_EQ(sender_.transport_.NumRtcpSent(), 0u);
// Move ahead to the first rtcp. Send RTCP.
clock_.AdvanceTimeMilliseconds(1);
AdvanceTimeMs(1);
sender_.impl_->Process();
EXPECT_GT(sender_.RtcpSent().first_packet_time_ms, -1);
EXPECT_EQ(sender_.transport_.NumRtcpSent(), 1u);
@ -553,21 +563,21 @@ TEST_F(RtpRtcpImpl2Test, ConfigurableRtcpReportInterval) {
SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
// Move ahead to the last possible second before second rtcp is expected.
clock_.AdvanceTimeMilliseconds(kVideoReportInterval * 1 / 2 - 1);
AdvanceTimeMs(kVideoReportInterval * 1 / 2 - 1);
sender_.impl_->Process();
EXPECT_EQ(sender_.transport_.NumRtcpSent(), 1u);
// Move ahead into the range of second rtcp, the second rtcp may be sent.
clock_.AdvanceTimeMilliseconds(1);
AdvanceTimeMs(1);
sender_.impl_->Process();
EXPECT_GE(sender_.transport_.NumRtcpSent(), 1u);
clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2);
AdvanceTimeMs(kVideoReportInterval / 2);
sender_.impl_->Process();
EXPECT_GE(sender_.transport_.NumRtcpSent(), 1u);
// Move out the range of second rtcp, the second rtcp must have been sent.
clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2);
AdvanceTimeMs(kVideoReportInterval / 2);
sender_.impl_->Process();
EXPECT_EQ(sender_.transport_.NumRtcpSent(), 2u);
}
@ -588,7 +598,7 @@ TEST_F(RtpRtcpImpl2Test, StoresPacketInfoForSentPackets) {
packet.set_first_packet_of_frame(true);
packet.SetMarker(true);
sender_.impl_->TrySendPacket(&packet, pacing_info);
loop_.Flush();
AdvanceTimeMs(1);
std::vector<RtpSequenceNumberMap::Info> seqno_info =
sender_.impl_->GetSentRtpPacketInfos(std::vector<uint16_t>{1});
@ -613,7 +623,7 @@ TEST_F(RtpRtcpImpl2Test, StoresPacketInfoForSentPackets) {
packet.SetMarker(true);
sender_.impl_->TrySendPacket(&packet, pacing_info);
loop_.Flush();
AdvanceTimeMs(1);
seqno_info =
sender_.impl_->GetSentRtpPacketInfos(std::vector<uint16_t>{2, 3, 4});