1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-30 04:39:03 +01:00
libtgvoip/JitterBuffer.h

98 lines
2.6 KiB
C
Raw Normal View History

2017-02-02 17:24:40 +01:00
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#ifndef LIBTGVOIP_JITTERBUFFER_H
#define LIBTGVOIP_JITTERBUFFER_H
#include <stdlib.h>
#include <vector>
#include <stdio.h>
2017-02-02 17:24:40 +01:00
#include "MediaStreamItf.h"
#include "BlockingQueue.h"
2018-06-04 21:37:43 +02:00
#include "Buffers.h"
2017-02-02 17:24:40 +01:00
#include "threading.h"
#define JITTER_SLOT_COUNT 64
#define JITTER_SLOT_SIZE 1024
#define JR_OK 1
#define JR_MISSING 2
#define JR_BUFFERING 3
namespace tgvoip{
class JitterBuffer{
2017-02-02 17:24:40 +01:00
public:
JitterBuffer(MediaStreamItf* out, uint32_t step);
~JitterBuffer();
2017-02-02 17:24:40 +01:00
void SetMinPacketCount(uint32_t count);
int GetMinPacketCount();
2018-05-15 20:23:46 +02:00
unsigned int GetCurrentDelay();
double GetAverageDelay();
2017-02-02 17:24:40 +01:00
void Reset();
2018-06-04 21:37:43 +02:00
void HandleInput(unsigned char* data, size_t len, uint32_t timestamp, bool isEC);
size_t HandleOutput(unsigned char* buffer, size_t len, int offsetInSteps, bool advance, int& playbackScaledDuration, bool& isEC);
2017-02-02 17:24:40 +01:00
void Tick();
void GetAverageLateCount(double* out);
int GetAndResetLostPacketCount();
double GetLastMeasuredJitter();
double GetLastMeasuredDelay();
2017-02-02 17:24:40 +01:00
private:
2018-06-04 21:37:43 +02:00
struct jitter_packet_t{
unsigned char* buffer=NULL;
2018-06-04 21:37:43 +02:00
size_t size;
uint32_t timestamp;
bool isEC;
double recvTimeDiff;
};
2017-02-02 17:24:40 +01:00
static size_t CallbackIn(unsigned char* data, size_t len, void* param);
static size_t CallbackOut(unsigned char* data, size_t len, void* param);
2018-06-04 21:37:43 +02:00
void PutInternal(jitter_packet_t* pkt, bool overwriteExisting);
2018-05-15 20:23:46 +02:00
int GetInternal(jitter_packet_t* pkt, int offset, bool advance);
2017-02-02 17:24:40 +01:00
void Advance();
BufferPool bufferPool;
2018-05-15 20:23:46 +02:00
Mutex mutex;
2017-02-02 17:24:40 +01:00
jitter_packet_t slots[JITTER_SLOT_COUNT];
int64_t nextTimestamp=0;
2017-02-02 17:24:40 +01:00
uint32_t step;
double minDelay=6;
2017-02-02 17:24:40 +01:00
uint32_t minMinDelay;
2017-03-30 16:06:59 +02:00
uint32_t maxMinDelay;
uint32_t maxUsedSlots;
2017-02-02 17:24:40 +01:00
uint32_t lastPutTimestamp;
2017-03-30 16:06:59 +02:00
uint32_t lossesToReset;
double resyncThreshold;
unsigned int lostCount=0;
unsigned int lostSinceReset=0;
unsigned int gotSinceReset=0;
bool wasReset=true;
bool needBuffering=true;
HistoricBuffer<int, 64, double> delayHistory;
HistoricBuffer<int, 64, double> lateHistory;
bool adjustingDelay=false;
unsigned int tickCount=0;
unsigned int latePacketCount=0;
unsigned int dontIncMinDelay=0;
unsigned int dontDecMinDelay=0;
int lostPackets=0;
double prevRecvTime=0;
double expectNextAtTime=0;
HistoricBuffer<double, 64> deviationHistory;
double lastMeasuredJitter=0;
double lastMeasuredDelay=0;
int outstandingDelayChange=0;
unsigned int dontChangeDelay=0;
double avgDelay=0;
bool first=true;
2018-05-15 20:23:46 +02:00
#ifdef TGVOIP_DUMP_JITTER_STATS
FILE* dump;
#endif
2017-02-02 17:24:40 +01:00
};
}
2017-02-02 17:24:40 +01:00
#endif //LIBTGVOIP_JITTERBUFFER_H