Skip to content

Commit

Permalink
Add initializer flag for the main context
Browse files Browse the repository at this point in the history
  • Loading branch information
disa6302 committed Sep 21, 2023
1 parent 04008a6 commit b4f7574
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/source/PeerConnection/PeerConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ PWebRtcClientContext getWebRtcClientInstance()
PStunIpAddrContext getStunIpContext()
{
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();
if (!pWebRtcClientContext->isContextInitialized) {
return NULL;
}
return pWebRtcClientContext->pStunIpAddrCtx;
}

STATUS createWebRtcClientInstance()
{
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();
STATUS retStatus = STATUS_SUCCESS;
CHK_WARN(!pWebRtcClientContext->isContextInitialized, retStatus, "WebRtc client context already initialized, nothing to do");

CHK_WARN(pWebRtcClientContext->pStunIpAddrCtx == NULL, STATUS_INVALID_OPERATION, "STUN object already allocated");
pWebRtcClientContext->pStunIpAddrCtx = (PStunIpAddrContext) MEMCALLOC(1, SIZEOF(StunIpAddrContext));
CHK_ERR(pWebRtcClientContext->pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "Memory allocation for WebRtc client object failed");
pWebRtcClientContext->pStunIpAddrCtx->lock = MUTEX_CREATE(FALSE);
pWebRtcClientContext->pStunIpAddrCtx->expirationDuration = 2 * HUNDREDS_OF_NANOS_IN_AN_HOUR;

pWebRtcClientContext->isContextInitialized = TRUE;
CleanUp:
return retStatus;
}
Expand Down Expand Up @@ -751,7 +756,6 @@ STATUS onSetStunServerIp(UINT64 customData, PCHAR url, PKvsIpAddress pIpAddr)
{
UNUSED_PARAM(customData);
STATUS retStatus = STATUS_SUCCESS;
CHAR addressResolved[KVS_IP_ADDRESS_STRING_BUFFER_LEN + 1] = {'\0'};
PStunIpAddrContext pStunIpAddrCtx = getStunIpContext();
CHK_ERR(pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "WebRTC Client object could not be created");
UINT64 currentTime = GETTIME();
Expand Down Expand Up @@ -1579,6 +1583,7 @@ STATUS cleanupWebRtcClientContext()
// Stun object cleanup
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();

CHK_WARN(pWebRtcClientContext->isContextInitialized, STATUS_INVALID_OPERATION, "WebRtc context not initialized, nothing to clean up");
CHK_WARN(pWebRtcClientContext->pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "Destroying STUN object without setting up");

/* Start of handling STUN object */
Expand All @@ -1596,6 +1601,7 @@ STATUS cleanupWebRtcClientContext()
/* End of handling STUN object */

DLOGI("Destroyed WebRtc client context");
pWebRtcClientContext->isContextInitialized = FALSE;
CleanUp:
return retStatus;
}
Expand Down
1 change: 1 addition & 0 deletions src/source/PeerConnection/PeerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ typedef struct {
// Members of the singleton are responsible for their own sync mechanisms.
typedef struct {
PStunIpAddrContext pStunIpAddrCtx;
BOOL isContextInitialized;
} WebRtcClientContext, *PWebRtcClientContext;

STATUS onFrameReadyFunc(UINT64, UINT16, UINT16, UINT32);
Expand Down

0 comments on commit b4f7574

Please sign in to comment.