Method for converting q32 to TimeDelta in capture clock offset updater

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 <danilchap@webrtc.org>
Commit-Queue: Olov Brändström <brandstrom@google.com>
Cr-Commit-Position: refs/heads/main@{#40745}
This commit is contained in:
Olov Brändström 2023-09-13 19:18:07 +02:00 committed by WebRTC LUCI CQ
parent 6ba7feb302
commit 0efb8323d5
3 changed files with 36 additions and 0 deletions

View File

@ -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<int64_t>
@ -25,6 +27,14 @@ CaptureClockOffsetUpdater::AdjustEstimatedCaptureClockOffset(
static_cast<uint64_t>(*remote_to_local_clock_offset_);
}
absl::optional<TimeDelta> CaptureClockOffsetUpdater::ConvertsToTimeDela(
absl::optional<int64_t> q32x32) {
if (q32x32 == absl::nullopt) {
return absl::nullopt;
}
return TimeDelta::Millis(Q32x32ToInt64Ms(*q32x32));
}
void CaptureClockOffsetUpdater::SetRemoteToLocalClockOffset(
absl::optional<int64_t> offset_q32x32) {
remote_to_local_clock_offset_ = offset_q32x32;

View File

@ -14,6 +14,7 @@
#include <stdint.h>
#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<int64_t> offset_q32x32);
// Converts a signed Q32.32-formatted fixed-point to a TimeDelta.
static absl::optional<TimeDelta> ConvertsToTimeDela(
absl::optional<int64_t> q32x32);
private:
absl::optional<int64_t> remote_to_local_clock_offset_;
};

View File

@ -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<TimeDelta> 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