STATUS pollApp()

in source/src/AppCommon.c [932:1001]


STATUS pollApp(PAppConfiguration pAppConfiguration)
{
    ENTERS();
    STATUS retStatus = STATUS_SUCCESS;
    PStreamingSession pStreamingSession = NULL;
    UINT32 i, clientIdHashKey;
    BOOL locked = FALSE, peerConnectionFound = FALSE;

    CHK(pAppConfiguration != NULL, STATUS_APP_COMMON_NULL_ARG);

    while (!ATOMIC_LOAD_BOOL(&pAppConfiguration->sigInt)) {
        // Keep the main set of operations interlocked until cvar wait which would atomically unlock
        MUTEX_LOCK(pAppConfiguration->appConfigurationObjLock);
        locked = TRUE;

        // scan and cleanup terminated streaming session
        for (i = 0; i < pAppConfiguration->streamingSessionCount; ++i) {
            if (ATOMIC_LOAD_BOOL(&pAppConfiguration->streamingSessionList[i]->terminateFlag)) {
                pStreamingSession = pAppConfiguration->streamingSessionList[i];

                MUTEX_LOCK(pAppConfiguration->streamingSessionListReadLock);

                // swap with last element and decrement count
                pAppConfiguration->streamingSessionCount--;
                if (pAppConfiguration->streamingSessionCount > 0) {
                    pAppConfiguration->streamingSessionList[i] = pAppConfiguration->streamingSessionList[pAppConfiguration->streamingSessionCount];
                    i--;
                }

                // Remove from the hash table
                clientIdHashKey = COMPUTE_CRC32((PBYTE) pStreamingSession->peerId, (UINT32) STRLEN(pStreamingSession->peerId));
                CHK_STATUS((appHashTableContains(pAppConfiguration->pRemoteRtcPeerConnections, clientIdHashKey, &peerConnectionFound)));
                if (peerConnectionFound) {
                    CHK_STATUS((appHashTableRemove(pAppConfiguration->pRemoteRtcPeerConnections, clientIdHashKey)));
                }
                if (pAppConfiguration->streamingSessionCount == 0) {
                    shutdownMediaSource(pAppConfiguration->pMediaContext);
                }
                MUTEX_UNLOCK(pAppConfiguration->streamingSessionListReadLock);

                freeStreamingSession(&pStreamingSession);
            }
        }

        // Check if we need to re-create the signaling client on-the-fly
        if (ATOMIC_LOAD_BOOL(&pAppConfiguration->restartSignalingClient) && STATUS_SUCCEEDED(restartAppSignaling(&pAppConfiguration->appSignaling))) {
            // Re-set the variable again
            ATOMIC_STORE_BOOL(&pAppConfiguration->restartSignalingClient, FALSE);
        }
        CHK_STATUS((checkAppSignaling(&pAppConfiguration->appSignaling)));

        // Check if any lingering pending message queues
        CHK_STATUS((removeExpiredPendingMsgQ(pAppConfiguration->pRemotePeerPendingSignalingMessages, APP_PENDING_MESSAGE_CLEANUP_DURATION)));
        // periodically wake up and clean up terminated streaming session
        CVAR_WAIT(pAppConfiguration->cvar, pAppConfiguration->appConfigurationObjLock, APP_CLEANUP_WAIT_PERIOD);
        MUTEX_UNLOCK(pAppConfiguration->appConfigurationObjLock);
        locked = FALSE;
    }

CleanUp:

    CHK_LOG_ERR((retStatus));

    if (locked) {
        MUTEX_UNLOCK(pAppConfiguration->appConfigurationObjLock);
    }

    LEAVES();
    return retStatus;
}