turn_connectivity.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_turn_connectivity() {
  28. InitLogger(LogLevel::Debug);
  29. Configuration config1;
  30. config1.iceTransportPolicy = TransportPolicy::Relay; // force relay
  31. // STUN server example (not necessary, just here for testing)
  32. // Please do not use outside of libdatachannel tests
  33. config1.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  34. // TURN server example
  35. // Please do not use outside of libdatachannel tests
  36. config1.iceServers.emplace_back("turn:datachannel_test:[email protected]:3478");
  37. PeerConnection pc1(config1);
  38. Configuration config2;
  39. config2.iceTransportPolicy = TransportPolicy::Relay; // force relay
  40. // STUN server example (not necessary, just here for testing)
  41. // Please do not use outside of libdatachannel tests
  42. config1.iceServers.emplace_back("stun:stun.ageneau.net:3478");
  43. // TURN server example
  44. // Please do not use outside of libdatachannel tests
  45. config2.iceServers.emplace_back("turn:datachannel_test:[email protected]:3478");
  46. PeerConnection pc2(config2);
  47. pc1.onLocalDescription([&pc2](Description sdp) {
  48. cout << "Description 1: " << sdp << endl;
  49. pc2.setRemoteDescription(string(sdp));
  50. });
  51. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  52. cout << "Candidate 1: " << candidate << endl;
  53. pc2.addRemoteCandidate(string(candidate));
  54. });
  55. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  56. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  57. cout << "Gathering state 1: " << state << endl;
  58. });
  59. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  60. cout << "Signaling state 1: " << state << endl;
  61. });
  62. pc2.onLocalDescription([&pc1](Description sdp) {
  63. cout << "Description 2: " << sdp << endl;
  64. pc1.setRemoteDescription(string(sdp));
  65. });
  66. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  67. cout << "Candidate 2: " << candidate << endl;
  68. pc1.addRemoteCandidate(string(candidate));
  69. });
  70. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  71. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  72. cout << "Gathering state 2: " << state << endl;
  73. });
  74. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  75. cout << "Signaling state 2: " << state << endl;
  76. });
  77. shared_ptr<DataChannel> dc2;
  78. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  79. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  80. if (dc->label() != "test") {
  81. cerr << "Wrong DataChannel label" << endl;
  82. return;
  83. }
  84. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  85. if (auto dc = wdc.lock())
  86. dc->send("Hello from 2");
  87. });
  88. dc->onMessage([](variant<binary, string> message) {
  89. if (holds_alternative<string>(message)) {
  90. cout << "Message 2: " << get<string>(message) << endl;
  91. }
  92. });
  93. std::atomic_store(&dc2, dc);
  94. });
  95. auto dc1 = pc1.createDataChannel("test");
  96. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  97. auto dc1 = wdc1.lock();
  98. if (!dc1)
  99. return;
  100. cout << "DataChannel 1: Open" << endl;
  101. dc1->send("Hello from 1");
  102. });
  103. dc1->onMessage([](const variant<binary, string> &message) {
  104. if (holds_alternative<string>(message)) {
  105. cout << "Message 1: " << get<string>(message) << endl;
  106. }
  107. });
  108. // Wait a bit
  109. int attempts = 10;
  110. shared_ptr<DataChannel> adc2;
  111. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  112. this_thread::sleep_for(1s);
  113. if (pc1.state() != PeerConnection::State::Connected &&
  114. pc2.state() != PeerConnection::State::Connected)
  115. throw runtime_error("PeerConnection is not connected");
  116. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  117. throw runtime_error("DataChannel is not open");
  118. if (auto addr = pc1.localAddress())
  119. cout << "Local address 1: " << *addr << endl;
  120. if (auto addr = pc1.remoteAddress())
  121. cout << "Remote address 1: " << *addr << endl;
  122. if (auto addr = pc2.localAddress())
  123. cout << "Local address 2: " << *addr << endl;
  124. if (auto addr = pc2.remoteAddress())
  125. cout << "Remote address 2: " << *addr << endl;
  126. Candidate local, remote;
  127. if (!pc1.getSelectedCandidatePair(&local, &remote))
  128. throw runtime_error("getSelectedCabndidatePair failed");
  129. cout << "Local candidate 1: " << local << endl;
  130. cout << "Remote candidate 1: " << remote << endl;
  131. if(local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
  132. throw runtime_error("Connection is not relayed as expected");
  133. if (!pc2.getSelectedCandidatePair(&local, &remote))
  134. throw runtime_error("getSelectedCabndidatePair failed");
  135. cout << "Local candidate 2: " << local << endl;
  136. cout << "Remote candidate 2: " << remote << endl;
  137. if(local.type() != Candidate::Type::Relayed || remote.type() != Candidate::Type::Relayed)
  138. throw runtime_error("Connection is not relayed as expected");
  139. // Try to open a second data channel with another label
  140. shared_ptr<DataChannel> second2;
  141. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  142. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  143. if (dc->label() != "second") {
  144. cerr << "Wrong second DataChannel label" << endl;
  145. return;
  146. }
  147. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  148. if (auto dc = wdc.lock())
  149. dc->send("Second hello from 2");
  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. std::atomic_store(&second2, dc);
  157. });
  158. auto second1 = pc1.createDataChannel("second");
  159. second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
  160. auto second1 = wsecond1.lock();
  161. if (!second1)
  162. return;
  163. cout << "Second DataChannel 1: Open" << endl;
  164. second1->send("Second hello from 1");
  165. });
  166. dc1->onMessage([](const variant<binary, string> &message) {
  167. if (holds_alternative<string>(message)) {
  168. cout << "Second Message 1: " << get<string>(message) << endl;
  169. }
  170. });
  171. // Wait a bit
  172. attempts = 10;
  173. shared_ptr<DataChannel> asecond2;
  174. while (
  175. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  176. attempts--)
  177. this_thread::sleep_for(1s);
  178. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  179. throw runtime_error("Second DataChannel is not open");
  180. // Try to open a negotiated channel
  181. DataChannelInit init;
  182. init.negotiated = true;
  183. init.id = 42;
  184. auto negotiated1 = pc1.createDataChannel("negotiated", init);
  185. auto negotiated2 = pc2.createDataChannel("negoctated", init);
  186. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  187. throw runtime_error("Negotiated DataChannel is not open");
  188. std::atomic<bool> received = false;
  189. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  190. if (holds_alternative<string>(message)) {
  191. cout << "Second Message 2: " << get<string>(message) << endl;
  192. received = true;
  193. }
  194. });
  195. negotiated1->send("Hello from negotiated channel");
  196. // Wait a bit
  197. attempts = 5;
  198. while (!received && attempts--)
  199. this_thread::sleep_for(1s);
  200. if (!received)
  201. throw runtime_error("Negotiated DataChannel failed");
  202. // Delay close of peer 2 to check closing works properly
  203. pc1.close();
  204. this_thread::sleep_for(1s);
  205. pc2.close();
  206. this_thread::sleep_for(1s);
  207. cout << "Success" << endl;
  208. }