Skip to content

Commit

Permalink
Fixing view sample with async ICE config retrieval. (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
MushMal authored May 1, 2020
1 parent bf0eabf commit d273359
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
28 changes: 27 additions & 1 deletion samples/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcP

if (pSampleConfiguration->useTurn) {
// Set the URIs from the configuration
CHK_STATUS(signalingClientGetIceConfigInfoCount(pSampleConfiguration->signalingClientHandle, &iceConfigCount));
CHK_STATUS(awaitGetIceConfigInfoCount(pSampleConfiguration->signalingClientHandle, &iceConfigCount));

/* signalingClientGetIceConfigInfoCount can return more than one turn server. Use only one to optimize
* candidate gathering latency. But user can also choose to use more than 1 turn server. */
Expand Down Expand Up @@ -373,6 +373,32 @@ STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcP
return retStatus;
}

STATUS awaitGetIceConfigInfoCount(SIGNALING_CLIENT_HANDLE signalingClientHandle, PUINT32 pIceConfigInfoCount)
{
STATUS retStatus = STATUS_SUCCESS;
UINT64 elapsed = 0;

CHK(IS_VALID_SIGNALING_CLIENT_HANDLE(signalingClientHandle) && pIceConfigInfoCount != NULL, STATUS_NULL_ARG);

while (TRUE) {
// Get the configuration count
CHK_STATUS(signalingClientGetIceConfigInfoCount(signalingClientHandle, pIceConfigInfoCount));

// Return OK if we have some ice configs
CHK(*pIceConfigInfoCount == 0, retStatus);

// Check for timeout
CHK_ERR(elapsed <= ASYNC_ICE_CONFIG_INFO_WAIT_TIMEOUT, STATUS_OPERATION_TIMED_OUT, "Couldn't retrieve ICE configurations in alotted time.");

THREAD_SLEEP(ICE_CONFIG_INFO_POLL_PERIOD);
elapsed += ICE_CONFIG_INFO_POLL_PERIOD;
}

CleanUp:

return retStatus;
}

STATUS createSampleStreamingSession(PSampleConfiguration pSampleConfiguration, PCHAR peerId, BOOL isMaster,
PSampleStreamingSession *ppSampleStreamingSession)
{
Expand Down
4 changes: 4 additions & 0 deletions samples/Samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ extern "C" {
#define SAMPLE_AUDIO_FRAME_DURATION (20 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND)
#define SAMPLE_VIDEO_FRAME_DURATION (HUNDREDS_OF_NANOS_IN_A_SECOND / DEFAULT_FPS_VALUE)

#define ASYNC_ICE_CONFIG_INFO_WAIT_TIMEOUT (3 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define ICE_CONFIG_INFO_POLL_PERIOD (20 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND)

#define CA_CERT_PEM_FILE_EXTENSION ".pem"
typedef enum {
SAMPLE_STREAMING_VIDEO_ONLY,
Expand Down Expand Up @@ -118,6 +121,7 @@ VOID sampleBandwidthEstimationHandler(UINT64, DOUBLE);
VOID onDataChannel(UINT64, PRtcDataChannel);
VOID onConnectionStateChange(UINT64, RTC_PEER_CONNECTION_STATE);
STATUS sessionCleanupWait(PSampleConfiguration);
STATUS awaitGetIceConfigInfoCount(SIGNALING_CLIENT_HANDLE, PUINT32);

#ifdef __cplusplus
}
Expand Down
18 changes: 9 additions & 9 deletions samples/kvsWebRTCClientViewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ INT32 main(INT32 argc, CHAR *argv[])

printf("[KVS Viewer] Signaling client created successfully\n");

// Enable the processing of the messages
retStatus = signalingClientConnectSync(pSampleConfiguration->signalingClientHandle);
if(retStatus != STATUS_SUCCESS) {
printf("[KVS Viewer] signalingClientConnectSync(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}

printf("[KVS Viewer] Signaling client connection to socket established\n");

// Initialize streaming session
MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = TRUE;
Expand All @@ -69,15 +78,6 @@ INT32 main(INT32 argc, CHAR *argv[])
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = FALSE;

// Enable the processing of the messages
retStatus = signalingClientConnectSync(pSampleConfiguration->signalingClientHandle);
if(retStatus != STATUS_SUCCESS) {
printf("[KVS Viewer] signalingClientConnectSync(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}

printf("[KVS Viewer] Signaling client connection to socket established\n");

memset(&offerSessionDescriptionInit, 0x00, SIZEOF(RtcSessionDescriptionInit));

retStatus = createOffer(pSampleStreamingSession->pPeerConnection, &offerSessionDescriptionInit);
Expand Down
26 changes: 21 additions & 5 deletions src/source/Signaling/Signaling.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ STATUS createSignalingSync(PSignalingClientInfoInternal pClientInfo, PChannelInf
ATOMIC_STORE_BOOL(&pSignalingClient->connected, FALSE);
ATOMIC_STORE_BOOL(&pSignalingClient->deleting, FALSE);
ATOMIC_STORE_BOOL(&pSignalingClient->deleted, FALSE);
ATOMIC_STORE_BOOL(&pSignalingClient->iceConfigRetrieved, FALSE);

// Add to the signal handler
// signal(SIGINT, lwsSignalHandler);
Expand Down Expand Up @@ -327,10 +328,18 @@ STATUS signalingGetIceConfigInfoCout(PSignalingClient pSignalingClient, PUINT32

CHK(pSignalingClient != NULL && pIceConfigCount != NULL, STATUS_NULL_ARG);

// Validate the state
CHK_STATUS(acceptStateMachineState(pSignalingClient->pStateMachine, SIGNALING_STATE_READY | SIGNALING_STATE_CONNECT | SIGNALING_STATE_CONNECTED));
// Validate the state in sync ICE config mode only
if (!pSignalingClient->pChannelInfo->asyncIceServerConfig) {
CHK_STATUS(acceptStateMachineState(pSignalingClient->pStateMachine,
SIGNALING_STATE_READY | SIGNALING_STATE_CONNECT |
SIGNALING_STATE_CONNECTED));
}

*pIceConfigCount = pSignalingClient->iceConfigCount;
if (ATOMIC_LOAD_BOOL(&pSignalingClient->iceConfigRetrieved)) {
*pIceConfigCount = pSignalingClient->iceConfigCount;
} else {
*pIceConfigCount = 0;
}

CleanUp:

Expand All @@ -348,8 +357,12 @@ STATUS signalingGetIceConfigInfo(PSignalingClient pSignalingClient, UINT32 index
CHK(pSignalingClient != NULL && ppIceConfigInfo != NULL, STATUS_NULL_ARG);
CHK(index < pSignalingClient->iceConfigCount, STATUS_INVALID_ARG);

// Validate the state
CHK_STATUS(acceptStateMachineState(pSignalingClient->pStateMachine, SIGNALING_STATE_READY | SIGNALING_STATE_CONNECT | SIGNALING_STATE_CONNECTED));
// Validate the state in sync ICE config mode only
if (!pSignalingClient->pChannelInfo->asyncIceServerConfig) {
CHK_STATUS(acceptStateMachineState(pSignalingClient->pStateMachine,
SIGNALING_STATE_READY | SIGNALING_STATE_CONNECT |
SIGNALING_STATE_CONNECTED));
}

*ppIceConfigInfo = &pSignalingClient->iceConfigs[index];

Expand Down Expand Up @@ -516,6 +529,9 @@ STATUS validateIceConfiguration(PSignalingClient pSignalingClient)

CHK(minTtl > ICE_CONFIGURATION_REFRESH_GRACE_PERIOD, STATUS_SIGNALING_ICE_TTL_LESS_THAN_GRACE_PERIOD);

// Indicate that we have successfully retrieved ICE configs
ATOMIC_STORE_BOOL(&pSignalingClient->iceConfigRetrieved, TRUE);

refreshPeriod = (pSignalingClient->clientInfo.iceRefreshPeriod != 0) ? pSignalingClient->clientInfo.iceRefreshPeriod :
minTtl - ICE_CONFIGURATION_REFRESH_GRACE_PERIOD;

Expand Down
3 changes: 3 additions & 0 deletions src/source/Signaling/Signaling.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ typedef struct {
// if it comes first forcing the state machine to loop back to connected state.
volatile ATOMIC_BOOL refreshIceConfig;

// Indicate whether the ICE configuration has been retrieved at least once
volatile ATOMIC_BOOL iceConfigRetrieved;

// Current version of the structure
UINT32 version;

Expand Down

0 comments on commit d273359

Please sign in to comment.