Skip to content

Commit

Permalink
Aligned memory access (#68)
Browse files Browse the repository at this point in the history
* Aligned memory access

* Multiple alignment fixes. Main badness comes from non-64 bit alignment and rounded ICE-Controlled attribute which makes the code look really ugly

* Pass same call info custom data into Lws callbacks and verify protocol type

* Use aligned_mem branch from KVS C/C++ producer SDK

* Forward typedef is not allowed by some C compiler

* Fix failed unit test
  • Loading branch information
MushMal authored and zhiyua-git committed Dec 17, 2019
1 parent 86bd653 commit 3fe9465
Show file tree
Hide file tree
Showing 39 changed files with 145 additions and 293 deletions.
1 change: 1 addition & 0 deletions .gitmodules
@@ -1,3 +1,4 @@
[submodule "open-source/amazon-kinesis-video-streams-producer-sdk-cpp"]
path = open-source/amazon-kinesis-video-streams-producer-sdk-cpp
url = https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp.git
branch = aligned_mem
6 changes: 0 additions & 6 deletions samples/Samples.h
Expand Up @@ -10,16 +10,12 @@ Shared include file for the samples
extern "C" {
#endif

// For tight packing
#pragma pack(push, samples_i, 1) // for byte alignment

#include <com/amazonaws/kinesis/video/webrtcclient/Include.h>

#define NUMBER_OF_H264_FRAME_FILES 403
#define NUMBER_OF_OPUS_FRAME_FILES 618
#define DEFAULT_FPS_VALUE 25
#define DEFAULT_MAX_CONCURRENT_STREAMING_SESSION 10
#define DEFAULT_TERMINATION_POLLING_INTERVAL 5 * HUNDREDS_OF_NANOS_IN_A_SECOND

#define SAMPLE_MASTER_CLIENT_ID "ProducerMaster"
#define SAMPLE_VIEWER_CLIENT_ID "ConsumerViewer"
Expand Down Expand Up @@ -112,8 +108,6 @@ VOID sampleFrameHandler(UINT64, PFrame);
VOID onDataChannel(UINT64, PRtcDataChannel);
VOID onConnectionStateChange(UINT64, RTC_PEER_CONNECTION_STATE);

#pragma pack(pop, samples_i)

#ifdef __cplusplus
}
#endif
Expand Down
Expand Up @@ -19,9 +19,6 @@ extern "C" {
#include <com/amazonaws/kinesis/video/common/Include.h>
#pragma clang diagnostic pop

// For tight packing
#pragma pack(push, include, 1) // for byte alignment

////////////////////////////////////////////////////
// Status return codes
////////////////////////////////////////////////////
Expand Down Expand Up @@ -1320,8 +1317,6 @@ PUBLIC_API STATUS signalingClientGetIceConfigInfo(SIGNALING_CLIENT_HANDLE, UINT3
*/
PUBLIC_API STATUS signalingClientConnectSync(SIGNALING_CLIENT_HANDLE);

#pragma pack(pop, include)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Dtls/Dtls.h
Expand Up @@ -11,9 +11,6 @@
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define MAX_SRTP_MASTER_KEY_LEN 16
#define MAX_SRTP_SALT_KEY_LEN 14

Expand Down Expand Up @@ -88,8 +85,6 @@ STATUS dtlsSessionVerifyRemoteCertificateFingerprint(PDtlsSession, PCHAR);
STATUS dtlsSessionPutApplicationData(PDtlsSession, PBYTE, INT32);
STATUS dtlsCheckOutgoingDataBuffer(PDtlsSession);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/ConnectionListener.h
Expand Up @@ -10,9 +10,6 @@ Connection Listener internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define MAX_UDP_PACKET_SIZE 65507
#define SOCKET_WAIT_FOR_DATA_TIMEOUT_SECONDS 2

Expand Down Expand Up @@ -82,8 +79,6 @@ STATUS connectionListenerStart(PConnectionListener);
////////////////////////////////////////////
PVOID connectionListenerReceiveDataRoutine(PVOID arg);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/IceAgent.h
Expand Up @@ -10,9 +10,6 @@ IceAgent internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define KVS_ICE_MAX_CANDIDATE_PAIR_COUNT 1024
#define KVS_ICE_MAX_REMOTE_CANDIDATE_COUNT 100
#define KVS_ICE_MAX_LOCAL_CANDIDATE_COUNT 100
Expand Down Expand Up @@ -306,8 +303,6 @@ UINT32 computeCandidatePriority(PIceCandidate);
UINT64 computeCandidatePairPriority(PIceCandidatePair, BOOL);
PCHAR iceAgentGetCandidateTypeStr(ICE_CANDIDATE_TYPE);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/IceAgentStateMachine.h
Expand Up @@ -10,9 +10,6 @@ Signaling State Machine internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

/**
* Ice states definitions
*
Expand Down Expand Up @@ -72,8 +69,6 @@ STATUS executeDisconnectedIceAgentState(UINT64, UINT64);
STATUS fromFailedIceAgentState(UINT64, PUINT64);
STATUS executeFailedIceAgentState(UINT64, UINT64);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/IceUtils.h
Expand Up @@ -10,9 +10,6 @@ Ice Utils internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define DEFAULT_MAX_STORED_TRANSACTION_ID_COUNT 20
#define MAX_STORED_TRANSACTION_ID_COUNT 100

Expand Down Expand Up @@ -41,8 +38,6 @@ STATUS iceUtilsSendStunPacket(PStunPacket, PBYTE, UINT32, PKvsIpAddress, PSocket

STATUS populateIpFromString(PKvsIpAddress, PCHAR);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/Network.h
Expand Up @@ -10,9 +10,6 @@ HostInfo internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define MAX_LOCAL_NETWORK_INTERFACE_COUNT 128

// string buffer size for ipv4 and ipv6. Null terminator included.
Expand Down Expand Up @@ -63,8 +60,6 @@ STATUS getIpAddrStr(PKvsIpAddress, PCHAR, UINT32);

BOOL isSameIpAddress(PKvsIpAddress, PKvsIpAddress, BOOL);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Ice/SocketConnection.h
Expand Up @@ -10,9 +10,6 @@ Socket Connection internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

typedef STATUS (*ConnectionDataAvailableFunc)(UINT64, struct __SocketConnection*, PBYTE, UINT32, PKvsIpAddress, PKvsIpAddress);

typedef struct __SocketConnection SocketConnection;
Expand Down Expand Up @@ -116,8 +113,6 @@ STATUS socketConnectionReadData(PSocketConnection, PBYTE, UINT32, PUINT32);
STATUS createConnectionCertificateAndKey(X509 **, EVP_PKEY **);
INT32 certificateVerifyCallback(INT32 preverify_ok, X509_STORE_CTX *ctx);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
33 changes: 14 additions & 19 deletions src/source/Ice/TurnConnection.h
Expand Up @@ -10,36 +10,33 @@ TurnConnection internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

// https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
#define TURN_REQUEST_TRANSPORT_UDP 17
#define TURN_REQUEST_TRANSPORT_TCP 6
#define DEFAULT_TURN_ALLOCATION_LIFETIME_SECONDS 600
// required by rfc5766 to be 300s
#define TURN_PERMISSION_LIFETIME 300 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_TIMER_INTERVAL_BEFORE_READY 100 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND
#define DEFAULT_TURN_TIMER_INTERVAL_AFTER_READY 1 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_SEND_REFRESH_INVERVAL 1 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define TURN_PERMISSION_LIFETIME (300 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_TIMER_INTERVAL_BEFORE_READY (100 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND)
#define DEFAULT_TURN_TIMER_INTERVAL_AFTER_READY (1 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_SEND_REFRESH_INVERVAL (1 * HUNDREDS_OF_NANOS_IN_A_SECOND)

// turn state timeouts
#define DEFAULT_TURN_GET_CREDENTIAL_TIMEOUT 5 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_ALLOCATION_TIMEOUT 5 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_CREATE_PERMISSION_TIMEOUT 2 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_BIND_CHANNEL_TIMEOUT 3 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_CLEAN_UP_TIMEOUT 10 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_GET_CREDENTIAL_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_ALLOCATION_TIMEOUT (5 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_CREATE_PERMISSION_TIMEOUT (2 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_BIND_CHANNEL_TIMEOUT (3 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_CLEAN_UP_TIMEOUT (10 * HUNDREDS_OF_NANOS_IN_A_SECOND)

/*
* if no application data is sent through turn for this much time then we assume that a better connection is found
* and initiate turn clean up.
*/
#define DEFAULT_TURN_START_CLEAN_UP_TIMEOUT 10 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_ALLOCATION_REFRESH_GRACE_PERIOD 30 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_PERMISSION_REFRESH_GRACE_PERIOD 30 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define DEFAULT_TURN_START_CLEAN_UP_TIMEOUT (10 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_ALLOCATION_REFRESH_GRACE_PERIOD (30 * HUNDREDS_OF_NANOS_IN_A_SECOND)
#define DEFAULT_TURN_PERMISSION_REFRESH_GRACE_PERIOD (30 * HUNDREDS_OF_NANOS_IN_A_SECOND)

#define DEFAULT_TURN_MESSAGE_SEND_CHANNEL_DATA_BUFFER_LEN 10 * 1024
#define DEFAULT_TURN_MESSAGE_RECV_CHANNEL_DATA_BUFFER_LEN 10 * 1024
#define DEFAULT_TURN_MESSAGE_SEND_CHANNEL_DATA_BUFFER_LEN (10 * 1024)
#define DEFAULT_TURN_MESSAGE_RECV_CHANNEL_DATA_BUFFER_LEN (10 * 1024)

// all turn channel numbers must be greater than 0x4000 and less than 0x7FFF
#define TURN_CHANNEL_BIND_CHANNEL_NUMBER_BASE (UINT16) 0x4000
Expand Down Expand Up @@ -185,8 +182,6 @@ STATUS turnConnectionHandleStun(PTurnConnection, PSocketConnection, PBYTE, UINT3
STATUS turnConnectionHandleStunError(PTurnConnection, PSocketConnection, PBYTE, UINT32);
STATUS turnConnectionHandleChannelDataTcpMode(PTurnConnection, PSocketConnection, PBYTE, UINT32);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/Include_i.h
Expand Up @@ -50,9 +50,6 @@ struct __SocketConnection;
#include <arpa/inet.h>
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

// Max uFrag and uPwd length as documented in https://tools.ietf.org/html/rfc5245#section-15.4
#define ICE_MAX_UFRAG_LEN 256
#define ICE_MAX_UPWD_LEN 256
Expand Down Expand Up @@ -134,8 +131,6 @@ typedef struct {
// Project internal functions
////////////////////////////////////////////////////

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 0 additions & 6 deletions src/source/PeerConnection/DataChannel.h
Expand Up @@ -10,9 +10,6 @@ DataChannel internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

typedef struct {
RtcDataChannel dataChannel;

Expand All @@ -23,9 +20,6 @@ typedef struct {
RtcOnMessage onMessage;
} KvsDataChannel, *PKvsDataChannel;


#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions src/source/PeerConnection/JitterBuffer.h
Expand Up @@ -10,9 +10,6 @@ PeerConnection internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

typedef STATUS (*FrameReadyFunc)(UINT64, UINT16, UINT16, UINT32);
typedef STATUS (*FrameDroppedFunc)(UINT64, UINT32);
#define UINT16_DEC(a) ((UINT16) ((a) - 1))
Expand All @@ -24,8 +21,8 @@ typedef struct {
DepayRtpPayloadFunc depayPayloadFn;

UINT32 lastPushTimestamp;
UINT16 lastRemovedSequenceNumber;
UINT32 lastRemovedTimestamp;
UINT16 lastRemovedSequenceNumber;
UINT16 lastPopSequenceNumber;
UINT32 lastPopTimestamp;
UINT64 maxLatency;
Expand All @@ -41,8 +38,6 @@ STATUS jitterBufferPop(PJitterBuffer, BOOL);
STATUS jitterBufferDropBufferData(PJitterBuffer, UINT16, UINT16, UINT32);
STATUS jitterBufferFillFrameData(PJitterBuffer, PBYTE, UINT32, PUINT32, UINT16, UINT16);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 2 additions & 7 deletions src/source/PeerConnection/PeerConnection.h
Expand Up @@ -10,9 +10,6 @@ PeerConnection internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

#define LOCAL_ICE_UFRAG_LEN 4
#define LOCAL_ICE_PWD_LEN 24
#define LOCAL_CNAME_LEN 16
Expand All @@ -30,8 +27,8 @@ extern "C" {

#define CODEC_HASH_TABLE_BUCKET_COUNT 50
#define CODEC_HASH_TABLE_BUCKET_LENGTH 2
#define RTX_HASH_TABLE_BUCKET_COUNT 50
#define RTX_HASH_TABLE_BUCKET_LENGTH 2
#define RTX_HASH_TABLE_BUCKET_COUNT 50
#define RTX_HASH_TABLE_BUCKET_LENGTH 2

#define DATA_CHANNEL_HASH_TABLE_BUCKET_COUNT 200
#define DATA_CHANNEL_HASH_TABLE_BUCKET_LENGTH 2
Expand Down Expand Up @@ -103,8 +100,6 @@ STATUS sendPacketToRtpReceiver(PKvsPeerConnection, PBYTE, UINT32);

STATUS generateJSONSafeString(PCHAR, UINT32);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions src/source/PeerConnection/Retransmitter.h
Expand Up @@ -10,22 +10,17 @@ Retransmitter internal include file
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

typedef struct {
PUINT16 sequenceNumberList;
UINT32 seqNumListLen;
PUINT64 validIndexList;
UINT32 validIndexListLen;
PUINT64 validIndexList;
} Retransmitter, *PRetransmitter;

STATUS createRetransmitter(UINT32, UINT32, PRetransmitter*);
STATUS freeRetransmitter(PRetransmitter*);
STATUS resendPacketOnNack(PRtcpPacket, PKvsPeerConnection);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions src/source/PeerConnection/Rtcp.h
Expand Up @@ -7,13 +7,8 @@
extern "C" {
#endif

// For tight packing
#pragma pack(push, include_i, 1) // for byte alignment

STATUS onRtcpPacket(PKvsPeerConnection, PBYTE, INT32);

#pragma pack(pop, include_i)

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 3fe9465

Please sign in to comment.