rtc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * Copyright (c) 2019-2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_C_API
  9. #define RTC_C_API
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #ifdef RTC_STATIC
  14. #define RTC_C_EXPORT
  15. #else // dynamic library
  16. #ifdef _WIN32
  17. #ifdef RTC_EXPORTS
  18. #define RTC_C_EXPORT __declspec(dllexport) // building the library
  19. #else
  20. #define RTC_C_EXPORT __declspec(dllimport) // using the library
  21. #endif
  22. #else // not WIN32
  23. #define RTC_C_EXPORT
  24. #endif
  25. #endif
  26. #ifdef _WIN32
  27. #ifdef CAPI_STDCALL
  28. #define RTC_API __stdcall
  29. #else
  30. #define RTC_API
  31. #endif
  32. #else // not WIN32
  33. #define RTC_API
  34. #endif
  35. #ifndef RTC_ENABLE_WEBSOCKET
  36. #define RTC_ENABLE_WEBSOCKET 1
  37. #endif
  38. #ifndef RTC_ENABLE_MEDIA
  39. #define RTC_ENABLE_MEDIA 1
  40. #endif
  41. #define RTC_DEFAULT_MTU 1280 // IPv6 minimum guaranteed MTU
  42. #if RTC_ENABLE_MEDIA
  43. #define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE \
  44. ((uint16_t)(RTC_DEFAULT_MTU - 12 - 8 - 40)) // SRTP/UDP/IPv6
  45. #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512)
  46. #endif
  47. #include <stdbool.h>
  48. #include <stdint.h>
  49. // libdatachannel C API
  50. typedef enum {
  51. RTC_NEW = 0,
  52. RTC_CONNECTING = 1,
  53. RTC_CONNECTED = 2,
  54. RTC_DISCONNECTED = 3,
  55. RTC_FAILED = 4,
  56. RTC_CLOSED = 5
  57. } rtcState;
  58. typedef enum {
  59. RTC_GATHERING_NEW = 0,
  60. RTC_GATHERING_INPROGRESS = 1,
  61. RTC_GATHERING_COMPLETE = 2
  62. } rtcGatheringState;
  63. typedef enum {
  64. RTC_SIGNALING_STABLE = 0,
  65. RTC_SIGNALING_HAVE_LOCAL_OFFER = 1,
  66. RTC_SIGNALING_HAVE_REMOTE_OFFER = 2,
  67. RTC_SIGNALING_HAVE_LOCAL_PRANSWER = 3,
  68. RTC_SIGNALING_HAVE_REMOTE_PRANSWER = 4,
  69. } rtcSignalingState;
  70. typedef enum { // Don't change, it must match plog severity
  71. RTC_LOG_NONE = 0,
  72. RTC_LOG_FATAL = 1,
  73. RTC_LOG_ERROR = 2,
  74. RTC_LOG_WARNING = 3,
  75. RTC_LOG_INFO = 4,
  76. RTC_LOG_DEBUG = 5,
  77. RTC_LOG_VERBOSE = 6
  78. } rtcLogLevel;
  79. typedef enum {
  80. RTC_CERTIFICATE_DEFAULT = 0, // ECDSA
  81. RTC_CERTIFICATE_ECDSA = 1,
  82. RTC_CERTIFICATE_RSA = 2,
  83. } rtcCertificateType;
  84. typedef enum {
  85. // video
  86. RTC_CODEC_H264 = 0,
  87. RTC_CODEC_VP8 = 1,
  88. RTC_CODEC_VP9 = 2,
  89. // audio
  90. RTC_CODEC_OPUS = 128,
  91. RTC_CODEC_PCMU = 129,
  92. RTC_CODEC_PCMA = 130
  93. } rtcCodec;
  94. typedef enum {
  95. RTC_DIRECTION_UNKNOWN = 0,
  96. RTC_DIRECTION_SENDONLY = 1,
  97. RTC_DIRECTION_RECVONLY = 2,
  98. RTC_DIRECTION_SENDRECV = 3,
  99. RTC_DIRECTION_INACTIVE = 4
  100. } rtcDirection;
  101. typedef enum { RTC_TRANSPORT_POLICY_ALL = 0, RTC_TRANSPORT_POLICY_RELAY = 1 } rtcTransportPolicy;
  102. #define RTC_ERR_SUCCESS 0
  103. #define RTC_ERR_INVALID -1 // invalid argument
  104. #define RTC_ERR_FAILURE -2 // runtime error
  105. #define RTC_ERR_NOT_AVAIL -3 // element not available
  106. #define RTC_ERR_TOO_SMALL -4 // buffer too small
  107. typedef void(RTC_API *rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  108. typedef void(RTC_API *rtcDescriptionCallbackFunc)(int pc, const char *sdp, const char *type,
  109. void *ptr);
  110. typedef void(RTC_API *rtcCandidateCallbackFunc)(int pc, const char *cand, const char *mid,
  111. void *ptr);
  112. typedef void(RTC_API *rtcStateChangeCallbackFunc)(int pc, rtcState state, void *ptr);
  113. typedef void(RTC_API *rtcGatheringStateCallbackFunc)(int pc, rtcGatheringState state, void *ptr);
  114. typedef void(RTC_API *rtcSignalingStateCallbackFunc)(int pc, rtcSignalingState state, void *ptr);
  115. typedef void(RTC_API *rtcDataChannelCallbackFunc)(int pc, int dc, void *ptr);
  116. typedef void(RTC_API *rtcTrackCallbackFunc)(int pc, int tr, void *ptr);
  117. typedef void(RTC_API *rtcOpenCallbackFunc)(int id, void *ptr);
  118. typedef void(RTC_API *rtcClosedCallbackFunc)(int id, void *ptr);
  119. typedef void(RTC_API *rtcErrorCallbackFunc)(int id, const char *error, void *ptr);
  120. typedef void(RTC_API *rtcMessageCallbackFunc)(int id, const char *message, int size, void *ptr);
  121. typedef void *(RTC_API *rtcInterceptorCallbackFunc)(int pc, const char *message, int size,
  122. void *ptr);
  123. typedef void(RTC_API *rtcBufferedAmountLowCallbackFunc)(int id, void *ptr);
  124. typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
  125. // Log
  126. // NULL cb on the first call will log to stdout
  127. RTC_C_EXPORT void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb);
  128. // User pointer
  129. RTC_C_EXPORT void rtcSetUserPointer(int id, void *ptr);
  130. RTC_C_EXPORT void *rtcGetUserPointer(int i);
  131. // PeerConnection
  132. typedef struct {
  133. const char **iceServers;
  134. int iceServersCount;
  135. const char *proxyServer; // libnice only
  136. const char *bindAddress; // libjuice only, NULL means any
  137. rtcCertificateType certificateType;
  138. rtcTransportPolicy iceTransportPolicy;
  139. bool enableIceTcp; // libnice only
  140. bool enableIceUdpMux; // libjuice only
  141. bool disableAutoNegotiation;
  142. bool forceMediaTransport;
  143. uint16_t portRangeBegin; // 0 means automatic
  144. uint16_t portRangeEnd; // 0 means automatic
  145. int mtu; // <= 0 means automatic
  146. int maxMessageSize; // <= 0 means default
  147. } rtcConfiguration;
  148. RTC_C_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  149. RTC_C_EXPORT int rtcClosePeerConnection(int pc);
  150. RTC_C_EXPORT int rtcDeletePeerConnection(int pc);
  151. RTC_C_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  152. RTC_C_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  153. RTC_C_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  154. RTC_C_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  155. RTC_C_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCallbackFunc cb);
  156. RTC_C_EXPORT int rtcSetLocalDescription(int pc, const char *type);
  157. RTC_C_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  158. RTC_C_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  159. RTC_C_EXPORT int rtcGetLocalDescription(int pc, char *buffer, int size);
  160. RTC_C_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);
  161. RTC_C_EXPORT int rtcGetLocalDescriptionType(int pc, char *buffer, int size);
  162. RTC_C_EXPORT int rtcGetRemoteDescriptionType(int pc, char *buffer, int size);
  163. RTC_C_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
  164. RTC_C_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
  165. RTC_C_EXPORT int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote,
  166. int remoteSize);
  167. RTC_C_EXPORT int rtcGetMaxDataChannelStream(int pc);
  168. // DataChannel, Track, and WebSocket common API
  169. RTC_C_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  170. RTC_C_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  171. RTC_C_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  172. RTC_C_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  173. RTC_C_EXPORT int rtcSendMessage(int id, const char *data, int size);
  174. RTC_C_EXPORT int rtcClose(int id);
  175. RTC_C_EXPORT int rtcDelete(int id);
  176. RTC_C_EXPORT bool rtcIsOpen(int id);
  177. RTC_C_EXPORT bool rtcIsClosed(int id);
  178. RTC_C_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
  179. RTC_C_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
  180. RTC_C_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  181. // DataChannel, Track, and WebSocket common extended API
  182. RTC_C_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
  183. RTC_C_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  184. RTC_C_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);
  185. // DataChannel
  186. typedef struct {
  187. bool unordered;
  188. bool unreliable;
  189. int maxPacketLifeTime; // ignored if reliable
  190. int maxRetransmits; // ignored if reliable
  191. } rtcReliability;
  192. typedef struct {
  193. rtcReliability reliability;
  194. const char *protocol; // empty string if NULL
  195. bool negotiated;
  196. bool manualStream;
  197. uint16_t stream; // numeric ID 0-65534, ignored if manualStream is false
  198. } rtcDataChannelInit;
  199. RTC_C_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  200. RTC_C_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  201. RTC_C_EXPORT int rtcCreateDataChannelEx(int pc, const char *label,
  202. const rtcDataChannelInit *init); // returns dc id
  203. RTC_C_EXPORT int rtcDeleteDataChannel(int dc);
  204. RTC_C_EXPORT int rtcGetDataChannelStream(int dc);
  205. RTC_C_EXPORT int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  206. RTC_C_EXPORT int rtcGetDataChannelProtocol(int dc, char *buffer, int size);
  207. RTC_C_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability);
  208. // Track
  209. typedef struct {
  210. rtcDirection direction;
  211. rtcCodec codec;
  212. int payloadType;
  213. uint32_t ssrc;
  214. const char *mid;
  215. const char *name; // optional
  216. const char *msid; // optional
  217. const char *trackId; // optional, track ID used in MSID
  218. } rtcTrackInit;
  219. RTC_C_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
  220. RTC_C_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
  221. RTC_C_EXPORT int rtcAddTrackEx(int pc, const rtcTrackInit *init); // returns tr id
  222. RTC_C_EXPORT int rtcDeleteTrack(int tr);
  223. RTC_C_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
  224. RTC_C_EXPORT int rtcGetTrackMid(int tr, char *buffer, int size);
  225. RTC_C_EXPORT int rtcGetTrackDirection(int tr, rtcDirection *direction);
  226. #if RTC_ENABLE_MEDIA
  227. // Media
  228. // Define how NAL units are separated in a H264 sample
  229. typedef enum {
  230. RTC_NAL_SEPARATOR_LENGTH = 0, // first 4 bytes are NAL unit length
  231. RTC_NAL_SEPARATOR_LONG_START_SEQUENCE = 1, // 0x00, 0x00, 0x00, 0x01
  232. RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE = 2, // 0x00, 0x00, 0x01
  233. RTC_NAL_SEPARATOR_START_SEQUENCE = 3, // long or short start sequence
  234. } rtcNalUnitSeparator;
  235. typedef struct {
  236. uint32_t ssrc;
  237. const char *cname;
  238. uint8_t payloadType;
  239. uint32_t clockRate;
  240. uint16_t sequenceNumber;
  241. uint32_t timestamp;
  242. // H264
  243. rtcNalUnitSeparator nalSeparator; // NAL unit separator
  244. uint16_t maxFragmentSize; // Maximum NAL unit fragment size
  245. } rtcPacketizationHandlerInit;
  246. typedef struct {
  247. uint32_t ssrc;
  248. const char *name; // optional
  249. const char *msid; // optional
  250. const char *trackId; // optional, track ID used in MSID
  251. } rtcSsrcForTypeInit;
  252. // Opaque message
  253. // Opaque type used (via rtcMessage*) to reference an rtc::Message
  254. typedef void* rtcMessage;
  255. // Allocate a new opaque message.
  256. // Must be explicitly freed by rtcDeleteOpaqueMessage() unless
  257. // explicitly returned by a media interceptor callback;
  258. RTC_C_EXPORT rtcMessage *rtcCreateOpaqueMessage(void *data, int size);
  259. RTC_C_EXPORT void rtcDeleteOpaqueMessage(rtcMessage *msg);
  260. // Set MediaInterceptor for peer connection
  261. RTC_C_EXPORT int rtcSetMediaInterceptorCallback(int id, rtcInterceptorCallbackFunc cb);
  262. // Set H264PacketizationHandler for track
  263. RTC_C_EXPORT int rtcSetH264PacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);
  264. // Set OpusPacketizationHandler for track
  265. RTC_C_EXPORT int rtcSetOpusPacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);
  266. // Chain RtcpSrReporter to handler chain for given track
  267. RTC_C_EXPORT int rtcChainRtcpSrReporter(int tr);
  268. // Chain RtcpNackResponder to handler chain for given track
  269. RTC_C_EXPORT int rtcChainRtcpNackResponder(int tr, unsigned int maxStoredPacketsCount);
  270. // Transform seconds to timestamp using track's clock rate, result is written to timestamp
  271. RTC_C_EXPORT int rtcTransformSecondsToTimestamp(int id, double seconds, uint32_t *timestamp);
  272. // Transform timestamp to seconds using track's clock rate, result is written to seconds
  273. RTC_C_EXPORT int rtcTransformTimestampToSeconds(int id, uint32_t timestamp, double *seconds);
  274. // Get current timestamp, result is written to timestamp
  275. RTC_C_EXPORT int rtcGetCurrentTrackTimestamp(int id, uint32_t *timestamp);
  276. // Set RTP timestamp for track identified by given id
  277. RTC_C_EXPORT int rtcSetTrackRtpTimestamp(int id, uint32_t timestamp);
  278. // Get timestamp of previous RTCP SR, result is written to timestamp
  279. RTC_C_EXPORT int rtcGetPreviousTrackSenderReportTimestamp(int id, uint32_t *timestamp);
  280. // Set NeedsToReport flag in RtcpSrReporter handler identified by given track id
  281. RTC_C_EXPORT int rtcSetNeedsToSendRtcpSr(int id);
  282. // Get all available payload types for given codec and stores them in buffer, does nothing if
  283. // buffer is NULL
  284. int rtcGetTrackPayloadTypesForCodec(int tr, const char *ccodec, int *buffer, int size);
  285. // Get all SSRCs for given track
  286. int rtcGetSsrcsForTrack(int tr, uint32_t *buffer, int count);
  287. // Get CName for SSRC
  288. int rtcGetCNameForSsrc(int tr, uint32_t ssrc, char *cname, int cnameSize);
  289. // Get all SSRCs for given media type in given SDP
  290. int rtcGetSsrcsForType(const char *mediaType, const char *sdp, uint32_t *buffer, int bufferSize);
  291. // Set SSRC for given media type in given SDP
  292. int rtcSetSsrcForType(const char *mediaType, const char *sdp, char *buffer, const int bufferSize,
  293. rtcSsrcForTypeInit *init);
  294. #endif // RTC_ENABLE_MEDIA
  295. #if RTC_ENABLE_WEBSOCKET
  296. // WebSocket
  297. typedef struct {
  298. bool disableTlsVerification; // if true, don't verify the TLS certificate
  299. const char *proxyServer; // unsupported for now
  300. const char **protocols;
  301. int protocolsCount;
  302. int pingInterval; // in milliseconds, 0 means default, < 0 means disabled
  303. int maxOutstandingPings; // 0 means default, < 0 means disabled
  304. } rtcWsConfiguration;
  305. RTC_C_EXPORT int rtcCreateWebSocket(const char *url); // returns ws id
  306. RTC_C_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *config);
  307. RTC_C_EXPORT int rtcDeleteWebSocket(int ws);
  308. RTC_C_EXPORT int rtcGetWebSocketRemoteAddress(int ws, char *buffer, int size);
  309. RTC_C_EXPORT int rtcGetWebSocketPath(int ws, char *buffer, int size);
  310. // WebSocketServer
  311. typedef void(RTC_API *rtcWebSocketClientCallbackFunc)(int wsserver, int ws, void *ptr);
  312. typedef struct {
  313. uint16_t port; // 0 means automatic selection
  314. bool enableTls; // if true, enable TLS (WSS)
  315. const char *certificatePemFile; // NULL for autogenerated certificate
  316. const char *keyPemFile; // NULL for autogenerated certificate
  317. const char *keyPemPass; // NULL if no pass
  318. const char *bindAddress; // NULL for IP_ANY_ADDR
  319. } rtcWsServerConfiguration;
  320. RTC_C_EXPORT int rtcCreateWebSocketServer(const rtcWsServerConfiguration *config,
  321. rtcWebSocketClientCallbackFunc cb); // returns wsserver id
  322. RTC_C_EXPORT int rtcDeleteWebSocketServer(int wsserver);
  323. RTC_C_EXPORT int rtcGetWebSocketServerPort(int wsserver);
  324. #endif
  325. // Optional global preload and cleanup
  326. RTC_C_EXPORT void rtcPreload(void);
  327. RTC_C_EXPORT void rtcCleanup(void);
  328. // SCTP global settings
  329. typedef struct {
  330. int recvBufferSize; // in bytes, <= 0 means optimized default
  331. int sendBufferSize; // in bytes, <= 0 means optimized default
  332. int maxChunksOnQueue; // in chunks, <= 0 means optimized default
  333. int initialCongestionWindow; // in MTUs, <= 0 means optimized default
  334. int maxBurst; // in MTUs, 0 means optimized default, < 0 means disabled
  335. int congestionControlModule; // 0: RFC2581 (default), 1: HSTCP, 2: H-TCP, 3: RTCC
  336. int delayedSackTimeMs; // in msecs, 0 means optimized default, < 0 means disabled
  337. int minRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  338. int maxRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  339. int initialRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  340. int maxRetransmitAttempts; // number of retransmissions, <= 0 means optimized default
  341. int heartbeatIntervalMs; // in msecs, <= 0 means optimized default
  342. } rtcSctpSettings;
  343. // Note: SCTP settings apply to newly-created PeerConnections only
  344. RTC_C_EXPORT int rtcSetSctpSettings(const rtcSctpSettings *settings);
  345. #ifdef __cplusplus
  346. } // extern "C"
  347. #endif
  348. #endif