Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ice turn sleepless state machine #1825

Merged
merged 29 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f984747
Starting point for turn state machine
jdelapla Sep 12, 2023
7d466b0
Add timerqueue kick and checks for ice agent state machine
jdelapla Sep 17, 2023
3fd3049
Merge branch 'develop' into ice-turn-sleepless-state-machine
jdelapla Sep 27, 2023
eced12f
Completing turn connection state machine
jdelapla Oct 9, 2023
c2d8982
Merge branch 'develop' into ice-turn-sleepless-state-machine
jdelapla Oct 11, 2023
1f255fb
Fixing tests
jdelapla Oct 12, 2023
4c88b14
clang format
jdelapla Oct 12, 2023
58c6d18
correct header function name
jdelapla Oct 12, 2023
1d0b9f0
improved error logging
jdelapla Oct 12, 2023
db7b8ad
fix mac compile error
jdelapla Oct 12, 2023
8bcf5fb
remove unused variables
jdelapla Oct 12, 2023
383f5e1
Another mac compile error from unused variables
jdelapla Oct 12, 2023
e35b562
Merge branch 'develop' into ice-turn-sleepless-state-machine
jdelapla Oct 12, 2023
6fbef43
More mac compile erorrs
jdelapla Oct 12, 2023
1ff8d2a
Better error logging around ICE_STATE_FAILED
jdelapla Oct 12, 2023
96d055e
clang format
jdelapla Oct 12, 2023
2fb6f38
Adding lock around step state machine to prevent race conditions with
jdelapla Oct 12, 2023
b55cbbe
Only lock the iterator during stepstate
jdelapla Oct 12, 2023
531a853
Fix error response check for dtlsv1_get_timeout
jdelapla Oct 13, 2023
23c192c
Current DTLS thread cannot handle dtlsv1_get_timeout() returning < 0
jdelapla Oct 13, 2023
c54cd2f
Add turn state transition after handling stun packets for turn
jdelapla Oct 13, 2023
9235e45
add null check to iceagentrestart
jdelapla Oct 13, 2023
e03e995
socketconnection needs to be checked in check_socket state
jdelapla Oct 13, 2023
7570e23
Fix comments
jdelapla Oct 13, 2023
4d2ac7b
Use producer-c develop
jdelapla Oct 13, 2023
04fabab
Merge branch 'develop' into ice-turn-sleepless-state-machine
jdelapla Oct 13, 2023
ef3dc13
Fix data race
jdelapla Oct 16, 2023
e36fb47
Cleanup commented out code
jdelapla Oct 16, 2023
f1e6a49
remove excessive buffer check in dtls_openssl
jdelapla Oct 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMake/Dependencies/libkvsCommonLws-CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include(ExternalProject)

ExternalProject_Add(libkvsCommonLws-download
GIT_REPOSITORY https://github.com/awslabs/amazon-kinesis-video-streams-producer-c.git
GIT_TAG fd562a16e41b1972df14c230b6be7a7d3f75bc6b
GIT_TAG f934b6c5a36f0daa7a002fd843542902e3ed7471
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/build
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${OPEN_SRC_INSTALL_PREFIX}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ extern "C" {
#define STATUS_ICE_SERVER_INDEX_INVALID STATUS_ICE_BASE + 0x00000028
#define STATUS_ICE_CANDIDATE_STRING_MISSING_TYPE STATUS_ICE_BASE + 0x00000029
#define STATUS_TURN_CONNECTION_ALLOCAITON_FAILED STATUS_ICE_BASE + 0x0000002a
#define STATUS_TURN_INVALID_STATE STATUS_ICE_BASE + 0x0000002b
/*!@} */

/////////////////////////////////////////////////////
Expand Down
38 changes: 17 additions & 21 deletions src/source/Crypto/Dtls_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
struct timeval timeout;
LONG dtlsTimeoutRet = 0;
BOOL firstMsg = TRUE;
UINT64 waitTime = INFINITE_TIME_VALUE;
UINT64 waitTime = HUNDREDS_OF_NANOS_IN_A_SECOND;
BOOL dtlsHandshakeErrored = FALSE;
BOOL timedOut = FALSE;
MEMSET(&timeout, 0x00, SIZEOF(struct timeval));
Expand All @@ -472,43 +472,39 @@ STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
switch (pDtlsSession->handshakeState) {
case DTLS_STATE_HANDSHAKE_NEW:
if (sslRet <= 0) {
DLOGI("Failed to complete handshake..but let it go on");
sslErr = SSL_get_error(pDtlsSession->pSsl, sslRet);
if (sslErr == SSL_ERROR_WANT_READ || sslErr == SSL_ERROR_WANT_WRITE) {
// If OpenSSL wants to read or write, it's an indication we should check the BIO
DLOGD("Handshake want READ/WRITE");
CHK_STATUS(dtlsCheckOutgoingDataBuffer(pDtlsSession));
} else {
DLOGI("Failed to complete handshake..but let it go on");
// Handle other errors
LOG_OPENSSL_ERROR("SSL_do_handshake");
}
pDtlsSession->handshakeState = DTLS_STATE_HANDSHAKE_IN_PROGRESS;
}
pDtlsSession->handshakeState = DTLS_STATE_HANDSHAKE_IN_PROGRESS;
break;
case DTLS_STATE_HANDSHAKE_IN_PROGRESS:
if (SSL_is_init_finished(pDtlsSession->pSsl)) {
pDtlsSession->handshakeState = DTLS_STATE_HANDSHAKE_COMPLETED;
ATOMIC_STORE_BOOL(&pDtlsSession->sslInitFinished, TRUE);
CHK_STATUS(dtlsSessionChangeState(pDtlsSession, RTC_DTLS_TRANSPORT_STATE_CONNECTED));
} else {
if (dtlsTimeoutRet < 0) {
pDtlsSession->handshakeState = DTLS_STATE_HANDSHAKE_ERROR;
dtlsHandshakeErrored = TRUE;
} else {
timedOut = (CVAR_WAIT(pDtlsSession->receivePacketCvar, pDtlsSession->sslLock, waitTime) == STATUS_OPERATION_TIMED_OUT);
if (timedOut) {
DLOGD("DTLS handshake timeout event occurred, going to retransmit");
DTLSv1_handle_timeout(pDtlsSession->pSsl);
}

// We start calculating start of handshake DTLS handshake time taken in server mode only after clientHello
// is received, until then, we are only waiting, so we should not count that time into handshake latency
// calculation
if (isServer && firstMsg) {
pDtlsSession->dtlsSessionStartTime = GETTIME();
firstMsg = FALSE;
}
CHK_STATUS(dtlsCheckOutgoingDataBuffer(pDtlsSession));
timedOut = (CVAR_WAIT(pDtlsSession->receivePacketCvar, pDtlsSession->sslLock, waitTime) == STATUS_OPERATION_TIMED_OUT);
if (timedOut) {
DLOGD("DTLS handshake timeout event occurred, going to retransmit");
DTLSv1_handle_timeout(pDtlsSession->pSsl);
}

// We start calculating start of handshake DTLS handshake time taken in server mode only after clientHello
// is received, until then, we are only waiting, so we should not count that time into handshake latency
// calculation
if (isServer && firstMsg) {
pDtlsSession->dtlsSessionStartTime = GETTIME();
firstMsg = FALSE;
}
CHK_STATUS(dtlsCheckOutgoingDataBuffer(pDtlsSession));
}
break;
case DTLS_STATE_HANDSHAKE_COMPLETED:
Expand Down
17 changes: 14 additions & 3 deletions src/source/Ice/IceAgent.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Kinesis Video Producer Callbacks Provider
* Ice Agent APIs
*/
#define LOG_CLASS "IceAgent"
#include "../Include_i.h"
Expand Down Expand Up @@ -61,7 +61,7 @@ STATUS createIceAgent(PCHAR username, PCHAR password, PIceAgentCallbacks pIceAge
pIceAgent->localNetworkInterfaceCount = ARRAY_SIZE(pIceAgent->localNetworkInterfaces);
pIceAgent->candidateGatheringEndTime = INVALID_TIMESTAMP_VALUE;

pIceAgent->lock = MUTEX_CREATE(FALSE);
pIceAgent->lock = MUTEX_CREATE(TRUE);

// Create the state machine
CHK_STATUS(createStateMachine(ICE_AGENT_STATE_MACHINE_STATES, ICE_AGENT_STATE_MACHINE_STATE_COUNT, (UINT64) pIceAgent, iceAgentGetCurrentTime,
Expand Down Expand Up @@ -580,6 +580,8 @@ STATUS iceAgentStartAgent(PIceAgent pIceAgent, PCHAR remoteUsername, PCHAR remot

CleanUp:

CHK_LOG_ERR(retStatus);

if (locked) {
MUTEX_UNLOCK(pIceAgent->lock);
}
Expand Down Expand Up @@ -913,7 +915,7 @@ STATUS iceAgentRestart(PIceAgent pIceAgent, PCHAR localIceUfrag, PCHAR localIceP
* pIceAgent->pDataSendingIceCandidatePair and its ice candidates. Therefore safe to proceed freeing resources */

for (i = 0; i < localCandidateCount; ++i) {
if (localCandidates[i] != pIceAgent->pDataSendingIceCandidatePair->local) {
if (pIceAgent->pDataSendingIceCandidatePair == NULL || localCandidates[i] != pIceAgent->pDataSendingIceCandidatePair->local) {
if (localCandidates[i]->iceCandidateType != ICE_CANDIDATE_TYPE_RELAYED) {
CHK_STATUS(connectionListenerRemoveConnection(pIceAgent->pConnectionListener, localCandidates[i]->pSocketConnection));
CHK_STATUS(freeSocketConnection(&localCandidates[i]->pSocketConnection));
Expand Down Expand Up @@ -2112,6 +2114,10 @@ STATUS iceAgentNominatingStateSetup(PIceAgent pIceAgent)

pIceAgent->stateEndTime = GETTIME() + pIceAgent->kvsRtcConfiguration.iceCandidateNominationTimeout;

MUTEX_UNLOCK(pIceAgent->lock);
locked = FALSE;
checkIceAgentStateMachine(pIceAgent);
jdelapla marked this conversation as resolved.
Show resolved Hide resolved

CleanUp:

CHK_LOG_ERR(retStatus);
Expand Down Expand Up @@ -2369,6 +2375,9 @@ STATUS incomingDataHandler(UINT64 customData, PSocketConnection pSocketConnectio
} else {
if (ATOMIC_LOAD_BOOL(&pIceAgent->processStun)) {
CHK_STATUS(handleStunPacket(pIceAgent, pBuffer, bufferLen, pSocketConnection, pSrc, pDest));
MUTEX_UNLOCK(pIceAgent->lock);
locked = FALSE;
checkIceAgentStateMachine(pIceAgent);
}
}

Expand Down Expand Up @@ -2482,6 +2491,7 @@ STATUS handleStunPacket(PIceAgent pIceAgent, PBYTE pBuffer, UINT32 bufferLen, PS
iceAgentGetCandidateTypeStr(pIceCandidatePair->local->iceCandidateType), pIceCandidatePair->local->id,
pIceCandidatePair->remote->id);
pIceCandidatePair->nominated = TRUE;
// checkIceAgentStateMachine(pIceAgent);
jdelapla marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -2583,6 +2593,7 @@ STATUS handleStunPacket(PIceAgent pIceAgent, PBYTE pBuffer, UINT32 bufferLen, PS
if (pIceCandidatePair->state != ICE_CANDIDATE_PAIR_STATE_SUCCEEDED) {
DLOGD("Pair succeeded! %s %s", pIceCandidatePair->local->id, pIceCandidatePair->remote->id);
pIceCandidatePair->state = ICE_CANDIDATE_PAIR_STATE_SUCCEEDED;
// checkIceAgentStateMachine(pIceAgent);
disa6302 marked this conversation as resolved.
Show resolved Hide resolved
retStatus = hashTableGet(pIceCandidatePair->requestSentTime, checkSum, &requestSentTime);
if (hashTableGet(pIceCandidatePair->requestSentTime, checkSum, &requestSentTime) == STATUS_SUCCESS) {
pIceCandidatePair->roundTripTime = GETTIME() - requestSentTime;
Expand Down
Loading
Loading