rtc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #if RTC_ENABLE_MEDIA
  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. } rtcCodec;
  92. typedef enum {
  93. RTC_DIRECTION_UNKNOWN = 0,
  94. RTC_DIRECTION_SENDONLY = 1,
  95. RTC_DIRECTION_RECVONLY = 2,
  96. RTC_DIRECTION_SENDRECV = 3,
  97. RTC_DIRECTION_INACTIVE = 4
  98. } rtcDirection;
  99. #endif // RTC_ENABLE_MEDIA
  100. #define RTC_ERR_SUCCESS 0
  101. #define RTC_ERR_INVALID -1 // invalid argument
  102. #define RTC_ERR_FAILURE -2 // runtime error
  103. #define RTC_ERR_NOT_AVAIL -3 // element not available
  104. #define RTC_ERR_TOO_SMALL -4 // buffer too small
  105. typedef struct {
  106. const char **iceServers;
  107. int iceServersCount;
  108. rtcCertificateType certificateType;
  109. bool enableIceTcp;
  110. bool disableAutoNegotiation;
  111. uint16_t portRangeBegin;
  112. uint16_t portRangeEnd;
  113. int mtu; // <= 0 means automatic
  114. int maxMessageSize; // <= 0 means default
  115. } rtcConfiguration;
  116. typedef struct {
  117. bool unordered;
  118. bool unreliable;
  119. int maxPacketLifeTime; // ignored if reliable
  120. int maxRetransmits; // ignored if reliable
  121. } rtcReliability;
  122. typedef struct {
  123. rtcReliability reliability;
  124. const char *protocol; // empty string if NULL
  125. bool negotiated;
  126. bool manualStream;
  127. uint16_t stream; // numeric ID 0-65534, ignored if manualStream is false
  128. } rtcDataChannelInit;
  129. typedef void(RTC_API *rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  130. typedef void(RTC_API *rtcDescriptionCallbackFunc)(int pc, const char *sdp, const char *type,
  131. void *ptr);
  132. typedef void(RTC_API *rtcCandidateCallbackFunc)(int pc, const char *cand, const char *mid,
  133. void *ptr);
  134. typedef void(RTC_API *rtcStateChangeCallbackFunc)(int pc, rtcState state, void *ptr);
  135. typedef void(RTC_API *rtcGatheringStateCallbackFunc)(int pc, rtcGatheringState state, void *ptr);
  136. typedef void(RTC_API *rtcSignalingStateCallbackFunc)(int pc, rtcSignalingState state, void *ptr);
  137. typedef void(RTC_API *rtcDataChannelCallbackFunc)(int pc, int dc, void *ptr);
  138. typedef void(RTC_API *rtcTrackCallbackFunc)(int pc, int tr, void *ptr);
  139. typedef void(RTC_API *rtcOpenCallbackFunc)(int id, void *ptr);
  140. typedef void(RTC_API *rtcClosedCallbackFunc)(int id, void *ptr);
  141. typedef void(RTC_API *rtcErrorCallbackFunc)(int id, const char *error, void *ptr);
  142. typedef void(RTC_API *rtcMessageCallbackFunc)(int id, const char *message, int size, void *ptr);
  143. typedef void(RTC_API *rtcBufferedAmountLowCallbackFunc)(int id, void *ptr);
  144. typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
  145. // Log
  146. // NULL cb on the first call will log to stdout
  147. RTC_EXPORT void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb);
  148. // User pointer
  149. RTC_EXPORT void rtcSetUserPointer(int id, void *ptr);
  150. RTC_EXPORT void *rtcGetUserPointer(int i);
  151. // PeerConnection
  152. RTC_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  153. RTC_EXPORT int rtcDeletePeerConnection(int pc);
  154. RTC_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  155. RTC_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  156. RTC_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  157. RTC_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  158. RTC_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCallbackFunc cb);
  159. RTC_EXPORT int rtcSetLocalDescription(int pc, const char *type);
  160. RTC_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  161. RTC_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  162. RTC_EXPORT int rtcGetLocalDescription(int pc, char *buffer, int size);
  163. RTC_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);
  164. RTC_EXPORT int rtcGetLocalDescriptionType(int pc, char *buffer, int size);
  165. RTC_EXPORT int rtcGetRemoteDescriptionType(int pc, char *buffer, int size);
  166. RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
  167. RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
  168. RTC_EXPORT int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote,
  169. int remoteSize);
  170. // DataChannel
  171. RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  172. RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  173. RTC_EXPORT int rtcCreateDataChannelEx(int pc, const char *label,
  174. const rtcDataChannelInit *init); // returns dc id
  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. RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
  182. RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
  183. RTC_EXPORT int rtcDeleteTrack(int tr);
  184. RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
  185. // SCTP settings
  186. typedef struct {
  187. int recvBufferSize; // <= 0 means optimized default
  188. int sendBufferSize; // <= 0 means optimized default
  189. int maxChunksOnQueue; // <= 0 means optimized default
  190. int initialCongestionWindow; // <= 0 means optimized default
  191. int maxBurst; // 0 means optimized default, < 0 means disabled
  192. int congestionControlModule; // <= 0 means default (0: RFC2581, 1: HSTCP, 2: H-TCP, 3: RTCC)
  193. int delayedSackTimeMs; // <= 0 means optimized default
  194. } rtcSctpSettings;
  195. // Note: SCTP settings apply to newly-created PeerConnections only
  196. RTC_EXPORT int rtcSetSctpSettings(const rtcSctpSettings *settings);
  197. // Media
  198. #if RTC_ENABLE_MEDIA
  199. /// Add track
  200. /// @param pc Peer connection id
  201. /// @param codec Codec
  202. /// @param payloadType Payload type
  203. /// @param ssrc SSRC
  204. /// @param _mid MID
  205. /// @param _direction Direction
  206. /// @param _name Name (optional)
  207. /// @param _msid MSID (optional)
  208. /// @param _trackID Track ID used in MSID (optional)
  209. /// @returns Track id
  210. RTC_EXPORT int rtcAddTrackEx(int pc, rtcCodec codec, int payloadType, uint32_t ssrc,
  211. const char *_mid, rtcDirection direction, const char *_name,
  212. const char *_msid, const char *_trackID);
  213. /// Set H264PacketizationHandler for track
  214. /// @param tr Track id
  215. /// @param ssrc SSRC
  216. /// @param cname CName
  217. /// @param payloadType Payload Type
  218. /// @param clockRate Clock rate
  219. /// @param maxFragmentSize Maximum NALU fragment size
  220. /// @param sequenceNumber Sequence number
  221. /// @param timestamp Timestamp
  222. RTC_EXPORT int rtcSetH264PacketizationHandler(int tr, uint32_t ssrc, const char *cname,
  223. uint8_t payloadType, uint32_t clockRate,
  224. uint16_t maxFragmentSize, uint16_t sequenceNumber,
  225. uint32_t timestamp);
  226. /// Set OpusPacketizationHandler for track
  227. /// @param tr Track id
  228. /// @param ssrc SSRC
  229. /// @param cname CName
  230. /// @param payloadType Payload Type
  231. /// @param clockRate Clock rate
  232. /// @param _sequenceNumber Sequence number
  233. /// @param _timestamp Timestamp
  234. RTC_EXPORT int rtcSetOpusPacketizationHandler(int tr, uint32_t ssrc, const char *cname,
  235. uint8_t payloadType, uint32_t clockRate,
  236. uint16_t _sequenceNumber, uint32_t _timestamp);
  237. /// Chain RtcpSrReporter to handler chain for given track
  238. /// @param tr Track id
  239. int rtcChainRtcpSrReporter(int tr);
  240. /// Chain RtcpNackResponder to handler chain for given track
  241. /// @param tr Track id
  242. /// @param maxStoredPacketsCount Maximum stored packet count
  243. int rtcChainRtcpNackResponder(int tr, unsigned int maxStoredPacketsCount);
  244. /// Set start time for RTP stream
  245. /// @param startTime_s Start time in seconds
  246. /// @param timeIntervalSince1970 Set true if `startTime_s` is time interval since 1970, false if
  247. /// `startTime_s` is time interval since 1900
  248. /// @param _timestamp Start timestamp
  249. int rtcSetRtpConfigurationStartTime(int id, double startTime_s, bool timeIntervalSince1970,
  250. uint32_t _timestamp);
  251. /// Start stats recording for RTCP Sender Reporter
  252. /// @param id Track identifier
  253. int rtcStartRtcpSenderReporterRecording(int id);
  254. /// Transform seconds to timestamp using track's clock rate
  255. /// @param id Track id
  256. /// @param seconds Seconds
  257. /// @param timestamp Pointer to result
  258. int rtcTransformSecondsToTimestamp(int id, double seconds, uint32_t *timestamp);
  259. /// Transform timestamp to seconds using track's clock rate
  260. /// @param id Track id
  261. /// @param timestamp Timestamp
  262. /// @param seconds Pointer for result
  263. int rtcTransformTimestampToSeconds(int id, uint32_t timestamp, double *seconds);
  264. /// Get current timestamp
  265. /// @param id Track id
  266. /// @param timestamp Pointer for result
  267. int rtcGetCurrentTrackTimestamp(int id, uint32_t *timestamp);
  268. /// Get start timestamp for track identified by given id
  269. /// @param id Track id
  270. /// @param timestamp Pointer for result
  271. int rtcGetTrackStartTimestamp(int id, uint32_t *timestamp);
  272. /// Set RTP timestamp for track identified by given id
  273. /// @param id Track id
  274. /// @param timestamp New timestamp
  275. int rtcSetTrackRTPTimestamp(int id, uint32_t timestamp);
  276. /// Get timestamp of previous RTCP SR
  277. /// @param id Track id
  278. /// @param timestamp Pointer for result
  279. int rtcGetPreviousTrackSenderReportTimestamp(int id, uint32_t *timestamp);
  280. /// Set `NeedsToReport` flag in RtcpSrReporter handler identified by given track id
  281. /// @param id Track id
  282. int rtcSetNeedsToSendRtcpSr(int id);
  283. #endif // RTC_ENABLE_MEDIA
  284. // WebSocket
  285. #if RTC_ENABLE_WEBSOCKET
  286. typedef struct {
  287. bool disableTlsVerification; // if true, don't verify the TLS certificate
  288. } rtcWsConfiguration;
  289. RTC_EXPORT int rtcCreateWebSocket(const char *url); // returns ws id
  290. RTC_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *config);
  291. RTC_EXPORT int rtcDeleteWebsocket(int ws);
  292. #endif
  293. // DataChannel, Track, and WebSocket common API
  294. RTC_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  295. RTC_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  296. RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  297. RTC_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  298. RTC_EXPORT int rtcSendMessage(int id, const char *data, int size);
  299. RTC_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
  300. RTC_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
  301. RTC_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  302. // DataChannel, Track, and WebSocket common extended API
  303. RTC_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
  304. RTC_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  305. RTC_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);
  306. // Optional preload and cleanup
  307. RTC_EXPORT void rtcPreload(void);
  308. RTC_EXPORT void rtcCleanup(void);
  309. #ifdef __cplusplus
  310. } // extern "C"
  311. #endif
  312. #endif