OLD | NEW |
(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/remote_estimator_proxy.h" |
| 12 |
| 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/system_wrappers/interface/clock.h" |
| 16 #include "webrtc/modules/pacing/include/packet_router.h" |
| 17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
| 18 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 // TODO(sprang): Tune these! |
| 23 const int RemoteEstimatorProxy::kDefaultProcessIntervalMs = 200; |
| 24 const int RemoteEstimatorProxy::kBackWindowMs = 500; |
| 25 |
| 26 RemoteEstimatorProxy::RemoteEstimatorProxy(Clock* clock, |
| 27 PacketRouter* packet_router) |
| 28 : clock_(clock), |
| 29 packet_router_(packet_router), |
| 30 last_process_time_ms_(-1), |
| 31 media_ssrc_(0), |
| 32 feedback_sequence_(0), |
| 33 window_start_seq_(-1) {} |
| 34 |
| 35 RemoteEstimatorProxy::~RemoteEstimatorProxy() {} |
| 36 |
| 37 void RemoteEstimatorProxy::IncomingPacketFeedbackVector( |
| 38 const std::vector<PacketInfo>& packet_feedback_vector) { |
| 39 rtc::CritScope cs(&lock_); |
| 40 for (PacketInfo info : packet_feedback_vector) |
| 41 OnPacketArrival(info.sequence_number, info.arrival_time_ms); |
| 42 } |
| 43 |
| 44 void RemoteEstimatorProxy::IncomingPacket(int64_t arrival_time_ms, |
| 45 size_t payload_size, |
| 46 const RTPHeader& header, |
| 47 bool was_paced) { |
| 48 DCHECK(header.extension.hasTransportSequenceNumber); |
| 49 rtc::CritScope cs(&lock_); |
| 50 media_ssrc_ = header.ssrc; |
| 51 OnPacketArrival(header.extension.transportSequenceNumber, arrival_time_ms); |
| 52 } |
| 53 |
| 54 void RemoteEstimatorProxy::RemoveStream(unsigned int ssrc) {} |
| 55 |
| 56 bool RemoteEstimatorProxy::LatestEstimate(std::vector<unsigned int>* ssrcs, |
| 57 unsigned int* bitrate_bps) const { |
| 58 return false; |
| 59 } |
| 60 |
| 61 bool RemoteEstimatorProxy::GetStats( |
| 62 ReceiveBandwidthEstimatorStats* output) const { |
| 63 return false; |
| 64 } |
| 65 |
| 66 void RemoteEstimatorProxy::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
| 67 } |
| 68 |
| 69 int64_t RemoteEstimatorProxy::TimeUntilNextProcess() { |
| 70 int64_t now = clock_->TimeInMilliseconds(); |
| 71 int64_t time_until_next = 0; |
| 72 if (last_process_time_ms_ != -1 && |
| 73 now - last_process_time_ms_ < kDefaultProcessIntervalMs) { |
| 74 time_until_next = (last_process_time_ms_ + kDefaultProcessIntervalMs - now); |
| 75 } |
| 76 return time_until_next; |
| 77 } |
| 78 |
| 79 int32_t RemoteEstimatorProxy::Process() { |
| 80 // TODO(sprang): Perhaps we need a dedicated thread here instead? |
| 81 |
| 82 if (TimeUntilNextProcess() > 0) |
| 83 return 0; |
| 84 last_process_time_ms_ = clock_->TimeInMilliseconds(); |
| 85 |
| 86 bool more_to_build = true; |
| 87 while (more_to_build) { |
| 88 rtcp::TransportFeedback feedback_packet; |
| 89 if (BuildFeedbackPacket(&feedback_packet)) { |
| 90 DCHECK(packet_router_ != nullptr); |
| 91 packet_router_->SendFeedback(&feedback_packet); |
| 92 } else { |
| 93 more_to_build = false; |
| 94 } |
| 95 } |
| 96 |
| 97 return 0; |
| 98 } |
| 99 |
| 100 void RemoteEstimatorProxy::OnPacketArrival(uint16_t sequence_number, |
| 101 int64_t arrival_time) { |
| 102 int64_t seq = unwrapper_.Unwrap(sequence_number); |
| 103 |
| 104 if (window_start_seq_ == -1) { |
| 105 window_start_seq_ = seq; |
| 106 // Start new feedback packet, cull old packets. |
| 107 for (auto it = packet_arrival_times_.begin(); |
| 108 it != packet_arrival_times_.end() && it->first < seq && |
| 109 arrival_time - it->second >= kBackWindowMs;) { |
| 110 auto delete_it = it; |
| 111 ++it; |
| 112 packet_arrival_times_.erase(delete_it); |
| 113 } |
| 114 } else if (seq < window_start_seq_) { |
| 115 window_start_seq_ = seq; |
| 116 } |
| 117 |
| 118 DCHECK(packet_arrival_times_.end() == packet_arrival_times_.find(seq)); |
| 119 packet_arrival_times_[seq] = arrival_time; |
| 120 } |
| 121 |
| 122 bool RemoteEstimatorProxy::BuildFeedbackPacket( |
| 123 rtcp::TransportFeedback* feedback_packet) { |
| 124 rtc::CritScope cs(&lock_); |
| 125 if (window_start_seq_ == -1) |
| 126 return false; |
| 127 |
| 128 // window_start_seq_ is the first sequence number to include in the current |
| 129 // feedback packet. Some older may still be in the map, in case a reordering |
| 130 // happens and we need to retransmit them. |
| 131 auto it = packet_arrival_times_.find(window_start_seq_); |
| 132 DCHECK(it != packet_arrival_times_.end()); |
| 133 |
| 134 // TODO(sprang): Measure receive times in microseconds and remove the |
| 135 // conversions below. |
| 136 feedback_packet->WithMediaSourceSsrc(media_ssrc_); |
| 137 feedback_packet->WithBase(static_cast<uint16_t>(it->first & 0xFFFF), |
| 138 it->second * 1000); |
| 139 feedback_packet->WithFeedbackSequenceNumber(feedback_sequence_++); |
| 140 for (; it != packet_arrival_times_.end(); ++it) { |
| 141 if (!feedback_packet->WithReceivedPacket( |
| 142 static_cast<uint16_t>(it->first & 0xFFFF), it->second * 1000)) { |
| 143 // If we can't even add the first seq to the feedback packet, we won't be |
| 144 // able to build it at all. |
| 145 CHECK_NE(window_start_seq_, it->first); |
| 146 |
| 147 // Could not add timestamp, feedback packet might be full. Return and |
| 148 // try again with a fresh packet. |
| 149 window_start_seq_ = it->first; |
| 150 break; |
| 151 } |
| 152 // Note: Don't erase items from packet_arrival_times_ after sending, in case |
| 153 // they need to be re-sent after a reordering. Removal will be handled |
| 154 // by OnPacketArrival once packets are too old. |
| 155 } |
| 156 if (it == packet_arrival_times_.end()) |
| 157 window_start_seq_ = -1; |
| 158 |
| 159 return true; |
| 160 } |
| 161 |
| 162 } // namespace webrtc |
OLD | NEW |