1 /*
2 * Copyright (c) 2015 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 "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h"
12
13 #include <limits>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/logging.h"
17 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
19 #include "webrtc/modules/utility/include/process_thread.h"
20
21 namespace webrtc {
22
23 const int64_t kNoTimestamp = -1;
24 const int64_t kSendTimeHistoryWindowMs = 10000;
25 const int64_t kBaseTimestampScaleFactor =
26 rtcp::TransportFeedback::kDeltaScaleFactor * (1 << 8);
27 const int64_t kBaseTimestampRangeSizeUs = kBaseTimestampScaleFactor * (1 << 24);
28
TransportFeedbackAdapter(RtcpBandwidthObserver * bandwidth_observer,Clock * clock,ProcessThread * process_thread)29 TransportFeedbackAdapter::TransportFeedbackAdapter(
30 RtcpBandwidthObserver* bandwidth_observer,
31 Clock* clock,
32 ProcessThread* process_thread)
33 : send_time_history_(clock, kSendTimeHistoryWindowMs),
34 rtcp_bandwidth_observer_(bandwidth_observer),
35 process_thread_(process_thread),
36 clock_(clock),
37 current_offset_ms_(kNoTimestamp),
38 last_timestamp_us_(kNoTimestamp) {}
39
~TransportFeedbackAdapter()40 TransportFeedbackAdapter::~TransportFeedbackAdapter() {
41 if (bitrate_estimator_.get())
42 process_thread_->DeRegisterModule(bitrate_estimator_.get());
43 }
44
SetBitrateEstimator(RemoteBitrateEstimator * rbe)45 void TransportFeedbackAdapter::SetBitrateEstimator(
46 RemoteBitrateEstimator* rbe) {
47 if (bitrate_estimator_.get() != rbe) {
48 bitrate_estimator_.reset(rbe);
49 process_thread_->RegisterModule(rbe);
50 }
51 }
52
AddPacket(uint16_t sequence_number,size_t length,bool was_paced)53 void TransportFeedbackAdapter::AddPacket(uint16_t sequence_number,
54 size_t length,
55 bool was_paced) {
56 rtc::CritScope cs(&lock_);
57 send_time_history_.AddAndRemoveOld(sequence_number, length, was_paced);
58 }
59
OnSentPacket(uint16_t sequence_number,int64_t send_time_ms)60 void TransportFeedbackAdapter::OnSentPacket(uint16_t sequence_number,
61 int64_t send_time_ms) {
62 rtc::CritScope cs(&lock_);
63 send_time_history_.OnSentPacket(sequence_number, send_time_ms);
64 }
65
OnTransportFeedback(const rtcp::TransportFeedback & feedback)66 void TransportFeedbackAdapter::OnTransportFeedback(
67 const rtcp::TransportFeedback& feedback) {
68 int64_t timestamp_us = feedback.GetBaseTimeUs();
69 // Add timestamp deltas to a local time base selected on first packet arrival.
70 // This won't be the true time base, but makes it easier to manually inspect
71 // time stamps.
72 if (last_timestamp_us_ == kNoTimestamp) {
73 current_offset_ms_ = clock_->TimeInMilliseconds();
74 } else {
75 int64_t delta = timestamp_us - last_timestamp_us_;
76
77 // Detect and compensate for wrap-arounds in base time.
78 if (std::abs(delta - kBaseTimestampRangeSizeUs) < std::abs(delta)) {
79 delta -= kBaseTimestampRangeSizeUs; // Wrap backwards.
80 } else if (std::abs(delta + kBaseTimestampRangeSizeUs) < std::abs(delta)) {
81 delta += kBaseTimestampRangeSizeUs; // Wrap forwards.
82 }
83
84 current_offset_ms_ += delta / 1000;
85 }
86 last_timestamp_us_ = timestamp_us;
87
88 uint16_t sequence_number = feedback.GetBaseSequence();
89 std::vector<int64_t> delta_vec = feedback.GetReceiveDeltasUs();
90 auto delta_it = delta_vec.begin();
91 std::vector<PacketInfo> packet_feedback_vector;
92 packet_feedback_vector.reserve(delta_vec.size());
93
94 {
95 rtc::CritScope cs(&lock_);
96 size_t failed_lookups = 0;
97 int64_t offset_us = 0;
98 for (auto symbol : feedback.GetStatusVector()) {
99 if (symbol != rtcp::TransportFeedback::StatusSymbol::kNotReceived) {
100 RTC_DCHECK(delta_it != delta_vec.end());
101 offset_us += *(delta_it++);
102 int64_t timestamp_ms = current_offset_ms_ + (offset_us / 1000);
103 PacketInfo info(timestamp_ms, sequence_number);
104 if (send_time_history_.GetInfo(&info, true) && info.send_time_ms >= 0) {
105 packet_feedback_vector.push_back(info);
106 } else {
107 ++failed_lookups;
108 }
109 }
110 ++sequence_number;
111 }
112 RTC_DCHECK(delta_it == delta_vec.end());
113 if (failed_lookups > 0) {
114 LOG(LS_WARNING) << "Failed to lookup send time for " << failed_lookups
115 << " packet" << (failed_lookups > 1 ? "s" : "")
116 << ". Send time history too small?";
117 }
118 }
119
120 RTC_DCHECK(bitrate_estimator_.get() != nullptr);
121 bitrate_estimator_->IncomingPacketFeedbackVector(packet_feedback_vector);
122 }
123
OnReceiveBitrateChanged(const std::vector<unsigned int> & ssrcs,unsigned int bitrate)124 void TransportFeedbackAdapter::OnReceiveBitrateChanged(
125 const std::vector<unsigned int>& ssrcs,
126 unsigned int bitrate) {
127 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
128 }
129
OnRttUpdate(int64_t avg_rtt_ms,int64_t max_rtt_ms)130 void TransportFeedbackAdapter::OnRttUpdate(int64_t avg_rtt_ms,
131 int64_t max_rtt_ms) {
132 RTC_DCHECK(bitrate_estimator_.get() != nullptr);
133 bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
134 }
135
136 } // namespace webrtc
137