blob: fafd8ec99725576c45908a550e168f438dafa0c7 [file] [log] [blame]
Sebastian Janssondf023aa2018-02-20 19:38:37 +01001/*
2 * Copyright (c) 2018 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 "call/rtp_bitrate_configurator.h"
12
13#include <algorithm>
14
15#include "rtc_base/checks.h"
16
17namespace webrtc {
18RtpBitrateConfigurator::RtpBitrateConfigurator(
19 const BitrateConstraints& bitrate_config)
20 : bitrate_config_(bitrate_config), base_bitrate_config_(bitrate_config) {
21 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
22 RTC_DCHECK_GE(bitrate_config.start_bitrate_bps,
23 bitrate_config.min_bitrate_bps);
24 if (bitrate_config.max_bitrate_bps != -1) {
25 RTC_DCHECK_GE(bitrate_config.max_bitrate_bps,
26 bitrate_config.start_bitrate_bps);
27 }
28}
29
30RtpBitrateConfigurator::~RtpBitrateConfigurator() = default;
31
32BitrateConstraints RtpBitrateConfigurator::GetConfig() const {
33 return bitrate_config_;
34}
35
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020036absl::optional<BitrateConstraints>
Sebastian Janssondf023aa2018-02-20 19:38:37 +010037RtpBitrateConfigurator::UpdateWithSdpParameters(
38 const BitrateConstraints& bitrate_config) {
39 RTC_DCHECK_GE(bitrate_config.min_bitrate_bps, 0);
40 RTC_DCHECK_NE(bitrate_config.start_bitrate_bps, 0);
41 if (bitrate_config.max_bitrate_bps != -1) {
42 RTC_DCHECK_GT(bitrate_config.max_bitrate_bps, 0);
43 }
44
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020045 absl::optional<int> new_start;
Sebastian Janssondf023aa2018-02-20 19:38:37 +010046 // Only update the "start" bitrate if it's set, and different from the old
47 // value. In practice, this value comes from the x-google-start-bitrate codec
48 // parameter in SDP, and setting the same remote description twice shouldn't
49 // restart bandwidth estimation.
50 if (bitrate_config.start_bitrate_bps != -1 &&
51 bitrate_config.start_bitrate_bps !=
52 base_bitrate_config_.start_bitrate_bps) {
53 new_start.emplace(bitrate_config.start_bitrate_bps);
54 }
55 base_bitrate_config_ = bitrate_config;
56 return UpdateConstraints(new_start);
57}
58
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020059absl::optional<BitrateConstraints>
Sebastian Janssondf023aa2018-02-20 19:38:37 +010060RtpBitrateConfigurator::UpdateWithClientPreferences(
Niels Möller0c4f7be2018-05-07 14:01:37 +020061 const BitrateSettings& bitrate_mask) {
Sebastian Janssondf023aa2018-02-20 19:38:37 +010062 bitrate_config_mask_ = bitrate_mask;
63 return UpdateConstraints(bitrate_mask.start_bitrate_bps);
64}
65
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020066absl::optional<BitrateConstraints> RtpBitrateConfigurator::UpdateConstraints(
67 const absl::optional<int>& new_start) {
Sebastian Janssondf023aa2018-02-20 19:38:37 +010068 BitrateConstraints updated;
69 updated.min_bitrate_bps =
70 std::max(bitrate_config_mask_.min_bitrate_bps.value_or(0),
71 base_bitrate_config_.min_bitrate_bps);
72
73 updated.max_bitrate_bps =
74 MinPositive(bitrate_config_mask_.max_bitrate_bps.value_or(-1),
75 base_bitrate_config_.max_bitrate_bps);
76
77 // If the combined min ends up greater than the combined max, the max takes
78 // priority.
79 if (updated.max_bitrate_bps != -1 &&
80 updated.min_bitrate_bps > updated.max_bitrate_bps) {
81 updated.min_bitrate_bps = updated.max_bitrate_bps;
82 }
83
84 // If there is nothing to update (min/max unchanged, no new bandwidth
85 // estimation start value), return early.
86 if (updated.min_bitrate_bps == bitrate_config_.min_bitrate_bps &&
87 updated.max_bitrate_bps == bitrate_config_.max_bitrate_bps &&
88 !new_start) {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020089 return absl::nullopt;
Sebastian Janssondf023aa2018-02-20 19:38:37 +010090 }
91
92 if (new_start) {
93 // Clamp start by min and max.
94 updated.start_bitrate_bps = MinPositive(
95 std::max(*new_start, updated.min_bitrate_bps), updated.max_bitrate_bps);
96 } else {
97 updated.start_bitrate_bps = -1;
98 }
99 BitrateConstraints config_to_return = updated;
100 if (!new_start) {
101 updated.start_bitrate_bps = bitrate_config_.start_bitrate_bps;
102 }
103 bitrate_config_ = updated;
104 return config_to_return;
105}
106
107} // namespace webrtc