2
0

turn_connectivity.cpp 7.0 KB

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