tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit eddcea11477c96b1ee696e89fd28d5e44229a64f
parent 3a9e56050ddfd92177cd565b031c6da9498da161
Author: Dan Baker <dbaker@mozilla.com>
Date:   Mon, 27 Oct 2025 15:24:47 -0600

Bug 1995393 - Vendor libwebrtc from e2a20f38cc

Upstream commit: https://webrtc.googlesource.com/src/+/e2a20f38cc0054946924917d45a487a3ab3e7ced
    Change Connection api to prefer strong time types in the api

    Use injected clock in Connection, P2PTransportChannel and TcpPort

    Bug: webrtc:42223992, webrtc:42223979
    Change-Id: I5cb6f1712572fa9b6081079aaac6fdd81a667828
    Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/406764
    Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
    Reviewed-by: Per Kjellander <perkj@webrtc.org>
    Cr-Commit-Position: refs/heads/main@{#45487}

Diffstat:
Mthird_party/libwebrtc/README.mozilla.last-vendor | 4++--
Mthird_party/libwebrtc/p2p/BUILD.gn | 17++++++++++++-----
Mthird_party/libwebrtc/p2p/base/connection.cc | 197+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mthird_party/libwebrtc/p2p/base/connection.h | 157++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mthird_party/libwebrtc/p2p/base/p2p_constants.cc | 20++++++++++----------
Mthird_party/libwebrtc/p2p/base/p2p_constants.h | 38++++++++++++++++++++++++++++----------
Mthird_party/libwebrtc/p2p/base/p2p_transport_channel.cc | 37+++++++++++++++++++------------------
Mthird_party/libwebrtc/p2p/base/p2p_transport_channel.h | 9+++++----
Mthird_party/libwebrtc/p2p/base/tcp_port.cc | 6+++---
9 files changed, 301 insertions(+), 184 deletions(-)

diff --git a/third_party/libwebrtc/README.mozilla.last-vendor b/third_party/libwebrtc/README.mozilla.last-vendor @@ -1,4 +1,4 @@ # ./mach python dom/media/webrtc/third_party_build/vendor-libwebrtc.py --from-local /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc --commit mozpatches libwebrtc -libwebrtc updated from /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc commit mozpatches on 2025-10-27T21:21:58.658860+00:00. +libwebrtc updated from /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc commit mozpatches on 2025-10-27T21:24:35.218510+00:00. # base of lastest vendoring -f941eb1014 +e2a20f38cc diff --git a/third_party/libwebrtc/p2p/BUILD.gn b/third_party/libwebrtc/p2p/BUILD.gn @@ -220,6 +220,7 @@ rtc_library("connection") { "../rtc_base:net_helpers", "../rtc_base:network", "../rtc_base:network_constants", + "../rtc_base:platform_thread_types", "../rtc_base:rate_tracker", "../rtc_base:rtc_numerics", "../rtc_base:safe_minmax", @@ -448,7 +449,12 @@ rtc_library("p2p_constants") { "base/p2p_constants.cc", "base/p2p_constants.h", ] - deps = [ "../rtc_base/system:rtc_export" ] + deps = [ + "../api/units:data_rate", + "../api/units:data_size", + "../api/units:time_delta", + "../rtc_base/system:rtc_export", + ] } rtc_library("p2p_transport_channel") { @@ -491,6 +497,7 @@ rtc_library("p2p_transport_channel") { "../api/transport:enums", "../api/transport:stun_types", "../api/units:time_delta", + "../api/units:timestamp", "../logging:ice_log", "../rtc_base:async_packet_socket", "../rtc_base:checks", @@ -505,6 +512,7 @@ rtc_library("p2p_transport_channel") { "../rtc_base:network", "../rtc_base:network_constants", "../rtc_base:network_route", + "../rtc_base:platform_thread_types", "../rtc_base:socket", "../rtc_base:socket_address", "../rtc_base:stringutils", @@ -645,7 +653,6 @@ rtc_library("port_interface") { "../rtc_base:callback_list", "../rtc_base:dscp", "../rtc_base:network", - "../rtc_base:network", "../rtc_base:socket", "../rtc_base:socket_address", "../rtc_base/network:sent_packet", @@ -715,12 +722,10 @@ rtc_library("dtls_stun_piggyback_controller") { "../api:array_view", "../api:sequence_checker", "../api/transport:stun_types", - "../api/transport:stun_types", "../rtc_base:buffer", "../rtc_base:byte_buffer", "../rtc_base:checks", "../rtc_base:logging", - "../rtc_base:logging", "../rtc_base:macromagic", "../rtc_base:stringutils", "../rtc_base/system:no_unique_address", @@ -812,6 +817,7 @@ rtc_library("tcp_port") { "../api/task_queue:pending_task_safety_flag", "../api/transport:stun_types", "../api/units:time_delta", + "../api/units:timestamp", "../rtc_base:async_packet_socket", "../rtc_base:checks", "../rtc_base:ip_address", @@ -1116,7 +1122,6 @@ if (rtc_include_tests) { "../api/units:timestamp", "../rtc_base:async_packet_socket", "../rtc_base:async_udp_socket", - "../rtc_base:async_udp_socket", "../rtc_base:buffer", "../rtc_base:byte_order", "../rtc_base:checks", @@ -1241,6 +1246,7 @@ if (rtc_include_tests) { "../api/transport:stun_types", "../api/units:data_rate", "../api/units:time_delta", + "../api/units:timestamp", "../rtc_base:async_packet_socket", "../rtc_base:async_tcp_socket", "../rtc_base:async_udp_socket", @@ -1261,6 +1267,7 @@ if (rtc_include_tests) { "../rtc_base:network", "../rtc_base:network_constants", "../rtc_base:network_route", + "../rtc_base:platform_thread_types", "../rtc_base:rtc_base_tests_utils", "../rtc_base:rtc_event", "../rtc_base:socket", diff --git a/third_party/libwebrtc/p2p/base/connection.cc b/third_party/libwebrtc/p2p/base/connection.cc @@ -30,6 +30,7 @@ #include "api/sequence_checker.h" #include "api/task_queue/task_queue_base.h" #include "api/transport/stun.h" +#include "api/units/time_delta.h" #include "api/units/timestamp.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" @@ -48,13 +49,11 @@ #include "rtc_base/crypto_random.h" #include "rtc_base/logging.h" #include "rtc_base/net_helper.h" -#include "rtc_base/net_helpers.h" #include "rtc_base/network.h" #include "rtc_base/network/received_packet.h" #include "rtc_base/network/sent_packet.h" #include "rtc_base/network_constants.h" -#include "rtc_base/numerics/safe_minmax.h" -#include "rtc_base/socket.h" +#include "rtc_base/platform_thread_types.h" #include "rtc_base/socket_address.h" #include "rtc_base/string_encode.h" #include "rtc_base/string_utils.h" @@ -70,15 +69,15 @@ namespace { inline bool TooManyFailures( const std::vector<Connection::SentPing>& pings_since_last_response, uint32_t maximum_failures, - int rtt_estimate, - int64_t now) { + TimeDelta rtt_estimate, + Timestamp now) { // If we haven't sent that many pings, then we can't have failed that many. if (pings_since_last_response.size() < maximum_failures) return false; // Check if the window in which we would expect a response to the ping has // already elapsed. - int64_t expected_response_time = + Timestamp expected_response_time = pings_since_last_response[maximum_failures - 1].sent_time + rtt_estimate; return now > expected_response_time; } @@ -86,8 +85,8 @@ inline bool TooManyFailures( // Determines whether we have gone too long without seeing any response. inline bool TooLongWithoutResponse( const std::vector<Connection::SentPing>& pings_since_last_response, - int64_t maximum_time, - int64_t now) { + TimeDelta maximum_time, + Timestamp now) { if (pings_since_last_response.empty()) return false; @@ -154,18 +153,18 @@ IceCandidateNetworkType ConvertNetworkType(AdapterType type) { // When we don't have any RTT data, we have to pick something reasonable. We // use a large value just in case the connection is really slow. -const int DEFAULT_RTT = 3000; // 3 seconds +constexpr TimeDelta kDefaultRtt = TimeDelta::Seconds(3); // We will restrict RTT estimates (when used for determining state) to be // within a reasonable range. -const int MINIMUM_RTT = 100; // 0.1 seconds -const int MAXIMUM_RTT = 60000; // 60 seconds +constexpr TimeDelta kMinimumRtt = TimeDelta::Millis(100); +constexpr TimeDelta kMaximumRtt = TimeDelta::Seconds(60); -const int DEFAULT_RTT_ESTIMATE_HALF_TIME_MS = 500; +constexpr int kDefaultRttEstimateHalfTimeMs = 500; // Weighting of the old rtt value to new data. -const int RTT_RATIO = 3; // 3 : 1 +constexpr int kRttRatio = 3; // 3 : 1 -constexpr int64_t kMinExtraPingDelayMs = 100; +constexpr TimeDelta kMinExtraPingDelay = TimeDelta::Millis(100); // Default field trials. const IceFieldTrials kDefaultFieldTrials; @@ -239,6 +238,7 @@ Connection::Connection(const Environment& env, remote_candidate_(remote_candidate), recv_rate_tracker_(100, 10u), send_rate_tracker_(100, 10u), + last_send_data_(Timestamp::Zero()), write_state_(STATE_WRITE_INIT), receiving_(false), connected_(true), @@ -248,16 +248,18 @@ Connection::Connection(const Environment& env, [this](const void* data, size_t size, StunRequest* request) { OnSendStunPacket(data, size, request); }), - rtt_(DEFAULT_RTT), - last_ping_sent_(0), - last_ping_received_(0), - last_data_received_(0), - last_ping_response_received_(0), + rtt_(kDefaultRtt), + last_ping_sent_(Timestamp::Zero()), + last_ping_received_(Timestamp::Zero()), + last_data_received_(Timestamp::Zero()), + last_ping_response_received_(Timestamp::Zero()), + receiving_unchanged_since_(Timestamp::Zero()), state_(IceCandidatePairState::WAITING), - time_created_ms_(TimeMillis()), - delta_internal_unix_epoch_ms_(TimeUTCMillis() - time_created_ms_), + time_created_(AlignTime(env_.clock().CurrentTime())), + delta_internal_unix_epoch_(Timestamp::Millis(TimeUTCMillis()) - + time_created_), field_trials_(&kDefaultFieldTrials), - rtt_estimate_(DEFAULT_RTT_ESTIMATE_HALF_TIME_MS) { + rtt_estimate_(kDefaultRttEstimateHalfTimeMs) { RTC_DCHECK_RUN_ON(network_thread_); RTC_DCHECK(port_); RTC_LOG(LS_INFO) << ToString() << ": Connection created"; @@ -332,10 +334,11 @@ void Connection::set_write_state(WriteState value) { } } -void Connection::UpdateReceiving(int64_t now) { +void Connection::UpdateReceiving(Timestamp now) { + now = AlignTime(now); RTC_DCHECK_RUN_ON(network_thread_); bool receiving; - if (last_ping_sent() < last_ping_response_received()) { + if (LastPingSent() < LastPingResponseReceived()) { // We consider any candidate pair that has its last connectivity check // acknowledged by a response as receiving, particularly for backup // candidate pairs that send checks at a much slower pace than the selected @@ -346,8 +349,8 @@ void Connection::UpdateReceiving(int64_t now) { // IceConfig.ice_connection_receiving_timeout and their default value. receiving = true; } else { - receiving = - last_received() > 0 && now <= last_received() + receiving_timeout(); + receiving = LastReceived() > Timestamp::Zero() && + now <= LastReceived() + ReceivingTimeout(); } if (receiving_ == receiving) { return; @@ -402,14 +405,14 @@ bool Connection::nominated() const { return acked_nomination_ || remote_nomination_; } -int Connection::unwritable_timeout() const { +TimeDelta Connection::UnwritableTimeout() const { RTC_DCHECK_RUN_ON(network_thread_); - return unwritable_timeout_.value_or(CONNECTION_WRITE_CONNECT_TIMEOUT); + return unwritable_timeout_.value_or(kConnectionWriteConnectTimeout); } -void Connection::set_unwritable_timeout(const std::optional<int>& value_ms) { +void Connection::SetUnwritableTimeout(std::optional<TimeDelta> value) { RTC_DCHECK_RUN_ON(network_thread_); - unwritable_timeout_ = value_ms; + unwritable_timeout_ = value; } int Connection::unwritable_min_checks() const { @@ -422,25 +425,25 @@ void Connection::set_unwritable_min_checks(const std::optional<int>& value) { unwritable_min_checks_ = value; } -int Connection::inactive_timeout() const { +TimeDelta Connection::InactiveTimeout() const { RTC_DCHECK_RUN_ON(network_thread_); - return inactive_timeout_.value_or(CONNECTION_WRITE_TIMEOUT); + return inactive_timeout_.value_or(kConnectionWriteTimeout); } -void Connection::set_inactive_timeout(const std::optional<int>& value) { +void Connection::SetInactiveTimeout(std::optional<TimeDelta> value) { RTC_DCHECK_RUN_ON(network_thread_); inactive_timeout_ = value; } -int Connection::receiving_timeout() const { +TimeDelta Connection::ReceivingTimeout() const { RTC_DCHECK_RUN_ON(network_thread_); - return receiving_timeout_.value_or(WEAK_CONNECTION_RECEIVE_TIMEOUT); + return receiving_timeout_.value_or(kWeakConnectionReceiveTimeout); } -void Connection::set_receiving_timeout( - std::optional<int> receiving_timeout_ms) { +void Connection::SetReceivingTimeout( + std::optional<TimeDelta> receiving_timeout) { RTC_DCHECK_RUN_ON(network_thread_); - receiving_timeout_ = receiving_timeout_ms; + receiving_timeout_ = receiving_timeout; } void Connection::SetIceFieldTrials(const IceFieldTrials* field_trials) { @@ -494,7 +497,7 @@ void Connection::OnReadPacket(const ReceivedIpPacket& packet) { packet.payload().size(), addr, &msg, &remote_ufrag)) { // The packet did not parse as a valid STUN message // This is a data packet, pass it along. - last_data_received_ = TimeMillis(); + last_data_received_ = AlignTime(env_.clock().CurrentTime()); UpdateReceiving(last_data_received_); recv_rate_tracker_.AddSamples(packet.payload().size()); stats_.packets_received++; @@ -673,11 +676,12 @@ void Connection::HandleStunBindingOrGoogPingRequest(IceMessage* msg) { RTC_DCHECK_RUN_ON(network_thread_); // This connection should now be receiving. ReceivedPing(msg->transaction_id()); - if (field_trials_->extra_ice_ping && last_ping_response_received_ == 0) { + if (field_trials_->extra_ice_ping && + last_ping_response_received_ <= Timestamp::Zero()) { if (local_candidate().is_relay() || local_candidate().is_prflx() || remote_candidate().is_relay() || remote_candidate().is_prflx()) { - const int64_t now = TimeMillis(); - if (last_ping_sent_ + kMinExtraPingDelayMs <= now) { + const Timestamp now = AlignTime(env_.clock().CurrentTime()); + if (last_ping_sent_ + kMinExtraPingDelay <= now) { RTC_LOG(LS_INFO) << ToString() << "WebRTC-ExtraICEPing/Sending extra ping" " last_ping_sent_: " @@ -997,14 +1001,15 @@ void Connection::set_selected(bool selected) { selected_ = selected; } -void Connection::UpdateState(int64_t now) { +void Connection::UpdateState(Timestamp now) { + now = AlignTime(now); RTC_DCHECK_RUN_ON(network_thread_); RTC_DCHECK(port_) << ToDebugId() << ": port_ null in UpdateState()"; if (!port_) return; // Computes our estimate of the RTT given the current estimate. - int rtt = SafeClamp(2 * rtt_, MINIMUM_RTT, MAXIMUM_RTT); + TimeDelta rtt = std::clamp(2 * rtt_, kMinimumRtt, kMaximumRtt); if (RTC_LOG_CHECK_LEVEL(LS_VERBOSE)) { std::string pings; @@ -1030,7 +1035,7 @@ void Connection::UpdateState(int64_t now) { if ((write_state_ == STATE_WRITABLE) && TooManyFailures(pings_since_last_response_, unwritable_min_checks(), rtt, now) && - TooLongWithoutResponse(pings_since_last_response_, unwritable_timeout(), + TooLongWithoutResponse(pings_since_last_response_, UnwritableTimeout(), now)) { uint32_t max_pings = unwritable_min_checks(); RTC_LOG(LS_INFO) << ToString() << ": Unwritable after " << max_pings @@ -1045,7 +1050,7 @@ void Connection::UpdateState(int64_t now) { } if ((write_state_ == STATE_WRITE_UNRELIABLE || write_state_ == STATE_WRITE_INIT) && - TooLongWithoutResponse(pings_since_last_response_, inactive_timeout(), + TooLongWithoutResponse(pings_since_last_response_, InactiveTimeout(), now)) { RTC_LOG(LS_INFO) << ToString() << ": Timed out after " << now - pings_since_last_response_[0].sent_time @@ -1069,13 +1074,18 @@ void Connection::UpdateLocalIceParameters(int component, local_candidate_.set_password(password); } -int64_t Connection::last_ping_sent() const { +Timestamp Connection::LastPingSent() const { RTC_DCHECK_RUN_ON(network_thread_); return last_ping_sent_; } -void Connection::Ping(int64_t now, +void Connection::Ping() { + Ping(env_.clock().CurrentTime()); +} + +void Connection::Ping(Timestamp now, std::unique_ptr<StunByteStringAttribute> delta) { + now = AlignTime(now); RTC_DCHECK_RUN_ON(network_thread_); RTC_DCHECK(port_) << ToDebugId() << ": port_ null in Ping()"; if (!port_) @@ -1182,7 +1192,7 @@ std::unique_ptr<IceMessage> Connection::BuildPingRequest( return message; } -int64_t Connection::last_ping_response_received() const { +Timestamp Connection::LastPingResponseReceived() const { RTC_DCHECK_RUN_ON(network_thread_); return last_ping_response_received_; } @@ -1200,14 +1210,14 @@ int Connection::rtt_samples() const { // Called whenever a valid ping is received on this connection. This is // public because the connection intercepts the first ping for us. -int64_t Connection::last_ping_received() const { +Timestamp Connection::LastPingReceived() const { RTC_DCHECK_RUN_ON(network_thread_); return last_ping_received_; } void Connection::ReceivedPing(const std::optional<std::string>& request_id) { RTC_DCHECK_RUN_ON(network_thread_); - last_ping_received_ = TimeMillis(); + last_ping_received_ = AlignTime(env_.clock().CurrentTime()); last_ping_id_received_ = request_id; UpdateReceiving(last_ping_received_); } @@ -1229,28 +1239,29 @@ void Connection::HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg) { RTC_LOG_V(sev) << ToString() << ": Received piggyback STUN ping response, id=" << hex_encode(request_id); - const int64_t rtt = TimeMillis() - iter->sent_time; + const TimeDelta rtt = + AlignTime(env_.clock().CurrentTime()) - iter->sent_time; ReceivedPingResponse(rtt, request_id, iter->nomination); } } } -int64_t Connection::last_send_data() const { +Timestamp Connection::LastSendData() const { RTC_DCHECK_RUN_ON(network_thread_); return last_send_data_; } -int64_t Connection::last_data_received() const { +Timestamp Connection::LastDataReceived() const { RTC_DCHECK_RUN_ON(network_thread_); return last_data_received_; } void Connection::ReceivedPingResponse( - int rtt, + TimeDelta rtt, absl::string_view /* request_id */, const std::optional<uint32_t>& nomination) { RTC_DCHECK_RUN_ON(network_thread_); - RTC_DCHECK_GE(rtt, 0); + RTC_DCHECK_GE(rtt, TimeDelta::Zero()); // We've already validated that this is a STUN binding response with // the correct local and remote username for this connection. // So if we're not already, become writable. We may be bringing a pruned @@ -1260,10 +1271,10 @@ void Connection::ReceivedPingResponse( acked_nomination_ = nomination.value(); } - int64_t now = TimeMillis(); - total_round_trip_time_ms_ += rtt; - current_round_trip_time_ms_ = static_cast<uint32_t>(rtt); - rtt_estimate_.AddSample(now, rtt); + Timestamp now = AlignTime(env_.clock().CurrentTime()); + total_round_trip_time_ += rtt; + current_round_trip_time_ = rtt; + rtt_estimate_.AddSample(now.ms(), rtt.ms()); pings_since_last_response_.clear(); last_ping_response_received_ = now; @@ -1273,7 +1284,7 @@ void Connection::ReceivedPingResponse( // Smooth the RTT estimate using a moving average. if (rtt_samples_ > 0) { - rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1); + rtt_ = (kRttRatio * rtt_ + rtt) / (kRttRatio + 1); } else { rtt_ = rtt; } @@ -1311,9 +1322,10 @@ bool Connection::active() const { return write_state_ != STATE_WRITE_TIMEOUT; } -bool Connection::dead(int64_t now) const { +bool Connection::dead(Timestamp now) const { + now = AlignTime(now); RTC_DCHECK_RUN_ON(network_thread_); - if (last_received() > 0) { + if (LastReceived() > Timestamp::Zero()) { // If it has ever received anything, we keep it alive // - if it has recevied last DEAD_CONNECTION_RECEIVE_TIMEOUT (30s) // - if it has a ping outstanding shorter than @@ -1325,7 +1337,7 @@ bool Connection::dead(int64_t now) const { // locally inactive (pruned) connection. This also allows the local agent to // ping with longer interval than 30s as long as it shorter than // `dead_connection_timeout_ms`. - if (now <= (last_received() + DEAD_CONNECTION_RECEIVE_TIMEOUT)) { + if (now <= (LastReceived() + kDeadConnectionReceiveTimeout)) { // Not dead since we have received the last 30s. return false; } @@ -1333,12 +1345,14 @@ bool Connection::dead(int64_t now) const { // Outstanding pings: let it live until the ping is unreplied for // DEAD_CONNECTION_RECEIVE_TIMEOUT. return now > (pings_since_last_response_[0].sent_time + - DEAD_CONNECTION_RECEIVE_TIMEOUT); + kDeadConnectionReceiveTimeout); } // No outstanding pings: let it live until // field_trials_->dead_connection_timeout_ms has passed. - return now > (last_received() + field_trials_->dead_connection_timeout_ms); + return now > + LastReceived() + + TimeDelta::Millis(field_trials_->dead_connection_timeout_ms); } if (active()) { @@ -1353,15 +1367,15 @@ bool Connection::dead(int64_t now) const { // keep it around for at least MIN_CONNECTION_LIFETIME to prevent connections // from being pruned too quickly during a network change event when two // networks would be up simultaneously but only for a brief period. - return now > (time_created_ms_ + MIN_CONNECTION_LIFETIME); + return now > (time_created_ + kMinConnectionLifetime); } -int Connection::rtt() const { +TimeDelta Connection::Rtt() const { RTC_DCHECK_RUN_ON(network_thread_); return rtt_; } -bool Connection::stable(int64_t now) const { +bool Connection::stable(Timestamp now) const { // A connection is stable if it's RTT has converged and it isn't missing any // responses. We should send pings at a higher rate until the RTT converges // and whenever a ping response is missing (so that we can detect @@ -1435,8 +1449,8 @@ std::string Connection::ToString() const { if (port_) ss << priority() << "|"; - if (rtt_ < DEFAULT_RTT) { - ss << rtt_ << "]"; + if (rtt_ < kDefaultRtt) { + ss << rtt_.ms() << "]"; } else { ss << "-]"; } @@ -1499,7 +1513,7 @@ void Connection::OnConnectionRequestResponse(StunRequest* request, // connection. LoggingSeverity sev = !writable() ? LS_INFO : LS_VERBOSE; - int rtt = request->Elapsed(); + TimeDelta rtt = TimeDelta::Millis(request->Elapsed()); if (RTC_LOG_CHECK_LEVEL_V(sev)) { std::string pings; @@ -1680,13 +1694,13 @@ void Connection::MaybeUpdatePeerReflexiveCandidate( } } -int64_t Connection::last_received() const { +Timestamp Connection::LastReceived() const { RTC_DCHECK_RUN_ON(network_thread_); - return std::max(last_data_received_, - std::max(last_ping_received_, last_ping_response_received_)); + return std::max( + {last_data_received_, last_ping_received_, last_ping_response_received_}); } -int64_t Connection::receiving_unchanged_since() const { +Timestamp Connection::ReceivingUnchangedSince() const { RTC_DCHECK_RUN_ON(network_thread_); return receiving_unchanged_since_; } @@ -1715,7 +1729,7 @@ ConnectionInfo Connection::stats() { stats_.receiving = receiving_; stats_.writable = write_state_ == STATE_WRITABLE; stats_.timeout = write_state_ == STATE_WRITE_TIMEOUT; - stats_.rtt = rtt_; + stats_.rtt = rtt_.ms(); stats_.key = this; stats_.state = state_; if (port_) { @@ -1723,16 +1737,18 @@ ConnectionInfo Connection::stats() { stats_.local_candidate = local_candidate(); } stats_.nominated = nominated(); - stats_.total_round_trip_time_ms = total_round_trip_time_ms_; - stats_.current_round_trip_time_ms = current_round_trip_time_ms_; + stats_.total_round_trip_time_ms = total_round_trip_time_.ms(); + stats_.current_round_trip_time_ms = + current_round_trip_time_.has_value() + ? std::optional(current_round_trip_time_->ms<uint32_t>()) + : std::nullopt; stats_.remote_candidate = remote_candidate(); - if (last_data_received_ > 0) { + if (last_data_received_ > Timestamp::Zero()) { stats_.last_data_received = - Timestamp::Millis(last_data_received_ + delta_internal_unix_epoch_ms_); + last_data_received_ + delta_internal_unix_epoch_; } - if (last_send_data_ > 0) { - stats_.last_data_sent = - Timestamp::Millis(last_send_data_ + delta_internal_unix_epoch_ms_); + if (last_send_data_ > Timestamp::Zero()) { + stats_.last_data_sent = last_send_data_ + delta_internal_unix_epoch_; } return stats_; } @@ -1808,17 +1824,18 @@ void Connection::MaybeUpdateLocalCandidate(StunRequest* request, bool Connection::rtt_converged() const { RTC_DCHECK_RUN_ON(network_thread_); - return rtt_samples_ > (RTT_RATIO + 1); + return rtt_samples_ > (kRttRatio + 1); } -bool Connection::missing_responses(int64_t now) const { +bool Connection::missing_responses(Timestamp now) const { RTC_DCHECK_RUN_ON(network_thread_); + now = AlignTime(now); if (pings_since_last_response_.empty()) { return false; } - int64_t waiting = now - pings_since_last_response_[0].sent_time; - return waiting > 2 * rtt(); + TimeDelta waiting = now - pings_since_last_response_[0].sent_time; + return waiting > 2 * Rtt(); } bool Connection::TooManyOutstandingPings( @@ -1891,14 +1908,14 @@ int ProxyConnection::Send(const void* data, mutable_stats().sent_total_packets++; int sent = port()->SendTo(data, size, remote_candidate().address(), options, true); - int64_t now = TimeMillis(); + Timestamp now = env().clock().CurrentTime(); if (sent <= 0) { RTC_DCHECK(sent < 0); error_ = port()->GetError(); mutable_stats().sent_discarded_packets++; mutable_stats().sent_discarded_bytes += size; } else { - send_rate_tracker().AddSamplesAtTime(now, sent); + send_rate_tracker().AddSamplesAtTime(now.ms(), sent); } set_last_send_data(now); return sent; diff --git a/third_party/libwebrtc/p2p/base/connection.h b/third_party/libwebrtc/p2p/base/connection.h @@ -28,6 +28,8 @@ #include "api/sequence_checker.h" #include "api/task_queue/task_queue_base.h" #include "api/transport/stun.h" +#include "api/units/time_delta.h" +#include "api/units/timestamp.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" #include "logging/rtc_event_log/ice_logger.h" @@ -56,19 +58,20 @@ constexpr int kGoogPingVersion = 1; // 1200 is the "commonly used" MTU. Subtract M-I attribute (20+4) and FP (4+4). constexpr int kMaxStunBindingLength = 1200 - 24 - 8; -// Forward declaration so that a ConnectionRequest can contain a Connection. -class Connection; +// TODO: bugs.webrtc.org/42223979 - Delete or mark deprecated functions that +// use integers to represent time when remaining WebRTC is updated to use +// Timestamp and TimeDelta types instead. // Represents a communication link between a port on the local client and a // port on the remote client. class RTC_EXPORT Connection : public CandidatePairInterface { public: struct SentPing { - SentPing(absl::string_view id, int64_t sent_time, uint32_t nomination) + SentPing(absl::string_view id, Timestamp sent_time, uint32_t nomination) : id(id), sent_time(sent_time), nomination(nomination) {} std::string id; - int64_t sent_time; + Timestamp sent_time; uint32_t nomination; }; @@ -117,17 +120,36 @@ class RTC_EXPORT Connection : public CandidatePairInterface { bool pending_delete() const { return !port_; } // A connection is dead if it can be safely deleted. - bool dead(int64_t now) const; + bool dead(Timestamp now) const; // Estimate of the round-trip time over this connection. - int rtt() const; - - int unwritable_timeout() const; - void set_unwritable_timeout(const std::optional<int>& value_ms); + // [[deprecated("bugs.webrtc.org/42223979")]] + int rtt() const { return Rtt().ms(); } + TimeDelta Rtt() const; + + TimeDelta UnwritableTimeout() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + void set_unwritable_timeout(const std::optional<int>& value_ms) { + if (value_ms.has_value()) { + SetUnwritableTimeout(TimeDelta::Millis(*value_ms)); + } else { + SetUnwritableTimeout(std::nullopt); + } + } + void SetUnwritableTimeout(std::optional<TimeDelta> value); int unwritable_min_checks() const; void set_unwritable_min_checks(const std::optional<int>& value); - int inactive_timeout() const; - void set_inactive_timeout(const std::optional<int>& value); + + // [[deprecated("bugs.webrtc.org/42223979")]] + void set_inactive_timeout(const std::optional<int>& value) { + if (value.has_value()) { + SetInactiveTimeout(TimeDelta::Millis(*value)); + } else { + SetInactiveTimeout(std::nullopt); + } + } + TimeDelta InactiveTimeout() const; + void SetInactiveTimeout(std::optional<TimeDelta> value); // Gets the `ConnectionInfo` stats, where `best_connection` has not been // populated (default value false). @@ -187,8 +209,16 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // when receiving a response to a nominating ping. bool nominated() const; - int receiving_timeout() const; - void set_receiving_timeout(std::optional<int> receiving_timeout_ms); + TimeDelta ReceivingTimeout() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + void set_receiving_timeout(std::optional<int> receiving_timeout_ms) { + if (receiving_timeout_ms.has_value()) { + SetReceivingTimeout(TimeDelta::Millis(*receiving_timeout_ms)); + } else { + SetReceivingTimeout(std::nullopt); + } + } + void SetReceivingTimeout(std::optional<TimeDelta> receiving_timeout); // Deletes a `Connection` instance is by calling the `DestroyConnection` // method in `Port`. @@ -208,25 +238,47 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // Checks that the state of this connection is up-to-date. The argument is // the current time, which is compared against various timeouts. - void UpdateState(int64_t now); + // [[deprecated("bugs.webrtc.org/42223979")]] + void UpdateState(int64_t now) { UpdateState(Timestamp::Millis(now)); } + void UpdateState(Timestamp now); void UpdateLocalIceParameters(int component, absl::string_view username_fragment, absl::string_view password); // Called when this connection should try checking writability again. - int64_t last_ping_sent() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + int64_t last_ping_sent() const { return LastPingSent().ms(); } + Timestamp LastPingSent() const; + + // [[deprecated("bugs.webrtc.org/42223979")]] void Ping(int64_t now, + std::unique_ptr<StunByteStringAttribute> delta = nullptr) { + Ping(Timestamp::Millis(now), std::move(delta)); + } + void Ping(); + void Ping(Timestamp now, std::unique_ptr<StunByteStringAttribute> delta = nullptr); + // [[deprecated("bugs.webrtc.org/42223979")]] void ReceivedPingResponse( int rtt, absl::string_view request_id, + const std::optional<uint32_t>& nomination = std::nullopt) { + ReceivedPingResponse(TimeDelta::Millis(rtt), request_id, nomination); + } + void ReceivedPingResponse( + TimeDelta rtt, + absl::string_view request_id, const std::optional<uint32_t>& nomination = std::nullopt); std::unique_ptr<IceMessage> BuildPingRequest( std::unique_ptr<StunByteStringAttribute> delta) RTC_RUN_ON(network_thread_); - int64_t last_ping_response_received() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + int64_t last_ping_response_received() const { + return LastPingResponseReceived().ms(); + } + Timestamp LastPingResponseReceived() const; const std::optional<std::string>& last_ping_id_received() const; // Used to check if any STUN ping response has been received. @@ -234,7 +286,9 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // Called whenever a valid ping is received on this connection. This is // public because the connection intercepts the first ping for us. - int64_t last_ping_received() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + int64_t last_ping_received() const { return LastPingReceived().ms(); } + Timestamp LastPingReceived() const; void ReceivedPing( const std::optional<std::string>& request_id = std::nullopt); @@ -245,8 +299,10 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // connectivity check from the peer. void HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg); // Timestamp when data was last sent (or attempted to be sent). - int64_t last_send_data() const; - int64_t last_data_received() const; + Timestamp LastSendData() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + int64_t last_data_received() const { return LastDataReceived().ms(); } + Timestamp LastDataReceived() const; // Debugging description of this connection std::string ToDebugId() const; @@ -288,15 +344,22 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // Returns the last received time of any data, stun request, or stun // response in milliseconds - int64_t last_received() const; + Timestamp LastReceived() const; + // Returns the last time when the connection changed its receiving state. - int64_t receiving_unchanged_since() const; + // [[deprecated("bugs.webrtc.org/42223979")]] + int64_t receiving_unchanged_since() const { + return ReceivingUnchangedSince().ms(); + } + Timestamp ReceivingUnchangedSince() const; // Constructs the prflx priority as described in // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 uint32_t prflx_priority() const; - bool stable(int64_t now) const; + // [[deprecated("bugs.webrtc.org/42223979")]] + bool stable(int64_t now) const { return stable(Timestamp::Millis(now)); } + bool stable(Timestamp now) const; // Check if we sent `val` pings without receving a response. bool TooManyOutstandingPings(const std::optional<int>& val) const; @@ -367,6 +430,16 @@ class RTC_EXPORT Connection : public CandidatePairInterface { void DeregisterDtlsPiggyback() { dtls_stun_piggyback_callbacks_.reset(); } + // TODO: bugs.webrtc.org/439515766 - Make this helper an identity or remove it + // when all users provide time queried from `Clock` and passed around with + // 'Timestamp' type. Connection class is sensative to current time rounding. + // While users pass in `TimeMillis()` as current time, use the same rounding. + // At the same time steer users into passing time using `Timestamp` type + // queried from a Clock. + static constexpr Timestamp AlignTime(Timestamp time) { + return Timestamp::Millis(time.us() / 1000); + } + protected: // A ConnectionRequest is a simple STUN ping used to determine writability. class ConnectionRequest; @@ -395,11 +468,12 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // If the response is not received within 2 * RTT, the response is assumed to // be missing. - bool missing_responses(int64_t now) const; + bool missing_responses(Timestamp now) const; // Changes the state and signals if necessary. void set_write_state(WriteState value); - void UpdateReceiving(int64_t now); + void UpdateReceiving(Timestamp now); + void set_state(IceCandidatePairState state); void set_connected(bool value); @@ -409,7 +483,7 @@ class RTC_EXPORT Connection : public CandidatePairInterface { const Environment& env() { return env_; } ConnectionInfo& mutable_stats() { return stats_; } RateTracker& send_rate_tracker() { return send_rate_tracker_; } - void set_last_send_data(int64_t now_ms) { last_send_data_ = now_ms; } + void set_last_send_data(Timestamp now) { last_send_data_ = AlignTime(now); } private: // Update the local candidate based on the mapped address attribute. @@ -444,7 +518,7 @@ class RTC_EXPORT Connection : public CandidatePairInterface { ConnectionInfo stats_; RateTracker recv_rate_tracker_; RateTracker send_rate_tracker_; - int64_t last_send_data_ = 0; + Timestamp last_send_data_; WriteState write_state_ RTC_GUARDED_BY(network_thread_); bool receiving_ RTC_GUARDED_BY(network_thread_); @@ -470,21 +544,20 @@ class RTC_EXPORT Connection : public CandidatePairInterface { uint32_t remote_nomination_ RTC_GUARDED_BY(network_thread_) = 0; StunRequestManager requests_ RTC_GUARDED_BY(network_thread_); - int rtt_ RTC_GUARDED_BY(network_thread_); + TimeDelta rtt_ RTC_GUARDED_BY(network_thread_); int rtt_samples_ RTC_GUARDED_BY(network_thread_) = 0; // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime - uint64_t total_round_trip_time_ms_ RTC_GUARDED_BY(network_thread_) = 0; + TimeDelta total_round_trip_time_ RTC_GUARDED_BY(network_thread_); // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime - std::optional<uint32_t> current_round_trip_time_ms_ + std::optional<TimeDelta> current_round_trip_time_ RTC_GUARDED_BY(network_thread_); - int64_t last_ping_sent_ RTC_GUARDED_BY( - network_thread_); // last time we sent a ping to the other side - int64_t last_ping_received_ - RTC_GUARDED_BY(network_thread_); // last time we received a ping from the - // other side - int64_t last_data_received_ RTC_GUARDED_BY(network_thread_); - int64_t last_ping_response_received_ RTC_GUARDED_BY(network_thread_); - int64_t receiving_unchanged_since_ RTC_GUARDED_BY(network_thread_) = 0; + // last time we sent a ping to the other side + Timestamp last_ping_sent_ RTC_GUARDED_BY(network_thread_); + // last time we received a ping from the other side + Timestamp last_ping_received_ RTC_GUARDED_BY(network_thread_); + Timestamp last_data_received_ RTC_GUARDED_BY(network_thread_); + Timestamp last_ping_response_received_ RTC_GUARDED_BY(network_thread_); + Timestamp receiving_unchanged_since_ RTC_GUARDED_BY(network_thread_); std::vector<SentPing> pings_since_last_response_ RTC_GUARDED_BY(network_thread_); // Transaction ID of the last connectivity check received. Null if having not @@ -492,15 +565,15 @@ class RTC_EXPORT Connection : public CandidatePairInterface { std::optional<std::string> last_ping_id_received_ RTC_GUARDED_BY(network_thread_); - std::optional<int> unwritable_timeout_ RTC_GUARDED_BY(network_thread_); + std::optional<TimeDelta> unwritable_timeout_ RTC_GUARDED_BY(network_thread_); std::optional<int> unwritable_min_checks_ RTC_GUARDED_BY(network_thread_); - std::optional<int> inactive_timeout_ RTC_GUARDED_BY(network_thread_); + std::optional<TimeDelta> inactive_timeout_ RTC_GUARDED_BY(network_thread_); IceCandidatePairState state_ RTC_GUARDED_BY(network_thread_); // Time duration to switch from receiving to not receiving. - std::optional<int> receiving_timeout_ RTC_GUARDED_BY(network_thread_); - const int64_t time_created_ms_ RTC_GUARDED_BY(network_thread_); - const int64_t delta_internal_unix_epoch_ms_ RTC_GUARDED_BY(network_thread_); + std::optional<TimeDelta> receiving_timeout_ RTC_GUARDED_BY(network_thread_); + const Timestamp time_created_ RTC_GUARDED_BY(network_thread_); + const TimeDelta delta_internal_unix_epoch_ RTC_GUARDED_BY(network_thread_); int num_pings_sent_ RTC_GUARDED_BY(network_thread_) = 0; std::optional<IceCandidatePairDescription> log_description_ diff --git a/third_party/libwebrtc/p2p/base/p2p_constants.cc b/third_party/libwebrtc/p2p/base/p2p_constants.cc @@ -13,6 +13,10 @@ #include <cstddef> #include <cstdint> +#include "api/units/data_rate.h" +#include "api/units/data_size.h" +#include "api/units/time_delta.h" + namespace webrtc { const char CN_AUDIO[] = "audio"; @@ -57,20 +61,16 @@ const int REGATHER_ON_FAILED_NETWORKS_INTERVAL = 5 * 60 * 1000; // don't want to degrade the quality on a modem. These numbers should work well // on a 28.8K modem, which is the slowest connection on which the voice quality // is reasonable at all. -const int STUN_PING_PACKET_SIZE = 60 * 8; -const int STRONG_PING_INTERVAL = 1000 * STUN_PING_PACKET_SIZE / 1000; // 480ms. -const int WEAK_PING_INTERVAL = 1000 * STUN_PING_PACKET_SIZE / 10000; // 48ms. -const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL = 900; -const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL = 2500; -const int CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds +constexpr DataSize kStunPingPacketSize = DataSize::Bytes(60); +static_assert(kStrongPingInterval == + kStunPingPacketSize / DataRate::BitsPerSec(1'000)); +static_assert(kWeakPingInterval == + kStunPingPacketSize / DataRate::BitsPerSec(10'000)); + const uint32_t CONNECTION_WRITE_CONNECT_FAILURES = 5; // 5 pings const int STUN_KEEPALIVE_INTERVAL = 10 * 1000; // 10 seconds -const int MIN_CONNECTION_LIFETIME = 10 * 1000; // 10 seconds. -const int DEAD_CONNECTION_RECEIVE_TIMEOUT = 30 * 1000; // 30 seconds. -const int WEAK_CONNECTION_RECEIVE_TIMEOUT = 2500; // 2.5 seconds -const int CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds // There is no harm to keep this value high other than a small amount // of increased memory, but in some networks (2G), we observe up to 60s RTTs. const int CONNECTION_RESPONSE_TIMEOUT = 60 * 1000; // 60 seconds diff --git a/third_party/libwebrtc/p2p/base/p2p_constants.h b/third_party/libwebrtc/p2p/base/p2p_constants.h @@ -14,6 +14,7 @@ #include <cstddef> #include <cstdint> +#include "api/units/time_delta.h" #include "rtc_base/system/rtc_export.h" namespace webrtc { @@ -70,20 +71,28 @@ extern const int MIN_CHECK_RECEIVING_INTERVAL; // writable and receiving. // // Default value of IceConfig.ice_check_interval_strong_connectivity. -extern const int STRONG_PING_INTERVAL; +inline constexpr TimeDelta kStrongPingInterval = TimeDelta::Millis(480); +inline constexpr int STRONG_PING_INTERVAL = kStrongPingInterval.ms(); // WEAK_PING_INTERVAL is applied when the selected connection is either // not writable or not receiving. // // Defaul value of IceConfig.ice_check_interval_weak_connectivity. -extern const int WEAK_PING_INTERVAL; +inline constexpr TimeDelta kWeakPingInterval = TimeDelta::Millis(48); +inline constexpr int WEAK_PING_INTERVAL = kWeakPingInterval.ms(); // The next two ping intervals are at the candidate pair level. // // Writable candidate pairs are pinged at a slower rate once they are stabilized // and the channel is strongly connected. -extern const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL; +inline constexpr TimeDelta kStrongAndStableWritableConnectionPingInterval = + TimeDelta::Millis(2'500); +inline constexpr int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL = + kStrongAndStableWritableConnectionPingInterval.ms(); // Writable candidate pairs are pinged at a faster rate while the connections // are stabilizing or the channel is weak. -extern const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL; +inline constexpr TimeDelta kWeakOrStabilizingWritableConnectionPingInterval = + TimeDelta::Millis(900); +inline constexpr int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL = + kWeakOrStabilizingWritableConnectionPingInterval.ms(); // Default value of IceConfig.backup_connection_ping_interval extern const int BACKUP_CONNECTION_PING_INTERVAL; // Defualt value of IceConfig.receiving_switching_delay. @@ -91,29 +100,38 @@ extern const int RECEIVING_SWITCHING_DELAY; // Default value of IceConfig.regather_on_failed_networks_interval. extern const int REGATHER_ON_FAILED_NETWORKS_INTERVAL; // Default vaule of IceConfig.ice_unwritable_timeout. -extern const int CONNECTION_WRITE_CONNECT_TIMEOUT; +inline constexpr TimeDelta kConnectionWriteConnectTimeout = + TimeDelta::Seconds(5); +inline constexpr int CONNECTION_WRITE_CONNECT_TIMEOUT = + kConnectionWriteConnectTimeout.ms(); // Default vaule of IceConfig.ice_unwritable_min_checks. extern const uint32_t CONNECTION_WRITE_CONNECT_FAILURES; // Default value of IceConfig.ice_inactive_timeout; -extern const int CONNECTION_WRITE_TIMEOUT; +inline constexpr TimeDelta kConnectionWriteTimeout = TimeDelta::Seconds(15); +inline constexpr int CONNECTION_WRITE_TIMEOUT = kConnectionWriteTimeout.ms(); // Default value of IceConfig.stun_keepalive_interval; extern const int STUN_KEEPALIVE_INTERVAL; -static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; +inline constexpr int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; // The following constants are used at the candidate pair level to determine the // state of a candidate pair. // // The timeout duration when a connection does not receive anything. -extern const int WEAK_CONNECTION_RECEIVE_TIMEOUT; +inline constexpr TimeDelta kWeakConnectionReceiveTimeout = + TimeDelta::Millis(2'500); // A connection will be declared dead if it has not received anything for this // long. -extern const int DEAD_CONNECTION_RECEIVE_TIMEOUT; +inline constexpr TimeDelta kDeadConnectionReceiveTimeout = + TimeDelta::Seconds(30); +inline constexpr int DEAD_CONNECTION_RECEIVE_TIMEOUT = + kDeadConnectionReceiveTimeout.ms(); // This is the length of time that we wait for a ping response to come back. extern const int CONNECTION_RESPONSE_TIMEOUT; // The minimum time we will wait before destroying a connection after creating // it. -extern const int MIN_CONNECTION_LIFETIME; +inline constexpr TimeDelta kMinConnectionLifetime = TimeDelta::Seconds(10); +inline constexpr int MIN_CONNECTION_LIFETIME = kMinConnectionLifetime.ms(); // The type preference MUST be an integer from 0 to 126 inclusive. // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 diff --git a/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc b/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc @@ -40,6 +40,8 @@ #include "api/sequence_checker.h" #include "api/transport/enums.h" #include "api/transport/stun.h" +#include "api/units/time_delta.h" +#include "api/units/timestamp.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" #include "logging/rtc_event_log/ice_logger.h" #include "p2p/base/active_ice_controller_factory_interface.h" @@ -64,16 +66,15 @@ #include "rtc_base/ip_address.h" #include "rtc_base/logging.h" #include "rtc_base/net_helper.h" -#include "rtc_base/net_helpers.h" #include "rtc_base/network.h" #include "rtc_base/network/received_packet.h" #include "rtc_base/network/sent_packet.h" #include "rtc_base/network_constants.h" #include "rtc_base/network_route.h" +#include "rtc_base/platform_thread_types.h" #include "rtc_base/socket.h" #include "rtc_base/socket_address.h" #include "rtc_base/thread.h" -#include "rtc_base/time_utils.h" #include "rtc_base/trace_event.h" #include "system_wrappers/include/metrics.h" @@ -1714,7 +1715,7 @@ void P2PTransportChannel::RemoveConnectionForTest(Connection* connection) { // Monitor connection states. void P2PTransportChannel::UpdateConnectionStates() { RTC_DCHECK_RUN_ON(network_thread_); - int64_t now = TimeMillis(); + Timestamp now = env_.clock().CurrentTime(); // We need to copy the list of connections since some may delete themselves // when we call UpdateState. @@ -1872,12 +1873,12 @@ void P2PTransportChannel::SwitchSelectedConnectionInternal( CandidatePairChangeEvent pair_change = { .transport_name = transport_name(), .selected_candidate_pair = *GetSelectedCandidatePair(), - .last_data_received_ms = selected_connection_->last_data_received(), + .last_data_received_ms = selected_connection_->LastDataReceived().ms(), .reason = IceSwitchReasonToString(reason), .estimated_disconnected_time_ms = - old_selected_connection ? ComputeEstimatedDisconnectedTimeMs( - TimeMillis(), old_selected_connection) - : 0}; + old_selected_connection != nullptr + ? ComputeEstimatedDisconnectedTime(old_selected_connection).ms() + : 0}; candidate_pair_change_callback_(pair_change); } @@ -1886,14 +1887,14 @@ void P2PTransportChannel::SwitchSelectedConnectionInternal( ice_controller_->OnConnectionSwitched(selected_connection_); } -int64_t P2PTransportChannel::ComputeEstimatedDisconnectedTimeMs( - int64_t now_ms, +TimeDelta P2PTransportChannel::ComputeEstimatedDisconnectedTime( Connection* old_connection) { + Timestamp now = Connection::AlignTime(env_.clock().CurrentTime()); // TODO(jonaso): nicer keeps estimate of how frequently data _should_ be // received, this could be used to give better estimate (if needed). - int64_t last_data_or_old_ping = - std::max(old_connection->last_received(), last_data_received_ms_); - return (now_ms - last_data_or_old_ping); + Timestamp last_data_or_old_ping = + std::max(old_connection->LastReceived(), last_data_received_); + return now - last_data_or_old_ping; } // Warning: UpdateTransportState should eventually be called whenever a @@ -2044,7 +2045,7 @@ Connection* P2PTransportChannel::FindNextPingableConnection() { int64_t P2PTransportChannel::GetLastPingSentMs() const { RTC_DCHECK_RUN_ON(network_thread_); - return last_ping_sent_ms_; + return last_ping_sent_.ms(); } void P2PTransportChannel::SendPingRequest(const Connection* connection) { @@ -2085,8 +2086,8 @@ void P2PTransportChannel::PingConnection(Connection* conn) { } conn->set_nomination(nomination); conn->set_use_candidate_attr(use_candidate_attr); - last_ping_sent_ms_ = TimeMillis(); - conn->Ping(last_ping_sent_ms_, stun_dict_writer_.CreateDelta()); + last_ping_sent_ = Connection::AlignTime(env_.clock().CurrentTime()); + conn->Ping(last_ping_sent_, stun_dict_writer_.CreateDelta()); } uint32_t P2PTransportChannel::GetNominationAttr(Connection* conn) const { @@ -2239,9 +2240,9 @@ void P2PTransportChannel::OnReadPacket(Connection* connection, // Let the client know of an incoming packet packets_received_++; bytes_received_ += packet.payload().size(); - RTC_DCHECK(connection->last_data_received() >= last_data_received_ms_); - last_data_received_ms_ = - std::max(last_data_received_ms_, connection->last_data_received()); + RTC_DCHECK_GE(connection->LastDataReceived(), last_data_received_); + last_data_received_ = + std::max(last_data_received_, connection->LastDataReceived()); NotifyPacketReceived(packet); diff --git a/third_party/libwebrtc/p2p/base/p2p_transport_channel.h b/third_party/libwebrtc/p2p/base/p2p_transport_channel.h @@ -40,6 +40,8 @@ #include "api/sequence_checker.h" #include "api/transport/enums.h" #include "api/transport/stun.h" +#include "api/units/time_delta.h" +#include "api/units/timestamp.h" #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" #include "logging/rtc_event_log/ice_logger.h" #include "p2p/base/active_ice_controller_factory_interface.h" @@ -423,8 +425,7 @@ class RTC_EXPORT P2PTransportChannel : public IceTransportInternal, return const_cast<Connection*>(conn); } - int64_t ComputeEstimatedDisconnectedTimeMs(int64_t now, - Connection* old_connection); + TimeDelta ComputeEstimatedDisconnectedTime(Connection* old_connection); void ParseFieldTrials(const FieldTrialsView& field_trials); @@ -482,7 +483,7 @@ class RTC_EXPORT P2PTransportChannel : public IceTransportInternal, IceGatheringState gathering_state_ RTC_GUARDED_BY(network_thread_); std::unique_ptr<BasicRegatheringController> regathering_controller_ RTC_GUARDED_BY(network_thread_); - int64_t last_ping_sent_ms_ RTC_GUARDED_BY(network_thread_) = 0; + Timestamp last_ping_sent_ RTC_GUARDED_BY(network_thread_) = Timestamp::Zero(); int weak_ping_interval_ RTC_GUARDED_BY(network_thread_) = WEAK_PING_INTERVAL; // TODO(jonasolsson): Remove state_ and rename standardized_state_ once state_ // is no longer used to compute the ICE connection state. @@ -523,7 +524,7 @@ class RTC_EXPORT P2PTransportChannel : public IceTransportInternal, // When was last data received on a existing connection, // from connection->last_data_received() that uses TimeMillis(). - int64_t last_data_received_ms_ = 0; + Timestamp last_data_received_ = Timestamp::Zero(); // Parsed field trials. IceFieldTrials ice_field_trials_; diff --git a/third_party/libwebrtc/p2p/base/tcp_port.cc b/third_party/libwebrtc/p2p/base/tcp_port.cc @@ -82,6 +82,7 @@ #include "api/task_queue/pending_task_safety_flag.h" #include "api/transport/stun.h" #include "api/units/time_delta.h" +#include "api/units/timestamp.h" #include "p2p/base/connection.h" #include "p2p/base/connection_info.h" #include "p2p/base/p2p_constants.h" @@ -98,7 +99,6 @@ #include "rtc_base/rate_tracker.h" #include "rtc_base/socket.h" #include "rtc_base/socket_address.h" -#include "rtc_base/time_utils.h" #include "rtc_base/weak_ptr.h" namespace webrtc { @@ -410,12 +410,12 @@ int TCPConnection::Send(const void* data, tcp_port()->CopyPortInformationToPacketInfo( &modified_options.info_signaled_after_sent); int sent = socket_->Send(data, size, modified_options); - int64_t now = TimeMillis(); + Timestamp now = Connection::AlignTime(env().clock().CurrentTime()); if (sent < 0) { mutable_stats().sent_discarded_packets++; error_ = socket_->GetError(); } else { - send_rate_tracker().AddSamplesAtTime(now, sent); + send_rate_tracker().AddSamplesAtTime(now.ms(), sent); } set_last_send_data(now); return sent;