connectivity.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. using namespace rtc;
  25. using namespace std;
  26. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  27. void test_connectivity() {
  28. InitLogger(LogLevel::Debug);
  29. Configuration config1;
  30. // STUN server example (not necessary to connect locally)
  31. // Please do not use outside of libdatachannel tests
  32. config1.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  33. // Custom MTU example
  34. config1.mtu = 1500;
  35. auto pc1 = std::make_shared<PeerConnection>(config1);
  36. Configuration config2;
  37. // STUN server example (not necessary to connect locally)
  38. // Please do not use outside of libdatachannel tests
  39. config2.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  40. // Custom MTU example
  41. config2.mtu = 1500;
  42. // Port range example
  43. config2.portRangeBegin = 5000;
  44. config2.portRangeEnd = 6000;
  45. auto pc2 = std::make_shared<PeerConnection>(config2);
  46. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
  47. auto pc2 = wpc2.lock();
  48. if (!pc2)
  49. return;
  50. cout << "Description 1: " << sdp << endl;
  51. pc2->setRemoteDescription(string(sdp));
  52. });
  53. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
  54. auto pc2 = wpc2.lock();
  55. if (!pc2)
  56. return;
  57. cout << "Candidate 1: " << candidate << endl;
  58. pc2->addRemoteCandidate(string(candidate));
  59. });
  60. pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  61. pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
  62. cout << "Gathering state 1: " << state << endl;
  63. });
  64. pc1->onSignalingStateChange([](PeerConnection::SignalingState state) {
  65. cout << "Signaling state 1: " << state << endl;
  66. });
  67. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
  68. auto pc1 = wpc1.lock();
  69. if (!pc1)
  70. return;
  71. cout << "Description 2: " << sdp << endl;
  72. pc1->setRemoteDescription(string(sdp));
  73. });
  74. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
  75. auto pc1 = wpc1.lock();
  76. if (!pc1)
  77. return;
  78. cout << "Candidate 2: " << candidate << endl;
  79. pc1->addRemoteCandidate(string(candidate));
  80. });
  81. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  82. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  83. cout << "Gathering state 2: " << state << endl;
  84. });
  85. pc2->onSignalingStateChange([](PeerConnection::SignalingState state) {
  86. cout << "Signaling state 2: " << state << endl;
  87. });
  88. shared_ptr<DataChannel> dc2;
  89. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  90. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  91. if (dc->label() != "test") {
  92. cerr << "Wrong DataChannel label" << endl;
  93. return;
  94. }
  95. dc->onMessage([](variant<binary, string> message) {
  96. if (holds_alternative<string>(message)) {
  97. cout << "Message 2: " << get<string>(message) << endl;
  98. }
  99. });
  100. dc->send("Hello from 2");
  101. std::atomic_store(&dc2, dc);
  102. });
  103. auto dc1 = pc1->createDataChannel("test");
  104. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  105. auto dc1 = wdc1.lock();
  106. if (!dc1)
  107. return;
  108. cout << "DataChannel 1: Open" << endl;
  109. dc1->send("Hello from 1");
  110. });
  111. dc1->onMessage([](const variant<binary, string> &message) {
  112. if (holds_alternative<string>(message)) {
  113. cout << "Message 1: " << get<string>(message) << endl;
  114. }
  115. });
  116. // Wait a bit
  117. int attempts = 10;
  118. shared_ptr<DataChannel> adc2;
  119. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  120. this_thread::sleep_for(1s);
  121. if (pc1->state() != PeerConnection::State::Connected &&
  122. pc2->state() != PeerConnection::State::Connected)
  123. throw runtime_error("PeerConnection is not connected");
  124. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  125. throw runtime_error("DataChannel is not open");
  126. if (auto addr = pc1->localAddress())
  127. cout << "Local address 1: " << *addr << endl;
  128. if (auto addr = pc1->remoteAddress())
  129. cout << "Remote address 1: " << *addr << endl;
  130. if (auto addr = pc2->localAddress())
  131. cout << "Local address 2: " << *addr << endl;
  132. if (auto addr = pc2->remoteAddress())
  133. cout << "Remote address 2: " << *addr << endl;
  134. Candidate local, remote;
  135. if (pc1->getSelectedCandidatePair(&local, &remote)) {
  136. cout << "Local candidate 1: " << local << endl;
  137. cout << "Remote candidate 1: " << remote << endl;
  138. }
  139. if (pc2->getSelectedCandidatePair(&local, &remote)) {
  140. cout << "Local candidate 2: " << local << endl;
  141. cout << "Remote candidate 2: " << remote << endl;
  142. }
  143. // Try to open a second data channel with another label
  144. shared_ptr<DataChannel> second2;
  145. pc2->onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  146. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  147. if (dc->label() != "second") {
  148. cerr << "Wrong second DataChannel label" << endl;
  149. return;
  150. }
  151. dc->onMessage([](variant<binary, string> message) {
  152. if (holds_alternative<string>(message)) {
  153. cout << "Second Message 2: " << get<string>(message) << endl;
  154. }
  155. });
  156. dc->send("Send hello from 2");
  157. std::atomic_store(&second2, dc);
  158. });
  159. auto second1 = pc1->createDataChannel("second");
  160. second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
  161. auto second1 = wsecond1.lock();
  162. if (!second1)
  163. return;
  164. cout << "Second DataChannel 1: Open" << endl;
  165. second1->send("Second hello from 1");
  166. });
  167. dc1->onMessage([](const variant<binary, string> &message) {
  168. if (holds_alternative<string>(message)) {
  169. cout << "Second Message 1: " << get<string>(message) << endl;
  170. }
  171. });
  172. // Wait a bit
  173. attempts = 10;
  174. shared_ptr<DataChannel> asecond2;
  175. while (
  176. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  177. attempts--)
  178. this_thread::sleep_for(1s);
  179. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  180. throw runtime_error("Second DataChannel is not open");
  181. // Try to open a negotiated channel
  182. DataChannelInit init;
  183. init.negotiated = true;
  184. init.id = 42;
  185. auto negotiated1 = pc1->createDataChannel("negotiated", init);
  186. auto negotiated2 = pc2->createDataChannel("negoctated", init);
  187. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  188. throw runtime_error("Negotiated DataChannel is not open");
  189. std::atomic<bool> received = false;
  190. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  191. if (holds_alternative<string>(message)) {
  192. cout << "Second Message 2: " << get<string>(message) << endl;
  193. received = true;
  194. }
  195. });
  196. negotiated1->send("Hello from negotiated channel");
  197. // Wait a bit
  198. attempts = 5;
  199. while (!received && attempts--)
  200. this_thread::sleep_for(1s);
  201. if (!received)
  202. throw runtime_error("Negotiated DataChannel failed");
  203. // Delay close of peer 2 to check closing works properly
  204. pc1->close();
  205. this_thread::sleep_for(1s);
  206. pc2->close();
  207. this_thread::sleep_for(1s);
  208. // You may call rtc::Cleanup() when finished to free static resources
  209. rtc::Cleanup();
  210. this_thread::sleep_for(1s);
  211. cout << "Success" << endl;
  212. }