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