From cb89e2a7991182bb7b887e827ec505bc6f4d6423 Mon Sep 17 00:00:00 2001 From: Niyati Maheshwari Date: Mon, 26 Jul 2021 16:57:30 -0700 Subject: [PATCH 1/5] create data channel sample --- samples/Common.c | 7 +++++ samples/kvsWebRTCClientViewer.c | 54 +++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/samples/Common.c b/samples/Common.c index 0d78ec9957..88b9a845a6 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -1,5 +1,6 @@ #define LOG_CLASS "WebRtcSamples" #include "Samples.h" +// #define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master" PSampleConfiguration gSampleConfiguration = NULL; @@ -21,6 +22,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 + /*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) diff --git a/samples/kvsWebRTCClientViewer.c b/samples/kvsWebRTCClientViewer.c index 92d5affaf5..c8c72cd1ec 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -1,7 +1,39 @@ #include "Samples.h" +#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer" 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); + STATUS retStatus = STATUS_SUCCESS; + if (isBinary) { + DLOGI("DataChannel Binary Message"); + } else { + DLOGI("DataChannel String Message: %.*s\n", pMessageLen, pMessage); + } + // Send a response to the message sent by the master + 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); + } + DLOGI("[KVS Viewer] successfully sent the message on the data channel\n"); +} + +// onOpen callback for the onOpen event of a viewer created data channel +VOID dataChannelOnOpenCallback(UINT64 customData, PRtcDataChannel pDataChannel) { + DLOGI("New DataChannel has been opened %s \n", pDataChannel->name); + dataChannelOnMessage(pDataChannel, customData, dataChannelOnMessageCallback); + ATOMIC_INCREMENT((PSIZE_T) customData); + STATUS retStatus = STATUS_SUCCESS; + // 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; @@ -81,13 +113,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; @@ -165,6 +195,26 @@ INT32 main(INT32 argc, CHAR* argv[]) goto CleanUp; } + PRtcDataChannel pDataChannel = NULL; + 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"); + // Block until interrupted while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->interrupted) && !ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag)) { THREAD_SLEEP(HUNDREDS_OF_NANOS_IN_A_SECOND); From 581cb0725b191ad96d98a5274cbb5405e7ef3c6a Mon Sep 17 00:00:00 2001 From: Niyati Maheshwari Date: Wed, 28 Jul 2021 17:27:11 -0700 Subject: [PATCH 2/5] moved variables to Samples.h, encapsulated with ENABLE_DATA_CHANNEL directive --- samples/Common.c | 5 ++--- samples/Samples.h | 3 +++ samples/kvsWebRTCClientViewer.c | 9 ++------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/samples/Common.c b/samples/Common.c index 88b9a845a6..f1c71621f2 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -1,6 +1,5 @@ #define LOG_CLASS "WebRtcSamples" #include "Samples.h" -// #define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master" PSampleConfiguration gSampleConfiguration = NULL; @@ -23,11 +22,11 @@ VOID onDataChannelMessage(UINT64 customData, PRtcDataChannel pDataChannel, BOOL DLOGI("DataChannel String Message: %.*s\n", pMessageLen, pMessage); } // Send a response to the message sent by the viewer - /*STATUS retStatus = STATUS_SUCCESS; + 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) diff --git a/samples/Samples.h b/samples/Samples.h index 34a8baec85..6b721a8d6f 100644 --- a/samples/Samples.h +++ b/samples/Samples.h @@ -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 diff --git a/samples/kvsWebRTCClientViewer.c b/samples/kvsWebRTCClientViewer.c index c8c72cd1ec..238e03066b 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -1,5 +1,4 @@ #include "Samples.h" -#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer" extern PSampleConfiguration gSampleConfiguration; @@ -13,12 +12,6 @@ VOID dataChannelOnMessageCallback(UINT64 customData, PRtcDataChannel pDataChanne } else { DLOGI("DataChannel String Message: %.*s\n", pMessageLen, pMessage); } - // Send a response to the message sent by the master - 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); - } - DLOGI("[KVS Viewer] successfully sent the message on the data channel\n"); } // onOpen callback for the onOpen event of a viewer created data channel @@ -195,6 +188,7 @@ INT32 main(INT32 argc, CHAR* argv[]) goto CleanUp; } +#ifdef ENABLE_DATA_CHANNEL PRtcDataChannel pDataChannel = NULL; PRtcPeerConnection pPeerConnection = pSampleStreamingSession->pPeerConnection; SIZE_T datachannelLocalOpenCount = 0; @@ -214,6 +208,7 @@ INT32 main(INT32 argc, CHAR* argv[]) goto CleanUp; } printf("[KVS Viewer] Data Channel open now...\n"); +#endif // Block until interrupted while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->interrupted) && !ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag)) { From 189830d52e4d2def289c908d197c124a68d33334 Mon Sep 17 00:00:00 2001 From: Niyati Maheshwari Date: Thu, 29 Jul 2021 09:33:33 -0700 Subject: [PATCH 3/5] removed unused variables, moved variable declarations to the top of a block --- samples/kvsWebRTCClientViewer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/kvsWebRTCClientViewer.c b/samples/kvsWebRTCClientViewer.c index 238e03066b..e425ed8594 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -6,7 +6,7 @@ extern PSampleConfiguration gSampleConfiguration; VOID dataChannelOnMessageCallback(UINT64 customData, PRtcDataChannel pDataChannel, BOOL isBinary, PBYTE pMessage, UINT32 pMessageLen) { UNUSED_PARAM(customData); - STATUS retStatus = STATUS_SUCCESS; + UNUSED_PARAM(pDataChannel); if (isBinary) { DLOGI("DataChannel Binary Message"); } else { @@ -16,10 +16,10 @@ VOID dataChannelOnMessageCallback(UINT64 customData, PRtcDataChannel pDataChanne // 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); - STATUS retStatus = STATUS_SUCCESS; // 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){ From 121011177991258a6099f2a1ca7b6188b7d918bf Mon Sep 17 00:00:00 2001 From: Niyati Maheshwari Date: Mon, 26 Jul 2021 16:57:30 -0700 Subject: [PATCH 4/5] create data channel sample --- samples/Common.c | 1 + samples/kvsWebRTCClientViewer.c | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/Common.c b/samples/Common.c index f1c71621f2..14b4996e88 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -1,5 +1,6 @@ #define LOG_CLASS "WebRtcSamples" #include "Samples.h" +// #define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master" PSampleConfiguration gSampleConfiguration = NULL; diff --git a/samples/kvsWebRTCClientViewer.c b/samples/kvsWebRTCClientViewer.c index e425ed8594..d608ea668a 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -1,4 +1,5 @@ #include "Samples.h" +#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer" extern PSampleConfiguration gSampleConfiguration; From ec1c939152f516d7db8a5a1e8dbbfcd2255cfe5f Mon Sep 17 00:00:00 2001 From: Niyati Maheshwari Date: Wed, 28 Jul 2021 17:27:11 -0700 Subject: [PATCH 5/5] moved variables to Samples.h, encapsulated with ENABLE_DATA_CHANNEL directive --- samples/Common.c | 1 - samples/kvsWebRTCClientViewer.c | 1 - 2 files changed, 2 deletions(-) diff --git a/samples/Common.c b/samples/Common.c index 14b4996e88..f1c71621f2 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -1,6 +1,5 @@ #define LOG_CLASS "WebRtcSamples" #include "Samples.h" -// #define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master" PSampleConfiguration gSampleConfiguration = NULL; diff --git a/samples/kvsWebRTCClientViewer.c b/samples/kvsWebRTCClientViewer.c index d608ea668a..e425ed8594 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -1,5 +1,4 @@ #include "Samples.h" -#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer" extern PSampleConfiguration gSampleConfiguration;