-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathkvsWebRTCClientViewer.c
221 lines (180 loc) · 9.27 KB
/
kvsWebRTCClientViewer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "Samples.h"
extern PSampleConfiguration gSampleConfiguration;
#ifdef ENABLE_DATA_CHANNEL
// 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", 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 ", 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 ", retStatus);
}
}
#endif
INT32 main(INT32 argc, CHAR* argv[])
{
STATUS retStatus = STATUS_SUCCESS;
RtcSessionDescriptionInit offerSessionDescriptionInit;
UINT32 buffLen = 0;
SignalingMessage message;
PSampleConfiguration pSampleConfiguration = NULL;
PSampleStreamingSession pSampleStreamingSession = NULL;
RTC_CODEC audioCodec = RTC_CODEC_OPUS;
RTC_CODEC videoCodec = RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE;
BOOL locked = FALSE;
PCHAR pChannelName;
CHAR clientId[256];
CreateSampleConfigurationParams createSampleConfigurationParams;
SET_INSTRUMENTED_ALLOCATORS();
UINT32 logLevel = setLogLevel();
#ifndef _WIN32
signal(SIGINT, sigintHandler);
#endif
#ifdef IOT_CORE_ENABLE_CREDENTIALS
CHK_ERR((pChannelName = argc > 1 ? argv[1] : GETENV(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION,
"AWS_IOT_CORE_THING_NAME must be set");
#else
pChannelName = argc > 1 ? argv[1] : SAMPLE_CHANNEL_NAME;
#endif
if (argc > 2) {
if (!STRCMP(argv[2], AUDIO_CODEC_NAME_OPUS)) {
audioCodec = RTC_CODEC_OPUS;
} else if (!STRCMP(argv[2], AUDIO_CODEC_NAME_ALAW)) {
audioCodec = RTC_CODEC_ALAW;
} else if (!STRCMP(argv[2], AUDIO_CODEC_NAME_MULAW)) {
audioCodec = RTC_CODEC_MULAW;
} else {
DLOGI("[KVS Viewer] Defaulting to Opus audio codec");
}
}
if (argc > 3) {
if (!STRCMP(argv[3], VIDEO_CODEC_NAME_H265)) {
videoCodec = RTC_CODEC_H265;
} else if (!STRCMP(argv[3], VIDEO_CODEC_NAME_VP8)) {
videoCodec = RTC_CODEC_VP8;
} else {
DLOGI("[KVS Viewer] Defaulting to H264 video codec");
}
}
createSampleConfigurationParams.channelName = pChannelName;
createSampleConfigurationParams.roleType = SIGNALING_CHANNEL_ROLE_TYPE_VIEWER;
createSampleConfigurationParams.trickleIce = TRUE;
createSampleConfigurationParams.useTurn = TRUE;
createSampleConfigurationParams.useDualStackEndpoints = FALSE;
createSampleConfigurationParams.logLevel = logLevel;
CHK_STATUS(createSampleConfiguration(&createSampleConfigurationParams, &pSampleConfiguration));
pSampleConfiguration->mediaType = SAMPLE_STREAMING_AUDIO_VIDEO;
pSampleConfiguration->audioCodec = audioCodec;
pSampleConfiguration->videoCodec = videoCodec;
// Initialize KVS WebRTC. This must be done before anything else, and must only be done once.
CHK_STATUS(initKvsWebRtc());
DLOGI("[KVS Viewer] KVS WebRTC initialization completed successfully");
#ifdef ENABLE_DATA_CHANNEL
pSampleConfiguration->onDataChannel = onDataChannel;
#endif
SPRINTF(clientId, "%s_%u", SAMPLE_VIEWER_CLIENT_ID, RAND() % MAX_UINT32);
CHK_STATUS(initSignaling(pSampleConfiguration, clientId));
DLOGI("[KVS Viewer] Signaling client connection established");
// Initialize streaming session
MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = TRUE;
CHK_STATUS(createSampleStreamingSession(pSampleConfiguration, NULL, FALSE, &pSampleStreamingSession));
DLOGI("[KVS Viewer] Creating streaming session...completed");
pSampleConfiguration->sampleStreamingSessionList[pSampleConfiguration->streamingSessionCount++] = pSampleStreamingSession;
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = FALSE;
MEMSET(&offerSessionDescriptionInit, 0x00, SIZEOF(RtcSessionDescriptionInit));
offerSessionDescriptionInit.useTrickleIce = pSampleStreamingSession->remoteCanTrickleIce;
CHK_STATUS(setLocalDescription(pSampleStreamingSession->pPeerConnection, &offerSessionDescriptionInit));
DLOGI("[KVS Viewer] Completed setting local description");
CHK_STATUS(transceiverOnFrame(pSampleStreamingSession->pAudioRtcRtpTransceiver, (UINT64) pSampleStreamingSession, sampleAudioFrameHandler));
CHK_STATUS(transceiverOnFrame(pSampleStreamingSession->pVideoRtcRtpTransceiver, (UINT64) pSampleStreamingSession, sampleVideoFrameHandler));
if (!pSampleConfiguration->trickleIce) {
DLOGI("[KVS Viewer] Non trickle ice. Wait for Candidate collection to complete");
MUTEX_LOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = TRUE;
while (!ATOMIC_LOAD_BOOL(&pSampleStreamingSession->candidateGatheringDone)) {
CHK_WARN(!ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag), STATUS_OPERATION_TIMED_OUT,
"application terminated and candidate gathering still not done");
CVAR_WAIT(pSampleConfiguration->cvar, pSampleConfiguration->sampleConfigurationObjLock, 5 * HUNDREDS_OF_NANOS_IN_A_SECOND);
}
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);
locked = FALSE;
DLOGI("[KVS Viewer] Candidate collection completed");
}
CHK_STATUS(createOffer(pSampleStreamingSession->pPeerConnection, &offerSessionDescriptionInit));
DLOGI("[KVS Viewer] Offer creation successful");
DLOGI("[KVS Viewer] Generating JSON of session description....");
CHK_STATUS(serializeSessionDescriptionInit(&offerSessionDescriptionInit, NULL, &buffLen));
if (buffLen >= SIZEOF(message.payload)) {
DLOGE("[KVS Viewer] serializeSessionDescriptionInit(): operation returned status code: 0x%08x ", STATUS_INVALID_OPERATION);
retStatus = STATUS_INVALID_OPERATION;
goto CleanUp;
}
CHK_STATUS(serializeSessionDescriptionInit(&offerSessionDescriptionInit, message.payload, &buffLen));
message.version = SIGNALING_MESSAGE_CURRENT_VERSION;
message.messageType = SIGNALING_MESSAGE_TYPE_OFFER;
STRCPY(message.peerClientId, SAMPLE_MASTER_CLIENT_ID);
message.payloadLen = (buffLen / SIZEOF(CHAR)) - 1;
message.correlationId[0] = '\0';
CHK_STATUS(signalingClientSendMessageSync(pSampleConfiguration->signalingClientHandle, &message));
#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
CHK_STATUS(createDataChannel(pPeerConnection, pChannelName, NULL, &pDataChannel));
DLOGI("[KVS Viewer] Creating data channel...completed");
// Setting a callback for when the data channel is open
CHK_STATUS(dataChannelOnOpen(pDataChannel, (UINT64) &datachannelLocalOpenCount, dataChannelOnOpenCallback));
DLOGI("[KVS Viewer] Data Channel open now...");
#endif
// Block until interrupted
while (!ATOMIC_LOAD_BOOL(&pSampleConfiguration->interrupted) && !ATOMIC_LOAD_BOOL(&pSampleStreamingSession->terminateFlag)) {
THREAD_SLEEP(HUNDREDS_OF_NANOS_IN_A_SECOND);
}
CleanUp:
if (retStatus != STATUS_SUCCESS) {
DLOGE("[KVS Viewer] Terminated with status code 0x%08x", retStatus);
}
DLOGI("[KVS Viewer] Cleaning up....");
if (locked) {
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);
}
if (pSampleConfiguration->enableFileLogging) {
freeFileLogger();
}
if (pSampleConfiguration != NULL) {
retStatus = freeSignalingClient(&pSampleConfiguration->signalingClientHandle);
if (retStatus != STATUS_SUCCESS) {
DLOGE("[KVS Viewer] freeSignalingClient(): operation returned status code: 0x%08x ", retStatus);
}
retStatus = freeSampleConfiguration(&pSampleConfiguration);
if (retStatus != STATUS_SUCCESS) {
DLOGE("[KVS Viewer] freeSampleConfiguration(): operation returned status code: 0x%08x ", retStatus);
}
}
DLOGI("[KVS Viewer] Cleanup done");
RESET_INSTRUMENTED_ALLOCATORS();
// https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
// We can only return with 0 - 127. Some platforms treat exit code >= 128
// to be a success code, which might give an unintended behaviour.
// Some platforms also treat 1 or 0 differently, so it's better to use
// EXIT_FAILURE and EXIT_SUCCESS macros for portability.
return STATUS_FAILED(retStatus) ? EXIT_FAILURE : EXIT_SUCCESS;
}