From 0efb8323d5957b824685b1e6b74229205aede25f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olov=20Br=C3=A4ndstr=C3=B6m?= Date: Wed, 13 Sep 2023 19:18:07 +0200 Subject: [PATCH] Method for converting q32 to TimeDelta in capture clock offset updater MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In change https://webrtc-review.googlesource.com/c/src/+/319961, I changed a error. Also the same code will be added for video to enable Glass 2 Glass metric for Android. To me it make sense to add this method, and then change the audio code and video code to use it. Bug: None Change-Id: Id5d38c3bb8266213a93e67ceb82e88d65f29de53 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/320080 Reviewed-by: Danil Chapovalov Commit-Queue: Olov Brändström Cr-Commit-Position: refs/heads/main@{#40745} --- .../source/capture_clock_offset_updater.cc | 10 +++++++++ .../source/capture_clock_offset_updater.h | 5 +++++ .../capture_clock_offset_updater_unittest.cc | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/modules/rtp_rtcp/source/capture_clock_offset_updater.cc b/modules/rtp_rtcp/source/capture_clock_offset_updater.cc index a5b12cb422..ad935a94f0 100644 --- a/modules/rtp_rtcp/source/capture_clock_offset_updater.cc +++ b/modules/rtp_rtcp/source/capture_clock_offset_updater.cc @@ -10,6 +10,8 @@ #include "modules/rtp_rtcp/source/capture_clock_offset_updater.h" +#include "system_wrappers/include/ntp_time.h" + namespace webrtc { absl::optional @@ -25,6 +27,14 @@ CaptureClockOffsetUpdater::AdjustEstimatedCaptureClockOffset( static_cast(*remote_to_local_clock_offset_); } +absl::optional CaptureClockOffsetUpdater::ConvertsToTimeDela( + absl::optional q32x32) { + if (q32x32 == absl::nullopt) { + return absl::nullopt; + } + return TimeDelta::Millis(Q32x32ToInt64Ms(*q32x32)); +} + void CaptureClockOffsetUpdater::SetRemoteToLocalClockOffset( absl::optional offset_q32x32) { remote_to_local_clock_offset_ = offset_q32x32; diff --git a/modules/rtp_rtcp/source/capture_clock_offset_updater.h b/modules/rtp_rtcp/source/capture_clock_offset_updater.h index 71d3eb4831..9b28848169 100644 --- a/modules/rtp_rtcp/source/capture_clock_offset_updater.h +++ b/modules/rtp_rtcp/source/capture_clock_offset_updater.h @@ -14,6 +14,7 @@ #include #include "absl/types/optional.h" +#include "api/units/time_delta.h" namespace webrtc { @@ -42,6 +43,10 @@ class CaptureClockOffsetUpdater { // Note that the value must be in Q32.32-formatted fixed-point seconds. void SetRemoteToLocalClockOffset(absl::optional offset_q32x32); + // Converts a signed Q32.32-formatted fixed-point to a TimeDelta. + static absl::optional ConvertsToTimeDela( + absl::optional q32x32); + private: absl::optional remote_to_local_clock_offset_; }; diff --git a/modules/rtp_rtcp/source/capture_clock_offset_updater_unittest.cc b/modules/rtp_rtcp/source/capture_clock_offset_updater_unittest.cc index 43e1dd1379..f6bea4ba96 100644 --- a/modules/rtp_rtcp/source/capture_clock_offset_updater_unittest.cc +++ b/modules/rtp_rtcp/source/capture_clock_offset_updater_unittest.cc @@ -55,4 +55,25 @@ TEST(AbsoluteCaptureTimeReceiverTest, EstimatedCaptureClockOffsetArithmetic) { *kRemoteToLocalClockOffset))); } +TEST(AbsoluteCaptureTimeReceiverTest, ConvertClockOffset) { + constexpr TimeDelta kNegative = TimeDelta::Millis(-350); + constexpr int64_t kNegativeQ32x32 = + kNegative.ms() * (NtpTime::kFractionsPerSecond / 1000); + constexpr TimeDelta kPositive = TimeDelta::Millis(400); + constexpr int64_t kPositiveQ32x32 = + kPositive.ms() * (NtpTime::kFractionsPerSecond / 1000); + constexpr TimeDelta kEpsilon = TimeDelta::Millis(1); + absl::optional converted = + CaptureClockOffsetUpdater::ConvertsToTimeDela(kNegativeQ32x32); + EXPECT_GT(converted, kNegative - kEpsilon); + EXPECT_LT(converted, kNegative + kEpsilon); + + converted = CaptureClockOffsetUpdater::ConvertsToTimeDela(kPositiveQ32x32); + EXPECT_GT(converted, kPositive - kEpsilon); + EXPECT_LT(converted, kPositive + kEpsilon); + + EXPECT_FALSE( + CaptureClockOffsetUpdater::ConvertsToTimeDela(absl::nullopt).has_value()); +} + } // namespace webrtc