Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | namespace webrtc { |
| 18 | RtpBitrateConfigurator::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 | |
| 30 | RtpBitrateConfigurator::~RtpBitrateConfigurator() = default; |
| 31 | |
| 32 | BitrateConstraints RtpBitrateConfigurator::GetConfig() const { |
| 33 | return bitrate_config_; |
| 34 | } |
| 35 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 36 | absl::optional<BitrateConstraints> |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 37 | RtpBitrateConfigurator::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 Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 45 | absl::optional<int> new_start; |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 46 | // 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 Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 59 | absl::optional<BitrateConstraints> |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 60 | RtpBitrateConfigurator::UpdateWithClientPreferences( |
Niels Möller | 0c4f7be | 2018-05-07 14:01:37 +0200 | [diff] [blame] | 61 | const BitrateSettings& bitrate_mask) { |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 62 | bitrate_config_mask_ = bitrate_mask; |
| 63 | return UpdateConstraints(bitrate_mask.start_bitrate_bps); |
| 64 | } |
| 65 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 66 | absl::optional<BitrateConstraints> RtpBitrateConfigurator::UpdateConstraints( |
| 67 | const absl::optional<int>& new_start) { |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 68 | 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 Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 89 | return absl::nullopt; |
Sebastian Jansson | df023aa | 2018-02-20 19:38:37 +0100 | [diff] [blame] | 90 | } |
| 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 |