connectivity.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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
  31. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  32. auto pc1 = std::make_shared<PeerConnection>(config1);
  33. Configuration config2;
  34. // STUN server example
  35. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  36. // Port range example
  37. config2.portRangeBegin = 5000;
  38. config2.portRangeEnd = 6000;
  39. auto pc2 = std::make_shared<PeerConnection>(config2);
  40. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
  41. auto pc2 = wpc2.lock();
  42. if (!pc2)
  43. return;
  44. cout << "Description 1: " << sdp << endl;
  45. pc2->setRemoteDescription(string(sdp));
  46. });
  47. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
  48. auto pc2 = wpc2.lock();
  49. if (!pc2)
  50. return;
  51. cout << "Candidate 1: " << candidate << endl;
  52. pc2->addRemoteCandidate(string(candidate));
  53. });
  54. pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  55. pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
  56. cout << "Gathering state 1: " << state << endl;
  57. });
  58. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
  59. auto pc1 = wpc1.lock();
  60. if (!pc1)
  61. return;
  62. cout << "Description 2: " << sdp << endl;
  63. pc1->setRemoteDescription(string(sdp));
  64. });
  65. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
  66. auto pc1 = wpc1.lock();
  67. if (!pc1)
  68. return;
  69. cout << "Candidate 2: " << candidate << endl;
  70. pc1->addRemoteCandidate(string(candidate));
  71. });
  72. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  73. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  74. cout << "Gathering state 2: " << state << endl;
  75. });
  76. shared_ptr<DataChannel> dc2;
  77. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  78. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  79. if (dc->label() != "test") {
  80. cerr << "Wrong DataChannel label" << endl;
  81. return;
  82. }
  83. dc->onMessage([](variant<binary, string> message) {
  84. if (holds_alternative<string>(message)) {
  85. cout << "Message 2: " << get<string>(message) << endl;
  86. }
  87. });
  88. dc->send("Hello from 2");
  89. std::atomic_store(&dc2, dc);
  90. });
  91. auto dc1 = pc1->createDataChannel("test");
  92. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  93. auto dc1 = wdc1.lock();
  94. if (!dc1)
  95. return;
  96. cout << "DataChannel 1: Open" << endl;
  97. dc1->send("Hello from 1");
  98. });
  99. dc1->onMessage([](const variant<binary, string> &message) {
  100. if (holds_alternative<string>(message)) {
  101. cout << "Message 1: " << get<string>(message) << endl;
  102. }
  103. });
  104. // Wait a bit
  105. int attempts = 10;
  106. shared_ptr<DataChannel> adc2;
  107. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  108. this_thread::sleep_for(1s);
  109. if (pc1->state() != PeerConnection::State::Connected &&
  110. pc2->state() != PeerConnection::State::Connected)
  111. throw runtime_error("PeerConnection is not connected");
  112. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  113. throw runtime_error("DataChannel is not open");
  114. if (auto addr = pc1->localAddress())
  115. cout << "Local address 1: " << *addr << endl;
  116. if (auto addr = pc1->remoteAddress())
  117. cout << "Remote address 1: " << *addr << endl;
  118. if (auto addr = pc2->localAddress())
  119. cout << "Local address 2: " << *addr << endl;
  120. if (auto addr = pc2->remoteAddress())
  121. cout << "Remote address 2: " << *addr << endl;
  122. Candidate local, remote;
  123. if(pc1->getSelectedCandidatePair(&local, &remote)) {
  124. cout << "Local candidate 1: " << local << endl;
  125. cout << "Remote candidate 1: " << remote << endl;
  126. }
  127. if(pc2->getSelectedCandidatePair(&local, &remote)) {
  128. cout << "Local candidate 2: " << local << endl;
  129. cout << "Remote candidate 2: " << remote << endl;
  130. }
  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->onMessage([](variant<binary, string> message) {
  140. if (holds_alternative<string>(message)) {
  141. cout << "Second Message 2: " << get<string>(message) << endl;
  142. }
  143. });
  144. dc->send("Send hello from 2");
  145. std::atomic_store(&second2, dc);
  146. });
  147. auto second1 = pc1->createDataChannel("second");
  148. second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
  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. dc1->onMessage([](const variant<binary, string> &message) {
  156. if (holds_alternative<string>(message)) {
  157. cout << "Second Message 1: " << get<string>(message) << endl;
  158. }
  159. });
  160. // Wait a bit
  161. attempts = 10;
  162. shared_ptr<DataChannel> asecond2;
  163. while (
  164. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  165. attempts--)
  166. this_thread::sleep_for(1s);
  167. // Delay close of peer 2 to check closing works properly
  168. pc1->close();
  169. this_thread::sleep_for(1s);
  170. pc2->close();
  171. this_thread::sleep_for(1s);
  172. // You may call rtc::Cleanup() when finished to free static resources
  173. rtc::Cleanup();
  174. this_thread::sleep_for(2s);
  175. cout << "Success" << endl;
  176. }