1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-26 20:24:38 +01:00

Update jitter buffer

This commit is contained in:
Daniil Gentili 2020-03-26 21:22:36 +01:00
parent 6e715d8b39
commit ded4338153
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 30 additions and 43 deletions

View File

@ -74,26 +74,9 @@ double JitterBuffer::GetTimeoutWindow()
void JitterBuffer::HandleInput(std::unique_ptr<Buffer> &&buf, uint32_t timestamp, bool isEC) void JitterBuffer::HandleInput(std::unique_ptr<Buffer> &&buf, uint32_t timestamp, bool isEC)
{ {
MutexGuard m(mutex); MutexGuard m(mutex);
jitter_packet_t pkt; size_t size = buf->Length();
pkt.size = buf->Length();
pkt.buffer = std::move(buf);
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);
}
void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting) if (size > JITTER_SLOT_SIZE)
{
if (pkt.size > JITTER_SLOT_SIZE)
{ {
LOGE("The packet is too big to fit into the jitter buffer"); LOGE("The packet is too big to fit into the jitter buffer");
return; return;
@ -101,13 +84,13 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
for (auto &slot : slots) for (auto &slot : slots)
{ {
if (slot.timestamp == pkt.timestamp && slot.buffer) if (slot.buffer && slot.timestamp == timestamp)
{ {
if (overwriteExisting) if (!isEC)
{ {
slot.buffer = std::move(pkt.buffer); slot.buffer = std::move(buf);
slot.size = pkt.size; slot.size = size;
slot.isEC = pkt.isEC; slot.isEC = isEC;
} }
return; return;
} }
@ -118,7 +101,7 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
{ {
wasReset = false; wasReset = false;
outstandingDelayChange = 0; outstandingDelayChange = 0;
nextFetchTimestamp = static_cast<int64_t>(static_cast<int64_t>(pkt.timestamp) - step * minDelay); nextFetchTimestamp = static_cast<int64_t>(static_cast<int64_t>(timestamp) - step * minDelay);
first = true; first = true;
LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextFetchTimestamp, step, (double)minDelay); LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextFetchTimestamp, step, (double)minDelay);
} }
@ -126,7 +109,7 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
for (auto &slot : slots) for (auto &slot : slots)
{ {
// Clear packets older than the last packet pulled from jitter buffer // Clear packets older than the last packet pulled from jitter buffer
if (slot.timestamp < nextFetchTimestamp - 1 && slot.buffer) if (slot.buffer && slot.timestamp < nextFetchTimestamp - 1)
{ {
slot.buffer = nullptr; slot.buffer = nullptr;
} }
@ -135,7 +118,7 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
/*double prevTime=0; /*double prevTime=0;
uint32_t closestTime=0; uint32_t closestTime=0;
for(i=0;i<JITTER_SLOT_COUNT;i++){ for(i=0;i<JITTER_SLOT_COUNT;i++){
if(slots[i].buffer!=NULL && pkt.timestamp-slots[i].timestamp<pkt.timestamp-closestTime){ if(slots[i].buffer!=NULL && timestamp-slots[i].timestamp<timestamp-closestTime){
closestTime=slots[i].timestamp; closestTime=slots[i].timestamp;
prevTime=slots[i].recvTime; prevTime=slots[i].recvTime;
} }
@ -154,27 +137,27 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
} }
// Late packet check // Late packet check
if (pkt.timestamp < nextFetchTimestamp) if (timestamp < nextFetchTimestamp)
{ {
if (overwriteExisting) // If EC, do not count as late packet if (!isEC) // If EC, do not count as late packet
{ {
LOGW("jitter: would drop packet with timestamp %d because it is late but not hopelessly", pkt.timestamp); LOGW("jitter: would drop packet with timestamp %d because it is late but not hopelessly", timestamp);
latePacketCount++; latePacketCount++;
lostPackets--; lostPackets--;
} }
} }
else if (pkt.timestamp < nextFetchTimestamp - 1) else if (timestamp < nextFetchTimestamp - 1)
{ {
if (overwriteExisting) // If EC, do not count as late packet if (!isEC) // If EC, do not count as late packet
{ {
LOGW("jitter: dropping packet with timestamp %d because it is too late", pkt.timestamp); LOGW("jitter: dropping packet with timestamp %d because it is too late", timestamp);
latePacketCount++; latePacketCount++;
} }
return; return;
} }
if (pkt.timestamp > lastPutTimestamp) if (timestamp > lastPutTimestamp)
lastPutTimestamp = pkt.timestamp; lastPutTimestamp = timestamp;
// If no free slots or too many used up slots to be useful // If no free slots or too many used up slots to be useful
auto slot = GetCurrentDelay() >= maxUsedSlots ? slots.end() : std::find_if(slots.begin(), slots.end(), [](const jitter_packet_t &a) -> bool { auto slot = GetCurrentDelay() >= maxUsedSlots ? slots.end() : std::find_if(slots.begin(), slots.end(), [](const jitter_packet_t &a) -> bool {
@ -191,13 +174,17 @@ void JitterBuffer::PutInternal(jitter_packet_t &pkt, bool overwriteExisting)
Advance(); Advance();
} }
slot->timestamp = pkt.timestamp; LOGW("Putting into jitter buffer");
slot->size = pkt.size; std::string pad(slot - slots.begin(), ' ');
slot->buffer = std::move(pkt.buffer); pad += '^';
slot->timestamp = timestamp;
slot->size = size;
slot->buffer = std::move(buf);
slot->recvTimeDiff = time - prevRecvTime; slot->recvTimeDiff = time - prevRecvTime;
slot->isEC = pkt.isEC; slot->isEC = isEC;
#ifdef TGVOIP_DUMP_JITTER_STATS #ifdef TGVOIP_DUMP_JITTER_STATS
fprintf(dump, "%u\t%.03f\t%d\t%.03f\t%.03f\t%.03f\n", pkt.timestamp, time, GetCurrentDelay(), lastMeasuredJitter, lastMeasuredDelay, minDelay); fprintf(dump, "%u\t%.03f\t%d\t%.03f\t%.03f\t%.03f\n", timestamp, time, GetCurrentDelay(), lastMeasuredJitter, lastMeasuredDelay, minDelay);
#endif #endif
prevRecvTime = time; prevRecvTime = time;
} }
@ -328,8 +315,9 @@ int JitterBuffer::GetInternal(jitter_packet_t &pkt, bool advance)
//minDelay++; //minDelay++;
dontIncMinDelay = 16; dontIncMinDelay = 16;
dontDecMinDelay += 128; dontDecMinDelay += 128;
if (GetCurrentDelay() < minDelay) auto currentDelay = GetCurrentDelay();
nextFetchTimestamp -= (int64_t)(minDelay - GetCurrentDelay()); if (currentDelay < minDelay)
nextFetchTimestamp -= static_cast<int64_t>(minDelay - currentDelay);
lostCount = 0; lostCount = 0;
Reset(); Reset();
} }

View File

@ -65,7 +65,6 @@ private:
bool isEC = 0; bool isEC = 0;
double recvTimeDiff = 0.0; double recvTimeDiff = 0.0;
}; };
void PutInternal(jitter_packet_t &pkt, bool overwriteExisting);
int GetInternal(jitter_packet_t &pkt, bool advance); int GetInternal(jitter_packet_t &pkt, bool advance);
void Advance(); void Advance();