Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created August 26, 2020 17:26
Show Gist options
  • Save fernandoc1/f534064d43094fde5140ec749649be34 to your computer and use it in GitHub Desktop.
Save fernandoc1/f534064d43094fde5140ec749649be34 to your computer and use it in GitHub Desktop.
This is my samples/kvsWebRTCClientMaster.c modifications
#include "shared_memory_source.hpp"
#include "Samples.h"
extern PSampleConfiguration gSampleConfiguration;
FrameSource* frameSource = new SharedMemorySource();
// #define VERBOSE
INT32 main(INT32 argc, CHAR* argv[])
{
STATUS retStatus = STATUS_SUCCESS;
UINT32 frameSize;
PSampleConfiguration pSampleConfiguration = NULL;
SignalingClientMetrics signalingClientMetrics;
signalingClientMetrics.version = 0;
#ifndef _WIN32
signal(SIGINT, sigintHandler);
#endif
// do tricketIce by default
printf("[KVS Master] Using trickleICE by default\n");
retStatus =
createSampleConfiguration(argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME, SIGNALING_CHANNEL_ROLE_TYPE_MASTER, TRUE, FALSE, &pSampleConfiguration);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] createSampleConfiguration(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Master] Created signaling channel %s\n", (argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME));
if (pSampleConfiguration->enableFileLogging) {
retStatus =
createFileLogger(FILE_LOGGING_BUFFER_SIZE, MAX_NUMBER_OF_LOG_FILES, (PCHAR) FILE_LOGGER_LOG_FILE_DIRECTORY_PATH, TRUE, TRUE, NULL);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] createFileLogger(): operation returned status code: 0x%08x \n", retStatus);
pSampleConfiguration->enableFileLogging = FALSE;
}
}
// Set the audio and video handlers
pSampleConfiguration->videoSource = sendVideoPackets;
pSampleConfiguration->receiveAudioVideoSource = sampleReceiveAudioFrame;
pSampleConfiguration->onDataChannel = onDataChannel;
//pSampleConfiguration->mediaType = SAMPLE_STREAMING_AUDIO_VIDEO;
pSampleConfiguration->mediaType = SAMPLE_STREAMING_VIDEO_ONLY;
printf("[KVS Master] Finished setting audio and video handlers\n");
// Initialize KVS WebRTC. This must be done before anything else, and must only be done once.
retStatus = initKvsWebRtc();
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] initKvsWebRtc(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Master] KVS WebRTC initialization completed successfully\n");
pSampleConfiguration->signalingClientCallbacks.messageReceivedFn = masterMessageReceived;
strcpy(pSampleConfiguration->clientInfo.clientId, SAMPLE_MASTER_CLIENT_ID);
retStatus = createSignalingClientSync(&pSampleConfiguration->clientInfo, &pSampleConfiguration->channelInfo,
&pSampleConfiguration->signalingClientCallbacks, pSampleConfiguration->pCredentialProvider,
&pSampleConfiguration->signalingClientHandle);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] createSignalingClientSync(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Master] Signaling client created successfully\n");
// Enable the processing of the messages
retStatus = signalingClientConnectSync(pSampleConfiguration->signalingClientHandle);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] signalingClientConnectSync(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Master] Signaling client connection to socket established\n");
gSampleConfiguration = pSampleConfiguration;
printf("[KVS Master] Channel %s set up done \n", (argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME));
// Checking for termination
retStatus = sessionCleanupWait(pSampleConfiguration);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] sessionCleanupWait(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Master] Streaming session terminated\n");
CleanUp:
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] Terminated with status code 0x%08x", retStatus);
}
printf("[KVS Master] Cleaning up....\n");
if (pSampleConfiguration != NULL) {
// Kick of the termination sequence
ATOMIC_STORE_BOOL(&pSampleConfiguration->appTerminateFlag, TRUE);
// Join the threads
if (pSampleConfiguration->videoSenderTid != (UINT64) NULL) {
// Join the threads
THREAD_JOIN(pSampleConfiguration->videoSenderTid, NULL);
}
if (pSampleConfiguration->audioSenderTid != (UINT64) NULL) {
// Join the threads
THREAD_JOIN(pSampleConfiguration->audioSenderTid, NULL);
}
if (pSampleConfiguration->enableFileLogging) {
freeFileLogger();
}
retStatus = signalingClientGetMetrics(pSampleConfiguration->signalingClientHandle, &signalingClientMetrics);
if (retStatus == STATUS_SUCCESS) {
logSignalingClientStats(&signalingClientMetrics);
} else {
printf("[KVS Master] signalingClientGetMetrics() operation returned status code: 0x%08x", retStatus);
}
retStatus = freeSignalingClient(&pSampleConfiguration->signalingClientHandle);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] freeSignalingClient(): operation returned status code: 0x%08x", retStatus);
}
retStatus = freeSampleConfiguration(&pSampleConfiguration);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] freeSampleConfiguration(): operation returned status code: 0x%08x", retStatus);
}
}
printf("[KVS Master] Cleanup done\n");
return (INT32) retStatus;
}
PVOID sendVideoPackets(PVOID args)
{
STATUS retStatus = STATUS_SUCCESS;
PSampleConfiguration pSampleConfiguration = (PSampleConfiguration) args;
RtcEncoderStats encoderStats;
Frame frame;
UINT32 fileIndex = 0, frameSize;
STATUS status;
UINT32 i;
MEMSET(&encoderStats, 0x00, SIZEOF(RtcEncoderStats));
if (pSampleConfiguration == NULL) {
printf("[KVS Master] sendVideoPackets(): operation returned status code: 0x%08x \n", STATUS_NULL_ARG);
goto CleanUp;
}
memset(&frame, 0x00, sizeof(Frame));
while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->appTerminateFlag)) {
frameSource->getFrame();
frame.frameData = frameSource->getDataPtr();
frame.size = frameSource->getDataLength();
frame.presentationTs += SAMPLE_VIDEO_FRAME_DURATION;
if (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->updatingSampleStreamingSessionList)) {
ATOMIC_INCREMENT(&pSampleConfiguration->streamingSessionListReadingThreadCount);
for (i = 0; i < pSampleConfiguration->streamingSessionCount; ++i) {
status = writeFrame(pSampleConfiguration->sampleStreamingSessionList[i]->pVideoRtcRtpTransceiver, &frame);
encoderStats.encodeTimeMsec = 4; // update encode time to an arbitrary number to demonstrate stats update
//updateEncoderStats(pSampleConfiguration->sampleStreamingSessionList[i]->pVideoRtcRtpTransceiver, &encoderStats);
if (status != STATUS_SUCCESS) {
#ifdef VERBOSE
printf("writeFrame() failed with 0x%08x", status);
#endif
}
}
ATOMIC_DECREMENT(&pSampleConfiguration->streamingSessionListReadingThreadCount);
}
//THREAD_SLEEP(SAMPLE_VIDEO_FRAME_DURATION);
}
CleanUp:
return (PVOID)(ULONG_PTR) retStatus;
}
PVOID sampleReceiveAudioFrame(PVOID args)
{
STATUS retStatus = STATUS_SUCCESS;
PSampleStreamingSession pSampleStreamingSession = (PSampleStreamingSession) args;
if (pSampleStreamingSession == NULL) {
printf("[KVS Master] sampleReceiveAudioFrame(): operation returned status code: 0x%08x \n", STATUS_NULL_ARG);
goto CleanUp;
}
retStatus = transceiverOnFrame(pSampleStreamingSession->pAudioRtcRtpTransceiver, (UINT64) pSampleStreamingSession, sampleFrameHandler);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Master] transceiverOnFrame(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
CleanUp:
return (PVOID)(ULONG_PTR) retStatus;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment