Amazon Kinesis Webrtc C SDK
LwsApiCalls.h
Go to the documentation of this file.
1 /*******************************************
2 Signaling LibWebSocket based API calls include file
3 *******************************************/
4 #ifndef __KINESIS_VIDEO_WEBRTC_LWS_API_CALLS__
5 #define __KINESIS_VIDEO_WEBRTC_LWS_API_CALLS__
6 
7 #pragma once
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 // Timeout values
14 #define SIGNALING_SERVICE_API_CALL_CONNECTION_TIMEOUT (2 * HUNDREDS_OF_NANOS_IN_A_SECOND)
15 #define SIGNALING_SERVICE_API_CALL_COMPLETION_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
16 #define SIGNALING_SERVICE_API_CALL_TIMEOUT_IN_SECONDS \
17  ((SIGNALING_SERVICE_API_CALL_CONNECTION_TIMEOUT + SIGNALING_SERVICE_API_CALL_COMPLETION_TIMEOUT) / HUNDREDS_OF_NANOS_IN_A_SECOND)
18 #define SIGNALING_SERVICE_TCP_KEEPALIVE_IN_SECONDS 3
19 #define SIGNALING_SERVICE_TCP_KEEPALIVE_PROBE_COUNT 3
20 #define SIGNALING_SERVICE_TCP_KEEPALIVE_PROBE_INTERVAL_IN_SECONDS 1
21 #define SIGNALING_SERVICE_WSS_PING_PONG_INTERVAL_IN_SECONDS 10
22 #define SIGNALING_SERVICE_WSS_HANGUP_IN_SECONDS 7200
23 
24 // Protocol indexes
25 #define PROTOCOL_INDEX_HTTPS 0
26 #define PROTOCOL_INDEX_WSS 1
27 
28 // API postfix definitions
29 #define CREATE_SIGNALING_CHANNEL_API_POSTFIX "/createSignalingChannel"
30 #define DESCRIBE_SIGNALING_CHANNEL_API_POSTFIX "/describeSignalingChannel"
31 #define GET_SIGNALING_CHANNEL_ENDPOINT_API_POSTFIX "/getSignalingChannelEndpoint"
32 #define DELETE_SIGNALING_CHANNEL_API_POSTFIX "/deleteSignalingChannel"
33 #define GET_ICE_CONFIG_API_POSTFIX "/v1/get-ice-server-config"
34 #define JOIN_STORAGE_SESSION_API_POSTFIX "/joinStorageSession"
35 #define DESCRIBE_MEDIA_STORAGE_CONF_API_POSTFIX "/describeMediaStorageConfiguration"
36 #define UPDATE_MEDIA_STORAGE_CONF_API_POSTFIX "/updateMediaStorageConfiguration"
37 
38 // Signaling protocol name
39 #define SIGNALING_CHANNEL_PROTOCOL "\"WSS\", \"HTTPS\""
40 #define SIGNALING_CHANNEL_PROTOCOL_W_MEDIA_STORAGE "\"WSS\", \"HTTPS\", \"WEBRTC\""
41 
42 // Parameterized string for Describe Channel API
43 #define DESCRIBE_CHANNEL_PARAM_JSON_TEMPLATE "{\n\t\"ChannelName\": \"%s\"\n}"
44 #define DESCRIBE_MEDIA_STORAGE_CONF_PARAM_JSON_TEMPLATE "{\n\t\"ChannelARN\": \"%s\"\n}"
45 
46 // Parameterized string for Delete Channel API
47 #define DELETE_CHANNEL_PARAM_JSON_TEMPLATE \
48  "{\n\t\"ChannelARN\": \"%s\"," \
49  "\n\t\"CurrentVersion\": \"%s\"\n}"
50 
51 // Parameterized string for Create Channel API
52 #define CREATE_CHANNEL_PARAM_JSON_TEMPLATE \
53  "{\n\t\"ChannelName\": \"%s\"," \
54  "\n\t\"ChannelType\": \"%s\"," \
55  "\n\t\"SingleMasterConfiguration\": {" \
56  "\n\t\t\"MessageTtlSeconds\": %" PRIu64 "\n\t}" \
57  "%s\n}"
58 
59 // Parameterized string for each tag pair as a JSON object
60 #define TAG_PARAM_JSON_OBJ_TEMPLATE "\n\t\t{\"Key\": \"%s\", \"Value\": \"%s\"},"
61 
62 // Parameterized string for TagStream API - we should have at least one tag
63 #define TAGS_PARAM_JSON_TEMPLATE ",\n\t\"Tags\": [%s\n\t]"
64 
65 // Parameterized string for Get Channel Endpoint API
66 #define GET_CHANNEL_ENDPOINT_PARAM_JSON_TEMPLATE \
67  "{\n\t\"ChannelARN\": \"%s\"," \
68  "\n\t\"SingleMasterChannelEndpointConfiguration\": {" \
69  "\n\t\t\"Protocols\": [%s]," \
70  "\n\t\t\"Role\": \"%s\"" \
71  "\n\t}\n}"
72 
73 // Parameterized string for Get Ice Server Config API
74 #define GET_ICE_CONFIG_PARAM_JSON_TEMPLATE \
75  "{\n\t\"ChannelARN\": \"%s\"," \
76  "\n\t\"ClientId\": \"%s\"," \
77  "\n\t\"Service\": \"TURN\"" \
78  "\n}"
79 
80 #define SIGNALING_JOIN_STORAGE_SESSION_MASTER_PARAM_JSON_TEMPLATE "{\n\t\"channelArn\": \"%s\"\n}"
81 #define SIGNALING_JOIN_STORAGE_SESSION_VIEWER_PARAM_JSON_TEMPLATE \
82  "{\n\t\"channelArn\": \"%s\"," \
83  "\n\t\"clientId\": \"%s\"\n}"
84 #define SIGNALING_UPDATE_STORAGE_CONFIG_PARAM_JSON_TEMPLATE \
85  "{\n\t\"StreamARN\": \"%s\"," \
86  "\n\t\"ChannelARN\": \"%s\"," \
87  "\n\t\"StorageStatus\": \"%s\"" \
88  "\n}"
89 
90 // Parameter names for Signaling connect URL
91 #define SIGNALING_ROLE_PARAM_NAME "X-Amz-Role"
92 #define SIGNALING_CHANNEL_NAME_PARAM_NAME "X-Amz-ChannelName"
93 #define SIGNALING_CHANNEL_ARN_PARAM_NAME "X-Amz-ChannelARN"
94 #define SIGNALING_CLIENT_ID_PARAM_NAME "X-Amz-ClientId"
95 
96 // Parameterized string for WSS connect
97 #define SIGNALING_ENDPOINT_MASTER_URL_WSS_TEMPLATE "%s?%s=%s"
98 #define SIGNALING_ENDPOINT_VIEWER_URL_WSS_TEMPLATE "%s?%s=%s&%s=%s"
99 
100 #define SIGNALING_SDP_TYPE_OFFER "SDP_OFFER"
101 #define SIGNALING_SDP_TYPE_ANSWER "SDP_ANSWER"
102 #define SIGNALING_ICE_CANDIDATE "ICE_CANDIDATE"
103 #define SIGNALING_GO_AWAY "GO_AWAY"
104 #define SIGNALING_RECONNECT_ICE_SERVER "RECONNECT_ICE_SERVER"
105 #define SIGNALING_STATUS_RESPONSE "STATUS_RESPONSE"
106 #define SIGNALING_MESSAGE_UNKNOWN "UNKNOWN"
107 
108 // Max length of the signaling message type string length
109 #define MAX_SIGNALING_MESSAGE_TYPE_LEN ARRAY_SIZE(SIGNALING_RECONNECT_ICE_SERVER)
110 
111 // Max value length for the status code
112 #define MAX_SIGNALING_STATUS_MESSAGE_LEN 16
113 
114 // Send message JSON template
115 #define SIGNALING_SEND_MESSAGE_TEMPLATE \
116  "{\n" \
117  "\t\"action\": \"%s\",\n" \
118  "\t\"RecipientClientId\": \"%.*s\",\n" \
119  "\t\"MessagePayload\": \"%s\"%s\n" \
120  "}"
121 
122 // Send message JSON template with correlation id
123 #define SIGNALING_SEND_MESSAGE_TEMPLATE_WITH_CORRELATION_ID \
124  "{\n" \
125  "\t\"action\": \"%s\",\n" \
126  "\t\"RecipientClientId\": \"%.*s\",\n" \
127  "\t\"MessagePayload\": \"%s\",\n" \
128  "\t\"CorrelationId\": \"%.*s\"%s\n" \
129  "}"
130 
131 #define SIGNALING_ICE_SERVER_LIST_TEMPLATE_START \
132  ",\n" \
133  "\t\"IceServerList\": ["
134 
135 #define SIGNALING_ICE_SERVER_LIST_TEMPLATE_END "\n\t]"
136 
137 #define SIGNALING_ICE_SERVER_TEMPLATE \
138  "\n" \
139  "\t\t{\n" \
140  "\t\t\t\"Password\": \"%s\",\n" \
141  "\t\t\t\"Ttl\": %" PRIu64 ",\n" \
142  "\t\t\t\"Uris\": [%s],\n" \
143  "\t\t\t\"Username\": \"%s\"\n" \
144  "\t\t},"
145 
146 // max length for http date header, must follow RFC 7231, should be less than 32 characters
147 #define MAX_DATE_HEADER_BUFFER_LENGTH 64
148 
149 #define MIN_CLOCK_SKEW_TIME_TO_CORRECT (5 * HUNDREDS_OF_NANOS_IN_A_MINUTE)
150 
151 // Defining max bloat size per item in the JSON template
152 #define ICE_SERVER_INFO_TEMPLATE_BLOAT_SIZE 128
153 
154 // Max bloat size for representing a single ICE URI in the JSON
155 #define ICE_SERVER_URI_BLOAT_SIZE 10
156 
157 // Max string length for representing the URIs
158 #define MAX_ICE_SERVER_URI_STR_LEN (MAX_ICE_CONFIG_URI_COUNT * (MAX_ICE_CONFIG_URI_LEN + ICE_SERVER_URI_BLOAT_SIZE))
159 
160 // Max string length for representing an ICE config
161 #define MAX_ICE_SERVER_INFO_STR_LEN \
162  (MAX_ICE_SERVER_URI_STR_LEN + MAX_ICE_CONFIG_USER_NAME_LEN + MAX_ICE_CONFIG_CREDENTIAL_LEN + ICE_SERVER_INFO_TEMPLATE_BLOAT_SIZE)
163 
164 // Max string length for the ice server info which includes the template length * max struct count * content
165 #define MAX_ICE_SERVER_INFOS_STR_LEN (MAX_ICE_CONFIG_COUNT * MAX_ICE_SERVER_INFO_STR_LEN)
166 
167 // Encoded max ice server infos string len
168 #define MAX_ENCODED_ICE_SERVER_INFOS_STR_LEN (MAX_ICE_SERVER_INFOS_STR_LEN + ICE_SERVER_INFO_TEMPLATE_BLOAT_SIZE)
169 
170 // Scratch buffer size
171 #define LWS_SCRATCH_BUFFER_SIZE (MAX_JSON_PARAMETER_STRING_LEN + LWS_PRE)
172 
173 // Send and receive buffer size
174 #define LWS_MESSAGE_BUFFER_SIZE (SIZEOF(CHAR) * (MAX_SIGNALING_MESSAGE_LEN + LWS_PRE))
175 
176 #define AWS_SIG_V4_HEADER_HOST (PCHAR) "host"
177 
178 // Specifies whether to block on the correlation id
179 #define BLOCK_ON_CORRELATION_ID FALSE
180 
181 // Service loop iteration wait time when there is an already servicing thread
182 #define LWS_SERVICE_LOOP_ITERATION_WAIT (50 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND)
183 
184 // Check for the stale credentials
185 #define CHECK_SIGNALING_CREDENTIALS_EXPIRATION(p) \
186  do { \
187  if (SIGNALING_GET_CURRENT_TIME((p)) >= (p)->pAwsCredentials->expiration) { \
188  ATOMIC_STORE(&(p)->result, (SIZE_T) SERVICE_CALL_NOT_AUTHORIZED); \
189  CHK(FALSE, retStatus); \
190  } \
191  } while (FALSE)
192 
198 #define WSS_SIGNALING_PROTOCOL_INDEX 1
199 
200 typedef struct __LwsCallInfo LwsCallInfo;
202  // Size of the data in the buffer
203  volatile SIZE_T sendBufferSize;
204 
205  // Offset from which to send data
206  volatile SIZE_T sendOffset;
207 
208  // Service exit indicator;
209  volatile ATOMIC_BOOL cancelService;
210 
211  // Protocol index
213 
214  // Call info object
215  CallInfo callInfo;
216 
217  // Back reference to the signaling client
219 
220  // Scratch buffer for http processing
222 
223  // Scratch buffer for sending
225 
226  // Scratch buffer for receiving
228 
229  // Size of the data in the receive buffer
231 };
232 
233 typedef struct {
234  // The first member is the public signaling message structure
236 
237  // The messaging client object
240 
241 // Signal handler routine
242 VOID lwsSignalHandler(INT32);
243 
244 // Performs a blocking call
246 
247 // LWS listener handler
248 PVOID lwsListenerHandler(PVOID);
249 
250 // Retry thread
251 PVOID reconnectHandler(PVOID);
252 
253 // LWS callback routine
254 INT32 lwsHttpCallbackRoutine(struct lws*, enum lws_callback_reasons, PVOID, PVOID, size_t);
255 INT32 lwsWssCallbackRoutine(struct lws*, enum lws_callback_reasons, PVOID, PVOID, size_t);
256 
257 BOOL isCallResultSignatureExpired(PCallInfo);
258 BOOL isCallResultSignatureNotYetCurrent(PCallInfo);
259 
260 STATUS describeChannelLws(PSignalingClient, UINT64);
261 STATUS createChannelLws(PSignalingClient, UINT64);
263 STATUS getIceConfigLws(PSignalingClient, UINT64);
267 STATUS deleteChannelLws(PSignalingClient, UINT64);
268 
269 STATUS createLwsCallInfo(PSignalingClient, PRequestInfo, UINT32, PLwsCallInfo*);
271 
272 PVOID receiveLwsMessageWrapper(PVOID);
273 
274 STATUS sendLwsMessage(PSignalingClient, SIGNALING_MESSAGE_TYPE, PCHAR, PCHAR, UINT32, PCHAR, UINT32);
275 STATUS writeLwsData(PSignalingClient, BOOL);
277 STATUS receiveLwsMessage(PSignalingClient, PCHAR, UINT32);
278 STATUS getMessageTypeFromString(PCHAR, UINT32, SIGNALING_MESSAGE_TYPE*);
281 STATUS terminateConnectionWithStatus(PSignalingClient, SERVICE_CALL_RESULT);
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 #endif /* __KINESIS_VIDEO_WEBRTC_LWS_API_CALLS__ */
INT32 lwsHttpCallbackRoutine(struct lws *, enum lws_callback_reasons, PVOID, PVOID, size_t)
Definition: LwsApiCalls.c:15
STATUS getIceConfigLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:1135
PVOID reconnectHandler(PVOID)
Definition: LwsApiCalls.c:1720
STATUS connectSignalingChannelLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:1392
STATUS writeLwsData(PSignalingClient, BOOL)
Definition: LwsApiCalls.c:1905
STATUS describeMediaStorageConfLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:1562
STATUS deleteChannelLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:1268
STATUS terminateConnectionWithStatus(PSignalingClient, SERVICE_CALL_RESULT)
Definition: LwsApiCalls.c:2248
VOID lwsSignalHandler(INT32)
Definition: LwsApiCalls.c:9
STATUS lwsCompleteSync(PLwsCallInfo)
Definition: LwsApiCalls.c:533
STATUS receiveLwsMessage(PSignalingClient, PCHAR, UINT32)
Definition: LwsApiCalls.c:1979
BOOL isCallResultSignatureNotYetCurrent(PCallInfo)
Definition: LwsApiCalls.c:705
PVOID receiveLwsMessageWrapper(PVOID)
Definition: LwsApiCalls.c:2360
STATUS describeChannelLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:745
struct SignalingMessageWrapper * PSignalingMessageWrapper
BOOL isCallResultSignatureExpired(PCallInfo)
Definition: LwsApiCalls.c:700
STATUS createChannelLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:879
STATUS wakeLwsServiceEventLoop(PSignalingClient, UINT32)
Definition: LwsApiCalls.c:2412
STATUS createLwsCallInfo(PSignalingClient, PRequestInfo, UINT32, PLwsCallInfo *)
Definition: LwsApiCalls.c:1338
PCHAR getMessageTypeInString(SIGNALING_MESSAGE_TYPE)
Definition: LwsApiCalls.c:2318
PVOID lwsListenerHandler(PVOID)
Definition: LwsApiCalls.c:1667
STATUS sendLwsMessage(PSignalingClient, SIGNALING_MESSAGE_TYPE, PCHAR, PCHAR, UINT32, PCHAR, UINT32)
Definition: LwsApiCalls.c:1772
STATUS getMessageTypeFromString(PCHAR, UINT32, SIGNALING_MESSAGE_TYPE *)
Definition: LwsApiCalls.c:2281
#define LWS_SCRATCH_BUFFER_SIZE
Definition: LwsApiCalls.h:171
INT32 lwsWssCallbackRoutine(struct lws *, enum lws_callback_reasons, PVOID, PVOID, size_t)
Definition: LwsApiCalls.c:290
STATUS getChannelEndpointLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:986
STATUS joinStorageSessionLws(PSignalingClient, UINT64)
Definition: LwsApiCalls.c:1484
STATUS terminateLwsListenerLoop(PSignalingClient)
Definition: LwsApiCalls.c:2339
#define LWS_MESSAGE_BUFFER_SIZE
Definition: LwsApiCalls.h:174
STATUS freeLwsCallInfo(PLwsCallInfo *)
Definition: LwsApiCalls.c:1368
SIGNALING_MESSAGE_TYPE
Defines different signaling messages.
Definition: Include.h:880
Structure defining the signaling message to be received.
Definition: Include.h:1290
Definition: Signaling.h:225
Definition: LwsApiCalls.h:233
PSignalingClient pSignalingClient
Definition: LwsApiCalls.h:238
ReceivedSignalingMessage receivedSignalingMessage
Definition: LwsApiCalls.h:235
Definition: LwsApiCalls.h:201
UINT32 receiveBufferSize
Definition: LwsApiCalls.h:230
BYTE sendBuffer[(SIZEOF(CHAR) *(MAX_SIGNALING_MESSAGE_LEN+LWS_PRE))]
Definition: LwsApiCalls.h:224
volatile SIZE_T sendOffset
Definition: LwsApiCalls.h:206
volatile ATOMIC_BOOL cancelService
Definition: LwsApiCalls.h:209
UINT32 protocolIndex
Definition: LwsApiCalls.h:212
BYTE receiveBuffer[(SIZEOF(CHAR) *(MAX_SIGNALING_MESSAGE_LEN+LWS_PRE))]
Definition: LwsApiCalls.h:227
CHAR buffer[(MAX_JSON_PARAMETER_STRING_LEN+LWS_PRE)]
Definition: LwsApiCalls.h:221
CallInfo callInfo
Definition: LwsApiCalls.h:215
volatile SIZE_T sendBufferSize
Definition: LwsApiCalls.h:203
PSignalingClient pSignalingClient
Definition: LwsApiCalls.h:218