rtc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define RTC_ERR_SUCCESS 0
  54. #define RTC_ERR_INVALID -1 // invalid argument
  55. #define RTC_ERR_FAILURE -2 // runtime error
  56. typedef struct {
  57. const char **iceServers;
  58. int iceServersCount;
  59. uint16_t portRangeBegin;
  60. uint16_t portRangeEnd;
  61. } rtcConfiguration;
  62. typedef void (*rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
  63. typedef void (*rtcDataChannelCallbackFunc)(int dc, void *ptr);
  64. typedef void (*rtcDescriptionCallbackFunc)(const char *sdp, const char *type, void *ptr);
  65. typedef void (*rtcCandidateCallbackFunc)(const char *cand, const char *mid, void *ptr);
  66. typedef void (*rtcStateChangeCallbackFunc)(rtcState state, void *ptr);
  67. typedef void (*rtcGatheringStateCallbackFunc)(rtcGatheringState state, void *ptr);
  68. typedef void (*rtcOpenCallbackFunc)(void *ptr);
  69. typedef void (*rtcClosedCallbackFunc)(void *ptr);
  70. typedef void (*rtcErrorCallbackFunc)(const char *error, void *ptr);
  71. typedef void (*rtcMessageCallbackFunc)(const char *message, int size, void *ptr);
  72. typedef void (*rtcBufferedAmountLowCallbackFunc)(void *ptr);
  73. typedef void (*rtcAvailableCallbackFunc)(void *ptr);
  74. // Log
  75. void rtcInitLogger(rtcLogLevel level, rtcLogCallbackFunc cb); // NULL cb to log to stdout
  76. // User pointer
  77. void rtcSetUserPointer(int id, void *ptr);
  78. // PeerConnection
  79. int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
  80. int rtcDeletePeerConnection(int pc);
  81. int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
  82. int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
  83. int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
  84. int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
  85. int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
  86. int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
  87. int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
  88. int rtcGetLocalAddress(int pc, char *buffer, int size);
  89. int rtcGetRemoteAddress(int pc, char *buffer, int size);
  90. // DataChannel
  91. int rtcCreateDataChannel(int pc, const char *label); // returns dc id
  92. int rtcDeleteDataChannel(int dc);
  93. int rtcGetDataChannelLabel(int dc, char *buffer, int size);
  94. // WebSocket
  95. #if RTC_ENABLE_WEBSOCKET
  96. int rtcCreateWebSocket(const char *url); // returns ws id
  97. int rtcDeleteWebsocket(int ws);
  98. #endif
  99. // DataChannel and WebSocket common API
  100. int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
  101. int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
  102. int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
  103. int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
  104. int rtcSendMessage(int id, const char *data, int size);
  105. int rtcGetBufferedAmount(int id); // total size buffered to send
  106. int rtcSetBufferedAmountLowThreshold(int id, int amount);
  107. int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
  108. // DataChannel and WebSocket common extended API
  109. int rtcGetAvailableAmount(int id); // total size available to receive
  110. int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
  111. int rtcReceiveMessage(int id, char *buffer, int *size);
  112. // Optional preload and cleanup
  113. void rtcPreload();
  114. void rtcCleanup();
  115. #ifdef __cplusplus
  116. } // extern "C"
  117. #endif
  118. #endif