Skip to content

Commit 1cf9fff

Browse files
committed
Prefer dynamic allocations over large STATIC arrays
- Use dynamically allocated WSS URL - Use dynamic allocations for singaling payload - Use required only allocations in LwsApiCalls instead large static arrays
1 parent 16c40d4 commit 1cf9fff

File tree

6 files changed

+432
-29
lines changed

6 files changed

+432
-29
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ option(INSTRUMENTED_ALLOCATORS "Enable memory instrumentation" OFF)
2323
option(ENABLE_AWS_SDK_IN_TESTS "Enable support for compiling AWS SDKs for tests" ON)
2424
option(ENABLE_STATS_CALCULATION_CONTROL "Enable support for runtime control of ice agent stat calculations." OFF)
2525
option(BUILD_OLD_MBEDTLS_VERSION "Use MbedTLS version 2.28.8." OFF)
26+
option(PREFER_DYNAMIC_ALLOCS "Prefer dynamic allocations for signalingpayloads and URLs" OFF)
2627

2728
# Developer Flags
2829
option(BUILD_TEST "Build the testing tree." OFF)
@@ -147,6 +148,10 @@ if (ENABLE_STATS_CALCULATION_CONTROL)
147148
add_definitions(-DENABLE_STATS_CALCULATION_CONTROL)
148149
endif()
149150

151+
if (PREFER_DYNAMIC_ALLOCS)
152+
add_definitions(-DPREFER_DYNAMIC_ALLOCS=1)
153+
endif()
154+
150155
if(USE_OPENSSL)
151156
add_definitions(-DKVS_USE_OPENSSL)
152157
elseif(USE_MBEDTLS)

samples/Common.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ STATUS respondWithAnswer(PSampleStreamingSession pSampleStreamingSession)
280280
SignalingMessage message;
281281
UINT32 buffLen = MAX_SIGNALING_MESSAGE_LEN;
282282

283+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
284+
message.payload = (PCHAR) MEMALLOC(MAX_SIGNALING_MESSAGE_LEN + 1);
285+
CHK(message.payload != NULL, STATUS_NOT_ENOUGH_MEMORY);
286+
#endif
283287
CHK_STATUS(serializeSessionDescriptionInit(&pSampleStreamingSession->answerSessionDescriptionInit, message.payload, &buffLen));
284288

285289
message.version = SIGNALING_MESSAGE_CURRENT_VERSION;
@@ -293,6 +297,10 @@ STATUS respondWithAnswer(PSampleStreamingSession pSampleStreamingSession)
293297

294298
CleanUp:
295299

300+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
301+
SAFE_MEMFREE(message.payload);
302+
#endif
303+
296304
CHK_LOG_ERR(retStatus);
297305
return retStatus;
298306
}
@@ -335,13 +343,21 @@ VOID onIceCandidateHandler(UINT64 customData, PCHAR candidateJson)
335343
message.messageType = SIGNALING_MESSAGE_TYPE_ICE_CANDIDATE;
336344
STRNCPY(message.peerClientId, pSampleStreamingSession->peerId, MAX_SIGNALING_CLIENT_ID_LEN);
337345
message.payloadLen = (UINT32) STRNLEN(candidateJson, MAX_SIGNALING_MESSAGE_LEN);
346+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
347+
message.payload = (PCHAR) MEMALLOC(message.payloadLen + 1);
348+
CHK(message.payload != NULL, STATUS_NOT_ENOUGH_MEMORY);
349+
#endif
338350
STRNCPY(message.payload, candidateJson, message.payloadLen);
339351
message.correlationId[0] = '\0';
340352
CHK_STATUS(sendSignalingMessage(pSampleStreamingSession, &message));
341353
}
342354

343355
CleanUp:
344356

357+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
358+
SAFE_MEMFREE(message.payload);
359+
#endif
360+
345361
CHK_LOG_ERR(retStatus);
346362
}
347363

src/include/com/amazonaws/kinesis/video/webrtcclient/Include.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,21 @@ typedef struct {
12791279
//!<
12801280
} RtcIceCandidateInit, *PRtcIceCandidateInit;
12811281

1282+
/**
1283+
* @brief Define this macro to use dynamically allocated payload in SignalingMessage
1284+
* This can be useful for platforms with limited memory as it avoids allocating
1285+
* MAX_SIGNALING_MESSAGE_LEN for each message when only a small payload is needed
1286+
*/
1287+
1288+
/**
1289+
* @brief If PREFER_DYNAMIC_ALLOCS is set to 1, use dynamic allocation for signaling payload
1290+
* Otherwise, use the existing DYNAMIC_SIGNALING_PAYLOAD setting
1291+
*/
1292+
#if PREFER_DYNAMIC_ALLOCS
1293+
#define DYNAMIC_SIGNALING_PAYLOAD 1
1294+
#define USE_DYNAMIC_URL 1
1295+
#endif
1296+
12821297
/**
12831298
* @brief Structure defining the basic signaling message
12841299
*/
@@ -1293,7 +1308,11 @@ typedef struct {
12931308

12941309
UINT32 payloadLen; //!< Optional payload length. If 0, the length will be calculated
12951310

1311+
#ifdef DYNAMIC_SIGNALING_PAYLOAD
1312+
PCHAR payload; //!< Actual signaling message payload - dynamically allocated
1313+
#else
12961314
CHAR payload[MAX_SIGNALING_MESSAGE_LEN + 1]; //!< Actual signaling message payload
1315+
#endif
12971316
} SignalingMessage, *PSignalingMessage;
12981317

12991318
/**

0 commit comments

Comments
 (0)