tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decision_logic.h (6903B)


      1 /*
      2 *  Copyright (c) 2013 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_NETEQ_DECISION_LOGIC_H_
     12 #define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <memory>
     17 #include <optional>
     18 
     19 #include "api/environment/environment.h"
     20 #include "api/neteq/delay_manager_interface.h"
     21 #include "api/neteq/neteq.h"
     22 #include "api/neteq/neteq_controller.h"
     23 #include "api/neteq/tick_timer.h"
     24 #include "modules/audio_coding/neteq/buffer_level_filter.h"
     25 #include "modules/audio_coding/neteq/delay_constraints.h"
     26 #include "modules/audio_coding/neteq/packet_arrival_history.h"
     27 
     28 namespace webrtc {
     29 
     30 // This is the class for the decision tree implementation.
     31 class DecisionLogic : public NetEqController {
     32 public:
     33  DecisionLogic(const Environment& env,
     34                NetEqController::Config config,
     35                std::unique_ptr<DelayManagerInterface> delay_manager);
     36  DecisionLogic(
     37      NetEqController::Config config,
     38      std::unique_ptr<DelayManagerInterface> delay_manager,
     39      std::unique_ptr<BufferLevelFilter> buffer_level_filter,
     40      std::unique_ptr<PacketArrivalHistory> packet_arrival_history = nullptr);
     41 
     42  ~DecisionLogic() override;
     43 
     44  DecisionLogic(const DecisionLogic&) = delete;
     45  DecisionLogic& operator=(const DecisionLogic&) = delete;
     46 
     47  // Not used.
     48  void Reset() override {}
     49 
     50  // Resets parts of the state. Typically done when switching codecs.
     51  void SoftReset() override;
     52 
     53  // Sets the sample rate and the output block size.
     54  void SetSampleRate(int fs_hz, size_t output_size_samples) override;
     55 
     56  // Given info about the latest received packet, and current jitter buffer
     57  // status, returns the operation. `target_timestamp` and `expand_mutefactor`
     58  // are provided for reference. `last_packet_samples` is the number of samples
     59  // obtained from the last decoded frame. If there is a packet available, it
     60  // should be supplied in `packet`; otherwise it should be NULL. The mode
     61  // resulting from the last call to NetEqImpl::GetAudio is supplied in
     62  // `last_mode`. If there is a DTMF event to play, `play_dtmf` should be set to
     63  // true. The output variable `reset_decoder` will be set to true if a reset is
     64  // required; otherwise it is left unchanged (i.e., it can remain true if it
     65  // was true before the call).
     66  NetEq::Operation GetDecision(const NetEqController::NetEqStatus& status,
     67                               bool* reset_decoder) override;
     68 
     69  void ExpandDecision(NetEq::Operation /* operation */) override {}
     70 
     71  // Adds `value` to `sample_memory_`.
     72  void AddSampleMemory(int32_t value) override { sample_memory_ += value; }
     73 
     74  int TargetLevelMs() const override;
     75 
     76  int UnlimitedTargetLevelMs() const override;
     77 
     78  std::optional<int> PacketArrived(int fs_hz,
     79                                   bool should_update_stats,
     80                                   const PacketArrivedInfo& info) override;
     81 
     82  void RegisterEmptyPacket() override {}
     83 
     84  bool SetMaximumDelay(int delay_ms) override {
     85    return delay_constraints_.SetMaximumDelay(delay_ms);
     86  }
     87  bool SetMinimumDelay(int delay_ms) override {
     88    return delay_constraints_.SetMinimumDelay(delay_ms);
     89  }
     90  bool SetBaseMinimumDelay(int delay_ms) override {
     91    return delay_constraints_.SetBaseMinimumDelay(delay_ms);
     92  }
     93  int GetBaseMinimumDelay() const override {
     94    return delay_constraints_.GetBaseMinimumDelay();
     95  }
     96  bool PeakFound() const override { return false; }
     97 
     98  int GetFilteredBufferLevel() const override;
     99 
    100  // Accessors and mutators.
    101  void set_sample_memory(int32_t value) override { sample_memory_ = value; }
    102  size_t noise_fast_forward() const override { return noise_fast_forward_; }
    103  size_t packet_length_samples() const override {
    104    return packet_length_samples_;
    105  }
    106  void set_packet_length_samples(size_t value) override {
    107    packet_length_samples_ = value;
    108  }
    109  void set_prev_time_scale(bool value) override { prev_time_scale_ = value; }
    110 
    111 private:
    112  // The value 5 sets maximum time-stretch rate to about 100 ms/s.
    113  static const int kMinTimescaleInterval = 5;
    114 
    115  // Updates the `buffer_level_filter_` with the current buffer level
    116  // `buffer_size_samples`.
    117  void FilterBufferLevel(size_t buffer_size_samples);
    118 
    119  // Returns the operation given that the next available packet is a comfort
    120  // noise payload (RFC 3389 only, not codec-internal).
    121  virtual NetEq::Operation CngOperation(NetEqController::NetEqStatus status);
    122 
    123  // Returns the operation given that no packets are available (except maybe
    124  // a DTMF event, flagged by setting `play_dtmf` true).
    125  virtual NetEq::Operation NoPacket(NetEqController::NetEqStatus status);
    126 
    127  // Returns the operation to do given that the expected packet is available.
    128  virtual NetEq::Operation ExpectedPacketAvailable(
    129      NetEqController::NetEqStatus status);
    130 
    131  // Returns the operation to do given that the expected packet is not
    132  // available, but a packet further into the future is at hand.
    133  virtual NetEq::Operation FuturePacketAvailable(
    134      NetEqController::NetEqStatus status);
    135 
    136  // Checks if enough time has elapsed since the last successful timescale
    137  // operation was done (i.e., accelerate or preemptive expand).
    138  bool TimescaleAllowed() const {
    139    return !timescale_countdown_ || timescale_countdown_->Finished();
    140  }
    141 
    142  // Checks if the current (filtered) buffer level is under the target level.
    143  bool UnderTargetLevel() const;
    144 
    145  // Checks if an ongoing concealment should be continued due to low buffer
    146  // level, even though the next packet is available.
    147  bool PostponeDecode(NetEqController::NetEqStatus status) const;
    148 
    149  // Checks if we still have not done enough expands to cover the distance from
    150  // the last decoded packet to the next available packet.
    151  bool PacketTooEarly(NetEqController::NetEqStatus status) const;
    152 
    153  int GetPlayoutDelayMs(NetEqController::NetEqStatus status) const;
    154 
    155  std::unique_ptr<DelayManagerInterface> delay_manager_;
    156  DelayConstraints delay_constraints_;
    157  std::unique_ptr<BufferLevelFilter> buffer_level_filter_;
    158  std::unique_ptr<PacketArrivalHistory> packet_arrival_history_;
    159  const TickTimer* tick_timer_;
    160  int sample_rate_khz_;
    161  size_t output_size_samples_;
    162  size_t noise_fast_forward_ = 0;
    163  size_t packet_length_samples_ = 0;
    164  int sample_memory_ = 0;
    165  bool prev_time_scale_ = false;
    166  bool disallow_time_stretching_;
    167  std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
    168  int time_stretched_cn_samples_ = 0;
    169  bool buffer_flush_ = false;
    170 };
    171 
    172 }  // namespace webrtc
    173 #endif  // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_