blob: 8857ee4b4a8ef8b3a73752ca2a871a83c5fdc945 [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
2 * Copyright (c) 2012 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
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000012#include "webrtc/modules/bitrate_controller/bitrate_controller_impl.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000013
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000014#include <algorithm>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000015#include <utility>
16
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000017#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000018
19namespace webrtc {
20
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000021class BitrateControllerImpl::RtcpBandwidthObserverImpl
22 : public RtcpBandwidthObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000023 public:
24 explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner)
25 : owner_(owner) {
26 }
27 virtual ~RtcpBandwidthObserverImpl() {
28 }
29 // Received RTCP REMB or TMMBR.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000030 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000031 owner_->OnReceivedEstimatedBitrate(bitrate);
32 }
33 // Received RTCP receiver block.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
35 int64_t rtt,
36 int64_t now_ms) override {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000037 if (report_blocks.empty())
38 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000039
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000040 int fraction_lost_aggregate = 0;
41 int total_number_of_packets = 0;
42
43 // Compute the a weighted average of the fraction loss from all report
44 // blocks.
45 for (ReportBlockList::const_iterator it = report_blocks.begin();
46 it != report_blocks.end(); ++it) {
47 std::map<uint32_t, uint32_t>::iterator seq_num_it =
48 ssrc_to_last_received_extended_high_seq_num_.find(it->sourceSSRC);
49
50 int number_of_packets = 0;
51 if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end())
52 number_of_packets = it->extendedHighSeqNum -
53 seq_num_it->second;
54
55 fraction_lost_aggregate += number_of_packets * it->fractionLost;
56 total_number_of_packets += number_of_packets;
57
58 // Update last received for this SSRC.
59 ssrc_to_last_received_extended_high_seq_num_[it->sourceSSRC] =
60 it->extendedHighSeqNum;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000061 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000062 if (total_number_of_packets == 0)
63 fraction_lost_aggregate = 0;
64 else
65 fraction_lost_aggregate = (fraction_lost_aggregate +
66 total_number_of_packets / 2) / total_number_of_packets;
67 if (fraction_lost_aggregate > 255)
68 return;
69
70 owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt,
71 total_number_of_packets, now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000072 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000073
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000074 private:
75 std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_;
76 BitrateControllerImpl* owner_;
77};
78
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000079BitrateController* BitrateController::CreateBitrateController(
andresp@webrtc.org44caf012014-03-26 21:00:21 +000080 Clock* clock,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000081 BitrateObserver* observer) {
82 return new BitrateControllerImpl(clock, observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000083}
84
sprang@webrtc.org9b791972014-12-18 11:53:59 +000085BitrateControllerImpl::BitrateControllerImpl(Clock* clock,
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000086 BitrateObserver* observer)
andresp@webrtc.org44caf012014-03-26 21:00:21 +000087 : clock_(clock),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000088 observer_(observer),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000089 last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000090 bandwidth_estimation_(),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000091 reserved_bitrate_bps_(0),
92 last_bitrate_bps_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000093 last_fraction_loss_(0),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000094 last_rtt_ms_(0),
sprang@webrtc.org8bd2f402015-03-16 14:11:21 +000095 last_reserved_bitrate_bps_(0) {
Stefan Holmere5904162015-03-26 11:11:06 +010096 // This calls the observer_, which means that the observer provided by the
97 // user must be ready to accept a bitrate update when it constructs the
98 // controller. We do this to avoid having to keep synchronized initial values
99 // in both the controller and the allocator.
100 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000101}
102
103RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
104 return new RtcpBandwidthObserverImpl(this);
105}
106
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000107void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100108 {
sprang867fb522015-08-03 04:38:41 -0700109 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100110 bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
111 }
112 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000113}
114
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000115void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
116 int max_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100117 {
sprang867fb522015-08-03 04:38:41 -0700118 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100119 bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
120 }
121 MaybeTriggerOnNetworkChanged();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000122}
123
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000124void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000125 {
sprang867fb522015-08-03 04:38:41 -0700126 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127 reserved_bitrate_bps_ = reserved_bitrate_bps;
128 }
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000129 MaybeTriggerOnNetworkChanged();
130}
131
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000132void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000133 {
sprang867fb522015-08-03 04:38:41 -0700134 rtc::CritScope cs(&critsect_);
stefanb6b0b922015-09-04 03:04:56 -0700135 bandwidth_estimation_.UpdateReceiverEstimate(clock_->TimeInMilliseconds(),
136 bitrate);
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000137 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000138 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000139}
140
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000141int64_t BitrateControllerImpl::TimeUntilNextProcess() {
142 const int64_t kBitrateControllerUpdateIntervalMs = 25;
sprang867fb522015-08-03 04:38:41 -0700143 rtc::CritScope cs(&critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000144 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000145 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000146 return std::max<int64_t>(
147 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000148}
149
150int32_t BitrateControllerImpl::Process() {
151 if (TimeUntilNextProcess() > 0)
152 return 0;
153 {
sprang867fb522015-08-03 04:38:41 -0700154 rtc::CritScope cs(&critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000155 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000156 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000157 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000158 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
159 return 0;
160}
161
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000162void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000163 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000164 int64_t rtt,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000165 int number_of_packets,
166 int64_t now_ms) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167 {
sprang867fb522015-08-03 04:38:41 -0700168 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169 bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
170 number_of_packets, now_ms);
171 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000172 MaybeTriggerOnNetworkChanged();
173}
174
175void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
176 uint32_t bitrate;
177 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000178 int64_t rtt;
Stefan Holmere5904162015-03-26 11:11:06 +0100179 if (GetNetworkParameters(&bitrate, &fraction_loss, &rtt))
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000180 observer_->OnNetworkChanged(bitrate, fraction_loss, rtt);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000181}
182
Stefan Holmere5904162015-03-26 11:11:06 +0100183bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
184 uint8_t* fraction_loss,
185 int64_t* rtt) {
sprang867fb522015-08-03 04:38:41 -0700186 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100187 int current_bitrate;
188 bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
189 *bitrate = current_bitrate;
190 *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
191 *bitrate =
192 std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
193
194 bool new_bitrate = false;
195 if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
196 *rtt != last_rtt_ms_ ||
197 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
198 last_bitrate_bps_ = *bitrate;
199 last_fraction_loss_ = *fraction_loss;
200 last_rtt_ms_ = *rtt;
201 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
202 new_bitrate = true;
203 }
204 return new_bitrate;
205}
206
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000207bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
sprang867fb522015-08-03 04:38:41 -0700208 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100209 int bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000210 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000211 int64_t rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000212 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
Stefan Holmere5904162015-03-26 11:11:06 +0100213 if (bitrate > 0) {
214 bitrate = bitrate - std::min<int>(bitrate, reserved_bitrate_bps_);
215 bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
216 *bandwidth = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000217 return true;
218 }
219 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000220}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221} // namespace webrtc