rtc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 ((uint16_t)(RTC_DEFAULT_MTU - 12 - 8 - 40)) // SRTP/UDP/IPv6
  43. #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512)
  44. #endif
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. // libdatachannel C API
  48. typedef enum {
  49. RTC_NEW = 0,
  50. RTC_CONNECTING = 1,
  51. RTC_CONNECTED = 2,
  52. RTC_DISCONNECTED = 3,
  53. RTC_FAILED = 4,
  54. RTC_CLOSED = 5
  55. } rtcState;
  56. typedef enum {
  57. RTC_GATHERING_NEW = 0,
  58. RTC_GATHERING_INPROGRESS = 1,
  59. RTC_GATHERING_COMPLETE = 2
  60. } rtcGatheringState;
  61. typedef enum {
  62. RTC_SIGNALING_STABLE = 0,
  63. RTC_SIGNALING_HAVE_LOCAL_OFFER = 1,
  64. RTC_SIGNALING_HAVE_REMOTE_OFFER = 2,
  65. RTC_SIGNALING_HAVE_LOCAL_PRANSWER = 3,
  66. RTC_SIGNALING_HAVE_REMOTE_PRANSWER = 4,
  67. } rtcSignalingState;
  68. typedef enum { // Don't change, it must match plog severity
  69. RTC_LOG_NONE = 0,
  70. RTC_LOG_FATAL = 1,
  71. RTC_LOG_ERROR = 2,
  72. RTC_LOG_WARNING = 3,
  73. RTC_LOG_INFO = 4,
  74. RTC_LOG_DEBUG = 5,
  75. RTC_LOG_VERBOSE = 6
  76. } rtcLogLevel;
  77. #if RTC_ENABLE_MEDIA
  78. typedef enum {
  79. // video
  80. RTC_CODEC_H264 = 0,
  81. RTC_CODEC_VP8 = 1,
  82. RTC_CODEC_VP9 = 2,
  83. // audio
  84. RTC_CODEC_OPUS = 128
  85. } rtcCodec;
  86. typedef enum {
  87. RTC_DIRECTION_UNKNOWN = 0,
  88. RTC_DIRECTION_SENDONLY = 1,
  89. RTC_DIRECTION_RECVONLY = 2,
  90. RTC_DIRECTION_SENDRECV = 3,
  91. RTC_DIRECTION_INACTIVE = 4
  92. } rtcDirection;
  93. #endif // RTC_ENABLE_MEDIA
  94. #define RTC_ERR_SUCCESS 0
  95. #define RTC_ERR_INVALID -1 // invalid argument
  96. #define RTC_ERR_FAILURE -2 // runtime error
  97. #define RTC_ERR_NOT_AVAIL -3 // element not available
  98. #define RTC_ERR_TOO_SMALL -4 // buffer too small
  99. typedef struct {
  100. const char **iceServers;
  101. int iceServersCount;
  102. bool enableIceTcp;
  103. uint16_t portRangeBegin;
  104. uint16_t portRangeEnd;
  105. int mtu; // <= 0 means automatic
  106. } rtcConfiguration;
  107. typedef struct {
  108. bool unordered;
  109. bool unreliable;
  110. int maxPacketLifeTime; // ignored if reliable
  111. int maxRetransmits; // ignored if reliable
  112. } rtcReliability;
  113. typedef struct {
  114. rtcReliability reliability;
  115. const char *protocol; // empty string if NULL
  116. bool negotiated;
  117. bool manualStream;
  118. uint16_t stream; // numeric ID 0-65534, ignored if manualStream is false
  119. } rtcDataChannelInit;
  120. typedef void(RTC_API *rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  121. typedef void(RTC_API *rtcDescriptionCallbackFunc)(int pc, const char *sdp, const char *type,
  122. void *ptr);
  123. typedef void(RTC_API *rtcCandidateCallbackFunc)(int pc, const char *cand, const char *mid,
  124. void *ptr);
  125. typedef void(RTC_API *rtcStateChangeCallbackFunc)(int pc, rtcState state, void *ptr);
  126. typedef void(RTC_API *rtcGatheringStateCallbackFunc)(int pc, rtcGatheringState state, void *ptr);
  127. typedef void(RTC_API *rtcSignalingStateCallbackFunc)(int pc, rtcSignalingState state, void *ptr);
  128. typedef void(RTC_API *rtcDataChannelCallbackFunc)(int pc, int dc, void *ptr);
  129. typedef void(RTC_API *rtcTrackCallbackFunc)(int pc, int tr, void *ptr);
  130. typedef void(RTC_API *rtcOpenCallbackFunc)(int id, void *ptr);
  131. typedef void(RTC_API *rtcClosedCallbackFunc)(int id, void *ptr);
  132. typedef void(RTC_API *rtcErrorCallbackFunc)(int id, const char *error, void *ptr);
  133. typedef void(RTC_API *rtcMessageCallbackFunc)(int id, const char *message, int size, void *ptr);
  134. typedef void(RTC_API *rtcBufferedAmountLowCallbackFunc)(int id, void *ptr);
  135. typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
  136. // Log
  137. // NULL cb on the first call will log to stdout
  138. RTC_EXPORT void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb);
  139. // User pointer
  140. RTC_EXPORT void rtcSetUserPointer(int id, void *ptr);
  141. RTC_EXPORT void * rtcGetUserPointer(int i);
  142. // PeerConnection
  143. RTC_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  144. RTC_EXPORT int rtcDeletePeerConnection(int pc);
  145. RTC_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  146. RTC_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  147. RTC_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  148. RTC_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  149. RTC_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCallbackFunc cb);
  150. RTC_EXPORT int rtcSetLocalDescription(int pc, const char *type);
  151. RTC_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  152. RTC_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  153. RTC_EXPORT int rtcGetLocalDescription(int pc, char *buffer, int size);
  154. RTC_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);
  155. RTC_EXPORT int rtcGetLocalDescriptionType(int pc, char *buffer, int size);
  156. RTC_EXPORT int rtcGetRemoteDescriptionType(int pc, char *buffer, int size);
  157. RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
  158. RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
  159. RTC_EXPORT int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote,
  160. int remoteSize);
  161. // DataChannel
  162. RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  163. RTC_EXPORT int rtcAddDataChannel(int pc, const char *label); // returns dc id
  164. RTC_EXPORT int rtcAddDataChannelEx(int pc, const char *label,
  165. const rtcDataChannelInit *init); // returns dc id
  166. // Equivalent to calling rtcAddDataChannel() and rtcSetLocalDescription()
  167. RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  168. RTC_EXPORT int rtcCreateDataChannelEx(int pc, const char *label,
  169. const rtcDataChannelInit *init); // returns dc id
  170. RTC_EXPORT int rtcDeleteDataChannel(int dc);
  171. RTC_EXPORT int rtcGetDataChannelStream(int dc);
  172. RTC_EXPORT int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  173. RTC_EXPORT int rtcGetDataChannelProtocol(int dc, char *buffer, int size);
  174. RTC_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability);
  175. // Track
  176. RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
  177. RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
  178. RTC_EXPORT int rtcDeleteTrack(int tr);
  179. RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
  180. // Media
  181. #if RTC_ENABLE_MEDIA
  182. /// Add track
  183. /// @param pc Peer connection id
  184. /// @param codec Codec
  185. /// @param payloadType Payload type
  186. /// @param ssrc SSRC
  187. /// @param _mid MID
  188. /// @param _direction Direction
  189. /// @param _name Name (optional)
  190. /// @param _msid MSID (optional)
  191. /// @param _trackID Track ID used in MSID (optional)
  192. /// @returns Track id
  193. RTC_EXPORT int rtcAddTrackEx(int pc, rtcCodec codec, int payloadType, uint32_t ssrc, const char *_mid, rtcDirection direction, const char *_name, const char *_msid, const char *_trackID);
  194. /// Set H264PacketizationHandler for track
  195. /// @param tr Track id
  196. /// @param ssrc SSRC
  197. /// @param cname CName
  198. /// @param payloadType Payload Type
  199. /// @param clockRate Clock rate
  200. /// @param maxFragmentSize Maximum NALU fragment size
  201. /// @param sequenceNumber Sequence number
  202. /// @param timestamp Timestamp
  203. RTC_EXPORT int rtcSetH264PacketizationHandler(int tr, uint32_t ssrc, const char * cname, uint8_t payloadType, uint32_t clockRate, uint16_t maxFragmentSize, uint16_t sequenceNumber, uint32_t timestamp);
  204. /// Set OpusPacketizationHandler for track
  205. /// @param tr Track id
  206. /// @param ssrc SSRC
  207. /// @param cname CName
  208. /// @param payloadType Payload Type
  209. /// @param clockRate Clock rate
  210. /// @param _sequenceNumber Sequence number
  211. /// @param _timestamp Timestamp
  212. RTC_EXPORT int rtcSetOpusPacketizationHandler(int tr, uint32_t ssrc, const char * cname, uint8_t payloadType, uint32_t clockRate, uint16_t _sequenceNumber, uint32_t _timestamp);
  213. /// Chain RtcpSrReporter to handler chain for given track
  214. /// @param tr Track id
  215. int rtcChainRtcpSrReporter(int tr);
  216. /// Chain RtcpNackResponder to handler chain for given track
  217. /// @param tr Track id
  218. /// @param maxStoredPacketsCount Maximum stored packet count
  219. int rtcChainRtcpNackResponder(int tr, unsigned maxStoredPacketsCount);
  220. /// Set start time for RTP stream
  221. /// @param startTime_s Start time in seconds
  222. /// @param timeIntervalSince1970 Set true if `startTime_s` is time interval since 1970, false if `startTime_s` is time interval since 1900
  223. /// @param _timestamp Start timestamp
  224. int rtcSetRtpConfigurationStartTime(int id, double startTime_s, bool timeIntervalSince1970, uint32_t _timestamp);
  225. /// Start stats recording for RTCP Sender Reporter
  226. /// @param id Track identifier
  227. int rtcStartRtcpSenderReporterRecording(int id);
  228. /// Transform seconds to timestamp using track's clock rate
  229. /// @param id Track id
  230. /// @param seconds Seconds
  231. /// @param timestamp Pointer to result
  232. int rtcTransformSecondsToTimestamp(int id, double seconds, uint32_t * timestamp);
  233. /// Transform timestamp to seconds using track's clock rate
  234. /// @param id Track id
  235. /// @param timestamp Timestamp
  236. /// @param seconds Pointer for result
  237. int rtcTransformTimestampToSeconds(int id, uint32_t timestamp, double * seconds);
  238. /// Get current timestamp
  239. /// @param id Track id
  240. /// @param timestamp Pointer for result
  241. int rtcGetCurrentTrackTimestamp(int id, uint32_t * timestamp);
  242. /// Get start timestamp for track identified by given id
  243. /// @param id Track id
  244. /// @param timestamp Pointer for result
  245. int rtcGetTrackStartTimestamp(int id, uint32_t * timestamp);
  246. /// Set RTP timestamp for track identified by given id
  247. /// @param id Track id
  248. /// @param timestamp New timestamp
  249. int rtcSetTrackRTPTimestamp(int id, uint32_t timestamp);
  250. /// Get timestamp of previous RTCP SR
  251. /// @param id Track id
  252. /// @param timestamp Pointer for result
  253. int rtcGetPreviousTrackSenderReportTimestamp(int id, uint32_t * timestamp);
  254. /// Set `NeedsToReport` flag in RtcpSrReporter handler identified by given track id
  255. /// @param id Track id
  256. int rtcSetNeedsToSendRtcpSr(int id);
  257. #endif // RTC_ENABLE_MEDIA
  258. // WebSocket
  259. #if RTC_ENABLE_WEBSOCKET
  260. typedef struct {
  261. bool disableTlsVerification; // if true, don't verify the TLS certificate
  262. } rtcWsConfiguration;
  263. RTC_EXPORT int rtcCreateWebSocket(const char *url); // returns ws id
  264. RTC_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *config);
  265. RTC_EXPORT int rtcDeleteWebsocket(int ws);
  266. #endif
  267. // DataChannel, Track, and WebSocket common API
  268. RTC_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  269. RTC_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  270. RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  271. RTC_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  272. RTC_EXPORT int rtcSendMessage(int id, const char *data, int size);
  273. RTC_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
  274. RTC_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
  275. RTC_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  276. // DataChannel, Track, and WebSocket common extended API
  277. RTC_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
  278. RTC_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  279. RTC_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);
  280. // Optional preload and cleanup
  281. RTC_EXPORT void rtcPreload(void);
  282. RTC_EXPORT void rtcCleanup(void);
  283. #ifdef __cplusplus
  284. } // extern "C"
  285. #endif
  286. #endif