1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-27 12:44:37 +01:00
libtgvoip/JitterBuffer.h

96 lines
2.4 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"
#include "BufferPool.h"
#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
struct jitter_packet_t{
unsigned char* buffer;
size_t size;
uint32_t timestamp;
2017-03-30 16:06:59 +02:00
double recvTimeDiff;
2017-02-02 17:24:40 +01:00
};
typedef struct jitter_packet_t jitter_packet_t;
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();
int GetCurrentDelay();
double GetAverageDelay();
2017-02-02 17:24:40 +01:00
void Reset();
void HandleInput(unsigned char* data, size_t len, uint32_t timestamp);
size_t HandleOutput(unsigned char* buffer, size_t len, int offsetInSteps, int* playbackScaledDuration);
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:
static size_t CallbackIn(unsigned char* data, size_t len, void* param);
static size_t CallbackOut(unsigned char* data, size_t len, void* param);
void PutInternal(jitter_packet_t* pkt);
int GetInternal(jitter_packet_t* pkt, int offset);
void Advance();
BufferPool bufferPool;
2017-02-02 17:24:40 +01:00
tgvoip_mutex_t mutex;
jitter_packet_t slots[JITTER_SLOT_COUNT];
int64_t nextTimestamp;
uint32_t step;
uint32_t minDelay;
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;
2017-02-02 17:24:40 +01:00
int lostCount;
2017-03-30 16:06:59 +02:00
int lostSinceReset;
int gotSinceReset;
2017-02-02 17:24:40 +01:00
bool wasReset;
bool needBuffering;
int delayHistory[64];
int lateHistory[64];
bool adjustingDelay;
unsigned int tickCount;
unsigned int latePacketCount;
unsigned int dontIncMinDelay;
unsigned int dontDecMinDelay;
int lostPackets;
2017-03-30 16:06:59 +02:00
double prevRecvTime;
double expectNextAtTime;
double deviationHistory[64];
int deviationPtr;
double lastMeasuredJitter;
double lastMeasuredDelay;
int outstandingDelayChange;
unsigned int dontChangeDelay;
double avgDelay;
//FILE* dump;
2017-02-02 17:24:40 +01:00
};
}
2017-02-02 17:24:40 +01:00
#endif //LIBTGVOIP_JITTERBUFFER_H