rtc.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #else
  26. #define RTC_EXPORT
  27. #endif
  28. #ifdef CAPI_STDCALL
  29. #define RTC_API __stdcall
  30. #else
  31. #define RTC_API
  32. #endif
  33. #ifndef RTC_ENABLE_WEBSOCKET
  34. #define RTC_ENABLE_WEBSOCKET 1
  35. #endif
  36. #include <stdbool.h>
  37. #include <stdint.h>
  38. // libdatachannel C API
  39. typedef enum {
  40. RTC_NEW = 0,
  41. RTC_CONNECTING = 1,
  42. RTC_CONNECTED = 2,
  43. RTC_DISCONNECTED = 3,
  44. RTC_FAILED = 4,
  45. RTC_CLOSED = 5
  46. } rtcState;
  47. typedef enum {
  48. RTC_GATHERING_NEW = 0,
  49. RTC_GATHERING_INPROGRESS = 1,
  50. RTC_GATHERING_COMPLETE = 2
  51. } rtcGatheringState;
  52. typedef enum { // Don't change, it must match plog severity
  53. RTC_LOG_NONE = 0,
  54. RTC_LOG_FATAL = 1,
  55. RTC_LOG_ERROR = 2,
  56. RTC_LOG_WARNING = 3,
  57. RTC_LOG_INFO = 4,
  58. RTC_LOG_DEBUG = 5,
  59. RTC_LOG_VERBOSE = 6
  60. } rtcLogLevel;
  61. #define RTC_ERR_SUCCESS 0
  62. #define RTC_ERR_INVALID -1 // invalid argument
  63. #define RTC_ERR_FAILURE -2 // runtime error
  64. typedef struct {
  65. const char **iceServers;
  66. int iceServersCount;
  67. uint16_t portRangeBegin;
  68. uint16_t portRangeEnd;
  69. } rtcConfiguration;
  70. typedef struct {
  71. bool unordered;
  72. bool unreliable;
  73. unsigned int maxPacketLifeTime; // ignored if reliable
  74. unsigned int maxRetransmits; // ignored if reliable
  75. } rtcReliability;
  76. typedef void (RTC_API *rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  77. typedef void (RTC_API *rtcDescriptionCallbackFunc)(const char *sdp, const char *type, void *ptr);
  78. typedef void (RTC_API *rtcCandidateCallbackFunc)(const char *cand, const char *mid, void *ptr);
  79. typedef void (RTC_API *rtcStateChangeCallbackFunc)(rtcState state, void *ptr);
  80. typedef void (RTC_API *rtcGatheringStateCallbackFunc)(rtcGatheringState state, void *ptr);
  81. typedef void (RTC_API *rtcDataChannelCallbackFunc)(int dc, void *ptr);
  82. typedef void (RTC_API *rtcTrackCallbackFunc)(int tr, void *ptr);
  83. typedef void (RTC_API *rtcOpenCallbackFunc)(void *ptr);
  84. typedef void (RTC_API *rtcClosedCallbackFunc)(void *ptr);
  85. typedef void (RTC_API *rtcErrorCallbackFunc)(const char *error, void *ptr);
  86. typedef void (RTC_API *rtcMessageCallbackFunc)(const char *message, int size, void *ptr);
  87. typedef void (RTC_API *rtcBufferedAmountLowCallbackFunc)(void *ptr);
  88. typedef void (RTC_API *rtcAvailableCallbackFunc)(void *ptr);
  89. // Log
  90. // NULL cb on the first call will log to stdout
  91. RTC_EXPORT void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb);
  92. // User pointer
  93. RTC_EXPORT void rtcSetUserPointer(int id, void *ptr);
  94. // PeerConnection
  95. RTC_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  96. RTC_EXPORT int rtcDeletePeerConnection(int pc);
  97. RTC_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  98. RTC_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  99. RTC_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  100. RTC_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  101. RTC_EXPORT int rtcSetLocalDescription(int pc);
  102. RTC_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  103. RTC_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  104. RTC_EXPORT int rtcGetLocalDescriptionSdp(int pc, char *buffer, int size);
  105. RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
  106. RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
  107. // DataChannel
  108. RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  109. RTC_EXPORT int rtcAddDataChannel(int pc, const char *label); // returns dc id
  110. RTC_EXPORT int rtcAddDataChannelExt(int pc, const char *label, const char *protocol,
  111. const rtcReliability *reliability); // returns dc id
  112. // Equivalent to calling rtcAddDataChannel() and rtcSetLocalDescription()
  113. RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  114. RTC_EXPORT int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
  115. const rtcReliability *reliability); // returns dc id
  116. RTC_EXPORT int rtcDeleteDataChannel(int dc);
  117. RTC_EXPORT int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  118. RTC_EXPORT int rtcGetDataChannelProtocol(int dc, char *buffer, int size);
  119. RTC_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability);
  120. // Track
  121. RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
  122. RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
  123. RTC_EXPORT int rtcDeleteTrack(int tr);
  124. RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
  125. // WebSocket
  126. #if RTC_ENABLE_WEBSOCKET
  127. typedef struct {
  128. bool disableTlsVerification; // if true, don't verify the TLS certificate
  129. } rtcWsConfiguration;
  130. RTC_EXPORT int rtcCreateWebSocket(const char *url); // returns ws id
  131. RTC_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *config);
  132. RTC_EXPORT int rtcDeleteWebsocket(int ws);
  133. #endif
  134. // DataChannel, Track, and WebSocket common API
  135. RTC_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  136. RTC_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  137. RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  138. RTC_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  139. RTC_EXPORT int rtcSendMessage(int id, const char *data, int size);
  140. RTC_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
  141. RTC_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
  142. RTC_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  143. // DataChannel, Track, and WebSocket common extended API
  144. RTC_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
  145. RTC_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  146. RTC_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);
  147. // Optional preload and cleanup
  148. RTC_EXPORT void rtcPreload(void);
  149. RTC_EXPORT void rtcCleanup(void);
  150. #ifdef __cplusplus
  151. } // extern "C"
  152. #endif
  153. #endif