Line data Source code
1 : /*
2 : * Copyright (c) 2016 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/audio_coding/audio_network_adaptor/frame_length_controller.h"
12 :
13 : #include <utility>
14 :
15 : #include "webrtc/base/checks.h"
16 : #include "webrtc/base/logging.h"
17 :
18 : namespace webrtc {
19 :
20 0 : FrameLengthController::Config::Config(
21 : const std::vector<int>& encoder_frame_lengths_ms,
22 : int initial_frame_length_ms,
23 : float fl_increasing_packet_loss_fraction,
24 : float fl_decreasing_packet_loss_fraction,
25 : int fl_20ms_to_60ms_bandwidth_bps,
26 0 : int fl_60ms_to_20ms_bandwidth_bps)
27 : : encoder_frame_lengths_ms(encoder_frame_lengths_ms),
28 : initial_frame_length_ms(initial_frame_length_ms),
29 : fl_increasing_packet_loss_fraction(fl_increasing_packet_loss_fraction),
30 : fl_decreasing_packet_loss_fraction(fl_decreasing_packet_loss_fraction),
31 : fl_20ms_to_60ms_bandwidth_bps(fl_20ms_to_60ms_bandwidth_bps),
32 0 : fl_60ms_to_20ms_bandwidth_bps(fl_60ms_to_20ms_bandwidth_bps) {}
33 :
34 : FrameLengthController::Config::Config(const Config& other) = default;
35 :
36 : FrameLengthController::Config::~Config() = default;
37 :
38 0 : FrameLengthController::FrameLengthController(const Config& config)
39 0 : : config_(config) {
40 : frame_length_ms_ = std::find(config_.encoder_frame_lengths_ms.begin(),
41 : config_.encoder_frame_lengths_ms.end(),
42 0 : config_.initial_frame_length_ms);
43 : // |encoder_frame_lengths_ms| must contain |initial_frame_length_ms|.
44 0 : RTC_DCHECK(frame_length_ms_ != config_.encoder_frame_lengths_ms.end());
45 :
46 0 : frame_length_change_criteria_.insert(std::make_pair(
47 0 : FrameLengthChange(20, 60), config_.fl_20ms_to_60ms_bandwidth_bps));
48 0 : frame_length_change_criteria_.insert(std::make_pair(
49 0 : FrameLengthChange(60, 20), config_.fl_60ms_to_20ms_bandwidth_bps));
50 0 : }
51 :
52 : FrameLengthController::~FrameLengthController() = default;
53 :
54 0 : void FrameLengthController::MakeDecision(
55 : const NetworkMetrics& metrics,
56 : AudioNetworkAdaptor::EncoderRuntimeConfig* config) {
57 : // Decision on |frame_length_ms| should not have been made.
58 0 : RTC_DCHECK(!config->frame_length_ms);
59 :
60 0 : if (FrameLengthIncreasingDecision(metrics, *config)) {
61 0 : ++frame_length_ms_;
62 0 : } else if (FrameLengthDecreasingDecision(metrics, *config)) {
63 0 : --frame_length_ms_;
64 : }
65 0 : config->frame_length_ms = rtc::Optional<int>(*frame_length_ms_);
66 0 : }
67 :
68 0 : FrameLengthController::FrameLengthChange::FrameLengthChange(
69 : int from_frame_length_ms,
70 0 : int to_frame_length_ms)
71 : : from_frame_length_ms(from_frame_length_ms),
72 0 : to_frame_length_ms(to_frame_length_ms) {}
73 :
74 : FrameLengthController::FrameLengthChange::~FrameLengthChange() = default;
75 :
76 0 : bool FrameLengthController::FrameLengthChange::operator<(
77 : const FrameLengthChange& rhs) const {
78 0 : return from_frame_length_ms < rhs.from_frame_length_ms ||
79 0 : (from_frame_length_ms == rhs.from_frame_length_ms &&
80 0 : to_frame_length_ms < rhs.to_frame_length_ms);
81 : }
82 :
83 0 : bool FrameLengthController::FrameLengthIncreasingDecision(
84 : const NetworkMetrics& metrics,
85 : const AudioNetworkAdaptor::EncoderRuntimeConfig& config) const {
86 : // Increase frame length if
87 : // 1. longer frame length is available AND
88 : // 2. |uplink_bandwidth_bps| is known to be smaller than a threshold AND
89 : // 3. |uplink_packet_loss_fraction| is known to be smaller than a threshold
90 : // AND
91 : // 4. FEC is not decided or is OFF.
92 0 : auto longer_frame_length_ms = std::next(frame_length_ms_);
93 0 : if (longer_frame_length_ms == config_.encoder_frame_lengths_ms.end())
94 0 : return false;
95 :
96 : auto increase_threshold = frame_length_change_criteria_.find(
97 0 : FrameLengthChange(*frame_length_ms_, *longer_frame_length_ms));
98 :
99 0 : if (increase_threshold == frame_length_change_criteria_.end())
100 0 : return false;
101 :
102 0 : return (metrics.uplink_bandwidth_bps &&
103 0 : *metrics.uplink_bandwidth_bps <= increase_threshold->second) &&
104 0 : (metrics.uplink_packet_loss_fraction &&
105 0 : *metrics.uplink_packet_loss_fraction <=
106 0 : config_.fl_increasing_packet_loss_fraction) &&
107 0 : !config.enable_fec.value_or(false);
108 : }
109 :
110 0 : bool FrameLengthController::FrameLengthDecreasingDecision(
111 : const NetworkMetrics& metrics,
112 : const AudioNetworkAdaptor::EncoderRuntimeConfig& config) const {
113 : // Decrease frame length if
114 : // 1. shorter frame length is available AND one or more of the followings:
115 : // 2. |uplink_bandwidth_bps| is known to be larger than a threshold,
116 : // 3. |uplink_packet_loss_fraction| is known to be larger than a threshold,
117 : // 4. FEC is decided ON.
118 0 : if (frame_length_ms_ == config_.encoder_frame_lengths_ms.begin())
119 0 : return false;
120 :
121 0 : auto shorter_frame_length_ms = std::prev(frame_length_ms_);
122 : auto decrease_threshold = frame_length_change_criteria_.find(
123 0 : FrameLengthChange(*frame_length_ms_, *shorter_frame_length_ms));
124 :
125 0 : if (decrease_threshold == frame_length_change_criteria_.end())
126 0 : return false;
127 :
128 0 : return (metrics.uplink_bandwidth_bps &&
129 0 : *metrics.uplink_bandwidth_bps >= decrease_threshold->second) ||
130 0 : (metrics.uplink_packet_loss_fraction &&
131 0 : *metrics.uplink_packet_loss_fraction >=
132 0 : config_.fl_decreasing_packet_loss_fraction) ||
133 0 : config.enable_fec.value_or(false);
134 : }
135 :
136 : } // namespace webrtc
|