mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-17 23:57:59 +01:00

Removed thread safety: for a low level helper it adds overhead that users may not need. In particular RtpSenderVideo doesn't need it because calls to SendVideo are already synchronized. Added a feature to force producing extension as requested by downstream. Cleanup and document api: Changed rtp_frequency type to int as it has no reason to use uint32_t per style guide Changed absolute_capture_time to NtpTime to clarify both units and offset of the time. NtpTime has trivial conversion to/from uint64_t Documented all the parameters. Cleanup tests. Bug: b/307553606 Change-Id: I0922ca4d3c89f124eeb561742dca79ed5c2327fd Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/325022 Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Reviewed-by: Chen Xing <chxg@google.com> Cr-Commit-Position: refs/heads/main@{#41023}
121 lines
4.2 KiB
C++
121 lines
4.2 KiB
C++
/*
|
|
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "modules/rtp_rtcp/source/absolute_capture_time_sender.h"
|
|
|
|
#include <limits>
|
|
|
|
#include "modules/rtp_rtcp/source/absolute_capture_time_interpolator.h"
|
|
#include "system_wrappers/include/ntp_time.h"
|
|
|
|
namespace webrtc {
|
|
|
|
static_assert(
|
|
AbsoluteCaptureTimeInterpolator::kInterpolationMaxInterval >=
|
|
AbsoluteCaptureTimeSender::kInterpolationMaxInterval,
|
|
"Receivers should be as willing to interpolate timestamps as senders.");
|
|
|
|
AbsoluteCaptureTimeSender::AbsoluteCaptureTimeSender(Clock* clock)
|
|
: clock_(clock) {}
|
|
|
|
uint32_t AbsoluteCaptureTimeSender::GetSource(
|
|
uint32_t ssrc,
|
|
rtc::ArrayView<const uint32_t> csrcs) {
|
|
return AbsoluteCaptureTimeInterpolator::GetSource(ssrc, csrcs);
|
|
}
|
|
|
|
absl::optional<AbsoluteCaptureTime> AbsoluteCaptureTimeSender::OnSendPacket(
|
|
uint32_t source,
|
|
uint32_t rtp_timestamp,
|
|
uint32_t rtp_clock_frequency,
|
|
uint64_t absolute_capture_timestamp,
|
|
absl::optional<int64_t> estimated_capture_clock_offset) {
|
|
return OnSendPacket(source, rtp_timestamp, rtp_clock_frequency,
|
|
NtpTime(absolute_capture_timestamp),
|
|
estimated_capture_clock_offset, /*force=*/false);
|
|
}
|
|
|
|
absl::optional<AbsoluteCaptureTime> AbsoluteCaptureTimeSender::OnSendPacket(
|
|
uint32_t source,
|
|
uint32_t rtp_timestamp,
|
|
int rtp_clock_frequency_hz,
|
|
NtpTime absolute_capture_time,
|
|
absl::optional<int64_t> estimated_capture_clock_offset,
|
|
bool force) {
|
|
Timestamp send_time = clock_->CurrentTime();
|
|
if (!(force || ShouldSendExtension(
|
|
send_time, source, rtp_timestamp, rtp_clock_frequency_hz,
|
|
absolute_capture_time, estimated_capture_clock_offset))) {
|
|
return absl::nullopt;
|
|
}
|
|
|
|
last_source_ = source;
|
|
last_rtp_timestamp_ = rtp_timestamp;
|
|
last_rtp_clock_frequency_hz_ = rtp_clock_frequency_hz;
|
|
last_absolute_capture_time_ = absolute_capture_time;
|
|
last_estimated_capture_clock_offset_ = estimated_capture_clock_offset;
|
|
last_send_time_ = send_time;
|
|
|
|
return AbsoluteCaptureTime{
|
|
.absolute_capture_timestamp = uint64_t{absolute_capture_time},
|
|
.estimated_capture_clock_offset = estimated_capture_clock_offset,
|
|
};
|
|
}
|
|
|
|
bool AbsoluteCaptureTimeSender::ShouldSendExtension(
|
|
Timestamp send_time,
|
|
uint32_t source,
|
|
uint32_t rtp_timestamp,
|
|
int rtp_clock_frequency_hz,
|
|
NtpTime absolute_capture_time,
|
|
absl::optional<int64_t> estimated_capture_clock_offset) const {
|
|
// Should if the last sent extension is too old, in particular if we've never
|
|
// sent anything before.
|
|
if (send_time - last_send_time_ > kInterpolationMaxInterval) {
|
|
return true;
|
|
}
|
|
|
|
// Should if the source has changed.
|
|
if (last_source_ != source) {
|
|
return true;
|
|
}
|
|
|
|
// Should if the RTP clock frequency has changed.
|
|
if (last_rtp_clock_frequency_hz_ != rtp_clock_frequency_hz) {
|
|
return true;
|
|
}
|
|
|
|
// Should if the RTP clock frequency is invalid.
|
|
if (rtp_clock_frequency_hz <= 0) {
|
|
return true;
|
|
}
|
|
|
|
// Should if the estimated capture clock offset has changed.
|
|
if (last_estimated_capture_clock_offset_ != estimated_capture_clock_offset) {
|
|
return true;
|
|
}
|
|
|
|
// Should if interpolation would introduce too much error.
|
|
const uint64_t interpolated_absolute_capture_timestamp =
|
|
AbsoluteCaptureTimeInterpolator::InterpolateAbsoluteCaptureTimestamp(
|
|
rtp_timestamp, rtp_clock_frequency_hz, last_rtp_timestamp_,
|
|
uint64_t{last_absolute_capture_time_});
|
|
const uint64_t absolute_capture_timestamp = uint64_t{absolute_capture_time};
|
|
const int64_t interpolation_error_ms = UQ32x32ToInt64Ms(std::min(
|
|
interpolated_absolute_capture_timestamp - absolute_capture_timestamp,
|
|
absolute_capture_timestamp - interpolated_absolute_capture_timestamp));
|
|
if (interpolation_error_ms > kInterpolationMaxError.ms()) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
} // namespace webrtc
|