Chromium Code Reviews| Index: webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.cc |
| diff --git a/webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.cc b/webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e84f7dec159512a242625744212d656a5b02e9bc |
| --- /dev/null |
| +++ b/webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.cc |
| @@ -0,0 +1,126 @@ |
| +/* |
| + * Copyright (c) 2015 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 <limits> |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/base/logging.h" |
| +#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h" |
| +#include "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h" |
| +#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
| +#include "webrtc/modules/utility/interface/process_thread.h" |
| + |
| +namespace webrtc { |
| + |
| +const int64_t kSendTimeHistoryWindowMs = 2000; |
| +const int64_t kNoTimestamp = std::numeric_limits<int64_t>::min(); |
|
stefan-webrtc
2015/09/10 11:10:01
Doesn't -1 work?
sprang_webrtc
2015/09/11 12:33:47
Yes. Initially I allowed slightly negative timesta
|
| +const int64_t kBaseTimestampScaleFactor = |
| + rtcp::TransportFeedback::kDeltaScaleFactor * (1 << 8); |
| +const int64_t kBaseTimestampRangeSizeUs = kBaseTimestampScaleFactor * (1 << 24); |
| + |
| +TransportFeedbackAdapter::TransportFeedbackAdapter( |
| + RtcpBandwidthObserver* bandwidth_observer, |
| + Clock* clock, |
| + ProcessThread* process_thread) |
| + : send_time_history_(kSendTimeHistoryWindowMs), |
| + rtcp_bandwidth_observer_(bandwidth_observer), |
| + process_thread_(process_thread), |
| + clock_(clock), |
| + current_offset_ms_(kNoTimestamp), |
| + last_timestamp_us_(kNoTimestamp) {} |
| + |
| +TransportFeedbackAdapter::~TransportFeedbackAdapter() { |
| + if (bitrate_estimator_.get()) |
| + process_thread_->DeRegisterModule(bitrate_estimator_.get()); |
| +} |
| + |
| +void TransportFeedbackAdapter::SetBitrateEstimator( |
| + RemoteBitrateEstimator* rbe) { |
| + if (bitrate_estimator_.get() != rbe) { |
| + bitrate_estimator_.reset(rbe); |
| + process_thread_->RegisterModule(rbe); |
| + } |
| +} |
| + |
| +void TransportFeedbackAdapter::OnPacketSent(uint16_t sequence_number, |
| + int64_t send_time, |
| + size_t size, |
| + bool was_paced) { |
| + rtc::CritScope cs(&lock_); |
| + send_time_history_.AddAndRemoveOld( |
| + PacketInfo(0, send_time, sequence_number, size, was_paced)); |
| +} |
| + |
| +void TransportFeedbackAdapter::OnTransportFeedback( |
| + const rtcp::TransportFeedback& feedback) { |
| + int64_t timestamp_us = feedback.GetBaseTimeUs(); |
| + if (last_timestamp_us_ == kNoTimestamp) { |
| + current_offset_ms_ = clock_->TimeInMilliseconds(); |
| + } else { |
| + int64_t delta = timestamp_us - last_timestamp_us_; |
| + |
| + if (labs(delta - kBaseTimestampRangeSizeUs) < labs(delta)) { |
|
stefan-webrtc
2015/09/10 11:10:01
std::abs<int64_t> or something like that instead?
stefan-webrtc
2015/09/10 11:10:01
Comment on what lines 69-73 are doing.
sprang_webrtc
2015/09/11 12:33:47
Done.
sprang_webrtc
2015/09/11 12:33:47
Done.
|
| + delta -= kBaseTimestampRangeSizeUs; |
| + } else if (labs(delta + kBaseTimestampRangeSizeUs) < labs(delta)) { |
| + delta += kBaseTimestampRangeSizeUs; |
| + } |
| + |
| + current_offset_ms_ += delta / 1000; |
| + } |
| + last_timestamp_us_ = timestamp_us; |
| + |
| + uint16_t sequence_number = feedback.GetBaseSequence(); |
| + std::vector<int64_t> delta_vec = feedback.GetReceiveDeltasUs(); |
| + auto delta_it = delta_vec.begin(); |
| + std::vector<PacketInfo> packet_feedback_vector; |
| + packet_feedback_vector.reserve(delta_vec.size()); |
| + |
| + { |
| + rtc::CritScope cs(&lock_); |
| + size_t failed_lookups = 0; |
| + int64_t offset_us = 0; |
| + for (auto symbol : feedback.GetStatusVector()) { |
| + if (symbol != rtcp::TransportFeedback::StatusSymbol::kNotReceived) { |
| + DCHECK(delta_it != delta_vec.end()); |
| + offset_us += *(delta_it++); |
| + int64_t ms_timestamp = current_offset_ms_ + (offset_us / 1000); |
|
stefan-webrtc
2015/09/10 11:10:01
timestamp_ms
sprang_webrtc
2015/09/11 12:33:47
Done.
|
| + PacketInfo info = {ms_timestamp, 0, sequence_number, 0, false}; |
| + if (send_time_history_.GetInfo(&info, true)) { |
| + packet_feedback_vector.push_back(info); |
| + } else { |
| + ++failed_lookups; |
| + } |
| + } |
| + ++sequence_number; |
| + } |
| + DCHECK(delta_it == delta_vec.end()); |
| + if (failed_lookups > 0) { |
| + LOG(LS_WARNING) << "Failed to lookup send time for " << failed_lookups |
| + << " packet" << (failed_lookups > 1 ? "s" : "") |
| + << ". Send time history too small?"; |
| + } |
| + } |
| + DCHECK(bitrate_estimator_.get() != nullptr); |
| + bitrate_estimator_->IncomingPacketFeedbackVector(packet_feedback_vector); |
| +} |
| + |
| +void TransportFeedbackAdapter::OnReceiveBitrateChanged( |
| + const std::vector<unsigned int>& ssrcs, |
| + unsigned int bitrate) { |
| + rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate); |
| +} |
| + |
| +void TransportFeedbackAdapter::OnRttUpdate(int64_t avg_rtt_ms, |
| + int64_t max_rtt_ms) { |
| + DCHECK(bitrate_estimator_.get() != nullptr); |
| + bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| +} |
| + |
| +} // namespace webrtc |