rtc.h 15 KB

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