• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/rtp_rtcp/source/absolute_capture_time_receiver.h"
12 
13 #include <limits>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 namespace {
19 
20 constexpr Timestamp kInvalidLastReceiveTime = Timestamp::MinusInfinity();
21 }  // namespace
22 
23 constexpr TimeDelta AbsoluteCaptureTimeReceiver::kInterpolationMaxInterval;
24 
AbsoluteCaptureTimeReceiver(Clock * clock)25 AbsoluteCaptureTimeReceiver::AbsoluteCaptureTimeReceiver(Clock* clock)
26     : clock_(clock),
27       remote_to_local_clock_offset_(absl::nullopt),
28       last_receive_time_(kInvalidLastReceiveTime) {}
29 
GetSource(uint32_t ssrc,rtc::ArrayView<const uint32_t> csrcs)30 uint32_t AbsoluteCaptureTimeReceiver::GetSource(
31     uint32_t ssrc,
32     rtc::ArrayView<const uint32_t> csrcs) {
33   if (csrcs.empty()) {
34     return ssrc;
35   }
36 
37   return csrcs[0];
38 }
39 
SetRemoteToLocalClockOffset(absl::optional<int64_t> value_q32x32)40 void AbsoluteCaptureTimeReceiver::SetRemoteToLocalClockOffset(
41     absl::optional<int64_t> value_q32x32) {
42   MutexLock lock(&mutex_);
43 
44   remote_to_local_clock_offset_ = value_q32x32;
45 }
46 
47 absl::optional<AbsoluteCaptureTime>
OnReceivePacket(uint32_t source,uint32_t rtp_timestamp,uint32_t rtp_clock_frequency,const absl::optional<AbsoluteCaptureTime> & received_extension)48 AbsoluteCaptureTimeReceiver::OnReceivePacket(
49     uint32_t source,
50     uint32_t rtp_timestamp,
51     uint32_t rtp_clock_frequency,
52     const absl::optional<AbsoluteCaptureTime>& received_extension) {
53   const Timestamp receive_time = clock_->CurrentTime();
54 
55   MutexLock lock(&mutex_);
56 
57   AbsoluteCaptureTime extension;
58   if (received_extension == absl::nullopt) {
59     if (!ShouldInterpolateExtension(receive_time, source, rtp_timestamp,
60                                     rtp_clock_frequency)) {
61       last_receive_time_ = kInvalidLastReceiveTime;
62       return absl::nullopt;
63     }
64 
65     extension.absolute_capture_timestamp = InterpolateAbsoluteCaptureTimestamp(
66         rtp_timestamp, rtp_clock_frequency, last_rtp_timestamp_,
67         last_absolute_capture_timestamp_);
68     extension.estimated_capture_clock_offset =
69         last_estimated_capture_clock_offset_;
70   } else {
71     last_source_ = source;
72     last_rtp_timestamp_ = rtp_timestamp;
73     last_rtp_clock_frequency_ = rtp_clock_frequency;
74     last_absolute_capture_timestamp_ =
75         received_extension->absolute_capture_timestamp;
76     last_estimated_capture_clock_offset_ =
77         received_extension->estimated_capture_clock_offset;
78 
79     last_receive_time_ = receive_time;
80 
81     extension = *received_extension;
82   }
83 
84   extension.estimated_capture_clock_offset = AdjustEstimatedCaptureClockOffset(
85       extension.estimated_capture_clock_offset);
86 
87   return extension;
88 }
89 
InterpolateAbsoluteCaptureTimestamp(uint32_t rtp_timestamp,uint32_t rtp_clock_frequency,uint32_t last_rtp_timestamp,uint64_t last_absolute_capture_timestamp)90 uint64_t AbsoluteCaptureTimeReceiver::InterpolateAbsoluteCaptureTimestamp(
91     uint32_t rtp_timestamp,
92     uint32_t rtp_clock_frequency,
93     uint32_t last_rtp_timestamp,
94     uint64_t last_absolute_capture_timestamp) {
95   RTC_DCHECK_GT(rtp_clock_frequency, 0);
96 
97   return last_absolute_capture_timestamp +
98          static_cast<int64_t>(
99              rtc::dchecked_cast<uint64_t>(rtp_timestamp - last_rtp_timestamp)
100              << 32) /
101              rtp_clock_frequency;
102 }
103 
ShouldInterpolateExtension(Timestamp receive_time,uint32_t source,uint32_t rtp_timestamp,uint32_t rtp_clock_frequency) const104 bool AbsoluteCaptureTimeReceiver::ShouldInterpolateExtension(
105     Timestamp receive_time,
106     uint32_t source,
107     uint32_t rtp_timestamp,
108     uint32_t rtp_clock_frequency) const {
109   // Shouldn't if we don't have a previously received extension stored.
110   if (last_receive_time_ == kInvalidLastReceiveTime) {
111     return false;
112   }
113 
114   // Shouldn't if the last received extension is too old.
115   if ((receive_time - last_receive_time_) > kInterpolationMaxInterval) {
116     return false;
117   }
118 
119   // Shouldn't if the source has changed.
120   if (last_source_ != source) {
121     return false;
122   }
123 
124   // Shouldn't if the RTP clock frequency has changed.
125   if (last_rtp_clock_frequency_ != rtp_clock_frequency) {
126     return false;
127   }
128 
129   // Shouldn't if the RTP clock frequency is invalid.
130   if (rtp_clock_frequency <= 0) {
131     return false;
132   }
133 
134   return true;
135 }
136 
137 absl::optional<int64_t>
AdjustEstimatedCaptureClockOffset(absl::optional<int64_t> received_value) const138 AbsoluteCaptureTimeReceiver::AdjustEstimatedCaptureClockOffset(
139     absl::optional<int64_t> received_value) const {
140   if (received_value == absl::nullopt ||
141       remote_to_local_clock_offset_ == absl::nullopt) {
142     return absl::nullopt;
143   }
144 
145   // Do calculations as "unsigned" to make overflows deterministic.
146   return static_cast<uint64_t>(*received_value) +
147          static_cast<uint64_t>(*remote_to_local_clock_offset_);
148 }
149 
150 }  // namespace webrtc
151