Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create data channel sample #1203

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions samples/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ VOID onDataChannelMessage(UINT64 customData, PRtcDataChannel pDataChannel, BOOL
} else {
DLOGI("DataChannel String Message: %.*s\n", pMessageLen, pMessage);
}
// Send a response to the message sent by the viewer
niyatim23 marked this conversation as resolved.
Show resolved Hide resolved
STATUS retStatus = STATUS_SUCCESS;
retStatus = dataChannelSend(pDataChannel, FALSE, (PBYTE) MASTER_DATA_CHANNEL_MESSAGE, STRLEN(MASTER_DATA_CHANNEL_MESSAGE));
if(retStatus != STATUS_SUCCESS) {
DLOGI("[KVS Master] dataChannelSend(): operation returned status code: 0x%08x \n", retStatus);
}
}

VOID onDataChannel(UINT64 customData, PRtcDataChannel pRtcDataChannel)
Expand Down
3 changes: 3 additions & 0 deletions samples/Samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ extern "C" {
#define IOT_CORE_ROLE_ALIAS ((PCHAR) "AWS_IOT_CORE_ROLE_ALIAS")
#define IOT_CORE_THING_NAME ((PCHAR) "AWS_IOT_CORE_THING_NAME")

#define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master"
#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer"

/* Uncomment the following line in order to enable IoT credentials checks in the provided samples */
//#define IOT_CORE_ENABLE_CREDENTIALS 1

Expand Down
49 changes: 47 additions & 2 deletions samples/kvsWebRTCClientViewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

extern PSampleConfiguration gSampleConfiguration;

// onMessage callback for a message received by the viewer on a data channel
VOID dataChannelOnMessageCallback(UINT64 customData, PRtcDataChannel pDataChannel, BOOL isBinary, PBYTE pMessage, UINT32 pMessageLen)
{
UNUSED_PARAM(customData);
UNUSED_PARAM(pDataChannel);
if (isBinary) {
DLOGI("DataChannel Binary Message");
} else {
DLOGI("DataChannel String Message: %.*s\n", pMessageLen, pMessage);
}
}

// onOpen callback for the onOpen event of a viewer created data channel
VOID dataChannelOnOpenCallback(UINT64 customData, PRtcDataChannel pDataChannel) {
STATUS retStatus = STATUS_SUCCESS;
DLOGI("New DataChannel has been opened %s \n", pDataChannel->name);
dataChannelOnMessage(pDataChannel, customData, dataChannelOnMessageCallback);
ATOMIC_INCREMENT((PSIZE_T) customData);
// Sending first message to the master over the data channel
retStatus = dataChannelSend(pDataChannel, FALSE, (PBYTE) VIEWER_DATA_CHANNEL_MESSAGE, STRLEN(VIEWER_DATA_CHANNEL_MESSAGE));
if(retStatus != STATUS_SUCCESS){
DLOGI("[KVS Viewer] dataChannelSend(): operation returned status code: 0x%08x \n", retStatus);
}
}

INT32 main(INT32 argc, CHAR* argv[])
{
STATUS retStatus = STATUS_SUCCESS;
Expand Down Expand Up @@ -81,13 +106,11 @@ INT32 main(INT32 argc, CHAR* argv[])
// Initialize streaming session
MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = TRUE;

retStatus = createSampleStreamingSession(pSampleConfiguration, NULL, FALSE, &pSampleStreamingSession);
if (retStatus != STATUS_SUCCESS) {
printf("[KVS Viewer] createSampleStreamingSession(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}

printf("[KVS Viewer] Creating streaming session...completed\n");
pSampleConfiguration->sampleStreamingSessionList[pSampleConfiguration->streamingSessionCount++] = pSampleStreamingSession;

Expand Down Expand Up @@ -165,6 +188,28 @@ INT32 main(INT32 argc, CHAR* argv[])
goto CleanUp;
}

#ifdef ENABLE_DATA_CHANNEL
PRtcDataChannel pDataChannel = NULL;
niyatim23 marked this conversation as resolved.
Show resolved Hide resolved
PRtcPeerConnection pPeerConnection = pSampleStreamingSession->pPeerConnection;
SIZE_T datachannelLocalOpenCount = 0;

// Creating a new datachannel on the peer connection of the existing sample streaming session
retStatus = createDataChannel(pPeerConnection, pChannelName, NULL, &pDataChannel);
if(retStatus != STATUS_SUCCESS) {
printf("[KVS Viewer] createDataChannel(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Viewer] Creating data channel...completed\n");

// Setting a callback for when the data channel is open
retStatus = dataChannelOnOpen(pDataChannel, (UINT64) &datachannelLocalOpenCount, dataChannelOnOpenCallback);
if(retStatus != STATUS_SUCCESS) {
printf("[KVS Viewer] dataChannelOnOpen(): operation returned status code: 0x%08x \n", retStatus);
goto CleanUp;
}
printf("[KVS Viewer] Data Channel open now...\n");
#endif

niyatim23 marked this conversation as resolved.
Show resolved Hide resolved
// Block until interrupted
while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->interrupted) && !ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag)) {
THREAD_SLEEP(HUNDREDS_OF_NANOS_IN_A_SECOND);
Expand Down