Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.cc

Issue 2378103005: Reland: Fix race / crash in OnNetworkRouteChanged(). (Closed)
Patch Set: . Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <algorithm>
14 #include <limits>
15
16 #include "webrtc/base/checks.h"
17 #include "webrtc/base/logging.h"
18 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimat or.h"
19 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
20 #include "webrtc/modules/utility/include/process_thread.h"
21
22 namespace webrtc {
23
24 const int64_t kNoTimestamp = -1;
25 const int64_t kSendTimeHistoryWindowMs = 10000;
26 const int64_t kBaseTimestampScaleFactor =
27 rtcp::TransportFeedback::kDeltaScaleFactor * (1 << 8);
28 const int64_t kBaseTimestampRangeSizeUs = kBaseTimestampScaleFactor * (1 << 24);
29
30 class PacketInfoComparator {
31 public:
32 inline bool operator()(const PacketInfo& lhs, const PacketInfo& rhs) {
33 if (lhs.arrival_time_ms != rhs.arrival_time_ms)
34 return lhs.arrival_time_ms < rhs.arrival_time_ms;
35 if (lhs.send_time_ms != rhs.send_time_ms)
36 return lhs.send_time_ms < rhs.send_time_ms;
37 return lhs.sequence_number < rhs.sequence_number;
38 }
39 };
40
41 TransportFeedbackAdapter::TransportFeedbackAdapter(
42 Clock* clock)
43 : send_time_history_(clock, kSendTimeHistoryWindowMs),
44 clock_(clock),
45 current_offset_ms_(kNoTimestamp),
46 last_timestamp_us_(kNoTimestamp) {}
47
48 TransportFeedbackAdapter::~TransportFeedbackAdapter() {
49 }
50
51 void TransportFeedbackAdapter::SetBitrateEstimator(
52 RemoteBitrateEstimator* rbe) {
53 if (bitrate_estimator_.get() != rbe) {
54 bitrate_estimator_.reset(rbe);
55 }
56 }
57
58 void TransportFeedbackAdapter::AddPacket(uint16_t sequence_number,
59 size_t length,
60 int probe_cluster_id) {
61 rtc::CritScope cs(&lock_);
62 send_time_history_.AddAndRemoveOld(sequence_number, length, probe_cluster_id);
63 }
64
65 void TransportFeedbackAdapter::OnSentPacket(uint16_t sequence_number,
66 int64_t send_time_ms) {
67 rtc::CritScope cs(&lock_);
68 send_time_history_.OnSentPacket(sequence_number, send_time_ms);
69 }
70
71 std::vector<PacketInfo> TransportFeedbackAdapter::GetPacketFeedbackVector(
72 const rtcp::TransportFeedback& feedback) {
73 int64_t timestamp_us = feedback.GetBaseTimeUs();
74 // Add timestamp deltas to a local time base selected on first packet arrival.
75 // This won't be the true time base, but makes it easier to manually inspect
76 // time stamps.
77 if (last_timestamp_us_ == kNoTimestamp) {
78 current_offset_ms_ = clock_->TimeInMilliseconds();
79 } else {
80 int64_t delta = timestamp_us - last_timestamp_us_;
81
82 // Detect and compensate for wrap-arounds in base time.
83 if (std::abs(delta - kBaseTimestampRangeSizeUs) < std::abs(delta)) {
84 delta -= kBaseTimestampRangeSizeUs; // Wrap backwards.
85 } else if (std::abs(delta + kBaseTimestampRangeSizeUs) < std::abs(delta)) {
86 delta += kBaseTimestampRangeSizeUs; // Wrap forwards.
87 }
88
89 current_offset_ms_ += delta / 1000;
90 }
91 last_timestamp_us_ = timestamp_us;
92
93 uint16_t sequence_number = feedback.GetBaseSequence();
94 std::vector<int64_t> delta_vec = feedback.GetReceiveDeltasUs();
95 auto delta_it = delta_vec.begin();
96 std::vector<PacketInfo> packet_feedback_vector;
97 packet_feedback_vector.reserve(delta_vec.size());
98
99 {
100 rtc::CritScope cs(&lock_);
101 size_t failed_lookups = 0;
102 int64_t offset_us = 0;
103 for (auto symbol : feedback.GetStatusVector()) {
104 if (symbol != rtcp::TransportFeedback::StatusSymbol::kNotReceived) {
105 RTC_DCHECK(delta_it != delta_vec.end());
106 offset_us += *(delta_it++);
107 int64_t timestamp_ms = current_offset_ms_ + (offset_us / 1000);
108 PacketInfo info(timestamp_ms, sequence_number);
109 if (send_time_history_.GetInfo(&info, true) && info.send_time_ms >= 0) {
110 packet_feedback_vector.push_back(info);
111 } else {
112 ++failed_lookups;
113 }
114 }
115 ++sequence_number;
116 }
117 std::sort(packet_feedback_vector.begin(), packet_feedback_vector.end(),
118 PacketInfoComparator());
119 RTC_DCHECK(delta_it == delta_vec.end());
120 if (failed_lookups > 0) {
121 LOG(LS_WARNING) << "Failed to lookup send time for " << failed_lookups
122 << " packet" << (failed_lookups > 1 ? "s" : "")
123 << ". Send time history too small?";
124 }
125 }
126 return packet_feedback_vector;
127 }
128
129 void TransportFeedbackAdapter::OnTransportFeedback(
130 const rtcp::TransportFeedback& feedback) {
131 last_packet_feedback_vector_ = GetPacketFeedbackVector(feedback);
132 if (bitrate_estimator_.get())
133 bitrate_estimator_->IncomingPacketFeedbackVector(
134 last_packet_feedback_vector_);
135 }
136
137 std::vector<PacketInfo> TransportFeedbackAdapter::GetTransportFeedbackVector()
138 const {
139 return last_packet_feedback_vector_;
140 }
141
142 void TransportFeedbackAdapter::OnRttUpdate(int64_t avg_rtt_ms,
143 int64_t max_rtt_ms) {
144 RTC_DCHECK(bitrate_estimator_.get() != nullptr);
145 bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
146 }
147
148 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698