Amazon Kinesis Webrtc C SDK
Loading...
Searching...
No Matches
PeerConnection.h
Go to the documentation of this file.
1/*******************************************
2PeerConnection internal include file
3*******************************************/
4#ifndef __KINESIS_VIDEO_WEBRTC_CLIENT_PEERCONNECTION_PEERCONNECTION__
5#define __KINESIS_VIDEO_WEBRTC_CLIENT_PEERCONNECTION_PEERCONNECTION__
6
7#pragma once
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#define LOCAL_ICE_UFRAG_LEN 4
14#define LOCAL_ICE_PWD_LEN 24
15#define LOCAL_CNAME_LEN 16
16
17// https://tools.ietf.org/html/rfc5245#section-15.4
18#define MAX_ICE_UFRAG_LEN 256
19#define MAX_ICE_PWD_LEN 256
20
21#define PEER_FRAME_BUFFER_SIZE_INCREMENT_FACTOR 1.5
22
23// A non-comprehensive list of valid JSON characters
24#define VALID_CHAR_SET_FOR_JSON "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
25
26#define ICE_CANDIDATE_JSON_TEMPLATE (PCHAR) "{\"candidate\":\"candidate:%s\",\"sdpMid\":\"0\",\"sdpMLineIndex\":0}"
27
28#define MAX_ICE_CANDIDATE_JSON_LEN (MAX_SDP_ATTRIBUTE_VALUE_LENGTH + SIZEOF(ICE_CANDIDATE_JSON_TEMPLATE) + 1)
29
30#define CODEC_HASH_TABLE_BUCKET_COUNT 50
31#define CODEC_HASH_TABLE_BUCKET_LENGTH 2
32#define RTX_HASH_TABLE_BUCKET_COUNT 50
33#define RTX_HASH_TABLE_BUCKET_LENGTH 2
34#define TWCC_HASH_TABLE_BUCKET_COUNT 100
35#define TWCC_HASH_TABLE_BUCKET_LENGTH 2
36
37#define DATA_CHANNEL_HASH_TABLE_BUCKET_COUNT 200
38#define DATA_CHANNEL_HASH_TABLE_BUCKET_LENGTH 2
39
40// Environment variable to display SDPs
41#define DEBUG_LOG_SDP ((PCHAR) "DEBUG_LOG_SDP")
42
43#define MAX_ACCESS_THREADS_WEBRTC_CLIENT_CONTEXT 50
44
50
63
64typedef struct {
65 PHashTable pTwccRtpPktInfosHashTable; // Hash table of [seqNum, PTwccPacket]
66 UINT16 firstSeqNumInRollingWindow; // To monitor the last deleted packet in the rolling window
67 UINT16 lastReportedSeqNum; // To monitor the last packet's seqNum in the TWCC response
68 UINT16 prevReportedBaseSeqNum; // To monitor the base seqNum in the TWCC response
69 DOUBLE smoothedSlope; // EMA-smoothed trendline slope (hundreds-of-nanos units)
70 DOUBLE lastQueueDelay; // Last computed queue delay (hundreds-of-nanos units)
72
80
81typedef struct {
83 // UINT32 padding makes transportWideSequenceNumber 64bit aligned
84 // we put atomics at the top of structs because customers application could set the packing to 0
85 // in which case any atomic operations would result in bus errors if there is a misalignment
86 // for more see https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-c/pull/987#discussion_r534432907
87 UINT32 padding;
89
92 // Atomic: written by setRemoteDescription (signaling thread) and read when the DTLS
93 // handshake starts (ICE timer / threadpool thread). A re-negotiation setRemoteDescription
94 // can overlap those reads, so plain BOOL access would race.
95 volatile ATOMIC_BOOL dtlsIsServer;
96
99
101
103 PDoubleList pTransceivers;
104 PDoubleList pFakeTransceivers;
106
107 volatile ATOMIC_BOOL sctpIsEnabled;
108
111
114
116
118
120 MUTEX dataChannelsLock; // guards pDataChannels (app threads create channels while the SCTP thread uses the table)
121
122 // If the local session description is an SDP offer.
123 // (TRUE = viewer mode, FALSE = master mode)
125
126 TIMER_QUEUE_HANDLE timerQueueHandle;
127
128 // Codecs that we support and their payloadTypes
129 // When offering we generate values starting from 96
130 // When answering this is populated from the remote offer
131 PHashTable pCodecTable;
132
133 // Payload types that we use to retransmit data
134 // When answering this is populated from the remote offer
135 PHashTable pRtxTable;
136
137 // DataChannels keyed by streamId
138 PHashTable pDataChannels;
139
142
145
149
150 UINT16 MTU;
151
153
154 // congestion control
155 // https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
156 UINT16 twccExtId;
157 BOOL remoteTwccOfferedPerMedia[MAX_SDP_SESSION_MEDIA_COUNT]; // tracks which remote m-lines offered TWCC extmap
158 MUTEX twccLock;
162
163 // TWCC trendline callback (configurable estimator)
166
167 // Bandwidth controller callback (application-side)
170
174
181
191
192// Declare the structure of the Singleton
193// Members of the singleton are responsible for their own sync mechanisms.
200
201STATUS onFrameReadyFunc(UINT64, UINT16, UINT16, UINT32);
202STATUS onFrameDroppedFunc(UINT64, UINT16, UINT16, UINT32);
203VOID onSctpSessionOutboundPacket(UINT64, PBYTE, UINT32);
204VOID onSctpSessionDataChannelMessage(UINT64, UINT32, BOOL, PBYTE, UINT32);
205STATUS onSctpSessionDataChannelOpen(UINT64, UINT32, PBYTE, UINT32, PRtcDataChannelInit);
206VOID onSctpSessionDataChannelAck(UINT64, UINT32);
207
208STATUS sendPacketToRtpReceiver(PKvsPeerConnection, PBYTE, UINT32);
211UINT32 parseExtId(PCHAR);
212
213// visible for testing only
214VOID onIceConnectionStateChange(UINT64, UINT64);
215
216// STUN server address pre-resolution (initKvsWebRtc) handed to new peer connections
221STATUS onSetStunServerIp(UINT64, PCHAR, PDualKvsIpAddresses);
222
223#ifdef __cplusplus
224}
225#endif
226#endif /* __KINESIS_VIDEO_WEBRTC_CLIENT_PEERCONNECTION_PEERCONNECTION__ */
struct __DtlsSession * PDtlsSession
Definition Dtls.h:134
struct __IceAgent * PIceAgent
Definition IceAgent.h:77
struct DualKvsIpAddresses * PDualKvsIpAddresses
#define CERTIFICATE_FINGERPRINT_LENGTH
Definition Include_i.h:136
#define LOCAL_ICE_PWD_LEN
Definition PeerConnection.h:14
struct TwccRtpPacketInfo * PTwccRtpPacketInfo
PWebRtcClientContext getWebRtcClientInstance()
Definition PeerConnection.c:8
RTX_CODEC
Definition PeerConnection.h:45
@ RTC_RTX_CODEC_H265
Definition PeerConnection.h:48
@ RTC_RTX_CODEC_VP8
Definition PeerConnection.h:47
@ RTC_RTX_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE
Definition PeerConnection.h:46
STATUS onFrameDroppedFunc(UINT64, UINT16, UINT16, UINT32)
Definition PeerConnection.c:532
#define LOCAL_ICE_UFRAG_LEN
Definition PeerConnection.h:13
struct KvsPeerConnection * PKvsPeerConnection
#define MAX_ICE_PWD_LEN
Definition PeerConnection.h:19
STATUS onFrameReadyFunc(UINT64, UINT16, UINT16, UINT32)
Definition PeerConnection.c:472
STATUS createWebRtcClientInstance()
Definition PeerConnection.c:24
#define LOCAL_CNAME_LEN
Definition PeerConnection.h:15
VOID onSctpSessionDataChannelAck(UINT64, UINT32)
Definition PeerConnection.c:751
VOID onSctpSessionOutboundPacket(UINT64, PBYTE, UINT32)
Definition PeerConnection.c:689
struct AllocateSctpSortDataChannelsData * PAllocateSctpSortDataChannelsData
VOID onIceConnectionStateChange(UINT64, UINT64)
Definition PeerConnection.c:579
STATUS changePeerConnectionState(PKvsPeerConnection, RTC_PEER_CONNECTION_STATE)
Definition PeerConnection.c:419
STATUS onSctpSessionDataChannelOpen(UINT64, UINT32, PBYTE, UINT32, PRtcDataChannelInit)
Definition PeerConnection.c:766
STATUS sendPacketToRtpReceiver(PKvsPeerConnection, PBYTE, UINT32)
Definition PeerConnection.c:327
STATUS onSetStunServerIp(UINT64, PCHAR, PDualKvsIpAddresses)
Definition PeerConnection.c:1010
STATUS cleanupWebRtcClientInstance()
Definition PeerConnection.c:2130
struct TwccManager * PTwccManager
struct StunIpAddrContext * PStunIpAddrContext
struct WebRtcClientContext * PWebRtcClientContext
UINT32 parseExtId(PCHAR)
parses string of form "$number $whatever" returns $number as uint32
Definition PeerConnection.c:1543
#define MAX_ICE_UFRAG_LEN
Definition PeerConnection.h:18
STATUS twccManagerOnPacketSent(PKvsPeerConnection, PRtpPacket)
Definition PeerConnection.c:2261
VOID releaseHoldOnInstance(PWebRtcClientContext)
Definition PeerConnection.c:17
struct KvsPeerConnectionDiagnostics * PKvsPeerConnectionDiagnostics
VOID onSctpSessionDataChannelMessage(UINT64, UINT32, BOOL, PBYTE, UINT32)
Definition PeerConnection.c:707
RtpPacket * PRtpPacket
Definition RtpPacket.h:110
struct SctpSession * PSctpSession
#define MAX_SDP_SESSION_MEDIA_COUNT
Definition Sdp.h:112
struct SessionDescription * PSessionDescription
SrtpSession * PSrtpSession
Definition SrtpSession.h:21
STATUS(* RtcOnPeerCongestionFeedback)(UINT64, PCongestionCtx)
Called when the SDK has computed congestion context from TWCC feedback. The application uses this to ...
Definition Include.h:1304
VOID(* RtcOnDataChannel)(UINT64, PRtcDataChannel)
RtcOnDataChannel is fired when the remote PeerConnection creates a new DataChannel.
Definition Include.h:1349
VOID(* RtcOnSenderBandwidthEstimation)(UINT64, UINT32, UINT32, UINT32, UINT32, UINT64)
RtcOnSenderBandwidthEstimation is fired everytime a bandwidth estimation value is computed by sender....
Definition Include.h:1244
STATUS(* RtcOnTwccFeedbackReceived)(UINT64, PTwccFeedback, UINT32, PTwccCongestionState)
Called when new TWCC feedback is available from the remote peer. Updates the current network congesti...
Definition Include.h:1295
VOID(* RtcOnIceCandidate)(UINT64, PCHAR)
RtcOnIceCandidate is fired when new iceCandidate is found. if PCHAR is NULL then candidate gathering ...
Definition Include.h:1356
VOID(* RtcOnConnectionStateChange)(UINT64, RTC_PEER_CONNECTION_STATE)
RtcOnConnectionStateChange is fired to report a change in peer connection state.
Definition Include.h:1369
RTC_PEER_CONNECTION_STATE
RTC_PEER_CONNECTION_STATE Stats of RTC peer connection.
Definition Include.h:1023
struct RtcDataChannelInit * PRtcDataChannelInit
#define MAX_ICE_CONFIG_URI_LEN
Definition Stats.h:30
Definition PeerConnection.h:175
PKvsPeerConnection pKvsPeerConnection
Definition PeerConnection.h:177
BOOL negotiatedPass
TRUE while keying negotiated channels by their explicit id, FALSE while assigning in-band ids.
Definition PeerConnection.h:179
PHashTable unkeyedDataChannels
Definition PeerConnection.h:178
UINT32 currentDataChannelId
Definition PeerConnection.h:176
Definition Include_i.h:154
Definition PeerConnection.h:73
UINT64 peerConnectionCreationTime
Definition PeerConnection.h:74
UINT64 dtlsSessionSetupTime
Definition PeerConnection.h:75
UINT64 closePeerConnectionTime
Definition PeerConnection.h:77
UINT64 iceHolePunchingTime
Definition PeerConnection.h:76
UINT64 freePeerConnectionTime
Definition PeerConnection.h:78
Definition PeerConnection.h:81
RtcOnTwccFeedbackReceived onTwccFeedbackReceived
Definition PeerConnection.h:164
PHashTable pDataChannels
Definition PeerConnection.h:138
RtcOnDataChannel onDataChannel
Definition PeerConnection.h:141
PDoubleList pTransceivers
Definition PeerConnection.h:103
volatile SIZE_T transportWideSequenceNumber
Definition PeerConnection.h:88
MUTEX pSrtpSessionLock
Definition PeerConnection.h:97
PHashTable pCodecTable
Definition PeerConnection.h:131
CHAR remoteIcePwd[256+1]
Definition PeerConnection.h:113
RtcOnConnectionStateChange onConnectionStateChange
Definition PeerConnection.h:147
RtcOnIceCandidate onIceCandidate
Definition PeerConnection.h:144
RtcOnSenderBandwidthEstimation onSenderBandwidthEstimation
Definition PeerConnection.h:160
PHashTable pRtxTable
Definition PeerConnection.h:135
UINT32 padding
Definition PeerConnection.h:87
UINT64 onIceCandidateCustomData
Definition PeerConnection.h:143
UINT64 onDataChannelCustomData
Definition PeerConnection.h:140
UINT64 onConnectionStateChangeCustomData
Definition PeerConnection.h:146
RtcPeerConnection peerConnection
Definition PeerConnection.h:82
UINT16 MTU
Definition PeerConnection.h:150
BOOL isOffer
Definition PeerConnection.h:124
UINT64 onPeerCongestionFeedbackCustomData
Definition PeerConnection.h:169
PSctpSession pSctpSession
Definition PeerConnection.h:100
BOOL remoteTwccOfferedPerMedia[MAX_SDP_SESSION_MEDIA_COUNT]
Definition PeerConnection.h:157
CHAR remoteCertificateFingerprint[CERTIFICATE_FINGERPRINT_LENGTH+1]
Definition PeerConnection.h:117
PTwccManager pTwccManager
Definition PeerConnection.h:159
PIceAgent pIceAgent
Definition PeerConnection.h:90
PDoubleList pAnswerTransceivers
Definition PeerConnection.h:105
MUTEX twccLock
Definition PeerConnection.h:158
CHAR localCNAME[16+1]
Definition PeerConnection.h:115
PSessionDescription pRemoteSessionDescription
Definition PeerConnection.h:102
UINT64 iceConnectingStartTime
Definition PeerConnection.h:171
CHAR remoteIceUfrag[256+1]
Definition PeerConnection.h:112
MUTEX peerConnectionObjLock
Definition PeerConnection.h:119
CHAR localIceUfrag[4+1]
Definition PeerConnection.h:109
UINT64 onSenderBandwidthEstimationCustomData
Definition PeerConnection.h:161
KvsPeerConnectionDiagnostics peerConnectionDiagnostics
Definition PeerConnection.h:172
NullableBool canTrickleIce
Definition PeerConnection.h:152
TIMER_QUEUE_HANDLE timerQueueHandle
Definition PeerConnection.h:126
PDtlsSession pDtlsSession
Definition PeerConnection.h:91
volatile ATOMIC_BOOL sctpIsEnabled
Definition PeerConnection.h:107
RtcOnPeerCongestionFeedback onPeerCongestionFeedback
Definition PeerConnection.h:168
volatile ATOMIC_BOOL dtlsIsServer
Definition PeerConnection.h:95
RTC_PEER_CONNECTION_STATE connectionState
Definition PeerConnection.h:148
UINT16 twccExtId
Definition PeerConnection.h:156
MUTEX dataChannelsLock
Definition PeerConnection.h:120
CHAR localIcePwd[24+1]
Definition PeerConnection.h:110
UINT64 onTwccFeedbackReceivedCustomData
Definition PeerConnection.h:165
PDoubleList pFakeTransceivers
Definition PeerConnection.h:104
PSrtpSession pSrtpSession
Definition PeerConnection.h:98
Definition NullableDefs.h:60
An RtcPeerConnection instance allows an application to establish peer-to-peer communications with ano...
Definition Include.h:1387
Definition PeerConnection.h:182
UINT64 expirationDuration
Definition PeerConnection.h:188
UINT64 stunDnsResolutionTime
Definition PeerConnection.h:187
STATUS status
Definition PeerConnection.h:189
DualKvsIpAddresses kvsIpAddresses
Definition PeerConnection.h:184
BOOL isIpInitialized
Definition PeerConnection.h:185
CHAR hostname[MAX_ICE_CONFIG_URI_LEN+1]
Definition PeerConnection.h:183
UINT64 startTime
Definition PeerConnection.h:186
Definition PeerConnection.h:64
DOUBLE lastQueueDelay
Definition PeerConnection.h:70
UINT16 lastReportedSeqNum
Definition PeerConnection.h:67
PHashTable pTwccRtpPktInfosHashTable
Definition PeerConnection.h:65
DOUBLE smoothedSlope
Definition PeerConnection.h:69
UINT16 prevReportedBaseSeqNum
Definition PeerConnection.h:68
UINT16 firstSeqNumInRollingWindow
Definition PeerConnection.h:66
Definition PeerConnection.h:58
UINT64 localTimeKvs
sender send time (T) in hundreds of nanos
Definition PeerConnection.h:59
UINT64 remoteTimeKvs
receiver arrival time (R) in hundreds of nanos (reconstructed)
Definition PeerConnection.h:60
UINT32 packetSize
Definition PeerConnection.h:61
Definition PeerConnection.h:194
PStunIpAddrContext pStunIpAddrCtx
Definition PeerConnection.h:195
volatile ATOMIC_BOOL isContextInitialized
Definition PeerConnection.h:196
MUTEX stunCtxlock
Definition PeerConnection.h:198
volatile SIZE_T contextRefCnt
Definition PeerConnection.h:197