rtc.h 6.1 KB

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