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 #ifndef MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_FEC_CONTROLLER_PLR_BASED_H_
12 #define MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_FEC_CONTROLLER_PLR_BASED_H_
13 
14 #include <memory>
15 
16 #include "absl/types/optional.h"
17 #include "common_audio/smoothing_filter.h"
18 #include "modules/audio_coding/audio_network_adaptor/controller.h"
19 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
20 #include "modules/audio_coding/audio_network_adaptor/util/threshold_curve.h"
21 
22 namespace webrtc {
23 
24 class FecControllerPlrBased final : public Controller {
25  public:
26   struct Config {
27     // `fec_enabling_threshold` defines a curve, above which FEC should be
28     // enabled. `fec_disabling_threshold` defines a curve, under which FEC
29     // should be disabled. See below
30     //
31     // packet-loss ^   |  |
32     //             |   |  |   FEC
33     //             |    \  \   ON
34     //             | FEC \  \_______ fec_enabling_threshold
35     //             | OFF  \_________ fec_disabling_threshold
36     //             |-----------------> bandwidth
37     Config(bool initial_fec_enabled,
38            const ThresholdCurve& fec_enabling_threshold,
39            const ThresholdCurve& fec_disabling_threshold,
40            int time_constant_ms);
41     bool initial_fec_enabled;
42     ThresholdCurve fec_enabling_threshold;
43     ThresholdCurve fec_disabling_threshold;
44     int time_constant_ms;
45   };
46 
47   // Dependency injection for testing.
48   FecControllerPlrBased(const Config& config,
49                         std::unique_ptr<SmoothingFilter> smoothing_filter);
50 
51   explicit FecControllerPlrBased(const Config& config);
52 
53   ~FecControllerPlrBased() override;
54 
55   FecControllerPlrBased(const FecControllerPlrBased&) = delete;
56   FecControllerPlrBased& operator=(const FecControllerPlrBased&) = delete;
57 
58   void UpdateNetworkMetrics(const NetworkMetrics& network_metrics) override;
59 
60   void MakeDecision(AudioEncoderRuntimeConfig* config) override;
61 
62  private:
63   bool FecEnablingDecision(const absl::optional<float>& packet_loss) const;
64   bool FecDisablingDecision(const absl::optional<float>& packet_loss) const;
65 
66   const Config config_;
67   bool fec_enabled_;
68   absl::optional<int> uplink_bandwidth_bps_;
69   const std::unique_ptr<SmoothingFilter> packet_loss_smoother_;
70 };
71 
72 }  // namespace webrtc
73 
74 #endif  // MODULES_AUDIO_CODING_AUDIO_NETWORK_ADAPTOR_FEC_CONTROLLER_PLR_BASED_H_
75