STATUS createStreamingSession()

in source/src/AppCommon.c [597:669]


STATUS createStreamingSession(PAppConfiguration pAppConfiguration, PCHAR peerId, PStreamingSession* ppStreamingSession)
{
    STATUS retStatus = STATUS_SUCCESS;
    RtcMediaStreamTrack videoTrack, audioTrack;
    PStreamingSession pStreamingSession = NULL;
    RtcRtpTransceiverInit audioRtpTransceiverInit;
    RtcRtpTransceiverInit videoRtpTransceiverInit;
    RTC_CODEC codec;
    MEMSET(&videoTrack, 0x00, SIZEOF(RtcMediaStreamTrack));
    MEMSET(&audioTrack, 0x00, SIZEOF(RtcMediaStreamTrack));

    pStreamingSession = (PStreamingSession) MEMCALLOC(1, SIZEOF(StreamingSession));
    CHK(pStreamingSession != NULL, STATUS_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)));
    CHK_STATUS((peerConnectionOnDataChannel(pStreamingSession->pPeerConnection, (UINT64) pStreamingSession, onDataChannel)));

    // Add a SendRecv Transceiver of type video
    CHK_STATUS((queryMediaVideoCap(pAppConfiguration->pMediaContext, &codec)));
    CHK_STATUS((addSupportedCodec(pStreamingSession->pPeerConnection, codec)));
    videoTrack.kind = MEDIA_STREAM_TRACK_KIND_VIDEO;
    videoTrack.codec = codec;
    videoRtpTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
    STRCPY(videoTrack.streamId, APP_VIDEO_TRACK_STREAM_ID);
    STRCPY(videoTrack.trackId, APP_VIDEO_TRACK_ID);
    CHK_STATUS(
        (addTransceiver(pStreamingSession->pPeerConnection, &videoTrack, &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)));
    audioTrack.kind = MEDIA_STREAM_TRACK_KIND_AUDIO;
    audioTrack.codec = codec;
    audioRtpTransceiverInit.direction = RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
    STRCPY(audioTrack.streamId, APP_AUDIO_TRACK_STREAM_ID);
    STRCPY(audioTrack.trackId, APP_AUDIO_TRACK_ID);
    CHK_STATUS(
        (addTransceiver(pStreamingSession->pPeerConnection, &audioTrack, &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;
    return retStatus;
}