STATUS createStreamingSession()

in main/AppCommon.c [598:676]


STATUS createStreamingSession(PAppConfiguration pAppConfiguration, PCHAR peerId, PStreamingSession* ppStreamingSession)
{
    STATUS retStatus = STATUS_SUCCESS;
    PRtcMediaStreamTrack pVideoTrack = NULL;
    PRtcMediaStreamTrack pAudioTrack = NULL;
    PStreamingSession pStreamingSession = NULL;
    RtcRtpTransceiverInit audioRtpTransceiverInit;
    RtcRtpTransceiverInit videoRtpTransceiverInit;
    RTC_CODEC codec;

    CHK(NULL != (pVideoTrack = (PRtcMediaStreamTrack) MEMCALLOC(1, SIZEOF(RtcMediaStreamTrack))), STATUS_APP_COMMON_NOT_ENOUGH_MEMORY);
    CHK(NULL != (pAudioTrack = (PRtcMediaStreamTrack) MEMCALLOC(1, SIZEOF(RtcMediaStreamTrack))), STATUS_APP_COMMON_NOT_ENOUGH_MEMORY);

    pStreamingSession = (PStreamingSession) MEMCALLOC(1, SIZEOF(StreamingSession));
    CHK(pStreamingSession != NULL, STATUS_APP_COMMON_NOT_ENOUGH_MEMORY);

    CHK_STATUS((isMediaSourceReady(pAppConfiguration->pMediaContext)));

    STRCPY(pStreamingSession->peerId, peerId);
    ATOMIC_STORE_BOOL(&pStreamingSession->peerIdReceived, TRUE);

    pStreamingSession->pAppConfiguration = pAppConfiguration;
    pStreamingSession->rtcMetricsHistory.prevTs = GETTIME();
    // if we're the viewer, we control the trickle ice mode
    pStreamingSession->remoteCanTrickleIce = FALSE;

    ATOMIC_STORE_BOOL(&pStreamingSession->terminateFlag, FALSE);
    ATOMIC_STORE_BOOL(&pStreamingSession->candidateGatheringDone, FALSE);

    CHK_STATUS((initializePeerConnection(pAppConfiguration, &pStreamingSession->pPeerConnection)));
    CHK_STATUS((peerConnectionOnIceCandidate(pStreamingSession->pPeerConnection, (UINT64) pStreamingSession, onIceCandidateHandler)));
    CHK_STATUS((peerConnectionOnConnectionStateChange(pStreamingSession->pPeerConnection, (UINT64) pStreamingSession, onConnectionStateChange)));
    #ifdef ENABLE_DATA_CHANNEL
    CHK_STATUS((peerConnectionOnDataChannel(pStreamingSession->pPeerConnection, (UINT64) pStreamingSession, onDataChannel)));
    #endif

    // Add a SendRecv Transceiver of type video
    CHK_STATUS((queryMediaVideoCap(pAppConfiguration->pMediaContext, &codec)));
    CHK_STATUS((addSupportedCodec(pStreamingSession->pPeerConnection, codec)));
    pVideoTrack->kind = MEDIA_STREAM_TRACK_KIND_VIDEO;
    pVideoTrack->codec = codec;
    videoRtpTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
    STRCPY(pVideoTrack->streamId, APP_VIDEO_TRACK_STREAM_ID);
    STRCPY(pVideoTrack->trackId, APP_VIDEO_TRACK_ID);
    CHK_STATUS(
        (addTransceiver(pStreamingSession->pPeerConnection, pVideoTrack, &videoRtpTransceiverInit, &pStreamingSession->pVideoRtcRtpTransceiver)));

    CHK_STATUS(
        (transceiverOnBandwidthEstimation(pStreamingSession->pVideoRtcRtpTransceiver, (UINT64) pStreamingSession, onBandwidthEstimationHandler)));

    // Add a SendRecv Transceiver of type audio
    //CHK_STATUS((queryMediaAudioCap(pAppConfiguration->pMediaContext, &codec)));
    //CHK_STATUS((addSupportedCodec(pStreamingSession->pPeerConnection, codec)));
    //pAudioTrack->kind = MEDIA_STREAM_TRACK_KIND_AUDIO;
    //pAudioTrack->codec = codec;
    //audioRtpTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
    //STRCPY(pAudioTrack->streamId, APP_AUDIO_TRACK_STREAM_ID);
    //STRCPY(pAudioTrack->trackId, APP_AUDIO_TRACK_ID);
    //CHK_STATUS(
    //    (addTransceiver(pStreamingSession->pPeerConnection, pAudioTrack, &audioRtpTransceiverInit, &pStreamingSession->pAudioRtcRtpTransceiver)));

    //CHK_STATUS(
    //    (transceiverOnBandwidthEstimation(pStreamingSession->pAudioRtcRtpTransceiver, (UINT64) pStreamingSession, onBandwidthEstimationHandler)));
    // twcc bandwidth estimation
    //CHK_STATUS((peerConnectionOnSenderBandwidthEstimation(pStreamingSession->pPeerConnection, (UINT64) pStreamingSession,
    //                                                      onSenderBandwidthEstimationHandler)));

CleanUp:

    if (STATUS_FAILED(retStatus) && pStreamingSession != NULL) {
        freeStreamingSession(&pStreamingSession);
        pStreamingSession = NULL;
    }

    *ppStreamingSession = pStreamingSession;
    SAFE_MEMFREE(pVideoTrack);
    SAFE_MEMFREE(pAudioTrack);
    return retStatus;
}