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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// This is the implementation of the PacketBuffer class. It is mostly based on
|
|
|
|
|
// an STL list. The list is kept sorted at all times so that the next packet to
|
|
|
|
|
// decode is at the beginning of the list.
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/neteq/packet_buffer.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
#include <algorithm> // find_if()
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/audio_codecs/audio_decoder.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/decoder_database.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/statistics_calculator.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/tick_timer.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2016-09-01 23:19:05 -07:00
|
|
|
namespace {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Predicate used when inserting packets in the buffer list.
|
|
|
|
|
// Operator() returns true when |packet| goes before |new_packet|.
|
|
|
|
|
class NewTimestampIsLarger {
|
|
|
|
|
public:
|
2016-10-24 08:25:28 -07:00
|
|
|
explicit NewTimestampIsLarger(const Packet& new_packet)
|
2013-01-29 12:09:21 +00:00
|
|
|
: new_packet_(new_packet) {
|
|
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
bool operator()(const Packet& packet) {
|
|
|
|
|
return (new_packet_ >= packet);
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2016-10-24 08:25:28 -07:00
|
|
|
const Packet& new_packet_;
|
2013-01-29 12:09:21 +00:00
|
|
|
};
|
|
|
|
|
|
2016-09-01 23:19:05 -07:00
|
|
|
// Returns true if both payload types are known to the decoder database, and
|
|
|
|
|
// have the same sample rate.
|
|
|
|
|
bool EqualSampleRates(uint8_t pt1,
|
|
|
|
|
uint8_t pt2,
|
|
|
|
|
const DecoderDatabase& decoder_database) {
|
2017-02-26 19:53:40 -08:00
|
|
|
auto* di1 = decoder_database.GetDecoderInfo(pt1);
|
|
|
|
|
auto* di2 = decoder_database.GetDecoderInfo(pt2);
|
2016-09-01 23:19:05 -07:00
|
|
|
return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz();
|
|
|
|
|
}
|
2017-08-23 15:59:38 +02:00
|
|
|
|
|
|
|
|
void LogPacketDiscarded(int codec_level, StatisticsCalculator* stats) {
|
|
|
|
|
RTC_CHECK(stats);
|
|
|
|
|
if (codec_level > 0) {
|
|
|
|
|
stats->SecondaryPacketsDiscarded(1);
|
|
|
|
|
} else {
|
|
|
|
|
stats->PacketsDiscarded(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 23:19:05 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
2016-04-26 07:45:16 -07:00
|
|
|
PacketBuffer::PacketBuffer(size_t max_number_of_packets,
|
|
|
|
|
const TickTimer* tick_timer)
|
|
|
|
|
: max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {}
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Destructor. All packets in the buffer will be destroyed.
|
|
|
|
|
PacketBuffer::~PacketBuffer() {
|
|
|
|
|
Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flush the buffer. All packets in the buffer will be destroyed.
|
|
|
|
|
void PacketBuffer::Flush() {
|
2016-10-24 08:25:28 -07:00
|
|
|
buffer_.clear();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-09 15:44:22 +02:00
|
|
|
bool PacketBuffer::Empty() const {
|
|
|
|
|
return buffer_.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 11:44:06 +02:00
|
|
|
int PacketBuffer::InsertPacket(Packet&& packet, StatisticsCalculator* stats) {
|
2016-10-24 08:25:28 -07:00
|
|
|
if (packet.empty()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "InsertPacket invalid packet";
|
2013-01-29 12:09:21 +00:00
|
|
|
return kInvalidPacket;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:25:28 -07:00
|
|
|
RTC_DCHECK_GE(packet.priority.codec_level, 0);
|
|
|
|
|
RTC_DCHECK_GE(packet.priority.red_level, 0);
|
2016-09-22 02:06:28 -07:00
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
int return_val = kOK;
|
|
|
|
|
|
2016-10-24 08:25:28 -07:00
|
|
|
packet.waiting_time = tick_timer_->GetNewStopwatch();
|
2016-04-26 07:45:16 -07:00
|
|
|
|
2014-04-28 08:20:04 +00:00
|
|
|
if (buffer_.size() >= max_number_of_packets_) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Buffer is full. Flush it.
|
|
|
|
|
Flush();
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "Packet buffer flushed";
|
2013-01-29 12:09:21 +00:00
|
|
|
return_val = kFlushed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an iterator pointing to the place in the buffer where the new packet
|
|
|
|
|
// should be inserted. The list is searched from the back, since the most
|
|
|
|
|
// likely case is that the new packet should be near the end of the list.
|
|
|
|
|
PacketList::reverse_iterator rit = std::find_if(
|
|
|
|
|
buffer_.rbegin(), buffer_.rend(),
|
|
|
|
|
NewTimestampIsLarger(packet));
|
2014-10-09 10:49:54 +00:00
|
|
|
|
|
|
|
|
// The new packet is to be inserted to the right of |rit|. If it has the same
|
|
|
|
|
// timestamp as |rit|, which has a higher priority, do not insert the new
|
|
|
|
|
// packet to list.
|
2016-10-24 08:25:28 -07:00
|
|
|
if (rit != buffer_.rend() && packet.timestamp == rit->timestamp) {
|
2017-08-23 15:59:38 +02:00
|
|
|
LogPacketDiscarded(packet.priority.codec_level, stats);
|
2014-10-09 10:49:54 +00:00
|
|
|
return return_val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The new packet is to be inserted to the left of |it|. If it has the same
|
|
|
|
|
// timestamp as |it|, which has a lower priority, replace |it| with the new
|
|
|
|
|
// packet.
|
|
|
|
|
PacketList::iterator it = rit.base();
|
2016-10-24 08:25:28 -07:00
|
|
|
if (it != buffer_.end() && packet.timestamp == it->timestamp) {
|
2017-08-23 15:59:38 +02:00
|
|
|
LogPacketDiscarded(packet.priority.codec_level, stats);
|
2014-10-09 10:49:54 +00:00
|
|
|
it = buffer_.erase(it);
|
|
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
buffer_.insert(it, std::move(packet)); // Insert the packet at that position.
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
return return_val;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 03:14:11 -07:00
|
|
|
int PacketBuffer::InsertPacketList(
|
|
|
|
|
PacketList* packet_list,
|
|
|
|
|
const DecoderDatabase& decoder_database,
|
|
|
|
|
rtc::Optional<uint8_t>* current_rtp_payload_type,
|
2017-07-19 11:44:06 +02:00
|
|
|
rtc::Optional<uint8_t>* current_cng_rtp_payload_type,
|
|
|
|
|
StatisticsCalculator* stats) {
|
|
|
|
|
RTC_DCHECK(stats);
|
2013-01-29 12:09:21 +00:00
|
|
|
bool flushed = false;
|
2016-10-24 08:25:28 -07:00
|
|
|
for (auto& packet : *packet_list) {
|
|
|
|
|
if (decoder_database.IsComfortNoise(packet.payload_type)) {
|
2016-08-31 03:14:11 -07:00
|
|
|
if (*current_cng_rtp_payload_type &&
|
2016-10-24 08:25:28 -07:00
|
|
|
**current_cng_rtp_payload_type != packet.payload_type) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// New CNG payload type implies new codec type.
|
2016-08-31 03:14:11 -07:00
|
|
|
*current_rtp_payload_type = rtc::Optional<uint8_t>();
|
2013-01-29 12:09:21 +00:00
|
|
|
Flush();
|
|
|
|
|
flushed = true;
|
|
|
|
|
}
|
2016-08-31 03:14:11 -07:00
|
|
|
*current_cng_rtp_payload_type =
|
2016-10-24 08:25:28 -07:00
|
|
|
rtc::Optional<uint8_t>(packet.payload_type);
|
|
|
|
|
} else if (!decoder_database.IsDtmf(packet.payload_type)) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// This must be speech.
|
2016-09-01 23:19:05 -07:00
|
|
|
if ((*current_rtp_payload_type &&
|
2016-10-24 08:25:28 -07:00
|
|
|
**current_rtp_payload_type != packet.payload_type) ||
|
2016-09-01 23:19:05 -07:00
|
|
|
(*current_cng_rtp_payload_type &&
|
2016-10-24 08:25:28 -07:00
|
|
|
!EqualSampleRates(packet.payload_type,
|
2016-09-01 23:19:05 -07:00
|
|
|
**current_cng_rtp_payload_type,
|
|
|
|
|
decoder_database))) {
|
2016-08-31 03:14:11 -07:00
|
|
|
*current_cng_rtp_payload_type = rtc::Optional<uint8_t>();
|
2013-01-29 12:09:21 +00:00
|
|
|
Flush();
|
|
|
|
|
flushed = true;
|
|
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
*current_rtp_payload_type = rtc::Optional<uint8_t>(packet.payload_type);
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
2017-07-19 11:44:06 +02:00
|
|
|
int return_val = InsertPacket(std::move(packet), stats);
|
2013-01-29 12:09:21 +00:00
|
|
|
if (return_val == kFlushed) {
|
|
|
|
|
// The buffer flushed, but this is not an error. We can still continue.
|
|
|
|
|
flushed = true;
|
|
|
|
|
} else if (return_val != kOK) {
|
|
|
|
|
// An error occurred. Delete remaining packets in list and return.
|
2016-10-24 08:25:28 -07:00
|
|
|
packet_list->clear();
|
2013-01-29 12:09:21 +00:00
|
|
|
return return_val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
packet_list->clear();
|
2013-01-29 12:09:21 +00:00
|
|
|
return flushed ? kFlushed : kOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PacketBuffer::NextTimestamp(uint32_t* next_timestamp) const {
|
|
|
|
|
if (Empty()) {
|
|
|
|
|
return kBufferEmpty;
|
|
|
|
|
}
|
|
|
|
|
if (!next_timestamp) {
|
|
|
|
|
return kInvalidPointer;
|
|
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
*next_timestamp = buffer_.front().timestamp;
|
2013-01-29 12:09:21 +00:00
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PacketBuffer::NextHigherTimestamp(uint32_t timestamp,
|
|
|
|
|
uint32_t* next_timestamp) const {
|
|
|
|
|
if (Empty()) {
|
|
|
|
|
return kBufferEmpty;
|
|
|
|
|
}
|
|
|
|
|
if (!next_timestamp) {
|
|
|
|
|
return kInvalidPointer;
|
|
|
|
|
}
|
|
|
|
|
PacketList::const_iterator it;
|
|
|
|
|
for (it = buffer_.begin(); it != buffer_.end(); ++it) {
|
2016-10-24 08:25:28 -07:00
|
|
|
if (it->timestamp >= timestamp) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Found a packet matching the search.
|
2016-10-24 08:25:28 -07:00
|
|
|
*next_timestamp = it->timestamp;
|
2013-01-29 12:09:21 +00:00
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return kNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 04:06:13 -07:00
|
|
|
const Packet* PacketBuffer::PeekNextPacket() const {
|
2016-10-24 08:25:28 -07:00
|
|
|
return buffer_.empty() ? nullptr : &buffer_.front();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:25:28 -07:00
|
|
|
rtc::Optional<Packet> PacketBuffer::GetNextPacket() {
|
2013-01-29 12:09:21 +00:00
|
|
|
if (Empty()) {
|
|
|
|
|
// Buffer is empty.
|
2016-10-24 08:25:28 -07:00
|
|
|
return rtc::Optional<Packet>();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:25:28 -07:00
|
|
|
rtc::Optional<Packet> packet(std::move(buffer_.front()));
|
2013-01-29 12:09:21 +00:00
|
|
|
// Assert that the packet sanity checks in InsertPacket method works.
|
2016-10-24 08:25:28 -07:00
|
|
|
RTC_DCHECK(!packet->empty());
|
2013-01-29 12:09:21 +00:00
|
|
|
buffer_.pop_front();
|
2014-10-09 10:49:54 +00:00
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
return packet;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 11:17:40 +02:00
|
|
|
int PacketBuffer::DiscardNextPacket(StatisticsCalculator* stats) {
|
2013-01-29 12:09:21 +00:00
|
|
|
if (Empty()) {
|
|
|
|
|
return kBufferEmpty;
|
|
|
|
|
}
|
|
|
|
|
// Assert that the packet sanity checks in InsertPacket method works.
|
2017-08-23 15:59:38 +02:00
|
|
|
const Packet& packet = buffer_.front();
|
|
|
|
|
RTC_DCHECK(!packet.empty());
|
|
|
|
|
LogPacketDiscarded(packet.priority.codec_level, stats);
|
2016-10-24 08:25:28 -07:00
|
|
|
buffer_.pop_front();
|
2013-01-29 12:09:21 +00:00
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 11:17:40 +02:00
|
|
|
void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit,
|
|
|
|
|
uint32_t horizon_samples,
|
|
|
|
|
StatisticsCalculator* stats) {
|
2017-08-23 15:59:38 +02:00
|
|
|
buffer_.remove_if([timestamp_limit, horizon_samples, stats](const Packet& p) {
|
|
|
|
|
if (timestamp_limit == p.timestamp ||
|
|
|
|
|
!IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
LogPacketDiscarded(p.priority.codec_level, stats);
|
|
|
|
|
return true;
|
2017-07-05 07:03:34 -07:00
|
|
|
});
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-05 11:17:40 +02:00
|
|
|
void PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit,
|
|
|
|
|
StatisticsCalculator* stats) {
|
|
|
|
|
DiscardOldPackets(timestamp_limit, 0, stats);
|
2015-04-09 15:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-05 11:17:40 +02:00
|
|
|
void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type,
|
|
|
|
|
StatisticsCalculator* stats) {
|
2017-08-23 15:59:38 +02:00
|
|
|
buffer_.remove_if([payload_type, stats](const Packet& p) {
|
|
|
|
|
if (p.payload_type != payload_type) {
|
|
|
|
|
return false;
|
2016-09-20 01:38:00 -07:00
|
|
|
}
|
2017-08-23 15:59:38 +02:00
|
|
|
LogPacketDiscarded(p.priority.codec_level, stats);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2016-09-20 01:38:00 -07:00
|
|
|
}
|
|
|
|
|
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t PacketBuffer::NumPacketsInBuffer() const {
|
|
|
|
|
return buffer_.size();
|
2015-04-09 15:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-20 01:38:00 -07:00
|
|
|
size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const {
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t num_samples = 0;
|
|
|
|
|
size_t last_duration = last_decoded_length;
|
2016-10-24 08:25:28 -07:00
|
|
|
for (const Packet& packet : buffer_) {
|
|
|
|
|
if (packet.frame) {
|
2016-09-22 02:06:28 -07:00
|
|
|
// TODO(hlundin): Verify that it's fine to count all packets and remove
|
|
|
|
|
// this check.
|
2016-10-24 08:25:28 -07:00
|
|
|
if (packet.priority != Packet::Priority(0, 0)) {
|
2014-05-28 07:48:01 +00:00
|
|
|
continue;
|
2014-03-21 12:07:40 +00:00
|
|
|
}
|
2016-10-24 08:25:28 -07:00
|
|
|
size_t duration = packet.frame->Duration();
|
2016-09-20 01:38:00 -07:00
|
|
|
if (duration > 0) {
|
2013-09-26 00:27:56 +00:00
|
|
|
last_duration = duration; // Save the most up-to-date (valid) duration.
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-26 00:27:56 +00:00
|
|
|
num_samples += last_duration;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
return num_samples;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-28 08:20:04 +00:00
|
|
|
void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const {
|
2013-09-20 16:25:28 +00:00
|
|
|
*num_packets = static_cast<int>(buffer_.size());
|
|
|
|
|
*max_num_packets = static_cast<int>(max_number_of_packets_);
|
2013-08-02 18:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
} // namespace webrtc
|