Skip to content

Commit

Permalink
Reduce hash table size for SDP transceivers (#1914)
Browse files Browse the repository at this point in the history
* Modify hash table sizes

* Add logs

* Fix hash table size to be set according to number of m-lines and unique supported codecs

* CI branch

* Update actions to use node20

* Unit test for fake transceiver

* Cleanup

* Fix build failure

* Fix unit test

* Initialize to 0
  • Loading branch information
disa6302 authored Feb 8, 2024
1 parent 7eab30c commit 2821172
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 46 deletions.
70 changes: 35 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: macos-latest
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install clang-format
run: |
brew install clang-format
Expand All @@ -33,9 +33,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand All @@ -59,9 +59,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand All @@ -83,9 +83,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand All @@ -108,9 +108,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand All @@ -136,9 +136,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -171,9 +171,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -202,7 +202,7 @@ jobs:
# AWS_KVS_LOG_LEVEL: 2
# steps:
# - name: Clone repository
# uses: actions/checkout@v3
# uses: actions/checkout@v4
# - name: Install dependencies
# run: |
# sudo apt clean && sudo apt update
Expand All @@ -227,9 +227,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -259,9 +259,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -300,7 +300,7 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install dependencies
run: |
apk update
Expand All @@ -320,9 +320,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -356,9 +356,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -392,9 +392,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -425,9 +425,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -508,9 +508,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -538,9 +538,9 @@ jobs:
contents: read
steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}
Expand Down Expand Up @@ -587,7 +587,7 @@ jobs:
# AWS_KVS_LOG_LEVEL: 7
# steps:
# - name: Clone repository
# uses: actions/checkout@v3
# uses: actions/checkout@v4
# - name: Move cloned repo
# shell: powershell
# run: |
Expand Down Expand Up @@ -620,7 +620,7 @@ jobs:
sudo apt clean && sudo apt update
sudo apt-get -y install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build Repository
run: |
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
Expand All @@ -638,7 +638,7 @@ jobs:
sudo apt clean && sudo apt update
sudo apt-get -y install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build Repository
run: |
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
Expand All @@ -656,7 +656,7 @@ jobs:
sudo apt clean && sudo apt update
sudo apt-get -y install gcc-arm-linux-gnueabi g++-arm-linux-gnueabi binutils-arm-linux-gnueabi
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build Repository
run: |
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ typedef enum {
RTC_CODEC_MULAW = 4, //!< MULAW audio codec
RTC_CODEC_ALAW = 5, //!< ALAW audio codec
RTC_CODEC_UNKNOWN = 6,
// RTC_CODEC_MAX **MUST** be the last enum in the list **ALWAYS** and not assigned a value
RTC_CODEC_MAX //!< Placeholder for max number of supported codecs
} RTC_CODEC;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/source/Ice/IceAgent.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ STATUS createIceAgent(PCHAR username, PCHAR password, PIceAgentCallbacks pIceAge

// Pre-allocate stun packets

// no other attribtues needed: https://tools.ietf.org/html/rfc8445#section-11
// no other attributes needed: https://tools.ietf.org/html/rfc8445#section-11
CHK_STATUS(createStunPacket(STUN_PACKET_TYPE_BINDING_INDICATION, NULL, &pIceAgent->pBindingIndication));
CHK_STATUS(hashTableCreateWithParams(ICE_HASH_TABLE_BUCKET_COUNT, ICE_HASH_TABLE_BUCKET_LENGTH, &pIceAgent->requestTimestampDiagnostics));

Expand Down
20 changes: 12 additions & 8 deletions src/source/PeerConnection/SessionDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ STATUS populateSessionDescriptionMedia(PKvsPeerConnection pKvsPeerConnection, PS
PCHAR pDtlsRole = NULL;
PHashTable pUnknownCodecPayloadTypesTable = NULL, pUnknownCodecRtpmapTable = NULL;
UINT32 unknownCodecHashTableKey = 0;
UINT32 unknownHashTableBucketCount = 0;

CHK_STATUS(dtlsSessionGetLocalCertificateFingerprint(pKvsPeerConnection->pDtlsSession, certificateFingerprint, CERTIFICATE_FINGERPRINT_LENGTH));

Expand All @@ -910,8 +911,12 @@ STATUS populateSessionDescriptionMedia(PKvsPeerConnection pKvsPeerConnection, PS
}
} else {
pDtlsRole = DTLS_ROLE_ACTIVE;
CHK_STATUS(hashTableCreate(&pUnknownCodecPayloadTypesTable));
CHK_STATUS(hashTableCreate(&pUnknownCodecRtpmapTable));
unknownHashTableBucketCount =
pRemoteSessionDescription->mediaCount < MIN_HASH_BUCKET_COUNT ? MIN_HASH_BUCKET_COUNT : pRemoteSessionDescription->mediaCount;
CHK_STATUS(hashTableCreateWithParams(unknownHashTableBucketCount, CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH,
&pUnknownCodecPayloadTypesTable));
CHK_STATUS(
hashTableCreateWithParams(unknownHashTableBucketCount, CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH, &pUnknownCodecRtpmapTable));

// this function creates a list of transceivers corresponding to each m-line and adds it answerTransceivers
// if an m-line does not have a corresponding transceiver created by the user, we create a fake transceiver
Expand Down Expand Up @@ -1112,18 +1117,21 @@ STATUS findTransceiversByRemoteDescription(PKvsPeerConnection pKvsPeerConnection
PRtcMediaStreamTrack pRtcMediaStreamTrack;
RtcMediaStreamTrack track;

CHK_STATUS(hashTableCreate(&pSeenTransceivers)); // to be used by findCodecInTransceivers
// pSeenTranceivers is populated only with codec types supported by the SDK. And if already populated, it is not added again.
// Hence, it is sufficient if the hash table count is set to minimum or required transceivers
UINT32 seenTranceiversHashBucketCnt = RTC_CODEC_MAX < MIN_HASH_BUCKET_COUNT ? MIN_HASH_BUCKET_COUNT : RTC_CODEC_MAX;
CHK_STATUS(hashTableCreateWithParams(seenTranceiversHashBucketCnt, CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH, &pSeenTransceivers));

// sample m-lines
// m=audio 9 UDP/TLS/RTP/SAVPF 111 63 103 104 9 0 8 106 105 13 110 112 113 126
// m=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 102 121 127 120 125 107 108 109 124 119 123 117 35 36 114 115 116 62 118
// this loop iterates over all the m-lines

for (currentMedia = 0; currentMedia < pRemoteSessionDescription->mediaCount; currentMedia++) {
pMediaDescription = &(pRemoteSessionDescription->mediaDescriptions[currentMedia]);
foundMediaSectionWithCodec = FALSE;
count = 0;
MEMSET(firstCodec, 0x00, MAX_PAYLOAD_TYPE_LENGTH);

// Scan the media section name for any codecs we support
// sample attributeValue=audio 9 UDP/TLS/RTP/SAVPF 111 63 103 104 9 0 8 106 105 13 110 112 113 126
attributeValue = pMediaDescription->mediaName;
Expand Down Expand Up @@ -1172,7 +1180,6 @@ STATUS findTransceiversByRemoteDescription(PKvsPeerConnection pKvsPeerConnection
attributeValue = end + 1;
}
} while (end != NULL && !foundMediaSectionWithCodec);

// get the first payload type from codecs in case we need to use it to generate a fake transceiver to respond to an m-line
// if we don't have a user-created one corresponding to an m-line
// we can respond to an m-line by including any one codec the offer had
Expand All @@ -1189,7 +1196,6 @@ STATUS findTransceiversByRemoteDescription(PKvsPeerConnection pKvsPeerConnection
for (currentAttribute = 0; currentAttribute < pMediaDescription->mediaAttributesCount && !foundMediaSectionWithCodec; currentAttribute++) {
attributeValue = pMediaDescription->sdpAttributes[currentAttribute].attributeValue;
rtcCodec = RTC_CODEC_UNKNOWN;

// check for supported codec in rtpmap values only if an a-line contains the keyword "rtpmap" to save string comparisons
if (STRNCMP(RTPMAP_VALUE, pMediaDescription->sdpAttributes[currentAttribute].attributeName, 6) == 0) {
if (STRSTR(attributeValue, H264_VALUE) != NULL) {
Expand Down Expand Up @@ -1249,7 +1255,6 @@ STATUS findTransceiversByRemoteDescription(PKvsPeerConnection pKvsPeerConnection
CHK_STATUS(doubleListInsertItemTail(pKvsPeerConnection->pAnswerTransceivers, (UINT64) pKvsRtpFakeTransceiver));

CHK_STATUS(STRTOUI32(firstCodec, firstCodec + tokenLen, 10, &codec));

// Insert (int)(firstCodec) and rtpMapValue into the hashtables with the same key so they can be retrieved later during serialization
CHK_STATUS(hashTableContains(pUnknownCodecPayloadTypesTable, (UINT64) codec, &containsPayloadType));
CHK_STATUS(hashTableContains(pUnknownCodecRtpmapTable, (UINT64) rtpMapValue, &containsRtpMap));
Expand All @@ -1264,7 +1269,6 @@ STATUS findTransceiversByRemoteDescription(PKvsPeerConnection pKvsPeerConnection
}

CleanUp:

CHK_STATUS(hashTableFree(pSeenTransceivers));
CHK_LOG_ERR(retStatus);

Expand Down
2 changes: 2 additions & 0 deletions src/source/PeerConnection/SessionDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ extern "C" {
#define TWCC_SDP_ATTR "transport-cc"
#define TWCC_EXT_URL "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"

#define CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH 2

STATUS setPayloadTypesFromOffer(PHashTable, PHashTable, PSessionDescription);
STATUS setPayloadTypesForOffer(PHashTable);

Expand Down
2 changes: 1 addition & 1 deletion src/source/Sdp/Deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ STATUS parseMediaName(PSessionDescription pSessionDescription, PCHAR pch, UINT32
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
CHK(pSessionDescription->mediaCount < MAX_SDP_SESSION_MEDIA_COUNT, STATUS_BUFFER_TOO_SMALL);
CHK(pSessionDescription->mediaCount < MAX_SDP_SESSION_MEDIA_COUNT, STATUS_SESSION_DESCRIPTION_MAX_MEDIA_COUNT);

STRNCPY(pSessionDescription->mediaDescriptions[pSessionDescription->mediaCount].mediaName, (pch + SDP_ATTRIBUTE_LENGTH),
MIN(MAX_SDP_MEDIA_NAME_LENGTH, lineLen - SDP_ATTRIBUTE_LENGTH));
Expand Down
Loading

0 comments on commit 2821172

Please sign in to comment.