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>
|
2017-05-17 13:30:14 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
namespace tgvoip{
|
|
|
|
class JitterBuffer{
|
2017-02-02 17:24:40 +01:00
|
|
|
public:
|
2017-04-28 13:17:56 +02:00
|
|
|
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();
|
2017-05-21 16:50:23 +02:00
|
|
|
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();
|
2017-04-28 13:17:56 +02:00
|
|
|
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{
|
2018-11-09 16:44:01 +01:00
|
|
|
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();
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
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];
|
2018-07-17 18:48:21 +02:00
|
|
|
int64_t nextTimestamp=0;
|
2017-02-02 17:24:40 +01:00
|
|
|
uint32_t step;
|
2018-07-17 18:48:21 +02:00
|
|
|
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;
|
2018-07-17 18:48:21 +02:00
|
|
|
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;
|
2018-11-09 16:44:01 +01:00
|
|
|
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-04-28 13:17:56 +02:00
|
|
|
}
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
#endif //LIBTGVOIP_JITTERBUFFER_H
|