Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fake timers for unit tests #1058

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "system_wrappers/source/field_trial.h"
#include "modules/congestion_controller/goog_cc/goog_cc_network_control.h"

#include "DepLibUV.hpp"
#include "handles/Timer.hpp"
#include "Logger.hpp"
#include "RTC/RTCP/FeedbackRtpTransport.hpp"

Expand All @@ -36,7 +36,7 @@ TargetRateConstraints ConvertConstraints(int min_bitrate_bps,
int max_bitrate_bps,
int start_bitrate_bps) {
TargetRateConstraints msg;
msg.at_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
msg.at_time = Timestamp::ms(GetTimeMsInt64());
msg.min_data_rate =
min_bitrate_bps >= 0 ? DataRate::bps(min_bitrate_bps) : DataRate::Zero();
msg.max_data_rate = max_bitrate_bps > 0 ? DataRate::bps(max_bitrate_bps)
Expand All @@ -63,7 +63,7 @@ RtpTransportControllerSend::RtpTransportControllerSend(
observer_(nullptr),
controller_factory_override_(controller_factory),
process_interval_(controller_factory_override_->GetProcessInterval()),
last_report_block_time_(Timestamp::ms(DepLibUV::GetTimeMsInt64())),
last_report_block_time_(Timestamp::ms(GetTimeMsInt64())),
send_side_bwe_with_overhead_(
webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
transport_overhead_bytes_per_packet_(0),
Expand Down Expand Up @@ -147,7 +147,7 @@ void RtpTransportControllerSend::OnNetworkAvailability(bool network_available) {
MS_DEBUG_DEV("<<<<< network_available:%s", network_available ? "true" : "false");

NetworkAvailability msg;
msg.at_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
msg.at_time = Timestamp::ms(GetTimeMsInt64());
msg.network_available = network_available;

if (network_available_ == msg.network_available)
Expand Down Expand Up @@ -200,7 +200,7 @@ void RtpTransportControllerSend::OnReceivedEstimatedBitrate(uint32_t bitrate) {
MS_DEBUG_DEV("<<<<< bitrate:%zu", bitrate);

RemoteBitrateReport msg;
msg.receive_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
msg.receive_time = Timestamp::ms(GetTimeMsInt64());
msg.bandwidth = DataRate::bps(bitrate);

PostUpdates(controller_->OnRemoteBitrateReport(msg));
Expand Down Expand Up @@ -228,7 +228,7 @@ void RtpTransportControllerSend::OnAddPacket(
packet_info,
send_side_bwe_with_overhead_ ? transport_overhead_bytes_per_packet_.load()
: 0,
Timestamp::ms(DepLibUV::GetTimeMsInt64()));
Timestamp::ms(GetTimeMsInt64()));
}

void RtpTransportControllerSend::OnTransportFeedback(
Expand All @@ -237,7 +237,7 @@ void RtpTransportControllerSend::OnTransportFeedback(

absl::optional<TransportPacketsFeedback> feedback_msg =
transport_feedback_adapter_.ProcessTransportFeedback(
feedback, Timestamp::ms(DepLibUV::GetTimeMsInt64()));
feedback, Timestamp::ms(GetTimeMsInt64()));
if (feedback_msg)
PostUpdates(controller_->OnTransportPacketsFeedback(*feedback_msg));
pacer_.UpdateOutstandingData(
Expand All @@ -246,7 +246,7 @@ void RtpTransportControllerSend::OnTransportFeedback(

void RtpTransportControllerSend::OnRemoteNetworkEstimate(
NetworkStateEstimate estimate) {
estimate.update_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
estimate.update_time = Timestamp::ms(GetTimeMsInt64());
controller_->OnNetworkStateEstimate(estimate);
}

Expand All @@ -268,7 +268,7 @@ void RtpTransportControllerSend::MaybeCreateControllers() {
control_handler_ = absl::make_unique<CongestionControlHandler>();

initial_config_.constraints.at_time =
Timestamp::ms(DepLibUV::GetTimeMsInt64());
Timestamp::ms(GetTimeMsInt64());

controller_ = controller_factory_override_->Create(initial_config_);
process_interval_ = controller_factory_override_->GetProcessInterval();
Expand All @@ -283,15 +283,15 @@ void RtpTransportControllerSend::UpdateControllerWithTimeInterval() {
MS_ASSERT(controller_, "controller not set");

ProcessInterval msg;
msg.at_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
msg.at_time = Timestamp::ms(GetTimeMsInt64());

PostUpdates(controller_->OnProcessInterval(msg));
}

void RtpTransportControllerSend::UpdateStreamsConfig() {
MS_DEBUG_DEV("<<<<<");

streams_config_.at_time = Timestamp::ms(DepLibUV::GetTimeMsInt64());
streams_config_.at_time = Timestamp::ms(GetTimeMsInt64());
if (controller_)
PostUpdates(controller_->OnStreamsConfig(streams_config_));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "modules/congestion_controller/goog_cc/alr_detector.h"
#include "rtc_base/numerics/safe_conversions.h"

#include "DepLibUV.hpp"
#include "handles/Timer.hpp"
#include "Logger.hpp"

#include <absl/memory/memory.h>
Expand Down Expand Up @@ -85,7 +85,7 @@ void AlrDetector::OnBytesSent(size_t bytes_sent, int64_t send_time_ms) {
bool state_changed = false;
if (alr_budget_.budget_ratio() > start_budget_level_ratio_ &&
!alr_started_time_ms_) {
alr_started_time_ms_.emplace(DepLibUV::GetTimeMsInt64());
alr_started_time_ms_.emplace(GetTimeMsInt64());
state_changed = true;
} else if (alr_budget_.budget_ratio() < stop_budget_level_ratio_ &&
alr_started_time_ms_) {
Expand Down
20 changes: 9 additions & 11 deletions worker/deps/libwebrtc/libwebrtc/modules/pacing/paced_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "system_wrappers/source/field_trial.h" // webrtc::field_trial.

#include "DepLibUV.hpp"
#include "handles/Timer.hpp"
#include "Logger.hpp"
#include "RTC/RtpPacket.hpp"

Expand Down Expand Up @@ -53,7 +53,7 @@ PacedSender::PacedSender(PacketRouter* packet_router,
prober_(*field_trials_),
probing_send_failure_(false),
pacing_bitrate_kbps_(0),
time_last_process_us_(DepLibUV::GetTimeUsInt64()),
time_last_process_us_(GetTimeUsInt64()),
first_sent_packet_ms_(-1),
packet_counter_(0),
account_for_audio_(false) {
Expand All @@ -66,7 +66,7 @@ void PacedSender::CreateProbeCluster(int bitrate_bps, int cluster_id) {
// TODO: REMOVE
// MS_DEBUG_DEV("---- bitrate_bps:%d, cluster_id:%d", bitrate_bps, cluster_id);

prober_.CreateProbeCluster(bitrate_bps, DepLibUV::GetTimeMsInt64(), cluster_id);
prober_.CreateProbeCluster(bitrate_bps, GetTimeMsInt64(), cluster_id);
}

void PacedSender::Pause() {
Expand Down Expand Up @@ -147,15 +147,15 @@ void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {

int64_t PacedSender::TimeUntilNextProcess() {
int64_t elapsed_time_us =
DepLibUV::GetTimeUsInt64() - time_last_process_us_;
GetTimeUsInt64() - time_last_process_us_;
int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
// When paused we wake up every 500 ms to send a padding packet to ensure
// we won't get stuck in the paused state due to no feedback being received.
if (paused_)
return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0);

if (prober_.IsProbing()) {
int64_t ret = prober_.TimeUntilNextProbe(DepLibUV::GetTimeMsInt64());
int64_t ret = prober_.TimeUntilNextProbe(GetTimeMsInt64());
if (ret > 0 || (ret == 0 && !probing_send_failure_))
return ret;
}
Expand All @@ -176,7 +176,7 @@ int64_t PacedSender::UpdateTimeAndGetElapsedMs(int64_t now_us) {
}

void PacedSender::Process() {
int64_t now_us = DepLibUV::GetTimeUsInt64();
int64_t now_us = GetTimeUsInt64();
int64_t elapsed_time_ms = UpdateTimeAndGetElapsedMs(now_us);

if (paused_)
Expand Down Expand Up @@ -230,10 +230,8 @@ void PacedSender::Process() {

if (bytes_sent != 0)
{
auto now = DepLibUV::GetTimeUsInt64();

OnPaddingSent(now, bytes_sent);
prober_.ProbeSent((now + 500) / 1000, bytes_sent);
OnPaddingSent(now_us, bytes_sent);
prober_.ProbeSent((now_us + 500) / 1000, bytes_sent);
}
}

Expand Down Expand Up @@ -265,7 +263,7 @@ size_t PacedSender::PaddingBytesToAdd(

void PacedSender::OnPacketSent(size_t size) {
if (first_sent_packet_ms_ == -1)
first_sent_packet_ms_ = DepLibUV::GetTimeMsInt64();
first_sent_packet_ms_ = GetTimeMsInt64();

// Update media bytes sent.
UpdateBudgetWithBytesSent(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "rtc_base/constructor_magic.h"

#include "Logger.hpp"
#include "DepLibUV.hpp"
#include "handles/Timer.hpp"

#include <math.h>
#include <algorithm>
Expand Down Expand Up @@ -251,7 +251,7 @@ void RemoteBitrateEstimatorAbsSendTime::IncomingPacketInfo(
uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift;
int64_t send_time_ms = static_cast<int64_t>(timestamp) * kTimestampToMs;

int64_t now_ms = DepLibUV::GetTimeMsInt64();
int64_t now_ms = GetTimeMsInt64();
// TODO(holmer): SSRCs are only needed for REMB, should be broken out from
// here.

Expand Down
1 change: 0 additions & 1 deletion worker/include/RTC/RateCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define MS_RTC_RATE_CALCULATOR_HPP

#include "common.hpp"
#include "DepLibUV.hpp"
#include "RTC/RtpPacket.hpp"

namespace RTC
Expand Down
48 changes: 44 additions & 4 deletions worker/include/handles/Timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@
#include "common.hpp"
#include <uv.h>

#ifdef FAKE_TIMERS

#include <map>

class FakeTimerManager
{
public:
static uint16_t GetNexTimerId();
static void StartTimer(uint16_t timerId, uint64_t timeout, uint64_t repeat, std::function<void()> cb);
static void StopTimer(uint16_t timerId);
static bool IsActive(uint16_t timerId);

static int64_t GetTimeMs()
{
return now;
}
static int64_t NextTimerTime();
static void RunPending(int64_t now);
static void RunLoop(int64_t maxTime = -1);

private:
struct Timer
{
uint64_t nextTime;
uint64_t repeat;
std::function<void()> cb;
};

static int64_t now;
static uint16_t nextTimerId;
static std::map<uint16_t, Timer> timers;
};
#endif

class Timer
{
public:
Expand Down Expand Up @@ -36,10 +70,7 @@ class Timer
{
return this->repeat;
}
bool IsActive() const
{
return uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0;
}
bool IsActive() const;

/* Callbacks fired by UV events. */
public:
Expand All @@ -48,12 +79,21 @@ class Timer
private:
// Passed by argument.
Listener* listener{ nullptr };
#ifdef FAKE_TIMERS
uint64_t timerId;
#else
// Allocated by this.
uv_timer_t* uvHandle{ nullptr };
#endif
// Others.
bool closed{ false };
uint64_t timeout{ 0u };
uint64_t repeat{ 0u };
};

uint64_t GetTimeMs();
int64_t GetTimeMsInt64();
uint64_t GetTimeUs();
int64_t GetTimeUsInt64();

#endif
1 change: 1 addition & 0 deletions worker/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ mediasoup_worker_test = executable(
cpp_args: cpp_args + [
'-DMS_LOG_STD',
'-DMS_TEST',
'-DFAKE_TIMERS',
],
)

Expand Down
3 changes: 2 additions & 1 deletion worker/src/RTC/RateCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "RTC/RateCalculator.hpp"
#include "Logger.hpp"
#include "handles/Timer.hpp"
#include <cmath> // std::trunc()

namespace RTC
Expand Down Expand Up @@ -116,7 +117,7 @@ namespace RTC

void RtpDataCounter::Update(RTC::RtpPacket* packet)
{
const uint64_t nowMs = DepLibUV::GetTimeMs();
const uint64_t nowMs = GetTimeMs();

this->packets++;
this->rate.Update(packet->GetSize(), nowMs);
Expand Down
11 changes: 5 additions & 6 deletions worker/src/RTC/TransportCongestionControlClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define USE_TREND_CALCULATOR

#include "RTC/TransportCongestionControlClient.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include <libwebrtc/api/transport/network_types.h> // webrtc::TargetRateConstraints
Expand Down Expand Up @@ -114,7 +113,7 @@ namespace RTC
MS_TRACE();

#ifdef USE_TREND_CALCULATOR
auto nowMs = DepLibUV::GetTimeMsInt64();
auto nowMs = GetTimeMsInt64();
#endif

this->bitrates.desiredBitrate = 0u;
Expand Down Expand Up @@ -306,7 +305,7 @@ namespace RTC
MS_TRACE();

#ifdef USE_TREND_CALCULATOR
auto nowMs = DepLibUV::GetTimeMsInt64();
auto nowMs = GetTimeMsInt64();
#endif

// Manage it via trending and increase it a bit to avoid immediate oscillations.
Expand Down Expand Up @@ -408,7 +407,7 @@ namespace RTC

webrtc::TargetRateConstraints constraints;

constraints.at_time = webrtc::Timestamp::ms(DepLibUV::GetTimeMs());
constraints.at_time = webrtc::Timestamp::ms(GetTimeMs());
constraints.min_data_rate = webrtc::DataRate::bps(this->bitrates.minBitrate);
constraints.max_data_rate = webrtc::DataRate::bps(this->bitrates.maxBitrate);
constraints.starting_rate = webrtc::DataRate::bps(this->bitrates.startBitrate);
Expand All @@ -434,14 +433,14 @@ namespace RTC
{
MS_TRACE();

this->lastAvailableBitrateEventAtMs = DepLibUV::GetTimeMs();
this->lastAvailableBitrateEventAtMs = GetTimeMs();
}

void TransportCongestionControlClient::MayEmitAvailableBitrateEvent(uint32_t previousAvailableBitrate)
{
MS_TRACE();

const uint64_t nowMs = DepLibUV::GetTimeMsInt64();
const uint64_t nowMs = GetTimeMsInt64();
bool notify{ false };

// Ignore if first event.
Expand Down
3 changes: 1 addition & 2 deletions worker/src/RTC/TransportCongestionControlServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// #define MS_LOG_DEV_LEVEL 3

#include "RTC/TransportCongestionControlServer.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "RTC/RTCP/FeedbackPsRemb.hpp"
#include <iterator> // std::ostream_iterator
Expand Down Expand Up @@ -275,7 +274,7 @@ namespace RTC
{
MS_TRACE();

auto nowMs = DepLibUV::GetTimeMs();
auto nowMs = GetTimeMs();

// May fix unlimitedRembCounter.
if (this->unlimitedRembCounter > 0u && this->maxIncomingBitrate != 0u)
Expand Down