mirror of
https://github.com/danog/libtgvoip.git
synced 2024-11-26 12:14:39 +01:00
Jitter buffer bugfixes
This commit is contained in:
parent
5367ab3e68
commit
5ba6e99882
@ -251,6 +251,7 @@ int tgvoip::OpusDecoder::DecodeNextFrame()
|
||||
int16_t* samples = reinterpret_cast<int16_t*>(decodeBuffer);
|
||||
constexpr float coeffs[] = {0.999802f, 0.995062f, 0.984031f, 0.966778f, 0.943413f, 0.914084f, 0.878975f, 0.838309f, 0.792344f, 0.741368f,
|
||||
0.685706f, 0.625708f, 0.561754f, 0.494249f, 0.423619f, 0.350311f, 0.274788f, 0.197527f, 0.119018f, 0.039757f};
|
||||
// But why 20 samples? Typically the sample size is 60....
|
||||
for (int i = 0; i < 20; i++) {
|
||||
samples[i] = static_cast<int16_t>(round(plcSamples[i] * coeffs[i] + samples[i] * (1.f - coeffs[i])));
|
||||
}
|
||||
@ -261,8 +262,7 @@ int tgvoip::OpusDecoder::DecodeNextFrame()
|
||||
}
|
||||
else
|
||||
{ // do packet loss concealment
|
||||
consecutiveLostPackets++;
|
||||
if (consecutiveLostPackets > 2 && enableDTX)
|
||||
if (++consecutiveLostPackets > 2 && enableDTX)
|
||||
{
|
||||
silentPacketCount += packetsPerFrame;
|
||||
size = packetsPerFrame * 960;
|
||||
|
@ -79,11 +79,16 @@ void JitterBuffer::HandleInput(unsigned char *data, size_t len, uint32_t timesta
|
||||
pkt.buffer = Buffer::Wrap(data, len, [](void *) {}, [](void *a, size_t) -> void * { return a; });
|
||||
pkt.timestamp = timestamp;
|
||||
pkt.isEC = isEC;
|
||||
if (!isEC)
|
||||
{
|
||||
LOGV("in, ts=%d, ec=%d", timestamp, isEC);
|
||||
}
|
||||
else
|
||||
LOGW("in, ts=%d, ec=%d", timestamp, isEC);
|
||||
PutInternal(pkt, !isEC);
|
||||
//LOGV("in, ts=%d, ec=%d", timestamp, isEC);
|
||||
}
|
||||
|
||||
void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
|
||||
void JitterBuffer::PutInternal(const jitter_packet_t &pkt, bool overwriteExisting)
|
||||
{
|
||||
if (pkt.size > JITTER_SLOT_SIZE)
|
||||
{
|
||||
@ -91,18 +96,20 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
|
||||
return;
|
||||
}
|
||||
|
||||
auto existing = std::find_if(slots.begin(), slots.end(), [timestamp = pkt.timestamp](const jitter_packet_t &slot) -> bool {
|
||||
return slot.timestamp == timestamp && !slot.buffer.IsEmpty();
|
||||
});
|
||||
if (existing != slots.end())
|
||||
for (auto &slot : slots)
|
||||
{
|
||||
if (overwriteExisting)
|
||||
if (slot.timestamp == pkt.timestamp && !slot.buffer.IsEmpty())
|
||||
{
|
||||
existing->buffer.CopyFromOtherBuffer(pkt.buffer, pkt.size);
|
||||
existing->size = pkt.size;
|
||||
existing->isEC = pkt.isEC;
|
||||
if (overwriteExisting)
|
||||
{
|
||||
LOGE("Overwriting");
|
||||
slot.buffer.CopyFromOtherBuffer(pkt.buffer, pkt.size);
|
||||
slot.size = pkt.size;
|
||||
slot.isEC = pkt.isEC;
|
||||
}
|
||||
LOGE("Not storing");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
gotSinceReset++;
|
||||
@ -149,13 +156,19 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
|
||||
if (pkt.timestamp < nextFetchTimestamp)
|
||||
{
|
||||
//LOGW("jitter: would drop packet with timestamp %d because it is late but not hopelessly", pkt.timestamp);
|
||||
latePacketCount++;
|
||||
lostPackets--;
|
||||
if (overwriteExisting) // If EC, do not count as late packet
|
||||
{
|
||||
latePacketCount++;
|
||||
lostPackets--;
|
||||
}
|
||||
}
|
||||
else if (pkt.timestamp < nextFetchTimestamp - 1)
|
||||
{
|
||||
//LOGW("jitter: dropping packet with timestamp %d because it is too late", pkt.timestamp);
|
||||
latePacketCount++;
|
||||
if (overwriteExisting) // If EC, do not count as late packet
|
||||
{
|
||||
latePacketCount++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@ -169,6 +182,7 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
|
||||
|
||||
if (slot == slots.end())
|
||||
{
|
||||
LOGW("No free slots!");
|
||||
slot = std::min_element(slots.begin(), slots.end(), [](const jitter_packet_t &a, const jitter_packet_t &b) -> bool {
|
||||
return !a.buffer.IsEmpty() && a.timestamp < b.timestamp;
|
||||
});
|
||||
@ -302,6 +316,7 @@ int JitterBuffer::GetInternal(jitter_packet_t &pkt, int offset, bool advance)
|
||||
pkt.isEC = slot->isEC;
|
||||
}
|
||||
slot->buffer = Buffer();
|
||||
LOGV("out ts=%d, ec=%d", pkt.timestamp, pkt.isEC);
|
||||
if (offset == 0)
|
||||
Advance();
|
||||
lostCount = 0;
|
||||
|
@ -65,7 +65,7 @@ private:
|
||||
bool isEC = 0;
|
||||
double recvTimeDiff = 0.0;
|
||||
};
|
||||
void PutInternal(jitter_packet_t &pkt, bool overwriteExisting);
|
||||
void PutInternal(const jitter_packet_t &pkt, bool overwriteExisting);
|
||||
int GetInternal(jitter_packet_t &pkt, int offset, bool advance);
|
||||
void Advance();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user