rtc.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <stdint.h>
  24. // libdatachannel C API
  25. #ifndef RTC_ENABLE_WEBSOCKET
  26. #define RTC_ENABLE_WEBSOCKET 1
  27. #endif
  28. typedef enum {
  29. RTC_NEW = 0,
  30. RTC_CONNECTING = 1,
  31. RTC_CONNECTED = 2,
  32. RTC_DISCONNECTED = 3,
  33. RTC_FAILED = 4,
  34. RTC_CLOSED = 5
  35. } rtcState;
  36. typedef enum {
  37. RTC_GATHERING_NEW = 0,
  38. RTC_GATHERING_INPROGRESS = 1,
  39. RTC_GATHERING_COMPLETE = 2
  40. } rtcGatheringState;
  41. typedef enum { // Don't change, it must match plog severity
  42. RTC_LOG_NONE = 0,
  43. RTC_LOG_FATAL = 1,
  44. RTC_LOG_ERROR = 2,
  45. RTC_LOG_WARNING = 3,
  46. RTC_LOG_INFO = 4,
  47. RTC_LOG_DEBUG = 5,
  48. RTC_LOG_VERBOSE = 6
  49. } rtcLogLevel;
  50. typedef struct {
  51. const char **iceServers;
  52. int iceServersCount;
  53. uint16_t portRangeBegin;
  54. uint16_t portRangeEnd;
  55. } rtcConfiguration;
  56. typedef void (*dataChannelCallbackFunc)(int dc, void *ptr);
  57. typedef void (*descriptionCallbackFunc)(const char *sdp, const char *type, void *ptr);
  58. typedef void (*candidateCallbackFunc)(const char *cand, const char *mid, void *ptr);
  59. typedef void (*stateChangeCallbackFunc)(rtcState state, void *ptr);
  60. typedef void (*gatheringStateCallbackFunc)(rtcGatheringState state, void *ptr);
  61. typedef void (*openCallbackFunc)(void *ptr);
  62. typedef void (*closedCallbackFunc)(void *ptr);
  63. typedef void (*errorCallbackFunc)(const char *error, void *ptr);
  64. typedef void (*messageCallbackFunc)(const char *message, int size, void *ptr);
  65. typedef void (*bufferedAmountLowCallbackFunc)(void *ptr);
  66. typedef void (*availableCallbackFunc)(void *ptr);
  67. // Log
  68. void rtcInitLogger(rtcLogLevel level);
  69. // User pointer
  70. void rtcSetUserPointer(int id, void *ptr);
  71. // PeerConnection
  72. int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  73. int rtcDeletePeerConnection(int pc);
  74. int rtcSetDataChannelCallback(int pc, dataChannelCallbackFunc cb);
  75. int rtcSetLocalDescriptionCallback(int pc, descriptionCallbackFunc cb);
  76. int rtcSetLocalCandidateCallback(int pc, candidateCallbackFunc cb);
  77. int rtcSetStateChangeCallback(int pc, stateChangeCallbackFunc cb);
  78. int rtcSetGatheringStateChangeCallback(int pc, gatheringStateCallbackFunc cb);
  79. int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  80. int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  81. int rtcGetLocalAddress(int pc, char *buffer, int size);
  82. int rtcGetRemoteAddress(int pc, char *buffer, int size);
  83. // DataChannel
  84. int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  85. int rtcDeleteDataChannel(int dc);
  86. int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  87. // WebSocket
  88. #if RTC_ENABLE_WEBSOCKET
  89. int rtcCreateWebSocket(const char *url); // returns ws id
  90. int rtcDeleteWebsocket(int ws);
  91. #endif
  92. // DataChannel and WebSocket common API
  93. int rtcSetOpenCallback(int id, openCallbackFunc cb);
  94. int rtcSetClosedCallback(int id, closedCallbackFunc cb);
  95. int rtcSetErrorCallback(int id, errorCallbackFunc cb);
  96. int rtcSetMessageCallback(int id, messageCallbackFunc cb);
  97. int rtcSendMessage(int id, const char *data, int size);
  98. int rtcGetBufferedAmount(int id); // total size buffered to send
  99. int rtcSetBufferedAmountLowThreshold(int id, int amount);
  100. int rtcSetBufferedAmountLowCallback(int id, bufferedAmountLowCallbackFunc cb);
  101. // DataChannel and WebSocket common extended API
  102. int rtcGetAvailableAmount(int id); // total size available to receive
  103. int rtcSetAvailableCallback(int id, availableCallbackFunc cb);
  104. int rtcReceiveMessage(int id, char *buffer, int *size);
  105. // Cleanup
  106. void rtcCleanup();
  107. #ifdef __cplusplus
  108. } // extern "C"
  109. #endif
  110. #endif