2013-01-29 12:09:21 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
|
|
|
|
|
#define MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <string.h> // Provide access to size_t.
|
|
|
|
|
|
2019-02-27 10:08:09 +01:00
|
|
|
#include <deque>
|
2016-04-28 23:19:20 -07:00
|
|
|
#include <memory>
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2018-10-04 11:31:03 +02:00
|
|
|
#include "absl/types/optional.h"
|
2019-10-31 14:38:11 +01:00
|
|
|
#include "api/neteq/tick_timer.h"
|
2019-02-21 15:42:31 +01:00
|
|
|
#include "modules/audio_coding/neteq/histogram.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/constructor_magic.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class DelayManager {
|
|
|
|
|
public:
|
2020-10-06 18:12:07 +02:00
|
|
|
DelayManager(int max_packets_in_buffer,
|
2019-02-21 15:42:31 +01:00
|
|
|
int base_minimum_delay_ms,
|
2019-02-27 10:08:09 +01:00
|
|
|
int histogram_quantile,
|
2019-02-21 15:42:31 +01:00
|
|
|
const TickTimer* tick_timer,
|
2019-02-27 10:08:09 +01:00
|
|
|
std::unique_ptr<Histogram> histogram);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Create a DelayManager object. Notify the delay manager that the packet
|
|
|
|
|
// buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
|
2018-11-27 15:45:20 +01:00
|
|
|
// is the number of packet slots in the buffer) and that the target delay
|
2019-02-13 14:25:39 +01:00
|
|
|
// should be greater than or equal to |base_minimum_delay_ms|. Supply a
|
2018-11-27 15:45:20 +01:00
|
|
|
// PeakDetector object to the DelayManager.
|
2020-10-06 18:12:07 +02:00
|
|
|
static std::unique_ptr<DelayManager> Create(int max_packets_in_buffer,
|
2019-02-21 15:42:31 +01:00
|
|
|
int base_minimum_delay_ms,
|
2019-10-24 15:20:39 +02:00
|
|
|
const TickTimer* tick_timer);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2013-07-31 15:54:00 +00:00
|
|
|
virtual ~DelayManager();
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2020-10-06 18:12:07 +02:00
|
|
|
// Updates the delay manager with a new incoming packet, with |timestamp| from
|
|
|
|
|
// the RTP header. This updates the statistics and a new target buffer level
|
|
|
|
|
// is calculated. Returns the relative delay if it can be calculated. If
|
|
|
|
|
// |reset| is true, restarts the relative arrival delay calculation from this
|
|
|
|
|
// packet.
|
|
|
|
|
virtual absl::optional<int> Update(uint32_t timestamp,
|
|
|
|
|
int sample_rate_hz,
|
|
|
|
|
bool reset = false);
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2020-10-06 18:12:07 +02:00
|
|
|
// Resets all state.
|
2013-01-29 12:09:21 +00:00
|
|
|
virtual void Reset();
|
|
|
|
|
|
2020-10-06 18:12:07 +02:00
|
|
|
// Gets the target buffer level in milliseconds.
|
|
|
|
|
virtual int TargetDelayMs() const;
|
2020-10-06 15:37:28 +00:00
|
|
|
|
2020-10-06 18:12:07 +02:00
|
|
|
// Notifies the DelayManager of how much audio data is carried in each packet.
|
|
|
|
|
virtual int SetPacketAudioLength(int length_ms);
|
Handle padded audio packets correctly
RTP packets can be padded with extra data at the end of the payload. The usable
payload length of the packet should then be reduced with the padding length,
since the padding must be discarded. This was not the case; instead, the entire
payload, including padding data, was forwarded to the audio channel and in the
end to the decoder.
A special case of padding is packets which are empty except for the padding.
That is, they carry no usable payload. These packets are sometimes used for
probing the network and were discarded in
RTPReceiverAudio::ParseAudioCodecSpecific. The result is that NetEq never sees
those empty packets, just the holes in the sequence number series; this can
throw off the target buffer calculations.
With this change, the empty (after removing the padding) packets are let through,
all the way down to NetEq, to a new method called NetEq::InsertEmptyPacket. This
method notifies the DelayManager that an empty packet was received.
BUG=webrtc:7610, webrtc:7625
Review-Url: https://codereview.webrtc.org/2870043003
Cr-Commit-Position: refs/heads/master@{#18083}
2017-05-10 07:38:01 -07:00
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
// Accessors and mutators.
|
2013-08-16 23:44:24 +00:00
|
|
|
// Assuming |delay| is in valid range.
|
|
|
|
|
virtual bool SetMinimumDelay(int delay_ms);
|
|
|
|
|
virtual bool SetMaximumDelay(int delay_ms);
|
2019-02-04 16:17:31 +01:00
|
|
|
virtual bool SetBaseMinimumDelay(int delay_ms);
|
|
|
|
|
virtual int GetBaseMinimumDelay() const;
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2020-10-06 18:12:07 +02:00
|
|
|
// These accessors are only intended for testing purposes.
|
2019-02-13 14:25:39 +01:00
|
|
|
int effective_minimum_delay_ms_for_test() const {
|
|
|
|
|
return effective_minimum_delay_ms_;
|
|
|
|
|
}
|
2019-02-27 10:08:09 +01:00
|
|
|
int histogram_quantile() const { return histogram_quantile_; }
|
2019-05-22 16:54:09 +02:00
|
|
|
Histogram* histogram() const { return histogram_.get(); }
|
2019-02-27 10:08:09 +01:00
|
|
|
|
2018-10-04 11:31:03 +02:00
|
|
|
private:
|
2019-02-13 14:25:39 +01:00
|
|
|
// Provides value which minimum delay can't exceed based on current buffer
|
|
|
|
|
// size and given |maximum_delay_ms_|. Lower bound is a constant 0.
|
|
|
|
|
int MinimumDelayUpperBound() const;
|
|
|
|
|
|
2019-02-27 10:08:09 +01:00
|
|
|
// Updates |delay_history_|.
|
2019-08-22 15:00:16 +02:00
|
|
|
void UpdateDelayHistory(int iat_delay_ms,
|
|
|
|
|
uint32_t timestamp,
|
|
|
|
|
int sample_rate_hz);
|
2019-02-27 10:08:09 +01:00
|
|
|
|
|
|
|
|
// Calculate relative packet arrival delay from |delay_history_|.
|
|
|
|
|
int CalculateRelativePacketArrivalDelay() const;
|
|
|
|
|
|
2019-02-13 14:25:39 +01:00
|
|
|
// Updates |effective_minimum_delay_ms_| delay based on current
|
|
|
|
|
// |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_|
|
|
|
|
|
// and buffer size.
|
|
|
|
|
void UpdateEffectiveMinimumDelay();
|
|
|
|
|
|
2019-02-04 16:17:31 +01:00
|
|
|
// Makes sure that |delay_ms| is less than maximum delay, if any maximum
|
|
|
|
|
// is set. Also, if possible check |delay_ms| to be less than 75% of
|
|
|
|
|
// |max_packets_in_buffer_|.
|
2019-02-13 14:25:39 +01:00
|
|
|
bool IsValidMinimumDelay(int delay_ms) const;
|
|
|
|
|
|
|
|
|
|
bool IsValidBaseMinimumDelay(int delay_ms) const;
|
2019-02-04 16:17:31 +01:00
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
bool first_packet_received_;
|
2020-10-06 18:12:07 +02:00
|
|
|
// TODO(jakobi): set maximum buffer delay instead of number of packets.
|
|
|
|
|
const int max_packets_in_buffer_;
|
2019-02-27 10:08:09 +01:00
|
|
|
std::unique_ptr<Histogram> histogram_;
|
|
|
|
|
const int histogram_quantile_;
|
2016-04-28 23:19:20 -07:00
|
|
|
const TickTimer* tick_timer_;
|
2019-02-13 14:25:39 +01:00
|
|
|
int base_minimum_delay_ms_;
|
2020-10-06 18:12:07 +02:00
|
|
|
int effective_minimum_delay_ms_; // Used as lower bound for target delay.
|
|
|
|
|
int minimum_delay_ms_; // Externally set minimum delay.
|
|
|
|
|
int maximum_delay_ms_; // Externally set maximum allowed delay.
|
|
|
|
|
|
|
|
|
|
int packet_len_ms_ = 0;
|
|
|
|
|
std::unique_ptr<TickTimer::Stopwatch>
|
|
|
|
|
packet_iat_stopwatch_; // Time elapsed since last packet.
|
|
|
|
|
int target_level_ms_; // Currently preferred buffer level.
|
|
|
|
|
uint32_t last_timestamp_; // Timestamp for the last received packet.
|
2019-08-22 15:00:16 +02:00
|
|
|
|
|
|
|
|
struct PacketDelay {
|
|
|
|
|
int iat_delay_ms;
|
|
|
|
|
uint32_t timestamp;
|
|
|
|
|
};
|
|
|
|
|
std::deque<PacketDelay> delay_history_;
|
|
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
|
2013-01-29 12:09:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
|