capi_connectivity.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. // Please do not use outside of libdatachannel tests
  168. const char *iceServers[1] = {"stun:stun.ageneau.net:3478"};
  169. // Create peer 1
  170. rtcConfiguration config1;
  171. memset(&config1, 0, sizeof(config1));
  172. config1.iceServers = iceServers;
  173. config1.iceServersCount = 1;
  174. // Custom MTU example
  175. config1.mtu = 1500;
  176. peer1 = createPeer(&config1);
  177. if (!peer1)
  178. goto error;
  179. // Create peer 2
  180. rtcConfiguration config2;
  181. memset(&config2, 0, sizeof(config2));
  182. // STUN server example (not necessary to connect locally)
  183. // Please do not use outside of libdatachannel tests
  184. config2.iceServers = iceServers;
  185. config2.iceServersCount = 1;
  186. // Custom MTU example
  187. config2.mtu = 1500;
  188. // Port range example
  189. config2.portRangeBegin = 5000;
  190. config2.portRangeEnd = 6000;
  191. peer2 = createPeer(&config2);
  192. if (!peer2)
  193. goto error;
  194. // Peer 1: Create data channel
  195. rtcDataChannelInit init;
  196. memset(&init, 0, sizeof(init));
  197. init.protocol = "protocol";
  198. init.reliability.unordered = true;
  199. peer1->dc = rtcCreateDataChannelEx(peer1->pc, "test", &init);
  200. rtcSetOpenCallback(peer1->dc, openCallback);
  201. rtcSetClosedCallback(peer1->dc, closedCallback);
  202. rtcSetMessageCallback(peer1->dc, messageCallback);
  203. attempts = 10;
  204. while ((!peer2->connected || !peer1->connected) && attempts--)
  205. sleep(1);
  206. if (peer1->state != RTC_CONNECTED || peer2->state != RTC_CONNECTED) {
  207. fprintf(stderr, "PeerConnection is not connected\n");
  208. goto error;
  209. }
  210. if (!peer1->connected || !peer2->connected) {
  211. fprintf(stderr, "DataChannel is not connected\n");
  212. goto error;
  213. }
  214. char buffer[BUFFER_SIZE];
  215. char buffer2[BUFFER_SIZE];
  216. if (rtcGetLocalDescriptionType(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  217. fprintf(stderr, "rtcGetLocalDescriptionType failed\n");
  218. goto error;
  219. }
  220. printf("Local description type 1: %s\n", buffer);
  221. if (rtcGetLocalDescription(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  222. fprintf(stderr, "rtcGetLocalDescription failed\n");
  223. goto error;
  224. }
  225. printf("Local description 1: %s\n", buffer);
  226. if (rtcGetRemoteDescriptionType(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  227. fprintf(stderr, "rtcGetRemoteDescriptionType failed\n");
  228. goto error;
  229. }
  230. printf("Remote description type 1: %s\n", buffer);
  231. if (rtcGetRemoteDescription(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  232. fprintf(stderr, "rtcGetRemoteDescription failed\n");
  233. goto error;
  234. }
  235. printf("Remote description 1: %s\n", buffer);
  236. if (rtcGetLocalDescriptionType(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  237. fprintf(stderr, "rtcGetLocalDescriptionType failed\n");
  238. goto error;
  239. }
  240. printf("Local description type 2: %s\n", buffer);
  241. if (rtcGetLocalDescription(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  242. fprintf(stderr, "rtcGetLocalDescription failed\n");
  243. goto error;
  244. }
  245. printf("Local description 2: %s\n", buffer);
  246. if (rtcGetRemoteDescriptionType(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  247. fprintf(stderr, "rtcGetRemoteDescriptionType failed\n");
  248. goto error;
  249. }
  250. printf("Remote description type 2: %s\n", buffer);
  251. if (rtcGetRemoteDescription(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  252. fprintf(stderr, "rtcGetRemoteDescription failed\n");
  253. goto error;
  254. }
  255. printf("Remote description 2: %s\n", buffer);
  256. if (rtcGetLocalAddress(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  257. fprintf(stderr, "rtcGetLocalAddress failed\n");
  258. goto error;
  259. }
  260. printf("Local address 1: %s\n", buffer);
  261. if (rtcGetRemoteAddress(peer1->pc, buffer, BUFFER_SIZE) < 0) {
  262. fprintf(stderr, "rtcGetRemoteAddress failed\n");
  263. goto error;
  264. }
  265. printf("Remote address 1: %s\n", buffer);
  266. if (rtcGetLocalAddress(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  267. fprintf(stderr, "rtcGetLocalAddress failed\n");
  268. goto error;
  269. }
  270. printf("Local address 2: %s\n", buffer);
  271. if (rtcGetRemoteAddress(peer2->pc, buffer, BUFFER_SIZE) < 0) {
  272. fprintf(stderr, "rtcGetRemoteAddress failed\n");
  273. goto error;
  274. }
  275. printf("Remote address 2: %s\n", buffer);
  276. if (rtcGetSelectedCandidatePair(peer1->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) {
  277. fprintf(stderr, "rtcGetSelectedCandidatePair failed\n");
  278. goto error;
  279. }
  280. printf("Local candidate 1: %s\n", buffer);
  281. printf("Remote candidate 1: %s\n", buffer2);
  282. if (rtcGetSelectedCandidatePair(peer2->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) {
  283. fprintf(stderr, "rtcGetSelectedCandidatePair failed\n");
  284. goto error;
  285. }
  286. printf("Local candidate 2: %s\n", buffer);
  287. printf("Remote candidate 2: %s\n", buffer2);
  288. if (rtcGetMaxDataChannelStream(peer1->pc) <= 0 || rtcGetMaxDataChannelStream(peer2->pc) <= 0) {
  289. fprintf(stderr, "rtcGetMaxDataChannelStream failed\n");
  290. goto error;
  291. }
  292. deletePeer(peer1);
  293. sleep(1);
  294. deletePeer(peer2);
  295. sleep(1);
  296. printf("Success\n");
  297. return 0;
  298. error:
  299. deletePeer(peer1);
  300. deletePeer(peer2);
  301. return -1;
  302. }
  303. #include <stdexcept>
  304. void test_capi_connectivity() {
  305. if (test_capi_connectivity_main())
  306. throw std::runtime_error("Connection failed");
  307. }