turn_connectivity.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #include "test.hpp"
  10. #include <atomic>
  11. #include <chrono>
  12. #include <iostream>
  13. #include <memory>
  14. #include <thread>
  15. using namespace rtc;
  16. using namespace std;
  17. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  18. TestResult test_turn_connectivity() {
  19. InitLogger(LogLevel::Debug);
  20. Configuration config1;
  21. config1.iceTransportPolicy = TransportPolicy::Relay; // force relay
  22. // TURN server example (use your own server in production)
  23. config1.iceServers.emplace_back(
  24. "turn:openrelayproject:[email protected]:80");
  25. PeerConnection pc1(config1);
  26. Configuration config2;
  27. // STUN server example (use your own server in production)
  28. config2.iceServers.emplace_back("stun:openrelay.metered.ca:80");
  29. PeerConnection pc2(config2);
  30. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  31. pc1.onIceStateChange(
  32. [](PeerConnection::IceState state) { cout << "ICE state 1: " << state << endl; });
  33. pc1.onGatheringStateChange([&pc1, &pc2](PeerConnection::GatheringState state) {
  34. cout << "Gathering state 1: " << state << endl;
  35. if (state == PeerConnection::GatheringState::Complete) {
  36. auto sdp = pc1.localDescription().value();
  37. cout << "Description 1: " << sdp << endl;
  38. pc2.setRemoteDescription(string(sdp));
  39. }
  40. });
  41. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  42. cout << "Signaling state 1: " << state << endl;
  43. });
  44. pc2.onLocalDescription([&pc1](Description sdp) {
  45. cout << "Description 2: " << sdp << endl;
  46. pc1.setRemoteDescription(string(sdp));
  47. });
  48. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  49. // Filter server reflexive candidates
  50. if (candidate.type() != rtc::Candidate::Type::ServerReflexive)
  51. return;
  52. cout << "Candidate 2: " << candidate << endl;
  53. pc1.addRemoteCandidate(string(candidate));
  54. });
  55. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  56. pc2.onIceStateChange(
  57. [](PeerConnection::IceState state) { cout << "ICE state 2: " << state << endl; });
  58. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  59. cout << "Gathering state 2: " << state << endl;
  60. });
  61. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  62. cout << "Signaling state 2: " << state << endl;
  63. });
  64. shared_ptr<DataChannel> dc2;
  65. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  66. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  67. if (dc->label() != "test") {
  68. cerr << "Wrong DataChannel label" << endl;
  69. return;
  70. }
  71. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  72. if (auto dc = wdc.lock()) {
  73. cout << "DataChannel 2: Open" << endl;
  74. dc->send("Hello from 2");
  75. }
  76. });
  77. dc->onMessage([](variant<binary, string> message) {
  78. if (holds_alternative<string>(message)) {
  79. cout << "Message 2: " << get<string>(message) << endl;
  80. }
  81. });
  82. std::atomic_store(&dc2, dc);
  83. });
  84. auto dc1 = pc1.createDataChannel("test");
  85. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  86. auto dc1 = wdc1.lock();
  87. if (!dc1)
  88. return;
  89. cout << "DataChannel 1: Open" << endl;
  90. dc1->send("Hello from 1");
  91. });
  92. dc1->onClosed([]() { cout << "DataChannel 1: Closed" << endl; });
  93. dc1->onMessage([](const variant<binary, string> &message) {
  94. if (holds_alternative<string>(message)) {
  95. cout << "Message 1: " << get<string>(message) << endl;
  96. }
  97. });
  98. // Wait a bit
  99. int attempts = 10;
  100. shared_ptr<DataChannel> adc2;
  101. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  102. this_thread::sleep_for(1s);
  103. if (pc1.state() != PeerConnection::State::Connected ||
  104. pc2.state() != PeerConnection::State::Connected)
  105. return TestResult(false, "PeerConnection is not connected");
  106. if ((pc1.iceState() != PeerConnection::IceState::Connected &&
  107. pc1.iceState() != PeerConnection::IceState::Completed) ||
  108. (pc2.iceState() != PeerConnection::IceState::Connected &&
  109. pc2.iceState() != PeerConnection::IceState::Completed))
  110. return TestResult(false, "ICE is not connected");
  111. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  112. return TestResult(false, "DataChannel is not open");
  113. if (auto addr = pc1.localAddress())
  114. cout << "Local address 1: " << *addr << endl;
  115. if (auto addr = pc1.remoteAddress())
  116. cout << "Remote address 1: " << *addr << endl;
  117. if (auto addr = pc2.localAddress())
  118. cout << "Local address 2: " << *addr << endl;
  119. if (auto addr = pc2.remoteAddress())
  120. cout << "Remote address 2: " << *addr << endl;
  121. Candidate local, remote;
  122. if (!pc1.getSelectedCandidatePair(&local, &remote))
  123. return TestResult(false, "getSelectedCandidatePair failed");
  124. cout << "Local candidate 1: " << local << endl;
  125. cout << "Remote candidate 1: " << remote << endl;
  126. if (local.type() != Candidate::Type::Relayed)
  127. return TestResult(false, "Connection is not relayed as expected");
  128. // Try to open a second data channel with another label
  129. shared_ptr<DataChannel> second2;
  130. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  131. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  132. if (dc->label() != "second") {
  133. cerr << "Wrong second DataChannel label" << endl;
  134. return;
  135. }
  136. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  137. if (auto dc = wdc.lock())
  138. dc->send("Second hello from 2");
  139. });
  140. dc->onMessage([](variant<binary, string> message) {
  141. if (holds_alternative<string>(message)) {
  142. cout << "Second Message 2: " << get<string>(message) << endl;
  143. }
  144. });
  145. std::atomic_store(&second2, dc);
  146. });
  147. auto second1 = pc1.createDataChannel("second");
  148. second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
  149. auto second1 = wsecond1.lock();
  150. if (!second1)
  151. return;
  152. cout << "Second DataChannel 1: Open" << endl;
  153. second1->send("Second hello from 1");
  154. });
  155. second1->onClosed([]() { cout << "Second DataChannel 1: Closed" << endl; });
  156. second1->onMessage([](const variant<binary, string> &message) {
  157. if (holds_alternative<string>(message)) {
  158. cout << "Second Message 1: " << get<string>(message) << endl;
  159. }
  160. });
  161. // Wait a bit
  162. attempts = 10;
  163. shared_ptr<DataChannel> asecond2;
  164. while (
  165. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  166. attempts--)
  167. this_thread::sleep_for(1s);
  168. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  169. return TestResult(false, "Second DataChannel is not open");
  170. // Try to open a negotiated channel
  171. DataChannelInit init;
  172. init.negotiated = true;
  173. init.id = 42;
  174. auto negotiated1 = pc1.createDataChannel("negotiated", init);
  175. auto negotiated2 = pc2.createDataChannel("negoctated", init);
  176. if (!negotiated1->isOpen() || !negotiated2->isOpen())
  177. return TestResult(false, "Negotiated DataChannel is not open");
  178. std::atomic<bool> received = false;
  179. negotiated2->onMessage([&received](const variant<binary, string> &message) {
  180. if (holds_alternative<string>(message)) {
  181. cout << "Second Message 2: " << get<string>(message) << endl;
  182. received = true;
  183. }
  184. });
  185. negotiated1->send("Hello from negotiated channel");
  186. // Wait a bit
  187. attempts = 5;
  188. while (!received && attempts--)
  189. this_thread::sleep_for(1s);
  190. if (!received)
  191. return TestResult(false, "Negotiated DataChannel failed");
  192. // Delay close of peer 2 to check closing works properly
  193. pc1.close();
  194. this_thread::sleep_for(1s);
  195. pc2.close();
  196. this_thread::sleep_for(1s);
  197. return TestResult(true);
  198. }