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