rtc.h 4.3 KB

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