Skip to content

Commit 2f1a8af

Browse files
Memory Optimization - ICE Agent Stats (#1947) (#2074)
* Ice memory reduction - enable ice stats flag (#1947) * Change params size * Use dyanmic allocation and flag for ice stats * Debug 1 * Revert "Debug 1" This reverts commit ad7d02f. * Revert "Use dyanmic allocation and flag for ice stats" This reverts commit bf9a2ee. * Working version * enable flag in samples * Add unit test * Fix bug * README for the flag * Update readme * Fix readme typos * clang format * Add ENABLE_STATS_CALCULATION_CONTROL CMake option and compiler flag. Update ReadMe. * Add IceStatsControlOn tests * Add CI test forENABLE_STATS_CALCULATION_CONTROL=TRUE * Clang formatting * Remove unused iceAgentAddConfig * Remove unintended readme merge changes "Controlling RTP rolling buffer capacity" * Address comments, add null check * Cleanup unused variable * Rename and negate variable * Move setting of enableIceStats to fix segfault * Revert member size savings changes (to be applied in separate PR) * Move other instance setting of enableIceStats, clang format. * Fix setting of enableIceStats in tests * Address comment --------- Co-authored-by: Divya Sampath Kumar <disa6302@colorado.edu>
1 parent 0d45715 commit 2f1a8af

File tree

10 files changed

+454
-143
lines changed

10 files changed

+454
-143
lines changed

.github/workflows/ci.yml

+30
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,36 @@ jobs:
533533
run: |
534534
cd build
535535
timeout --signal=SIGABRT 60m ./tst/webrtc_client_test
536+
ubuntu-os-build-stats-calc-control:
537+
runs-on: ubuntu-20.04
538+
env:
539+
AWS_KVS_LOG_LEVEL: 2
540+
permissions:
541+
id-token: write
542+
contents: read
543+
steps:
544+
- name: Clone repository
545+
uses: actions/checkout@v4
546+
- name: Configure AWS Credentials
547+
uses: aws-actions/configure-aws-credentials@v4
548+
with:
549+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
550+
aws-region: ${{ secrets.AWS_REGION }}
551+
- name: Install dependencies
552+
run: |
553+
sudo apt clean && sudo apt update
554+
sudo apt-get -y install libcurl4-openssl-dev
555+
- name: Build repository
556+
run: |
557+
# TODO: Remove the following line. This is only a workaround for enabling IPv6, https://github.com/travis-ci/travis-ci/issues/8891.
558+
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
559+
mkdir build && cd build
560+
cmake .. -DBUILD_TEST=TRUE -DENABLE_STATS_CALCULATION_CONTROL=TRUE
561+
make
562+
- name: Run tests
563+
run: |
564+
cd build
565+
timeout --signal=SIGABRT 60m ./tst/webrtc_client_test
536566
windows-msvc-openssl:
537567
runs-on: windows-2022
538568
env:

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ option(ENABLE_DATA_CHANNEL "Enable support for data channel" ON)
2121
option(ENABLE_KVS_THREADPOOL "Enable support for KVS thread pool in signaling" OFF)
2222
option(INSTRUMENTED_ALLOCATORS "Enable memory instrumentation" OFF)
2323
option(ENABLE_AWS_SDK_IN_TESTS "Enable support for compiling AWS SDKs for tests" ON)
24+
option(ENABLE_STATS_CALCULATION_CONTROL "Enable support for runtime control of ice agent stat calculations." OFF)
2425

2526
# Developer Flags
2627
option(BUILD_TEST "Build the testing tree." OFF)
@@ -115,6 +116,10 @@ if (ENABLE_KVS_THREADPOOL)
115116
add_definitions(-DENABLE_KVS_THREADPOOL)
116117
endif()
117118

119+
if (ENABLE_STATS_CALCULATION_CONTROL)
120+
add_definitions(-DENABLE_STATS_CALCULATION_CONTROL)
121+
endif()
122+
118123
if(USE_OPENSSL)
119124
add_definitions(-DKVS_USE_OPENSSL)
120125
elseif(USE_MBEDTLS)

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ You can pass the following options to `cmake ..`.
157157
* `-DLINK_PROFILER` -- Link with gperftools (available profiler options are listed [here](https://github.com/gperftools/gperftools))
158158
* `-DPKG_CONFIG_EXECUTABLE` -- Set pkg config path. This might be required to find gstreamer's pkg config specifically on Windows.
159159
* `-DENABLE_KVS_THREADPOOL` -- Enable the KVS threadpool which is off by default.
160+
* `-DENABLE_STATS_CALCULATION_CONTROL` -- Enable the runtime control of ICE agent stats calculations.
160161

161162
To clean up the `open-source` and `build` folders from previous build, use `cmake --build . --target clean` from the `build` folder
162163

@@ -589,6 +590,20 @@ Let us look into when each of these could be changed:
589590
3. `iceConnectionCheckTimeout`: It is useful to increase this timeout in unstable/slow network where the packet exchange takes time and hence the binding request/response. Essentially, increasing it will allow atleast one candidate pair to be tried for nomination by the other peer.
590591
4. `iceConnectionCheckPollingInterval`: This value is set to a default of 50 ms per [spec](https://datatracker.ietf.org/doc/html/rfc8445#section-14.2). Changing this would change the frequency of connectivity checks and essentially, the ICE state machine transitions. Decreasing the value could help in faster connection establishment in a reliable high performant network setting with good system resources. Increasing the value could help in reducing the network load, however, the connection establishment could slow down. Unless there is a strong reasoning, it is **NOT** recommended to deviate from spec/default.
591592
593+
### Enable ICE agent stats
594+
595+
The SDK calculates 4 different stats:
596+
1. ICE server stats - stats for ICE servers the SDK is using
597+
2. [Local candidate stats](https://www.w3.org/TR/webrtc-stats/#dom-rtcstatstype-local-candidate) - stats for the selected local candidate
598+
3. [Remote candidate stats](https://www.w3.org/TR/webrtc-stats/#dom-rtcstatstype-remote-candidate) - stats for the selected remote candidate
599+
4. [Candidate pair stats](https://www.w3.org/TR/webrtc-stats/#dom-rtcstatstype-candidate-pair) - stats for the selected candidate pair
600+
601+
For more information on these stats, refer to [AWS Docs](https://docs.aws.amazon.com/kinesisvideostreams-webrtc-dg/latest/devguide/kvswebrtc-reference.html)
602+
603+
The SDK enables generating these stats by default. To control whether the SDK calculates these stats, the ENABLE_STATS_CALCULATION_CONTROL CMake option must be set, enabling the use of the following field:
604+
`configuration.kvsRtcConfiguration.enableIceStats = FALSE`.
605+
Disabling these stats may lead to reductions in memory use.
606+
592607
## Documentation
593608
All Public APIs are documented in our [Include.h](https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-c/blob/main/src/include/com/amazonaws/kinesis/video/webrtcclient/Include.h), we also generate a [Doxygen](https://awslabs.github.io/amazon-kinesis-video-streams-webrtc-sdk-c/) each commit for easier navigation.
594609

samples/Common.c

+25-18
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ VOID onConnectionStateChange(UINT64 customData, RTC_PEER_CONNECTION_STATE newSta
5151
CHK_STATUS(peerConnectionGetMetrics(pSampleStreamingSession->pPeerConnection, &pSampleStreamingSession->peerConnectionMetrics));
5252
CHK_STATUS(iceAgentGetMetrics(pSampleStreamingSession->pPeerConnection, &pSampleStreamingSession->iceMetrics));
5353

54-
if (STATUS_FAILED(retStatus = logSelectedIceCandidatesInformation(pSampleStreamingSession))) {
55-
DLOGW("Failed to get information about selected Ice candidates: 0x%08x", retStatus);
54+
if (pSampleConfiguration->enableIceStats) {
55+
CHK_LOG_ERR(logSelectedIceCandidatesInformation(pSampleStreamingSession));
5656
}
5757
break;
5858
case RTC_PEER_CONNECTION_STATE_FAILED:
@@ -114,21 +114,21 @@ STATUS logSelectedIceCandidatesInformation(PSampleStreamingSession pSampleStream
114114
CHK(pSampleStreamingSession != NULL, STATUS_NULL_ARG);
115115
rtcMetrics.requestedTypeOfStats = RTC_STATS_TYPE_LOCAL_CANDIDATE;
116116
CHK_STATUS(rtcPeerConnectionGetMetrics(pSampleStreamingSession->pPeerConnection, NULL, &rtcMetrics));
117-
DLOGD("Local Candidate IP Address: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.address);
118-
DLOGD("Local Candidate type: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.candidateType);
119-
DLOGD("Local Candidate port: %d", rtcMetrics.rtcStatsObject.localIceCandidateStats.port);
120-
DLOGD("Local Candidate priority: %d", rtcMetrics.rtcStatsObject.localIceCandidateStats.priority);
121-
DLOGD("Local Candidate transport protocol: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.protocol);
122-
DLOGD("Local Candidate relay protocol: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.relayProtocol);
123-
DLOGD("Local Candidate Ice server source: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.url);
117+
DLOGI("Local Candidate IP Address: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.address);
118+
DLOGI("Local Candidate type: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.candidateType);
119+
DLOGI("Local Candidate port: %d", rtcMetrics.rtcStatsObject.localIceCandidateStats.port);
120+
DLOGI("Local Candidate priority: %d", rtcMetrics.rtcStatsObject.localIceCandidateStats.priority);
121+
DLOGI("Local Candidate transport protocol: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.protocol);
122+
DLOGI("Local Candidate relay protocol: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.relayProtocol);
123+
DLOGI("Local Candidate Ice server source: %s", rtcMetrics.rtcStatsObject.localIceCandidateStats.url);
124124

125125
rtcMetrics.requestedTypeOfStats = RTC_STATS_TYPE_REMOTE_CANDIDATE;
126126
CHK_STATUS(rtcPeerConnectionGetMetrics(pSampleStreamingSession->pPeerConnection, NULL, &rtcMetrics));
127-
DLOGD("Remote Candidate IP Address: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.address);
128-
DLOGD("Remote Candidate type: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.candidateType);
129-
DLOGD("Remote Candidate port: %d", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.port);
130-
DLOGD("Remote Candidate priority: %d", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.priority);
131-
DLOGD("Remote Candidate transport protocol: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.protocol);
127+
DLOGI("Remote Candidate IP Address: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.address);
128+
DLOGI("Remote Candidate type: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.candidateType);
129+
DLOGI("Remote Candidate port: %d", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.port);
130+
DLOGI("Remote Candidate priority: %d", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.priority);
131+
DLOGI("Remote Candidate transport protocol: %s", rtcMetrics.rtcStatsObject.remoteIceCandidateStats.protocol);
132132
CleanUp:
133133
LEAVES();
134134
return retStatus;
@@ -365,6 +365,10 @@ STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcP
365365
// Set the ICE mode explicitly
366366
configuration.iceTransportPolicy = ICE_TRANSPORT_POLICY_ALL;
367367

368+
#ifdef ENABLE_STATS_CALCULATION_CONTROL
369+
configuration.kvsRtcConfiguration.enableIceStats = pSampleConfiguration->enableIceStats;
370+
#endif
371+
368372
// Set the STUN server
369373
PCHAR pKinesisVideoStunUrlPostFix = KINESIS_VIDEO_STUN_URL_POSTFIX;
370374
// If region is in CN, add CN region uri postfix
@@ -501,6 +505,10 @@ STATUS createSampleStreamingSession(PSampleConfiguration pSampleConfiguration, P
501505
pSampleStreamingSession->twccMetadata.updateLock = MUTEX_CREATE(TRUE);
502506
}
503507

508+
// Flag to enable/disable SDK calculations of selected ice server, local, remote and candidate pair stats.
509+
// Note: enableIceStats only has an effect if compiler flag ENABLE_STATS_CALCULATION_CONTROL is defined.
510+
pSampleConfiguration->enableIceStats = FALSE;
511+
504512
CHK_STATUS(initializePeerConnection(pSampleConfiguration, &pSampleStreamingSession->pPeerConnection));
505513
CHK_STATUS(peerConnectionOnIceCandidate(pSampleStreamingSession->pPeerConnection, (UINT64) pSampleStreamingSession, onIceCandidateHandler));
506514
CHK_STATUS(
@@ -1208,9 +1216,8 @@ STATUS freeSampleConfiguration(PSampleConfiguration* ppSampleConfiguration)
12081216
}
12091217

12101218
for (i = 0; i < pSampleConfiguration->streamingSessionCount; ++i) {
1211-
retStatus = gatherIceServerStats(pSampleConfiguration->sampleStreamingSessionList[i]);
1212-
if (STATUS_FAILED(retStatus)) {
1213-
DLOGW("Failed to ICE Server Stats for streaming session %d: %08x", i, retStatus);
1219+
if (pSampleConfiguration->enableIceStats) {
1220+
CHK_LOG_ERR(gatherIceServerStats(pSampleConfiguration->sampleStreamingSessionList[i]));
12141221
}
12151222
freeSampleStreamingSession(&pSampleConfiguration->sampleStreamingSessionList[i]);
12161223
}
@@ -1542,7 +1549,7 @@ STATUS signalingMessageReceived(UINT64 customData, PReceivedSignalingMessage pRe
15421549
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);
15431550
locked = FALSE;
15441551

1545-
if (startStats &&
1552+
if (pSampleConfiguration->enableIceStats && startStats &&
15461553
STATUS_FAILED(retStatus = timerQueueAddTimer(pSampleConfiguration->timerQueueHandle, SAMPLE_STATS_DURATION, SAMPLE_STATS_DURATION,
15471554
getIceCandidatePairStatsCallback, (UINT64) pSampleConfiguration,
15481555
&pSampleConfiguration->iceCandidatePairStatsTimerId))) {

samples/Samples.h

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ typedef struct {
171171
PCHAR rtspUri;
172172
UINT32 logLevel;
173173
BOOL enableTwcc;
174+
BOOL enableIceStats;
174175
} SampleConfiguration, *PSampleConfiguration;
175176

176177
typedef struct {

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

+4
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,10 @@ typedef struct {
12041204
BOOL disableSenderSideBandwidthEstimation; //!< Disable TWCC feedback based sender bandwidth estimation, enabled by default.
12051205
//!< You want to set this to TRUE if you are on a very stable connection and want to save 1.2MB of
12061206
//!< memory
1207+
#ifdef ENABLE_STATS_CALCULATION_CONTROL
1208+
BOOL enableIceStats; //!< Control whether ICE agent stats are to be calculated. ENABLE_STATS_CALCULATION_CONTROL compiler flag must be defined
1209+
//!< to use this member, else stats are enabled by default.
1210+
#endif
12071211
} KvsRtcConfiguration, *PKvsRtcConfiguration;
12081212

12091213
/**

0 commit comments

Comments
 (0)