rtc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * Copyright (c) 2019 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 _WIN32
  24. #define RTC_EXPORT __declspec(dllexport)
  25. #ifdef CAPI_STDCALL
  26. #define RTC_API __stdcall
  27. #else
  28. #define RTC_API
  29. #endif
  30. #else // not WIN32
  31. #define RTC_EXPORT
  32. #define RTC_API
  33. #endif
  34. #ifndef RTC_ENABLE_WEBSOCKET
  35. #define RTC_ENABLE_WEBSOCKET 1
  36. #endif
  37. #ifndef RTC_ENABLE_MEDIA
  38. #define RTC_ENABLE_MEDIA 1
  39. #endif
  40. #define RTC_DEFAULT_MTU 1280 // IPv6 minimum guaranteed MTU
  41. #if RTC_ENABLE_MEDIA
  42. #define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE \
  43. ((uint16_t)(RTC_DEFAULT_MTU - 12 - 8 - 40)) // SRTP/UDP/IPv6
  44. #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512)
  45. #endif
  46. #include <stdbool.h>
  47. #include <stdint.h>
  48. // libdatachannel C API
  49. typedef enum {
  50. RTC_NEW = 0,
  51. RTC_CONNECTING = 1,
  52. RTC_CONNECTED = 2,
  53. RTC_DISCONNECTED = 3,
  54. RTC_FAILED = 4,
  55. RTC_CLOSED = 5
  56. } rtcState;
  57. typedef enum {
  58. RTC_GATHERING_NEW = 0,
  59. RTC_GATHERING_INPROGRESS = 1,
  60. RTC_GATHERING_COMPLETE = 2
  61. } rtcGatheringState;
  62. typedef enum {
  63. RTC_SIGNALING_STABLE = 0,
  64. RTC_SIGNALING_HAVE_LOCAL_OFFER = 1,
  65. RTC_SIGNALING_HAVE_REMOTE_OFFER = 2,
  66. RTC_SIGNALING_HAVE_LOCAL_PRANSWER = 3,
  67. RTC_SIGNALING_HAVE_REMOTE_PRANSWER = 4,
  68. } rtcSignalingState;
  69. typedef enum { // Don't change, it must match plog severity
  70. RTC_LOG_NONE = 0,
  71. RTC_LOG_FATAL = 1,
  72. RTC_LOG_ERROR = 2,
  73. RTC_LOG_WARNING = 3,
  74. RTC_LOG_INFO = 4,
  75. RTC_LOG_DEBUG = 5,
  76. RTC_LOG_VERBOSE = 6
  77. } rtcLogLevel;
  78. typedef enum {
  79. RTC_CERTIFICATE_DEFAULT = 0, // ECDSA
  80. RTC_CERTIFICATE_ECDSA = 1,
  81. RTC_CERTIFICATE_RSA = 2,
  82. } rtcCertificateType;
  83. typedef enum {
  84. // video
  85. RTC_CODEC_H264 = 0,
  86. RTC_CODEC_VP8 = 1,
  87. RTC_CODEC_VP9 = 2,
  88. // audio
  89. RTC_CODEC_OPUS = 128
  90. } rtcCodec;
  91. typedef enum {
  92. RTC_DIRECTION_UNKNOWN = 0,
  93. RTC_DIRECTION_SENDONLY = 1,
  94. RTC_DIRECTION_RECVONLY = 2,
  95. RTC_DIRECTION_SENDRECV = 3,
  96. RTC_DIRECTION_INACTIVE = 4
  97. } rtcDirection;
  98. #define RTC_ERR_SUCCESS 0
  99. #define RTC_ERR_INVALID -1 // invalid argument
  100. #define RTC_ERR_FAILURE -2 // runtime error
  101. #define RTC_ERR_NOT_AVAIL -3 // element not available
  102. #define RTC_ERR_TOO_SMALL -4 // buffer too small
  103. typedef void(RTC_API *rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  104. typedef void(RTC_API *rtcDescriptionCallbackFunc)(int pc, const char *sdp, const char *type,
  105. void *ptr);
  106. typedef void(RTC_API *rtcCandidateCallbackFunc)(int pc, const char *cand, const char *mid,
  107. void *ptr);
  108. typedef void(RTC_API *rtcStateChangeCallbackFunc)(int pc, rtcState state, void *ptr);
  109. typedef void(RTC_API *rtcGatheringStateCallbackFunc)(int pc, rtcGatheringState state, void *ptr);
  110. typedef void(RTC_API *rtcSignalingStateCallbackFunc)(int pc, rtcSignalingState state, void *ptr);
  111. typedef void(RTC_API *rtcDataChannelCallbackFunc)(int pc, int dc, void *ptr);
  112. typedef void(RTC_API *rtcTrackCallbackFunc)(int pc, int tr, void *ptr);
  113. typedef void(RTC_API *rtcOpenCallbackFunc)(int id, void *ptr);
  114. typedef void(RTC_API *rtcClosedCallbackFunc)(int id, void *ptr);
  115. typedef void(RTC_API *rtcErrorCallbackFunc)(int id, const char *error, void *ptr);
  116. typedef void(RTC_API *rtcMessageCallbackFunc)(int id, const char *message, int size, void *ptr);
  117. typedef void(RTC_API *rtcBufferedAmountLowCallbackFunc)(int id, void *ptr);
  118. typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
  119. // Log
  120. // NULL cb on the first call will log to stdout
  121. RTC_EXPORT void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb);
  122. // User pointer
  123. RTC_EXPORT void rtcSetUserPointer(int id, void *ptr);
  124. RTC_EXPORT void *rtcGetUserPointer(int i);
  125. // PeerConnection
  126. typedef struct {
  127. const char **iceServers;
  128. int iceServersCount;
  129. const char *bindAddress; // libjuice only, NULL means any
  130. rtcCertificateType certificateType;
  131. bool enableIceTcp;
  132. bool disableAutoNegotiation;
  133. uint16_t portRangeBegin; // 0 means automatic
  134. uint16_t portRangeEnd; // 0 means automatic
  135. int mtu; // <= 0 means automatic
  136. int maxMessageSize; // <= 0 means default
  137. } rtcConfiguration;
  138. RTC_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  139. RTC_EXPORT int rtcDeletePeerConnection(int pc);
  140. RTC_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  141. RTC_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  142. RTC_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  143. RTC_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  144. RTC_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCallbackFunc cb);
  145. RTC_EXPORT int rtcSetLocalDescription(int pc, const char *type);
  146. RTC_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  147. RTC_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  148. RTC_EXPORT int rtcGetLocalDescription(int pc, char *buffer, int size);
  149. RTC_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);
  150. RTC_EXPORT int rtcGetLocalDescriptionType(int pc, char *buffer, int size);
  151. RTC_EXPORT int rtcGetRemoteDescriptionType(int pc, char *buffer, int size);
  152. RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
  153. RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
  154. RTC_EXPORT int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote,
  155. int remoteSize);
  156. // DataChannel
  157. typedef struct {
  158. bool unordered;
  159. bool unreliable;
  160. int maxPacketLifeTime; // ignored if reliable
  161. int maxRetransmits; // ignored if reliable
  162. } rtcReliability;
  163. typedef struct {
  164. rtcReliability reliability;
  165. const char *protocol; // empty string if NULL
  166. bool negotiated;
  167. bool manualStream;
  168. uint16_t stream; // numeric ID 0-65534, ignored if manualStream is false
  169. } rtcDataChannelInit;
  170. RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  171. RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  172. RTC_EXPORT int rtcCreateDataChannelEx(int pc, const char *label,
  173. const rtcDataChannelInit *init); // returns dc id
  174. RTC_EXPORT int rtcIsOpen(int dc);
  175. RTC_EXPORT int rtcDeleteDataChannel(int dc);
  176. RTC_EXPORT int rtcGetDataChannelStream(int dc);
  177. RTC_EXPORT int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  178. RTC_EXPORT int rtcGetDataChannelProtocol(int dc, char *buffer, int size);
  179. RTC_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability);
  180. // Track
  181. typedef struct {
  182. rtcDirection direction;
  183. rtcCodec codec;
  184. int payloadType;
  185. uint32_t ssrc;
  186. const char *mid;
  187. const char *name; // optional
  188. const char *msid; // optional
  189. const char *trackId; // optional, track ID used in MSID
  190. } rtcTrackInit;
  191. RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
  192. RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
  193. RTC_EXPORT int rtcAddTrackEx(int pc, const rtcTrackInit *init); // returns tr id
  194. RTC_EXPORT int rtcDeleteTrack(int tr);
  195. RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
  196. #if RTC_ENABLE_MEDIA
  197. // Media
  198. typedef struct {
  199. uint32_t ssrc;
  200. const char *cname;
  201. uint8_t payloadType;
  202. uint32_t clockRate;
  203. uint16_t maxFragmentSize; // Maximum NALU fragment size
  204. uint16_t sequenceNumber;
  205. uint32_t timestamp;
  206. } rtcPacketizationHandlerInit;
  207. typedef struct {
  208. double seconds; // Start time in seconds
  209. bool since1970; // true if seconds since 1970
  210. // false if seconds since 1900
  211. uint32_t timestamp; // Start timestamp
  212. } rtcStartTime;
  213. typedef struct {
  214. uint32_t ssrc;
  215. const char *name; // optional
  216. const char *msid; // optional
  217. const char *trackId; // optional, track ID used in MSID
  218. } rtcSsrcForTypeInit;
  219. // Set H264PacketizationHandler for track
  220. RTC_EXPORT int rtcSetH264PacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);
  221. // Set OpusPacketizationHandler for track
  222. RTC_EXPORT int rtcSetOpusPacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);
  223. // Chain RtcpSrReporter to handler chain for given track
  224. RTC_EXPORT int rtcChainRtcpSrReporter(int tr);
  225. // Chain RtcpNackResponder to handler chain for given track
  226. RTC_EXPORT int rtcChainRtcpNackResponder(int tr, unsigned int maxStoredPacketsCount);
  227. /// Set start time for RTP stream
  228. RTC_EXPORT int rtcSetRtpConfigurationStartTime(int id, const rtcStartTime *startTime);
  229. // Start stats recording for RTCP Sender Reporter
  230. RTC_EXPORT int rtcStartRtcpSenderReporterRecording(int id);
  231. // Transform seconds to timestamp using track's clock rate
  232. // Result is written to timestamp
  233. RTC_EXPORT int rtcTransformSecondsToTimestamp(int id, double seconds, uint32_t *timestamp);
  234. // Transform timestamp to seconds using track's clock rate
  235. // Result is written to seconds
  236. RTC_EXPORT int rtcTransformTimestampToSeconds(int id, uint32_t timestamp, double *seconds);
  237. // Get current timestamp
  238. // Result is written to timestamp
  239. RTC_EXPORT int rtcGetCurrentTrackTimestamp(int id, uint32_t *timestamp);
  240. // Get start timestamp for track identified by given id
  241. // Result is written to timestamp
  242. RTC_EXPORT int rtcGetTrackStartTimestamp(int id, uint32_t *timestamp);
  243. // Set RTP timestamp for track identified by given id
  244. RTC_EXPORT int rtcSetTrackRtpTimestamp(int id, uint32_t timestamp);
  245. // Get timestamp of previous RTCP SR
  246. // Result is written to timestamp
  247. RTC_EXPORT int rtcGetPreviousTrackSenderReportTimestamp(int id, uint32_t *timestamp);
  248. // Set NeedsToReport flag in RtcpSrReporter handler identified by given track id
  249. RTC_EXPORT int rtcSetNeedsToSendRtcpSr(int id);
  250. /// Get all available payload types for given codec and stores them in buffer, does nothing if buffer is NULL
  251. int rtcGetTrackPayloadTypesForCodec(int tr, const char * ccodec, int * buffer, int size);
  252. /// Get all SSRCs for given track
  253. int rtcGetSsrcsForTrack(int tr, uint32_t * buffer, int count);
  254. /// Get CName for SSRC
  255. int rtcGetCNameForSsrc(int tr, uint32_t ssrc, char * cname, int cnameSize);
  256. /// Get all SSRCs for given media type in given SDP
  257. /// @param mediaType Media type (audio/video)
  258. int rtcGetSsrcsForType(const char * mediaType, const char * sdp, uint32_t * buffer, int bufferSize);
  259. /// Set SSRC for given media type in given SDP
  260. int rtcSetSsrcForType(const char * mediaType, const char * sdp, char * buffer, const int bufferSize, rtcSsrcForTypeInit * init);
  261. #endif // RTC_ENABLE_MEDIA
  262. #if RTC_ENABLE_WEBSOCKET
  263. // WebSocket
  264. typedef struct {
  265. bool disableTlsVerification; // if true, don't verify the TLS certificate
  266. } rtcWsConfiguration;
  267. RTC_EXPORT int rtcCreateWebSocket(const char *url); // returns ws id
  268. RTC_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *config);
  269. RTC_EXPORT int rtcDeleteWebsocket(int ws);
  270. #endif
  271. // DataChannel, Track, and WebSocket common API
  272. RTC_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  273. RTC_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  274. RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  275. RTC_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  276. RTC_EXPORT int rtcSendMessage(int id, const char *data, int size);
  277. RTC_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
  278. RTC_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
  279. RTC_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  280. // DataChannel, Track, and WebSocket common extended API
  281. RTC_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
  282. RTC_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  283. RTC_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);
  284. // Optional global preload and cleanup
  285. RTC_EXPORT void rtcPreload(void);
  286. RTC_EXPORT void rtcCleanup(void);
  287. // SCTP global settings
  288. typedef struct {
  289. int recvBufferSize; // in bytes, <= 0 means optimized default
  290. int sendBufferSize; // in bytes, <= 0 means optimized default
  291. int maxChunksOnQueue; // in chunks, <= 0 means optimized default
  292. int initialCongestionWindow; // in MTUs, <= 0 means optimized default
  293. int maxBurst; // in MTUs, 0 means optimized default, < 0 means disabled
  294. int congestionControlModule; // 0: RFC2581 (default), 1: HSTCP, 2: H-TCP, 3: RTCC
  295. int delayedSackTimeMs; // in msecs, 0 means optimized default, < 0 means disabled
  296. int minRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  297. int maxRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  298. int initialRetransmitTimeoutMs; // in msecs, <= 0 means optimized default
  299. int maxRetransmitAttempts; // number of retransmissions, <= 0 means optimized default
  300. int heartbeatIntervalMs; // in msecs, <= 0 means optimized default
  301. } rtcSctpSettings;
  302. // Note: SCTP settings apply to newly-created PeerConnections only
  303. RTC_EXPORT int rtcSetSctpSettings(const rtcSctpSettings *settings);
  304. #ifdef __cplusplus
  305. } // extern "C"
  306. #endif
  307. #endif