capi_connectivity.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * Copyright (c) 2020 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. #include <rtc/rtc.h>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #ifdef _WIN32
  23. #include <windows.h>
  24. static void sleep(unsigned int secs) { Sleep(secs * 1000); }
  25. #else
  26. #include <unistd.h> // for sleep
  27. #endif
  28. #define BUFFER_SIZE 4096
  29. typedef struct {
  30. rtcState state;
  31. rtcGatheringState gatheringState;
  32. rtcSignalingState signalingState;
  33. int pc;
  34. int dc;
  35. bool connected;
  36. } Peer;
  37. static Peer *peer1 = NULL;
  38. static Peer *peer2 = NULL;
  39. static void RTC_API descriptionCallback(int pc, const char *sdp, const char *type, void *ptr) {
  40. Peer *peer = (Peer *)ptr;
  41. printf("Description %d:\n%s\n", peer == peer1 ? 1 : 2, sdp);
  42. Peer *other = peer == peer1 ? peer2 : peer1;
  43. rtcSetRemoteDescription(other->pc, sdp, type);
  44. }
  45. static void RTC_API candidateCallback(int pc, const char *cand, const char *mid, void *ptr) {
  46. Peer *peer = (Peer *)ptr;
  47. printf("Candidate %d: %s\n", peer == peer1 ? 1 : 2, cand);
  48. Peer *other = peer == peer1 ? peer2 : peer1;
  49. rtcAddRemoteCandidate(other->pc, cand, mid);
  50. }
  51. static void RTC_API stateChangeCallback(int pc, rtcState state, void *ptr) {
  52. Peer *peer = (Peer *)ptr;
  53. peer->state = state;
  54. printf("State %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
  55. }
  56. static void RTC_API gatheringStateCallback(int pc, rtcGatheringState state, void *ptr) {
  57. Peer *peer = (Peer *)ptr;
  58. peer->gatheringState = state;
  59. printf("Gathering state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
  60. }
  61. static void RTC_API signalingStateCallback(int pc, rtcSignalingState state, void *ptr) {
  62. Peer *peer = (Peer *)ptr;
  63. peer->signalingState = state;
  64. printf("Signaling state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
  65. }
  66. static void RTC_API openCallback(int id, void *ptr) {
  67. Peer *peer = (Peer *)ptr;
  68. peer->connected = true;
  69. printf("DataChannel %d: Open\n", peer == peer1 ? 1 : 2);
  70. if (!rtcIsOpen(id)) {
  71. fprintf(stderr, "rtcIsOpen failed\n");
  72. return;
  73. }
  74. if (rtcIsClosed(id)) {
  75. fprintf(stderr, "rtcIsClosed failed\n");
  76. return;
  77. }
  78. const char *message = peer == peer1 ? "Hello from 1" : "Hello from 2";
  79. rtcSendMessage(peer->dc, message, -1); // negative size indicates a null-terminated string
  80. }
  81. static void RTC_API closedCallback(int id, void *ptr) {
  82. Peer *peer = (Peer *)ptr;
  83. peer->connected = false;
  84. printf("DataChannel %d: Closed\n", peer == peer1 ? 1 : 2);
  85. }
  86. static void RTC_API messageCallback(int id, const char *message, int size, void *ptr) {
  87. Peer *peer = (Peer *)ptr;
  88. if (size < 0) { // negative size indicates a null-terminated string
  89. printf("Message %d: %s\n", peer == peer1 ? 1 : 2, message);
  90. } else {
  91. printf("Message %d: [binary of size %d]\n", peer == peer1 ? 1 : 2, size);
  92. }
  93. }
  94. static void RTC_API dataChannelCallback(int pc, int dc, void *ptr) {
  95. Peer *peer = (Peer *)ptr;
  96. char label[256];
  97. if (rtcGetDataChannelLabel(dc, label, 256) < 0) {
  98. fprintf(stderr, "rtcGetDataChannelLabel failed\n");
  99. return;
  100. }
  101. char protocol[256];
  102. if (rtcGetDataChannelProtocol(dc, protocol, 256) < 0) {
  103. fprintf(stderr, "rtcGetDataChannelProtocol failed\n");
  104. return;
  105. }
  106. rtcReliability reliability;
  107. if (rtcGetDataChannelReliability(dc, &reliability) < 0) {
  108. fprintf(stderr, "rtcGetDataChannelReliability failed\n");
  109. return;
  110. }
  111. printf("DataChannel %d: Received with label \"%s\" and protocol \"%s\"\n",
  112. peer == peer1 ? 1 : 2, label, protocol);
  113. if (strcmp(label, "test") != 0) {
  114. fprintf(stderr, "Wrong DataChannel label\n");
  115. return;
  116. }
  117. if (strcmp(protocol, "protocol") != 0) {
  118. fprintf(stderr, "Wrong DataChannel protocol\n");
  119. return;
  120. }
  121. if (reliability.unordered == false) {
  122. fprintf(stderr, "Wrong DataChannel reliability\n");
  123. return;
  124. }
  125. rtcSetOpenCallback(dc, openCallback);
  126. rtcSetClosedCallback(dc, closedCallback);
  127. rtcSetMessageCallback(dc, messageCallback);
  128. peer->dc = dc;
  129. }
  130. static Peer *createPeer(const rtcConfiguration *config) {
  131. Peer *peer = (Peer *)malloc(sizeof(Peer));
  132. if (!peer)
  133. return nullptr;
  134. memset(peer, 0, sizeof(Peer));
  135. // Create peer connection
  136. peer->pc = rtcCreatePeerConnection(config);
  137. rtcSetUserPointer(peer->pc, peer);
  138. rtcSetDataChannelCallback(peer->pc, dataChannelCallback);
  139. rtcSetLocalDescriptionCallback(peer->pc, descriptionCallback);
  140. rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
  141. rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
  142. rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
  143. rtcSetSignalingStateChangeCallback(peer->pc, signalingStateCallback);
  144. return peer;
  145. }
  146. static void deletePeer(Peer *peer) {
  147. if (peer) {
  148. if (peer->dc)
  149. rtcDeleteDataChannel(peer->dc);
  150. if (peer->pc)
  151. rtcDeletePeerConnection(peer->pc);
  152. free(peer);
  153. }
  154. }
  155. int test_capi_connectivity_main() {
  156. int attempts;
  157. rtcInitLogger(RTC_LOG_DEBUG, nullptr);
  158. if (rtcIsOpen(666)) {
  159. fprintf(stderr, "rtcIsOpen for invalid channel id failed\n");
  160. return -1;
  161. }
  162. if (rtcIsClosed(666)) {
  163. fprintf(stderr, "rtcIsOpen for invalid channel id failed\n");
  164. return -1;
  165. }
  166. // STUN server example (not necessary to connect locally)
  167. const char *iceServers[1] = {"stun:stun.l.google.com:19302"};
  168. // Create peer 1
  169. rtcConfiguration config1;
  170. memset(&config1, 0, sizeof(config1));
  171. config1.iceServers = iceServers;
  172. config1.iceServersCount = 1;
  173. // Custom MTU example
  174. config1.mtu = 1500;
  175. peer1 = createPeer(&config1);
  176. if (!peer1)
  177. goto error;
  178. // Create peer 2
  179. rtcConfiguration config2;
  180. memset(&config2, 0, sizeof(config2));
  181. // STUN server example (not necessary to connect locally)
  182. // Please do not use outside of libdatachannel tests
  183. config2.iceServers = iceServers;
  184. config2.iceServersCount = 1;
  185. // Custom MTU example
  186. config2.mtu = 1500;
  187. // Port range example
  188. config2.portRangeBegin = 5000;
  189. config2.portRangeEnd = 6000;
  190. peer2 = createPeer(&config2);
  191. if (!peer2)
  192. goto error;
  193. // Peer 1: Create data channel
  194. rtcDataChannelInit init;
  195. memset(&init, 0, sizeof(init));
  196. init.protocol = "protocol";
  197. init.reliability.unordered = true;
  198. peer1->dc = rtcCreateDataChannelEx(peer1->pc, "test", &init);
  199. rtcSetOpenCallback(peer1->dc, openCallback);
  200. rtcSetClosedCallback(peer1->dc, closedCallback);
  201. rtcSetMessageCallback(peer1->dc, messageCallback);
  202. attempts = 10;
  203. while ((!peer2->connected || !peer1->connected) && attempts--)
  204. sleep(1);
  205. if (peer1->state != RTC_CONNECTED || peer2->state != RTC_CONNECTED) {
  206. fprintf(stderr, "PeerConnection is not connected\n");
  207. goto error;
  208. }
  209. if (!peer1->connected || !peer2->connected) {
  210. fprintf(stderr, "DataChannel is not connected\n");
  211. goto error;
  212. }
  213. char buffer[BUFFER_SIZE];
  214. char buffer2[BUFFER_SIZE];
  215. if (rtcGetLocalDescriptionType(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  216. fprintf(stderr, "rtcGetLocalDescriptionType failed\n");
  217. goto error;
  218. }
  219. printf("Local description type 1: %s\n", buffer);
  220. if (rtcGetLocalDescription(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  221. fprintf(stderr, "rtcGetLocalDescription failed\n");
  222. goto error;
  223. }
  224. printf("Local description 1: %s\n", buffer);
  225. if (rtcGetRemoteDescriptionType(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  226. fprintf(stderr, "rtcGetRemoteDescriptionType failed\n");
  227. goto error;
  228. }
  229. printf("Remote description type 1: %s\n", buffer);
  230. if (rtcGetRemoteDescription(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  231. fprintf(stderr, "rtcGetRemoteDescription failed\n");
  232. goto error;
  233. }
  234. printf("Remote description 1: %s\n", buffer);
  235. if (rtcGetLocalDescriptionType(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  236. fprintf(stderr, "rtcGetLocalDescriptionType failed\n");
  237. goto error;
  238. }
  239. printf("Local description type 2: %s\n", buffer);
  240. if (rtcGetLocalDescription(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  241. fprintf(stderr, "rtcGetLocalDescription failed\n");
  242. goto error;
  243. }
  244. printf("Local description 2: %s\n", buffer);
  245. if (rtcGetRemoteDescriptionType(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  246. fprintf(stderr, "rtcGetRemoteDescriptionType failed\n");
  247. goto error;
  248. }
  249. printf("Remote description type 2: %s\n", buffer);
  250. if (rtcGetRemoteDescription(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  251. fprintf(stderr, "rtcGetRemoteDescription failed\n");
  252. goto error;
  253. }
  254. printf("Remote description 2: %s\n", buffer);
  255. if (rtcGetLocalAddress(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  256. fprintf(stderr, "rtcGetLocalAddress failed\n");
  257. goto error;
  258. }
  259. printf("Local address 1: %s\n", buffer);
  260. if (rtcGetRemoteAddress(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  261. fprintf(stderr, "rtcGetRemoteAddress failed\n");
  262. goto error;
  263. }
  264. printf("Remote address 1: %s\n", buffer);
  265. if (rtcGetLocalAddress(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  266. fprintf(stderr, "rtcGetLocalAddress failed\n");
  267. goto error;
  268. }
  269. printf("Local address 2: %s\n", buffer);
  270. if (rtcGetRemoteAddress(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  271. fprintf(stderr, "rtcGetRemoteAddress failed\n");
  272. goto error;
  273. }
  274. printf("Remote address 2: %s\n", buffer);
  275. if (rtcGetSelectedCandidatePair(peer1->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) {
  276. fprintf(stderr, "rtcGetSelectedCandidatePair failed\n");
  277. goto error;
  278. }
  279. printf("Local candidate 1: %s\n", buffer);
  280. printf("Remote candidate 1: %s\n", buffer2);
  281. if (rtcGetSelectedCandidatePair(peer2->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) {
  282. fprintf(stderr, "rtcGetSelectedCandidatePair failed\n");
  283. goto error;
  284. }
  285. printf("Local candidate 2: %s\n", buffer);
  286. printf("Remote candidate 2: %s\n", buffer2);
  287. if (rtcGetMaxDataChannelStream(peer1->pc) <= 0 || rtcGetMaxDataChannelStream(peer2->pc) <= 0) {
  288. fprintf(stderr, "rtcGetMaxDataChannelStream failed\n");
  289. goto error;
  290. }
  291. rtcClose(peer1->dc); // optional
  292. rtcClosePeerConnection(peer1->pc); // optional
  293. deletePeer(peer1);
  294. sleep(1);
  295. deletePeer(peer2);
  296. sleep(1);
  297. printf("Success\n");
  298. return 0;
  299. error:
  300. deletePeer(peer1);
  301. deletePeer(peer2);
  302. return -1;
  303. }
  304. #include <stdexcept>
  305. void test_capi_connectivity() {
  306. if (test_capi_connectivity_main())
  307. throw std::runtime_error("Connection failed");
  308. }