turn_connectivity.cpp 7.6 KB

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