Reduce NAT keepalive packet rate after connection is established

This commit is contained in:
Cameron Gutman
2019-03-24 12:54:41 -07:00
parent e9bc1070b7
commit 40fa4ef3c8
2 changed files with 29 additions and 3 deletions
+15 -2
View File
@@ -15,6 +15,8 @@ static PLT_THREAD decoderThread;
static unsigned short lastSeq; static unsigned short lastSeq;
static int receivedDataFromPeer;
#define RTP_PORT 48000 #define RTP_PORT 48000
#define MAX_PACKET_SIZE 1400 #define MAX_PACKET_SIZE 1400
@@ -68,6 +70,7 @@ void initializeAudioStream(void) {
} }
RtpqInitializeQueue(&rtpReorderQueue, RTPQ_DEFAULT_MAX_SIZE, RTPQ_DEFAULT_QUEUE_TIME); RtpqInitializeQueue(&rtpReorderQueue, RTPQ_DEFAULT_MAX_SIZE, RTPQ_DEFAULT_QUEUE_TIME);
lastSeq = 0; lastSeq = 0;
receivedDataFromPeer = 0;
} }
static void freePacketList(PLINKED_BLOCKING_QUEUE_ENTRY entry) { static void freePacketList(PLINKED_BLOCKING_QUEUE_ENTRY entry) {
@@ -100,7 +103,7 @@ static void UdpPingThreadProc(void* context) {
memcpy(&saddr, &RemoteAddr, sizeof(saddr)); memcpy(&saddr, &RemoteAddr, sizeof(saddr));
saddr.sin6_port = htons(RTP_PORT); saddr.sin6_port = htons(RTP_PORT);
// Send PING every 500 milliseconds // Send PING every second until we get data back then every 5 seconds after that.
while (!PltIsThreadInterrupted(&udpPingThread)) { while (!PltIsThreadInterrupted(&udpPingThread)) {
err = sendto(rtpSocket, pingData, sizeof(pingData), 0, (struct sockaddr*)&saddr, RemoteAddrLen); err = sendto(rtpSocket, pingData, sizeof(pingData), 0, (struct sockaddr*)&saddr, RemoteAddrLen);
if (err != sizeof(pingData)) { if (err != sizeof(pingData)) {
@@ -109,7 +112,13 @@ static void UdpPingThreadProc(void* context) {
return; return;
} }
PltSleepMs(500); // Send less frequently if we've received data from our peer
if (receivedDataFromPeer) {
PltSleepMs(5000);
}
else {
PltSleepMs(1000);
}
} }
} }
@@ -201,6 +210,10 @@ static void ReceiveThreadProc(void* context) {
continue; continue;
} }
// We've received data, so we can stop sending our ping packets
// as quickly, since we're now just keeping the NAT session open.
receivedDataFromPeer = 1;
// GFE accumulates audio samples before we are ready to receive them, // GFE accumulates audio samples before we are ready to receive them,
// so we will drop the first 100 packets to avoid accumulating latency // so we will drop the first 100 packets to avoid accumulating latency
// by sending audio frames to the player faster than they can be played. // by sending audio frames to the player faster than they can be played.
+14 -1
View File
@@ -20,6 +20,8 @@ static PLT_THREAD udpPingThread;
static PLT_THREAD receiveThread; static PLT_THREAD receiveThread;
static PLT_THREAD decoderThread; static PLT_THREAD decoderThread;
static int receivedDataFromPeer;
// We can't request an IDR frame until the depacketizer knows // We can't request an IDR frame until the depacketizer knows
// that a packet was lost. This timeout bounds the time that // that a packet was lost. This timeout bounds the time that
// the RTP queue will wait for missing/reordered packets. // the RTP queue will wait for missing/reordered packets.
@@ -30,6 +32,7 @@ static PLT_THREAD decoderThread;
void initializeVideoStream(void) { void initializeVideoStream(void) {
initializeVideoDepacketizer(StreamConfig.packetSize); initializeVideoDepacketizer(StreamConfig.packetSize);
RtpfInitializeQueue(&rtpQueue); //TODO RTP_QUEUE_DELAY RtpfInitializeQueue(&rtpQueue); //TODO RTP_QUEUE_DELAY
receivedDataFromPeer = 0;
} }
// Clean up the video stream // Clean up the video stream
@@ -55,7 +58,13 @@ static void UdpPingThreadProc(void* context) {
return; return;
} }
PltSleepMs(500); // Send less frequently if we've received data from our peer
if (receivedDataFromPeer) {
PltSleepMs(5000);
}
else {
PltSleepMs(500);
}
} }
} }
@@ -104,6 +113,10 @@ static void ReceiveThreadProc(void* context) {
continue; continue;
} }
// We've received data, so we can stop sending our ping packets
// as quickly, since we're now just keeping the NAT session open.
receivedDataFromPeer = 1;
// RTP sequence number must be in host order for the RTP queue // RTP sequence number must be in host order for the RTP queue
packet = (PRTP_PACKET)&buffer[0]; packet = (PRTP_PACKET)&buffer[0];
packet->sequenceNumber = htons(packet->sequenceNumber); packet->sequenceNumber = htons(packet->sequenceNumber);