Chromium Code Reviews| Index: webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.cc | 
| diff --git a/webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.cc b/webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..72e63a1d0ee7042e8e8356dcf79776d13d84564a | 
| --- /dev/null | 
| +++ b/webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.cc | 
| @@ -0,0 +1,162 @@ | 
| +/* | 
| + * 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 "webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h" | 
| + | 
| +#include "webrtc/base/checks.h" | 
| +#include "webrtc/base/logging.h" | 
| +#include "webrtc/system_wrappers/interface/clock.h" | 
| +#include "webrtc/modules/pacing/include/packet_router.h" | 
| +#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" | 
| +#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" | 
| + | 
| +namespace webrtc { | 
| + | 
| +// TODO(sprang): Tune these! | 
| +const int RemoteEstimatorProxy::kDefaultProcessIntervalMs = 200; | 
| +const int RemoteEstimatorProxy::kBackWindowMs = 500; | 
| + | 
| +RemoteEstimatorProxy::RemoteEstimatorProxy(Clock* clock, | 
| + PacketRouter* packet_router) | 
| + : clock_(clock), | 
| + packet_router_(packet_router), | 
| + last_process_time_ms_(-1), | 
| + media_ssrc_(0), | 
| + feedback_sequence_(0), | 
| + window_start_seq_(-1) { | 
| +} | 
| + | 
| +RemoteEstimatorProxy::~RemoteEstimatorProxy() { | 
| +} | 
| + | 
| +void RemoteEstimatorProxy::IncomingPacketFeedbackVector( | 
| + const std::vector<PacketInfo>& packet_feedback_vector) { | 
| + rtc::CritScope cs(&lock_); | 
| + for (PacketInfo info : packet_feedback_vector) | 
| + OnPacketArrival(info.sequence_number, info.arrival_time_ms); | 
| +} | 
| + | 
| +void RemoteEstimatorProxy::IncomingPacket(int64_t arrival_time_ms, | 
| + size_t payload_size, | 
| + const RTPHeader& header, | 
| + bool was_paced) { | 
| + DCHECK(header.extension.hasTransportSequenceNumber); | 
| + rtc::CritScope cs(&lock_); | 
| + media_ssrc_ = header.ssrc; | 
| + OnPacketArrival(header.extension.transportSequenceNumber, arrival_time_ms); | 
| +} | 
| + | 
| +void RemoteEstimatorProxy::RemoveStream(unsigned int ssrc) { | 
| +} | 
| + | 
| +bool RemoteEstimatorProxy::LatestEstimate(std::vector<unsigned int>* ssrcs, | 
| + unsigned int* bitrate_bps) const { | 
| + return false; | 
| +} | 
| + | 
| +bool RemoteEstimatorProxy::GetStats( | 
| + ReceiveBandwidthEstimatorStats* output) const { | 
| + return false; | 
| +} | 
| + | 
| +void RemoteEstimatorProxy::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { | 
| +} | 
| + | 
| +int64_t RemoteEstimatorProxy::TimeUntilNextProcess() { | 
| + int64_t now = clock_->TimeInMilliseconds(); | 
| + int64_t time_until_next = 0; | 
| + if (last_process_time_ms_ != -1 && | 
| + now - last_process_time_ms_ < kDefaultProcessIntervalMs) { | 
| + time_until_next = (last_process_time_ms_ + kDefaultProcessIntervalMs - now); | 
| 
 
stefan-webrtc
2015/09/04 07:31:36
remove the parentheses
 
sprang_webrtc
2015/09/04 13:29:44
Done.
 
 | 
| + } | 
| + return time_until_next; | 
| +} | 
| + | 
| +int32_t RemoteEstimatorProxy::Process() { | 
| + // TODO(sprang): Perhaps we need a dedicated thread here instead? | 
| 
 
stefan-webrtc
2015/09/04 07:31:36
Could you comment on why?
 
sprang_webrtc
2015/09/04 13:29:44
I no longer think we do, so I'm removing this comm
 
 | 
| + | 
| + if (TimeUntilNextProcess() > 0) | 
| + return 0; | 
| + last_process_time_ms_ = clock_->TimeInMilliseconds(); | 
| + | 
| + bool more_to_build = true; | 
| + while (more_to_build) { | 
| + rtcp::TransportFeedback feedback_packet; | 
| + if (BuildFeedbackPacket(&feedback_packet)) { | 
| + DCHECK(packet_router_ != nullptr); | 
| + packet_router_->SendFeedback(&feedback_packet); | 
| + } else { | 
| + more_to_build = false; | 
| + } | 
| + } | 
| + | 
| + return 0; | 
| +} | 
| + | 
| +void RemoteEstimatorProxy::OnPacketArrival(uint16_t sequence_number, | 
| + int64_t arrival_time) { | 
| + int64_t seq = unwrapper_.Unwrap(sequence_number); | 
| + | 
| + if (window_start_seq_ == -1) { | 
| + window_start_seq_ = seq; | 
| + // Start new feedback packet, cull old packets. | 
| + for (auto it = received_packets_.begin(); | 
| + it != received_packets_.end() && it->first < seq && | 
| + arrival_time - it->second >= kBackWindowMs;) { | 
| + auto delete_it = it; | 
| + ++it; | 
| + received_packets_.erase(delete_it); | 
| 
 
stefan-webrtc
2015/09/04 07:31:36
Prefer received_packets_.erase(delete_it++);
 
sprang_webrtc
2015/09/04 13:29:44
Done.
 
 | 
| + } | 
| + } else if (seq < window_start_seq_) { | 
| + window_start_seq_ = seq; | 
| + } | 
| + | 
| + DCHECK(received_packets_.end() == received_packets_.find(seq)); | 
| + received_packets_[seq] = arrival_time; | 
| +} | 
| + | 
| +bool RemoteEstimatorProxy::BuildFeedbackPacket( | 
| + rtcp::TransportFeedback* feedback_packet) { | 
| + rtc::CritScope cs(&lock_); | 
| + if (window_start_seq_ == -1) | 
| + return false; | 
| + | 
| + auto it = received_packets_.find(window_start_seq_); | 
| 
 
stefan-webrtc
2015/09/04 07:31:36
Shouldn't this be received_packets_.begin()? If so
 
sprang_webrtc
2015/09/04 13:29:44
No, window_start_seq_ is the first packet to be in
 
stefan-webrtc
2015/09/07 10:12:21
Right, maybe a comment on that somewhere? Or do yo
 
sprang_webrtc
2015/09/07 11:14:35
Done.
 
 | 
| + DCHECK(it != received_packets_.end()); | 
| + | 
| + // TODO(sprang): Measure receive times in microseconds and remove the | 
| + // conversions below. | 
| + feedback_packet->WithMediaSourceSsrc(media_ssrc_); | 
| + feedback_packet->WithBase(static_cast<uint16_t>(it->first & 0xFFFF), | 
| + it->second * 1000); | 
| + feedback_packet->WithFeedbackSequenceNumber(feedback_sequence_++); | 
| + for (; it != received_packets_.end(); ++it) { | 
| + if (!feedback_packet->WithReceivedPacket( | 
| + static_cast<uint16_t>(it->first & 0xFFFF), it->second * 1000)) { | 
| + if (it->first == window_start_seq_) { | 
| + LOG(LS_WARNING) << "Unable to build feedback packet for #" << it->first; | 
| 
 
stefan-webrtc
2015/09/04 07:31:36
Should this ever happen? Seems like we have failed
 
sprang_webrtc
2015/09/04 13:29:44
I'm not sure what kind of error TransportFeedback
 
stefan-webrtc
2015/09/07 10:12:21
Right. I mostly want us to be sure that it doesn't
 
sprang_webrtc
2015/09/07 11:14:35
A CHECK is perhaps in order here.
 
 | 
| + return false; | 
| + } | 
| + // Could not add timestamp, feedback packet might be full. Return and | 
| + // try again with a fresh packet. | 
| + window_start_seq_ = it->first; | 
| + break; | 
| + } | 
| + // Note: Don't erase items from received_packets_ after sending, in case | 
| + // they need to be re-sent after a reordering. Removal will be handled | 
| + // by OnPacketArrival once packets are too old. | 
| + } | 
| + if (it == received_packets_.end()) | 
| + window_start_seq_ = -1; | 
| + | 
| + return true; | 
| +} | 
| + | 
| +} // namespace webrtc |