| 6 |
| 7 | #ifndef LIBTGVOIP_JITTERBUFFER_H |
| 8 | #define LIBTGVOIP_JITTERBUFFER_H |
| 9 |
| 10 | #include "threading.h" |
| 11 | #include "BlockingQueue.h" |
| 12 | #include "Buffers.h" |
| 13 | #include "MediaStreamItf.h" |
| 14 |
| 15 | #include <cstdint> |
| 16 | #include <map> |
| 17 |
| 18 | #define JITTER_SLOT_COUNT 64 |
| 19 | #define JITTER_SLOT_SIZE 1024 |
| 20 | #define HISTORY_SIZE 64 |
| 21 |
| 22 | namespace tgvoip |
| 23 | { |
| 24 |
| 25 | class JitterBuffer |
| 26 | { |
| 27 | public: |
| 28 | JitterBuffer(MediaStreamItf* out, std::uint32_t m_step); |
| 29 | ~JitterBuffer(); |
| 30 | void SetMinPacketCount(std::uint32_t count); |
| 31 | std::uint32_t GetMinPacketCount() const; |
| 32 | std::uint32_t GetCurrentDelay() const; |
| 33 | double GetAverageDelay() const; |
| 34 | void Reset(); |
| 35 | void HandleInput(const std::uint8_t* data, std::size_t len, std::uint32_t timestamp, bool isEC); |
| 36 | std::size_t HandleOutput(std::uint8_t* data, std::size_t len, std::uint32_t offsetInSteps, |
| 37 | bool advance, int& playbackScaledDuration, bool& isEC); |
| 38 | void Tick(); |
| 39 | void GetAverageLateCount(double* out) const; |
| 40 | int GetAndResetLostPacketCount(); |
| 41 | double GetLastMeasuredJitter() const; |
| 42 | double GetLastMeasuredDelay() const; |
| 43 |
| 44 | private: |
| 45 | struct jitter_packet_t |
| 46 | { |
| 47 | Buffer buffer = Buffer(); |
| 48 | double recvTimeDiff; |
| 49 | std::size_t size; |
| 50 | std::uint32_t timestamp; |
| 51 | bool isEC; |
| 52 | }; |
| 53 |
| 54 | enum class Status |
| 55 | { |
| 56 | OK = 1, |
| 57 | MISSING, |
| 58 | REPLACED, |
| 59 | }; |
| 60 |
| 61 | static std::size_t CallbackIn(std::uint8_t* data, std::size_t len, void* param); |
| 62 | static std::size_t CallbackOut(std::uint8_t* data, std::size_t len, void* param); |
| 63 | void PutInternal(const jitter_packet_t& pkt, const std::uint8_t* data, bool overwriteExisting); |
| 64 | Status GetInternal(jitter_packet_t* pkt, std::uint32_t offset, bool advance); |
| 65 | void Advance(); |
| 66 | std::uint32_t GetAdditionForTimestamp() const; |
| 67 | std::uint32_t GetMinPacketCountNonBlocking() const; |
| 68 | std::uint32_t GetCurrentDelayNonBlocking() const; |
| 69 | void ResetNonBlocking(); |
| 70 |
| 71 | #ifdef TGVOIP_DUMP_JITTER_STATS |
| 72 | FILE* dump; |
| 73 | #endif |
| 74 |
| 75 | BufferPool<JITTER_SLOT_SIZE, JITTER_SLOT_COUNT> m_bufferPool; |
| 76 | mutable Mutex m_mutex; |
| 77 |
| 78 | double m_resyncThreshold; |
| 79 | double m_prevRecvTime = 0; |
| 80 | double m_expectNextAtTime = 0; |
| 81 | double m_lastMeasuredJitter = 0; |
| 82 | double m_lastMeasuredDelay = 0; |
| 83 | double m_avgDelay = 0; |
| 84 |
| 85 | std::map<std::uint32_t, jitter_packet_t> m_slots; |
| 86 |
| 87 | // if there is no slot with requested timestamp in m_slots, |
| 88 | // attempt to find previous a slot with close previous timestamp |
| 89 | std::map<std::uint32_t, jitter_packet_t> m_slotsHistory; |
| 90 |
| 91 | HistoricBuffer<unsigned int, HISTORY_SIZE, double> m_delayHistory; |
| 92 | HistoricBuffer<unsigned int, HISTORY_SIZE, double> m_lateHistory; |
| 93 | HistoricBuffer<double, HISTORY_SIZE> m_deviationHistory; |
| 94 |
| 95 | std::uint32_t m_nextTimestamp = 0; |
| 96 | // if m_nextTimestamp is too little, we need to use this because of |
| 97 | // restrictions of unsigned arithmetics |
| 98 | std::uint32_t m_addToTimestamp = 0; |
| 99 | std::uint32_t m_step; |
| 100 | std::uint32_t m_delay = 6; |
| 101 | std::uint32_t m_minDelay; |
| 102 | std::uint32_t m_maxDelay; |
| 103 | std::uint32_t m_maxAllowedSlots; |
| 104 | std::uint32_t m_lastPutTimestamp; |
| 105 | std::uint32_t m_lossesToReset; |
| 106 |
| 107 | std::uint32_t m_replaceRadius = 1; |
| 108 |
| 109 | unsigned int m_lostCount = 0; |
| 110 | unsigned int m_lostSinceReset = 0; |
| 111 | unsigned int m_gotSinceReset = 0; |
| 112 | unsigned int m_latePacketCount = 0; |
| 113 | unsigned int m_dontIncDelay = 0; |
| 114 | unsigned int m_dontDecDelay = 0; |
| 115 | unsigned int m_dontChangeOutstandingDelay = 0; |
| 116 | int m_lostPackets = 0; |
| 117 | int m_outstandingDelayChange = 0; |
| 118 |
| 119 | bool m_wasReset = true; |
| 120 | }; |
| 121 |
| 122 | } // namespace tgvoip |
| 123 |
| 124 | #endif // LIBTGVOIP_JITTERBUFFER_H |
| 125 |