diff --git a/samples/Common.c b/samples/Common.c index 0d78ec9957..f1c71621f2 100644 --- a/samples/Common.c +++ b/samples/Common.c @@ -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 + 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 92d5affaf5..e425ed8594 100644 --- a/samples/kvsWebRTCClientViewer.c +++ b/samples/kvsWebRTCClientViewer.c @@ -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; @@ -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; @@ -165,6 +188,28 @@ INT32 main(INT32 argc, CHAR* argv[]) goto CleanUp; } +#ifdef ENABLE_DATA_CHANNEL + 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"); +#endif + // Block until interrupted while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->interrupted) && !ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag)) { THREAD_SLEEP(HUNDREDS_OF_NANOS_IN_A_SECOND);