Skip to content

Commit

Permalink
Add RTP component as submodule after re-factoring (#1975)
Browse files Browse the repository at this point in the history
* Add changes to user RTP librar with RTP serializaer & deserializer

* Add error conversion

* Fix clang formatting

* Update submodule and minor fix

* Fix size_t issue

* Add RTP error code test case

* Update submodule commit

* Remove not a valid value for type 'RtpResult_t
  • Loading branch information
moninom1 authored May 14, 2024
1 parent a28e1e4 commit 773e93c
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 87 deletions.
23 changes: 23 additions & 0 deletions CMake/Dependencies/libkvsrtp-CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.6.3)

project(libkvsrtp NONE)

include(ExternalProject)
if (BUILD_STATIC_LIBS OR WIN32)
set(LIBKVSRTP_SHARED_LIBS OFF)
else()
set(LIBKVSRTP_SHARED_LIBS ON)
endif()

ExternalProject_Add(libkvsrtp
GIT_REPOSITORY https://github.com/awslabs/amazon-kinesis-video-streams-rtp.git
GIT_TAG 2f53e1993d7f94867da0fd2785841a3c921f579b
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/build
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${OPEN_SRC_INSTALL_PREFIX}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DBUILD_SHARED_LIBS=${LIBKVSRTP_SHARED_LIBS}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
BUILD_ALWAYS TRUE
TEST_COMMAND ""
)
3 changes: 2 additions & 1 deletion CMake/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function(build_dependency lib_name)
kvspic
kvsCommonLws
kvssdp
kvsstun)
kvsstun
kvsrtp)
list(FIND supported_libs ${lib_name} index)
if(${index} EQUAL -1)
message(WARNING "${lib_name} is not supported to build from source")
Expand Down
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,8 @@ if(BUILD_DEPENDENCIES)
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS})

build_dependency(kvssdp ${BUILD_ARGS})

set(BUILD_ARGS
-DBUILD_STATIC_LIBS=${BUILD_STATIC_LIBS}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS})

build_dependency(kvsstun ${BUILD_ARGS})
build_dependency(kvsrtp ${BUILD_ARGS})

set(BUILD_ARGS
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
Expand Down Expand Up @@ -416,6 +411,7 @@ target_link_libraries(
kvspicState
kvssdp
kvsstun
kvsrtp
${CMAKE_THREAD_LIBS_INIT}
${OPENSSL_SSL_LIBRARY}
${OPENSSL_CRYPTO_LIBRARY}
Expand All @@ -439,6 +435,7 @@ target_link_libraries(
${LIBWEBSOCKETS_LIBRARIES}
kvssdp
kvsstun
kvsrtp
PRIVATE kvspicUtils
kvspicState
${CMAKE_THREAD_LIBS_INIT}
Expand Down
10 changes: 5 additions & 5 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ add_executable(
kvsWebrtcClientMaster
Common.c
kvsWebRTCClientMaster.c)
target_link_libraries(kvsWebrtcClientMaster kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} kvsCommonLws kvspicUtils websockets kvssdp kvsstun)
target_link_libraries(kvsWebrtcClientMaster kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} kvsCommonLws kvspicUtils websockets kvssdp kvsstun kvsrtp)

add_executable(
kvsWebrtcClientViewer
Common.c
kvsWebRTCClientViewer.c)
target_link_libraries(kvsWebrtcClientViewer kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} kvsCommonLws kvspicUtils websockets kvssdp kvsstun)
target_link_libraries(kvsWebrtcClientViewer kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} kvsCommonLws kvspicUtils websockets kvssdp kvsstun kvsrtp)

add_executable(
discoverNatBehavior
discoverNatBehavior.c)
target_link_libraries(discoverNatBehavior kvsWebrtcClient ${EXTRA_DEPS} kvssdp kvsstun)
target_link_libraries(discoverNatBehavior kvsWebrtcClient ${EXTRA_DEPS} kvssdp kvsstun kvsrtp)

if(GST_FOUND)
add_executable(
Expand All @@ -81,7 +81,7 @@ if(GST_FOUND)
GstAudioVideoReceiver.c
kvsWebRTCClientMasterGstSample.c
)
target_link_libraries(kvsWebrtcClientMasterGstSample kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets kvssdp kvsstun)
target_link_libraries(kvsWebrtcClientMasterGstSample kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets kvssdp kvsstun kvsrtp)

install(TARGETS kvsWebrtcClientMasterGstSample
RUNTIME DESTINATION bin
Expand All @@ -93,7 +93,7 @@ if(GST_FOUND)
GstAudioVideoReceiver.c
kvsWebRTCClientViewerGstSample.c
)
target_link_libraries(kvsWebrtcClientViewerGstSample kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets kvssdp)
target_link_libraries(kvsWebrtcClientViewerGstSample kvsWebrtcClient kvsWebrtcSignalingClient ${EXTRA_DEPS} ${GST_SAMPLE_LIBRARIES} kvsCommonLws kvspicUtils websockets kvssdp kvsrtp)

install(TARGETS kvsWebrtcClientViewerGstSample
RUNTIME DESTINATION bin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ extern "C" {
#define STATUS_RTP_INPUT_MTU_TOO_SMALL STATUS_RTP_BASE + 0x00000002
#define STATUS_RTP_INVALID_NALU STATUS_RTP_BASE + 0x00000003
#define STATUS_RTP_INVALID_EXTENSION_LEN STATUS_RTP_BASE + 0x00000004
#define STATUS_RTP_INVALID_VERSION STATUS_RTP_BASE + 0x00000005
#define STATUS_RTP_UNKNOWN_ERROR STATUS_RTP_BASE + 0x00000006
/*!@} */

/////////////////////////////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion src/source/Include_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#include <com/amazonaws/kinesis/video/webrtcclient/Include.h>
#include "kvssdp/sdp_data_types.h"
#include "kvsstun/stun_data_types.h"
#include "kvsrtp/rtp_data_types.h"

#ifdef KVS_USE_OPENSSL
#include <openssl/bio.h>
Expand Down Expand Up @@ -175,7 +176,8 @@ STATUS generateJSONSafeString(PCHAR, UINT32);
#define KVS_CONVERT_TIMESCALE(pts, from_timescale, to_timescale) (pts * to_timescale / from_timescale)

STATUS convertSdpErrorCode(SdpResult_t sdpResult);
STATUS convertStunErrorCode(StunResult_t sdpResult);
STATUS convertStunErrorCode(StunResult_t stunResult);
STATUS convertRtpErrorCode(RtpResult_t rtpResult);

#ifdef __cplusplus
}
Expand Down
129 changes: 71 additions & 58 deletions src/source/Rtp/RtpPacket.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define LOG_CLASS "RtpPacket"

#include "kvsrtp/rtp_api.h"
#include "../Include_i.h"

STATUS createRtpPacket(UINT8 version, BOOL padding, BOOL extension, UINT8 csrcCount, BOOL marker, UINT8 payloadType, UINT16 sequenceNumber,
Expand Down Expand Up @@ -168,45 +169,59 @@ STATUS setRtpPacketFromBytes(PBYTE rawPacket, UINT32 packetLength, PRtpPacket pR
BOOL marker;
UINT8 payloadType;
UINT16 sequenceNumber;
UINT16 i;
UINT32 timestamp;
UINT32 ssrc;
PUINT32 csrcArray = NULL;
PUINT32 extensionPayloadWord = NULL;
UINT16 extensionProfile = 0;
UINT16 extensionLength = 0;
PBYTE extensionPayload = NULL;
UINT32 currOffset = 0;

CHK(pRtpPacket != NULL && rawPacket != NULL, STATUS_NULL_ARG);
UINT32 word;
UINT16 currentIndex = 0;
RtpResult_t rtpResult;
RtpPacket_t deserializedPkt;
RtpContext_t ctx;
size_t dataLength = packetLength;

CHK(pRtpPacket != NULL, STATUS_NULL_ARG);
CHK(packetLength >= MIN_HEADER_LENGTH, STATUS_RTP_INPUT_PACKET_TOO_SMALL);

version = (rawPacket[0] >> VERSION_SHIFT) & VERSION_MASK;
padding = ((rawPacket[0] >> PADDING_SHIFT) & PADDING_MASK) > 0;
extension = ((rawPacket[0] >> EXTENSION_SHIFT) & EXTENSION_MASK) > 0;
csrcCount = rawPacket[0] & CSRC_COUNT_MASK;
marker = ((rawPacket[1] >> MARKER_SHIFT) & MARKER_MASK) > 0;
payloadType = rawPacket[1] & PAYLOAD_TYPE_MASK;
sequenceNumber = getInt16(*(PUINT16) (rawPacket + SEQ_NUMBER_OFFSET));
timestamp = getInt32(*(PUINT32) (rawPacket + TIMESTAMP_OFFSET));
ssrc = getInt32(*(PUINT32) (rawPacket + SSRC_OFFSET));
rtpResult = Rtp_Init(&(ctx));
CHK(rtpResult == RTP_RESULT_OK, convertRtpErrorCode(rtpResult));

MEMSET(&deserializedPkt, 0, SIZEOF(RtpPacket_t));
rtpResult = Rtp_DeSerialize(&(ctx), rawPacket, dataLength, &(deserializedPkt));
CHK(rtpResult == RTP_RESULT_OK, convertRtpErrorCode(rtpResult));

currOffset = CSRC_OFFSET + (csrcCount * CSRC_LENGTH);
CHK(packetLength >= currOffset, STATUS_RTP_INPUT_PACKET_TOO_SMALL);
version = RTP_HEADER_VERSION;
padding = (deserializedPkt.header.flags & RTP_HEADER_FLAG_PADDING) != 0;
extension = (deserializedPkt.header.flags & RTP_HEADER_FLAG_EXTENSION) != 0;
marker = (deserializedPkt.header.flags & RTP_HEADER_FLAG_MARKER) != 0;
csrcCount = deserializedPkt.header.csrcCount;
payloadType = deserializedPkt.header.payloadType;
sequenceNumber = deserializedPkt.header.sequenceNumber;
timestamp = deserializedPkt.header.timestamp;
ssrc = deserializedPkt.header.ssrc;

if (csrcCount > 0) {
csrcArray = (PUINT32) (rawPacket + CSRC_OFFSET);
csrcArray = deserializedPkt.header.pCsrc;
}

if (extension) {
extensionProfile = getInt16(*(PUINT16) (rawPacket + currOffset));
currOffset += SIZEOF(UINT16);
extensionLength = getInt16(*(PUINT16) (rawPacket + currOffset)) * 4;
currOffset += SIZEOF(UINT16);
extensionPayload = (PBYTE) (rawPacket + currOffset);
currOffset += extensionLength;
extensionProfile = deserializedPkt.header.extension.extensionProfile;
extensionLength = deserializedPkt.header.extension.extensionPayloadLength * 4;
extensionPayloadWord = (deserializedPkt.header.extension.pExtensionPayload);
for (i = 0; i < deserializedPkt.header.extension.extensionPayloadLength; i++) {
word = getInt32(*(PUINT32) (extensionPayloadWord + currentIndex));
extensionPayloadWord[currentIndex] = word;
currentIndex += 4;
}
extensionPayload = (PBYTE) extensionPayloadWord;
}

CHK_STATUS(setRtpPacket(version, padding, extension, csrcCount, marker, payloadType, sequenceNumber, timestamp, ssrc, csrcArray, extensionProfile,
extensionLength, extensionPayload, rawPacket + currOffset, packetLength - currOffset, pRtpPacket));
extensionLength, extensionPayload, deserializedPkt.pPayload, deserializedPkt.payloadLength, pRtpPacket));

CleanUp:
LEAVES();
Expand Down Expand Up @@ -246,9 +261,13 @@ STATUS setBytesFromRtpPacket(PRtpPacket pRtpPacket, PBYTE pRawPacket, UINT32 pac
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PRtpPacketHeader pHeader = &pRtpPacket->header;
UINT32 packetLengthNeeded = 0;
PBYTE pCurPtr = pRawPacket;
UINT8 i;
size_t packetLengthNeeded = 0;
UINT16 i;
RtpResult_t rtpResult;
RtpPacket_t pkt;
RtpContext_t ctx;
UINT32 word;
UINT16 currentIndex = 0;

CHK(pRtpPacket != NULL && pRawPacket != NULL, STATUS_NULL_ARG);

Expand All @@ -269,54 +288,48 @@ STATUS setBytesFromRtpPacket(PRtpPacket pRtpPacket, PBYTE pRawPacket, UINT32 pac
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

// The first byte contains the version, padding bit, extension bit, and csrc size
*pCurPtr = ((pHeader->version << VERSION_SHIFT) | pHeader->csrcCount);
rtpResult = Rtp_Init(&(ctx));
CHK(rtpResult == RTP_RESULT_OK, convertRtpErrorCode(rtpResult));
MEMSET(&pkt, 0, SIZEOF(RtpPacket_t));

if (pHeader->padding) {
*pCurPtr |= (1 << PADDING_SHIFT);
pkt.header.flags |= RTP_HEADER_FLAG_PADDING;
}
if (pHeader->extension) {
*pCurPtr |= (1 << EXTENSION_SHIFT);
pkt.header.flags |= RTP_HEADER_FLAG_EXTENSION;
}
pCurPtr++;

// The second byte contains the marker bit and payload type.
*pCurPtr = pHeader->payloadType;
if (pHeader->marker) {
*pCurPtr |= (1 << MARKER_SHIFT);
pkt.header.flags |= RTP_HEADER_FLAG_MARKER;
}
pCurPtr++;

// https://tools.ietf.org/html/rfc7741#page-5
// All integer fields in the specifications are encoded as
// unsigned integers in network octet order.
putUnalignedInt16BigEndian(pCurPtr, pHeader->sequenceNumber);
pCurPtr += SIZEOF(UINT16);

putUnalignedInt32BigEndian(pCurPtr, pHeader->timestamp);
pCurPtr += SIZEOF(UINT32);

putUnalignedInt32BigEndian(pCurPtr, pHeader->ssrc);
pCurPtr += SIZEOF(UINT32);

for (i = 0; i < pHeader->csrcCount; i++, pCurPtr += SIZEOF(UINT32)) {
putUnalignedInt32BigEndian(pCurPtr, pHeader->csrcArray[i]);
}
pkt.header.csrcCount = pHeader->csrcCount;
pkt.header.payloadType = pHeader->payloadType;
pkt.header.sequenceNumber = pHeader->sequenceNumber;
pkt.header.timestamp = pHeader->timestamp;
pkt.header.ssrc = pHeader->ssrc;
pkt.header.pCsrc = pHeader->csrcArray;

if (pHeader->extension) {
// the payload must be in 32-bit words.
CHK((pHeader->extensionLength) % SIZEOF(UINT32) == 0, STATUS_RTP_INVALID_EXTENSION_LEN);

putUnalignedInt16BigEndian(pCurPtr, pHeader->extensionProfile);
pCurPtr += SIZEOF(UINT16);
putUnalignedInt16BigEndian(pCurPtr, pHeader->extensionLength / SIZEOF(UINT32));
pCurPtr += SIZEOF(UINT16);
MEMCPY(pCurPtr, pHeader->extensionPayload, pHeader->extensionLength);
pCurPtr += pHeader->extensionLength;
pkt.header.extension.extensionProfile = pHeader->extensionProfile;
pkt.header.extension.extensionPayloadLength = pHeader->extensionLength / SIZEOF(UINT32);
for (i = 0; i < pkt.header.extension.extensionPayloadLength; i++) {
word = getInt32(*(PUINT32) (pHeader->extensionPayload + currentIndex));
MEMCPY((pHeader->extensionPayload + currentIndex), &word, SIZEOF(UINT32));
currentIndex += 4;
}
pkt.header.extension.pExtensionPayload = (PUINT32) pHeader->extensionPayload;
}

if (pRtpPacket->payload != NULL && pRtpPacket->payloadLength > 0) {
MEMCPY(pCurPtr, pRtpPacket->payload, pRtpPacket->payloadLength);
pkt.pPayload = pRtpPacket->payload;
pkt.payloadLength = pRtpPacket->payloadLength;
}

rtpResult = Rtp_Serialize(&(ctx), &(pkt), pRawPacket, &packetLengthNeeded);
CHK(rtpResult == RTP_RESULT_OK, convertRtpErrorCode(rtpResult));

CleanUp:
LEAVES();
return retStatus;
Expand Down
22 changes: 6 additions & 16 deletions src/source/Rtp/RtpPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@ RTP Packet include file
extern "C" {
#endif

#define MIN_HEADER_LENGTH 12
#define VERSION_SHIFT 6
#define VERSION_MASK 0x3
#define PADDING_SHIFT 5
#define PADDING_MASK 0x1
#define EXTENSION_SHIFT 4
#define EXTENSION_MASK 0x1
#define CSRC_COUNT_MASK 0xF
#define MARKER_SHIFT 7
#define MARKER_MASK 0x1
#define PAYLOAD_TYPE_MASK 0x7F
#define SEQ_NUMBER_OFFSET 2
#define TIMESTAMP_OFFSET 4
#define SSRC_OFFSET 8
#define CSRC_OFFSET 12
#define CSRC_LENGTH 4
#define MIN_HEADER_LENGTH 12
#define VERSION_SHIFT 6
#define VERSION_MASK 0x3
#define SSRC_OFFSET 8
#define CSRC_LENGTH 4
#define RTP_HEADER_VERSION 2

#define RTP_HEADER_LEN(pRtpPacket) \
(12 + (pRtpPacket)->header.csrcCount * CSRC_LENGTH + ((pRtpPacket)->header.extension ? 4 + (pRtpPacket)->header.extensionLength : 0))
Expand Down
31 changes: 31 additions & 0 deletions src/source/Rtp/RtpUtils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#define LOG_CLASS "RTPUtils"

#include "../Include_i.h"

STATUS convertRtpErrorCode(RtpResult_t rtpResult)
{
STATUS retStatus;

switch (rtpResult) {
case RTP_RESULT_OK:
retStatus = STATUS_SUCCESS;
break;
case RTP_RESULT_BAD_PARAM:
retStatus = STATUS_INVALID_ARG;
break;
case RTP_RESULT_OUT_OF_MEMORY:
retStatus = STATUS_NOT_ENOUGH_MEMORY;
break;
case RTP_RESULT_WRONG_VERSION:
retStatus = STATUS_RTP_INVALID_VERSION;
break;
case RTP_RESULT_MALFORMED_PACKET:
retStatus = STATUS_RTP_INPUT_PACKET_TOO_SMALL;
break;
default:
retStatus = STATUS_RTP_UNKNOWN_ERROR;
break;
}

return retStatus;
}
Loading

0 comments on commit 773e93c

Please sign in to comment.