From c1ec091f80c1b3ac176e3a862b3d653fdbc8ca7f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 26 Jan 2020 18:51:45 +0100 Subject: [PATCH] Minor refactorings --- controller/PrivateDefines.h | 8 ++++- controller/net/JitterBuffer.cpp | 57 +++++++++++++++----------------- controller/net/JitterBuffer.h | 3 +- controller/protocol/Protocol.cpp | 36 ++++++-------------- 4 files changed, 46 insertions(+), 58 deletions(-) diff --git a/controller/PrivateDefines.h b/controller/PrivateDefines.h index e5b2ac2..0b89fae 100644 --- a/controller/PrivateDefines.h +++ b/controller/PrivateDefines.h @@ -26,8 +26,14 @@ #define CHECK_ENDPOINT_PROTOCOL(endpointType, packetProtocol) ((endpointType != Endpoint::Type::TCP_RELAY && packetProtocol == NetworkProtocol::UDP) || (endpointType == Endpoint::Type::TCP_RELAY && packetProtocol == NetworkProtocol::TCP)) +enum ProtocolVersions +{ + PROTOCOL_OLD = 9, + PROTOCOL_DANOG = 10 +}; + #define PROTOCOL_NAME 0x50567247 // "GrVP" in little endian (reversed here) -#define PROTOCOL_VERSION 9 +#define PROTOCOL_VERSION 10 #define MIN_PROTOCOL_VERSION 3 #define STREAM_DATA_FLAG_LEN16 0x40 diff --git a/controller/net/JitterBuffer.cpp b/controller/net/JitterBuffer.cpp index 5dd1ad6..afd2e03 100755 --- a/controller/net/JitterBuffer.cpp +++ b/controller/net/JitterBuffer.cpp @@ -108,16 +108,16 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) { wasReset = false; outstandingDelayChange = 0; - nextTimestamp = static_cast(static_cast(pkt->timestamp) - step * minDelay); + nextFetchTimestamp = static_cast(static_cast(pkt->timestamp) - step * minDelay); first = true; - LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextTimestamp, step, minDelay); + LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextFetchTimestamp, step, minDelay); } for (i = 0; i < JITTER_SLOT_COUNT; i++) { if (!slots[i].buffer.IsEmpty()) { - if (slots[i].timestamp < nextTimestamp - 1) + if (slots[i].timestamp < nextFetchTimestamp - 1) { slots[i].buffer = Buffer(); } @@ -132,6 +132,8 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) prevTime=slots[i].recvTime; } }*/ + + // Time deviation check double time = VoIPController::GetCurrentTime(); if (expectNextAtTime != 0) { @@ -145,13 +147,14 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) expectNextAtTime = time + step / 1000.0; } - if (pkt->timestamp < nextTimestamp) + // Late packet check + if (pkt->timestamp < nextFetchTimestamp) { //LOGW("jitter: would drop packet with timestamp %d because it is late but not hopelessly", pkt->timestamp); latePacketCount++; lostPackets--; } - else if (pkt->timestamp < nextTimestamp - 1) + else if (pkt->timestamp < nextFetchTimestamp - 1) { //LOGW("jitter: dropping packet with timestamp %d because it is too late", pkt->timestamp); latePacketCount++; @@ -169,13 +172,13 @@ void JitterBuffer::PutInternal(jitter_packet_t *pkt, bool overwriteExisting) if (i == JITTER_SLOT_COUNT || GetCurrentDelay() >= maxUsedSlots) { int toRemove = JITTER_SLOT_COUNT; - uint32_t bestTimestamp = 0xFFFFFFFF; + uint32_t oldestTimestamp = 0xFFFFFFFF; for (i = 0; i < JITTER_SLOT_COUNT; i++) { - if (!slots[i].buffer.IsEmpty() && slots[i].timestamp < bestTimestamp) + if (!slots[i].buffer.IsEmpty() && slots[i].timestamp < oldestTimestamp) { toRemove = i; - bestTimestamp = slots[i].timestamp; + oldestTimestamp = slots[i].timestamp; } } Advance(); @@ -230,11 +233,11 @@ size_t JitterBuffer::HandleOutput(unsigned char *buffer, size_t len, int offsetI if (GetCurrentDelay() > 5) { LOGW("jitter: delay too big upon start (%u), dropping packets", delay); - while (delay > GetMinPacketCount()) + for (;delay > GetMinPacketCount(); --delay) { for (int i = 0; i < JITTER_SLOT_COUNT; i++) { - if (slots[i].timestamp == nextTimestamp) + if (slots[i].timestamp == nextFetchTimestamp) { if (!slots[i].buffer.IsEmpty()) { @@ -244,7 +247,6 @@ size_t JitterBuffer::HandleOutput(unsigned char *buffer, size_t len, int offsetI } } Advance(); - delay--; } } } @@ -285,15 +287,15 @@ size_t JitterBuffer::HandleOutput(unsigned char *buffer, size_t len, int offsetI int JitterBuffer::GetInternal(jitter_packet_t *pkt, int offset, bool advance) { - /*if(needBuffering && lastPutTimestamp maxMinDelay) - stddevDelay = maxMinDelay; + uint32_t stddevDelay = std::clamp((uint32_t)ceil(stddev * 2 * 1000 / step), minMinDelay, maxMinDelay); + if (stddevDelay != minDelay) { - int32_t diff = (int32_t)(stddevDelay - minDelay); + int32_t diff = std::clamp((int32_t)(stddevDelay - minDelay), -1, 1); if (diff > 0) { dontDecMinDelay = 100; } - if (diff < -1) - diff = -1; - if (diff > 1) - diff = 1; + if ((diff > 0 && dontIncMinDelay == 0) || (diff < 0 && dontDecMinDelay == 0)) { - //nextTimestamp+=diff*(int32_t)step; + //nextFetchTimestamp+=diff*(int32_t)step; minDelay += diff; outstandingDelayChange += diff * 60; dontChangeDelay += 32; @@ -487,10 +483,9 @@ void JitterBuffer::Tick() void JitterBuffer::GetAverageLateCount(double *out) { - double avgLate64 = lateHistory.Average(), avgLate32 = lateHistory.Average(32), avgLate16 = lateHistory.Average(16); - out[0] = avgLate16; - out[1] = avgLate32; - out[2] = avgLate64; + out[0] = lateHistory.Average(16); + out[1] = lateHistory.Average(32); + out[2] = lateHistory.Average(); } int JitterBuffer::GetAndResetLostPacketCount() diff --git a/controller/net/JitterBuffer.h b/controller/net/JitterBuffer.h index 6b06255..3b4800e 100644 --- a/controller/net/JitterBuffer.h +++ b/controller/net/JitterBuffer.h @@ -9,6 +9,7 @@ #include #include +#include #include #include "controller/media/MediaStreamItf.h" #include "tools/BlockingQueue.h" @@ -58,7 +59,7 @@ private: Mutex mutex; uint32_t step; std::array slots; - int64_t nextTimestamp = 0; + int64_t nextFetchTimestamp = 0; // What frame to read next double minDelay = 6; uint32_t minMinDelay; uint32_t maxMinDelay; diff --git a/controller/protocol/Protocol.cpp b/controller/protocol/Protocol.cpp index b229bab..abb9edc 100644 --- a/controller/protocol/Protocol.cpp +++ b/controller/protocol/Protocol.cpp @@ -381,7 +381,7 @@ void VoIPController::ProcessIncomingPacket(NetworkPacket &packet, Endpoint &srcE } unsigned int i; - unsigned int numSupportedAudioCodecs = in.ReadByte(); + unsigned char numSupportedAudioCodecs = in.ReadByte(); for (i = 0; i < numSupportedAudioCodecs; i++) { if (peerVersion < 5) @@ -574,7 +574,12 @@ void VoIPController::ProcessIncomingPacket(NetworkPacket &packet, Endpoint &srcE LOGW("First audio packet - setting state to ESTABLISHED"); } } - int count; + if (srcEndpoint.type == Endpoint::Type::UDP_RELAY && srcEndpoint.id != peerPreferredRelay) + { + peerPreferredRelay = srcEndpoint.id; + } + + uint8_t count = 1; switch (type) { case PKT_STREAM_DATA_X2: @@ -583,16 +588,8 @@ void VoIPController::ProcessIncomingPacket(NetworkPacket &packet, Endpoint &srcE case PKT_STREAM_DATA_X3: count = 3; break; - case PKT_STREAM_DATA: - default: - count = 1; - break; - } - int i; - if (srcEndpoint.type == Endpoint::Type::UDP_RELAY && srcEndpoint.id != peerPreferredRelay) - { - peerPreferredRelay = srcEndpoint.id; } + uint8_t i; for (i = 0; i < count; i++) { unsigned char streamID = in.ReadByte(); @@ -889,17 +886,6 @@ void VoIPController::ProcessExtraData(Buffer &data) else if (type == EXTRA_TYPE_STREAM_CSD) { LOGI("Received codec specific data"); - /* - os.WriteByte(stream.id); - os.WriteByte(static_cast(stream.codecSpecificData.size())); - for(Buffer& b:stream.codecSpecificData){ - assert(b.Length()<255); - os.WriteByte(static_cast(b.Length())); - os.WriteBytes(b); - } - Buffer buf(move(os)); - SendExtra(buf, EXTRA_TYPE_STREAM_CSD); - */ unsigned char streamID = in.ReadByte(); for (shared_ptr &stm : incomingStreams) { @@ -909,8 +895,8 @@ void VoIPController::ProcessExtraData(Buffer &data) stm->csdIsValid = false; stm->width = static_cast(in.ReadInt16()); stm->height = static_cast(in.ReadInt16()); - size_t count = (size_t)in.ReadByte(); - for (size_t i = 0; i < count; i++) + uint8_t count = in.ReadByte(); + for (uint8_t i = 0; i < count; i++) { size_t len = (size_t)in.ReadByte(); Buffer csd(len); @@ -927,7 +913,7 @@ void VoIPController::ProcessExtraData(Buffer &data) return; LOGV("received lan endpoint (extra)"); uint32_t peerAddr = in.ReadUInt32(); - uint16_t peerPort = (uint16_t)in.ReadInt32(); + uint16_t peerPort = static_cast(in.ReadInt32()); constexpr int64_t lanID = static_cast(FOURCC('L', 'A', 'N', '4')) << 32; if (currentEndpoint == lanID) currentEndpoint = preferredRelay;