rtc.h 11 KB

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